Class: Insight::Database::Table
- Inherits:
-
Object
- Object
- Insight::Database::Table
- Includes:
- Logging
- Defined in:
- lib/insight/database.rb
Direct Known Subclasses
Instance Method Summary (collapse)
- - (Object) create_keys_clause
- - (Object) create_sql
- - (Object) db
- - (Object) execute(*args)
- - (Object) fields_sql
-
- (Table) initialize(table_name, *keys)
constructor
A new instance of Table.
- - (Object) insert(values_sql)
- - (Object) keys(name)
- - (Object) length(where = "1 = 1")
- - (Object) select(which_sql, condition_sql)
- - (Object) to_a
Methods included from Logging
Constructor Details
- (Table) initialize(table_name, *keys)
A new instance of Table
97 98 99 100 101 102 103 104 |
# File 'lib/insight/database.rb', line 97 def initialize(table_name, *keys) @table_name = table_name @keys = keys if(execute("select * from sqlite_master where name = ?", table_name).empty?) logger.warn{ "Initializing a table called #{table_name}" } execute(create_sql) end end |
Instance Method Details
- (Object) create_keys_clause
84 85 86 |
# File 'lib/insight/database.rb', line 84 def create_keys_clause "#{@keys.map{|key| "#{key} varchar"}.join(", ")}" end |
- (Object) create_sql
88 89 90 |
# File 'lib/insight/database.rb', line 88 def create_sql "create table #@table_name ( id integer primary key, #{create_keys_clause} )" end |
- (Object) db
80 81 82 |
# File 'lib/insight/database.rb', line 80 def db Insight::Database.db end |
- (Object) execute(*args)
92 93 94 95 |
# File 'lib/insight/database.rb', line 92 def execute(*args) logger.info{ ins_args = args.inspect; "(#{[ins_args.length,120].min}/#{ins_args.length})" + ins_args[0..120] } db.execute(*args) end |
- (Object) fields_sql
110 111 112 |
# File 'lib/insight/database.rb', line 110 def fields_sql "#{@keys.join(",")}" end |
- (Object) insert(values_sql)
114 115 116 |
# File 'lib/insight/database.rb', line 114 def insert(values_sql) execute("insert into #@table_name(#{fields_sql}) values (#{values_sql})") end |
- (Object) keys(name)
118 119 120 |
# File 'lib/insight/database.rb', line 118 def keys(name) execute("select #{name} from #@table_name").flatten end |
- (Object) length(where = "1 = 1")
122 123 124 |
# File 'lib/insight/database.rb', line 122 def length(where = "1 = 1") execute("select count(1) from #@table_name where #{where}").first.first end |
- (Object) select(which_sql, condition_sql)
106 107 108 |
# File 'lib/insight/database.rb', line 106 def select(which_sql, condition_sql) execute("select #{which_sql} from #@table_name where #{condition_sql}") end |
- (Object) to_a
126 127 128 |
# File 'lib/insight/database.rb', line 126 def to_a execute("select * from #@table_name") end |