sqlite3-full 1.3.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/.gemtest +0 -0
  3. data/API_CHANGES.rdoc +50 -0
  4. data/CHANGELOG.rdoc +278 -0
  5. data/ChangeLog.cvs +88 -0
  6. data/Gemfile +15 -0
  7. data/LICENSE +34 -0
  8. data/Manifest.txt +52 -0
  9. data/README.rdoc +90 -0
  10. data/Rakefile +10 -0
  11. data/ext/sqlite3/backup.c +168 -0
  12. data/ext/sqlite3/backup.h +15 -0
  13. data/ext/sqlite3/database.c +825 -0
  14. data/ext/sqlite3/database.h +15 -0
  15. data/ext/sqlite3/exception.c +94 -0
  16. data/ext/sqlite3/exception.h +8 -0
  17. data/ext/sqlite3/extconf.rb +86 -0
  18. data/ext/sqlite3/sqlite3.c +97 -0
  19. data/ext/sqlite3/sqlite3_ruby.h +52 -0
  20. data/ext/sqlite3/sqlite3amalgamation.c +153367 -0
  21. data/ext/sqlite3/statement.c +447 -0
  22. data/ext/sqlite3/statement.h +16 -0
  23. data/faq/faq.rb +145 -0
  24. data/faq/faq.yml +426 -0
  25. data/lib/sqlite3/constants.rb +49 -0
  26. data/lib/sqlite3/database.rb +590 -0
  27. data/lib/sqlite3/errors.rb +44 -0
  28. data/lib/sqlite3/pragmas.rb +280 -0
  29. data/lib/sqlite3/resultset.rb +195 -0
  30. data/lib/sqlite3/statement.rb +144 -0
  31. data/lib/sqlite3/translator.rb +118 -0
  32. data/lib/sqlite3/value.rb +57 -0
  33. data/lib/sqlite3/version.rb +25 -0
  34. data/lib/sqlite3.rb +10 -0
  35. data/setup.rb +1333 -0
  36. data/tasks/faq.rake +9 -0
  37. data/tasks/gem.rake +38 -0
  38. data/tasks/native.rake +52 -0
  39. data/tasks/vendor_sqlite3.rake +91 -0
  40. data/test/helper.rb +18 -0
  41. data/test/test_backup.rb +33 -0
  42. data/test/test_collation.rb +82 -0
  43. data/test/test_database.rb +367 -0
  44. data/test/test_database_readonly.rb +29 -0
  45. data/test/test_deprecated.rb +44 -0
  46. data/test/test_encoding.rb +153 -0
  47. data/test/test_integration.rb +572 -0
  48. data/test/test_integration_open_close.rb +30 -0
  49. data/test/test_integration_pending.rb +115 -0
  50. data/test/test_integration_resultset.rb +159 -0
  51. data/test/test_integration_statement.rb +194 -0
  52. data/test/test_result_set.rb +37 -0
  53. data/test/test_sqlite3.rb +9 -0
  54. data/test/test_statement.rb +260 -0
  55. data/test/test_statement_execute.rb +35 -0
  56. metadata +205 -0
