Skip to content

Commit

Permalink
Added #changes and #reset to the sqlite3 driver
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Van Dyk committed Dec 9, 2009
1 parent 1cdb885 commit 0174eb6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
11 changes: 8 additions & 3 deletions lib/sqlite3/database.rb
Expand Up @@ -260,9 +260,14 @@ def last_insert_row_id
# Returns the number of changes made to this database instance by the last
# operation performed. Note that a "delete from table" without a where
# clause will not affect this value.
# def changes
# @driver.changes(@handle)
# end
def changes
@driver.changes(@handle)
end

# Resets a prepared statement object
def reset
@driver.reset(@handle)
end

# Returns the total number of changes made to this database instance
# since it was opened.
Expand Down
4 changes: 2 additions & 2 deletions lib/sqlite3/driver/ffi/api.rb
Expand Up @@ -35,10 +35,10 @@ module API
attach_function :sqlite3_prepare, [:pointer, :string, :int, :pointer, :pointer], :int
# attach_function :sqlite3_prepare16, [:pointer, :pointer, :int, :pointer, :pointer], :int
attach_function :sqlite3_finalize, [:pointer], :int
# attach_function :sqlite3_reset, [:pointer], :int
attach_function :sqlite3_reset, [:pointer], :int
attach_function :sqlite3_step, [:pointer], :int
attach_function :sqlite3_last_insert_rowid, [:pointer], :int64
# attach_function :sqlite3_changes, [:pointer], :int
attach_function :sqlite3_changes, [:pointer], :int
# attach_function :sqlite3_total_changes, [:pointer], :int
# attach_function :sqlite3_interrupt, [:pointer], :void
# attach_function :sqlite3_complete, [:string], :int
Expand Down
4 changes: 2 additions & 2 deletions lib/sqlite3/driver/ffi/driver.rb
Expand Up @@ -146,7 +146,7 @@ def self.api_delegate(name)
api_delegate :bind_parameter_index
api_delegate :bind_parameter_name
api_delegate :busy_timeout
# api_delegate :changes
api_delegate :changes
api_delegate :close
# api_delegate :column_bytes
# api_delegate :column_bytes16
Expand All @@ -161,7 +161,7 @@ def self.api_delegate(name)
# api_delegate :interrupt
api_delegate :last_insert_rowid
api_delegate :libversion
# api_delegate :reset
api_delegate :reset
# api_delegate :result_error
api_delegate :step
# api_delegate :total_changes
Expand Down
23 changes: 23 additions & 0 deletions test/test_database_queries_utf_8.rb
Expand Up @@ -27,6 +27,29 @@ def test_execute
assert_nil row[5]
end

def test_changes
assert_equal 0, @db.changes
@db.execute("INSERT INTO t1 VALUES(NULL, 'text1', 1.22, 42, 4294967296, NULL)")
@db.execute("INSERT INTO t1 VALUES(NULL, 'text1', 1.22, 42, 4294967296, NULL)")
assert_equal 1, @db.changes
@db.execute("update t1 set t = null")
assert_equal 2, @db.changes

last_id = @db.execute("select id from t1 limit 1")[0]
@db.execute("delete from t1 where id=?", last_id)
assert_equal 1, @db.changes

@db.execute("delete from t1")
assert_equal 0, @db.changes
end

# This test requires the existence of the reset method
def test_reset
query = "INSERT INTO t1 VALUES(1, 'text1', 1.22, 42, 4294967296, NULL)"
@db.execute(query)
assert_raise(SQLite3::SQLException) { @db.execute(query) }
end

def test_execute_with_bindings
blob = open("test/fixtures/SQLite.gif", "rb").read
@db.execute("INSERT INTO t1 VALUES(?, ?, ?, ?, ?, ?)", nil, "text1", 1.22, 42, 4294967296, blob)
Expand Down

0 comments on commit 0174eb6

Please sign in to comment.