sqlite3 1.5.0.rc1-x86-linux

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/.gemtest +0 -0
  3. data/API_CHANGES.md +49 -0
  4. data/CHANGELOG.md +419 -0
  5. data/CONTRIBUTING.md +24 -0
  6. data/ChangeLog.cvs +88 -0
  7. data/Gemfile +3 -0
  8. data/LICENSE +27 -0
  9. data/LICENSE-DEPENDENCIES +20 -0
  10. data/README.md +233 -0
  11. data/ext/sqlite3/aggregator.c +273 -0
  12. data/ext/sqlite3/aggregator.h +12 -0
  13. data/ext/sqlite3/backup.c +168 -0
  14. data/ext/sqlite3/backup.h +15 -0
  15. data/ext/sqlite3/database.c +853 -0
  16. data/ext/sqlite3/database.h +17 -0
  17. data/ext/sqlite3/exception.c +98 -0
  18. data/ext/sqlite3/exception.h +8 -0
  19. data/ext/sqlite3/extconf.rb +159 -0
  20. data/ext/sqlite3/sqlite3.c +163 -0
  21. data/ext/sqlite3/sqlite3_ruby.h +45 -0
  22. data/ext/sqlite3/statement.c +442 -0
  23. data/ext/sqlite3/statement.h +16 -0
  24. data/faq/faq.md +431 -0
  25. data/faq/faq.rb +145 -0
  26. data/faq/faq.yml +426 -0
  27. data/lib/sqlite3/2.6/sqlite3_native.so +0 -0
  28. data/lib/sqlite3/2.7/sqlite3_native.so +0 -0
  29. data/lib/sqlite3/3.0/sqlite3_native.so +0 -0
  30. data/lib/sqlite3/3.1/sqlite3_native.so +0 -0
  31. data/lib/sqlite3/constants.rb +50 -0
  32. data/lib/sqlite3/database.rb +741 -0
  33. data/lib/sqlite3/errors.rb +35 -0
  34. data/lib/sqlite3/pragmas.rb +595 -0
  35. data/lib/sqlite3/resultset.rb +187 -0
  36. data/lib/sqlite3/statement.rb +145 -0
  37. data/lib/sqlite3/translator.rb +118 -0
  38. data/lib/sqlite3/value.rb +57 -0
  39. data/lib/sqlite3/version.rb +25 -0
  40. data/lib/sqlite3.rb +15 -0
  41. data/test/helper.rb +27 -0
  42. data/test/test_backup.rb +33 -0
  43. data/test/test_collation.rb +82 -0
  44. data/test/test_database.rb +538 -0
  45. data/test/test_database_flags.rb +95 -0
  46. data/test/test_database_readonly.rb +36 -0
  47. data/test/test_database_readwrite.rb +41 -0
  48. data/test/test_deprecated.rb +44 -0
  49. data/test/test_encoding.rb +155 -0
  50. data/test/test_integration.rb +507 -0
  51. data/test/test_integration_aggregate.rb +336 -0
  52. data/test/test_integration_open_close.rb +30 -0
  53. data/test/test_integration_pending.rb +115 -0
  54. data/test/test_integration_resultset.rb +142 -0
  55. data/test/test_integration_statement.rb +194 -0
  56. data/test/test_result_set.rb +37 -0
  57. data/test/test_sqlite3.rb +25 -0
  58. data/test/test_statement.rb +263 -0
  59. data/test/test_statement_execute.rb +35 -0
  60. metadata +190 -0
