sqlite3 2.0.0-x86_64-linux-gnu

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