Class: RSQL::RSQL

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/rsql/rsql.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.execute(command) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rsql/rsql.rb', line 16

def execute(command)
  # split command into POSIX tokens
  if args = shellwords(command)
    # extract the actual command name
    method_name = args.shift
    # make sure there was a command
    unless method_name.nil?
      # if the command name corresponds to a method
      if instance.respond_to?(method_name)
        # invoke it
        instance.send(method_name, *args)
      else
        # otherwise, hand it off to ODBC
        instance.send :execute, command
      end
    end
  end
rescue => error
  puts "ERROR: #{error}"
end

Instance Method Details

#commitObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rsql/rsql.rb', line 78

def commit
  unless @database.nil?
    begin
      @database.commit
    rescue
      puts "ERROR: Commit failed"
      raise
    end
  else
    puts "No dsn selected"
  end
end

#describe(table, column = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rsql/rsql.rb', line 91

def describe(table, column = nil)
  unless @database.nil?
    begin
      if column
        statement = @database.columns(table, column)
      else
        statement = @database.columns(table)
      end
    rescue
      raise
    else
      begin
        statement.print :COLUMN_NAME => 'Field', :TYPE_NAME => 'Type', :COLUMN_SIZE => 'Size', :IS_NULLABLE => 'Null', :COLUMN_DEF => 'Default'
      rescue
        raise
      ensure
        statement.drop
      end
    end
  else
    puts "No dsn selected"
  end
end

#helpObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/rsql/rsql.rb', line 115

def help
  puts "COMMIT"
  puts "DESCRIBE table"
  puts "HELP"
  puts "QUIT"
  puts "ROLLBACK"
  puts "SET AUTOCOMMIT=value"
  puts "SHOW TABLES [LIKE pattern]"
  puts "USE dsn"
end

#quitObject



126
127
128
# File 'lib/rsql/rsql.rb', line 126

def quit
  exit
end

#rollbackObject



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/rsql/rsql.rb', line 130

def rollback
  unless @database.nil?
    begin
      @database.rollback
    rescue
      puts "ERROR: Rollback failed"
      raise
    end
  else
    puts "No dsn selected"
  end
end

#set(expression) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rsql/rsql.rb', line 143

def set(expression)
  unless @database.nil?
    matches = /([a-z]*)=(.*)/.match(expression)
    if matches
      variable,value = matches[1,2]
      case variable.downcase
      when "autocommit"
        begin
          @database.autocommit = (1.coerce(value)[0] != 0)
        rescue ArgumentError
          raise(StandardError, "Variable '#{variable}' can't be set to the value of '#{value}'")
        else
          puts "autocommit set to #{@database.autocommit}"
        end
      else
        raise(StandardError, "Unknown system variable '#{variable}'")
      end
    else
      raise(StandardError, "Syntax error at '#{expression}'")
    end
  else
    puts "No dsn selected"
  end
end

#show(what, *args) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/rsql/rsql.rb', line 168

def show(what, *args)
  unless @database.nil?
    case what.downcase
    when 'tables'
      source = @dsn
      begin
        if opt = args.shift
          if opt.downcase == 'like'
            if pattern = args.shift
              statement = @database.tables(pattern)
              source += " (#{pattern})"
            else
              raise(StandardError, "Missing pattern after '#{opt}'")
            end
          else
            raise(StandardError, "Syntax error at '#{opt}'")
          end
        else
          statement = @database.tables
        end
      rescue
        raise
      else
        unless statement.nil?
          begin
            statement.print :TABLE_NAME => "Tables_in_#{source}"
          rescue
            raise
          ensure
            statement.drop
          end
        else
          puts "No tables to show"
        end
      end
    else
      raise(StandardError, "Syntax error at '#{what}'")
    end
  else
    puts "No dsn selected"
  end
end

#use(dsn) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/rsql/rsql.rb', line 211

def use(dsn)
  begin
    unless @database.nil?
      begin
        if @database.connected?
          # in case of pending transaction
          @database.rollback unless @database.autocommit
          @database.disconnect
        end
      rescue
      end
    end
    @dsn = dsn
    @database = ODBC.connect(@dsn, OPTIONS[:user], OPTIONS[:password])
  rescue
    puts "ERROR: Unable to connect to DSN '#{@dsn}' as user '#{OPTIONS[:user]}'"
    @database = nil
    @dsn = nil
    raise
  else
    puts "Database changed" unless OPTIONS[:quiet]
  end
end