Class: Sequel::SQLite::Database
- Includes:
- DatabaseMethods
- Defined in:
- lib/sequel_core/adapters/sqlite.rb
Overview
Database class for PostgreSQL databases used with Sequel and the ruby-sqlite3 driver.
Constant Summary collapse
- UNIX_EPOCH_TIME_FORMAT =
/\A\d+\z/.freeze
Constants included from DatabaseMethods
Sequel::SQLite::DatabaseMethods::AUTO_VACUUM, Sequel::SQLite::DatabaseMethods::SYNCHRONOUS, Sequel::SQLite::DatabaseMethods::TABLES_FILTER, Sequel::SQLite::DatabaseMethods::TEMP_STORE
Constants inherited from Database
Database::ADAPTERS, Database::SQL_BEGIN, Database::SQL_COMMIT, Database::SQL_ROLLBACK
Constants included from Sequel::Schema::SQL
Sequel::Schema::SQL::AUTOINCREMENT, Sequel::Schema::SQL::CASCADE, Sequel::Schema::SQL::COMMA_SEPARATOR, Sequel::Schema::SQL::NOT_NULL, Sequel::Schema::SQL::NO_ACTION, Sequel::Schema::SQL::NULL, Sequel::Schema::SQL::PRIMARY_KEY, Sequel::Schema::SQL::RESTRICT, Sequel::Schema::SQL::SET_DEFAULT, Sequel::Schema::SQL::SET_NULL, Sequel::Schema::SQL::TYPES, Sequel::Schema::SQL::UNDERSCORE, Sequel::Schema::SQL::UNIQUE, Sequel::Schema::SQL::UNSIGNED
Instance Attribute Summary
Attributes inherited from Database
#loggers, #opts, #pool, #prepared_statements, #quote_identifiers
Instance Method Summary collapse
-
#connect(server) ⇒ Object
Connect to the database.
-
#dataset(opts = nil) ⇒ Object
Return instance of Sequel::SQLite::Dataset with the given options.
-
#disconnect ⇒ Object
Disconnect all connections from the database.
-
#execute(sql, opts = {}, &block) ⇒ Object
Run the given SQL with the given arguments and yield each row.
-
#execute_dui(sql, opts = {}) ⇒ Object
Run the given SQL with the given arguments and return the number of changed rows.
-
#execute_insert(sql, opts = {}) ⇒ Object
Run the given SQL with the given arguments and return the last inserted row id.
-
#single_value(sql, opts = {}) ⇒ Object
Run the given SQL with the given arguments and return the first value of the first row.
-
#transaction(server = nil, &block) ⇒ Object
Use the native driver transaction method if there isn’t already a transaction in progress on the connection, always yielding a connection inside a transaction transaction.
Methods included from DatabaseMethods
#alter_table_sql, #auto_vacuum, #auto_vacuum=, #pragma_get, #pragma_set, #synchronous, #synchronous=, #tables, #temp_store, #temp_store=
Methods inherited from Database
#<<, #[], adapter_class, adapter_scheme, #add_column, #add_index, #alter_table, #call, connect, #create_or_replace_view, #create_table, #create_table!, #create_view, #drop_column, #drop_index, #drop_table, #drop_view, #execute_ddl, #fetch, #from, #get, #initialize, #inspect, #log_info, #logger, #logger=, #multi_threaded?, #query, quote_identifiers=, #quote_identifiers?, #rename_column, #rename_table, #select, #serial_primary_key_options, #set_column_default, #set_column_type, single_threaded=, #single_threaded?, #synchronize, #table_exists?, #test_connection, #typecast_value, #uri
Methods included from Sequel::Schema::SQL
#alter_table_sql, #alter_table_sql_list, #auto_increment_sql, #column_definition_sql, #column_list_sql, #column_references_sql, #constraint_definition_sql, #create_table_sql_list, #default_index_name, #drop_index_sql, #drop_table_sql, #filter_expr, #index_definition_sql, #index_list_sql_list, #literal, #on_delete_clause, #quote_identifier, #rename_table_sql, #schema, #schema_utility_dataset
Constructor Details
This class inherits a constructor from Sequel::Database
Instance Method Details
#connect(server) ⇒ Object
Connect to the database. Since SQLite is a file based database, the only options available are :database (to specify the database name), and :timeout, to specify how long to wait for the database to be available if it is locked, given in milliseconds (default is 5000).
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/sequel_core/adapters/sqlite.rb', line 28 def connect(server) opts = server_opts(server) opts[:database] = ':memory:' if opts[:database].blank? db = ::SQLite3::Database.new(opts[:database]) db.busy_timeout(opts.fetch(:timeout, 5000)) db.type_translation = true # Handle datetime's with Sequel.datetime_class prok = proc do |t,v| v = Time.at(v.to_i).iso8601 if UNIX_EPOCH_TIME_FORMAT.match(v) v.to_sequel_time end db.translator.add_translator("timestamp", &prok) db.translator.add_translator("datetime", &prok) db end |
#dataset(opts = nil) ⇒ Object
Return instance of Sequel::SQLite::Dataset with the given options.
45 46 47 |
# File 'lib/sequel_core/adapters/sqlite.rb', line 45 def dataset(opts = nil) SQLite::Dataset.new(self, opts) end |
#disconnect ⇒ Object
Disconnect all connections from the database.
50 51 52 |
# File 'lib/sequel_core/adapters/sqlite.rb', line 50 def disconnect @pool.disconnect {|c| c.close} end |
#execute(sql, opts = {}, &block) ⇒ Object
Run the given SQL with the given arguments and yield each row.
65 66 67 |
# File 'lib/sequel_core/adapters/sqlite.rb', line 65 def execute(sql, opts={}, &block) _execute(sql, opts){|conn| conn.query(sql, opts[:arguments], &block)} end |
#execute_dui(sql, opts = {}) ⇒ Object
Run the given SQL with the given arguments and return the number of changed rows.
55 56 57 |
# File 'lib/sequel_core/adapters/sqlite.rb', line 55 def execute_dui(sql, opts={}) _execute(sql, opts){|conn| conn.execute_batch(sql, opts[:arguments]); conn.changes} end |
#execute_insert(sql, opts = {}) ⇒ Object
Run the given SQL with the given arguments and return the last inserted row id.
60 61 62 |
# File 'lib/sequel_core/adapters/sqlite.rb', line 60 def execute_insert(sql, opts={}) _execute(sql, opts){|conn| conn.execute(sql, opts[:arguments]); conn.last_insert_row_id} end |
#single_value(sql, opts = {}) ⇒ Object
Run the given SQL with the given arguments and return the first value of the first row.
70 71 72 |
# File 'lib/sequel_core/adapters/sqlite.rb', line 70 def single_value(sql, opts={}) _execute(sql, opts){|conn| conn.get_first_value(sql, opts[:arguments])} end |
#transaction(server = nil, &block) ⇒ Object
Use the native driver transaction method if there isn’t already a transaction in progress on the connection, always yielding a connection inside a transaction transaction.
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/sequel_core/adapters/sqlite.rb', line 77 def transaction(server=nil, &block) synchronize(server) do |conn| return yield(conn) if conn.transaction_active? begin result = nil conn.transaction{result = yield(conn)} result rescue ::Exception => e transaction_error(e, SQLite3::Exception) end end end |