Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
groesser3 committed May 21, 2011
0 parents commit 96f54d2
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
== Version 0.0.1 - 2011-02-28 <[email protected]>
* initial release
20 changes: 20 additions & 0 deletions MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2011 Elias Kugler

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Empty file added README
Empty file.
13 changes: 13 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
= SQLRunner

== Introduction

== Installation

# gem install sqlrunner

== Usage

c:\>sqlrunner


78 changes: 78 additions & 0 deletions bin/sqlrunner
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!ruby
#$:.unshift '../lib' #<< '../lib'

require 'rubygems'
require 'notes_mailer'
require 'sqlrunner'
require 'optparse'


conf = {}
opts = OptionParser.new do |opts|

opts.banner = "Usage: sqlrunner <command> <opts> "
opts.separator ""
opts.separator "Commands: run"
opts.separator ""
opts.separator "Specific options:"

opts.on("-D", "--db-name DATABASE", "ODBC database name ") do |para| conf[:db_name] = para end
opts.on("-U", "--db-user USERNAME", "DB user name") { |para| conf[:db_user]= para }
opts.on("-P", "--db-password PASSWORD", "DB password") { |para| conf[:db_password] = para }
opts.on("-S", "--path PATH_TO_SQL_FILES", "Path to SQL-statements") { |para| conf[:path_to_sql_files] = para }

opts.on("-m", "--mail-to E-MAIL", "Send result to e-mail adress") { |para| conf[:mail_to]= para }

opts.on("-u", "--notes-username USERNAME", "Lotus Notes user name") { |para| conf[:notes_user]=para }
opts.on("-p", "--notes-password PASSWORD", "Lotus Notes password") { |para| conf[:notes_password]= para }
opts.on("-d", "--notes-database DATABASE", "Lotus Notes database (.nsf file)") { |para| conf[:notes_db]= para }
opts.on("-s", "--notes-server SERVER", "Lotus Notes Server") { |para| conf[:notes_server]= para }

opts.on("--dry-run", "Do not Execute SQL statements") { |para| conf[:dry_run] =para }

opts.on("--mock-win32ole", "") { |para| conf[:mock_win32ole]= para }
opts.on("--verbose", "") { |para| conf[:verbose]= para }

opts.separator ""
opts.separator "Example:"
opts.separator " sqlrunner run -D GN3T -U sds -P first -m [email protected] -u user123 -p secret123 -d mail123.nsf "
opts.separator ""
opts.separator "Common options:"

# No argument, shows at tail. This will print an options summary.
opts.on_tail("-?", "--help", "Show this message") do
puts opts
exit
end

opts.on_tail("-v", "--version", "Show version") do
puts "sqlrunner 0.0.1"
exit
end

opts.on_tail "\n(c) 2011 Elias Kugler"

end

opts.parse! ARGV

if defined?Ocra
exit
else
if ARGV.length != 1
puts opts
exit
end
conf[:command] = ARGV[0]
end

# --------------------------------------------------
#
# --------------------------------------------------
if conf[:command] == 'run'
runner = SQLRunner.new(conf)
runner.run
else
puts opts
exit
end
114 changes: 114 additions & 0 deletions lib/sqlrunner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
require 'rubygems'
require 'dbi'
require 'dbi/utils'
require 'notes_mailer'


class SQLRunner
def initialize(conf)
# --------------------------------------------------------------------------------------
# DB-settings
# --------------------------------------------------------------------------------------
@db_id = conf[:db_name]
@db_user = conf[:db_user]
@db_password = conf[:db_password]

# --------------------------------------------------------------------------------------
# SQL-settings
# --------------------------------------------------------------------------------------
@path_to_sql_files = conf[:path_to_sql_files]

# --------------------------------------------------------------------------------------
# Mailsettings
# --------------------------------------------------------------------------------------
@notes_server = conf[:notes_server]
@notes_db = conf[:notes_db]
@notes_user = conf[:notes_user]
@notes_password = conf[:notes_password]

