class Rake::DataTask::Sqlite
Public Class Methods
new(options)
click to toggle source
Connect to an Sqlite
database.
@param [Hash] options the connection parameters @option options [String] ‘database’ the database name @return [Sqlite] an instance of this adapter
# File lib/data_task/adapters/sqlite.rb, line 20 def initialize options @connection = SQLite3::Database.new(options['database'] || 'temp') # set up trackig if it isn't set up already set_up_tracking if !tracking_tables? end
Public Instance Methods
[](name)
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 173 def [](name) Data.new(name, self) end
create_data(table_name, data_definition, column_definitions=nil, track_table=true)
Alias for: create_table
create_table(table_name, data_definition, column_definitions=nil, track_table=true)
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 80 def create_table table_name, data_definition, column_definitions=nil, track_table=true drop_table table_name execute <<-EOSQL create table #{table_name} #{column_definitions} #{ "as #{data_definition}" if !data_definition.nil? } EOSQL if track_table create_tracking_rules(table_name) track_creation table_name, 0 end end
Also aliased as: create_data
create_view(view_name, select_stmt)
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 117 def create_view view_name, select_stmt drop_view view_name execute "create view #{view_name} as #{select_stmt}" end
drop_table(table_name)
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 94 def drop_table table_name execute "drop table if exists #{table_name}" # manually cascade the drop operation to views for this table views_for_dropped_table = execute <<-EOSQL select name from sqlite_master where type = 'view' and ( -- add trailing space for views without where statements sql || ' ' like "% from #{table_name} %" or sql like "% join #{table_name} %" ) EOSQL views_for_dropped_table.flatten.each do |view_name| drop_view view_name end return if table_name.casecmp(TABLE_TRACKER_NAME) == 0 track_drop table_name end
Also aliased as: drop_data
drop_view(view_name)
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 122 def drop_view view_name execute "drop view if exists #{view_name}" end
execute(sql)
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 27 def execute sql connect if @connection.nil? begin @connection.execute sql rescue SQLite3::SQLException => e LOG.info e.message.chomp raise e end end
operations_supported()
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 166 def operations_supported { :by_db => operations_supported_by_db, :by_app => [:truncate, :create] } end
reset_tracking(options = {})
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 63 def reset_tracking options = {} truncate_table TABLE_TRACKER_NAME end
set_up_tracking(options = {})
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 51 def set_up_tracking options = {} tear_down_tracking options column_definitions = table_tracker_columns.map do |col,col_defn| col.to_s + ' ' + col_defn[:data_type].to_s end.join(', ') create_table TABLE_TRACKER_NAME, nil, " (#{column_definitions})", false end
table_exists?(table_name, options = {})
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 135 def table_exists? table_name, options = {} relation_exists?(table_name, 'table', options) end
Also aliased as: data_exists?
table_mtime(table_name)
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 67 def table_mtime table_name Sql.get_single_time( execute <<-EOSQL -- assume time is UTC (Sqlite3 default) and add offset for Ruby's Time.parse select datetime(max(time)) || ' -0000' from #{TABLE_TRACKER_NAME} where relation_name = '#{table_name}' EOSQL ) end
Also aliased as: data_mtime
table_tracker_columns()
click to toggle source
Calls superclass method
# File lib/data_task/adapters/sqlite.rb, line 44 def table_tracker_columns # replace the default datatype for time with SQLite's timestamp super.merge({ :time => {:data_type => :timestamp} }) end
tear_down_tracking(options = {})
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 59 def tear_down_tracking options = {} drop_table TABLE_TRACKER_NAME end
track_drop(table_name)
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 126 def track_drop table_name execute <<-EOSQL delete from #{TABLE_TRACKER_NAME} where relation_name = '#{table_name}' and relation_type = '#{relation_type_values[:table]}' EOSQL end
track_truncate(table_name)
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 153 def track_truncate table_name execute <<-EOSQL update #{TABLE_TRACKER_NAME} set operation = '#{operation_values[:truncate]}', -- Sqlite generates times at UTC and stores them without zone information time = datetime('now') where relation_name = '#{table_name}' and relation_type = '#{relation_type_values[:table]}' EOSQL end
tracking_tables?()
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 40 def tracking_tables? table_exists?(TABLE_TRACKER_NAME) end
truncate_table(table_name)
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 145 def truncate_table table_name return if table_name.casecmp(TABLE_TRACKER_NAME) == 0 execute "delete from #{table_name}" track_truncate table_name end
Also aliased as: truncate_data
view_exists?(table_name, options = {})
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 141 def view_exists? table_name, options = {} relation_exists?(table_name, 'view', options) end
Private Instance Methods
create_tracking_rules(table_name)
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 189 def create_tracking_rules table_name operations_supported_by_db.each do |operation| execute <<-EOSQL create trigger #{rule_name(table_name, operation)} after #{operation.to_s} on #{table_name} begin update #{TABLE_TRACKER_NAME} set operation = '#{operation_values[operation]}', time = datetime() where relation_name = '#{table_name}' and relation_type = '#{relation_type_values[:table]}' ; end EOSQL end end
operations_supported_by_db()
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 181 def operations_supported_by_db [:update, :insert, :delete] end
relation_exists?(relation_name, relation_type, options = {})
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 228 def relation_exists? relation_name, relation_type, options = {} n_matches = Sql.get_single_int( execute <<-EOSQL select count(*) from sqlite_master where name = '#{relation_name}' and type = '#{relation_type}' EOSQL ) (n_matches > 0) end
rule_name(table_name, operation)
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 185 def rule_name table_name, operation "#{table_name}_#{operation.to_s}" end
track_creation(table_name, n_tuples)
click to toggle source
# File lib/data_task/adapters/sqlite.rb, line 209 def track_creation table_name, n_tuples operation = :create execute <<-EOSQL delete from #{TABLE_TRACKER_NAME} where relation_name = '#{table_name}' and relation_type = '#{relation_type_values[:table]}' and operation = '#{operation_values[operation]}' ; EOSQL execute <<-EOSQL insert into #{TABLE_TRACKER_NAME} values ( '#{table_name}', '#{relation_type_values[:table]}', '#{operation_values[operation]}', datetime('now') ); EOSQL end