Class: MotionRecord::ConnectionAdapters::SQLiteAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/motion_record/connection_adapters/sqlite_adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, debug = true) ⇒ SQLiteAdapter

Returns a new instance of SQLiteAdapter.



62
63
64
65
# File 'lib/motion_record/connection_adapters/sqlite_adapter.rb', line 62

def initialize(file, debug=true)
  @db = SQLite3::Database.new(file)
  @db.logging = debug
end

Class Method Details

.configure(options = {}) ⇒ Object

Configure the SQLite connection

options - Hash of configuration options for the SQLite connection

:file - full name of the database file, or :memory for
        in-memory database files (default is "app.sqlite3"
        in the app's `/Library/Application Support` folder)
:debug - set to false to turn off SQL debug logging


12
13
14
# File 'lib/motion_record/connection_adapters/sqlite_adapter.rb', line 12

def configure(options={})
  @configuration_options = options
end

.debug?Boolean

Returns true if debug logging is enabled for the database

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/motion_record/connection_adapters/sqlite_adapter.rb', line 34

def debug?
  if @configuration_options.has_key?(:debug)
    !!@configuration_options[:debug]
  else
    true
  end
end

.filenameObject

Full filename of the database file



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/motion_record/connection_adapters/sqlite_adapter.rb', line 21

def filename
  if (file = @configuration_options[:file])
    if file == :memory
      ":memory:"
    else
      file
    end
  else
    create_default_database_file
  end
end

.instanceObject



16
17
18
# File 'lib/motion_record/connection_adapters/sqlite_adapter.rb', line 16

def instance
  @instance ||= ConnectionAdapters::SQLiteAdapter.new(filename, debug?)
end

Instance Method Details

#calculate(scope, method, column) ⇒ Object

Run a calculation on a set of rows

scope - MotionRecord::Scope which defines the set of rows method - one of :count, :maximum, :minimum, :sum, :average column - name of the column to run the calculation on

Returns the numerical value of calculation or nil if there were no rows in the scope



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/motion_record/connection_adapters/sqlite_adapter.rb', line 133

def calculate(scope, method, column)
  case method
  when :count
    calculation = "COUNT(#{column || "*"})"
  when :maximum
    calculation = "MAX(#{column})"
  when :minimum
    calculation = "MIN(#{column})"
  when :sum
    calculation = "SUM(#{column})"
  when :average
    calculation = "AVG(#{column})"
  else
    raise "Unrecognized calculation: #{method.inspect}"
  end

  calculate_statement = "SELECT #{calculation} AS #{method} FROM #{scope.klass.table_name} #{scope.predicate}"

  if (row = @db.execute(calculate_statement, scope.predicate_values).first)
    row[method]
  else
    nil
  end
end

#delete(scope) ⇒ Object

Delete rows from a table

scope - MotionRecord::Scope defining the set of rows to delete



119
120
121
122
123
# File 'lib/motion_record/connection_adapters/sqlite_adapter.rb', line 119

def delete(scope)
  delete_statement = "DELETE FROM #{scope.klass.table_name} #{scope.predicate}"

  @db.execute delete_statement, scope.predicate_values
end

#execute(command) ⇒ Object



67
68
69
# File 'lib/motion_record/connection_adapters/sqlite_adapter.rb', line 67

def execute(command)
  @db.execute(command)
end

#insert(table_name, params) ⇒ Object

Add a row to a table

table_name - name of the table params - Hash of column names to values to insert



90
91
92
93
94
95
96
97
98
99
# File 'lib/motion_record/connection_adapters/sqlite_adapter.rb', line 90

def insert(table_name, params)
  pairs = params.to_a
  param_names  = pairs.map(&:first)
  param_values = pairs.map(&:last)
  param_marks = Array.new(param_names.size, "?").join(", ")

  insert_statement = "INSERT INTO #{table_name} (#{param_names.join(", ")}) VALUES (#{param_marks})"

  @db.execute insert_statement, param_values
end

#select(scope) ⇒ Object

Load all records for a scope

scope - A MotionRecord::Scope

Returns an Array of row Hashes



81
82
83
84
# File 'lib/motion_record/connection_adapters/sqlite_adapter.rb', line 81

def select(scope)
  select_statement = "SELECT * FROM #{scope.klass.table_name} #{scope.predicate}"
  @db.execute(select_statement, scope.predicate_values)
end

#table_exists?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/motion_record/connection_adapters/sqlite_adapter.rb', line 71

def table_exists?(table_name)
  # FIXME: This statement is totally vulnerable to SQL injection
  @db.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='#{table_name}'").any?
end

#update(scope, params) ⇒ Object

Add a row to a table

scope - A MotionRecord::Scope params - Hash of column names to values to update



105
106
107
108
109
110
111
112
113
114
# File 'lib/motion_record/connection_adapters/sqlite_adapter.rb', line 105

def update(scope, params)
  pairs = params.to_a
  param_names  = pairs.map(&:first)
  param_values = pairs.map(&:last)
  param_marks  = param_names.map { |param| "#{param} = ?" }.join(", ")

  update_statement = "UPDATE #{scope.klass.table_name} SET #{param_marks} #{scope.predicate}"

  @db.execute update_statement, param_values + scope.predicate_values
end