lib/kit/db_sqlite3.rb in kit-0.0.6 vs lib/kit/db_sqlite3.rb in kit-0.1.0

- old
+ new

@@ -1,9 +1,11 @@ require 'sqlite3' -# Methods to make sqlite3 interation more consice +# Methods to make sqlite3 interation more concise. module SQLite3Tools + + # Array methods. class ::Array # Converts an array of symbols to a string of backquoted strings for use in SELECT statement # @return [String] # @example # a = [ :col_1, :col_2 ] @@ -22,10 +24,11 @@ def to_hash keys Hash[ *( 0...self.size ).inject( [] ) { |arr, ix| arr.push( keys[ix], self[ix] ) } ] end end + # Integer methods. class ::Integer # Generates placeholder string for INSERT statements. # @return String placeholder # @example # 3.make_placeholders #=> "?, ?, ?" @@ -35,10 +38,11 @@ n.times { placeholders << ", ?" } placeholders end end + # SQLite3 database methods. class SQLite3::Database # Selects given columns using given query and converts results to to hashes. # @param [Array<Symbol>] columns names of columns to select # @param [String] query sqlite3 query fragment to append to SELECT part of query # @return [Array<Hash>] results with each row a hash in :col => value form @@ -50,10 +54,11 @@ result end end end +# Backend abstracts database interactions. class Backend < Kit include SQLite3Tools attr_reader :db_paths @@ -112,49 +117,83 @@ return dbs end public - def select_all_actions_by_status action, fields, status - query = "FROM `#{action}` WHERE `status` = '#{status}'" - rows = @action_db.select fields, query - h = { :action => action } - rows.map { |t| t.merge h } + # Deletes database files. + def delete + @db_paths.each do |key, f| + File.delete f + end end - def insert_action table, data - data.merge! ( { :status => "pending" } ) - @action_db.execute "INSERT INTO #{table} ( `#{data.keys.join "`, `"}` ) VALUES ( #{data.length.make_placeholders} )", data.values - @action_db.last_insert_row_id - end - - def delete_action_by_id action, id -# puts "DELETE FROM `#{action}` WHERE `rowid` = #{id}" - end - + # Gets the row from an info table with the given id. + # @param [Symbol] table what database table to query + # @param [Array] fields list of column names to return + # @param [Integer] id rowid of record to return + # @return [Array] hash for each returned row with a key for each requested field def select_info_by_id table, fields, id info = @info_db.select fields, "FROM `#{table}` WHERE `rowid` = '#{id}'" info.first end + # Gets the rows from an info table with the given name. + # @param table (see #select_info_by_id) + # @param fields (see #select_info_by_id) + # @param [String] name of records to return + # @return (see #select_info_by_id) def select_info_by_name table, fields, name info = @info_db.select fields, "FROM `#{table}` WHERE `name` = '#{name}'" info.first end + # Gets the rows from an info table with the given criteria. + # @param table (see #select_info_by_id) + # @param fields (see #select_info_by_id) + # @param [Hash] criteria key / value pairs required to match + # @return (see #select_info_by_id) def select_info_by_criteria table, fields, criteria - q = [] criteria.each do |key, value| q << "`#{key}` = '#{value}'" end info = @info_db.select fields, "FROM `#{table}` WHERE #{q.join " AND "}" info.first end + # Inserts a new row into an info table. + # @param table (see #select_info_by_id) + # @param [Hash] data key / value pairs for new row + # @return [Integer] rowid of new row def insert_info table, data @info_db.execute "INSERT INTO #{table} ( `#{data.keys.join "`, `"}` ) VALUES ( #{data.length.make_placeholders} )", data.values @info_db.last_insert_row_id end + + # Gets the rows from the action table with given status. + # @param table (see #select_info_by_id) + # @param fields (see #select_info_by_id) + # @param [Symbol] status name of status to match + # @return (see #select_info_by_id) + def select_all_actions_by_status table, fields, status + query = "FROM `#{table}` WHERE `status` = '#{status}'" + rows = @action_db.select fields, query + + rows.map { |t| t.merge ( { :action => table, :status => t[:status].to_sym } ) } + end + + # Inserts a new row into an action table. + # @param (see #insert_info) + # @return (see #insert_info) + def insert_action table, data + data.merge! ( { :status => :pending.to_s, :time => Time.now.to_i } ) + @action_db.execute "INSERT INTO #{table} ( `#{data.keys.join "`, `"}` ) VALUES ( #{data.length.make_placeholders} )", data.values + @action_db.last_insert_row_id + end + + # def delete_action_by_id action, id + # puts "DELETE FROM `#{action}` WHERE `rowid` = #{id}" + # end + end