Class: SQLite3::Driver
- Inherits:
-
Object
- Object
- SQLite3::Driver
- Defined in:
- lib/sqlite3/driver.rb
Constant Summary collapse
- TRANSIENT =
FFI::Pointer.new(-1)
Class Method Summary collapse
Instance Method Summary collapse
- #bind_string(stmt, index, value) ⇒ Object
- #column_blob(stmt, column) ⇒ Object
- #column_text(stmt, column, utf_16 = false) ⇒ Object
- #enable_load_extension(db, onoff = false) ⇒ Object
- #errmsg(db, utf_16 = false) ⇒ Object
- #extension_support? ⇒ Boolean
- #load_extension(db, name, entry_point = nil) ⇒ Object
- #open(filename, utf_16 = false) ⇒ Object
- #prepare(db, sql) ⇒ Object
Class Method Details
.api_delegate(name) ⇒ Object
87 88 89 |
# File 'lib/sqlite3/driver.rb', line 87 def self.api_delegate(name) define_method(name) { |*args| API.send("sqlite3_#{name}", *args) } end |
Instance Method Details
#bind_string(stmt, index, value) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/sqlite3/driver.rb', line 46 def bind_string(stmt, index, value) case value.encoding when Encoding.utf_8, Encoding.us_ascii API.sqlite3_bind_text(stmt, index, value, value.bytesize, TRANSIENT) when Encoding.utf_16le, Encoding.utf_16be value = add_byte_order_mask(value) API.sqlite3_bind_text16(stmt, index, value, value.bytesize, TRANSIENT) else API.sqlite3_bind_blob(stmt, index, value, value.bytesize, TRANSIENT) end end |
#column_blob(stmt, column) ⇒ Object
58 59 60 61 62 |
# File 'lib/sqlite3/driver.rb', line 58 def column_blob(stmt, column) blob = API.sqlite3_column_blob(stmt, column) length = API.sqlite3_column_bytes(stmt, column) blob.get_bytes(0, length) # free? end |
#column_text(stmt, column, utf_16 = false) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/sqlite3/driver.rb', line 64 def column_text(stmt, column, utf_16 = false) if utf_16 ptr = API.sqlite3_column_text16(stmt, column) length = API.sqlite3_column_bytes16(stmt, column) ptr.get_bytes(0, length).force_encoding(Encoding.utf_16native) # free? else API.sqlite3_column_text(stmt, column).force_encoding(Encoding.utf_8) end end |
#enable_load_extension(db, onoff = false) ⇒ Object
83 84 85 |
# File 'lib/sqlite3/driver.rb', line 83 def enable_load_extension(db, onoff = false) API.sqlite3_enable_load_extension(db, (onoff ? 1 : 0)) end |
#errmsg(db, utf_16 = false) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/sqlite3/driver.rb', line 17 def errmsg(db, utf_16 = false) if utf_16 ptr = API.sqlite3_errmsg16(db) result = get_string_utf_16(ptr) result.force_encoding(Encoding.utf_16native) if result.respond_to?(:force_encoding) result else result = API.sqlite3_errmsg(db) result.force_encoding(Encoding.utf_8) if result.respond_to?(:force_encoding) result end end |
#extension_support? ⇒ Boolean
74 75 76 |
# File 'lib/sqlite3/driver.rb', line 74 def extension_support? API::EXTENSION_SUPPORT end |
#load_extension(db, name, entry_point = nil) ⇒ Object
78 79 80 81 |
# File 'lib/sqlite3/driver.rb', line 78 def load_extension(db, name, entry_point = nil) result = API.sqlite3_load_extension(db, name, entry_point, nil) [result, nil] end |
#open(filename, utf_16 = false) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/sqlite3/driver.rb', line 5 def open(filename, utf_16 = false) handle = FFI::MemoryPointer.new(:pointer) if utf_16 filename = filename.encode(Encoding.utf_16native) if filename.respond_to?(:encode) result = API.sqlite3_open16(c_string(filename), handle) else filename = filename.encode(Encoding.utf_8) if filename.respond_to?(:encode) result = API.sqlite3_open(filename, handle) end [result, handle.get_pointer(0)] end |
#prepare(db, sql) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/sqlite3/driver.rb', line 30 def prepare(db, sql) handle = FFI::MemoryPointer.new(:pointer) remainder = FFI::MemoryPointer.new(:pointer) if Encoding.utf_16?(sql) str = c_string(sql) result = API.sqlite3_prepare16(db, str, str.bytesize, handle, remainder) remainder_string = get_string_utf_16(remainder.get_pointer(0)) else result = API.sqlite3_prepare(db, sql, sql.bytesize, handle, remainder) remainder_string = remainder.get_pointer(0).get_string(0) end [result, handle.get_pointer(0), remainder_string] end |