sqlite3-ruby 0.9.0-mswin32

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sqlite3-ruby might be problematic. Click here for more details.

@@ -0,0 +1,218 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are met:
8
+ #
9
+ # * Redistributions of source code must retain the above copyright notice,
10
+ # this list of conditions and the following disclaimer.
11
+ #
12
+ # * Redistributions in binary form must reproduce the above copyright
13
+ # notice, this list of conditions and the following disclaimer in the
14
+ # documentation and/or other materials provided with the distribution.
15
+ #
16
+ # * The names of its contributors may not be used to endorse or promote
17
+ # products derived from this software without specific prior written
18
+ # permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
24
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ # =============================================================================
31
+ #++
32
+
33
+ require 'sqlite3_api'
34
+
35
+ module SQLite3 ; module Driver ; module Native
36
+
37
+ class Driver
38
+
39
+ def initialize
40
+ @callback_data = Hash.new
41
+ end
42
+
43
+ def complete?( sql, utf16=false )
44
+ API.send( utf16 ? :sqlite3_complete16 : :sqlite3_complete, sql ) != 0
45
+ end
46
+
47
+ def busy_handler( db, data=nil, &block )
48
+ if block
49
+ cb = API::CallbackData.new
50
+ cb.proc = block
51
+ cb.data = data
52
+ end
53
+
54
+ API.sqlite3_busy_handler( db,
55
+ block ? API::Sqlite3_ruby_busy_handler : nil, cb )
56
+ end
57
+
58
+ def set_authorizer( db, data=nil, &block )
59
+ if block
60
+ cb = API::CallbackData.new
61
+ cb.proc = block
62
+ cb.data = data
63
+ end
64
+
65
+ API.sqlite3_set_authorizer( db,
66
+ block ? API::Sqlite3_ruby_authorizer : nil, cb )
67
+ end
68
+
69
+ def trace( db, data=nil, &block )
70
+ if block
71
+ cb = API::CallbackData.new
72
+ cb.proc = block
73
+ cb.data = data
74
+ end
75
+
76
+ API.sqlite3_trace( db,
77
+ block ? API::Sqlite3_ruby_trace : nil, cb )
78
+ end
79
+
80
+ def open( filename, utf16=false )
81
+ API.send( utf16 ? :sqlite3_open16 : :sqlite3_open, filename )
82
+ end
83
+
84
+ def errmsg( db, utf16=false )
85
+ API.send( utf16 ? :sqlite3_errmsg16 : :sqlite3_errmsg, db )
86
+ end
87
+
88
+ def prepare( db, sql, utf16=false )
89
+ API.send( ( utf16 ? :sqlite3_prepare16 : :sqlite3_prepare ),
90
+ db, sql )
91
+ end
92
+
93
+ def bind_text( stmt, index, value, utf16=false )
94
+ API.send( ( utf16 ? :sqlite3_bind_text16 : :sqlite3_bind_text ),
95
+ stmt, index, value )
96
+ end
97
+
98
+ def column_name( stmt, index, utf16=false )
99
+ API.send( ( utf16 ? :sqlite3_column_name16 : :sqlite3_column_name ),
100
+ stmt, index )
101
+ end
102
+
103
+ def column_decltype( stmt, index, utf16=false )
104
+ API.send(
105
+ ( utf16 ? :sqlite3_column_decltype16 : :sqlite3_column_decltype ),
106
+ stmt, index )
107
+ end
108
+
109
+ def column_text( stmt, index, utf16=false )
110
+ API.send( ( utf16 ? :sqlite3_column_text16 : :sqlite3_column_text ),
111
+ stmt, index )
112
+ end
113
+
114
+ def create_function( db, name, args, text, cookie, func, step, final )
115
+ if func || ( step && final )
116
+ cb = API::CallbackData.new
117
+ cb.proc = cb.proc2 = nil
118
+ cb.data = cookie
119
+ @callback_data[ name ] = cb
120
+ else
121
+ @callback_data.delete( name )
122
+ end
123
+
124
+ if func
125
+ cb.proc = func
126
+
127
+ func = API::Sqlite3_ruby_function_step
128
+ step = final = nil
129
+ elsif step && final
130
+ cb.proc = step
131
+ cb.proc2 = final
132
+
133
+ func = nil
134
+ step = API::Sqlite3_ruby_function_step
135
+ final = API::Sqlite3_ruby_function_final
136
+ end
137
+
138
+ API.sqlite3_create_function( db, name, args, text, cb, func, step, final )
139
+ end
140
+
141
+ def value_text( value, utf16=false )
142
+ method = case utf16
143
+ when nil, false then :sqlite3_value_text
144
+ when :le then :sqlite3_value_text16le
145
+ when :be then :sqlite3_value_text16be
146
+ else :sqlite3_value_text16
147
+ end
148
+
149
+ API.send( method, value )
150
+ end
151
+
152
+ def result_text( context, result, utf16=false )
153
+ method = case utf16
154
+ when nil, false then :sqlite3_result_text
155
+ when :le then :sqlite3_result_text16le
156
+ when :be then :sqlite3_result_text16be
157
+ else :sqlite3_result_text16
158
+ end
159
+
160
+ API.send( method, context, result.to_s )
161
+ end
162
+
163
+ def result_error( context, value, utf16=false )
164
+ API.send( ( utf16 ? :sqlite3_result_error16 : :sqlite3_result_error ),
165
+ context, value )
166
+ end
167
+
168
+ def self.api_delegate( name )
169
+ define_method( name ) { |*args| API.send( "sqlite3_#{name}", *args ) }
170
+ end
171
+
172
+ api_delegate :libversion
173
+ api_delegate :close
174
+ api_delegate :last_insert_rowid
175
+ api_delegate :changes
176
+ api_delegate :total_changes
177
+ api_delegate :interrupt
178
+ api_delegate :busy_timeout
179
+ api_delegate :errcode
180
+ api_delegate :bind_blob
181
+ api_delegate :bind_double
182
+ api_delegate :bind_int
183
+ api_delegate :bind_int64
184
+ api_delegate :bind_null
185
+ api_delegate :bind_parameter_count
186
+ api_delegate :bind_parameter_name
187
+ api_delegate :bind_parameter_index
188
+ api_delegate :column_count
189
+ api_delegate :step
190
+ api_delegate :data_count
191
+ api_delegate :column_blob
192
+ api_delegate :column_bytes
193
+ api_delegate :column_bytes16
194
+ api_delegate :column_double
195
+ api_delegate :column_int
196
+ api_delegate :column_int64
197
+ api_delegate :column_type
198
+ api_delegate :finalize
199
+ api_delegate :reset
200
+ api_delegate :aggregate_count
201
+ api_delegate :value_blob
202
+ api_delegate :value_bytes
203
+ api_delegate :value_bytes16
204
+ api_delegate :value_double
205
+ api_delegate :value_int
206
+ api_delegate :value_int64
207
+ api_delegate :value_type
208
+ api_delegate :result_blob
209
+ api_delegate :result_double
210
+ api_delegate :result_int
211
+ api_delegate :result_int64
212
+ api_delegate :result_null
213
+ api_delegate :result_value
214
+ api_delegate :aggregate_context
215
+
216
+ end
217
+
218
+ end ; end ; end
@@ -0,0 +1,84 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are met:
8
+ #
9
+ # * Redistributions of source code must retain the above copyright notice,
10
+ # this list of conditions and the following disclaimer.
11
+ #
12
+ # * Redistributions in binary form must reproduce the above copyright
13
+ # notice, this list of conditions and the following disclaimer in the
14
+ # documentation and/or other materials provided with the distribution.
15
+ #
16
+ # * The names of its contributors may not be used to endorse or promote
17
+ # products derived from this software without specific prior written
18
+ # permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
24
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ # =============================================================================
31
+ #++
32
+
33
+ require 'sqlite3/constants'
34
+
35
+ module SQLite3
36
+
37
+ class Exception < ::Exception; end
38
+
39
+ class SQLException < Exception; end
40
+ class InternalException < Exception; end
41
+ class PermissionException < Exception; end
42
+ class AbortException < Exception; end
43
+ class BusyException < Exception; end
44
+ class LockedException < Exception; end
45
+ class MemoryException < Exception; end
46
+ class ReadOnlyException < Exception; end
47
+ class InterruptException < Exception; end
48
+ class IOException < Exception; end
49
+ class CorruptException < Exception; end
50
+ class NotFoundException < Exception; end
51
+ class FullException < Exception; end
52
+ class CantOpenException < Exception; end
53
+ class ProtocolException < Exception; end
54
+ class EmptyException < Exception; end
55
+ class SchemaChangedException < Exception; end
56
+ class TooBigException < Exception; end
57
+ class ConstraintException < Exception; end
58
+ class MismatchException < Exception; end
59
+ class MisuseException < Exception; end
60
+ class UnsupportedException < Exception; end
61
+ class AuthorizationException < Exception; end
62
+
63
+ EXCEPTIONS = [
64
+ nil,
65
+ SQLException, InternalException, PermissionException,
66
+ AbortException, BusyException, LockedException, MemoryException,
67
+ ReadOnlyException, InterruptException, IOException, CorruptException,
68
+ NotFoundException, FullException, CantOpenException, ProtocolException,
69
+ EmptyException, SchemaChangedException, TooBigException,
70
+ ConstraintException, MismatchException, MisuseException,
71
+ UnsupportedException, AuthorizationException
72
+ ]
73
+
74
+ module Error
75
+ def check( result, db=nil, msg=nil )
76
+ unless result == Constants::ErrorCode::OK
77
+ msg = ( msg ? msg + ": " : "" ) + db.errmsg if db
78
+ raise EXCEPTIONS[result], msg
79
+ end
80
+ end
81
+ module_function :check
82
+ end
83
+
84
+ end
@@ -0,0 +1,254 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are met:
8
+ #
9
+ # * Redistributions of source code must retain the above copyright notice,
10
+ # this list of conditions and the following disclaimer.
11
+ #
12
+ # * Redistributions in binary form must reproduce the above copyright
13
+ # notice, this list of conditions and the following disclaimer in the
14
+ # documentation and/or other materials provided with the distribution.
15
+ #
16
+ # * The names of its contributors may not be used to endorse or promote
17
+ # products derived from this software without specific prior written
18
+ # permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
24
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ # =============================================================================
31
+ #++
32
+
33
+ require 'sqlite3/errors'
34
+
35
+ module SQLite3
36
+
37
+ # This module is intended for inclusion solely by the Database class. It
38
+ # defines convenience methods for the various pragmas supported by SQLite3.
39
+ #
40
+ # For a detailed description of these pragmas, see the SQLite3 documentation
41
+ # at http://sqlite.org/pragma.html.
42
+ module Pragmas
43
+
44
+ # Returns +true+ or +false+ depending on the value of the named pragma.
45
+ def get_boolean_pragma( name )
46
+ get_first_value( "PRAGMA #{name}" ) != "0"
47
+ end
48
+ private :get_boolean_pragma
49
+
50
+ # Sets the given pragma to the given boolean value. The value itself
51
+ # may be +true+ or +false+, or any other commonly used string or
52
+ # integer that represents truth.
53
+ def set_boolean_pragma( name, mode )
54
+ case mode
55
+ when String
56
+ case mode.downcase
57
+ when "on", "yes", "true", "y", "t": mode = "'ON'"
58
+ when "off", "no", "false", "n", "f": mode = "'OFF'"
59
+ else
60
+ raise Exception,
61
+ "unrecognized pragma parameter #{mode.inspect}"
62
+ end
63
+ when true, 1
64
+ mode = "ON"
65
+ when false, 0, nil
66
+ mode = "OFF"
67
+ else
68
+ raise Exception,
69
+ "unrecognized pragma parameter #{mode.inspect}"
70
+ end
71
+
72
+ execute( "PRAGMA #{name}=#{mode}" )
73
+ end
74
+ private :set_boolean_pragma
75
+
76
+ # Requests the given pragma (and parameters), and if the block is given,
77
+ # each row of the result set will be yielded to it. Otherwise, the results
78
+ # are returned as an array.
79
+ def get_query_pragma( name, *parms, &block ) # :yields: row
80
+ if parms.empty?
81
+ execute( "PRAGMA #{name}", &block )
82
+ else
83
+ args = "'" + parms.join("','") + "'"
84
+ execute( "PRAGMA #{name}( #{args} )", &block )
85
+ end
86
+ end
87
+ private :get_query_pragma
88
+
89
+ # Return the value of the given pragma.
90
+ def get_enum_pragma( name )
91
+ get_first_value( "PRAGMA #{name}" )
92
+ end
93
+ private :get_enum_pragma
94
+
95
+ # Set the value of the given pragma to +mode+. The +mode+ parameter must
96
+ # conform to one of the values in the given +enum+ array. Each entry in
97
+ # the array is another array comprised of elements in the enumeration that
98
+ # have duplicate values. See #synchronous, #default_synchronous,
99
+ # #temp_store, and #default_temp_store for usage examples.
100
+ def set_enum_pragma( name, mode, enums )
101
+ match = enums.find { |p| p.find { |i| i.to_s.downcase == mode.to_s.downcase } }
102
+ raise Exception,
103
+ "unrecognized #{name} #{mode.inspect}" unless match
104
+ execute( "PRAGMA #{name}='#{match.first.upcase}'" )
105
+ end
106
+ private :set_enum_pragma
107
+
108
+ # Returns the value of the given pragma as an integer.
109
+ def get_int_pragma( name )
110
+ get_first_value( "PRAGMA #{name}" ).to_i
111
+ end
112
+ private :get_int_pragma
113
+
114
+ # Set the value of the given pragma to the integer value of the +value+
115
+ # parameter.
116
+ def set_int_pragma( name, value )
117
+ execute( "PRAGMA #{name}=#{value.to_i}" )
118
+ end
119
+ private :set_int_pragma
120
+
121
+ # The enumeration of valid synchronous modes.
122
+ SYNCHRONOUS_MODES = [ [ 'full', 2 ], [ 'normal', 1 ], [ 'off', 0 ] ]
123
+
124
+ # The enumeration of valid temp store modes.
125
+ TEMP_STORE_MODES = [ [ 'default', 0 ], [ 'file', 1 ], [ 'memory', 2 ] ]
126
+
127
+ # Does an integrity check on the database. If the check fails, a
128
+ # SQLite3::Exception will be raised. Otherwise it
129
+ # returns silently.
130
+ def integrity_check
131
+ execute( "PRAGMA integrity_check" ) do |row|
132
+ raise Exception, row[0] if row[0] != "ok"
133
+ end
134
+ end
135
+
136
+ def auto_vacuum
137
+ get_boolean_pragma "auto_vacuum"
138
+ end
139
+
140
+ def auto_vacuum=( mode )
141
+ set_boolean_pragma "auto_vacuum", mode
142
+ end
143
+
144
+ def schema_cookie
145
+ get_int_pragma "schema_cookie"
146
+ end
147
+
148
+ def schema_cookie=( cookie )
149
+ set_int_pragma "schema_cookie", cookie
150
+ end
151
+
152
+ def user_cookie
153
+ get_int_pragma "user_cookie"
154
+ end
155
+
156
+ def user_cookie=( cookie )
157
+ set_int_pragma "user_cookie", cookie
158
+ end
159
+
160
+ def cache_size
161
+ get_int_pragma "cache_size"
162
+ end
163
+
164
+ def cache_size=( size )
165
+ set_int_pragma "cache_size", size
166
+ end
167
+
168
+ def default_cache_size
169
+ get_int_pragma "default_cache_size"
170
+ end
171
+
172
+ def default_cache_size=( size )
173
+ set_int_pragma "default_cache_size", size
174
+ end
175
+
176
+ def default_synchronous
177
+ get_enum_pragma "default_synchronous"
178
+ end
179
+
180
+ def default_synchronous=( mode )
181
+ set_enum_pragma "default_synchronous", mode, SYNCHRONOUS_MODES
182
+ end
183
+
184
+ def synchronous
185
+ get_enum_pragma "synchronous"
186
+ end
187
+
188
+ def synchronous=( mode )
189
+ set_enum_pragma "synchronous", mode, SYNCHRONOUS_MODES
190
+ end
191
+
192
+ def default_temp_store
193
+ get_enum_pragma "default_temp_store"
194
+ end
195
+
196
+ def default_temp_store=( mode )
197
+ set_enum_pragma "default_temp_store", mode, TEMP_STORE_MODES
198
+ end
199
+
200
+ def temp_store
201
+ get_enum_pragma "temp_store"
202
+ end
203
+
204
+ def temp_store=( mode )
205
+ set_enum_pragma "temp_store", mode, TEMP_STORE_MODES
206
+ end
207
+
208
+ def full_column_names
209
+ get_boolean_pragma "full_column_names"
210
+ end
211
+
212
+ def full_column_names=( mode )
213
+ set_boolean_pragma "full_column_names", mode
214
+ end
215
+
216
+ def parser_trace
217
+ get_boolean_pragma "parser_trace"
218
+ end
219
+
220
+ def parser_trace=( mode )
221
+ set_boolean_pragma "parser_trace", mode
222
+ end
223
+
224
+ def vdbe_trace
225
+ get_boolean_pragma "vdbe_trace"
226
+ end
227
+
228
+ def vdbe_trace=( mode )
229
+ set_boolean_pragma "vdbe_trace", mode
230
+ end
231
+
232
+ def database_list( &block ) # :yields: row
233
+ get_query_pragma "database_list", &block
234
+ end
235
+
236
+ def foreign_key_list( table, &block ) # :yields: row
237
+ get_query_pragma "foreign_key_list", table, &block
238
+ end
239
+
240
+ def index_info( index, &block ) # :yields: row
241
+ get_query_pragma "index_info", index, &block
242
+ end
243
+
244
+ def index_list( table, &block ) # :yields: row
245
+ get_query_pragma "index_list", table, &block
246
+ end
247
+
248
+ def table_info( table, &block ) # :yields: row
249
+ get_query_pragma "table_info", table, &block
250
+ end
251
+
252
+ end
253
+
254
+ end