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
Constants included from DatabaseMethods
Sequel::SQLite::DatabaseMethods::AUTO_VACUUM, Sequel::SQLite::DatabaseMethods::SCHEMA_TYPE_RE, 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, #constraint_definition_sql, #create_table_sql_list, #default_index_name, #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, #type_literal
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).
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/sequel_core/adapters/sqlite.rb', line 27 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 # fix for timestamp translation db.translator.add_translator("timestamp") do |t, v| v =~ /^\d+$/ ? Time.at(v.to_i) : Time.parse(v) end db end |
#dataset(opts = nil) ⇒ Object
Return instance of Sequel::SQLite::Dataset with the given options.
41 42 43 |
# File 'lib/sequel_core/adapters/sqlite.rb', line 41 def dataset(opts = nil) SQLite::Dataset.new(self, opts) end |
#disconnect ⇒ Object
Disconnect all connections from the database.
46 47 48 |
# File 'lib/sequel_core/adapters/sqlite.rb', line 46 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.
61 62 63 |
# File 'lib/sequel_core/adapters/sqlite.rb', line 61 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.
51 52 53 |
# File 'lib/sequel_core/adapters/sqlite.rb', line 51 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.
56 57 58 |
# File 'lib/sequel_core/adapters/sqlite.rb', line 56 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.
66 67 68 |
# File 'lib/sequel_core/adapters/sqlite.rb', line 66 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.
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/sequel_core/adapters/sqlite.rb', line 73 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 raise (SQLite3::Exception === e ? Error.new(e.) : e) unless Error::Rollback === e end end end |