sqlite3-ruby 1.2.5-x86-mswin32
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog.cvs +88 -0
- data/History.txt +68 -0
- data/LICENSE +27 -0
- data/Manifest.txt +41 -0
- data/README.txt +56 -0
- data/Rakefile +5 -0
- data/ext/sqlite3_api/extconf.rb +10 -0
- data/ext/sqlite3_api/sqlite3_api.i +362 -0
- data/ext/sqlite3_api/sqlite3_api_wrap.c +5018 -0
- data/faq/faq.rb +145 -0
- data/faq/faq.yml +426 -0
- data/lib/1.8/sqlite3_api.so +0 -0
- data/lib/1.9/sqlite3_api.so +0 -0
- data/lib/sqlite3.rb +1 -0
- data/lib/sqlite3/constants.rb +49 -0
- data/lib/sqlite3/database.rb +721 -0
- data/lib/sqlite3/driver/dl/api.rb +152 -0
- data/lib/sqlite3/driver/dl/driver.rb +307 -0
- data/lib/sqlite3/driver/native/driver.rb +219 -0
- data/lib/sqlite3/errors.rb +68 -0
- data/lib/sqlite3/pragmas.rb +271 -0
- data/lib/sqlite3/resultset.rb +180 -0
- data/lib/sqlite3/statement.rb +231 -0
- data/lib/sqlite3/translator.rb +109 -0
- data/lib/sqlite3/value.rb +57 -0
- data/lib/sqlite3/version.rb +16 -0
- data/setup.rb +1333 -0
- data/tasks/benchmark.rake +9 -0
- data/tasks/faq.rake +9 -0
- data/tasks/gem.rake +32 -0
- data/tasks/native.rake +35 -0
- data/tasks/vendor_sqlite3.rake +104 -0
- data/test/bm.rb +140 -0
- data/test/driver/dl/tc_driver.rb +292 -0
- data/test/helper.rb +67 -0
- data/test/native-vs-dl.rb +126 -0
- data/test/test_database.rb +217 -0
- data/test/test_errors.rb +17 -0
- data/test/test_integration.rb +542 -0
- data/test/test_integration_open_close.rb +30 -0
- data/test/test_integration_pending.rb +111 -0
- data/test/test_integration_resultset.rb +159 -0
- data/test/test_integration_statement.rb +195 -0
- metadata +143 -0
Binary file
|
Binary file
|
data/lib/sqlite3.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'sqlite3/database'
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module SQLite3 ; module Constants
|
2
|
+
|
3
|
+
module TextRep
|
4
|
+
UTF8 = 1
|
5
|
+
UTF16LE = 2
|
6
|
+
UTF16BE = 3
|
7
|
+
UTF16 = 4
|
8
|
+
ANY = 5
|
9
|
+
end
|
10
|
+
|
11
|
+
module ColumnType
|
12
|
+
INTEGER = 1
|
13
|
+
FLOAT = 2
|
14
|
+
TEXT = 3
|
15
|
+
BLOB = 4
|
16
|
+
NULL = 5
|
17
|
+
end
|
18
|
+
|
19
|
+
module ErrorCode
|
20
|
+
OK = 0 # Successful result
|
21
|
+
ERROR = 1 # SQL error or missing database
|
22
|
+
INTERNAL = 2 # An internal logic error in SQLite
|
23
|
+
PERM = 3 # Access permission denied
|
24
|
+
ABORT = 4 # Callback routine requested an abort
|
25
|
+
BUSY = 5 # The database file is locked
|
26
|
+
LOCKED = 6 # A table in the database is locked
|
27
|
+
NOMEM = 7 # A malloc() failed
|
28
|
+
READONLY = 8 # Attempt to write a readonly database
|
29
|
+
INTERRUPT = 9 # Operation terminated by sqlite_interrupt()
|
30
|
+
IOERR = 10 # Some kind of disk I/O error occurred
|
31
|
+
CORRUPT = 11 # The database disk image is malformed
|
32
|
+
NOTFOUND = 12 # (Internal Only) Table or record not found
|
33
|
+
FULL = 13 # Insertion failed because database is full
|
34
|
+
CANTOPEN = 14 # Unable to open the database file
|
35
|
+
PROTOCOL = 15 # Database lock protocol error
|
36
|
+
EMPTY = 16 # (Internal Only) Database table is empty
|
37
|
+
SCHEMA = 17 # The database schema changed
|
38
|
+
TOOBIG = 18 # Too much data for one row of a table
|
39
|
+
CONSTRAINT = 19 # Abort due to contraint violation
|
40
|
+
MISMATCH = 20 # Data type mismatch
|
41
|
+
MISUSE = 21 # Library used incorrectly
|
42
|
+
NOLFS = 22 # Uses OS features not supported on host
|
43
|
+
AUTH = 23 # Authorization denied
|
44
|
+
|
45
|
+
ROW = 100 # sqlite_step() has another row ready
|
46
|
+
DONE = 101 # sqlite_step() has finished executing
|
47
|
+
end
|
48
|
+
|
49
|
+
end ; end
|
@@ -0,0 +1,721 @@
|
|
1
|
+
require 'sqlite3/constants'
|
2
|
+
require 'sqlite3/errors'
|
3
|
+
require 'sqlite3/pragmas'
|
4
|
+
require 'sqlite3/statement'
|
5
|
+
require 'sqlite3/translator'
|
6
|
+
require 'sqlite3/value'
|
7
|
+
|
8
|
+
module SQLite3
|
9
|
+
|
10
|
+
# The Database class encapsulates a single connection to a SQLite3 database.
|
11
|
+
# Its usage is very straightforward:
|
12
|
+
#
|
13
|
+
# require 'sqlite3'
|
14
|
+
#
|
15
|
+
# SQLite3::Database.new( "data.db" ) do |db|
|
16
|
+
# db.execute( "select * from table" ) do |row|
|
17
|
+
# p row
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# It wraps the lower-level methods provides by the selected driver, and
|
22
|
+
# includes the Pragmas module for access to various pragma convenience
|
23
|
+
# methods.
|
24
|
+
#
|
25
|
+
# The Database class provides type translation services as well, by which
|
26
|
+
# the SQLite3 data types (which are all represented as strings) may be
|
27
|
+
# converted into their corresponding types (as defined in the schemas
|
28
|
+
# for their tables). This translation only occurs when querying data from
|
29
|
+
# the database--insertions and updates are all still typeless.
|
30
|
+
#
|
31
|
+
# Furthermore, the Database class has been designed to work well with the
|
32
|
+
# ArrayFields module from Ara Howard. If you require the ArrayFields
|
33
|
+
# module before performing a query, and if you have not enabled results as
|
34
|
+
# hashes, then the results will all be indexible by field name.
|
35
|
+
class Database
|
36
|
+
include Pragmas
|
37
|
+
|
38
|
+
class <<self
|
39
|
+
|
40
|
+
alias :open :new
|
41
|
+
|
42
|
+
# Quotes the given string, making it safe to use in an SQL statement.
|
43
|
+
# It replaces all instances of the single-quote character with two
|
44
|
+
# single-quote characters. The modified string is returned.
|
45
|
+
def quote( string )
|
46
|
+
string.gsub( /'/, "''" )
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
# The low-level opaque database handle that this object wraps.
|
52
|
+
attr_reader :handle
|
53
|
+
|
54
|
+
# A reference to the underlying SQLite3 driver used by this database.
|
55
|
+
attr_reader :driver
|
56
|
+
|
57
|
+
# A boolean that indicates whether rows in result sets should be returned
|
58
|
+
# as hashes or not. By default, rows are returned as arrays.
|
59
|
+
attr_accessor :results_as_hash
|
60
|
+
|
61
|
+
# A boolean indicating whether or not type translation is enabled for this
|
62
|
+
# database.
|
63
|
+
attr_accessor :type_translation
|
64
|
+
|
65
|
+
# Create a new Database object that opens the given file. If utf16
|
66
|
+
# is +true+, the filename is interpreted as a UTF-16 encoded string.
|
67
|
+
#
|
68
|
+
# By default, the new database will return result rows as arrays
|
69
|
+
# (#results_as_hash) and has type translation disabled (#type_translation=).
|
70
|
+
def initialize( file_name, options={} ) # :yields: db
|
71
|
+
utf16 = options.fetch(:utf16, false)
|
72
|
+
load_driver( options[:driver] )
|
73
|
+
|
74
|
+
@statement_factory = options[:statement_factory] || Statement
|
75
|
+
|
76
|
+
result, @handle = @driver.open( file_name, utf16 )
|
77
|
+
Error.check( result, self, "could not open database" )
|
78
|
+
|
79
|
+
@closed = false
|
80
|
+
@results_as_hash = options.fetch(:results_as_hash,false)
|
81
|
+
@type_translation = options.fetch(:type_translation,false)
|
82
|
+
@translator = nil
|
83
|
+
@transaction_active = false
|
84
|
+
|
85
|
+
if block_given?
|
86
|
+
begin
|
87
|
+
yield self
|
88
|
+
ensure
|
89
|
+
self.close
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# Return +true+ if the string is a valid (ie, parsable) SQL statement, and
|
95
|
+
# +false+ otherwise. If +utf16+ is +true+, then the string is a UTF-16
|
96
|
+
# character string.
|
97
|
+
def complete?( string, utf16=false )
|
98
|
+
@driver.complete?( string, utf16 )
|
99
|
+
end
|
100
|
+
|
101
|
+
# Return a string describing the last error to have occurred with this
|
102
|
+
# database.
|
103
|
+
def errmsg( utf16=false )
|
104
|
+
@driver.errmsg( @handle, utf16 )
|
105
|
+
end
|
106
|
+
|
107
|
+
# Return an integer representing the last error to have occurred with this
|
108
|
+
# database.
|
109
|
+
def errcode
|
110
|
+
@driver.errcode( @handle )
|
111
|
+
end
|
112
|
+
|
113
|
+
# Return the type translator employed by this database instance. Each
|
114
|
+
# database instance has its own type translator; this allows for different
|
115
|
+
# type handlers to be installed in each instance without affecting other
|
116
|
+
# instances. Furthermore, the translators are instantiated lazily, so that
|
117
|
+
# if a database does not use type translation, it will not be burdened by
|
118
|
+
# the overhead of a useless type translator. (See the Translator class.)
|
119
|
+
def translator
|
120
|
+
@translator ||= Translator.new
|
121
|
+
end
|
122
|
+
|
123
|
+
# Closes this database.
|
124
|
+
def close
|
125
|
+
unless @closed
|
126
|
+
result = @driver.close( @handle )
|
127
|
+
Error.check( result, self )
|
128
|
+
end
|
129
|
+
@closed = true
|
130
|
+
end
|
131
|
+
|
132
|
+
# Returns +true+ if this database instance has been closed (see #close).
|
133
|
+
def closed?
|
134
|
+
@closed
|
135
|
+
end
|
136
|
+
|
137
|
+
# Installs (or removes) a block that will be invoked for every SQL
|
138
|
+
# statement executed. The block receives a two parameters: the +data+
|
139
|
+
# argument, and the SQL statement executed. If the block is +nil+,
|
140
|
+
# any existing tracer will be uninstalled.
|
141
|
+
def trace( data=nil, &block )
|
142
|
+
@driver.trace( @handle, data, &block )
|
143
|
+
end
|
144
|
+
|
145
|
+
# Installs (or removes) a block that will be invoked for every access
|
146
|
+
# to the database. If the block returns 0 (or +nil+), the statement
|
147
|
+
# is allowed to proceed. Returning 1 causes an authorization error to
|
148
|
+
# occur, and returning 2 causes the access to be silently denied.
|
149
|
+
def authorizer( data=nil, &block )
|
150
|
+
result = @driver.set_authorizer( @handle, data, &block )
|
151
|
+
Error.check( result, self )
|
152
|
+
end
|
153
|
+
|
154
|
+
# Returns a Statement object representing the given SQL. This does not
|
155
|
+
# execute the statement; it merely prepares the statement for execution.
|
156
|
+
#
|
157
|
+
# The Statement can then be executed using Statement#execute.
|
158
|
+
#
|
159
|
+
def prepare( sql )
|
160
|
+
stmt = @statement_factory.new( self, sql )
|
161
|
+
if block_given?
|
162
|
+
begin
|
163
|
+
yield stmt
|
164
|
+
ensure
|
165
|
+
stmt.close
|
166
|
+
end
|
167
|
+
else
|
168
|
+
return stmt
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
# Executes the given SQL statement. If additional parameters are given,
|
173
|
+
# they are treated as bind variables, and are bound to the placeholders in
|
174
|
+
# the query.
|
175
|
+
#
|
176
|
+
# Note that if any of the values passed to this are hashes, then the
|
177
|
+
# key/value pairs are each bound separately, with the key being used as
|
178
|
+
# the name of the placeholder to bind the value to.
|
179
|
+
#
|
180
|
+
# The block is optional. If given, it will be invoked for each row returned
|
181
|
+
# by the query. Otherwise, any results are accumulated into an array and
|
182
|
+
# returned wholesale.
|
183
|
+
#
|
184
|
+
# See also #execute2, #query, and #execute_batch for additional ways of
|
185
|
+
# executing statements.
|
186
|
+
def execute( sql, *bind_vars )
|
187
|
+
prepare( sql ) do |stmt|
|
188
|
+
result = stmt.execute( *bind_vars )
|
189
|
+
if block_given?
|
190
|
+
result.each { |row| yield row }
|
191
|
+
else
|
192
|
+
return result.inject( [] ) { |arr,row| arr << row; arr }
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
# Executes the given SQL statement, exactly as with #execute. However, the
|
198
|
+
# first row returned (either via the block, or in the returned array) is
|
199
|
+
# always the names of the columns. Subsequent rows correspond to the data
|
200
|
+
# from the result set.
|
201
|
+
#
|
202
|
+
# Thus, even if the query itself returns no rows, this method will always
|
203
|
+
# return at least one row--the names of the columns.
|
204
|
+
#
|
205
|
+
# See also #execute, #query, and #execute_batch for additional ways of
|
206
|
+
# executing statements.
|
207
|
+
def execute2( sql, *bind_vars )
|
208
|
+
prepare( sql ) do |stmt|
|
209
|
+
result = stmt.execute( *bind_vars )
|
210
|
+
if block_given?
|
211
|
+
yield result.columns
|
212
|
+
result.each { |row| yield row }
|
213
|
+
else
|
214
|
+
return result.inject( [ result.columns ] ) { |arr,row|
|
215
|
+
arr << row; arr }
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
# Executes all SQL statements in the given string. By contrast, the other
|
221
|
+
# means of executing queries will only execute the first statement in the
|
222
|
+
# string, ignoring all subsequent statements. This will execute each one
|
223
|
+
# in turn. The same bind parameters, if given, will be applied to each
|
224
|
+
# statement.
|
225
|
+
#
|
226
|
+
# This always returns +nil+, making it unsuitable for queries that return
|
227
|
+
# rows.
|
228
|
+
def execute_batch( sql, *bind_vars )
|
229
|
+
sql = sql.strip
|
230
|
+
until sql.empty? do
|
231
|
+
prepare( sql ) do |stmt|
|
232
|
+
stmt.execute( *bind_vars )
|
233
|
+
sql = stmt.remainder.strip
|
234
|
+
end
|
235
|
+
end
|
236
|
+
nil
|
237
|
+
end
|
238
|
+
|
239
|
+
# This is a convenience method for creating a statement, binding
|
240
|
+
# paramters to it, and calling execute:
|
241
|
+
#
|
242
|
+
# result = db.query( "select * from foo where a=?", 5 )
|
243
|
+
# # is the same as
|
244
|
+
# result = db.prepare( "select * from foo where a=?" ).execute( 5 )
|
245
|
+
#
|
246
|
+
# You must be sure to call +close+ on the ResultSet instance that is
|
247
|
+
# returned, or you could have problems with locks on the table. If called
|
248
|
+
# with a block, +close+ will be invoked implicitly when the block
|
249
|
+
# terminates.
|
250
|
+
def query( sql, *bind_vars )
|
251
|
+
result = prepare( sql ).execute( *bind_vars )
|
252
|
+
if block_given?
|
253
|
+
begin
|
254
|
+
yield result
|
255
|
+
ensure
|
256
|
+
result.close
|
257
|
+
end
|
258
|
+
else
|
259
|
+
return result
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
# A convenience method for obtaining the first row of a result set, and
|
264
|
+
# discarding all others. It is otherwise identical to #execute.
|
265
|
+
#
|
266
|
+
# See also #get_first_value.
|
267
|
+
def get_first_row( sql, *bind_vars )
|
268
|
+
execute( sql, *bind_vars ) { |row| return row }
|
269
|
+
nil
|
270
|
+
end
|
271
|
+
|
272
|
+
# A convenience method for obtaining the first value of the first row of a
|
273
|
+
# result set, and discarding all other values and rows. It is otherwise
|
274
|
+
# identical to #execute.
|
275
|
+
#
|
276
|
+
# See also #get_first_row.
|
277
|
+
def get_first_value( sql, *bind_vars )
|
278
|
+
execute( sql, *bind_vars ) { |row| return row[0] }
|
279
|
+
nil
|
280
|
+
end
|
281
|
+
|
282
|
+
# Obtains the unique row ID of the last row to be inserted by this Database
|
283
|
+
# instance.
|
284
|
+
def last_insert_row_id
|
285
|
+
@driver.last_insert_rowid( @handle )
|
286
|
+
end
|
287
|
+
|
288
|
+
# Returns the number of changes made to this database instance by the last
|
289
|
+
# operation performed. Note that a "delete from table" without a where
|
290
|
+
# clause will not affect this value.
|
291
|
+
def changes
|
292
|
+
@driver.changes( @handle )
|
293
|
+
end
|
294
|
+
|
295
|
+
# Returns the total number of changes made to this database instance
|
296
|
+
# since it was opened.
|
297
|
+
def total_changes
|
298
|
+
@driver.total_changes( @handle )
|
299
|
+
end
|
300
|
+
|
301
|
+
# Interrupts the currently executing operation, causing it to abort.
|
302
|
+
def interrupt
|
303
|
+
@driver.interrupt( @handle )
|
304
|
+
end
|
305
|
+
|
306
|
+
# Register a busy handler with this database instance. When a requested
|
307
|
+
# resource is busy, this handler will be invoked. If the handler returns
|
308
|
+
# +false+, the operation will be aborted; otherwise, the resource will
|
309
|
+
# be requested again.
|
310
|
+
#
|
311
|
+
# The handler will be invoked with the name of the resource that was
|
312
|
+
# busy, and the number of times it has been retried.
|
313
|
+
#
|
314
|
+
# See also the mutually exclusive #busy_timeout.
|
315
|
+
def busy_handler( data=nil, &block ) # :yields: data, retries
|
316
|
+
result = @driver.busy_handler( @handle, data, &block )
|
317
|
+
Error.check( result, self )
|
318
|
+
end
|
319
|
+
|
320
|
+
# Indicates that if a request for a resource terminates because that
|
321
|
+
# resource is busy, SQLite should sleep and retry for up to the indicated
|
322
|
+
# number of milliseconds. By default, SQLite does not retry
|
323
|
+
# busy resources. To restore the default behavior, send 0 as the
|
324
|
+
# +ms+ parameter.
|
325
|
+
#
|
326
|
+
# See also the mutually exclusive #busy_handler.
|
327
|
+
def busy_timeout( ms )
|
328
|
+
result = @driver.busy_timeout( @handle, ms )
|
329
|
+
Error.check( result, self )
|
330
|
+
end
|
331
|
+
|
332
|
+
# Creates a new function for use in SQL statements. It will be added as
|
333
|
+
# +name+, with the given +arity+. (For variable arity functions, use
|
334
|
+
# -1 for the arity.)
|
335
|
+
#
|
336
|
+
# The block should accept at least one parameter--the FunctionProxy
|
337
|
+
# instance that wraps this function invocation--and any other
|
338
|
+
# arguments it needs (up to its arity).
|
339
|
+
#
|
340
|
+
# The block does not return a value directly. Instead, it will invoke
|
341
|
+
# the FunctionProxy#set_result method on the +func+ parameter and
|
342
|
+
# indicate the return value that way.
|
343
|
+
#
|
344
|
+
# Example:
|
345
|
+
#
|
346
|
+
# db.create_function( "maim", 1 ) do |func, value|
|
347
|
+
# if value.nil?
|
348
|
+
# func.result = nil
|
349
|
+
# else
|
350
|
+
# func.result = value.split(//).sort.join
|
351
|
+
# end
|
352
|
+
# end
|
353
|
+
#
|
354
|
+
# puts db.get_first_value( "select maim(name) from table" )
|
355
|
+
def create_function( name, arity, text_rep=Constants::TextRep::ANY,
|
356
|
+
&block ) # :yields: func, *args
|
357
|
+
# begin
|
358
|
+
callback = proc do |func,*args|
|
359
|
+
begin
|
360
|
+
block.call( FunctionProxy.new( @driver, func ),
|
361
|
+
*args.map{|v| Value.new(self,v)} )
|
362
|
+
rescue StandardError, Exception => e
|
363
|
+
@driver.result_error( func,
|
364
|
+
"#{e.message} (#{e.class})", -1 )
|
365
|
+
end
|
366
|
+
end
|
367
|
+
|
368
|
+
result = @driver.create_function( @handle, name, arity, text_rep, nil,
|
369
|
+
callback, nil, nil )
|
370
|
+
Error.check( result, self )
|
371
|
+
|
372
|
+
self
|
373
|
+
end
|
374
|
+
|
375
|
+
# Creates a new aggregate function for use in SQL statements. Aggregate
|
376
|
+
# functions are functions that apply over every row in the result set,
|
377
|
+
# instead of over just a single row. (A very common aggregate function
|
378
|
+
# is the "count" function, for determining the number of rows that match
|
379
|
+
# a query.)
|
380
|
+
#
|
381
|
+
# The new function will be added as +name+, with the given +arity+. (For
|
382
|
+
# variable arity functions, use -1 for the arity.)
|
383
|
+
#
|
384
|
+
# The +step+ parameter must be a proc object that accepts as its first
|
385
|
+
# parameter a FunctionProxy instance (representing the function
|
386
|
+
# invocation), with any subsequent parameters (up to the function's arity).
|
387
|
+
# The +step+ callback will be invoked once for each row of the result set.
|
388
|
+
#
|
389
|
+
# The +finalize+ parameter must be a +proc+ object that accepts only a
|
390
|
+
# single parameter, the FunctionProxy instance representing the current
|
391
|
+
# function invocation. It should invoke FunctionProxy#set_result to
|
392
|
+
# store the result of the function.
|
393
|
+
#
|
394
|
+
# Example:
|
395
|
+
#
|
396
|
+
# db.create_aggregate( "lengths", 1 ) do
|
397
|
+
# step do |func, value|
|
398
|
+
# func[ :total ] ||= 0
|
399
|
+
# func[ :total ] += ( value ? value.length : 0 )
|
400
|
+
# end
|
401
|
+
#
|
402
|
+
# finalize do |func|
|
403
|
+
# func.set_result( func[ :total ] || 0 )
|
404
|
+
# end
|
405
|
+
# end
|
406
|
+
#
|
407
|
+
# puts db.get_first_value( "select lengths(name) from table" )
|
408
|
+
#
|
409
|
+
# See also #create_aggregate_handler for a more object-oriented approach to
|
410
|
+
# aggregate functions.
|
411
|
+
def create_aggregate( name, arity, step=nil, finalize=nil,
|
412
|
+
text_rep=Constants::TextRep::ANY, &block )
|
413
|
+
# begin
|
414
|
+
if block
|
415
|
+
proxy = AggregateDefinitionProxy.new
|
416
|
+
proxy.instance_eval(&block)
|
417
|
+
step ||= proxy.step_callback
|
418
|
+
finalize ||= proxy.finalize_callback
|
419
|
+
end
|
420
|
+
|
421
|
+
step_callback = proc do |func,*args|
|
422
|
+
ctx = @driver.aggregate_context( func )
|
423
|
+
unless ctx[:__error]
|
424
|
+
begin
|
425
|
+
step.call( FunctionProxy.new( @driver, func, ctx ),
|
426
|
+
*args.map{|v| Value.new(self,v)} )
|
427
|
+
rescue Exception => e
|
428
|
+
ctx[:__error] = e
|
429
|
+
end
|
430
|
+
end
|
431
|
+
end
|
432
|
+
|
433
|
+
finalize_callback = proc do |func|
|
434
|
+
ctx = @driver.aggregate_context( func )
|
435
|
+
unless ctx[:__error]
|
436
|
+
begin
|
437
|
+
finalize.call( FunctionProxy.new( @driver, func, ctx ) )
|
438
|
+
rescue Exception => e
|
439
|
+
@driver.result_error( func,
|
440
|
+
"#{e.message} (#{e.class})", -1 )
|
441
|
+
end
|
442
|
+
else
|
443
|
+
e = ctx[:__error]
|
444
|
+
@driver.result_error( func,
|
445
|
+
"#{e.message} (#{e.class})", -1 )
|
446
|
+
end
|
447
|
+
end
|
448
|
+
|
449
|
+
result = @driver.create_function( @handle, name, arity, text_rep, nil,
|
450
|
+
nil, step_callback, finalize_callback )
|
451
|
+
Error.check( result, self )
|
452
|
+
|
453
|
+
self
|
454
|
+
end
|
455
|
+
|
456
|
+
# This is another approach to creating an aggregate function (see
|
457
|
+
# #create_aggregate). Instead of explicitly specifying the name,
|
458
|
+
# callbacks, arity, and type, you specify a factory object
|
459
|
+
# (the "handler") that knows how to obtain all of that information. The
|
460
|
+
# handler should respond to the following messages:
|
461
|
+
#
|
462
|
+
# +arity+:: corresponds to the +arity+ parameter of #create_aggregate. This
|
463
|
+
# message is optional, and if the handler does not respond to it,
|
464
|
+
# the function will have an arity of -1.
|
465
|
+
# +name+:: this is the name of the function. The handler _must_ implement
|
466
|
+
# this message.
|
467
|
+
# +new+:: this must be implemented by the handler. It should return a new
|
468
|
+
# instance of the object that will handle a specific invocation of
|
469
|
+
# the function.
|
470
|
+
#
|
471
|
+
# The handler instance (the object returned by the +new+ message, described
|
472
|
+
# above), must respond to the following messages:
|
473
|
+
#
|
474
|
+
# +step+:: this is the method that will be called for each step of the
|
475
|
+
# aggregate function's evaluation. It should implement the same
|
476
|
+
# signature as the +step+ callback for #create_aggregate.
|
477
|
+
# +finalize+:: this is the method that will be called to finalize the
|
478
|
+
# aggregate function's evaluation. It should implement the
|
479
|
+
# same signature as the +finalize+ callback for
|
480
|
+
# #create_aggregate.
|
481
|
+
#
|
482
|
+
# Example:
|
483
|
+
#
|
484
|
+
# class LengthsAggregateHandler
|
485
|
+
# def self.arity; 1; end
|
486
|
+
#
|
487
|
+
# def initialize
|
488
|
+
# @total = 0
|
489
|
+
# end
|
490
|
+
#
|
491
|
+
# def step( ctx, name )
|
492
|
+
# @total += ( name ? name.length : 0 )
|
493
|
+
# end
|
494
|
+
#
|
495
|
+
# def finalize( ctx )
|
496
|
+
# ctx.set_result( @total )
|
497
|
+
# end
|
498
|
+
# end
|
499
|
+
#
|
500
|
+
# db.create_aggregate_handler( LengthsAggregateHandler )
|
501
|
+
# puts db.get_first_value( "select lengths(name) from A" )
|
502
|
+
def create_aggregate_handler( handler )
|
503
|
+
arity = -1
|
504
|
+
text_rep = Constants::TextRep::ANY
|
505
|
+
|
506
|
+
arity = handler.arity if handler.respond_to?(:arity)
|
507
|
+
text_rep = handler.text_rep if handler.respond_to?(:text_rep)
|
508
|
+
name = handler.name
|
509
|
+
|
510
|
+
step = proc do |func,*args|
|
511
|
+
ctx = @driver.aggregate_context( func )
|
512
|
+
unless ctx[ :__error ]
|
513
|
+
ctx[ :handler ] ||= handler.new
|
514
|
+
begin
|
515
|
+
ctx[ :handler ].step( FunctionProxy.new( @driver, func, ctx ),
|
516
|
+
*args.map{|v| Value.new(self,v)} )
|
517
|
+
rescue Exception, StandardError => e
|
518
|
+
ctx[ :__error ] = e
|
519
|
+
end
|
520
|
+
end
|
521
|
+
end
|
522
|
+
|
523
|
+
finalize = proc do |func|
|
524
|
+
ctx = @driver.aggregate_context( func )
|
525
|
+
unless ctx[ :__error ]
|
526
|
+
ctx[ :handler ] ||= handler.new
|
527
|
+
begin
|
528
|
+
ctx[ :handler ].finalize( FunctionProxy.new( @driver, func, ctx ) )
|
529
|
+
rescue Exception => e
|
530
|
+
ctx[ :__error ] = e
|
531
|
+
end
|
532
|
+
end
|
533
|
+
|
534
|
+
if ctx[ :__error ]
|
535
|
+
e = ctx[ :__error ]
|
536
|
+
@driver.sqlite3_result_error( func, "#{e.message} (#{e.class})", -1 )
|
537
|
+
end
|
538
|
+
end
|
539
|
+
|
540
|
+
result = @driver.create_function( @handle, name, arity, text_rep, nil,
|
541
|
+
nil, step, finalize )
|
542
|
+
Error.check( result, self )
|
543
|
+
|
544
|
+
self
|
545
|
+
end
|
546
|
+
|
547
|
+
# Begins a new transaction. Note that nested transactions are not allowed
|
548
|
+
# by SQLite, so attempting to nest a transaction will result in a runtime
|
549
|
+
# exception.
|
550
|
+
#
|
551
|
+
# The +mode+ parameter may be either <tt>:deferred</tt> (the default),
|
552
|
+
# <tt>:immediate</tt>, or <tt>:exclusive</tt>.
|
553
|
+
#
|
554
|
+
# If a block is given, the database instance is yielded to it, and the
|
555
|
+
# transaction is committed when the block terminates. If the block
|
556
|
+
# raises an exception, a rollback will be performed instead. Note that if
|
557
|
+
# a block is given, #commit and #rollback should never be called
|
558
|
+
# explicitly or you'll get an error when the block terminates.
|
559
|
+
#
|
560
|
+
# If a block is not given, it is the caller's responsibility to end the
|
561
|
+
# transaction explicitly, either by calling #commit, or by calling
|
562
|
+
# #rollback.
|
563
|
+
def transaction( mode = :deferred )
|
564
|
+
execute "begin #{mode.to_s} transaction"
|
565
|
+
@transaction_active = true
|
566
|
+
|
567
|
+
if block_given?
|
568
|
+
abort = false
|
569
|
+
begin
|
570
|
+
yield self
|
571
|
+
rescue ::Object
|
572
|
+
abort = true
|
573
|
+
raise
|
574
|
+
ensure
|
575
|
+
abort and rollback or commit
|
576
|
+
end
|
577
|
+
end
|
578
|
+
|
579
|
+
true
|
580
|
+
end
|
581
|
+
|
582
|
+
# Commits the current transaction. If there is no current transaction,
|
583
|
+
# this will cause an error to be raised. This returns +true+, in order
|
584
|
+
# to allow it to be used in idioms like
|
585
|
+
# <tt>abort? and rollback or commit</tt>.
|
586
|
+
def commit
|
587
|
+
execute "commit transaction"
|
588
|
+
@transaction_active = false
|
589
|
+
true
|
590
|
+
end
|
591
|
+
|
592
|
+
# Rolls the current transaction back. If there is no current transaction,
|
593
|
+
# this will cause an error to be raised. This returns +true+, in order
|
594
|
+
# to allow it to be used in idioms like
|
595
|
+
# <tt>abort? and rollback or commit</tt>.
|
596
|
+
def rollback
|
597
|
+
execute "rollback transaction"
|
598
|
+
@transaction_active = false
|
599
|
+
true
|
600
|
+
end
|
601
|
+
|
602
|
+
# Returns +true+ if there is a transaction active, and +false+ otherwise.
|
603
|
+
def transaction_active?
|
604
|
+
@transaction_active
|
605
|
+
end
|
606
|
+
|
607
|
+
# Loads the corresponding driver, or if it is nil, attempts to locate a
|
608
|
+
# suitable driver.
|
609
|
+
def load_driver( driver )
|
610
|
+
case driver
|
611
|
+
when Class
|
612
|
+
# do nothing--use what was given
|
613
|
+
when Symbol, String
|
614
|
+
require "sqlite3/driver/#{driver.to_s.downcase}/driver"
|
615
|
+
driver = SQLite3::Driver.const_get( driver )::Driver
|
616
|
+
else
|
617
|
+
[ "Native", "DL" ].each do |d|
|
618
|
+
begin
|
619
|
+
require "sqlite3/driver/#{d.downcase}/driver"
|
620
|
+
driver = SQLite3::Driver.const_get( d )::Driver
|
621
|
+
break
|
622
|
+
rescue SyntaxError
|
623
|
+
raise
|
624
|
+
rescue ScriptError, Exception, NameError
|
625
|
+
end
|
626
|
+
end
|
627
|
+
raise "no driver for sqlite3 found" unless driver
|
628
|
+
end
|
629
|
+
|
630
|
+
@driver = driver.new
|
631
|
+
end
|
632
|
+
private :load_driver
|
633
|
+
|
634
|
+
# A helper class for dealing with custom functions (see #create_function,
|
635
|
+
# #create_aggregate, and #create_aggregate_handler). It encapsulates the
|
636
|
+
# opaque function object that represents the current invocation. It also
|
637
|
+
# provides more convenient access to the API functions that operate on
|
638
|
+
# the function object.
|
639
|
+
#
|
640
|
+
# This class will almost _always_ be instantiated indirectly, by working
|
641
|
+
# with the create methods mentioned above.
|
642
|
+
class FunctionProxy
|
643
|
+
|
644
|
+
# Create a new FunctionProxy that encapsulates the given +func+ object.
|
645
|
+
# If context is non-nil, the functions context will be set to that. If
|
646
|
+
# it is non-nil, it must quack like a Hash. If it is nil, then none of
|
647
|
+
# the context functions will be available.
|
648
|
+
def initialize( driver, func, context=nil )
|
649
|
+
@driver = driver
|
650
|
+
@func = func
|
651
|
+
@context = context
|
652
|
+
end
|
653
|
+
|
654
|
+
# Calls #set_result to set the result of this function.
|
655
|
+
def result=( result )
|
656
|
+
set_result( result )
|
657
|
+
end
|
658
|
+
|
659
|
+
# Set the result of the function to the given value. The function will
|
660
|
+
# then return this value.
|
661
|
+
def set_result( result, utf16=false )
|
662
|
+
@driver.result_text( @func, result, utf16 )
|
663
|
+
end
|
664
|
+
|
665
|
+
# Set the result of the function to the given error message.
|
666
|
+
# The function will then return that error.
|
667
|
+
def set_error( error )
|
668
|
+
@driver.result_error( @func, error.to_s, -1 )
|
669
|
+
end
|
670
|
+
|
671
|
+
# (Only available to aggregate functions.) Returns the number of rows
|
672
|
+
# that the aggregate has processed so far. This will include the current
|
673
|
+
# row, and so will always return at least 1.
|
674
|
+
def count
|
675
|
+
ensure_aggregate!
|
676
|
+
@driver.aggregate_count( @func )
|
677
|
+
end
|
678
|
+
|
679
|
+
# Returns the value with the given key from the context. This is only
|
680
|
+
# available to aggregate functions.
|
681
|
+
def []( key )
|
682
|
+
ensure_aggregate!
|
683
|
+
@context[ key ]
|
684
|
+
end
|
685
|
+
|
686
|
+
# Sets the value with the given key in the context. This is only
|
687
|
+
# available to aggregate functions.
|
688
|
+
def []=( key, value )
|
689
|
+
ensure_aggregate!
|
690
|
+
@context[ key ] = value
|
691
|
+
end
|
692
|
+
|
693
|
+
# A function for performing a sanity check, to ensure that the function
|
694
|
+
# being invoked is an aggregate function. This is implied by the
|
695
|
+
# existence of the context variable.
|
696
|
+
def ensure_aggregate!
|
697
|
+
unless @context
|
698
|
+
raise MisuseException, "function is not an aggregate"
|
699
|
+
end
|
700
|
+
end
|
701
|
+
private :ensure_aggregate!
|
702
|
+
|
703
|
+
end
|
704
|
+
|
705
|
+
# A proxy used for defining the callbacks to an aggregate function.
|
706
|
+
class AggregateDefinitionProxy # :nodoc:
|
707
|
+
attr_reader :step_callback, :finalize_callback
|
708
|
+
|
709
|
+
def step( &block )
|
710
|
+
@step_callback = block
|
711
|
+
end
|
712
|
+
|
713
|
+
def finalize( &block )
|
714
|
+
@finalize_callback = block
|
715
|
+
end
|
716
|
+
end
|
717
|
+
|
718
|
+
end
|
719
|
+
|
720
|
+
end
|
721
|
+
|