summaryrefslogtreecommitdiffstats
path: root/lib/sqlite3/driver
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sqlite3/driver')
-rw-r--r--lib/sqlite3/driver/dl/api.rb184
-rw-r--r--lib/sqlite3/driver/dl/driver.rb334
-rw-r--r--lib/sqlite3/driver/native/driver.rb218
3 files changed, 736 insertions, 0 deletions
diff --git a/lib/sqlite3/driver/dl/api.rb b/lib/sqlite3/driver/dl/api.rb
new file mode 100644
index 0000000..f892013
--- /dev/null
+++ b/lib/sqlite3/driver/dl/api.rb
@@ -0,0 +1,184 @@
+#--
+# =============================================================================
+# Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# * Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# * The names of its contributors may not be used to endorse or promote
+# products derived from this software without specific prior written
+# permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+# =============================================================================
+#++
+
+require 'dl/import'
+
+module SQLite3 ; module Driver; module DL;
+
+ module API
+ extend ::DL::Importable
+
+ library_name = case RUBY_PLATFORM.downcase
+ when /darwin/
+ "libsqlite3.dylib"
+ when /linux/
+ "libsqlite3.so"
+ when /win32/
+ "sqlite3.dll"
+ else
+ abort <<-EOF
+== * UNSUPPORTED PLATFORM ======================================================
+The platform '#{RUBY_PLATFORM}' is unsupported. Please help the author by
+editing the following file to allow your sqlite3 library to be found, and
+submitting a patch to jamis_buck@byu.edu. Thanks!
+
+#{__FILE__}
+=========================================================================== * ==
+ EOF
+ end
+
+ if defined? SQLITE3_LIB_PATH
+ library_name = File.join( SQLITE3_LIB_PATH, library_name )
+ end
+
+ dlload library_name
+
+ typealias "db", "void*"
+ typealias "stmt", "void*"
+ typealias "value", "void*"
+ typealias "context", "void*"
+
+ # until Ruby/DL supports 64-bit ints, we'll just treat them as 32-bit ints
+ typealias "int64", "unsigned long"
+
+ extern "const char *sqlite3_libversion()"
+
+ extern "int sqlite3_open(const char*,db*)"
+ extern "int sqlite3_open16(const void*,db*)"
+ extern "int sqlite3_close(db)"
+ extern "const char* sqlite3_errmsg(db)"
+ extern "void* sqlite3_errmsg16(db)"
+ extern "int sqlite3_errcode(db)"
+
+ extern "int sqlite3_prepare(db,const char*,int,stmt*,const char**)"
+ extern "int sqlite3_prepare16(db,const void*,int,stmt*,const void**)"
+ extern "int sqlite3_finalize(stmt)"
+ extern "int sqlite3_reset(stmt)"
+ extern "int sqlite3_step(stmt)"
+
+ extern "int64 sqlite3_last_insert_rowid(db)"
+ extern "int sqlite3_changes(db)"
+ extern "int sqlite3_total_changes(db)"
+ extern "void sqlite3_interrupt(db)"
+ extern "ibool sqlite3_complete(const char*)"
+ extern "ibool sqlite3_complete16(const void*)"
+
+ extern "int sqlite3_busy_handler(db,void*,void*)"
+ extern "int sqlite3_busy_timeout(db,int)"
+
+ extern "int sqlite3_set_authorizer(db,void*,void*)"
+ extern "void* sqlite3_trace(db,void*,void*)"
+
+ extern "int sqlite3_bind_blob(stmt,int,const void*,int,void*)"
+ extern "int sqlite3_bind_double(stmt,int,double)"
+ extern "int sqlite3_bind_int(stmt,int,int)"
+ extern "int sqlite3_bind_int64(stmt,int,int64)"
+ extern "int sqlite3_bind_null(stmt,int)"
+ extern "int sqlite3_bind_text(stmt,int,const char*,int,void*)"
+ extern "int sqlite3_bind_text16(stmt,int,const void*,int,void*)"
+ #extern "int sqlite3_bind_value(stmt,int,value)"
+
+ extern "int sqlite3_bind_parameter_count(stmt)"
+ extern "const char* sqlite3_bind_parameter_name(stmt,int)"
+ extern "int sqlite3_bind_parameter_index(stmt,const char*)"
+
+ extern "int sqlite3_column_count(stmt)"
+ extern "int sqlite3_data_count(stmt)"
+
+ extern "const void *sqlite3_column_blob(stmt,int)"
+ extern "int sqlite3_column_bytes(stmt,int)"
+ extern "int sqlite3_column_bytes16(stmt,int)"
+ extern "const char *sqlite3_column_decltype(stmt,int)"
+ extern "void *sqlite3_column_decltype16(stmt,int)"
+ extern "double sqlite3_column_double(stmt,int)"
+ extern "int sqlite3_column_int(stmt,int)"
+ extern "int64 sqlite3_column_int64(stmt,int)"
+ extern "const char *sqlite3_column_name(stmt,int)"
+ extern "const void *sqlite3_column_name16(stmt,int)"
+ extern "const char *sqlite3_column_text(stmt,int)"
+ extern "const void *sqlite3_column_text16(stmt,int)"
+ extern "int sqlite3_column_type(stmt,int)"
+
+ extern "int sqlite3_create_function(db,const char*,int,int,void*,void*,void*,void*)"
+ extern "int sqlite3_create_function16(db,const void*,int,int,void*,void*,void*,void*)"
+ extern "int sqlite3_aggregate_count(context)"
+
+ extern "const void *sqlite3_value_blob(value)"
+ extern "int sqlite3_value_bytes(value)"
+ extern "int sqlite3_value_bytes16(value)"
+ extern "double sqlite3_value_double(value)"
+ extern "int sqlite3_value_int(value)"
+ extern "int64 sqlite3_value_int64(value)"
+ extern "const char* sqlite3_value_text(value)"
+ extern "const void* sqlite3_value_text16(value)"
+ extern "const void* sqlite3_value_text16le(value)"
+ extern "const void* sqlite3_value_text16be(value)"
+ extern "int sqlite3_value_type(value)"
+
+ extern "void *sqlite3_aggregate_context(context,int)"
+ extern "void *sqlite3_user_data(context)"
+ extern "void *sqlite3_get_auxdata(context,int)"
+ extern "void sqlite3_set_auxdata(context,int,void*,void*)"
+
+ extern "void sqlite3_result_blob(context,const void*,int,void*)"
+ extern "void sqlite3_result_double(context,double)"
+ extern "void sqlite3_result_error(context,const char*,int)"
+ extern "void sqlite3_result_error16(context,const void*,int)"
+ extern "void sqlite3_result_int(context,int)"
+ extern "void sqlite3_result_int64(context,int64)"
+ extern "void sqlite3_result_null(context)"
+ extern "void sqlite3_result_text(context,const char*,int,void*)"
+ extern "void sqlite3_result_text16(context,const void*,int,void*)"
+ extern "void sqlite3_result_text16le(context,const void*,int,void*)"
+ extern "void sqlite3_result_text16be(context,const void*,int,void*)"
+ extern "void sqlite3_result_value(context,value)"
+
+ extern "int sqlite3_create_collation(db,const char*,int,void*,void*)"
+ extern "int sqlite3_create_collation16(db,const char*,int,void*,void*)"
+ extern "int sqlite3_collation_needed(db,void*,void*)"
+ extern "int sqlite3_collation_needed16(db,void*,void*)"
+
+ # ==== CRYPTO (NOT IN PUBLIC RELEASE) ====
+ if defined?( CRYPTO_API ) && CRYPTO_API
+ extern "int sqlite3_key(db,void*,int)"
+ extern "int sqlite3_rekey(db,void*,int)"
+ end
+
+ # ==== EXPERIMENTAL ====
+ if defined?( EXPERIMENTAL_API ) && EXPERIMENTAL_API
+ extern "int sqlite3_progress_handler(db,int,void*,void*)"
+ extern "int sqlite3_commit_hook(db,void*,void*)"
+ end
+
+ end
+
+end ; end ; end
diff --git a/lib/sqlite3/driver/dl/driver.rb b/lib/sqlite3/driver/dl/driver.rb
new file mode 100644
index 0000000..ab24a13
--- /dev/null
+++ b/lib/sqlite3/driver/dl/driver.rb
@@ -0,0 +1,334 @@
+#--
+# =============================================================================
+# Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# * Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# * The names of its contributors may not be used to endorse or promote
+# products derived from this software without specific prior written
+# permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+# =============================================================================
+#++
+
+require 'sqlite3/driver/dl/api'
+
+module Kernel
+ # Allows arbitrary objects to be passed as a pointer to functions.
+ # (Probably not very GC safe, but by encapsulating it like this we
+ # can change the implementation later.)
+ def to_ptr
+ ptr = DL.malloc(DL.sizeof("L"))
+ ptr.set_object self
+ ptr
+ end
+end
+
+class DL::PtrData
+ # The inverse of the Kernel#to_ptr operation.
+ def to_object
+ n = to_s(4).unpack("L").first
+ return nil if n < 1
+ ObjectSpace._id2ref(n) rescue self.to_s
+ end
+
+ def set_object(obj)
+ self[0] = [obj.object_id].pack("L")
+ end
+end
+
+module SQLite3 ; module Driver ; module DL
+
+ class Driver
+ STATIC = ::DL::PtrData.new(0)
+ TRANSIENT = ::DL::PtrData.new(-1)
+
+ def open( filename, utf16=false )
+ handle = ::DL::PtrData.new(0)
+ result = API.send( ( utf16 ? :sqlite3_open16 : :sqlite3_open ),
+ filename+"\0", handle.ref )
+ [ result, handle ]
+ end
+
+ def errmsg( db, utf16=false )
+ if utf16
+ msg = API.sqlite3_errmsg16( db )
+ msg.free = nil
+ msg.to_s(utf16_length(msg))
+ else
+ API.sqlite3_errmsg( db )
+ end
+ end
+
+ def prepare( db, sql, utf16=false )
+ handle = ::DL::PtrData.new(0)
+ remainder = ::DL::PtrData.new(0)
+
+ result = API.send( ( utf16 ? :sqlite3_prepare16 : :sqlite3_prepare ),
+ db, sql+"\0", sql.length, handle.ref, remainder.ref )
+
+ args = utf16 ? [ utf16_length(remainder) ] : []
+ remainder = remainder.to_s( *args )
+
+ [ result, handle, remainder ]
+ end
+
+ def complete?( sql, utf16=false )
+ API.send( utf16 ? :sqlite3_complete16 : :sqlite3_complete, sql+"\0" )
+ end
+
+ def value_blob( value )
+ blob = API.sqlite3_value_blob( value )
+ blob.free = nil
+ blob.to_s( API.sqlite3_value_bytes( value ) )
+ end
+
+ def value_text( value, utf16=false )
+ method = case utf16
+ when nil, false then :sqlite3_value_text
+ when :le then :sqlite3_value_text16le
+ when :be then :sqlite3_value_text16be
+ else :sqlite3_value_text16
+ end
+
+ result = API.send( method, value )
+ if utf16
+ result.free = nil
+ size = API.sqlite3_value_bytes( value )
+ result = result.to_s( size )
+ end
+
+ result
+ end
+
+ def column_blob( stmt, column )
+ blob = API.sqlite3_column_blob( stmt, column )
+ blob.free = nil
+ blob.to_s( API.sqlite3_column_bytes( stmt, column ) )
+ end
+
+ def result_text( func, text, utf16=false )
+ method = case utf16
+ when false, nil then :sqlite3_result_text
+ when :le then :sqlite3_result_text16le
+ when :be then :sqlite3_result_text16be
+ else :sqlite3_result_text16
+ end
+
+ s = text.to_s
+ API.send( method, func, s, s.length, TRANSIENT )
+ end
+
+ def busy_handler( db, data=nil, &block )
+ @busy_handler = block
+
+ unless @busy_handler_callback
+ @busy_handler_callback = ::DL.callback( "IPI" ) do |cookie, timeout|
+ @busy_handler.call( cookie, timeout ) || 0
+ end
+ end
+
+ API.sqlite3_busy_handler( db, block&&@busy_handler_callback, data )
+ end
+
+ def set_authorizer( db, data=nil, &block )
+ @authorizer_handler = block
+
+ unless @authorizer_handler_callback
+ @authorizer_handler_callback = ::DL.callback( "IPIPPPP"
+ ) do |cookie,mode,a,b,c,d|
+ @authorizer_handler.call( cookie, mode,
+ a&&a.to_s, b&&b.to_s, c&&c.to_s, d&&d.to_s ) || 0
+ end
+ end
+
+ API.sqlite3_set_authorizer( db, block&&@authorizer_handler_callback,
+ data )
+ end
+
+ def trace( db, data=nil, &block )
+ @trace_handler = block
+
+ unless @trace_handler_callback
+ @trace_handler_callback = ::DL.callback( "IPS" ) do |cookie,sql|
+ @trace_handler.call( cookie ? cookie.to_object : nil, sql ) || 0
+ end
+ end
+
+ API.sqlite3_trace( db, block&&@trace_handler_callback, data )
+ end
+
+ def create_function( db, name, args, text, cookie,
+ func, step, final )
+ # begin
+ if @func_handler_callback.nil? && func
+ @func_handler_callback = ::DL.callback( "0PIP" ) do |context,nargs,args|
+ args = args.to_s(nargs*4).unpack("L*").map {|i| ::DL::PtrData.new(i)}
+ data = API.sqlite3_user_data( context ).to_object
+ data[:func].call( context, *args )
+ end
+ end
+
+ if @step_handler_callback.nil? && step
+ @step_handler_callback = ::DL.callback( "0PIP" ) do |context,nargs,args|
+ args = args.to_s(nargs*4).unpack("L*").map {|i| ::DL::PtrData.new(i)}
+ data = API.sqlite3_user_data( context ).to_object
+ data[:step].call( context, *args )
+ end
+ end
+
+ if @final_handler_callback.nil? && final
+ @final_handler_callback = ::DL.callback( "0P" ) do |context|
+ data = API.sqlite3_user_data( context ).to_object
+ data[:final].call( context )
+ end
+ end
+
+ data = { :cookie => cookie,
+ :name => name,
+ :func => func,
+ :step => step,
+ :final => final }
+
+ API.sqlite3_create_function( db, name, args, text, data,
+ ( func ? @func_handler_callback : nil ),
+ ( step ? @step_handler_callback : nil ),
+ ( final ? @final_handler_callback : nil ) )
+ end
+
+ def aggregate_context( context )
+ ptr = API.sqlite3_aggregate_context( context, 4 )
+ ptr.free = nil
+ obj = ( ptr ? ptr.to_object : nil )
+ if obj.nil?
+ obj = Hash.new
+ ptr.set_object obj
+ end
+ obj
+ end
+
+ def bind_blob( stmt, index, value )
+ s = value.to_s
+ API.sqlite3_bind_blob( stmt, index, s, s.length, TRANSIENT )
+ end
+
+ def bind_text( stmt, index, value, utf16=false )
+ s = value.to_s
+ method = ( utf16 ? :sqlite3_bind_text16 : :sqlite3_bind_text )
+ API.send( method, stmt, index, s, s.length, TRANSIENT )
+ end
+
+ def column_text( stmt, column )
+ result = API.sqlite3_column_text( stmt, column )
+ result ? result.to_s : nil
+ end
+
+ def column_name( stmt, column )
+ result = API.sqlite3_column_name( stmt, column )
+ result ? result.to_s : nil
+ end
+
+ def column_decltype( stmt, column )
+ result = API.sqlite3_column_decltype( stmt, column )
+ result ? result.to_s : nil
+ end
+
+ def self.api_delegate( name )
+ define_method( name ) { |*args| API.send( "sqlite3_#{name}", *args ) }
+ end
+
+ api_delegate :aggregate_count
+ api_delegate :bind_double
+ api_delegate :bind_int
+ api_delegate :bind_null
+ api_delegate :bind_parameter_index
+ api_delegate :bind_parameter_name
+ api_delegate :busy_timeout
+ api_delegate :changes
+ api_delegate :close
+ api_delegate :column_bytes
+ api_delegate :column_bytes16
+ api_delegate :column_count
+ api_delegate :column_double
+ api_delegate :column_int
+ api_delegate :column_int64
+ api_delegate :column_type
+ api_delegate :data_count
+ api_delegate :errcode
+ api_delegate :finalize
+ api_delegate :interrupt
+ api_delegate :last_insert_rowid
+ api_delegate :libversion
+ api_delegate :reset
+ api_delegate :result_error
+ api_delegate :step
+ api_delegate :total_changes
+ api_delegate :value_bytes
+ api_delegate :value_bytes16
+ api_delegate :value_double
+ api_delegate :value_int
+ api_delegate :value_int64
+ api_delegate :value_type
+
+ # ==== EXPERIMENTAL ====
+ if defined?( EXPERIMENTAL_API ) && EXPERIMENTAL_API
+ def progress_handler( db, n, data=nil, &block )
+ @progress_handler = block
+
+ unless @progress_handler_callback
+ @progress_handler_callback = ::DL.callback( "IP" ) do |cookie|
+ @progress_handler.call( cookie )
+ end
+ end
+
+ API.sqlite3_progress_handler( db, n, block&&@progress_handler_callback,
+ data )
+ end
+
+ def commit_hook( db, data=nil, &block )
+ @commit_hook_handler = block
+
+ unless @commit_hook_handler_callback
+ @commit_hook_handler_callback = ::DL.callback( "IP" ) do |cookie|
+ @commit_hook_handler.call( cookie )
+ end
+ end
+
+ API.sqlite3_commit_hook( db, block&&@commit_hook_handler_callback,
+ data )
+ end
+ end
+
+ private
+
+ def utf16_length(ptr)
+ len = 0
+ loop do
+ break if ptr[len,1] == "\0"
+ len += 2
+ end
+ len
+ end
+
+ end
+
+end ; end ; end
diff --git a/lib/sqlite3/driver/native/driver.rb b/lib/sqlite3/driver/native/driver.rb
new file mode 100644
index 0000000..ee5e4a7
--- /dev/null
+++ b/lib/sqlite3/driver/native/driver.rb
@@ -0,0 +1,218 @@
+#--
+# =============================================================================
+# Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# * Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# * The names of its contributors may not be used to endorse or promote
+# products derived from this software without specific prior written
+# permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+# =============================================================================
+#++
+
+require 'sqlite3_api'
+
+module SQLite3 ; module Driver ; module Native
+
+ class Driver
+
+ def initialize
+ @callback_data = Hash.new
+ end
+
+ def complete?( sql, utf16=false )
+ API.send( utf16 ? :sqlite3_complete16 : :sqlite3_complete, sql ) != 0
+ end
+
+ def busy_handler( db, data=nil, &block )
+ if block
+ cb = API::CallbackData.new
+ cb.proc = block
+ cb.data = data
+ end
+
+ API.sqlite3_busy_handler( db,
+ block ? API::Sqlite3_ruby_busy_handler : nil, cb )
+ end
+
+ def set_authorizer( db, data=nil, &block )
+ if block
+ cb = API::CallbackData.new
+ cb.proc = block
+ cb.data = data
+ end
+
+ API.sqlite3_set_authorizer( db,
+ block ? API::Sqlite3_ruby_authorizer : nil, cb )
+ end
+
+ def trace( db, data=nil, &block )
+ if block
+ cb = API::CallbackData.new
+ cb.proc = block
+ cb.data = data
+ end
+
+ API.sqlite3_trace( db,
+ block ? API::Sqlite3_ruby_trace : nil, cb )
+ end
+
+ def open( filename, utf16=false )
+ API.send( utf16 ? :sqlite3_open16 : :sqlite3_open, filename )
+ end
+
+ def errmsg( db, utf16=false )
+ API.send( utf16 ? :sqlite3_errmsg16 : :sqlite3_errmsg, db )
+ end
+
+ def prepare( db, sql, utf16=false )
+ API.send( ( utf16 ? :sqlite3_prepare16 : :sqlite3_prepare ),
+ db, sql )
+ end
+
+ def bind_text( stmt, index, value, utf16=false )
+ API.send( ( utf16 ? :sqlite3_bind_text16 : :sqlite3_bind_text ),
+ stmt, index, value )
+ end
+
+ def column_name( stmt, index, utf16=false )
+ API.send( ( utf16 ? :sqlite3_column_name16 : :sqlite3_column_name ),
+ stmt, index )
+ end
+
+ def column_decltype( stmt, index, utf16=false )
+ API.send(
+ ( utf16 ? :sqlite3_column_decltype16 : :sqlite3_column_decltype ),
+ stmt, index )
+ end
+
+ def column_text( stmt, index, utf16=false )
+ API.send( ( utf16 ? :sqlite3_column_text16 : :sqlite3_column_text ),
+ stmt, index )
+ end
+
+ def create_function( db, name, args, text, cookie, func, step, final )
+ if func || ( step && final )
+ cb = API::CallbackData.new
+ cb.proc = cb.proc2 = nil
+ cb.data = cookie
+ @callback_data[ name ] = cb
+ else
+ @callback_data.delete( name )
+ end
+
+ if func
+ cb.proc = func
+
+ func = API::Sqlite3_ruby_function_step
+ step = final = nil
+ elsif step && final
+ cb.proc = step
+ cb.proc2 = final
+
+ func = nil
+ step = API::Sqlite3_ruby_function_step
+ final = API::Sqlite3_ruby_function_final
+ end
+
+ API.sqlite3_create_function( db, name, args, text, cb, func, step, final )
+ end
+
+ def value_text( value, utf16=false )
+ method = case utf16
+ when nil, false then :sqlite3_value_text
+ when :le then :sqlite3_value_text16le
+ when :be then :sqlite3_value_text16be
+ else :sqlite3_value_text16
+ end
+
+ API.send( method, value )
+ end
+
+ def result_text( context, result, utf16=false )
+ method = case utf16
+ when nil, false then :sqlite3_result_text
+ when :le then :sqlite3_result_text16le
+ when :be then :sqlite3_result_text16be
+ else :sqlite3_result_text16
+ end
+
+ API.send( method, context, result.to_s )
+ end
+
+ def result_error( context, value, utf16=false )
+ API.send( ( utf16 ? :sqlite3_result_error16 : :sqlite3_result_error ),
+ context, value )
+ end
+
+ def self.api_delegate( name )
+ define_method( name ) { |*args| API.send( "sqlite3_#{name}", *args ) }
+ end
+
+ api_delegate :libversion
+ api_delegate :close
+ api_delegate :last_insert_rowid
+ api_delegate :changes
+ api_delegate :total_changes
+ api_delegate :interrupt
+ api_delegate :busy_timeout
+ api_delegate :errcode
+ api_delegate :bind_blob
+ api_delegate :bind_double
+ api_delegate :bind_int
+ api_delegate :bind_int64
+ api_delegate :bind_null
+ api_delegate :bind_parameter_count
+ api_delegate :bind_parameter_name
+ api_delegate :bind_parameter_index
+ api_delegate :column_count
+ api_delegate :step
+ api_delegate :data_count
+ api_delegate :column_blob
+ api_delegate :column_bytes
+ api_delegate :column_bytes16
+ api_delegate :column_double
+ api_delegate :column_int
+ api_delegate :column_int64
+ api_delegate :column_type
+ api_delegate :finalize
+ api_delegate :reset
+ api_delegate :aggregate_count
+ api_delegate :value_blob
+ api_delegate :value_bytes
+ api_delegate :value_bytes16
+ api_delegate :value_double
+ api_delegate :value_int
+ api_delegate :value_int64
+ api_delegate :value_type
+ api_delegate :result_blob
+ api_delegate :result_double
+ api_delegate :result_int
+ api_delegate :result_int64
+ api_delegate :result_null
+ api_delegate :result_value
+ api_delegate :aggregate_context
+
+ end
+
+end ; end ; end