@mail_to = conf[:mail_to]

# --------------------------------------------------------------------------------------
# Other settings
# --------------------------------------------------------------------------------------
@dry_run = conf[:dry_run]
@mock_win32ole = conf[:mock_win32ole]
@verbose = conf[:verbose]

end

def run
error_text = execute_sql
send_mail({:to => @mail_to,
:body => error_text,
:db => @notes_db,
:adress => @notes_server,
:user_name =>@notes_user,
:password =>@notes_password,
:mock_win32ole => @mock_win32ole,
:logger => @verbose,
:debug => @verbose})
end

def execute_sql
# --------------------------------------------------------------------------------------
# Execute SQL statements
# --------------------------------------------------------------------------------------
error_text = ''
error_text << " -------------- DRY RUN --------------\n" if @dry_run
DBI.connect('DBI:ODBC:'+@db_id, @db_user, @db_password) do |dbh|
dbh['AutoCommit']=false
rbfiles = File.join(@path_to_sql_files+"**", "*.sql")
Dir.glob(rbfiles).each do |file|
begin
File.open(file) do |f|
sql = f.readlines.join("\n").to_s
if @dry_run
error_text << "File: #{file}\n"
print '.'
else
begin
sth = dbh.execute(sql)
header = sth.column_names
rows = sth.fetch_all
if rows.size > 0
error_text << "\nFile: #{file} (Anzahl Datensaetze: #{rows.size.to_s}).\n"
DBI::Utils::TableFormatter.ascii(header,rows, :left, :left, 2,1, nil, error_text)
print 'E'
else
print '.'
end
dbh.rollback()
end
end
end
rescue => detail
print 'F'
error_text << "File: #{file} \n"
error_text << detail.backtrace.join("\n")
end
end
end
error_text
end

def send_mail(opts)
# --------------------------------------------------------------------------------------
# Mail Settings
# --------------------------------------------------------------------------------------
Mail.defaults do
delivery_method(Mail::NotesMailer, opts)
end

# --------------------------------------------------------------------------------------
# Mail Versand
# --------------------------------------------------------------------------------------
Mail.deliver do
to opts[:to]
from 'SQLRunner (c) 2011 Elias Kugler'
subject (opts[:subject] || 'Fehler bei Pruefselects')
body "Bitte beachten: \n"+opts[:body]
#add_file File.join(Dir.pwd, 'test_notes_mailer.rb')
end
end
end
33 changes: 33 additions & 0 deletions sqlrunner.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "rubygems"

Gem::Specification.new do |s|
s.name = %q{sqlrunner}
s.version = "0.0.1"
s.authors = ["Elias Kugler"]
s.email = %q{[email protected]}
s.files = Dir["lib/**/*"] + Dir["bin/**/*"] + Dir["spec/**/*"]+ Dir["*.rb"] + ["MIT-LICENSE","sqlrunner.gemspec"]
s.platform = Gem::Platform::RUBY
s.has_rdoc = true
s.extra_rdoc_files = ["README.rdoc", "CHANGELOG.rdoc"]
s.require_paths = ["lib"]
s.summary = %q{SQLRunner }
s.description = %q{Run SQL statements}
s.files.reject! { |fn| fn.include? "CVS" }
s.require_path = "lib"
s.default_executable = %q{sqlrunner}
s.executables = ["sqlrunner"]
#s.homepage = %q{http://}
#s.rubyforge_project = %q{}
s.add_dependency("mail", ">= 2.2.1")
s.add_dependency("log4r", ">=1.0.5")
s.add_dependency("notes_mailer", ">= 0.0.1")
#s.add_dependency("dbi", ">= 0.4.5")
#s.add_dependency("dbd-odbc", ">= 0.2.5")
#s.add_dependency("ruby-odbc")
end


#if $0 == __FILE__
# Gem.manage_gems
# Gem::Builder.new(spec).build
#end

0 comments on commit 96f54d2

Please sign in to comment.