Class: Rack::Insight::Database::Table
- Inherits:
-
Object
- Object
- Rack::Insight::Database::Table
show all
- Includes:
- Logging
- Defined in:
- lib/rack/insight/database.rb
Instance Method Summary
collapse
Methods included from Logging
logger, verbose, verbosity
Constructor Details
#initialize(table_name, *keys) ⇒ Table
Returns a new instance of Table.
125
126
127
128
129
130
131
132
|
# File 'lib/rack/insight/database.rb', line 125
def initialize(table_name, *keys)
@table_name = table_name
@keys = keys
if(execute("select * from sqlite_master where name = ?", table_name).empty?)
logger.info{ "Initializing a table called #{table_name}" } if verbose(:med)
execute(create_sql)
end
end
|
Instance Method Details
#count(condition_sql) ⇒ Object
138
139
140
|
# File 'lib/rack/insight/database.rb', line 138
def count(condition_sql)
execute("select count(*) from #@table_name where #{condition_sql}")
end
|
#create_keys_clause ⇒ Object
112
113
114
|
# File 'lib/rack/insight/database.rb', line 112
def create_keys_clause
"#{@keys.map{|key| "#{key} varchar"}.join(", ")}"
end
|
#create_sql ⇒ Object
116
117
118
|
# File 'lib/rack/insight/database.rb', line 116
def create_sql
"create table #@table_name ( id integer primary key, #{create_keys_clause} )"
end
|
#execute(*args) ⇒ Object
120
121
122
123
|
# File 'lib/rack/insight/database.rb', line 120
def execute(*args)
db.execute(*args)
end
|
#fields_sql ⇒ Object
142
143
144
|
# File 'lib/rack/insight/database.rb', line 142
def fields_sql
"#{@keys.join(",")}"
end
|
#insert(values_sql) ⇒ Object
146
147
148
|
# File 'lib/rack/insight/database.rb', line 146
def insert(values_sql)
execute("insert into #@table_name(#{fields_sql}) values (#{values_sql})")
end
|
#keys(name) ⇒ Object
150
151
152
|
# File 'lib/rack/insight/database.rb', line 150
def keys(name)
execute("select #{name} from #@table_name").flatten
end
|
#length(where = "1 = 1") ⇒ Object
154
155
156
|
# File 'lib/rack/insight/database.rb', line 154
def length(where = "1 = 1")
execute("select count(1) from #@table_name where #{where}").first.first
end
|
#select(which_sql, condition_sql) ⇒ Object
134
135
136
|
# File 'lib/rack/insight/database.rb', line 134
def select(which_sql, condition_sql)
execute("select #{which_sql} from #@table_name where #{condition_sql}")
end
|
#to_a ⇒ Object
158
159
160
|
# File 'lib/rack/insight/database.rb', line 158
def to_a
execute("select * from #@table_name")
end
|