@@ -0,0 +1,35 @@
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
+ class InternalException < Exception; end
11
+ class PermissionException < Exception; end
12
+ class AbortException < Exception; end
13
+ class BusyException < Exception; end
14
+ class LockedException < Exception; end
15
+ class MemoryException < Exception; end
16
+ class ReadOnlyException < Exception; end
17
+ class InterruptException < Exception; end
18
+ class IOException < Exception; end
19
+ class CorruptException < Exception; end
20
+ class NotFoundException < Exception; end
21
+ class FullException < Exception; end
22
+ class CantOpenException < Exception; end
23
+ class ProtocolException < Exception; end
24
+ class EmptyException < Exception; end
25
+ class SchemaChangedException < Exception; end
26
+ class TooBigException < Exception; end
27
+ class ConstraintException < Exception; end
28
+ class MismatchException < Exception; end
29
+ class MisuseException < Exception; end
30
+ class UnsupportedException < Exception; end
31
+ class AuthorizationException < Exception; end
32
+ class FormatException < Exception; end
33
+ class RangeException < Exception; end
34
+ class NotADatabaseException < Exception; end
35
+ end
@@ -0,0 +1,595 @@
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, *params, &block ) # :yields: row
46
+ if params.empty?
47
+ execute( "PRAGMA #{name}", &block )
48
+ else
49
+ args = "'" + params.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
+ def application_id
105
+ get_int_pragma "application_id"
106
+ end
107
+
108
+ def application_id=( integer )
109
+ set_int_pragma "application_id", integer
110
+ end
111
+
112
+ def auto_vacuum
113
+ get_enum_pragma "auto_vacuum"
114
+ end
115
+
116
+ def auto_vacuum=( mode )
117
+ set_enum_pragma "auto_vacuum", mode, AUTO_VACUUM_MODES
118
+ end
119
+
120
+ def automatic_index
121
+ get_boolean_pragma "automatic_index"
122
+ end
123
+
124
+ def automatic_index=( mode )
125
+ set_boolean_pragma "automatic_index", mode
126
+ end
127
+
128
+ def busy_timeout
129
+ get_int_pragma "busy_timeout"
130
+ end
131
+
132
+ def busy_timeout=( milliseconds )
133
+ set_int_pragma "busy_timeout", milliseconds
134
+ end
135
+
136
+ def cache_size
137
+ get_int_pragma "cache_size"
138
+ end
139
+
140
+ def cache_size=( size )
141
+ set_int_pragma "cache_size", size
142
+ end
143
+
144
+ def cache_spill
145
+ get_boolean_pragma "cache_spill"
146
+ end
147
+
148
+ def cache_spill=( mode )
149
+ set_boolean_pragma "cache_spill", mode
150
+ end
151
+
152
+ def case_sensitive_like=( mode )
153
+ set_boolean_pragma "case_sensitive_like", mode
154
+ end
155
+
156
+ def cell_size_check
157
+ get_boolean_pragma "cell_size_check"
158
+ end
159
+
160
+ def cell_size_check=( mode )
161
+ set_boolean_pragma "cell_size_check", mode
162
+ end
163
+
164
+ def checkpoint_fullfsync
165
+ get_boolean_pragma "checkpoint_fullfsync"
166
+ end
167
+
168
+ def checkpoint_fullfsync=( mode )
169
+ set_boolean_pragma "checkpoint_fullfsync", mode
170
+ end
171
+
172
+ def collation_list( &block ) # :yields: row
173
+ get_query_pragma "collation_list", &block
174
+ end
175
+
176
+ def compile_options( &block ) # :yields: row
177
+ get_query_pragma "compile_options", &block
178
+ end
179
+
180
+ def count_changes
181
+ get_boolean_pragma "count_changes"
182
+ end
183
+
184
+ def count_changes=( mode )
185
+ set_boolean_pragma "count_changes", mode
186
+ end
187
+
188
+ def data_version
189
+ get_int_pragma "data_version"
190
+ end
191
+
192
+ def database_list( &block ) # :yields: row
193
+ get_query_pragma "database_list", &block
194
+ end
195
+
196
+ def default_cache_size
197
+ get_int_pragma "default_cache_size"
198
+ end
199
+
200
+ def default_cache_size=( size )
201
+ set_int_pragma "default_cache_size", size
202
+ end
203
+
204
+ def default_synchronous
205
+ get_enum_pragma "default_synchronous"
206
+ end
207
+
208
+ def default_synchronous=( mode )
209
+ set_enum_pragma "default_synchronous", mode, SYNCHRONOUS_MODES
210
+ end
211
+
212
+ def default_temp_store
213
+ get_enum_pragma "default_temp_store"
214
+ end
215
+
216
+ def default_temp_store=( mode )
217
+ set_enum_pragma "default_temp_store", mode, TEMP_STORE_MODES
218
+ end
219
+
220
+ def defer_foreign_keys
221
+ get_boolean_pragma "defer_foreign_keys"
222
+ end
223
+
224
+ def defer_foreign_keys=( mode )
225
+ set_boolean_pragma "defer_foreign_keys", mode
226
+ end
227
+
228
+ def encoding
229
+ get_enum_pragma "encoding"
230
+ end
231
+
232
+ def encoding=( mode )
233
+ set_enum_pragma "encoding", mode, ENCODINGS
234
+ end
235
+
236
+ def foreign_key_check( *table, &block ) # :yields: row
237
+ get_query_pragma "foreign_key_check", *table, &block
238
+ end
239
+
240
+ def foreign_key_list( table, &block ) # :yields: row
241
+ get_query_pragma "foreign_key_list", table, &block
242
+ end
243
+
244
+ def foreign_keys
245
+ get_boolean_pragma "foreign_keys"
246
+ end
247
+
248
+ def foreign_keys=( mode )
249
+ set_boolean_pragma "foreign_keys", mode
250
+ end
251
+
252
+ def freelist_count
253
+ get_int_pragma "freelist_count"
254
+ end
255
+
256
+ def full_column_names
257
+ get_boolean_pragma "full_column_names"
258
+ end
259
+
260
+ def full_column_names=( mode )
261
+ set_boolean_pragma "full_column_names", mode
262
+ end
263
+
264
+ def fullfsync
265
+ get_boolean_pragma "fullfsync"
266
+ end
267
+
268
+ def fullfsync=( mode )
269
+ set_boolean_pragma "fullfsync", mode
270
+ end
271
+
272
+ def ignore_check_constraints=( mode )
273
+ set_boolean_pragma "ignore_check_constraints", mode
274
+ end
275
+
276
+ def incremental_vacuum( pages, &block ) # :yields: row
277
+ get_query_pragma "incremental_vacuum", pages, &block
278
+ end
279
+
280
+ def index_info( index, &block ) # :yields: row
281
+ get_query_pragma "index_info", index, &block
282
+ end
283
+
284
+ def index_list( table, &block ) # :yields: row
285
+ get_query_pragma "index_list", table, &block
286
+ end
287
+
288
+ def index_xinfo( index, &block ) # :yields: row
289
+ get_query_pragma "index_xinfo", index, &block
290
+ end
291
+
292
+ def integrity_check( *num_errors, &block ) # :yields: row
293
+ get_query_pragma "integrity_check", *num_errors, &block
294
+ end
295
+
296
+ def journal_mode
297
+ get_enum_pragma "journal_mode"
298
+ end
299
+
300
+ def journal_mode=( mode )
301
+ set_enum_pragma "journal_mode", mode, JOURNAL_MODES
302
+ end
303
+
304
+ def journal_size_limit
305
+ get_int_pragma "journal_size_limit"
306
+ end
307
+
308
+ def journal_size_limit=( size )
309
+ set_int_pragma "journal_size_limit", size
310
+ end
311
+
312
+ def legacy_file_format
313
+ get_boolean_pragma "legacy_file_format"
314
+ end
315
+
316
+ def legacy_file_format=( mode )
317
+ set_boolean_pragma "legacy_file_format", mode
318
+ end
319
+
320
+ def locking_mode
321
+ get_enum_pragma "locking_mode"
322
+ end
323
+
324
+ def locking_mode=( mode )
325
+ set_enum_pragma "locking_mode", mode, LOCKING_MODES
326
+ end
327
+
328
+ def max_page_count
329
+ get_int_pragma "max_page_count"
330
+ end
331
+
332
+ def max_page_count=( size )
333
+ set_int_pragma "max_page_count", size
334
+ end
335
+
336
+ def mmap_size
337
+ get_int_pragma "mmap_size"
338
+ end
339
+
340
+ def mmap_size=( size )
341
+ set_int_pragma "mmap_size", size
342
+ end
343
+
344
+ def page_count
345
+ get_int_pragma "page_count"
346
+ end
347
+
348
+ def page_size
349
+ get_int_pragma "page_size"
350
+ end
351
+
352
+ def page_size=( size )
353
+ set_int_pragma "page_size", size
354
+ end
355
+
356
+ def parser_trace=( mode )
357
+ set_boolean_pragma "parser_trace", mode
358
+ end
359
+
360
+ def query_only
361
+ get_boolean_pragma "query_only"
362
+ end
363
+
364
+ def query_only=( mode )
365
+ set_boolean_pragma "query_only", mode
366
+ end
367
+
368
+ def quick_check( *num_errors, &block ) # :yields: row
369
+ get_query_pragma "quick_check", *num_errors, &block
370
+ end
371
+
372
+ def read_uncommitted
373
+ get_boolean_pragma "read_uncommitted"
374
+ end
375
+
376
+ def read_uncommitted=( mode )
377
+ set_boolean_pragma "read_uncommitted", mode
378
+ end
379
+
380
+ def recursive_triggers
381
+ get_boolean_pragma "recursive_triggers"
382
+ end
383
+
384
+ def recursive_triggers=( mode )
385
+ set_boolean_pragma "recursive_triggers", mode
386
+ end
387
+
388
+ def reverse_unordered_selects
389
+ get_boolean_pragma "reverse_unordered_selects"
390
+ end
391
+
392
+ def reverse_unordered_selects=( mode )
393
+ set_boolean_pragma "reverse_unordered_selects", mode
394
+ end
395
+
396
+ def schema_cookie
397
+ get_int_pragma "schema_cookie"
398
+ end
399
+
400
+ def schema_cookie=( cookie )
401
+ set_int_pragma "schema_cookie", cookie
402
+ end
403
+
404
+ def schema_version
405
+ get_int_pragma "schema_version"
406
+ end
407
+
408
+ def schema_version=( version )
409
+ set_int_pragma "schema_version", version
410
+ end
411
+
412
+ def secure_delete
413
+ get_boolean_pragma "secure_delete"
414
+ end
415
+
416
+ def secure_delete=( mode )
417
+ set_boolean_pragma "secure_delete", mode
418
+ end
419
+
420
+ def short_column_names
421
+ get_boolean_pragma "short_column_names"
422
+ end
423
+
424
+ def short_column_names=( mode )
425
+ set_boolean_pragma "short_column_names", mode
426
+ end
427
+
428
+ def shrink_memory
429
+ execute( "PRAGMA shrink_memory" )
430
+ end
431
+
432
+ def soft_heap_limit
433
+ get_int_pragma "soft_heap_limit"
434
+ end
435
+
436
+ def soft_heap_limit=( mode )
437
+ set_int_pragma "soft_heap_limit", mode
438
+ end
439
+
440
+ def stats( &block ) # :yields: row
441
+ get_query_pragma "stats", &block
442
+ end
443
+
444
+ def synchronous
445
+ get_enum_pragma "synchronous"
446
+ end
447
+
448
+ def synchronous=( mode )
449
+ set_enum_pragma "synchronous", mode, SYNCHRONOUS_MODES
450
+ end
451
+
452
+ def temp_store
453
+ get_enum_pragma "temp_store"
454
+ end
455
+
456
+ def temp_store=( mode )
457
+ set_enum_pragma "temp_store", mode, TEMP_STORE_MODES
458
+ end
459
+
460
+ def threads
461
+ get_int_pragma "threads"
462
+ end
463
+
464
+ def threads=( count )
465
+ set_int_pragma "threads", count
466
+ end
467
+
468
+ def user_cookie
469
+ get_int_pragma "user_cookie"
470
+ end
471
+
472
+ def user_cookie=( cookie )
473
+ set_int_pragma "user_cookie", cookie
474
+ end
475
+
476
+ def user_version
477
+ get_int_pragma "user_version"
478
+ end
479
+
480
+ def user_version=( version )
481
+ set_int_pragma "user_version", version
482
+ end
483
+
484
+ def vdbe_addoptrace=( mode )
485
+ set_boolean_pragma "vdbe_addoptrace", mode
486
+ end
487
+
488
+ def vdbe_debug=( mode )
489
+ set_boolean_pragma "vdbe_debug", mode
490
+ end
491
+
492
+ def vdbe_listing=( mode )
493
+ set_boolean_pragma "vdbe_listing", mode
494
+ end
495
+
496
+ def vdbe_trace
497
+ get_boolean_pragma "vdbe_trace"
498
+ end
499
+
500
+ def vdbe_trace=( mode )
501
+ set_boolean_pragma "vdbe_trace", mode
502
+ end
503
+
504
+ def wal_autocheckpoint
505
+ get_int_pragma "wal_autocheckpoint"
506
+ end
507
+
508
+ def wal_autocheckpoint=( mode )
509
+ set_int_pragma "wal_autocheckpoint", mode
510
+ end
511
+
512
+ def wal_checkpoint
513
+ get_enum_pragma "wal_checkpoint"
514
+ end
515
+
516
+ def wal_checkpoint=( mode )
517
+ set_enum_pragma "wal_checkpoint", mode, WAL_CHECKPOINTS
518
+ end
519
+
520
+ def writable_schema=( mode )
521
+ set_boolean_pragma "writable_schema", mode
522
+ end
523
+
524
+ ###
525
+ # Returns information about +table+. Yields each row of table information
526
+ # if a block is provided.
527
+ def table_info table
528
+ stmt = prepare "PRAGMA table_info(#{table})"
529
+ columns = stmt.columns
530
+
531
+ needs_tweak_default =
532
+ version_compare(SQLite3.libversion.to_s, "3.3.7") > 0
533
+
534
+ result = [] unless block_given?
535
+ stmt.each do |row|
536
+ new_row = Hash[columns.zip(row)]
537
+
538
+ # FIXME: This should be removed but is required for older versions
539
+ # of rails
540
+ if(Object.const_defined?(:ActiveRecord))
541
+ new_row['notnull'] = new_row['notnull'].to_s
542
+ end
543
+
544
+ tweak_default(new_row) if needs_tweak_default
545
+
546
+ # Ensure the type value is downcased. On Mac and Windows
547
+ # platforms this value is now being returned as all upper
548
+ # case.
549
+ if new_row['type']
550
+ new_row['type'] = new_row['type'].downcase
551
+ end
552
+
553
+ if block_given?
554
+ yield new_row
555
+ else
556
+ result << new_row
557
+ end
558
+ end
559
+ stmt.close
560
+
561
+ result
562
+ end
563
+
564
+ private
565
+
566
+ # Compares two version strings
567
+ def version_compare(v1, v2)
568
+ v1 = v1.split(".").map { |i| i.to_i }
569
+ v2 = v2.split(".").map { |i| i.to_i }
570
+ parts = [v1.length, v2.length].max
571
+ v1.push 0 while v1.length < parts
572
+ v2.push 0 while v2.length < parts
573
+ v1.zip(v2).each do |a,b|
574
+ return -1 if a < b
575
+ return 1 if a > b
576
+ end
577
+ return 0
578
+ end
579
+
580
+ # Since SQLite 3.3.8, the table_info pragma has returned the default
581
+ # value of the row as a quoted SQL value. This method essentially
582
+ # unquotes those values.
583
+ def tweak_default(hash)
584
+ case hash["dflt_value"]
585
+ when /^null$/i
586
+ hash["dflt_value"] = nil
587
+ when /^'(.*)'$/m
588
+ hash["dflt_value"] = $1.gsub(/''/, "'")
589
+ when /^"(.*)"$/m
590
+ hash["dflt_value"] = $1.gsub(/""/, '"')
591
+ end
592
+ end
593
+ end
594
+
595
+ end