Class: Database
- Inherits:
-
Object
- Object
- Database
- Defined in:
- lib/cetusql/cli_sqlite.rb
Defined Under Namespace
Classes: ResultSet
Instance Method Summary collapse
- #close ⇒ Object
- #columns(table) ⇒ Object
- #connect(name) ⇒ Object
- #execute_query(sql) ⇒ Object
-
#get_data(sql) ⇒ Object
(also: #sql)
runs sql query and returns an array of arrays.
- #get_metadata(table) ⇒ Object
- #indexed_columns(table) ⇒ Object
-
#initialize(name) ⇒ Database
constructor
A new instance of Database.
- #sql_recent_rows(tablename) ⇒ Object
- #tables ⇒ Object
Constructor Details
Instance Method Details
#close ⇒ Object
281 282 283 284 |
# File 'lib/cetusql/cli_sqlite.rb', line 281 def close @db = nil @tables = nil end |
#columns(table) ⇒ Object
232 233 234 235 236 |
# File 'lib/cetusql/cli_sqlite.rb', line 232 def columns table raise ArgumentError, "#{$0}: table name cannot be nil" unless table columns, ignore = self. table return columns end |
#connect(name) ⇒ Object
221 222 223 224 225 |
# File 'lib/cetusql/cli_sqlite.rb', line 221 def connect name raise ArgumentError, "Database name cannot be nil" unless name @tables = nil @db = SQLite3::Database.new(name) end |
#execute_query(sql) ⇒ Object
270 271 272 273 274 275 276 277 |
# File 'lib/cetusql/cli_sqlite.rb', line 270 def execute_query sql columns, *rows = @db.execute2(sql) content = rows return nil if content.nil? or content[0].nil? datatypes = content[0].types rs = ResultSet.new(content, columns, datatypes) return rs end |
#get_data(sql) ⇒ Object Also known as: sql
runs sql query and returns an array of arrays
262 263 264 265 266 267 268 |
# File 'lib/cetusql/cli_sqlite.rb', line 262 def get_data sql #$log.debug "SQL: #{sql} " columns, *rows = @db.execute2(sql) #$log.debug "XXX COLUMNS #{sql}, #{rows.count} " content = rows return content end |
#get_metadata(table) ⇒ Object
252 253 254 255 256 257 258 259 260 |
# File 'lib/cetusql/cli_sqlite.rb', line 252 def table raise ArgumentError, "#{$0}: table name cannot be nil" unless table sql = "select * from #{table} limit 1" columns, *rows = @db.execute2(sql) content = rows return nil if content.nil? or content[0].nil? datatypes = content[0].types return columns, datatypes end |
#indexed_columns(table) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/cetusql/cli_sqlite.rb', line 237 def indexed_columns table indexes = sql %Q{select sql from sqlite_master where type='index' and tbl_name = "#{table}" } return nil if indexes == [] #pp indexes arr = [] indexes.each do |ind| if ind.first.nil? arr << "auto?" next end m = ind.first.match /\((.*)\)/ arr << m[1] if m end return arr.join(",") end |
#sql_recent_rows(tablename) ⇒ Object
278 279 280 |
# File 'lib/cetusql/cli_sqlite.rb', line 278 def sql_recent_rows tablename sql = "SELECT * from #{tablename} ORDER by rowid DESC LIMIT 100" end |
#tables ⇒ Object
226 227 228 229 230 231 |
# File 'lib/cetusql/cli_sqlite.rb', line 226 def tables return @tables if @tables tables = sql "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name" tables.collect!{|x| x[0] } ## 1.9 hack, but will it run on 1.8 ?? @tables = tables end |