sqlite3-ruby 0.5.0

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/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
@@ -0,0 +1,89 @@
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 Value
38
+ attr_reader :handle
39
+
40
+ def initialize( db, handle )
41
+ @driver = db.driver
42
+ @handle = handle
43
+ end
44
+
45
+ def null?
46
+ type == :null
47
+ end
48
+
49
+ def to_blob
50
+ @driver.value_blob( @handle )
51
+ end
52
+
53
+ def length( utf16=false )
54
+ if utf16
55
+ @driver.value_bytes16( @handle )
56
+ else
57
+ @driver.value_bytes( @handle )
58
+ end
59
+ end
60
+
61
+ def to_f
62
+ @driver.value_double( @handle )
63
+ end
64
+
65
+ def to_i
66
+ @driver.value_int( @handle )
67
+ end
68
+
69
+ def to_int64
70
+ @driver.value_int64( @handle )
71
+ end
72
+
73
+ def to_s( utf16=false )
74
+ @driver.value_text( @handle, utf16 )
75
+ end
76
+
77
+ def type
78
+ case @driver.value_type( @handle )
79
+ when Constants::ColumnType::INTEGER then :int
80
+ when Constants::ColumnType::FLOAT then :float
81
+ when Constants::ColumnType::TEXT then :text
82
+ when Constants::ColumnType::BLOB then :blob
83
+ when Constants::ColumnType::NULL then :null
84
+ end
85
+ end
86
+
87
+ end
88
+
89
+ end
@@ -0,0 +1,45 @@
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
+ module SQLite3
34
+
35
+ module Version
36
+
37
+ MAJOR = 0
38
+ MINOR = 5
39
+ TINY = 0
40
+
41
+ STRING = [ MAJOR, MINOR, TINY ].join( "." )
42
+
43
+ end
44
+
45
+ end