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,172 @@
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
+ require 'sqlite3/errors'
35
+
36
+ module SQLite3
37
+
38
+ # The ResultSet object encapsulates the enumerability of a query's output.
39
+ # It is a simple cursor over the data that the query returns. It will
40
+ # very rarely (if ever) be instantiated directly. Instead, client's should
41
+ # obtain a ResultSet instance via Statement#execute.
42
+ class ResultSet
43
+ include Enumerable
44
+
45
+ # A trivial module for adding a +types+ accessor to an object.
46
+ module TypesContainer
47
+ attr_accessor :types
48
+ end
49
+
50
+ # A trivial module for adding a +fields+ accessor to an object.
51
+ module FieldsContainer
52
+ attr_accessor :fields
53
+ end
54
+
55
+ # Create a new ResultSet attached to the given database, using the
56
+ # given sql text.
57
+ def initialize( db, stmt )
58
+ @db = db
59
+ @driver = @db.driver
60
+ @stmt = stmt
61
+ commence
62
+ end
63
+
64
+ # A convenience method for compiling the virtual machine and stepping
65
+ # to the first row of the result set.
66
+ def commence
67
+ result = @driver.step( @stmt.handle )
68
+ check result
69
+ @first_row = true
70
+ end
71
+ private :commence
72
+
73
+ def check( result )
74
+ @eof = ( result == Constants::ErrorCode::DONE )
75
+ found = ( result == Constants::ErrorCode::ROW )
76
+ Error.check( result, @db ) unless @eof || found
77
+ end
78
+ private :check
79
+
80
+ # Reset the cursor, so that a result set which has reached end-of-file
81
+ # can be rewound and reiterated.
82
+ def reset( *bind_params )
83
+ @driver.reset( @stmt.handle )
84
+ @stmt.bind_params( *bind_params )
85
+ @eof = false
86
+ commence
87
+ end
88
+
89
+ # Query whether the cursor has reached the end of the result set or not.
90
+ def eof?
91
+ @eof
92
+ end
93
+
94
+ # Obtain the next row from the cursor. If there are no more rows to be
95
+ # had, this will return +nil+. If type translation is active on the
96
+ # corresponding database, the values in the row will be translated
97
+ # according to their types.
98
+ #
99
+ # The returned value will be an array, unless Database#results_as_hash has
100
+ # been set to +true+, in which case the returned value will be a hash.
101
+ #
102
+ # For arrays, the column names are accessible via the +fields+ property,
103
+ # and the column types are accessible via the +types+ property.
104
+ #
105
+ # For hashes, the column names are the keys of the hash, and the column
106
+ # types are accessible via the +types+ property.
107
+ def next
108
+ return nil if @eof
109
+
110
+ unless @first_row
111
+ result = @driver.step( @stmt.handle )
112
+ check result
113
+ end
114
+
115
+ @first_row = false
116
+
117
+ unless @eof
118
+ row = []
119
+ @driver.data_count( @stmt.handle ).times do |column|
120
+ case @driver.column_type( @stmt.handle, column )
121
+ when Constants::ColumnType::NULL then
122
+ row << nil
123
+ when Constants::ColumnType::BLOB then
124
+ row << @driver.column_blob( @stmt.handle, column )
125
+ else
126
+ row << @driver.column_text( @stmt.handle, column )
127
+ end
128
+ end
129
+
130
+ if @db.type_translation
131
+ row = @stmt.types.zip( row ).map do |type, value|
132
+ @db.translator.translate( type, value )
133
+ end
134
+ end
135
+
136
+ if @db.results_as_hash
137
+ new_row = Hash[ *( @stmt.columns.zip( row ).flatten ) ]
138
+ row.each_with_index { |value,idx| new_row[idx] = value }
139
+ row = new_row
140
+ else
141
+ row.extend FieldsContainer unless row.respond_to?(:fields)
142
+ row.fields = @stmt.columns
143
+ end
144
+
145
+ row.extend TypesContainer
146
+ row.types = @stmt.types
147
+
148
+ return row
149
+ end
150
+
151
+ nil
152
+ end
153
+
154
+ # Required by the Enumerable mixin. Provides an internal iterator over the
155
+ # rows of the result set.
156
+ def each
157
+ while row=self.next
158
+ yield row
159
+ end
160
+ end
161
+
162
+ def types
163
+ @stmt.types
164
+ end
165
+
166
+ def columns
167
+ @stmt.columns
168
+ end
169
+
170
+ end
171
+
172
+ end
@@ -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/errors'
34
+ require 'sqlite3/resultset'
35
+
36
+ class String
37
+ def to_blob
38
+ SQLite3::Blob.new( self )
39
+ end
40
+ end
41
+
42
+ module SQLite3
43
+
44
+ # A class for differentiating between strings and blobs, when binding them
45
+ # into statements.
46
+ class Blob < String; end
47
+
48
+ # A statement represents a prepared-but-unexecuted SQL query. It will rarely
49
+ # (if ever) be instantiated directly by a client, and is most often obtained
50
+ # via the Database#prepare method.
51
+ class Statement
52
+
53
+ # This is any text that followed the first valid SQL statement in the text
54
+ # with which the statement was initialized. If there was no trailing text,
55
+ # this will be the empty string.
56
+ attr_reader :remainder
57
+
58
+ # The underlying opaque handle used to access the SQLite @driver.
59
+ attr_reader :handle
60
+
61
+ # Create a new statement attached to the given Database instance, and which
62
+ # encapsulates the given SQL text. If the text contains more than one
63
+ # statement (i.e., separated by semicolons), then the #remainder property
64
+ # will be set to the trailing text.
65
+ def initialize( db, sql, utf16=false )
66
+ @db = db
67
+ @driver = @db.driver
68
+ result, @handle, @remainder = @driver.prepare( @db.handle, sql )
69
+ Error.check( result, @db )
70
+ end
71
+
72
+ def close
73
+ @driver.finalize( @handle )
74
+ end
75
+
76
+ # Binds the given variables to the corresponding placeholders in the SQL
77
+ # text.
78
+ #
79
+ # See Database#execute for a description of the valid placeholder
80
+ # syntaxes.
81
+ #
82
+ # Example:
83
+ #
84
+ # stmt = db.prepare( "select * from table where a=? and b=?" )
85
+ # stmt.bind_params( 15, "hello" )
86
+ #
87
+ # See also #execute, #bind_param, Statement#bind_param, and
88
+ # Statement#bind_params.
89
+ def bind_params( *bind_vars )
90
+ index = 1
91
+ bind_vars.each do |var|
92
+ if Hash === var
93
+ var.each { |key, val| bind_param key, val }
94
+ else
95
+ bind_param index, var
96
+ index += 1
97
+ end
98
+ end
99
+ end
100
+
101
+ # Binds value to the named (or positional) placeholder. If +param+ is a
102
+ # Fixnum, it is treated as an index for a positional placeholder.
103
+ # Otherwise it is used as the name of the placeholder to bind to.
104
+ #
105
+ # See also #bind_params.
106
+ def bind_param( param, value )
107
+ if Fixnum === param
108
+ case value
109
+ when Integer then
110
+ @driver.bind_int( @handle, param, value )
111
+ when Numeric then
112
+ @driver.bind_double( @handle, param, value.to_f )
113
+ when Blob then
114
+ @driver.bind_blob( @handle, param, value )
115
+ when nil then
116
+ @driver.bind_null( @handle, param )
117
+ else
118
+ @driver.bind_text( @handle, param, value )
119
+ end
120
+ else
121
+ index = @driver.bind_parameter_index(
122
+ @handle, param.to_s )
123
+ raise Exception, "no such bind parameter '#{param}'" if index == 0
124
+ bind_param index, value
125
+ end
126
+ end
127
+
128
+ # Execute the statement. This creates a new ResultSet object for the
129
+ # statement's virtual machine. If a block was given, the new ResultSet will
130
+ # be yielded to it; otherwise, the ResultSet will be returned.
131
+ #
132
+ # Any parameters will be bound to the statement using #bind_params.
133
+ #
134
+ # Example:
135
+ #
136
+ # stmt = db.prepare( "select * from table" )
137
+ # stmt.execute do |result|
138
+ # ...
139
+ # end
140
+ #
141
+ # See also #bind_params, #execute!.
142
+ def execute( *bind_vars )
143
+ @driver.reset( @handle ) if @results
144
+
145
+ bind_params *bind_vars unless bind_vars.empty?
146
+ @results = ResultSet.new( @db, self )
147
+
148
+ if block_given?
149
+ yield @results
150
+ else
151
+ return @results
152
+ end
153
+ end
154
+
155
+ # Execute the statement. If no block was given, this returns an array of
156
+ # rows returned by executing the statement. Otherwise, each row will be
157
+ # yielded to the block.
158
+ #
159
+ # Any parameters will be bound to the statement using #bind_params.
160
+ #
161
+ # Example:
162
+ #
163
+ # stmt = db.prepare( "select * from table" )
164
+ # stmt.execute! do |row|
165
+ # ...
166
+ # end
167
+ #
168
+ # See also #bind_params, #execute.
169
+ def execute!( *bind_vars )
170
+ result = execute( *bind_vars )
171
+ rows = [] unless block_given?
172
+ while row = result.next
173
+ if block_given?
174
+ yield row
175
+ else
176
+ rows << row
177
+ end
178
+ end
179
+ rows
180
+ end
181
+
182
+ # Return an array of the column names for this statement. Note that this
183
+ # may execute the statement in order to obtain the metadata; this makes it
184
+ # a (potentially) expensive operation.
185
+ def columns
186
+ get_metadata unless @columns
187
+ return @columns
188
+ end
189
+
190
+ # Return an array of the data types for each column in this statement. Note
191
+ # that this may execute the statement in order to obtain the metadata; this
192
+ # makes it a (potentially) expensive operation.
193
+ def types
194
+ get_metadata unless @types
195
+ return @types
196
+ end
197
+
198
+ # A convenience method for obtaining the metadata about the query. Note
199
+ # that this will actually execute the SQL, which means it can be a
200
+ # (potentially) expensive operation.
201
+ def get_metadata
202
+ @columns = []
203
+ @types = []
204
+
205
+ column_count = @driver.column_count( @handle )
206
+ column_count.times do |column|
207
+ @columns << @driver.column_name( @handle, column )
208
+ @types << @driver.column_decltype( @handle, column )
209
+ end
210
+
211
+ @columns.freeze
212
+ @types.freeze
213
+ end
214
+ private :get_metadata
215
+
216
+ end
217
+
218
+ end
@@ -0,0 +1,135 @@
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 'time'
34
+
35
+ module SQLite3
36
+
37
+ # The Translator class encapsulates the logic and callbacks necessary for
38
+ # converting string data to a value of some specified type. Every Database
39
+ # instance may have a Translator instance, in order to assist in type
40
+ # translation (Database#type_translation).
41
+ #
42
+ # Further, applications may define their own custom type translation logic
43
+ # by registering translator blocks with the corresponding database's
44
+ # translator instance (Database#translator).
45
+ class Translator
46
+
47
+ # Create a new Translator instance. It will be preinitialized with default
48
+ # translators for most SQL data types.
49
+ def initialize
50
+ @translators = Hash.new( proc { |type,value| value } )
51
+ register_default_translators
52
+ end
53
+
54
+ # Add a new translator block, which will be invoked to process type
55
+ # translations to the given type. The type should be an SQL datatype, and
56
+ # may include parentheses (i.e., "VARCHAR(30)"). However, any parenthetical
57
+ # information is stripped off and discarded, so type translation decisions
58
+ # are made solely on the "base" type name.
59
+ #
60
+ # The translator block itself should accept two parameters, "type" and
61
+ # "value". In this case, the "type" is the full type name (including
62
+ # parentheses), so the block itself may include logic for changing how a
63
+ # type is translated based on the additional data. The "value" parameter
64
+ # is the (string) data to convert.
65
+ #
66
+ # The block should return the translated value.
67
+ def add_translator( type, &block ) # :yields: type, value
68
+ @translators[ type_name( type ) ] = block
69
+ end
70
+
71
+ # Translate the given string value to a value of the given type. In the
72
+ # absense of an installed translator block for the given type, the value
73
+ # itself is always returned. Further, +nil+ values are never translated,
74
+ # and are always passed straight through regardless of the type parameter.
75
+ def translate( type, value )
76
+ unless value.nil?
77
+ @translators[ type_name( type ) ].call( type, value )
78
+ end
79
+ end
80
+
81
+ # A convenience method for working with type names. This returns the "base"
82
+ # type name, without any parenthetical data.
83
+ def type_name( type )
84
+ type = $1 if type =~ /^(.*?)\(/
85
+ type.upcase
86
+ end
87
+ private :type_name
88
+
89
+ # Register the default translators for the current Translator instance.
90
+ # This includes translators for most major SQL data types.
91
+ def register_default_translators
92
+ [ "date",
93
+ "datetime",
94
+ "time" ].each { |type| add_translator( type ) { |t,v| Time.parse( v ) } }
95
+
96
+ [ "decimal",
97
+ "float",
98
+ "numeric",
99
+ "double",
100
+ "real",
101
+ "dec",
102
+ "fixed" ].each { |type| add_translator( type ) { |t,v| v.to_f } }
103
+
104
+ [ "integer",
105
+ "smallint",
106
+ "mediumint",
107
+ "int",
108
+ "bigint" ].each { |type| add_translator( type ) { |t,v| v.to_i } }
109
+
110
+ [ "bit",
111
+ "bool",
112
+ "boolean" ].each do |type|
113
+ add_translator( type ) do |t,v|
114
+ !( v.strip.gsub(/00+/,"0") == "0" ||
115
+ v.downcase == "false" ||
116
+ v.downcase == "f" ||
117
+ v.downcase == "no" ||
118
+ v.downcase == "n" )
119
+ end
120
+ end
121
+
122
+ add_translator( "timestamp" ) { |type, value| Time.at( value.to_i ) }
123
+ add_translator( "tinyint" ) do |type, value|
124
+ if type =~ /\(\s*1\s*\)/
125
+ value.to_i == 1
126
+ else
127
+ value.to_i
128
+ end
129
+ end
130
+ end
131
+ private :register_default_translators
132
+
133
+ end
134
+
135
+ end