Class: Insight::Database::Table

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

Direct Known Subclasses

DataTable, RequestTable

Instance Method Summary collapse

Methods included from Logging

logger

Constructor Details

#initialize(table_name, *keys) ⇒ Table

Returns a new instance of Table.



97
98
99
100
101
102
103
104
105
# 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?)
    execute(create_sql)

    logger.debug{ "Initializing a table called #{table_name}" }
  end
end

Instance Method Details

#create_keys_clauseObject



84
85
86
# File 'lib/insight/database.rb', line 84

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

#create_sqlObject



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

#dbObject



80
81
82
# File 'lib/insight/database.rb', line 80

def db
  Insight::Database.db
end

#execute(*args) ⇒ Object



92
93
94
95
# File 'lib/insight/database.rb', line 92

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

#fields_sqlObject



111
112
113
# File 'lib/insight/database.rb', line 111

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

#insert(values_sql) ⇒ Object



115
116
117
# File 'lib/insight/database.rb', line 115

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

#keys(name) ⇒ Object



119
120
121
# File 'lib/insight/database.rb', line 119

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

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



123
124
125
# File 'lib/insight/database.rb', line 123

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

#select(which_sql, condition_sql) ⇒ Object



107
108
109
# File 'lib/insight/database.rb', line 107

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

#to_aObject



127
128
129
# File 'lib/insight/database.rb', line 127

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