sqlite3 1.5.0 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +390 -0
- data/CONTRIBUTING.md +34 -2
- data/{faq/faq.md → FAQ.md} +0 -43
- data/INSTALLATION.md +269 -0
- data/LICENSE +18 -22
- data/README.md +76 -128
- data/dependencies.yml +13 -0
- data/ext/sqlite3/aggregator.c +142 -146
- data/ext/sqlite3/aggregator.h +2 -4
- data/ext/sqlite3/backup.c +86 -64
- data/ext/sqlite3/backup.h +2 -2
- data/ext/sqlite3/database.c +543 -465
- data/ext/sqlite3/database.h +9 -4
- data/ext/sqlite3/exception.c +111 -92
- data/ext/sqlite3/exception.h +3 -1
- data/ext/sqlite3/extconf.rb +83 -51
- data/ext/sqlite3/sqlite3.c +160 -115
- data/ext/sqlite3/sqlite3_ruby.h +2 -2
- data/ext/sqlite3/statement.c +518 -293
- data/ext/sqlite3/statement.h +3 -3
- data/ext/sqlite3/timespec.h +20 -0
- data/lib/sqlite3/constants.rb +171 -47
- data/lib/sqlite3/database.rb +141 -181
- data/lib/sqlite3/errors.rb +26 -1
- data/lib/sqlite3/pragmas.rb +128 -138
- data/lib/sqlite3/resultset.rb +14 -105
- data/lib/sqlite3/statement.rb +58 -13
- data/lib/sqlite3/value.rb +17 -20
- data/lib/sqlite3/version.rb +1 -21
- data/lib/sqlite3.rb +6 -4
- data/ports/archives/sqlite-autoconf-3460000.tar.gz +0 -0
- metadata +19 -107
- data/API_CHANGES.md +0 -49
- data/ChangeLog.cvs +0 -88
- data/Gemfile +0 -3
- data/LICENSE-DEPENDENCIES +0 -20
- data/faq/faq.rb +0 -145
- data/faq/faq.yml +0 -426
- data/lib/sqlite3/translator.rb +0 -118
- data/ports/archives/sqlite-autoconf-3380500.tar.gz +0 -0
- data/test/helper.rb +0 -27
- data/test/test_backup.rb +0 -33
- data/test/test_collation.rb +0 -82
- data/test/test_database.rb +0 -545
- data/test/test_database_flags.rb +0 -95
- data/test/test_database_readonly.rb +0 -36
- data/test/test_database_readwrite.rb +0 -41
- data/test/test_deprecated.rb +0 -44
- data/test/test_encoding.rb +0 -155
- data/test/test_integration.rb +0 -507
- data/test/test_integration_aggregate.rb +0 -336
- data/test/test_integration_open_close.rb +0 -30
- data/test/test_integration_pending.rb +0 -115
- data/test/test_integration_resultset.rb +0 -142
- data/test/test_integration_statement.rb +0 -194
- data/test/test_result_set.rb +0 -37
- data/test/test_sqlite3.rb +0 -30
- data/test/test_statement.rb +0 -263
- data/test/test_statement_execute.rb +0 -35
data/lib/sqlite3/database.rb
CHANGED
@@ -1,12 +1,10 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require 'sqlite3/value'
|
1
|
+
require "sqlite3/constants"
|
2
|
+
require "sqlite3/errors"
|
3
|
+
require "sqlite3/pragmas"
|
4
|
+
require "sqlite3/statement"
|
5
|
+
require "sqlite3/value"
|
7
6
|
|
8
7
|
module SQLite3
|
9
|
-
|
10
8
|
# The Database class encapsulates a single connection to a SQLite3 database.
|
11
9
|
# Its usage is very straightforward:
|
12
10
|
#
|
@@ -18,7 +16,7 @@ module SQLite3
|
|
18
16
|
# end
|
19
17
|
# end
|
20
18
|
#
|
21
|
-
# It wraps the lower-level methods
|
19
|
+
# It wraps the lower-level methods provided by the selected driver, and
|
22
20
|
# includes the Pragmas module for access to various pragma convenience
|
23
21
|
# methods.
|
24
22
|
#
|
@@ -32,22 +30,43 @@ module SQLite3
|
|
32
30
|
# ArrayFields module from Ara Howard. If you require the ArrayFields
|
33
31
|
# module before performing a query, and if you have not enabled results as
|
34
32
|
# hashes, then the results will all be indexible by field name.
|
33
|
+
#
|
34
|
+
# Thread safety:
|
35
|
+
#
|
36
|
+
# When `SQLite3.threadsafe?` returns true, it is safe to share instances of
|
37
|
+
# the database class among threads without adding specific locking. Other
|
38
|
+
# object instances may require applications to provide their own locks if
|
39
|
+
# they are to be shared among threads. Please see the README.md for more
|
40
|
+
# information.
|
35
41
|
class Database
|
36
42
|
attr_reader :collations
|
37
43
|
|
38
44
|
include Pragmas
|
39
45
|
|
40
46
|
class << self
|
47
|
+
# Without block works exactly as new.
|
48
|
+
# With block, like new closes the database at the end, but unlike new
|
49
|
+
# returns the result of the block instead of the database instance.
|
50
|
+
def open(*args)
|
51
|
+
database = new(*args)
|
41
52
|
|
42
|
-
|
53
|
+
if block_given?
|
54
|
+
begin
|
55
|
+
yield database
|
56
|
+
ensure
|
57
|
+
database.close
|
58
|
+
end
|
59
|
+
else
|
60
|
+
database
|
61
|
+
end
|
62
|
+
end
|
43
63
|
|
44
64
|
# Quotes the given string, making it safe to use in an SQL statement.
|
45
65
|
# It replaces all instances of the single-quote character with two
|
46
66
|
# single-quote characters. The modified string is returned.
|
47
|
-
def quote(
|
48
|
-
string.gsub(
|
67
|
+
def quote(string)
|
68
|
+
string.gsub("'", "''")
|
49
69
|
end
|
50
|
-
|
51
70
|
end
|
52
71
|
|
53
72
|
# A boolean that indicates whether rows in result sets should be returned
|
@@ -56,12 +75,22 @@ module SQLite3
|
|
56
75
|
|
57
76
|
# call-seq: SQLite3::Database.new(file, options = {})
|
58
77
|
#
|
59
|
-
# Create a new Database object that opens the given file.
|
60
|
-
#
|
78
|
+
# Create a new Database object that opens the given file.
|
79
|
+
#
|
80
|
+
# Supported permissions +options+:
|
81
|
+
# - the default mode is <tt>READWRITE | CREATE</tt>
|
82
|
+
# - +:readonly+: boolean (default false), true to set the mode to +READONLY+
|
83
|
+
# - +:readwrite+: boolean (default false), true to set the mode to +READWRITE+
|
84
|
+
# - +:flags+: set the mode to a combination of SQLite3::Constants::Open flags.
|
85
|
+
#
|
86
|
+
# Supported encoding +options+:
|
87
|
+
# - +:utf16+: boolean (default false), is the filename's encoding UTF-16 (only needed if the filename encoding is not UTF_16LE or BE)
|
88
|
+
#
|
89
|
+
# Other supported +options+:
|
90
|
+
# - +:strict+: boolean (default false), disallow the use of double-quoted string literals (see https://www.sqlite.org/quirks.html#double_quoted_string_literals_are_accepted)
|
91
|
+
# - +:results_as_hash+: boolean (default false), return rows as hashes instead of arrays
|
92
|
+
# - +:default_transaction_mode+: one of +:deferred+ (default), +:immediate+, or +:exclusive+. If a mode is not specified in a call to #transaction, this will be the default transaction mode.
|
61
93
|
#
|
62
|
-
# By default, the new database will return result rows as arrays
|
63
|
-
# (#results_as_hash) and has type translation disabled (#type_translation=).
|
64
|
-
|
65
94
|
def initialize file, options = {}, zvfs = nil
|
66
95
|
mode = Constants::Open::READWRITE | Constants::Open::CREATE
|
67
96
|
|
@@ -94,16 +123,15 @@ module SQLite3
|
|
94
123
|
end
|
95
124
|
end
|
96
125
|
|
97
|
-
@tracefunc
|
98
|
-
@authorizer
|
99
|
-
@
|
100
|
-
@
|
101
|
-
@collations
|
102
|
-
@functions
|
103
|
-
@results_as_hash
|
104
|
-
@
|
105
|
-
@
|
106
|
-
@readonly = mode & Constants::Open::READONLY != 0
|
126
|
+
@tracefunc = nil
|
127
|
+
@authorizer = nil
|
128
|
+
@busy_handler = nil
|
129
|
+
@progress_handler = nil
|
130
|
+
@collations = {}
|
131
|
+
@functions = {}
|
132
|
+
@results_as_hash = options[:results_as_hash]
|
133
|
+
@readonly = mode & Constants::Open::READONLY != 0
|
134
|
+
@default_transaction_mode = options[:default_transaction_mode] || :deferred
|
107
135
|
|
108
136
|
if block_given?
|
109
137
|
begin
|
@@ -114,32 +142,18 @@ module SQLite3
|
|
114
142
|
end
|
115
143
|
end
|
116
144
|
|
117
|
-
|
118
|
-
|
119
|
-
#
|
120
|
-
|
121
|
-
|
122
|
-
eowarn
|
123
|
-
@type_translator = make_type_translator value
|
124
|
-
@type_translation = value
|
125
|
-
end
|
126
|
-
attr_reader :type_translation # :nodoc:
|
127
|
-
|
128
|
-
# Return the type translator employed by this database instance. Each
|
129
|
-
# database instance has its own type translator; this allows for different
|
130
|
-
# type handlers to be installed in each instance without affecting other
|
131
|
-
# instances. Furthermore, the translators are instantiated lazily, so that
|
132
|
-
# if a database does not use type translation, it will not be burdened by
|
133
|
-
# the overhead of a useless type translator. (See the Translator class.)
|
134
|
-
def translator
|
135
|
-
@translator ||= Translator.new
|
145
|
+
# call-seq: db.encoding
|
146
|
+
#
|
147
|
+
# Fetch the encoding set on this database
|
148
|
+
def encoding
|
149
|
+
prepare("PRAGMA encoding") { |stmt| Encoding.find(stmt.first.first) }
|
136
150
|
end
|
137
151
|
|
138
152
|
# Installs (or removes) a block that will be invoked for every access
|
139
153
|
# to the database. If the block returns 0 (or +nil+), the statement
|
140
154
|
# is allowed to proceed. Returning 1 causes an authorization error to
|
141
155
|
# occur, and returning 2 causes the access to be silently denied.
|
142
|
-
def authorizer(
|
156
|
+
def authorizer(&block)
|
143
157
|
self.authorizer = block
|
144
158
|
end
|
145
159
|
|
@@ -149,7 +163,7 @@ in version 2.0.0.
|
|
149
163
|
# The Statement can then be executed using Statement#execute.
|
150
164
|
#
|
151
165
|
def prepare sql
|
152
|
-
stmt = SQLite3::Statement.new(
|
166
|
+
stmt = SQLite3::Statement.new(self, sql)
|
153
167
|
return stmt unless block_given?
|
154
168
|
|
155
169
|
begin
|
@@ -162,7 +176,7 @@ in version 2.0.0.
|
|
162
176
|
# Returns the filename for the database named +db_name+. +db_name+ defaults
|
163
177
|
# to "main". Main return `nil` or an empty string if the database is
|
164
178
|
# temporary or in-memory.
|
165
|
-
def filename db_name =
|
179
|
+
def filename db_name = "main"
|
166
180
|
db_filename db_name
|
167
181
|
end
|
168
182
|
|
@@ -180,31 +194,17 @@ in version 2.0.0.
|
|
180
194
|
#
|
181
195
|
# See also #execute2, #query, and #execute_batch for additional ways of
|
182
196
|
# executing statements.
|
183
|
-
def execute sql, bind_vars = [],
|
184
|
-
|
185
|
-
if args.empty?
|
186
|
-
bind_vars = []
|
187
|
-
else
|
188
|
-
bind_vars = [bind_vars] + args
|
189
|
-
end
|
190
|
-
|
191
|
-
warn(<<-eowarn) if $VERBOSE
|
192
|
-
#{caller[0]} is calling SQLite3::Database#execute with nil or multiple bind params
|
193
|
-
without using an array. Please switch to passing bind parameters as an array.
|
194
|
-
Support for bind parameters as *args will be removed in 2.0.0.
|
195
|
-
eowarn
|
196
|
-
end
|
197
|
-
|
198
|
-
prepare( sql ) do |stmt|
|
197
|
+
def execute sql, bind_vars = [], &block
|
198
|
+
prepare(sql) do |stmt|
|
199
199
|
stmt.bind_params(bind_vars)
|
200
|
-
stmt
|
200
|
+
stmt = build_result_set stmt
|
201
201
|
|
202
|
-
if
|
202
|
+
if block
|
203
203
|
stmt.each do |row|
|
204
204
|
yield row
|
205
205
|
end
|
206
206
|
else
|
207
|
-
stmt.to_a
|
207
|
+
stmt.to_a.freeze
|
208
208
|
end
|
209
209
|
end
|
210
210
|
end
|
@@ -219,15 +219,16 @@ Support for bind parameters as *args will be removed in 2.0.0.
|
|
219
219
|
#
|
220
220
|
# See also #execute, #query, and #execute_batch for additional ways of
|
221
221
|
# executing statements.
|
222
|
-
def execute2(
|
223
|
-
prepare(
|
224
|
-
result = stmt.execute(
|
222
|
+
def execute2(sql, *bind_vars)
|
223
|
+
prepare(sql) do |stmt|
|
224
|
+
result = stmt.execute(*bind_vars)
|
225
225
|
if block_given?
|
226
226
|
yield stmt.columns
|
227
227
|
result.each { |row| yield row }
|
228
228
|
else
|
229
|
-
return result.
|
230
|
-
|
229
|
+
return result.each_with_object([stmt.columns]) { |row, arr|
|
230
|
+
arr << row
|
231
|
+
}
|
231
232
|
end
|
232
233
|
end
|
233
234
|
end
|
@@ -238,53 +239,28 @@ Support for bind parameters as *args will be removed in 2.0.0.
|
|
238
239
|
# in turn. The same bind parameters, if given, will be applied to each
|
239
240
|
# statement.
|
240
241
|
#
|
241
|
-
# This always returns
|
242
|
-
# rows.
|
242
|
+
# This always returns the result of the last statement.
|
243
243
|
#
|
244
244
|
# See also #execute_batch2 for additional ways of
|
245
245
|
# executing statements.
|
246
|
-
def execute_batch(
|
247
|
-
# FIXME: remove this stuff later
|
248
|
-
unless [Array, Hash].include?(bind_vars.class)
|
249
|
-
bind_vars = [bind_vars]
|
250
|
-
warn(<<-eowarn) if $VERBOSE
|
251
|
-
#{caller[0]} is calling SQLite3::Database#execute_batch with bind parameters
|
252
|
-
that are not a list of a hash. Please switch to passing bind parameters as an
|
253
|
-
array or hash. Support for this behavior will be removed in version 2.0.0.
|
254
|
-
eowarn
|
255
|
-
end
|
256
|
-
|
257
|
-
# FIXME: remove this stuff later
|
258
|
-
if bind_vars.nil? || !args.empty?
|
259
|
-
if args.empty?
|
260
|
-
bind_vars = []
|
261
|
-
else
|
262
|
-
bind_vars = [nil] + args
|
263
|
-
end
|
264
|
-
|
265
|
-
warn(<<-eowarn) if $VERBOSE
|
266
|
-
#{caller[0]} is calling SQLite3::Database#execute_batch with nil or multiple bind params
|
267
|
-
without using an array. Please switch to passing bind parameters as an array.
|
268
|
-
Support for this behavior will be removed in version 2.0.0.
|
269
|
-
eowarn
|
270
|
-
end
|
271
|
-
|
246
|
+
def execute_batch(sql, bind_vars = [])
|
272
247
|
sql = sql.strip
|
273
|
-
|
274
|
-
|
248
|
+
result = nil
|
249
|
+
until sql.empty?
|
250
|
+
prepare(sql) do |stmt|
|
275
251
|
unless stmt.closed?
|
276
252
|
# FIXME: this should probably use sqlite3's api for batch execution
|
277
253
|
# This implementation requires stepping over the results.
|
278
254
|
if bind_vars.length == stmt.bind_parameter_count
|
279
255
|
stmt.bind_params(bind_vars)
|
280
256
|
end
|
281
|
-
stmt.step
|
257
|
+
result = stmt.step
|
282
258
|
end
|
283
259
|
sql = stmt.remainder.strip
|
284
260
|
end
|
285
261
|
end
|
286
|
-
|
287
|
-
|
262
|
+
|
263
|
+
result
|
288
264
|
end
|
289
265
|
|
290
266
|
# Executes all SQL statements in the given string. By contrast, the other
|
@@ -301,7 +277,7 @@ Support for this behavior will be removed in version 2.0.0.
|
|
301
277
|
# See also #execute_batch for additional ways of
|
302
278
|
# executing statements.
|
303
279
|
def execute_batch2(sql, &block)
|
304
|
-
if
|
280
|
+
if block
|
305
281
|
result = exec_batch(sql, @results_as_hash)
|
306
282
|
result.map do |val|
|
307
283
|
yield val
|
@@ -322,23 +298,8 @@ Support for this behavior will be removed in version 2.0.0.
|
|
322
298
|
# returned, or you could have problems with locks on the table. If called
|
323
299
|
# with a block, +close+ will be invoked implicitly when the block
|
324
300
|
# terminates.
|
325
|
-
def query(
|
326
|
-
|
327
|
-
if bind_vars.nil? || !args.empty?
|
328
|
-
if args.empty?
|
329
|
-
bind_vars = []
|
330
|
-
else
|
331
|
-
bind_vars = [bind_vars] + args
|
332
|
-
end
|
333
|
-
|
334
|
-
warn(<<-eowarn) if $VERBOSE
|
335
|
-
#{caller[0]} is calling SQLite3::Database#query with nil or multiple bind params
|
336
|
-
without using an array. Please switch to passing bind parameters as an array.
|
337
|
-
Support for this will be removed in version 2.0.0.
|
338
|
-
eowarn
|
339
|
-
end
|
340
|
-
|
341
|
-
result = prepare( sql ).execute( bind_vars )
|
301
|
+
def query(sql, bind_vars = [])
|
302
|
+
result = prepare(sql).execute(bind_vars)
|
342
303
|
if block_given?
|
343
304
|
begin
|
344
305
|
yield result
|
@@ -346,7 +307,7 @@ Support for this will be removed in version 2.0.0.
|
|
346
307
|
result.close
|
347
308
|
end
|
348
309
|
else
|
349
|
-
|
310
|
+
result
|
350
311
|
end
|
351
312
|
end
|
352
313
|
|
@@ -354,8 +315,8 @@ Support for this will be removed in version 2.0.0.
|
|
354
315
|
# discarding all others. It is otherwise identical to #execute.
|
355
316
|
#
|
356
317
|
# See also #get_first_value.
|
357
|
-
def get_first_row(
|
358
|
-
execute(
|
318
|
+
def get_first_row(sql, *bind_vars)
|
319
|
+
execute(sql, *bind_vars).first
|
359
320
|
end
|
360
321
|
|
361
322
|
# A convenience method for obtaining the first value of the first row of a
|
@@ -363,8 +324,8 @@ Support for this will be removed in version 2.0.0.
|
|
363
324
|
# identical to #execute.
|
364
325
|
#
|
365
326
|
# See also #get_first_row.
|
366
|
-
def get_first_value(
|
367
|
-
query(
|
327
|
+
def get_first_value(sql, *bind_vars)
|
328
|
+
query(sql, bind_vars) do |rs|
|
368
329
|
if (row = rs.next)
|
369
330
|
return @results_as_hash ? row[rs.columns[0]] : row[0]
|
370
331
|
end
|
@@ -372,7 +333,7 @@ Support for this will be removed in version 2.0.0.
|
|
372
333
|
nil
|
373
334
|
end
|
374
335
|
|
375
|
-
|
336
|
+
alias_method :busy_timeout, :busy_timeout=
|
376
337
|
|
377
338
|
# Creates a new function for use in SQL statements. It will be added as
|
378
339
|
# +name+, with the given +arity+. (For variable arity functions, use
|
@@ -397,7 +358,7 @@ Support for this will be removed in version 2.0.0.
|
|
397
358
|
# end
|
398
359
|
#
|
399
360
|
# puts db.get_first_value( "select maim(name) from table" )
|
400
|
-
def create_function name, arity, text_rep=Constants::TextRep::UTF8, &block
|
361
|
+
def create_function name, arity, text_rep = Constants::TextRep::UTF8, &block
|
401
362
|
define_function_with_flags(name, text_rep) do |*args|
|
402
363
|
fp = FunctionProxy.new
|
403
364
|
block.call(fp, *args)
|
@@ -442,20 +403,20 @@ Support for this will be removed in version 2.0.0.
|
|
442
403
|
#
|
443
404
|
# See also #create_aggregate_handler for a more object-oriented approach to
|
444
405
|
# aggregate functions.
|
445
|
-
def create_aggregate(
|
446
|
-
text_rep=Constants::TextRep::ANY, &block
|
406
|
+
def create_aggregate(name, arity, step = nil, finalize = nil,
|
407
|
+
text_rep = Constants::TextRep::ANY, &block)
|
447
408
|
|
448
409
|
proxy = Class.new do
|
449
|
-
def self.step(
|
410
|
+
def self.step(&block)
|
450
411
|
define_method(:step_with_ctx, &block)
|
451
412
|
end
|
452
413
|
|
453
|
-
def self.finalize(
|
414
|
+
def self.finalize(&block)
|
454
415
|
define_method(:finalize_with_ctx, &block)
|
455
416
|
end
|
456
417
|
end
|
457
418
|
|
458
|
-
if
|
419
|
+
if block
|
459
420
|
proxy.instance_eval(&block)
|
460
421
|
else
|
461
422
|
proxy.class_eval do
|
@@ -481,7 +442,7 @@ Support for this will be removed in version 2.0.0.
|
|
481
442
|
@ctx = FunctionProxy.new
|
482
443
|
end
|
483
444
|
|
484
|
-
def step(
|
445
|
+
def step(*args)
|
485
446
|
step_with_ctx(@ctx, *args)
|
486
447
|
end
|
487
448
|
|
@@ -540,7 +501,7 @@ Support for this will be removed in version 2.0.0.
|
|
540
501
|
#
|
541
502
|
# db.create_aggregate_handler( LengthsAggregateHandler )
|
542
503
|
# puts db.get_first_value( "select lengths(name) from A" )
|
543
|
-
def create_aggregate_handler(
|
504
|
+
def create_aggregate_handler(handler)
|
544
505
|
# This is a compatibility shim so the (basically pointless) FunctionProxy
|
545
506
|
# "ctx" object is passed as first argument to both step() and finalize().
|
546
507
|
# Now its up to the library user whether he prefers to store his
|
@@ -554,7 +515,7 @@ Support for this will be removed in version 2.0.0.
|
|
554
515
|
@fp = FunctionProxy.new
|
555
516
|
end
|
556
517
|
|
557
|
-
def step(
|
518
|
+
def step(*args)
|
558
519
|
super(@fp, *args)
|
559
520
|
end
|
560
521
|
|
@@ -577,7 +538,7 @@ Support for this will be removed in version 2.0.0.
|
|
577
538
|
# individual instances of the aggregate function. Regular ruby objects
|
578
539
|
# already provide a suitable +clone+.
|
579
540
|
# The functions arity is the arity of the +step+ method.
|
580
|
-
def define_aggregator(
|
541
|
+
def define_aggregator(name, aggregator)
|
581
542
|
# Previously, this has been implemented in C. Now this is just yet
|
582
543
|
# another compatibility shim
|
583
544
|
proxy = Class.new do
|
@@ -617,8 +578,10 @@ Support for this will be removed in version 2.0.0.
|
|
617
578
|
# by SQLite, so attempting to nest a transaction will result in a runtime
|
618
579
|
# exception.
|
619
580
|
#
|
620
|
-
# The +mode+ parameter may be either <tt>:deferred</tt
|
581
|
+
# The +mode+ parameter may be either <tt>:deferred</tt>,
|
621
582
|
# <tt>:immediate</tt>, or <tt>:exclusive</tt>.
|
583
|
+
# If `nil` is specified, the default transaction mode, which was
|
584
|
+
# passed to #initialize, is used.
|
622
585
|
#
|
623
586
|
# If a block is given, the database instance is yielded to it, and the
|
624
587
|
# transaction is committed when the block terminates. If the block
|
@@ -629,8 +592,9 @@ Support for this will be removed in version 2.0.0.
|
|
629
592
|
# If a block is not given, it is the caller's responsibility to end the
|
630
593
|
# transaction explicitly, either by calling #commit, or by calling
|
631
594
|
# #rollback.
|
632
|
-
def transaction(
|
633
|
-
|
595
|
+
def transaction(mode = nil)
|
596
|
+
mode = @default_transaction_mode if mode.nil?
|
597
|
+
execute "begin #{mode} transaction"
|
634
598
|
|
635
599
|
if block_given?
|
636
600
|
abort = false
|
@@ -642,9 +606,9 @@ Support for this will be removed in version 2.0.0.
|
|
642
606
|
ensure
|
643
607
|
abort and rollback or commit
|
644
608
|
end
|
609
|
+
else
|
610
|
+
true
|
645
611
|
end
|
646
|
-
|
647
|
-
true
|
648
612
|
end
|
649
613
|
|
650
614
|
# Commits the current transaction. If there is no current transaction,
|
@@ -671,6 +635,25 @@ Support for this will be removed in version 2.0.0.
|
|
671
635
|
@readonly
|
672
636
|
end
|
673
637
|
|
638
|
+
# Sets a #busy_handler that releases the GVL between retries,
|
639
|
+
# but only retries up to the indicated number of +milliseconds+.
|
640
|
+
# This is an alternative to #busy_timeout, which holds the GVL
|
641
|
+
# while SQLite sleeps and retries.
|
642
|
+
def busy_handler_timeout=(milliseconds)
|
643
|
+
timeout_seconds = milliseconds.fdiv(1000)
|
644
|
+
|
645
|
+
busy_handler do |count|
|
646
|
+
now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
647
|
+
if count.zero?
|
648
|
+
@timeout_deadline = now + timeout_seconds
|
649
|
+
elsif now > @timeout_deadline
|
650
|
+
next false
|
651
|
+
else
|
652
|
+
sleep(0.001)
|
653
|
+
end
|
654
|
+
end
|
655
|
+
end
|
656
|
+
|
674
657
|
# A helper class for dealing with custom functions (see #create_function,
|
675
658
|
# #create_aggregate, and #create_aggregate_handler). It encapsulates the
|
676
659
|
# opaque function object that represents the current invocation. It also
|
@@ -687,54 +670,31 @@ Support for this will be removed in version 2.0.0.
|
|
687
670
|
# it is non-nil, it must quack like a Hash. If it is nil, then none of
|
688
671
|
# the context functions will be available.
|
689
672
|
def initialize
|
690
|
-
@result
|
691
|
-
@context
|
692
|
-
end
|
693
|
-
|
694
|
-
# Set the result of the function to the given error message.
|
695
|
-
# The function will then return that error.
|
696
|
-
def set_error( error )
|
697
|
-
@driver.result_error( @func, error.to_s, -1 )
|
698
|
-
end
|
699
|
-
|
700
|
-
# (Only available to aggregate functions.) Returns the number of rows
|
701
|
-
# that the aggregate has processed so far. This will include the current
|
702
|
-
# row, and so will always return at least 1.
|
703
|
-
def count
|
704
|
-
@driver.aggregate_count( @func )
|
673
|
+
@result = nil
|
674
|
+
@context = {}
|
705
675
|
end
|
706
676
|
|
707
677
|
# Returns the value with the given key from the context. This is only
|
708
678
|
# available to aggregate functions.
|
709
|
-
def [](
|
710
|
-
@context[
|
679
|
+
def [](key)
|
680
|
+
@context[key]
|
711
681
|
end
|
712
682
|
|
713
683
|
# Sets the value with the given key in the context. This is only
|
714
684
|
# available to aggregate functions.
|
715
|
-
def []=(
|
716
|
-
@context[
|
685
|
+
def []=(key, value)
|
686
|
+
@context[key] = value
|
717
687
|
end
|
718
688
|
end
|
719
689
|
|
720
|
-
#
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
NULL_TRANSLATOR = lambda { |_, row| row }
|
728
|
-
|
729
|
-
def make_type_translator should_translate
|
730
|
-
if should_translate
|
731
|
-
lambda { |types, row|
|
732
|
-
types.zip(row).map do |type, value|
|
733
|
-
translator.translate( type, value )
|
734
|
-
end
|
735
|
-
}
|
690
|
+
# Given a statement, return a result set.
|
691
|
+
# This is not intended for general consumption
|
692
|
+
# :nodoc:
|
693
|
+
def build_result_set stmt
|
694
|
+
if results_as_hash
|
695
|
+
HashResultSet.new(self, stmt)
|
736
696
|
else
|
737
|
-
|
697
|
+
ResultSet.new(self, stmt)
|
738
698
|
end
|
739
699
|
end
|
740
700
|
end
|
data/lib/sqlite3/errors.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "sqlite3/constants"
|
2
2
|
|
3
3
|
module SQLite3
|
4
4
|
class Exception < ::StandardError
|
@@ -7,29 +7,54 @@ module SQLite3
|
|
7
7
|
end
|
8
8
|
|
9
9
|
class SQLException < Exception; end
|
10
|
+
|
10
11
|
class InternalException < Exception; end
|
12
|
+
|
11
13
|
class PermissionException < Exception; end
|
14
|
+
|
12
15
|
class AbortException < Exception; end
|
16
|
+
|
13
17
|
class BusyException < Exception; end
|
18
|
+
|
14
19
|
class LockedException < Exception; end
|
20
|
+
|
15
21
|
class MemoryException < Exception; end
|
22
|
+
|
16
23
|
class ReadOnlyException < Exception; end
|
24
|
+
|
17
25
|
class InterruptException < Exception; end
|
26
|
+
|
18
27
|
class IOException < Exception; end
|
28
|
+
|
19
29
|
class CorruptException < Exception; end
|
30
|
+
|
20
31
|
class NotFoundException < Exception; end
|
32
|
+
|
21
33
|
class FullException < Exception; end
|
34
|
+
|
22
35
|
class CantOpenException < Exception; end
|
36
|
+
|
23
37
|
class ProtocolException < Exception; end
|
38
|
+
|
24
39
|
class EmptyException < Exception; end
|
40
|
+
|
25
41
|
class SchemaChangedException < Exception; end
|
42
|
+
|
26
43
|
class TooBigException < Exception; end
|
44
|
+
|
27
45
|
class ConstraintException < Exception; end
|
46
|
+
|
28
47
|
class MismatchException < Exception; end
|
48
|
+
|
29
49
|
class MisuseException < Exception; end
|
50
|
+
|
30
51
|
class UnsupportedException < Exception; end
|
52
|
+
|
31
53
|
class AuthorizationException < Exception; end
|
54
|
+
|
32
55
|
class FormatException < Exception; end
|
56
|
+
|
33
57
|
class RangeException < Exception; end
|
58
|
+
|
34
59
|
class NotADatabaseException < Exception; end
|
35
60
|
end
|