Class: DataMapper::Adapters::Sqlite3Adapter
- Inherits:
-
SqlAdapter
- Object
- AbstractAdapter
- SqlAdapter
- DataMapper::Adapters::Sqlite3Adapter
- Defined in:
- lib/data_mapper/adapters/sqlite3_adapter.rb
Defined Under Namespace
Modules: Commands
Constant Summary collapse
- TABLE_QUOTING_CHARACTER =
'"'.freeze
- COLUMN_QUOTING_CHARACTER =
'"'.freeze
Constants inherited from SqlAdapter
DataMapper::Adapters::SqlAdapter::FIND_OPTIONS, DataMapper::Adapters::SqlAdapter::TYPES
Instance Method Summary collapse
- #connection ⇒ Object
-
#initialize(configuration) ⇒ Sqlite3Adapter
constructor
A new instance of Sqlite3Adapter.
- #query(*args) ⇒ Object
- #type_cast_boolean(value) ⇒ Object
- #type_cast_datetime(value) ⇒ Object
Methods inherited from SqlAdapter
#[], #delete, #escape_sql, inherited, #load, #save, #schema, #single_threaded?, #table_exists?, #transaction
Methods inherited from AbstractAdapter
Constructor Details
#initialize(configuration) ⇒ Sqlite3Adapter
Returns a new instance of Sqlite3Adapter.
11 12 13 14 15 16 17 18 |
# File 'lib/data_mapper/adapters/sqlite3_adapter.rb', line 11 def initialize(configuration) super @connections = Support::ConnectionPool.new do dbh = SQLite3::Database.new(configuration.database) dbh.results_as_hash = true dbh end end |
Instance Method Details
#connection ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/data_mapper/adapters/sqlite3_adapter.rb', line 20 def connection raise ArgumentError.new('Sqlite3Adapter#connection requires a block-parameter') unless block_given? begin @connections.hold { |connection| yield connection } rescue SQLite3::Exception => sle @configuration.log.fatal(sle) @connections.available_connections.each do |sock| begin sock.close rescue => se @configuration.log.error(se) end end @connections.available_connections.clear raise sle end end |
#query(*args) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/data_mapper/adapters/sqlite3_adapter.rb', line 41 def query(*args) reader = connection { |db| db.query(escape_sql(*args)) } fields = nil rows = [] until reader.eof? hash = reader.next break if hash.nil? fields = hash.keys.select { |field| field.is_a?(String) } unless fields rows << fields.map { |field| hash[field] } end reader.close struct = Support::Struct::define(fields) rows.map do |row| struct.new(row) end end |
#type_cast_boolean(value) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/data_mapper/adapters/sqlite3_adapter.rb', line 75 def type_cast_boolean(value) case value when TrueClass, FalseClass then value when "1", "true", "TRUE" then true when "0", nil then false else "Can't type-cast #{value.inspect} to a boolean" end end |
#type_cast_datetime(value) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/data_mapper/adapters/sqlite3_adapter.rb', line 84 def type_cast_datetime(value) case value when DateTime then value when Date then DateTime.new(value) when String then DateTime::parse(value) else "Can't type-cast #{value.inspect} to a datetime" end end |