Class: DBI::DBD::SQLite3::Database
- Inherits:
-
BaseDatabase
- Object
- BaseDatabase
- DBI::DBD::SQLite3::Database
- Defined in:
- lib/dbd/sqlite3/database.rb
Overview
See DBI::BaseDatabase.
Instance Method Summary collapse
-
#[]=(attr, value) ⇒ Object
See #new for valid attributes.
-
#__generate_attr__ ⇒ Object
This method is used to aid the constructor and probably should not be used independently.
-
#columns(table) ⇒ Object
See DBI::BaseDatabase#columns.
- #commit ⇒ Object
- #disconnect ⇒ Object
-
#initialize(dbname, attr) ⇒ Database
constructor
Constructor.
- #ping ⇒ Object
- #prepare(statement) ⇒ Object
- #quote(value) ⇒ Object
-
#rollback ⇒ Object
See DBI::BaseDatabase#rollback.
- #tables ⇒ Object
Constructor Details
#initialize(dbname, attr) ⇒ Database
Constructor. Valid attributes:
-
AutoCommit: Commit after every statement execution.
The following attributes go directly to the low-level SQLite3 driver. Please consult it’s documentation for more information.
-
auto_vacuum
-
cache_size
-
default_cache_size
-
default_synchronous
-
default_temp_store
-
full_column_names
-
synchronous
-
temp_store
-
type_translation
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dbd/sqlite3/database.rb', line 23 def initialize(dbname, attr) @db = ::SQLite3::Database.new(dbname) @db.type_translation = false @attr = {'AutoCommit' => true} if attr then attr.each_pair do |key, value| begin self[key] = value rescue DBI::NotSupportedError end end end __generate_attr__ end |
Instance Method Details
#[]=(attr, value) ⇒ Object
See #new for valid attributes.
If Autocommit is set to true, commit happens immediately if a transaction is open.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/dbd/sqlite3/database.rb', line 162 def []=(attr, value) case attr when 'AutoCommit' if value @db.commit if @db.transaction_active? else @db.transaction unless @db.transaction_active? end @attr[attr] = value when 'auto_vacuum', 'cache_size', 'count_changes', 'default_cache_size', 'encoding', 'full_column_names', 'page_size', 'short_column_names', 'synchronous', 'temp_store', 'temp_store_directory' @db.__send__((attr+'='), value) @attr[attr] = @db.__send__(attr) when 'busy_timeout' @db.busy_timeout(value) @attr[attr] = value when 'busy_handler' @db.busy_timeout(&value) @attr[attr] = value when 'type_translation' @db.type_translation = value @attr[attr] = value else raise DBI::NotSupportedError end return value end |
#__generate_attr__ ⇒ Object
This method is used to aid the constructor and probably should not be used independently.
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/dbd/sqlite3/database.rb', line 143 def __generate_attr__() tt = @db.type_translation @db.type_translation = false [ 'auto_vacuum', 'cache_size', 'default_cache_size', 'default_synchronous', 'default_temp_store', 'full_column_names', 'synchronous', 'temp_store', 'type_translation' ].each do |key| unless @attr.has_key?(key) then @attr[key] = @db.__send__(key) end end @db.type_translation = tt end |
#columns(table) ⇒ Object
See DBI::BaseDatabase#columns.
Additional Attributes:
-
sql_type: XOPEN integer SQL Type.
-
nullable: true if NULL is allowed in this column.
-
default: the value that will be used in new rows if this column receives no data.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/dbd/sqlite3/database.rb', line 106 def columns(table) @db.type_translation = false ret = @db.table_info(table).map do |hash| m = DBI::DBD::SQLite3.parse_type(hash['type']) h = { 'name' => hash['name'], 'type_name' => m[1], 'sql_type' => begin DBI.const_get('SQL_'+hash['type'].upcase) rescue NameError DBI::SQL_OTHER end, 'nullable' => (hash['notnull'] == '0'), 'default' => (@attr['type_translation'] && (not hash['dflt_value'])) ? @db.translator.translate(hash['type'], hash['dflt_value']) : hash['dflt_value'] } h['precision'] = m[3].to_i if m[3] h['scale'] = m[5].to_i if m[5] h end @db.type_translation = @attr['type_translation'] ret end |
#commit ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/dbd/sqlite3/database.rb', line 53 def commit() if @db.transaction_active? @db.commit @db.transaction else raise DBI::ProgrammingError.new("No active transaction.") end end |
#disconnect ⇒ Object
40 41 42 43 |
# File 'lib/dbd/sqlite3/database.rb', line 40 def disconnect() @db.rollback if @db.transaction_active? @db.close end |
#ping ⇒ Object
49 50 51 |
# File 'lib/dbd/sqlite3/database.rb', line 49 def ping() not @db.closed? end |
#prepare(statement) ⇒ Object
45 46 47 |
# File 'lib/dbd/sqlite3/database.rb', line 45 def prepare(statement) DBI::DBD::SQLite3::Statement.new(statement, @db) end |
#quote(value) ⇒ Object
135 136 137 |
# File 'lib/dbd/sqlite3/database.rb', line 135 def quote(value) ::SQLite3::Database.quote(value.to_s) end |
#rollback ⇒ Object
See DBI::BaseDatabase#rollback.
If all statements were not closed before the rollback occurs, a DBI::Warning may be raised if the database encounters an error because of it.
This method will also raise DBI::ProgrammingError if not in a transaction.
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/dbd/sqlite3/database.rb', line 72 def rollback() if @db.transaction_active? begin @db.rollback @db.transaction rescue Exception => e raise DBI::Warning, "Statements were not closed prior to rollback" end else raise DBI::ProgrammingError.new("No active transaction.") end end |
#tables ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/dbd/sqlite3/database.rb', line 85 def tables() ret = [] result = @db.execute(%q( SELECT name FROM sqlite_master WHERE type IN ('table', 'view') UNION ALL SELECT name FROM sqlite_temp_master WHERE type in ('table', 'view') ORDER BY 1 )) result.each{|row| ret.push(row[0])} ret end |