@@ -0,0 +1,280 @@
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
+ private :get_boolean_pragma
17
+
18
+ # Sets the given pragma to the given boolean value. The value itself
19
+ # may be +true+ or +false+, or any other commonly used string or
20
+ # integer that represents truth.
21
+ def set_boolean_pragma( name, mode )
22
+ case mode
23
+ when String
24
+ case mode.downcase
25
+ when "on", "yes", "true", "y", "t"; mode = "'ON'"
26
+ when "off", "no", "false", "n", "f"; mode = "'OFF'"
27
+ else
28
+ raise Exception,
29
+ "unrecognized pragma parameter #{mode.inspect}"
30
+ end
31
+ when true, 1
32
+ mode = "ON"
33
+ when false, 0, nil
34
+ mode = "OFF"
35
+ else
36
+ raise Exception,
37
+ "unrecognized pragma parameter #{mode.inspect}"
38
+ end
39
+
40
+ execute( "PRAGMA #{name}=#{mode}" )
41
+ end
42
+ private :set_boolean_pragma
43
+
44
+ # Requests the given pragma (and parameters), and if the block is given,
45
+ # each row of the result set will be yielded to it. Otherwise, the results
46
+ # are returned as an array.
47
+ def get_query_pragma( name, *parms, &block ) # :yields: row
48
+ if parms.empty?
49
+ execute( "PRAGMA #{name}", &block )
50
+ else
51
+ args = "'" + parms.join("','") + "'"
52
+ execute( "PRAGMA #{name}( #{args} )", &block )
53
+ end
54
+ end
55
+ private :get_query_pragma
56
+
57
+ # Return the value of the given pragma.
58
+ def get_enum_pragma( name )
59
+ get_first_value( "PRAGMA #{name}" )
60
+ end
61
+ private :get_enum_pragma
62
+
63
+ # Set the value of the given pragma to +mode+. The +mode+ parameter must
64
+ # conform to one of the values in the given +enum+ array. Each entry in
65
+ # the array is another array comprised of elements in the enumeration that
66
+ # have duplicate values. See #synchronous, #default_synchronous,
67
+ # #temp_store, and #default_temp_store for usage examples.
68
+ def set_enum_pragma( name, mode, enums )
69
+ match = enums.find { |p| p.find { |i| i.to_s.downcase == mode.to_s.downcase } }
70
+ raise Exception,
71
+ "unrecognized #{name} #{mode.inspect}" unless match
72
+ execute( "PRAGMA #{name}='#{match.first.upcase}'" )
73
+ end
74
+ private :set_enum_pragma
75
+
76
+ # Returns the value of the given pragma as an integer.
77
+ def get_int_pragma( name )
78
+ get_first_value( "PRAGMA #{name}" ).to_i
79
+ end
80
+ private :get_int_pragma
81
+
82
+ # Set the value of the given pragma to the integer value of the +value+
83
+ # parameter.
84
+ def set_int_pragma( name, value )
85
+ execute( "PRAGMA #{name}=#{value.to_i}" )
86
+ end
87
+ private :set_int_pragma
88
+
89
+ # The enumeration of valid synchronous modes.
90
+ SYNCHRONOUS_MODES = [ [ 'full', 2 ], [ 'normal', 1 ], [ 'off', 0 ] ]
91
+
92
+ # The enumeration of valid temp store modes.
93
+ TEMP_STORE_MODES = [ [ 'default', 0 ], [ 'file', 1 ], [ 'memory', 2 ] ]
94
+
95
+ # Does an integrity check on the database. If the check fails, a
96
+ # SQLite3::Exception will be raised. Otherwise it
97
+ # returns silently.
98
+ def integrity_check
99
+ execute( "PRAGMA integrity_check" ) do |row|
100
+ raise Exception, row[0] if row[0] != "ok"
101
+ end
102
+ end
103
+
104
+ def auto_vacuum
105
+ get_boolean_pragma "auto_vacuum"
106
+ end
107
+
108
+ def auto_vacuum=( mode )
109
+ set_boolean_pragma "auto_vacuum", mode
110
+ end
111
+
112
+ def schema_cookie
113
+ get_int_pragma "schema_cookie"
114
+ end
115
+
116
+ def schema_cookie=( cookie )
117
+ set_int_pragma "schema_cookie", cookie
118
+ end
119
+
120
+ def user_cookie
121
+ get_int_pragma "user_cookie"
122
+ end
123
+
124
+ def user_cookie=( cookie )
125
+ set_int_pragma "user_cookie", cookie
126
+ end
127
+
128
+ def cache_size
129
+ get_int_pragma "cache_size"
130
+ end
131
+
132
+ def cache_size=( size )
133
+ set_int_pragma "cache_size", size
134
+ end
135
+
136
+ def default_cache_size
137
+ get_int_pragma "default_cache_size"
138
+ end
139
+
140
+ def default_cache_size=( size )
141
+ set_int_pragma "default_cache_size", size
142
+ end
143
+
144
+ def default_synchronous
145
+ get_enum_pragma "default_synchronous"
146
+ end
147
+
148
+ def default_synchronous=( mode )
149
+ set_enum_pragma "default_synchronous", mode, SYNCHRONOUS_MODES
150
+ end
151
+
152
+ def synchronous
153
+ get_enum_pragma "synchronous"
154
+ end
155
+
156
+ def synchronous=( mode )
157
+ set_enum_pragma "synchronous", mode, SYNCHRONOUS_MODES
158
+ end
159
+
160
+ def default_temp_store
161
+ get_enum_pragma "default_temp_store"
162
+ end
163
+
164
+ def default_temp_store=( mode )
165
+ set_enum_pragma "default_temp_store", mode, TEMP_STORE_MODES
166
+ end
167
+
168
+ def temp_store
169
+ get_enum_pragma "temp_store"
170
+ end
171
+
172
+ def temp_store=( mode )
173
+ set_enum_pragma "temp_store", mode, TEMP_STORE_MODES
174
+ end
175
+
176
+ def full_column_names
177
+ get_boolean_pragma "full_column_names"
178
+ end
179
+
180
+ def full_column_names=( mode )
181
+ set_boolean_pragma "full_column_names", mode
182
+ end
183
+
184
+ def parser_trace
185
+ get_boolean_pragma "parser_trace"
186
+ end
187
+
188
+ def parser_trace=( mode )
189
+ set_boolean_pragma "parser_trace", mode
190
+ end
191
+
192
+ def vdbe_trace
193
+ get_boolean_pragma "vdbe_trace"
194
+ end
195
+
196
+ def vdbe_trace=( mode )
197
+ set_boolean_pragma "vdbe_trace", mode
198
+ end
199
+
200
+ def database_list( &block ) # :yields: row
201
+ get_query_pragma "database_list", &block
202
+ end
203
+
204
+ def foreign_key_list( table, &block ) # :yields: row
205
+ get_query_pragma "foreign_key_list", table, &block
206
+ end
207
+
208
+ def index_info( index, &block ) # :yields: row
209
+ get_query_pragma "index_info", index, &block
210
+ end
211
+
212
+ def index_list( table, &block ) # :yields: row
213
+ get_query_pragma "index_list", table, &block
214
+ end
215
+
216
+ ###
217
+ # Returns information about +table+. Yields each row of table information
218
+ # if a block is provided.
219
+ def table_info table
220
+ stmt = prepare "PRAGMA table_info(#{table})"
221
+ columns = stmt.columns
222
+
223
+ needs_tweak_default =
224
+ version_compare(SQLite3.libversion.to_s, "3.3.7") > 0
225
+
226
+ result = [] unless block_given?
227
+ stmt.each do |row|
228
+ new_row = Hash[columns.zip(row)]
229
+
230
+ # FIXME: This should be removed but is required for older versions
231
+ # of rails
232
+ if(Object.const_defined?(:ActiveRecord))
233
+ new_row['notnull'] = new_row['notnull'].to_s
234
+ end
235
+
236
+ tweak_default(new_row) if needs_tweak_default
237
+
238
+ if block_given?
239
+ yield new_row
240
+ else
241
+ result << new_row
242
+ end
243
+ end
244
+ stmt.close
245
+
246
+ result
247
+ end
248
+
249
+ private
250
+
251
+ # Compares two version strings
252
+ def version_compare(v1, v2)
253
+ v1 = v1.split(".").map { |i| i.to_i }
254
+ v2 = v2.split(".").map { |i| i.to_i }
255
+ parts = [v1.length, v2.length].max
256
+ v1.push 0 while v1.length < parts
257
+ v2.push 0 while v2.length < parts
258
+ v1.zip(v2).each do |a,b|
259
+ return -1 if a < b
260
+ return 1 if a > b
261
+ end
262
+ return 0
263
+ end
264
+
265
+ # Since SQLite 3.3.8, the table_info pragma has returned the default
266
+ # value of the row as a quoted SQL value. This method essentially
267
+ # unquotes those values.
268
+ def tweak_default(hash)
269
+ case hash["dflt_value"]
270
+ when /^null$/i
271
+ hash["dflt_value"] = nil
272
+ when /^'(.*)'$/m
273
+ hash["dflt_value"] = $1.gsub(/''/, "'")
274
+ when /^"(.*)"$/m
275
+ hash["dflt_value"] = $1.gsub(/""/, '"')
276
+ end
277
+ end
278
+ end
279
+
280
+ end
@@ -0,0 +1,195 @@
1
+ require 'sqlite3/constants'
2
+ require 'sqlite3/errors'
3
+
4
+ module SQLite3
5
+
6
+ # The ResultSet object encapsulates the enumerability of a query's output.
7
+ # It is a simple cursor over the data that the query returns. It will
8
+ # very rarely (if ever) be instantiated directly. Instead, clients should
9
+ # obtain a ResultSet instance via Statement#execute.
10
+ class ResultSet
11
+ include Enumerable
12
+
13
+ class ArrayWithTypes < Array # :nodoc:
14
+ attr_accessor :types
15
+ end
16
+
17
+ class ArrayWithTypesAndFields < Array # :nodoc:
18
+ attr_writer :types
19
+ attr_writer :fields
20
+
21
+ def types
22
+ warn(<<-eowarn) if $VERBOSE
23
+ #{caller[0]} is calling #{self.class}#types. This method will be removed in
24
+ sqlite3 version 2.0.0, please call the `types` method on the SQLite3::ResultSet
25
+ object that created this object
26
+ eowarn
27
+ @types
28
+ end
29
+
30
+ def fields
31
+ warn(<<-eowarn) if $VERBOSE
32
+ #{caller[0]} is calling #{self.class}#fields. This method will be removed in
33
+ sqlite3 version 2.0.0, please call the `columns` method on the SQLite3::ResultSet
34
+ object that created this object
35
+ eowarn
36
+ @fields
37
+ end
38
+ end
39
+
40
+ # The class of which we return an object in case we want a Hash as
41
+ # result.
42
+ class HashWithTypesAndFields < Hash # :nodoc:
43
+ attr_writer :types
44
+ attr_writer :fields
45
+
46
+ def types
47
+ warn(<<-eowarn) if $VERBOSE
48
+ #{caller[0]} is calling #{self.class}#types. This method will be removed in
49
+ sqlite3 version 2.0.0, please call the `types` method on the SQLite3::ResultSet
50
+ object that created this object
51
+ eowarn
52
+ @types
53
+ end
54
+
55
+ def fields
56
+ warn(<<-eowarn) if $VERBOSE
57
+ #{caller[0]} is calling #{self.class}#fields. This method will be removed in
58
+ sqlite3 version 2.0.0, please call the `columns` method on the SQLite3::ResultSet
59
+ object that created this object
60
+ eowarn
61
+ @fields
62
+ end
63
+
64
+ def [] key
65
+ key = fields[key] if key.is_a? Numeric
66
+ super key
67
+ end
68
+ end
69
+
70
+ # Create a new ResultSet attached to the given database, using the
71
+ # given sql text.
72
+ def initialize db, stmt
73
+ @db = db
74
+ @stmt = stmt
75
+ end
76
+
77
+ # Reset the cursor, so that a result set which has reached end-of-file
78
+ # can be rewound and reiterated.
79
+ def reset( *bind_params )
80
+ @stmt.reset!
81
+ @stmt.bind_params( *bind_params )
82
+ @eof = false
83
+ end
84
+
85
+ # Query whether the cursor has reached the end of the result set or not.
86
+ def eof?
87
+ @stmt.done?
88
+ end
89
+
90
+ # Obtain the next row from the cursor. If there are no more rows to be
91
+ # had, this will return +nil+. If type translation is active on the
92
+ # corresponding database, the values in the row will be translated
93
+ # according to their types.
94
+ #
95
+ # The returned value will be an array, unless Database#results_as_hash has
96
+ # been set to +true+, in which case the returned value will be a hash.
97
+ #
98
+ # For arrays, the column names are accessible via the +fields+ property,
99
+ # and the column types are accessible via the +types+ property.
100
+ #
101
+ # For hashes, the column names are the keys of the hash, and the column
102
+ # types are accessible via the +types+ property.
103
+ def next
104
+ if @db.results_as_hash
105
+ return next_hash
106
+ end
107
+
108
+ row = @stmt.step
109
+ return nil if @stmt.done?
110
+
111
+ if @db.type_translation
112
+ row = @stmt.types.zip(row).map do |type, value|
113
+ @db.translator.translate( type, value )
114
+ end
115
+ end
116
+
117
+ if row.respond_to?(:fields)
118
+ # FIXME: this can only happen if the translator returns something
119
+ # that responds to `fields`. Since we're removing the translator
120
+ # in 2.0, we can remove this branch in 2.0.
121
+ row = ArrayWithTypes.new(row)
122
+ else
123
+ # FIXME: the `fields` and `types` methods are deprecated on this
124
+ # object for version 2.0, so we can safely remove this branch
125
+ # as well.
126
+ row = ArrayWithTypesAndFields.new(row)
127
+ end
128
+
129
+ row.fields = @stmt.columns
130
+ row.types = @stmt.types
131
+ row
132
+ end
133
+
134
+ # Required by the Enumerable mixin. Provides an internal iterator over the
135
+ # rows of the result set.
136
+ def each
137
+ while node = self.next
138
+ yield node
139
+ end
140
+ end
141
+
142
+ # Provides an internal iterator over the rows of the result set where
143
+ # each row is yielded as a hash.
144
+ def each_hash
145
+ while node = next_hash
146
+ yield node
147
+ end
148
+ end
149
+
150
+ # Closes the statement that spawned this result set.
151
+ # <em>Use with caution!</em> Closing a result set will automatically
152
+ # close any other result sets that were spawned from the same statement.
153
+ def close
154
+ @stmt.close
155
+ end
156
+
157
+ # Queries whether the underlying statement has been closed or not.
158
+ def closed?
159
+ @stmt.closed?
160
+ end
161
+
162
+ # Returns the types of the columns returned by this result set.
163
+ def types
164
+ @stmt.types
165
+ end
166
+
167
+ # Returns the names of the columns returned by this result set.
168
+ def columns
169
+ @stmt.columns
170
+ end
171
+
172
+ # Return the next row as a hash
173
+ def next_hash
174
+ row = @stmt.step
175
+ return nil if @stmt.done?
176
+
177
+ # FIXME: type translation is deprecated, so this can be removed
178
+ # in 2.0
179
+ if @db.type_translation
180
+ row = @stmt.types.zip(row).map do |type, value|
181
+ @db.translator.translate( type, value )
182
+ end
183
+ end
184
+
185
+ # FIXME: this can be switched to a regular hash in 2.0
186
+ row = HashWithTypesAndFields[*@stmt.columns.zip(row).flatten]
187
+
188
+ # FIXME: these methods are deprecated for version 2.0, so we can remove
189
+ # this code in 2.0
190
+ row.fields = @stmt.columns
191
+ row.types = @stmt.types
192
+ row
193
+ end
194
+ end
195
+ end
@@ -0,0 +1,144 @@
1
+ require 'sqlite3/errors'
2
+ require 'sqlite3/resultset'
3
+
4
+ class String
5
+ def to_blob
6
+ SQLite3::Blob.new( self )
7
+ end
8
+ end
9
+
10
+ module SQLite3
11
+ # A statement represents a prepared-but-unexecuted SQL query. It will rarely
12
+ # (if ever) be instantiated directly by a client, and is most often obtained
13
+ # via the Database#prepare method.
14
+ class Statement
15
+ include Enumerable
16
+
17
+ # This is any text that followed the first valid SQL statement in the text
18
+ # with which the statement was initialized. If there was no trailing text,
19
+ # this will be the empty string.
20
+ attr_reader :remainder
21
+
22
+ # Binds the given variables to the corresponding placeholders in the SQL
23
+ # text.
24
+ #
25
+ # See Database#execute for a description of the valid placeholder
26
+ # syntaxes.
27
+ #
28
+ # Example:
29
+ #
30
+ # stmt = db.prepare( "select * from table where a=? and b=?" )
31
+ # stmt.bind_params( 15, "hello" )
32
+ #
33
+ # See also #execute, #bind_param, Statement#bind_param, and
34
+ # Statement#bind_params.
35
+ def bind_params( *bind_vars )
36
+ index = 1
37
+ bind_vars.flatten.each do |var|
38
+ if Hash === var
39
+ var.each { |key, val| bind_param key, val }
40
+ else
41
+ bind_param index, var
42
+ index += 1
43
+ end
44
+ end
45
+ end
46
+
47
+ # Execute the statement. This creates a new ResultSet object for the
48
+ # statement's virtual machine. If a block was given, the new ResultSet will
49
+ # be yielded to it; otherwise, the ResultSet will be returned.
50
+ #
51
+ # Any parameters will be bound to the statement using #bind_params.
52
+ #
53
+ # Example:
54
+ #
55
+ # stmt = db.prepare( "select * from table" )
56
+ # stmt.execute do |result|
57
+ # ...
58
+ # end
59
+ #
60
+ # See also #bind_params, #execute!.
61
+ def execute( *bind_vars )
62
+ reset! if active? || done?
63
+
64
+ bind_params(*bind_vars) unless bind_vars.empty?
65
+ @results = ResultSet.new(@connection, self)
66
+
67
+ step if 0 == column_count
68
+
69
+ yield @results if block_given?
70
+ @results
71
+ end
72
+
73
+ # Execute the statement. If no block was given, this returns an array of
74
+ # rows returned by executing the statement. Otherwise, each row will be
75
+ # yielded to the block.
76
+ #
77
+ # Any parameters will be bound to the statement using #bind_params.
78
+ #
79
+ # Example:
80
+ #
81
+ # stmt = db.prepare( "select * from table" )
82
+ # stmt.execute! do |row|
83
+ # ...
84
+ # end
85
+ #
86
+ # See also #bind_params, #execute.
87
+ def execute!( *bind_vars, &block )
88
+ execute(*bind_vars)
89
+ block_given? ? each(&block) : to_a
90
+ end
91
+
92
+ # Returns true if the statement is currently active, meaning it has an
93
+ # open result set.
94
+ def active?
95
+ !done?
96
+ end
97
+
98
+ # Return an array of the column names for this statement. Note that this
99
+ # may execute the statement in order to obtain the metadata; this makes it
100
+ # a (potentially) expensive operation.
101
+ def columns
102
+ get_metadata unless @columns
103
+ return @columns
104
+ end
105
+
106
+ def each
107
+ loop do
108
+ val = step
109
+ break self if done?
110
+ yield val
111
+ end
112
+ end
113
+
114
+ # Return an array of the data types for each column in this statement. Note
115
+ # that this may execute the statement in order to obtain the metadata; this
116
+ # makes it a (potentially) expensive operation.
117
+ def types
118
+ must_be_open!
119
+ get_metadata unless @types
120
+ @types
121
+ end
122
+
123
+ # Performs a sanity check to ensure that the statement is not
124
+ # closed. If it is, an exception is raised.
125
+ def must_be_open! # :nodoc:
126
+ if closed?
127
+ raise SQLite3::Exception, "cannot use a closed statement"
128
+ end
129
+ end
130
+
131
+ private
132
+ # A convenience method for obtaining the metadata about the query. Note
133
+ # that this will actually execute the SQL, which means it can be a
134
+ # (potentially) expensive operation.
135
+ def get_metadata
136
+ @columns = Array.new(column_count) do |column|
137
+ column_name column
138
+ end
139
+ @types = Array.new(column_count) do |column|
140
+ column_decltype column
141
+ end
142
+ end
143
+ end
144
+ end