sqlite3-static 3.12.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.
@@ -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
@@ -0,0 +1,597 @@
1
+ require 'sqlite3/errors'
2
+
3
+ module SQLite3
4
+
5
+ # This module is intended for inclusion solely by the Database class. It
6
+ # defines convenience methods for the various pragmas supported by SQLite3.
7
+ #
8
+ # For a detailed description of these pragmas, see the SQLite3 documentation
9
+ # at http://sqlite.org/pragma.html.
10
+ module Pragmas
11
+
12
+ # Returns +true+ or +false+ depending on the value of the named pragma.
13
+ def get_boolean_pragma( name )
14
+ get_first_value( "PRAGMA #{name}" ) != "0"
15
+ end
16
+
17
+ # Sets the given pragma to the given boolean value. The value itself
18
+ # may be +true+ or +false+, or any other commonly used string or
19
+ # integer that represents truth.
20
+ def set_boolean_pragma( name, mode )
21
+ case mode
22
+ when String
23
+ case mode.downcase
24
+ when "on", "yes", "true", "y", "t"; mode = "'ON'"
25
+ when "off", "no", "false", "n", "f"; mode = "'OFF'"
26
+ else
27
+ raise Exception,
28
+ "unrecognized pragma parameter #{mode.inspect}"
29
+ end
30
+ when true, 1
31
+ mode = "ON"
32
+ when false, 0, nil
33
+ mode = "OFF"
34
+ else
35
+ raise Exception,
36
+ "unrecognized pragma parameter #{mode.inspect}"
37
+ end
38
+
39
+ execute( "PRAGMA #{name}=#{mode}" )
40
+ end
41
+
42
+ # Requests the given pragma (and parameters), and if the block is given,
43
+ # each row of the result set will be yielded to it. Otherwise, the results
44
+ # are returned as an array.
45
+ def get_query_pragma( name, *parms, &block ) # :yields: row
46
+ if parms.empty?
47
+ execute( "PRAGMA #{name}", &block )
48
+ else
49
+ args = "'" + parms.join("','") + "'"
50
+ execute( "PRAGMA #{name}( #{args} )", &block )
51
+ end
52
+ end
53
+
54
+ # Return the value of the given pragma.
55
+ def get_enum_pragma( name )
56
+ get_first_value( "PRAGMA #{name}" )
57
+ end
58
+
59
+ # Set the value of the given pragma to +mode+. The +mode+ parameter must
60
+ # conform to one of the values in the given +enum+ array. Each entry in
61
+ # the array is another array comprised of elements in the enumeration that
62
+ # have duplicate values. See #synchronous, #default_synchronous,
63
+ # #temp_store, and #default_temp_store for usage examples.
64
+ def set_enum_pragma( name, mode, enums )
65
+ match = enums.find { |p| p.find { |i| i.to_s.downcase == mode.to_s.downcase } }
66
+ raise Exception,
67
+ "unrecognized #{name} #{mode.inspect}" unless match
68
+ execute( "PRAGMA #{name}='#{match.first.upcase}'" )
69
+ end
70
+
71
+ # Returns the value of the given pragma as an integer.
72
+ def get_int_pragma( name )
73
+ get_first_value( "PRAGMA #{name}" ).to_i
74
+ end
75
+
76
+ # Set the value of the given pragma to the integer value of the +value+
77
+ # parameter.
78
+ def set_int_pragma( name, value )
79
+ execute( "PRAGMA #{name}=#{value.to_i}" )
80
+ end
81
+
82
+ # The enumeration of valid synchronous modes.
83
+ SYNCHRONOUS_MODES = [ [ 'full', 2 ], [ 'normal', 1 ], [ 'off', 0 ] ]
84
+
85
+ # The enumeration of valid temp store modes.
86
+ TEMP_STORE_MODES = [ [ 'default', 0 ], [ 'file', 1 ], [ 'memory', 2 ] ]
87
+
88
+ # The enumeration of valid auto vacuum modes.
89
+ AUTO_VACUUM_MODES = [ [ 'none', 0 ], [ 'full', 1 ], [ 'incremental', 2 ] ]
90
+
91
+ # The list of valid journaling modes.
92
+ JOURNAL_MODES = [ [ 'delete' ], [ 'truncate' ], [ 'persist' ], [ 'memory' ],
93
+ [ 'wal' ], [ 'off' ] ]
94
+
95
+ # The list of valid locking modes.
96
+ LOCKING_MODES = [ [ 'normal' ], [ 'exclusive' ] ]
97
+
98
+ # The list of valid encodings.
99
+ ENCODINGS = [ [ 'utf-8' ], [ 'utf-16' ], [ 'utf-16le' ], [ 'utf-16be ' ] ]
100
+
101
+ # The list of valid WAL checkpoints.
102
+ WAL_CHECKPOINTS = [ [ 'passive' ], [ 'full' ], [ 'restart' ], [ 'truncate' ] ]
103
+
104
+ # Does an integrity check on the database. If the check fails, a
105
+ # SQLite3::Exception will be raised. Otherwise it
106
+ # returns silently.
107
+ def integrity_check
108
+ execute( "PRAGMA integrity_check" ) do |row|
109
+ raise Exception, row[0] if row[0] != "ok"
110
+ end
111
+ end
112
+
113
+ def application_id
114
+ get_int_pragma "application_id"
115
+ end
116
+
117
+ def application_id=( integer )
118
+ set_int_pragma "application_id", integer
119
+ end
120
+
121
+ def auto_vacuum
122
+ get_enum_pragma "auto_vacuum"
123
+ end
124
+
125
+ def auto_vacuum=( mode )
126
+ set_enum_pragma "auto_vacuum", mode, AUTO_VACUUM_MODES
127
+ end
128
+
129
+ def automatic_index
130
+ get_boolean_pragma "automatic_index"
131
+ end
132
+
133
+ def automatic_index=( mode )
134
+ set_boolean_pragma "automatic_index", mode
135
+ end
136
+
137
+ def busy_timeout
138
+ get_int_pragma "busy_timeout"
139
+ end
140
+
141
+ def busy_timeout=( milliseconds )
142
+ set_int_pragma "busy_timeout", milliseconds
143
+ end
144
+
145
+ def cache_size
146
+ get_int_pragma "cache_size"
147
+ end
148
+
149
+ def cache_size=( size )
150
+ set_int_pragma "cache_size", size
151
+ end
152
+
153
+ def cache_spill
154
+ get_boolean_pragma "cache_spill"
155
+ end
156
+
157
+ def cache_spill=( mode )
158
+ set_boolean_pragma "cache_spill", mode
159
+ end
160
+
161
+ def case_sensitive_like=( mode )
162
+ set_boolean_pragma "case_sensitive_like", mode
163
+ end
164
+
165
+ def cell_size_check
166
+ get_boolean_pragma "cell_size_check"
167
+ end
168
+
169
+ def cell_size_check=( mode )
170
+ set_boolean_pragma "cell_size_check", mode
171
+ end
172
+
173
+ def checkpoint_fullfsync
174
+ get_boolean_pragma "checkpoint_fullfsync"
175
+ end
176
+
177
+ def checkpoint_fullfsync=( mode )
178
+ set_boolean_pragma "checkpoint_fullfsync", mode
179
+ end
180
+
181
+ def collation_list( &block ) # :yields: row
182
+ get_query_pragma "collation_list", &block
183
+ end
184
+
185
+ def compile_options( &block ) # :yields: row
186
+ get_query_pragma "compile_options", &block
187
+ end
188
+
189
+ def count_changes
190
+ get_boolean_pragma "count_changes"
191
+ end
192
+
193
+ def count_changes=( mode )
194
+ set_boolean_pragma "count_changes", mode
195
+ end
196
+
197
+ def data_version
198
+ get_int_pragma "data_version"
199
+ end
200
+
201
+ def database_list( &block ) # :yields: row
202
+ get_query_pragma "database_list", &block
203
+ end
204
+
205
+ def default_cache_size
206
+ get_int_pragma "default_cache_size"
207
+ end
208
+
209
+ def default_cache_size=( size )
210
+ set_int_pragma "default_cache_size", size
211
+ end
212
+
213
+ def default_synchronous
214
+ get_enum_pragma "default_synchronous"
215
+ end
216
+
217
+ def default_synchronous=( mode )
218
+ set_enum_pragma "default_synchronous", mode, SYNCHRONOUS_MODES
219
+ end
220
+
221
+ def default_temp_store
222
+ get_enum_pragma "default_temp_store"
223
+ end
224
+
225
+ def default_temp_store=( mode )
226
+ set_enum_pragma "default_temp_store", mode, TEMP_STORE_MODES
227
+ end
228
+
229
+ def defer_foreign_keys
230
+ get_boolean_pragma "defer_foreign_keys"
231
+ end
232
+
233
+ def defer_foreign_keys=( mode )
234
+ set_boolean_pragma "defer_foreign_keys", mode
235
+ end
236
+
237
+ def encoding
238
+ get_enum_pragma "encoding"
239
+ end
240
+
241
+ def encoding=( mode )
242
+ set_enum_pragma "encoding", mode, ENCODINGS
243
+ end
244
+
245
+ def foreign_key_check( *table, &block ) # :yields: row
246
+ get_query_pragma "foreign_key_check", *table, &block
247
+ end
248
+
249
+ def foreign_key_list( table, &block ) # :yields: row
250
+ get_query_pragma "foreign_key_list", table, &block
251
+ end
252
+
253
+ def foreign_keys
254
+ get_boolean_pragma "foreign_keys"
255
+ end
256
+
257
+ def foreign_keys=( mode )
258
+ set_boolean_pragma "foreign_keys", mode
259
+ end
260
+
261
+ def freelist_count
262
+ get_int_pragma "freelist_count"
263
+ end
264
+
265
+ def full_column_names
266
+ get_boolean_pragma "full_column_names"
267
+ end
268
+
269
+ def full_column_names=( mode )
270
+ set_boolean_pragma "full_column_names", mode
271
+ end
272
+
273
+ def fullfsync
274
+ get_boolean_pragma "fullfsync"
275
+ end
276
+
277
+ def fullfsync=( mode )
278
+ set_boolean_pragma "fullfsync", mode
279
+ end
280
+
281
+ def ignore_check_constraints=( mode )
282
+ set_boolean_pragma "ignore_check_constraints", mode
283
+ end
284
+
285
+ def incremental_vacuum( pages, &block ) # :yields: row
286
+ get_query_pragma "incremental_vacuum", pages, &block
287
+ end
288
+
289
+ def index_info( index, &block ) # :yields: row
290
+ get_query_pragma "index_info", index, &block
291
+ end
292
+
293
+ def index_list( table, &block ) # :yields: row
294
+ get_query_pragma "index_list", table, &block
295
+ end
296
+
297
+ def index_xinfo( index, &block ) # :yields: row
298
+ get_query_pragma "index_xinfo", index, &block
299
+ end
300
+
301
+ def integrity_check( *num_errors, &block ) # :yields: row
302
+ get_query_pragma "integrity_check", *num_errors, &block
303
+ end
304
+
305
+ def journal_mode
306
+ get_enum_pragma "journal_mode"
307
+ end
308
+
309
+ def journal_mode=( mode )
310
+ set_enum_pragma "journal_mode", mode, JOURNAL_MODES
311
+ end
312
+
313
+ def journal_size_limit
314
+ get_int_pragma "journal_size_limit"
315
+ end
316
+
317
+ def journal_size_limit=( size )
318
+ set_int_pragma "journal_size_limit", size
319
+ end
320
+
321
+ def legacy_file_format
322
+ get_boolean_pragma "legacy_file_format"
323
+ end
324
+
325
+ def legacy_file_format=( mode )
326
+ set_boolean_pragma "legacy_file_format", mode
327
+ end
328
+
329
+ def locking_mode
330
+ get_enum_pragma "locking_mode"
331
+ end
332
+
333
+ def locking_mode=( mode )
334
+ set_enum_pragma "locking_mode", mode, LOCKING_MODES
335
+ end
336
+
337
+ def max_page_count
338
+ get_int_pragma "max_page_count"
339
+ end
340
+
341
+ def max_page_count=( size )
342
+ set_int_pragma "max_page_count", size
343
+ end
344
+
345
+ def mmap_size
346
+ get_int_pragma "mmap_size"
347
+ end
348
+
349
+ def mmap_size=( size )
350
+ set_int_pragma "mmap_size", size
351
+ end
352
+
353
+ def page_count
354
+ get_int_pragma "page_count"
355
+ end
356
+
357
+ def page_size
358
+ get_int_pragma "page_size"
359
+ end
360
+
361
+ def page_size=( size )
362
+ set_int_pragma "page_size", size
363
+ end
364
+
365
+ def parser_trace=( mode )
366
+ set_boolean_pragma "parser_trace", mode
367
+ end
368
+
369
+ def query_only
370
+ get_boolean_pragma "query_only"
371
+ end
372
+
373
+ def query_only=( mode )
374
+ set_boolean_pragma "query_only", mode
375
+ end
376
+
377
+ def quick_check( *num_errors, &block ) # :yields: row
378
+ get_query_pragma "quick_check", *num_errors, &block
379
+ end
380
+
381
+ def read_uncommitted
382
+ get_boolean_pragma "read_uncommitted"
383
+ end
384
+
385
+ def read_uncommitted=( mode )
386
+ set_boolean_pragma "read_uncommitted", mode
387
+ end
388
+
389
+ def recursive_triggers
390
+ get_boolean_pragma "recursive_triggers"
391
+ end
392
+
393
+ def recursive_triggers=( mode )
394
+ set_boolean_pragma "recursive_triggers", mode
395
+ end
396
+
397
+ def reverse_unordered_selects
398
+ get_boolean_pragma "reverse_unordered_selects"
399
+ end
400
+
401
+ def reverse_unordered_selects=( mode )
402
+ set_boolean_pragma "reverse_unordered_selects", mode
403
+ end
404
+
405
+ def schema_cookie
406
+ get_int_pragma "schema_cookie"
407
+ end
408
+
409
+ def schema_cookie=( cookie )
410
+ set_int_pragma "schema_cookie", cookie
411
+ end
412
+
413
+ def schema_version
414
+ get_int_pragma "schema_version"
415
+ end
416
+
417
+ def schema_version=( version )
418
+ set_int_pragma "schema_version", version
419
+ end
420
+
421
+ def secure_delete
422
+ get_boolean_pragma "secure_delete"
423
+ end
424
+
425
+ def secure_delete=( mode )
426
+ set_boolean_pragma "secure_delete", mode
427
+ end
428
+
429
+ def short_column_names
430
+ get_boolean_pragma "short_column_names"
431
+ end
432
+
433
+ def short_column_names=( mode )
434
+ set_boolean_pragma "short_column_names", mode
435
+ end
436
+
437
+ def shrink_memory
438
+ execute( "PRAGMA shrink_memory" )
439
+ end
440
+
441
+ def soft_heap_limit
442
+ get_int_pragma "soft_heap_limit"
443
+ end
444
+
445
+ def soft_heap_limit=( mode )
446
+ set_int_pragma "soft_heap_limit", mode
447
+ end
448
+
449
+ def stats( &block ) # :yields: row
450
+ get_query_pragma "stats", &block
451
+ end
452
+
453
+ def synchronous
454
+ get_enum_pragma "synchronous"
455
+ end
456
+
457
+ def synchronous=( mode )
458
+ set_enum_pragma "synchronous", mode, SYNCHRONOUS_MODES
459
+ end
460
+
461
+ def temp_store
462
+ get_enum_pragma "temp_store"
463
+ end
464
+
465
+ def temp_store=( mode )
466
+ set_enum_pragma "temp_store", mode, TEMP_STORE_MODES
467
+ end
468
+
469
+ def threads
470
+ get_int_pragma "threads"
471
+ end
472
+
473
+ def threads=( count )
474
+ set_int_pragma "threads", count
475
+ end
476
+
477
+ def user_cookie
478
+ get_int_pragma "user_cookie"
479
+ end
480
+
481
+ def user_cookie=( cookie )
482
+ set_int_pragma "user_cookie", cookie
483
+ end
484
+
485
+ def user_version
486
+ get_int_pragma "user_version"
487
+ end
488
+
489
+ def user_version=( version )
490
+ set_int_pragma "user_version", version
491
+ end
492
+
493
+ def vdbe_addoptrace=( mode )
494
+ set_boolean_pragma "vdbe_addoptrace", mode
495
+ end
496
+
497
+ def vdbe_debug=( mode )
498
+ set_boolean_pragma "vdbe_debug", mode
499
+ end
500
+
501
+ def vdbe_listing=( mode )
502
+ set_boolean_pragma "vdbe_listing", mode
503
+ end
504
+
505
+ def vdbe_trace
506
+ get_boolean_pragma "vdbe_trace"
507
+ end
508
+
509
+ def vdbe_trace=( mode )
510
+ set_boolean_pragma "vdbe_trace", mode
511
+ end
512
+
513
+ def wal_autocheckpoint
514
+ get_int_pragma "wal_autocheckpoint"
515
+ end
516
+
517
+ def wal_autocheckpoint=( mode )
518
+ set_int_pragma "wal_autocheckpoint", mode
519
+ end
520
+
521
+ def wal_checkpoint
522
+ get_enum_pragma "wal_checkpoint"
523
+ end
524
+
525
+ def wal_checkpoint=( mode )
526
+ set_enum_pragma "wal_checkpoint", mode, WAL_CHECKPOINTS
527
+ end
528
+
529
+ def writable_schema=( mode )
530
+ set_boolean_pragma "writable_schema", mode
531
+ end
532
+
533
+ ###
534
+ # Returns information about +table+. Yields each row of table information
535
+ # if a block is provided.
536
+ def table_info table
537
+ stmt = prepare "PRAGMA table_info(#{table})"
538
+ columns = stmt.columns
539
+
540
+ needs_tweak_default =
541
+ version_compare(SQLite3.libversion.to_s, "3.3.7") > 0
542
+
543
+ result = [] unless block_given?
544
+ stmt.each do |row|
545
+ new_row = Hash[columns.zip(row)]
546
+
547
+ # FIXME: This should be removed but is required for older versions
548
+ # of rails
549
+ if(Object.const_defined?(:ActiveRecord))
550
+ new_row['notnull'] = new_row['notnull'].to_s
551
+ end
552
+
553
+ tweak_default(new_row) if needs_tweak_default
554
+
555
+ if block_given?
556
+ yield new_row
557
+ else
558
+ result << new_row
559
+ end
560
+ end
561
+ stmt.close
562
+
563
+ result
564
+ end
565
+
566
+ private
567
+
568
+ # Compares two version strings
569
+ def version_compare(v1, v2)
570
+ v1 = v1.split(".").map { |i| i.to_i }
571
+ v2 = v2.split(".").map { |i| i.to_i }
572
+ parts = [v1.length, v2.length].max
573
+ v1.push 0 while v1.length < parts
574
+ v2.push 0 while v2.length < parts
575
+ v1.zip(v2).each do |a,b|
576
+ return -1 if a < b
577
+ return 1 if a > b
578
+ end
579
+ return 0
580
+ end
581
+
582
+ # Since SQLite 3.3.8, the table_info pragma has returned the default
583
+ # value of the row as a quoted SQL value. This method essentially
584
+ # unquotes those values.
585
+ def tweak_default(hash)
586
+ case hash["dflt_value"]
587
+ when /^null$/i
588
+ hash["dflt_value"] = nil
589
+ when /^'(.*)'$/m
590
+ hash["dflt_value"] = $1.gsub(/''/, "'")
591
+ when /^"(.*)"$/m
592
+ hash["dflt_value"] = $1.gsub(/""/, '"')
593
+ end
594
+ end
595
+ end
596
+
597
+ end