Update vendored Sqlite3-ruby to 1.3.0
Also, some tweaks to Maruku.
This commit is contained in:
parent
9a80cacc34
commit
4f8759cdf3
55 changed files with 3071 additions and 8028 deletions
9
vendor/plugins/sqlite3-ruby/lib/sqlite3.rb
vendored
9
vendor/plugins/sqlite3-ruby/lib/sqlite3.rb
vendored
|
@ -1 +1,10 @@
|
|||
# support multiple ruby version (fat binaries under windows)
|
||||
begin
|
||||
RUBY_VERSION =~ /(\d+.\d+)/
|
||||
require "sqlite3/#{$1}/sqlite3_native"
|
||||
rescue LoadError
|
||||
require 'sqlite3/sqlite3_native'
|
||||
end
|
||||
|
||||
require 'sqlite3/database'
|
||||
require 'sqlite3/version'
|
||||
|
|
465
vendor/plugins/sqlite3-ruby/lib/sqlite3/database.rb
vendored
465
vendor/plugins/sqlite3-ruby/lib/sqlite3/database.rb
vendored
|
@ -35,7 +35,7 @@ module SQLite3
|
|||
class Database
|
||||
include Pragmas
|
||||
|
||||
class <<self
|
||||
class << self
|
||||
|
||||
alias :open :new
|
||||
|
||||
|
@ -48,12 +48,6 @@ module SQLite3
|
|||
|
||||
end
|
||||
|
||||
# The low-level opaque database handle that this object wraps.
|
||||
attr_reader :handle
|
||||
|
||||
# A reference to the underlying SQLite3 driver used by this database.
|
||||
attr_reader :driver
|
||||
|
||||
# A boolean that indicates whether rows in result sets should be returned
|
||||
# as hashes or not. By default, rows are returned as arrays.
|
||||
attr_accessor :results_as_hash
|
||||
|
@ -62,54 +56,6 @@ module SQLite3
|
|||
# database.
|
||||
attr_accessor :type_translation
|
||||
|
||||
# Create a new Database object that opens the given file. If utf16
|
||||
# is +true+, the filename is interpreted as a UTF-16 encoded string.
|
||||
#
|
||||
# By default, the new database will return result rows as arrays
|
||||
# (#results_as_hash) and has type translation disabled (#type_translation=).
|
||||
def initialize( file_name, options={} ) # :yields: db
|
||||
utf16 = options.fetch(:utf16, false)
|
||||
load_driver( options[:driver] )
|
||||
|
||||
@statement_factory = options[:statement_factory] || Statement
|
||||
|
||||
result, @handle = @driver.open( file_name, utf16 )
|
||||
Error.check( result, self, "could not open database" )
|
||||
|
||||
@closed = false
|
||||
@results_as_hash = options.fetch(:results_as_hash,false)
|
||||
@type_translation = options.fetch(:type_translation,false)
|
||||
@translator = nil
|
||||
@transaction_active = false
|
||||
|
||||
if block_given?
|
||||
begin
|
||||
yield self
|
||||
ensure
|
||||
self.close
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Return +true+ if the string is a valid (ie, parsable) SQL statement, and
|
||||
# +false+ otherwise. If +utf16+ is +true+, then the string is a UTF-16
|
||||
# character string.
|
||||
def complete?( string, utf16=false )
|
||||
@driver.complete?( string, utf16 )
|
||||
end
|
||||
|
||||
# Return a string describing the last error to have occurred with this
|
||||
# database.
|
||||
def errmsg( utf16=false )
|
||||
@driver.errmsg( @handle, utf16 )
|
||||
end
|
||||
|
||||
# Return an integer representing the last error to have occurred with this
|
||||
# database.
|
||||
def errcode
|
||||
@driver.errcode( @handle )
|
||||
end
|
||||
|
||||
# Return the type translator employed by this database instance. Each
|
||||
# database instance has its own type translator; this allows for different
|
||||
# type handlers to be installed in each instance without affecting other
|
||||
|
@ -120,35 +66,12 @@ module SQLite3
|
|||
@translator ||= Translator.new
|
||||
end
|
||||
|
||||
# Closes this database.
|
||||
def close
|
||||
unless @closed
|
||||
result = @driver.close( @handle )
|
||||
Error.check( result, self )
|
||||
end
|
||||
@closed = true
|
||||
end
|
||||
|
||||
# Returns +true+ if this database instance has been closed (see #close).
|
||||
def closed?
|
||||
@closed
|
||||
end
|
||||
|
||||
# Installs (or removes) a block that will be invoked for every SQL
|
||||
# statement executed. The block receives a two parameters: the +data+
|
||||
# argument, and the SQL statement executed. If the block is +nil+,
|
||||
# any existing tracer will be uninstalled.
|
||||
def trace( data=nil, &block )
|
||||
@driver.trace( @handle, data, &block )
|
||||
end
|
||||
|
||||
# Installs (or removes) a block that will be invoked for every access
|
||||
# to the database. If the block returns 0 (or +nil+), the statement
|
||||
# is allowed to proceed. Returning 1 causes an authorization error to
|
||||
# occur, and returning 2 causes the access to be silently denied.
|
||||
def authorizer( data=nil, &block )
|
||||
result = @driver.set_authorizer( @handle, data, &block )
|
||||
Error.check( result, self )
|
||||
def authorizer( &block )
|
||||
self.authorizer = block
|
||||
end
|
||||
|
||||
# Returns a Statement object representing the given SQL. This does not
|
||||
|
@ -156,16 +79,14 @@ module SQLite3
|
|||
#
|
||||
# The Statement can then be executed using Statement#execute.
|
||||
#
|
||||
def prepare( sql )
|
||||
stmt = @statement_factory.new( self, sql )
|
||||
if block_given?
|
||||
begin
|
||||
yield stmt
|
||||
ensure
|
||||
stmt.close
|
||||
end
|
||||
else
|
||||
return stmt
|
||||
def prepare sql
|
||||
stmt = SQLite3::Statement.new( self, sql )
|
||||
return stmt unless block_given?
|
||||
|
||||
begin
|
||||
yield stmt
|
||||
ensure
|
||||
stmt.close
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -183,13 +104,55 @@ module SQLite3
|
|||
#
|
||||
# See also #execute2, #query, and #execute_batch for additional ways of
|
||||
# executing statements.
|
||||
def execute( sql, *bind_vars )
|
||||
prepare( sql ) do |stmt|
|
||||
result = stmt.execute( *bind_vars )
|
||||
if block_given?
|
||||
result.each { |row| yield row }
|
||||
def execute sql, bind_vars = [], *args, &block
|
||||
# FIXME: This is a terrible hack and should be removed but is required
|
||||
# for older versions of rails
|
||||
hack = Object.const_defined?(:ActiveRecord) && sql =~ /^PRAGMA index_list/
|
||||
|
||||
if bind_vars.nil? || !args.empty?
|
||||
if args.empty?
|
||||
bind_vars = []
|
||||
else
|
||||
return result.inject( [] ) { |arr,row| arr << row; arr }
|
||||
bind_vars = [nil] + args
|
||||
end
|
||||
|
||||
warn(<<-eowarn) if $VERBOSE
|
||||
#{caller[0]} is calling SQLite3::Database#execute with nil or multiple bind params
|
||||
without using an array. Please switch to passing bind parameters as an array.
|
||||
eowarn
|
||||
end
|
||||
|
||||
prepare( sql ) do |stmt|
|
||||
stmt.bind_params(bind_vars)
|
||||
if type_translation
|
||||
stmt = ResultSet.new(self, stmt).to_a
|
||||
end
|
||||
|
||||
if block_given?
|
||||
stmt.each do |row|
|
||||
if @results_as_hash
|
||||
h = Hash[*stmt.columns.zip(row).flatten]
|
||||
row.each_with_index { |r, i| h[i] = r }
|
||||
|
||||
yield h
|
||||
else
|
||||
yield row
|
||||
end
|
||||
end
|
||||
else
|
||||
if @results_as_hash
|
||||
stmt.map { |row|
|
||||
h = Hash[*stmt.columns.zip(row).flatten]
|
||||
row.each_with_index { |r, i| h[i] = r }
|
||||
|
||||
# FIXME UGH TERRIBLE HACK!
|
||||
h['unique'] = h['unique'].to_s if hack
|
||||
|
||||
h
|
||||
}
|
||||
else
|
||||
stmt.to_a
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -208,10 +171,10 @@ module SQLite3
|
|||
prepare( sql ) do |stmt|
|
||||
result = stmt.execute( *bind_vars )
|
||||
if block_given?
|
||||
yield result.columns
|
||||
yield stmt.columns
|
||||
result.each { |row| yield row }
|
||||
else
|
||||
return result.inject( [ result.columns ] ) { |arr,row|
|
||||
return result.inject( [ stmt.columns ] ) { |arr,row|
|
||||
arr << row; arr }
|
||||
end
|
||||
end
|
||||
|
@ -225,11 +188,40 @@ module SQLite3
|
|||
#
|
||||
# This always returns +nil+, making it unsuitable for queries that return
|
||||
# rows.
|
||||
def execute_batch( sql, *bind_vars )
|
||||
def execute_batch( sql, bind_vars = [], *args )
|
||||
# FIXME: remove this stuff later
|
||||
unless [Array, Hash].include?(bind_vars.class)
|
||||
bind_vars = [bind_vars]
|
||||
warn(<<-eowarn) if $VERBOSE
|
||||
#{caller[0]} is calling SQLite3::Database#execute_batch with bind parameters
|
||||
that are not a list of a hash. Please switch to passing bind parameters as an
|
||||
array or hash.
|
||||
eowarn
|
||||
end
|
||||
|
||||
# FIXME: remove this stuff later
|
||||
if bind_vars.nil? || !args.empty?
|
||||
if args.empty?
|
||||
bind_vars = []
|
||||
else
|
||||
bind_vars = [nil] + args
|
||||
end
|
||||
|
||||
warn(<<-eowarn) if $VERBOSE
|
||||
#{caller[0]} is calling SQLite3::Database#execute_batch with nil or multiple bind params
|
||||
without using an array. Please switch to passing bind parameters as an array.
|
||||
eowarn
|
||||
end
|
||||
|
||||
sql = sql.strip
|
||||
until sql.empty? do
|
||||
prepare( sql ) do |stmt|
|
||||
stmt.execute( *bind_vars )
|
||||
# FIXME: this should probably use sqlite3's api for batch execution
|
||||
# This implementation requires stepping over the results.
|
||||
if bind_vars.length == stmt.bind_parameter_count
|
||||
stmt.bind_params(bind_vars)
|
||||
end
|
||||
stmt.step
|
||||
sql = stmt.remainder.strip
|
||||
end
|
||||
end
|
||||
|
@ -247,8 +239,22 @@ module SQLite3
|
|||
# returned, or you could have problems with locks on the table. If called
|
||||
# with a block, +close+ will be invoked implicitly when the block
|
||||
# terminates.
|
||||
def query( sql, *bind_vars )
|
||||
result = prepare( sql ).execute( *bind_vars )
|
||||
def query( sql, bind_vars = [], *args )
|
||||
|
||||
if bind_vars.nil? || !args.empty?
|
||||
if args.empty?
|
||||
bind_vars = []
|
||||
else
|
||||
bind_vars = [nil] + args
|
||||
end
|
||||
|
||||
warn(<<-eowarn) if $VERBOSE
|
||||
#{caller[0]} is calling SQLite3::Database#query with nil or multiple bind params
|
||||
without using an array. Please switch to passing bind parameters as an array.
|
||||
eowarn
|
||||
end
|
||||
|
||||
result = prepare( sql ).execute( bind_vars )
|
||||
if block_given?
|
||||
begin
|
||||
yield result
|
||||
|
@ -279,55 +285,7 @@ module SQLite3
|
|||
nil
|
||||
end
|
||||
|
||||
# Obtains the unique row ID of the last row to be inserted by this Database
|
||||
# instance.
|
||||
def last_insert_row_id
|
||||
@driver.last_insert_rowid( @handle )
|
||||
end
|
||||
|
||||
# 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
|
||||
|
||||
# Returns the total number of changes made to this database instance
|
||||
# since it was opened.
|
||||
def total_changes
|
||||
@driver.total_changes( @handle )
|
||||
end
|
||||
|
||||
# Interrupts the currently executing operation, causing it to abort.
|
||||
def interrupt
|
||||
@driver.interrupt( @handle )
|
||||
end
|
||||
|
||||
# Register a busy handler with this database instance. When a requested
|
||||
# resource is busy, this handler will be invoked. If the handler returns
|
||||
# +false+, the operation will be aborted; otherwise, the resource will
|
||||
# be requested again.
|
||||
#
|
||||
# The handler will be invoked with the name of the resource that was
|
||||
# busy, and the number of times it has been retried.
|
||||
#
|
||||
# See also the mutually exclusive #busy_timeout.
|
||||
def busy_handler( data=nil, &block ) # :yields: data, retries
|
||||
result = @driver.busy_handler( @handle, data, &block )
|
||||
Error.check( result, self )
|
||||
end
|
||||
|
||||
# Indicates that if a request for a resource terminates because that
|
||||
# resource is busy, SQLite should sleep and retry for up to the indicated
|
||||
# number of milliseconds. By default, SQLite does not retry
|
||||
# busy resources. To restore the default behavior, send 0 as the
|
||||
# +ms+ parameter.
|
||||
#
|
||||
# See also the mutually exclusive #busy_handler.
|
||||
def busy_timeout( ms )
|
||||
result = @driver.busy_timeout( @handle, ms )
|
||||
Error.check( result, self )
|
||||
end
|
||||
alias :busy_timeout :busy_timeout=
|
||||
|
||||
# Creates a new function for use in SQL statements. It will be added as
|
||||
# +name+, with the given +arity+. (For variable arity functions, use
|
||||
|
@ -352,23 +310,12 @@ module SQLite3
|
|||
# end
|
||||
#
|
||||
# puts db.get_first_value( "select maim(name) from table" )
|
||||
def create_function( name, arity, text_rep=Constants::TextRep::ANY,
|
||||
&block ) # :yields: func, *args
|
||||
# begin
|
||||
callback = proc do |func,*args|
|
||||
begin
|
||||
block.call( FunctionProxy.new( @driver, func ),
|
||||
*args.map{|v| Value.new(self,v)} )
|
||||
rescue StandardError, Exception => e
|
||||
@driver.result_error( func,
|
||||
"#{e.message} (#{e.class})", -1 )
|
||||
end
|
||||
def create_function name, arity, text_rep=Constants::TextRep::ANY, &block
|
||||
define_function(name) do |*args|
|
||||
fp = FunctionProxy.new
|
||||
block.call(fp, *args)
|
||||
fp.result
|
||||
end
|
||||
|
||||
result = @driver.create_function( @handle, name, arity, text_rep, nil,
|
||||
callback, nil, nil )
|
||||
Error.check( result, self )
|
||||
|
||||
self
|
||||
end
|
||||
|
||||
|
@ -410,47 +357,40 @@ module SQLite3
|
|||
# aggregate functions.
|
||||
def create_aggregate( name, arity, step=nil, finalize=nil,
|
||||
text_rep=Constants::TextRep::ANY, &block )
|
||||
# begin
|
||||
if block
|
||||
proxy = AggregateDefinitionProxy.new
|
||||
proxy.instance_eval(&block)
|
||||
step ||= proxy.step_callback
|
||||
finalize ||= proxy.finalize_callback
|
||||
end
|
||||
|
||||
step_callback = proc do |func,*args|
|
||||
ctx = @driver.aggregate_context( func )
|
||||
unless ctx[:__error]
|
||||
begin
|
||||
step.call( FunctionProxy.new( @driver, func, ctx ),
|
||||
*args.map{|v| Value.new(self,v)} )
|
||||
rescue Exception => e
|
||||
ctx[:__error] = e
|
||||
end
|
||||
factory = Class.new do
|
||||
def self.step( &block )
|
||||
define_method(:step, &block)
|
||||
end
|
||||
|
||||
def self.finalize( &block )
|
||||
define_method(:finalize, &block)
|
||||
end
|
||||
end
|
||||
|
||||
finalize_callback = proc do |func|
|
||||
ctx = @driver.aggregate_context( func )
|
||||
unless ctx[:__error]
|
||||
begin
|
||||
finalize.call( FunctionProxy.new( @driver, func, ctx ) )
|
||||
rescue Exception => e
|
||||
@driver.result_error( func,
|
||||
"#{e.message} (#{e.class})", -1 )
|
||||
end
|
||||
else
|
||||
e = ctx[:__error]
|
||||
@driver.result_error( func,
|
||||
"#{e.message} (#{e.class})", -1 )
|
||||
if block_given?
|
||||
factory.instance_eval(&block)
|
||||
else
|
||||
factory.class_eval do
|
||||
define_method(:step, step)
|
||||
define_method(:finalize, finalize)
|
||||
end
|
||||
end
|
||||
|
||||
result = @driver.create_function( @handle, name, arity, text_rep, nil,
|
||||
nil, step_callback, finalize_callback )
|
||||
Error.check( result, self )
|
||||
proxy = factory.new
|
||||
proxy.extend(Module.new {
|
||||
attr_accessor :ctx
|
||||
|
||||
self
|
||||
def step( *args )
|
||||
super(@ctx, *args)
|
||||
end
|
||||
|
||||
def finalize
|
||||
super(@ctx)
|
||||
end
|
||||
})
|
||||
proxy.ctx = FunctionProxy.new
|
||||
define_aggregator(name, proxy)
|
||||
end
|
||||
|
||||
# This is another approach to creating an aggregate function (see
|
||||
|
@ -500,47 +440,22 @@ module SQLite3
|
|||
# db.create_aggregate_handler( LengthsAggregateHandler )
|
||||
# puts db.get_first_value( "select lengths(name) from A" )
|
||||
def create_aggregate_handler( handler )
|
||||
arity = -1
|
||||
text_rep = Constants::TextRep::ANY
|
||||
proxy = Class.new do
|
||||
def initialize handler
|
||||
@handler = handler
|
||||
@fp = FunctionProxy.new
|
||||
end
|
||||
|
||||
arity = handler.arity if handler.respond_to?(:arity)
|
||||
text_rep = handler.text_rep if handler.respond_to?(:text_rep)
|
||||
name = handler.name
|
||||
def step( *args )
|
||||
@handler.step(@fp, *args)
|
||||
end
|
||||
|
||||
step = proc do |func,*args|
|
||||
ctx = @driver.aggregate_context( func )
|
||||
unless ctx[ :__error ]
|
||||
ctx[ :handler ] ||= handler.new
|
||||
begin
|
||||
ctx[ :handler ].step( FunctionProxy.new( @driver, func, ctx ),
|
||||
*args.map{|v| Value.new(self,v)} )
|
||||
rescue Exception, StandardError => e
|
||||
ctx[ :__error ] = e
|
||||
end
|
||||
def finalize
|
||||
@handler.finalize @fp
|
||||
@fp.result
|
||||
end
|
||||
end
|
||||
|
||||
finalize = proc do |func|
|
||||
ctx = @driver.aggregate_context( func )
|
||||
unless ctx[ :__error ]
|
||||
ctx[ :handler ] ||= handler.new
|
||||
begin
|
||||
ctx[ :handler ].finalize( FunctionProxy.new( @driver, func, ctx ) )
|
||||
rescue Exception => e
|
||||
ctx[ :__error ] = e
|
||||
end
|
||||
end
|
||||
|
||||
if ctx[ :__error ]
|
||||
e = ctx[ :__error ]
|
||||
@driver.sqlite3_result_error( func, "#{e.message} (#{e.class})", -1 )
|
||||
end
|
||||
end
|
||||
|
||||
result = @driver.create_function( @handle, name, arity, text_rep, nil,
|
||||
nil, step, finalize )
|
||||
Error.check( result, self )
|
||||
|
||||
define_aggregator(handler.name, proxy.new(handler.new))
|
||||
self
|
||||
end
|
||||
|
||||
|
@ -604,33 +519,6 @@ module SQLite3
|
|||
@transaction_active
|
||||
end
|
||||
|
||||
# Loads the corresponding driver, or if it is nil, attempts to locate a
|
||||
# suitable driver.
|
||||
def load_driver( driver )
|
||||
case driver
|
||||
when Class
|
||||
# do nothing--use what was given
|
||||
when Symbol, String
|
||||
require "sqlite3/driver/#{driver.to_s.downcase}/driver"
|
||||
driver = SQLite3::Driver.const_get( driver )::Driver
|
||||
else
|
||||
[ "Native", "DL" ].each do |d|
|
||||
begin
|
||||
require "sqlite3/driver/#{d.downcase}/driver"
|
||||
driver = SQLite3::Driver.const_get( d )::Driver
|
||||
break
|
||||
rescue SyntaxError
|
||||
raise
|
||||
rescue ScriptError, Exception, NameError
|
||||
end
|
||||
end
|
||||
raise "no driver for sqlite3 found" unless driver
|
||||
end
|
||||
|
||||
@driver = driver.new
|
||||
end
|
||||
private :load_driver
|
||||
|
||||
# A helper class for dealing with custom functions (see #create_function,
|
||||
# #create_aggregate, and #create_aggregate_handler). It encapsulates the
|
||||
# opaque function object that represents the current invocation. It also
|
||||
|
@ -640,26 +528,15 @@ module SQLite3
|
|||
# This class will almost _always_ be instantiated indirectly, by working
|
||||
# with the create methods mentioned above.
|
||||
class FunctionProxy
|
||||
attr_accessor :result
|
||||
|
||||
# Create a new FunctionProxy that encapsulates the given +func+ object.
|
||||
# If context is non-nil, the functions context will be set to that. If
|
||||
# it is non-nil, it must quack like a Hash. If it is nil, then none of
|
||||
# the context functions will be available.
|
||||
def initialize( driver, func, context=nil )
|
||||
@driver = driver
|
||||
@func = func
|
||||
@context = context
|
||||
end
|
||||
|
||||
# Calls #set_result to set the result of this function.
|
||||
def result=( result )
|
||||
set_result( result )
|
||||
end
|
||||
|
||||
# Set the result of the function to the given value. The function will
|
||||
# then return this value.
|
||||
def set_result( result, utf16=false )
|
||||
@driver.result_text( @func, result, utf16 )
|
||||
def initialize
|
||||
@result = nil
|
||||
@context = {}
|
||||
end
|
||||
|
||||
# Set the result of the function to the given error message.
|
||||
|
@ -672,50 +549,20 @@ module SQLite3
|
|||
# that the aggregate has processed so far. This will include the current
|
||||
# row, and so will always return at least 1.
|
||||
def count
|
||||
ensure_aggregate!
|
||||
@driver.aggregate_count( @func )
|
||||
end
|
||||
|
||||
# Returns the value with the given key from the context. This is only
|
||||
# available to aggregate functions.
|
||||
def []( key )
|
||||
ensure_aggregate!
|
||||
@context[ key ]
|
||||
end
|
||||
|
||||
# Sets the value with the given key in the context. This is only
|
||||
# available to aggregate functions.
|
||||
def []=( key, value )
|
||||
ensure_aggregate!
|
||||
@context[ key ] = value
|
||||
end
|
||||
|
||||
# A function for performing a sanity check, to ensure that the function
|
||||
# being invoked is an aggregate function. This is implied by the
|
||||
# existence of the context variable.
|
||||
def ensure_aggregate!
|
||||
unless @context
|
||||
raise MisuseException, "function is not an aggregate"
|
||||
end
|
||||
end
|
||||
private :ensure_aggregate!
|
||||
|
||||
end
|
||||
|
||||
# A proxy used for defining the callbacks to an aggregate function.
|
||||
class AggregateDefinitionProxy # :nodoc:
|
||||
attr_reader :step_callback, :finalize_callback
|
||||
|
||||
def step( &block )
|
||||
@step_callback = block
|
||||
end
|
||||
|
||||
def finalize( &block )
|
||||
@finalize_callback = block
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
|
|
@ -1,152 +0,0 @@
|
|||
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/, /freebsd|netbsd|openbsd|dragonfly/, /solaris/
|
||||
"libsqlite3.so"
|
||||
when /mswin|mingw/
|
||||
"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
|
|
@ -1,307 +0,0 @@
|
|||
require 'sqlite3/driver/dl/api'
|
||||
|
||||
warn "The DL driver for sqlite3-ruby is deprecated and will be removed"
|
||||
warn "in a future release. Please update your installation to use the"
|
||||
warn "Native driver."
|
||||
|
||||
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_int64
|
||||
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
|
|
@ -1,219 +0,0 @@
|
|||
# support multiple ruby version (fat binaries under windows)
|
||||
begin
|
||||
require 'sqlite3_api'
|
||||
rescue LoadError
|
||||
if RUBY_PLATFORM =~ /mingw|mswin/ then
|
||||
RUBY_VERSION =~ /(\d+.\d+)/
|
||||
require "#{$1}/sqlite3_api"
|
||||
end
|
||||
end
|
||||
|
||||
module SQLite3 ; module Driver ; module Native
|
||||
|
||||
class Driver
|
||||
|
||||
def initialize
|
||||
@callback_data = Hash.new
|
||||
@authorizer = Hash.new
|
||||
@busy_handler = Hash.new
|
||||
@trace = 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
|
||||
result = API.sqlite3_busy_handler( db, API::Sqlite3_ruby_busy_handler, cb )
|
||||
# Reference the Callback object so that
|
||||
# it is not deleted by the GC
|
||||
@busy_handler[db] = cb
|
||||
else
|
||||
# Unreference the callback *after* having removed it
|
||||
# from sqlite
|
||||
result = API.sqlite3_busy_handler( db, nil, nil )
|
||||
@busy_handler.delete(db)
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
def set_authorizer( db, data=nil, &block )
|
||||
if block
|
||||
cb = API::CallbackData.new
|
||||
cb.proc = block
|
||||
cb.data = data
|
||||
result = API.sqlite3_set_authorizer( db, API::Sqlite3_ruby_authorizer, cb )
|
||||
@authorizer[db] = cb # see comments in busy_handler
|
||||
else
|
||||
result = API.sqlite3_set_authorizer( db, nil, nil )
|
||||
@authorizer.delete(db) # see comments in busy_handler
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
def trace( db, data=nil, &block )
|
||||
if block
|
||||
cb = API::CallbackData.new
|
||||
cb.proc = block
|
||||
cb.data = data
|
||||
result = API.sqlite3_trace( db, API::Sqlite3_ruby_trace, cb )
|
||||
@trace[db] = cb # see comments in busy_handler
|
||||
else
|
||||
result = API.sqlite3_trace( db, nil, nil )
|
||||
@trace.delete(db) # see comments in busy_handler
|
||||
end
|
||||
|
||||
result
|
||||
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.to_s )
|
||||
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
|
||||
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
|
||||
|
||||
result = API.sqlite3_create_function( db, name, args, text, cb, func, step, final )
|
||||
|
||||
# see comments in busy_handler
|
||||
if cb
|
||||
@callback_data[ name ] = cb
|
||||
else
|
||||
@callback_data.delete( name )
|
||||
end
|
||||
|
||||
return result
|
||||
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 )
|
||||
eval "def #{name} (*args) API.sqlite3_#{name}( *args ) ; end"
|
||||
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
|
|
@ -1,7 +1,6 @@
|
|||
require 'sqlite3/constants'
|
||||
|
||||
module SQLite3
|
||||
|
||||
class Exception < ::StandardError
|
||||
@code = 0
|
||||
|
||||
|
@ -42,27 +41,4 @@ module SQLite3
|
|||
class FormatException < Exception; end
|
||||
class RangeException < Exception; end
|
||||
class NotADatabaseException < Exception; end
|
||||
|
||||
EXCEPTIONS = [
|
||||
nil,
|
||||
SQLException, InternalException, PermissionException,
|
||||
AbortException, BusyException, LockedException, MemoryException,
|
||||
ReadOnlyException, InterruptException, IOException, CorruptException,
|
||||
NotFoundException, FullException, CantOpenException, ProtocolException,
|
||||
EmptyException, SchemaChangedException, TooBigException,
|
||||
ConstraintException, MismatchException, MisuseException,
|
||||
UnsupportedException, AuthorizationException, FormatException,
|
||||
RangeException, NotADatabaseException
|
||||
].each_with_index { |e,i| e.instance_variable_set( :@code, i ) if e }
|
||||
|
||||
module Error
|
||||
def check( result, db=nil, msg=nil )
|
||||
unless result == Constants::ErrorCode::OK
|
||||
msg = ( msg ? msg + ": " : "" ) + db.errmsg if db
|
||||
raise(( EXCEPTIONS[result] || SQLite3::Exception ), msg)
|
||||
end
|
||||
end
|
||||
module_function :check
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -213,16 +213,24 @@ module SQLite3
|
|||
get_query_pragma "index_list", table, &block
|
||||
end
|
||||
|
||||
def table_info( table, &block ) # :yields: row
|
||||
columns, *rows = execute2("PRAGMA table_info(#{table})")
|
||||
###
|
||||
# Returns information about +table+. Yields each row of table information
|
||||
# if a block is provided.
|
||||
def table_info table
|
||||
stmt = prepare "PRAGMA table_info(#{table})"
|
||||
columns = stmt.columns
|
||||
|
||||
needs_tweak_default = version_compare(driver.libversion, "3.3.7") > 0
|
||||
needs_tweak_default =
|
||||
version_compare(SQLite3.libversion.to_s, "3.3.7") > 0
|
||||
|
||||
result = [] unless block_given?
|
||||
rows.each do |row|
|
||||
new_row = {}
|
||||
columns.each_with_index do |name, index|
|
||||
new_row[name] = row[index]
|
||||
stmt.each do |row|
|
||||
new_row = Hash[*columns.zip(row).flatten]
|
||||
|
||||
# FIXME: This should be removed but is required for older versions
|
||||
# of rails
|
||||
if(Object.const_defined?(:ActiveRecord))
|
||||
new_row['notnull'] = new_row['notnull'].to_s
|
||||
end
|
||||
|
||||
tweak_default(new_row) if needs_tweak_default
|
||||
|
@ -233,6 +241,7 @@ module SQLite3
|
|||
result << new_row
|
||||
end
|
||||
end
|
||||
stmt.close
|
||||
|
||||
result
|
||||
end
|
||||
|
|
106
vendor/plugins/sqlite3-ruby/lib/sqlite3/resultset.rb
vendored
106
vendor/plugins/sqlite3-ruby/lib/sqlite3/resultset.rb
vendored
|
@ -31,46 +31,22 @@ module SQLite3
|
|||
|
||||
# Create a new ResultSet attached to the given database, using the
|
||||
# given sql text.
|
||||
def initialize( db, stmt )
|
||||
@db = db
|
||||
@driver = @db.driver
|
||||
def initialize db, stmt
|
||||
@db = db
|
||||
@stmt = stmt
|
||||
commence
|
||||
end
|
||||
|
||||
# A convenience method for compiling the virtual machine and stepping
|
||||
# to the first row of the result set.
|
||||
def commence
|
||||
result = @driver.step( @stmt.handle )
|
||||
if result == Constants::ErrorCode::ERROR
|
||||
@driver.reset( @stmt.handle )
|
||||
end
|
||||
check result
|
||||
@first_row = true
|
||||
end
|
||||
private :commence
|
||||
|
||||
def check( result )
|
||||
@eof = ( result == Constants::ErrorCode::DONE )
|
||||
found = ( result == Constants::ErrorCode::ROW )
|
||||
Error.check( result, @db ) unless @eof || found
|
||||
end
|
||||
private :check
|
||||
|
||||
# Reset the cursor, so that a result set which has reached end-of-file
|
||||
# can be rewound and reiterated.
|
||||
def reset( *bind_params )
|
||||
@stmt.must_be_open!
|
||||
@stmt.reset!(false)
|
||||
@driver.reset( @stmt.handle )
|
||||
@stmt.reset!
|
||||
@stmt.bind_params( *bind_params )
|
||||
@eof = false
|
||||
commence
|
||||
end
|
||||
|
||||
# Query whether the cursor has reached the end of the result set or not.
|
||||
def eof?
|
||||
@eof
|
||||
@stmt.done?
|
||||
end
|
||||
|
||||
# Obtain the next row from the cursor. If there are no more rows to be
|
||||
|
@ -87,69 +63,39 @@ module SQLite3
|
|||
# For hashes, the column names are the keys of the hash, and the column
|
||||
# types are accessible via the +types+ property.
|
||||
def next
|
||||
return nil if @eof
|
||||
row = @stmt.step
|
||||
return nil if @stmt.done?
|
||||
|
||||
@stmt.must_be_open!
|
||||
|
||||
unless @first_row
|
||||
result = @driver.step( @stmt.handle )
|
||||
check result
|
||||
if @db.type_translation
|
||||
row = @stmt.types.zip(row).map do |type, value|
|
||||
@db.translator.translate( type, value )
|
||||
end
|
||||
end
|
||||
|
||||
@first_row = false
|
||||
|
||||
unless @eof
|
||||
row = []
|
||||
@driver.data_count( @stmt.handle ).times do |column|
|
||||
type = @driver.column_type( @stmt.handle, column )
|
||||
|
||||
if type == Constants::ColumnType::TEXT
|
||||
row << @driver.column_text( @stmt.handle, column )
|
||||
elsif type == Constants::ColumnType::NULL
|
||||
row << nil
|
||||
elsif type == Constants::ColumnType::BLOB
|
||||
row << @driver.column_blob( @stmt.handle, column )
|
||||
else
|
||||
row << @driver.column_text( @stmt.handle, column )
|
||||
end
|
||||
end
|
||||
|
||||
if @db.type_translation
|
||||
row = @stmt.types.zip( row ).map do |type, value|
|
||||
@db.translator.translate( type, value )
|
||||
end
|
||||
end
|
||||
|
||||
if @db.results_as_hash
|
||||
new_row = HashWithTypes[ *( @stmt.columns.zip( row ).to_a.flatten ) ]
|
||||
row.each_with_index { |value,idx|
|
||||
value.taint
|
||||
new_row[idx] = value
|
||||
}
|
||||
row = new_row
|
||||
if @db.results_as_hash
|
||||
new_row = HashWithTypes[*@stmt.columns.zip(row).flatten]
|
||||
row.each_with_index { |value,idx|
|
||||
new_row[idx] = value
|
||||
}
|
||||
row = new_row
|
||||
else
|
||||
if row.respond_to?(:fields)
|
||||
row = ArrayWithTypes.new(row)
|
||||
else
|
||||
if row.respond_to?(:fields)
|
||||
row = ArrayWithTypes.new(row)
|
||||
else
|
||||
row = ArrayWithTypesAndFields.new(row)
|
||||
end
|
||||
row.fields = @stmt.columns
|
||||
row.each { |column| column.taint }
|
||||
row = ArrayWithTypesAndFields.new(row)
|
||||
end
|
||||
|
||||
row.types = @stmt.types
|
||||
|
||||
return row
|
||||
row.fields = @stmt.columns
|
||||
end
|
||||
|
||||
nil
|
||||
row.types = @stmt.types
|
||||
row
|
||||
end
|
||||
|
||||
# Required by the Enumerable mixin. Provides an internal iterator over the
|
||||
# rows of the result set.
|
||||
def each
|
||||
while row=self.next
|
||||
yield row
|
||||
def each( &block )
|
||||
while node = self.next
|
||||
yield node
|
||||
end
|
||||
end
|
||||
|
||||
|
|
129
vendor/plugins/sqlite3-ruby/lib/sqlite3/statement.rb
vendored
129
vendor/plugins/sqlite3-ruby/lib/sqlite3/statement.rb
vendored
|
@ -8,51 +8,17 @@ class String
|
|||
end
|
||||
|
||||
module SQLite3
|
||||
|
||||
# A class for differentiating between strings and blobs, when binding them
|
||||
# into statements.
|
||||
class Blob < String; end
|
||||
|
||||
# A statement represents a prepared-but-unexecuted SQL query. It will rarely
|
||||
# (if ever) be instantiated directly by a client, and is most often obtained
|
||||
# via the Database#prepare method.
|
||||
class Statement
|
||||
include Enumerable
|
||||
|
||||
# This is any text that followed the first valid SQL statement in the text
|
||||
# with which the statement was initialized. If there was no trailing text,
|
||||
# this will be the empty string.
|
||||
attr_reader :remainder
|
||||
|
||||
# The underlying opaque handle used to access the SQLite @driver.
|
||||
attr_reader :handle
|
||||
|
||||
# Create a new statement attached to the given Database instance, and which
|
||||
# encapsulates the given SQL text. If the text contains more than one
|
||||
# statement (i.e., separated by semicolons), then the #remainder property
|
||||
# will be set to the trailing text.
|
||||
def initialize( db, sql, utf16=false )
|
||||
raise ArgumentError, "nil argument passed as sql text" unless sql
|
||||
@db = db
|
||||
@driver = @db.driver
|
||||
@closed = false
|
||||
@results = @columns = nil
|
||||
result, @handle, @remainder = @driver.prepare( @db.handle, sql )
|
||||
Error.check( result, @db )
|
||||
end
|
||||
|
||||
# Closes the statement by finalizing the underlying statement
|
||||
# handle. The statement must not be used after being closed.
|
||||
def close
|
||||
must_be_open!
|
||||
@closed = true
|
||||
@driver.finalize( @handle )
|
||||
end
|
||||
|
||||
# Returns true if the underlying statement has been closed.
|
||||
def closed?
|
||||
@closed
|
||||
end
|
||||
|
||||
# Binds the given variables to the corresponding placeholders in the SQL
|
||||
# text.
|
||||
#
|
||||
|
@ -78,42 +44,6 @@ module SQLite3
|
|||
end
|
||||
end
|
||||
|
||||
# Binds value to the named (or positional) placeholder. If +param+ is a
|
||||
# Fixnum, it is treated as an index for a positional placeholder.
|
||||
# Otherwise it is used as the name of the placeholder to bind to.
|
||||
#
|
||||
# See also #bind_params.
|
||||
def bind_param( param, value )
|
||||
must_be_open!
|
||||
reset! if active?
|
||||
if Fixnum === param
|
||||
case value
|
||||
when Bignum then
|
||||
@driver.bind_int64( @handle, param, value )
|
||||
when Integer then
|
||||
if value >= (2 ** 31)
|
||||
@driver.bind_int64( @handle, param, value )
|
||||
else
|
||||
@driver.bind_int( @handle, param, value )
|
||||
end
|
||||
when Numeric then
|
||||
@driver.bind_double( @handle, param, value.to_f )
|
||||
when Blob then
|
||||
@driver.bind_blob( @handle, param, value )
|
||||
when nil then
|
||||
@driver.bind_null( @handle, param )
|
||||
else
|
||||
@driver.bind_text( @handle, param, value )
|
||||
end
|
||||
else
|
||||
param = param.to_s
|
||||
param = ":#{param}" unless param[0] == ?:
|
||||
index = @driver.bind_parameter_index( @handle, param )
|
||||
raise Exception, "no such bind parameter '#{param}'" if index == 0
|
||||
bind_param index, value
|
||||
end
|
||||
end
|
||||
|
||||
# Execute the statement. This creates a new ResultSet object for the
|
||||
# statement's virtual machine. If a block was given, the new ResultSet will
|
||||
# be yielded to it; otherwise, the ResultSet will be returned.
|
||||
|
@ -129,17 +59,13 @@ module SQLite3
|
|||
#
|
||||
# See also #bind_params, #execute!.
|
||||
def execute( *bind_vars )
|
||||
must_be_open!
|
||||
reset! if active?
|
||||
reset! if active? || done?
|
||||
|
||||
bind_params(*bind_vars) unless bind_vars.empty?
|
||||
@results = ResultSet.new( @db, self )
|
||||
@results = ResultSet.new(@connection, self)
|
||||
|
||||
if block_given?
|
||||
yield @results
|
||||
else
|
||||
return @results
|
||||
end
|
||||
yield @results if block_given?
|
||||
@results
|
||||
end
|
||||
|
||||
# Execute the statement. If no block was given, this returns an array of
|
||||
|
@ -156,30 +82,15 @@ module SQLite3
|
|||
# end
|
||||
#
|
||||
# See also #bind_params, #execute.
|
||||
def execute!( *bind_vars )
|
||||
result = execute( *bind_vars )
|
||||
rows = [] unless block_given?
|
||||
while row = result.next
|
||||
if block_given?
|
||||
yield row
|
||||
else
|
||||
rows << row
|
||||
end
|
||||
end
|
||||
rows
|
||||
end
|
||||
|
||||
# Resets the statement. This is typically done internally, though it might
|
||||
# occassionally be necessary to manually reset the statement.
|
||||
def reset!(clear_result=true)
|
||||
@driver.reset(@handle)
|
||||
@results = nil if clear_result
|
||||
def execute!( *bind_vars, &block )
|
||||
execute(*bind_vars)
|
||||
block_given? ? each(&block) : to_a
|
||||
end
|
||||
|
||||
# Returns true if the statement is currently active, meaning it has an
|
||||
# open result set.
|
||||
def active?
|
||||
not @results.nil?
|
||||
!done?
|
||||
end
|
||||
|
||||
# Return an array of the column names for this statement. Note that this
|
||||
|
@ -190,11 +101,20 @@ module SQLite3
|
|||
return @columns
|
||||
end
|
||||
|
||||
def each
|
||||
loop do
|
||||
val = step
|
||||
break self if done?
|
||||
yield val
|
||||
end
|
||||
end
|
||||
|
||||
# Return an array of the data types for each column in this statement. Note
|
||||
# that this may execute the statement in order to obtain the metadata; this
|
||||
# makes it a (potentially) expensive operation.
|
||||
def types
|
||||
get_metadata unless defined?(@types)
|
||||
must_be_open!
|
||||
get_metadata unless @types
|
||||
@types
|
||||
end
|
||||
|
||||
|
@ -202,15 +122,12 @@ module SQLite3
|
|||
# that this will actually execute the SQL, which means it can be a
|
||||
# (potentially) expensive operation.
|
||||
def get_metadata
|
||||
must_be_open!
|
||||
|
||||
@columns = []
|
||||
@types = []
|
||||
|
||||
column_count = @driver.column_count( @handle )
|
||||
column_count.times do |column|
|
||||
@columns << @driver.column_name( @handle, column )
|
||||
@types << @driver.column_decltype( @handle, column )
|
||||
@columns << column_name(column)
|
||||
@types << column_decltype(column)
|
||||
end
|
||||
|
||||
@columns.freeze
|
||||
|
@ -221,11 +138,9 @@ module SQLite3
|
|||
# Performs a sanity check to ensure that the statement is not
|
||||
# closed. If it is, an exception is raised.
|
||||
def must_be_open! # :nodoc:
|
||||
if @closed
|
||||
if closed?
|
||||
raise SQLite3::Exception, "cannot use a closed statement"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -44,7 +44,12 @@ module SQLite3
|
|||
# and are always passed straight through regardless of the type parameter.
|
||||
def translate( type, value )
|
||||
unless value.nil?
|
||||
@translators[ type_name( type ) ].call( type, value )
|
||||
# FIXME: this is a hack to support Sequel
|
||||
if type && %w{ datetime timestamp }.include?(type.downcase)
|
||||
@translators[ type_name( type ) ].call( type, value.to_s )
|
||||
else
|
||||
@translators[ type_name( type ) ].call( type, value )
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -3,14 +3,14 @@ module SQLite3
|
|||
module Version
|
||||
|
||||
MAJOR = 1
|
||||
MINOR = 2
|
||||
TINY = 5
|
||||
MINOR = 3
|
||||
TINY = 0
|
||||
BUILD = nil
|
||||
|
||||
STRING = [ MAJOR, MINOR, TINY, BUILD ].compact.join( "." )
|
||||
#:beta-tag:
|
||||
|
||||
VERSION = '1.2.5'
|
||||
VERSION = '1.3.0'
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue