Class: Rack::Insight::Database::Table

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/rack/insight/database.rb

Direct Known Subclasses

DataTable, RequestTable

Instance Method Summary collapse

Methods included from Logging

logger, verbose, verbosity

Constructor Details

#initialize(table_name, *keys) ⇒ Table

Returns a new instance of Table.



126
127
128
129
130
131
132
133
# File 'lib/rack/insight/database.rb', line 126

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



139
140
141
# File 'lib/rack/insight/database.rb', line 139

def count(condition_sql)
  execute("select count(*) from #@table_name where #{condition_sql}")
end

#create_keys_clauseObject



113
114
115
# File 'lib/rack/insight/database.rb', line 113

def create_keys_clause
  "#{@keys.map{|key| "#{key} varchar"}.join(", ")}"
end

#create_sqlObject



117
118
119
# File 'lib/rack/insight/database.rb', line 117

def create_sql
  "create table #@table_name ( id integer primary key, #{create_keys_clause} )"
end

#dbObject



109
110
111
# File 'lib/rack/insight/database.rb', line 109

def db
  Rack::Insight::Database.db
end

#execute(*args) ⇒ Object



121
122
123
124
# File 'lib/rack/insight/database.rb', line 121

def execute(*args)
  #logger.info{ ins_args = args.inspect; "(#{[ins_args.length,120].min}/#{ins_args.length})" + ins_args[0..120] } if verbose(:debug)
  db.execute(*args)
end

#fields_sqlObject



143
144
145
# File 'lib/rack/insight/database.rb', line 143

def fields_sql
  "#{@keys.join(",")}"
end

#insert(values_sql) ⇒ Object



147
148
149
# File 'lib/rack/insight/database.rb', line 147

def insert(values_sql)
  execute("insert into #@table_name(#{fields_sql}) values (#{values_sql})")
end

#keys(name) ⇒ Object



151
152
153
# File 'lib/rack/insight/database.rb', line 151

def keys(name)
  execute("select #{name} from #@table_name").flatten
end

#length(where = "1 = 1") ⇒ Object



155
156
157
# File 'lib/rack/insight/database.rb', line 155

def length(where = "1 = 1")
  execute("select count(1) from #@table_name where #{where}").first.first
end

#select(which_sql, condition_sql) ⇒ Object



135
136
137
# File 'lib/rack/insight/database.rb', line 135

def select(which_sql, condition_sql)
  execute("select #{which_sql} from #@table_name where #{condition_sql}")
end

#to_aObject



159
160
161
# File 'lib/rack/insight/database.rb', line 159

def to_a
  execute("select * from #@table_name")
end