1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
# Copyright (C) 2003 Laurent Sansonetti <lrz@gnome.org>
#
# This file is part of Ruby/Libgda.
#
# Ruby/Libgda is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# Ruby/Libgda is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with Ruby/Libgda; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
require 'libgda'
require 'readline'
include Readline
def err(s)
$stderr.puts s
exit 1
end
def sql_execute(conn, text)
cmd = Gda::Command.new(text,
Gda::Command::TYPE_SQL,
Gda::Command::OPTION_STOP_ON_ERRORS)
out = ""
if datamod = conn.execute_single_command(cmd)
datamod.each_column { |title| out += title + "\t" }
out += "\n"
datamod.each_row do |row|
row.each_value { |val| out += val.to_s + "\t" }
out += "\n"
end
else
conn.errors.each do |err|
out += "Error #{err.number.to_s}\n" \
+ "Description: #{err.description}\n" \
+ "Source: #{err.source}\n" \
+ "SQL state: #{err.sqlstate}\n"
end
end
out.strip
end
def do_stuff(ds)
client = Gda::Client.new
client.open_connection(ds.name, ds.username, ds.password) do |conn|
puts "Connected."
while line = readline("> ", true) do
raise Interrupt if line.downcase =~ /^(exit|quit)$/
next if line.strip.empty?
puts sql_execute(conn, line)
end
end
end
Gda.init(__FILE__, '0.0.1')
err "Usage: #{__FILE__} [dsn]" unless ARGV.length == 1
datasource = Gda::DataSource.find(ARGV.first)
err "Could not find datasource '#{ARGV.first}'" unless datasource
begin
Gda.main { do_stuff(datasource) }
rescue Interrupt
Gda.main_quit
rescue => e
err "Exception: #{e.message}"
end
|