Class: Db2c::Command
Constant Summary
collapse
- @@cdb =
''
Constants included
from CONSTANTS
Db2c::CONSTANTS::DTORDER, Db2c::CONSTANTS::DTSELECT
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(input) ⇒ Command
Returns a new instance of Command.
7
8
9
10
11
12
13
|
# File 'lib/db2c/command.rb', line 7
def initialize input
if input
puts "initializing: #{input}" if $DB2CDBG
@input = input.chomp.strip.gsub(/^db2 /i,'')
parse unless @input =~ /^(select|update|delete|insert)/i
end
end
|
Class Method Details
.execute(command) ⇒ Object
96
97
98
|
# File 'lib/db2c/command.rb', line 96
def self.execute command
new(command).execute
end
|
.prompt ⇒ Object
89
90
91
92
93
94
|
# File 'lib/db2c/command.rb', line 89
def self.prompt
"db2c".tap do |pr|
pr << "(#{@@cdb})" if @@cdb.length > 0
pr << " => "
end
end
|
Instance Method Details
#execute ⇒ Object
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/db2c/command.rb', line 78
def execute
puts "executing: #{@input}" if $DB2CDBG
system 'db2', @input if valid?
if @input =~ /^connect to (.*)$/i
@@cdb = $1.downcase
end
if @input =~ /^disconnect #{@@cdb}$/i || @input =~ /^connect reset$/i
@@cdb = ''
end
end
|
#help? ⇒ Boolean
70
71
72
|
# File 'lib/db2c/command.rb', line 70
def help?
@input =~ /^(help|h|\\help|\\h)$/
end
|
#history? ⇒ Boolean
66
67
68
|
# File 'lib/db2c/command.rb', line 66
def history?
@input =~ /^(history|hist|\\history|\\hist)$/
end
|
#parse ⇒ Object
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
|
# File 'lib/db2c/command.rb', line 19
def parse
@input.gsub! /^use /, 'connect to '
@input.gsub! /^\\d /, 'describe '
if @input =~ /describe [^. ]+\.[^.+ ]+/
@input.gsub! /describe /, 'describe table '
return
end
if @input =~ /^(show|list) databases/ || @input == '\l'
@input = "list database directory"
return
end
if @input =~ /^\\lt ?(\w*)$/
@input = "list tables"
unless $1.empty?
@input += $1 == "all" ? " for all" : " for schema #{$1}"
end
return
end
if @input =~ /^\\d([a|s|t|v]) ?(\w*)$/
@input = DTSELECT
@input += " where type = '#{$1.upcase}'"
@input += " and tabschema = '#{$2.upcase}'" unless $2.empty?
@input += " #{DTORDER}"
return
end
shortcuts
end
|
#prepend(regex, value) ⇒ Object
58
59
60
|
# File 'lib/db2c/command.rb', line 58
def prepend regex, value
@input.insert 0, value if @input =~ regex
end
|
#quit? ⇒ Boolean
62
63
64
|
# File 'lib/db2c/command.rb', line 62
def quit?
@input.nil? || @input =~ /^(exit|quit|\\q|\\quit)$/
end
|
#shortcuts ⇒ Object
52
53
54
55
56
|
# File 'lib/db2c/command.rb', line 52
def shortcuts
prepend /^\-\d+$/, "? sql"
prepend /^\d+$/, "? "
prepend /^current.+$/i, "values "
end
|
#to_s ⇒ Object
15
16
17
|
# File 'lib/db2c/command.rb', line 15
def to_s
@input
end
|
#valid? ⇒ Boolean
74
75
76
|
# File 'lib/db2c/command.rb', line 74
def valid?
!quit? && !history? && !help?
end
|