mockley_crew 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mockley_crew/version.rb +1 -1
- data/vendor/gems/sqlite3/cache/sqlite3-1.3.13.gem +0 -0
- data/vendor/gems/sqlite3/extensions/x86_64-linux/2.5.0/sqlite3-1.3.13/gem.build_complete +0 -0
- data/vendor/gems/sqlite3/extensions/x86_64-linux/2.5.0/sqlite3-1.3.13/gem_make.out +33 -0
- data/vendor/gems/sqlite3/extensions/x86_64-linux/2.5.0/sqlite3-1.3.13/mkmf.log +592 -0
- data/vendor/gems/sqlite3/extensions/x86_64-linux/2.5.0/sqlite3-1.3.13/sqlite3/sqlite3_native.so +0 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/API_CHANGES.rdoc +50 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/CHANGELOG.rdoc +292 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/ChangeLog.cvs +88 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/Gemfile +15 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/LICENSE +27 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/Manifest.txt +52 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/README.rdoc +118 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/Rakefile +10 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/ext/sqlite3/Makefile +263 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/ext/sqlite3/backup.c +168 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/ext/sqlite3/backup.h +15 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/ext/sqlite3/backup.o +0 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/ext/sqlite3/database.c +888 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/ext/sqlite3/database.h +15 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/ext/sqlite3/database.o +0 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/ext/sqlite3/exception.c +94 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/ext/sqlite3/exception.h +8 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/ext/sqlite3/exception.o +0 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/ext/sqlite3/extconf.rb +71 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/ext/sqlite3/sqlite3.c +152 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/ext/sqlite3/sqlite3.o +0 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/ext/sqlite3/sqlite3_native.so +0 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/ext/sqlite3/sqlite3_ruby.h +52 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/ext/sqlite3/statement.c +447 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/ext/sqlite3/statement.h +16 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/ext/sqlite3/statement.o +0 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/faq/faq.rb +145 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/faq/faq.yml +426 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/lib/sqlite3/constants.rb +49 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/lib/sqlite3/database.rb +591 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/lib/sqlite3/errors.rb +44 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/lib/sqlite3/pragmas.rb +588 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/lib/sqlite3/resultset.rb +195 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/lib/sqlite3/sqlite3_native.so +0 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb +144 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/lib/sqlite3/translator.rb +118 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/lib/sqlite3/value.rb +57 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/lib/sqlite3/version.rb +25 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/lib/sqlite3.rb +15 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/setup.rb +1333 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/tasks/faq.rake +9 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/tasks/gem.rake +38 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/tasks/native.rake +52 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/tasks/vendor_sqlite3.rake +97 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/test/helper.rb +18 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/test/test_backup.rb +33 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/test/test_collation.rb +82 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/test/test_database.rb +430 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/test/test_database_readonly.rb +36 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/test/test_deprecated.rb +44 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/test/test_encoding.rb +155 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/test/test_integration.rb +576 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/test/test_integration_open_close.rb +30 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/test/test_integration_pending.rb +115 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/test/test_integration_resultset.rb +159 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/test/test_integration_statement.rb +194 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/test/test_result_set.rb +37 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/test/test_sqlite3.rb +21 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/test/test_statement.rb +260 -0
- data/vendor/gems/sqlite3/gems/sqlite3-1.3.13/test/test_statement_execute.rb +35 -0
- data/vendor/gems/sqlite3/specifications/sqlite3-1.3.13.gemspec +56 -0
- metadata +67 -1
@@ -0,0 +1,591 @@
|
|
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
|
+
attr_reader :collations
|
37
|
+
|
38
|
+
include Pragmas
|
39
|
+
|
40
|
+
class << self
|
41
|
+
|
42
|
+
alias :open :new
|
43
|
+
|
44
|
+
# Quotes the given string, making it safe to use in an SQL statement.
|
45
|
+
# It replaces all instances of the single-quote character with two
|
46
|
+
# single-quote characters. The modified string is returned.
|
47
|
+
def quote( string )
|
48
|
+
string.gsub( /'/, "''" )
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
# A boolean that indicates whether rows in result sets should be returned
|
54
|
+
# as hashes or not. By default, rows are returned as arrays.
|
55
|
+
attr_accessor :results_as_hash
|
56
|
+
|
57
|
+
def type_translation= value # :nodoc:
|
58
|
+
warn(<<-eowarn) if $VERBOSE
|
59
|
+
#{caller[0]} is calling SQLite3::Database#type_translation=
|
60
|
+
SQLite3::Database#type_translation= is deprecated and will be removed
|
61
|
+
in version 2.0.0.
|
62
|
+
eowarn
|
63
|
+
@type_translation = value
|
64
|
+
end
|
65
|
+
attr_reader :type_translation # :nodoc:
|
66
|
+
|
67
|
+
# Return the type translator employed by this database instance. Each
|
68
|
+
# database instance has its own type translator; this allows for different
|
69
|
+
# type handlers to be installed in each instance without affecting other
|
70
|
+
# instances. Furthermore, the translators are instantiated lazily, so that
|
71
|
+
# if a database does not use type translation, it will not be burdened by
|
72
|
+
# the overhead of a useless type translator. (See the Translator class.)
|
73
|
+
def translator
|
74
|
+
@translator ||= Translator.new
|
75
|
+
end
|
76
|
+
|
77
|
+
# Installs (or removes) a block that will be invoked for every access
|
78
|
+
# to the database. If the block returns 0 (or +nil+), the statement
|
79
|
+
# is allowed to proceed. Returning 1 causes an authorization error to
|
80
|
+
# occur, and returning 2 causes the access to be silently denied.
|
81
|
+
def authorizer( &block )
|
82
|
+
self.authorizer = block
|
83
|
+
end
|
84
|
+
|
85
|
+
# Returns a Statement object representing the given SQL. This does not
|
86
|
+
# execute the statement; it merely prepares the statement for execution.
|
87
|
+
#
|
88
|
+
# The Statement can then be executed using Statement#execute.
|
89
|
+
#
|
90
|
+
def prepare sql
|
91
|
+
stmt = SQLite3::Statement.new( self, sql )
|
92
|
+
return stmt unless block_given?
|
93
|
+
|
94
|
+
begin
|
95
|
+
yield stmt
|
96
|
+
ensure
|
97
|
+
stmt.close unless stmt.closed?
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# Returns the filename for the database named +db_name+. +db_name+ defaults
|
102
|
+
# to "main". Main return `nil` or an empty string if the database is
|
103
|
+
# temporary or in-memory.
|
104
|
+
def filename db_name = 'main'
|
105
|
+
db_filename db_name
|
106
|
+
end
|
107
|
+
|
108
|
+
# Executes the given SQL statement. If additional parameters are given,
|
109
|
+
# they are treated as bind variables, and are bound to the placeholders in
|
110
|
+
# the query.
|
111
|
+
#
|
112
|
+
# Note that if any of the values passed to this are hashes, then the
|
113
|
+
# key/value pairs are each bound separately, with the key being used as
|
114
|
+
# the name of the placeholder to bind the value to.
|
115
|
+
#
|
116
|
+
# The block is optional. If given, it will be invoked for each row returned
|
117
|
+
# by the query. Otherwise, any results are accumulated into an array and
|
118
|
+
# returned wholesale.
|
119
|
+
#
|
120
|
+
# See also #execute2, #query, and #execute_batch for additional ways of
|
121
|
+
# executing statements.
|
122
|
+
def execute sql, bind_vars = [], *args, &block
|
123
|
+
if bind_vars.nil? || !args.empty?
|
124
|
+
if args.empty?
|
125
|
+
bind_vars = []
|
126
|
+
else
|
127
|
+
bind_vars = [bind_vars] + args
|
128
|
+
end
|
129
|
+
|
130
|
+
warn(<<-eowarn) if $VERBOSE
|
131
|
+
#{caller[0]} is calling SQLite3::Database#execute with nil or multiple bind params
|
132
|
+
without using an array. Please switch to passing bind parameters as an array.
|
133
|
+
Support for bind parameters as *args will be removed in 2.0.0.
|
134
|
+
eowarn
|
135
|
+
end
|
136
|
+
|
137
|
+
prepare( sql ) do |stmt|
|
138
|
+
stmt.bind_params(bind_vars)
|
139
|
+
columns = stmt.columns
|
140
|
+
stmt = ResultSet.new(self, stmt).to_a if type_translation
|
141
|
+
|
142
|
+
if block_given?
|
143
|
+
stmt.each do |row|
|
144
|
+
if @results_as_hash
|
145
|
+
yield type_translation ? row : ordered_map_for(columns, row)
|
146
|
+
else
|
147
|
+
yield row
|
148
|
+
end
|
149
|
+
end
|
150
|
+
else
|
151
|
+
if @results_as_hash
|
152
|
+
stmt.map { |row|
|
153
|
+
type_translation ? row : ordered_map_for(columns, row)
|
154
|
+
}
|
155
|
+
else
|
156
|
+
stmt.to_a
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
# Executes the given SQL statement, exactly as with #execute. However, the
|
163
|
+
# first row returned (either via the block, or in the returned array) is
|
164
|
+
# always the names of the columns. Subsequent rows correspond to the data
|
165
|
+
# from the result set.
|
166
|
+
#
|
167
|
+
# Thus, even if the query itself returns no rows, this method will always
|
168
|
+
# return at least one row--the names of the columns.
|
169
|
+
#
|
170
|
+
# See also #execute, #query, and #execute_batch for additional ways of
|
171
|
+
# executing statements.
|
172
|
+
def execute2( sql, *bind_vars )
|
173
|
+
prepare( sql ) do |stmt|
|
174
|
+
result = stmt.execute( *bind_vars )
|
175
|
+
if block_given?
|
176
|
+
yield stmt.columns
|
177
|
+
result.each { |row| yield row }
|
178
|
+
else
|
179
|
+
return result.inject( [ stmt.columns ] ) { |arr,row|
|
180
|
+
arr << row; arr }
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
# Executes all SQL statements in the given string. By contrast, the other
|
186
|
+
# means of executing queries will only execute the first statement in the
|
187
|
+
# string, ignoring all subsequent statements. This will execute each one
|
188
|
+
# in turn. The same bind parameters, if given, will be applied to each
|
189
|
+
# statement.
|
190
|
+
#
|
191
|
+
# This always returns +nil+, making it unsuitable for queries that return
|
192
|
+
# rows.
|
193
|
+
def execute_batch( sql, bind_vars = [], *args )
|
194
|
+
# FIXME: remove this stuff later
|
195
|
+
unless [Array, Hash].include?(bind_vars.class)
|
196
|
+
bind_vars = [bind_vars]
|
197
|
+
warn(<<-eowarn) if $VERBOSE
|
198
|
+
#{caller[0]} is calling SQLite3::Database#execute_batch with bind parameters
|
199
|
+
that are not a list of a hash. Please switch to passing bind parameters as an
|
200
|
+
array or hash. Support for this behavior will be removed in version 2.0.0.
|
201
|
+
eowarn
|
202
|
+
end
|
203
|
+
|
204
|
+
# FIXME: remove this stuff later
|
205
|
+
if bind_vars.nil? || !args.empty?
|
206
|
+
if args.empty?
|
207
|
+
bind_vars = []
|
208
|
+
else
|
209
|
+
bind_vars = [nil] + args
|
210
|
+
end
|
211
|
+
|
212
|
+
warn(<<-eowarn) if $VERBOSE
|
213
|
+
#{caller[0]} is calling SQLite3::Database#execute_batch with nil or multiple bind params
|
214
|
+
without using an array. Please switch to passing bind parameters as an array.
|
215
|
+
Support for this behavior will be removed in version 2.0.0.
|
216
|
+
eowarn
|
217
|
+
end
|
218
|
+
|
219
|
+
sql = sql.strip
|
220
|
+
until sql.empty? do
|
221
|
+
prepare( sql ) do |stmt|
|
222
|
+
unless stmt.closed?
|
223
|
+
# FIXME: this should probably use sqlite3's api for batch execution
|
224
|
+
# This implementation requires stepping over the results.
|
225
|
+
if bind_vars.length == stmt.bind_parameter_count
|
226
|
+
stmt.bind_params(bind_vars)
|
227
|
+
end
|
228
|
+
stmt.step
|
229
|
+
end
|
230
|
+
sql = stmt.remainder.strip
|
231
|
+
end
|
232
|
+
end
|
233
|
+
# FIXME: we should not return `nil` as a success return value
|
234
|
+
nil
|
235
|
+
end
|
236
|
+
|
237
|
+
# This is a convenience method for creating a statement, binding
|
238
|
+
# paramters to it, and calling execute:
|
239
|
+
#
|
240
|
+
# result = db.query( "select * from foo where a=?", [5])
|
241
|
+
# # is the same as
|
242
|
+
# result = db.prepare( "select * from foo where a=?" ).execute( 5 )
|
243
|
+
#
|
244
|
+
# You must be sure to call +close+ on the ResultSet instance that is
|
245
|
+
# returned, or you could have problems with locks on the table. If called
|
246
|
+
# with a block, +close+ will be invoked implicitly when the block
|
247
|
+
# terminates.
|
248
|
+
def query( sql, bind_vars = [], *args )
|
249
|
+
|
250
|
+
if bind_vars.nil? || !args.empty?
|
251
|
+
if args.empty?
|
252
|
+
bind_vars = []
|
253
|
+
else
|
254
|
+
bind_vars = [bind_vars] + args
|
255
|
+
end
|
256
|
+
|
257
|
+
warn(<<-eowarn) if $VERBOSE
|
258
|
+
#{caller[0]} is calling SQLite3::Database#query with nil or multiple bind params
|
259
|
+
without using an array. Please switch to passing bind parameters as an array.
|
260
|
+
Support for this will be removed in version 2.0.0.
|
261
|
+
eowarn
|
262
|
+
end
|
263
|
+
|
264
|
+
result = prepare( sql ).execute( bind_vars )
|
265
|
+
if block_given?
|
266
|
+
begin
|
267
|
+
yield result
|
268
|
+
ensure
|
269
|
+
result.close
|
270
|
+
end
|
271
|
+
else
|
272
|
+
return result
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
# A convenience method for obtaining the first row of a result set, and
|
277
|
+
# discarding all others. It is otherwise identical to #execute.
|
278
|
+
#
|
279
|
+
# See also #get_first_value.
|
280
|
+
def get_first_row( sql, *bind_vars )
|
281
|
+
execute( sql, *bind_vars ).first
|
282
|
+
end
|
283
|
+
|
284
|
+
# A convenience method for obtaining the first value of the first row of a
|
285
|
+
# result set, and discarding all other values and rows. It is otherwise
|
286
|
+
# identical to #execute.
|
287
|
+
#
|
288
|
+
# See also #get_first_row.
|
289
|
+
def get_first_value( sql, *bind_vars )
|
290
|
+
execute( sql, *bind_vars ) { |row| return row[0] }
|
291
|
+
nil
|
292
|
+
end
|
293
|
+
|
294
|
+
alias :busy_timeout :busy_timeout=
|
295
|
+
|
296
|
+
# Creates a new function for use in SQL statements. It will be added as
|
297
|
+
# +name+, with the given +arity+. (For variable arity functions, use
|
298
|
+
# -1 for the arity.)
|
299
|
+
#
|
300
|
+
# The block should accept at least one parameter--the FunctionProxy
|
301
|
+
# instance that wraps this function invocation--and any other
|
302
|
+
# arguments it needs (up to its arity).
|
303
|
+
#
|
304
|
+
# The block does not return a value directly. Instead, it will invoke
|
305
|
+
# the FunctionProxy#result= method on the +func+ parameter and
|
306
|
+
# indicate the return value that way.
|
307
|
+
#
|
308
|
+
# Example:
|
309
|
+
#
|
310
|
+
# db.create_function( "maim", 1 ) do |func, value|
|
311
|
+
# if value.nil?
|
312
|
+
# func.result = nil
|
313
|
+
# else
|
314
|
+
# func.result = value.split(//).sort.join
|
315
|
+
# end
|
316
|
+
# end
|
317
|
+
#
|
318
|
+
# puts db.get_first_value( "select maim(name) from table" )
|
319
|
+
def create_function name, arity, text_rep=Constants::TextRep::ANY, &block
|
320
|
+
define_function(name) do |*args|
|
321
|
+
fp = FunctionProxy.new
|
322
|
+
block.call(fp, *args)
|
323
|
+
fp.result
|
324
|
+
end
|
325
|
+
self
|
326
|
+
end
|
327
|
+
|
328
|
+
# Creates a new aggregate function for use in SQL statements. Aggregate
|
329
|
+
# functions are functions that apply over every row in the result set,
|
330
|
+
# instead of over just a single row. (A very common aggregate function
|
331
|
+
# is the "count" function, for determining the number of rows that match
|
332
|
+
# a query.)
|
333
|
+
#
|
334
|
+
# The new function will be added as +name+, with the given +arity+. (For
|
335
|
+
# variable arity functions, use -1 for the arity.)
|
336
|
+
#
|
337
|
+
# The +step+ parameter must be a proc object that accepts as its first
|
338
|
+
# parameter a FunctionProxy instance (representing the function
|
339
|
+
# invocation), with any subsequent parameters (up to the function's arity).
|
340
|
+
# The +step+ callback will be invoked once for each row of the result set.
|
341
|
+
#
|
342
|
+
# The +finalize+ parameter must be a +proc+ object that accepts only a
|
343
|
+
# single parameter, the FunctionProxy instance representing the current
|
344
|
+
# function invocation. It should invoke FunctionProxy#result= to
|
345
|
+
# store the result of the function.
|
346
|
+
#
|
347
|
+
# Example:
|
348
|
+
#
|
349
|
+
# db.create_aggregate( "lengths", 1 ) do
|
350
|
+
# step do |func, value|
|
351
|
+
# func[ :total ] ||= 0
|
352
|
+
# func[ :total ] += ( value ? value.length : 0 )
|
353
|
+
# end
|
354
|
+
#
|
355
|
+
# finalize do |func|
|
356
|
+
# func.result = func[ :total ] || 0
|
357
|
+
# end
|
358
|
+
# end
|
359
|
+
#
|
360
|
+
# puts db.get_first_value( "select lengths(name) from table" )
|
361
|
+
#
|
362
|
+
# See also #create_aggregate_handler for a more object-oriented approach to
|
363
|
+
# aggregate functions.
|
364
|
+
def create_aggregate( name, arity, step=nil, finalize=nil,
|
365
|
+
text_rep=Constants::TextRep::ANY, &block )
|
366
|
+
|
367
|
+
factory = Class.new do
|
368
|
+
def self.step( &block )
|
369
|
+
define_method(:step, &block)
|
370
|
+
end
|
371
|
+
|
372
|
+
def self.finalize( &block )
|
373
|
+
define_method(:finalize, &block)
|
374
|
+
end
|
375
|
+
end
|
376
|
+
|
377
|
+
if block_given?
|
378
|
+
factory.instance_eval(&block)
|
379
|
+
else
|
380
|
+
factory.class_eval do
|
381
|
+
define_method(:step, step)
|
382
|
+
define_method(:finalize, finalize)
|
383
|
+
end
|
384
|
+
end
|
385
|
+
|
386
|
+
proxy = factory.new
|
387
|
+
proxy.extend(Module.new {
|
388
|
+
attr_accessor :ctx
|
389
|
+
|
390
|
+
def step( *args )
|
391
|
+
super(@ctx, *args)
|
392
|
+
end
|
393
|
+
|
394
|
+
def finalize
|
395
|
+
super(@ctx)
|
396
|
+
result = @ctx.result
|
397
|
+
@ctx = FunctionProxy.new
|
398
|
+
result
|
399
|
+
end
|
400
|
+
})
|
401
|
+
proxy.ctx = FunctionProxy.new
|
402
|
+
define_aggregator(name, proxy)
|
403
|
+
end
|
404
|
+
|
405
|
+
# This is another approach to creating an aggregate function (see
|
406
|
+
# #create_aggregate). Instead of explicitly specifying the name,
|
407
|
+
# callbacks, arity, and type, you specify a factory object
|
408
|
+
# (the "handler") that knows how to obtain all of that information. The
|
409
|
+
# handler should respond to the following messages:
|
410
|
+
#
|
411
|
+
# +arity+:: corresponds to the +arity+ parameter of #create_aggregate. This
|
412
|
+
# message is optional, and if the handler does not respond to it,
|
413
|
+
# the function will have an arity of -1.
|
414
|
+
# +name+:: this is the name of the function. The handler _must_ implement
|
415
|
+
# this message.
|
416
|
+
# +new+:: this must be implemented by the handler. It should return a new
|
417
|
+
# instance of the object that will handle a specific invocation of
|
418
|
+
# the function.
|
419
|
+
#
|
420
|
+
# The handler instance (the object returned by the +new+ message, described
|
421
|
+
# above), must respond to the following messages:
|
422
|
+
#
|
423
|
+
# +step+:: this is the method that will be called for each step of the
|
424
|
+
# aggregate function's evaluation. It should implement the same
|
425
|
+
# signature as the +step+ callback for #create_aggregate.
|
426
|
+
# +finalize+:: this is the method that will be called to finalize the
|
427
|
+
# aggregate function's evaluation. It should implement the
|
428
|
+
# same signature as the +finalize+ callback for
|
429
|
+
# #create_aggregate.
|
430
|
+
#
|
431
|
+
# Example:
|
432
|
+
#
|
433
|
+
# class LengthsAggregateHandler
|
434
|
+
# def self.arity; 1; end
|
435
|
+
# def self.name; 'lengths'; end
|
436
|
+
#
|
437
|
+
# def initialize
|
438
|
+
# @total = 0
|
439
|
+
# end
|
440
|
+
#
|
441
|
+
# def step( ctx, name )
|
442
|
+
# @total += ( name ? name.length : 0 )
|
443
|
+
# end
|
444
|
+
#
|
445
|
+
# def finalize( ctx )
|
446
|
+
# ctx.result = @total
|
447
|
+
# end
|
448
|
+
# end
|
449
|
+
#
|
450
|
+
# db.create_aggregate_handler( LengthsAggregateHandler )
|
451
|
+
# puts db.get_first_value( "select lengths(name) from A" )
|
452
|
+
def create_aggregate_handler( handler )
|
453
|
+
proxy = Class.new do
|
454
|
+
def initialize klass
|
455
|
+
@klass = klass
|
456
|
+
@fp = FunctionProxy.new
|
457
|
+
end
|
458
|
+
|
459
|
+
def step( *args )
|
460
|
+
instance.step(@fp, *args)
|
461
|
+
end
|
462
|
+
|
463
|
+
def finalize
|
464
|
+
instance.finalize @fp
|
465
|
+
@instance = nil
|
466
|
+
@fp.result
|
467
|
+
end
|
468
|
+
|
469
|
+
private
|
470
|
+
|
471
|
+
def instance
|
472
|
+
@instance ||= @klass.new
|
473
|
+
end
|
474
|
+
end
|
475
|
+
define_aggregator(handler.name, proxy.new(handler))
|
476
|
+
self
|
477
|
+
end
|
478
|
+
|
479
|
+
# Begins a new transaction. Note that nested transactions are not allowed
|
480
|
+
# by SQLite, so attempting to nest a transaction will result in a runtime
|
481
|
+
# exception.
|
482
|
+
#
|
483
|
+
# The +mode+ parameter may be either <tt>:deferred</tt> (the default),
|
484
|
+
# <tt>:immediate</tt>, or <tt>:exclusive</tt>.
|
485
|
+
#
|
486
|
+
# If a block is given, the database instance is yielded to it, and the
|
487
|
+
# transaction is committed when the block terminates. If the block
|
488
|
+
# raises an exception, a rollback will be performed instead. Note that if
|
489
|
+
# a block is given, #commit and #rollback should never be called
|
490
|
+
# explicitly or you'll get an error when the block terminates.
|
491
|
+
#
|
492
|
+
# If a block is not given, it is the caller's responsibility to end the
|
493
|
+
# transaction explicitly, either by calling #commit, or by calling
|
494
|
+
# #rollback.
|
495
|
+
def transaction( mode = :deferred )
|
496
|
+
execute "begin #{mode.to_s} transaction"
|
497
|
+
|
498
|
+
if block_given?
|
499
|
+
abort = false
|
500
|
+
begin
|
501
|
+
yield self
|
502
|
+
rescue ::Object
|
503
|
+
abort = true
|
504
|
+
raise
|
505
|
+
ensure
|
506
|
+
abort and rollback or commit
|
507
|
+
end
|
508
|
+
end
|
509
|
+
|
510
|
+
true
|
511
|
+
end
|
512
|
+
|
513
|
+
# Commits the current transaction. If there is no current transaction,
|
514
|
+
# this will cause an error to be raised. This returns +true+, in order
|
515
|
+
# to allow it to be used in idioms like
|
516
|
+
# <tt>abort? and rollback or commit</tt>.
|
517
|
+
def commit
|
518
|
+
execute "commit transaction"
|
519
|
+
true
|
520
|
+
end
|
521
|
+
|
522
|
+
# Rolls the current transaction back. If there is no current transaction,
|
523
|
+
# this will cause an error to be raised. This returns +true+, in order
|
524
|
+
# to allow it to be used in idioms like
|
525
|
+
# <tt>abort? and rollback or commit</tt>.
|
526
|
+
def rollback
|
527
|
+
execute "rollback transaction"
|
528
|
+
true
|
529
|
+
end
|
530
|
+
|
531
|
+
# Returns +true+ if the database has been open in readonly mode
|
532
|
+
# A helper to check before performing any operation
|
533
|
+
def readonly?
|
534
|
+
@readonly
|
535
|
+
end
|
536
|
+
|
537
|
+
# A helper class for dealing with custom functions (see #create_function,
|
538
|
+
# #create_aggregate, and #create_aggregate_handler). It encapsulates the
|
539
|
+
# opaque function object that represents the current invocation. It also
|
540
|
+
# provides more convenient access to the API functions that operate on
|
541
|
+
# the function object.
|
542
|
+
#
|
543
|
+
# This class will almost _always_ be instantiated indirectly, by working
|
544
|
+
# with the create methods mentioned above.
|
545
|
+
class FunctionProxy
|
546
|
+
attr_accessor :result
|
547
|
+
|
548
|
+
# Create a new FunctionProxy that encapsulates the given +func+ object.
|
549
|
+
# If context is non-nil, the functions context will be set to that. If
|
550
|
+
# it is non-nil, it must quack like a Hash. If it is nil, then none of
|
551
|
+
# the context functions will be available.
|
552
|
+
def initialize
|
553
|
+
@result = nil
|
554
|
+
@context = {}
|
555
|
+
end
|
556
|
+
|
557
|
+
# Set the result of the function to the given error message.
|
558
|
+
# The function will then return that error.
|
559
|
+
def set_error( error )
|
560
|
+
@driver.result_error( @func, error.to_s, -1 )
|
561
|
+
end
|
562
|
+
|
563
|
+
# (Only available to aggregate functions.) Returns the number of rows
|
564
|
+
# that the aggregate has processed so far. This will include the current
|
565
|
+
# row, and so will always return at least 1.
|
566
|
+
def count
|
567
|
+
@driver.aggregate_count( @func )
|
568
|
+
end
|
569
|
+
|
570
|
+
# Returns the value with the given key from the context. This is only
|
571
|
+
# available to aggregate functions.
|
572
|
+
def []( key )
|
573
|
+
@context[ key ]
|
574
|
+
end
|
575
|
+
|
576
|
+
# Sets the value with the given key in the context. This is only
|
577
|
+
# available to aggregate functions.
|
578
|
+
def []=( key, value )
|
579
|
+
@context[ key ] = value
|
580
|
+
end
|
581
|
+
end
|
582
|
+
|
583
|
+
private
|
584
|
+
|
585
|
+
def ordered_map_for columns, row
|
586
|
+
h = Hash[*columns.zip(row).flatten]
|
587
|
+
row.each_with_index { |r, i| h[i] = r }
|
588
|
+
h
|
589
|
+
end
|
590
|
+
end
|
591
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'sqlite3/constants'
|
2
|
+
|
3
|
+
module SQLite3
|
4
|
+
class Exception < ::StandardError
|
5
|
+
@code = 0
|
6
|
+
|
7
|
+
# The numeric error code that this exception represents.
|
8
|
+
def self.code
|
9
|
+
@code
|
10
|
+
end
|
11
|
+
|
12
|
+
# A convenience for accessing the error code for this exception.
|
13
|
+
def code
|
14
|
+
self.class.code
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class SQLException < Exception; end
|
19
|
+
class InternalException < Exception; end
|
20
|
+
class PermissionException < Exception; end
|
21
|
+
class AbortException < Exception; end
|
22
|
+
class BusyException < Exception; end
|
23
|
+
class LockedException < Exception; end
|
24
|
+
class MemoryException < Exception; end
|
25
|
+
class ReadOnlyException < Exception; end
|
26
|
+
class InterruptException < Exception; end
|
27
|
+
class IOException < Exception; end
|
28
|
+
class CorruptException < Exception; end
|
29
|
+
class NotFoundException < Exception; end
|
30
|
+
class FullException < Exception; end
|
31
|
+
class CantOpenException < Exception; end
|
32
|
+
class ProtocolException < Exception; end
|
33
|
+
class EmptyException < Exception; end
|
34
|
+
class SchemaChangedException < Exception; end
|
35
|
+
class TooBigException < Exception; end
|
36
|
+
class ConstraintException < Exception; end
|
37
|
+
class MismatchException < Exception; end
|
38
|
+
class MisuseException < Exception; end
|
39
|
+
class UnsupportedException < Exception; end
|
40
|
+
class AuthorizationException < Exception; end
|
41
|
+
class FormatException < Exception; end
|
42
|
+
class RangeException < Exception; end
|
43
|
+
class NotADatabaseException < Exception; end
|
44
|
+
end
|