Class: SQLite3::Database
- Inherits:
-
Object
- Object
- SQLite3::Database
- Defined in:
- lib/motion-sqlite3/database.rb
Instance Method Summary collapse
- #execute(sql, params = nil, &block) ⇒ Object
- #execute_debug(*args, &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 |
# File 'lib/motion-sqlite3/database.rb', line 3 def initialize(filename) @handle = Pointer.new(::Sqlite3.type) result = sqlite3_open(filename, @handle) raise SQLite3Error, sqlite3_errmsg(@handle.value) if result != SQLITE_OK end |
Instance Method Details
#execute(sql, params = nil, &block) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/motion-sqlite3/database.rb', line 10 def execute(sql, params = nil, &block) raise ArgumentError if sql.nil? #puts "*** #{sql}" #puts " #{params.inspect}" if params 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_debug(*args, &block) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/motion-sqlite3/database.rb', line 35 def execute_debug(*args, &block) puts "*** #{args[0]}" puts " #{args[1].inspect}" if args[1] execute(*args, &block) end |
#execute_scalar(*args) ⇒ Object
42 43 44 |
# File 'lib/motion-sqlite3/database.rb', line 42 def execute_scalar(*args) execute(*args).first.values.first end |
#transaction(&block) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/motion-sqlite3/database.rb', line 46 def transaction(&block) execute("BEGIN TRANSACTION") begin result = yield rescue execute("ROLLBACK TRANSACTION") raise end execute("COMMIT TRANSACTION") result end |