Permalink
Browse files

Start support for psql-like \commands

  • Loading branch information...
onsitedev committed Nov 22, 2011
1 parent f2a4c94 commit e2b695707a0a5f144eeb0bfaed65c3282148240d
Showing with 46 additions and 8 deletions.
  1. +22 −8 lib/db2c/command.rb
  2. +24 −0 spec/db2c/command_spec.rb
View
@@ -8,29 +8,43 @@ def initialize input
if input
puts "initializing: #{input}" if @@debug
@input = input.chomp.strip
- @input.gsub! /^db2 /i, ''
- @input.gsub! /;$/, ''
- @input.gsub! /^use /, 'connect to '
- @input.insert 0, "? sql" if @input =~ /^\-\d+$/
- @input.insert 0, "? " if @input =~ /^\d+$/
- @input.insert 0, "values " if @input =~ /^current.+$/i
+ parse
end
end
def to_s
@input
end
+ def parse
+ @input.gsub! /^db2 /i, ''
+ @input.gsub! /;$/, ''
+ @input.gsub! /^use /, 'connect to '
+ @input.gsub! /^\\d /, 'describe '
+ @input.insert 0, "? sql" if @input =~ /^\-\d+$/
+ @input.insert 0, "? " if @input =~ /^\d+$/
+ @input.insert 0, "values " if @input =~ /^current.+$/i
+
+ if @input =~ /describe [^. ]+\.[^.+ ]+/
+ @input.gsub! /describe /, 'describe table '
+ end
+ end
+
def quit?
- @input.nil? || @input =~ /^(exit|quit)$/
+ @input.nil? || @input =~ /^(exit|quit|\\q|\\quit)$/
+ end
+
+ def history?
+ @input =~ /^(history|hist|\\history|\\hist)$/
end
def valid?
- @input && @input.length > 0
+ !quit? && !history?
end
def execute
puts "executing: #{@input}" if @@debug
+ system 'less ~/.db2c_history' if history?
system 'db2', @input if valid?
if @input =~ /^connect to (.*)$/i
@@cdb = $1.downcase
View
@@ -0,0 +1,24 @@
+require 'db2c'
+
+describe Db2c::Command do
+ it 'strips db2 if any' do
+ Db2c::Command.new('db2 do something').to_s.should == 'do something'
+ Db2c::Command.new('db2 do something').should be_valid
+ end
+ it 'supports use *' do
+ Db2c::Command.new('use somedb').to_s.should == 'connect to somedb'
+ end
+ it 'supports describe tablei_name' do
+ Db2c::Command.new('describe schema.table').to_s.should == 'describe table schema.table'
+ end
+ it 'supports \d tale_name' do
+ Db2c::Command.new('\d schema.table').to_s.should == 'describe table schema.table'
+ end
+ it 'supports \q' do
+ Db2c::Command.new('\q').should be_quit
+ Db2c::Command.new('\quit').should be_quit
+ end
+ it 'supports history command' do
+ Db2c::Command.new('history').should be_history
+ end
+end

0 comments on commit e2b6957

Please sign in to comment.