Class: SQLite3::Database
- Inherits:
-
Object
- Object
- SQLite3::Database
- Defined in:
- lib/motion-sqlite3/database.rb
Instance Attribute Summary collapse
-
#logging ⇒ Object
Returns the value of attribute logging.
Instance Method Summary collapse
- #execute(sql, params = nil, &block) ⇒ Object
- #execute_scalar(*args) ⇒ Object
-
#initialize(filename) ⇒ Database
constructor
A new instance of Database.
- #transaction(&block) ⇒ Object
Constructor Details
#initialize(filename) ⇒ Database
Returns a new instance of Database.
3 4 5 6 7 8 9 |
# File 'lib/motion-sqlite3/database.rb', line 3 def initialize(filename) @handle = Pointer.new(::Sqlite3.type) @logging = false result = sqlite3_open(filename, @handle) raise SQLite3Error, sqlite3_errmsg(@handle.value) if result != SQLITE_OK end |
Instance Attribute Details
#logging ⇒ Object
Returns the value of attribute logging.
42 43 44 |
# File 'lib/motion-sqlite3/database.rb', line 42 def logging @logging end |
Instance Method Details
#execute(sql, params = nil, &block) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/motion-sqlite3/database.rb', line 11 def execute(sql, params = nil, &block) raise ArgumentError if sql.nil? if @logging puts " SQL: #{sql}" puts "Params: #{params}" if params && ! params.empty? end prepare(sql, params) do |statement| results = statement.execute if block_given? results.each do |result| yield result end else rows = [] results.each do |result| rows << result end rows end end end |
#execute_scalar(*args) ⇒ Object
38 39 40 |
# File 'lib/motion-sqlite3/database.rb', line 38 def execute_scalar(*args) execute(*args).first.values.first end |
#transaction(&block) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/motion-sqlite3/database.rb', line 44 def transaction(&block) execute("BEGIN TRANSACTION") begin result = yield rescue execute("ROLLBACK TRANSACTION") raise end execute("COMMIT TRANSACTION") result end |