dbd-sqlite3 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ (C) 2008 Erik Hollensbe <erik@hollensbe.org>. All rights reserved.
2
+
3
+ Please see "README" for earlier copyrights.
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions
7
+ are met:
8
+ 1. Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+ 2. Redistributions in binary form must reproduce the above copyright
11
+ notice, this list of conditions and the following disclaimer in the
12
+ documentation and/or other materials provided with the distribution.
13
+ 3. The name of the author may not be used to endorse or promote products
14
+ derived from this software without specific prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18
+ AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19
+ THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
+ OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
+ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
+ OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
+ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README ADDED
@@ -0,0 +1,271 @@
1
+ = Description
2
+ The DBI package is a vendor independent interface for accessing databases.
3
+ It is similar, but not identical to, Perl's DBI module.
4
+
5
+ = Synopsis
6
+
7
+ require 'dbi'
8
+
9
+ # Connect to a database, old style
10
+ dbh = DBI.connect('DBI:Mysql:test', 'testuser', 'testpwd')
11
+
12
+ # Insert some rows, use placeholders
13
+ 1.upto(13) do |i|
14
+ sql = "insert into simple01 (SongName, SongLength_s) VALUES (?, ?)"
15
+ dbh.do(sql, "Song #{i}", "#{i*10}")
16
+ end
17
+
18
+ # Select all rows from simple01
19
+ sth = dbh.prepare('select * from simple01')
20
+ sth.execute
21
+
22
+ # Print out each row
23
+ while row=sth.fetch do
24
+ p row
25
+ end
26
+
27
+ # Close the statement handle when done
28
+ sth.finish
29
+
30
+ # Don't prepare, just do it!
31
+ dbh.do('delete from simple01 where internal_id > 10')
32
+
33
+ # And finally, disconnect
34
+ dbh.disconnect
35
+
36
+ # Same example, but a little more Ruby-ish
37
+ DBI.connect('DBI:Mysql:test', 'testuser', 'testpwd') do | dbh |
38
+
39
+ sql = "insert into simple01 (SongName, SongLength_s) VALUES (?, ?)"
40
+
41
+ dbh.prepare(sql) do | sth |
42
+ 1.upto(13) { |i| sth.execute("Song #{i}", "#{i*10}") }
43
+ end
44
+
45
+ dbh.select_all('select * from simple01') do | row |
46
+ p row
47
+ end
48
+
49
+ dbh.do('delete from simple01 where internal_id > 10')
50
+
51
+ end
52
+
53
+ = Prerequisites
54
+ Ruby 1.8.6 or later is the test target, however you may have success with
55
+ earlier 1.8.x versions of Ruby.
56
+
57
+ = RubyForge Project
58
+ General information: http://ruby-dbi.rubyforge.org
59
+ Project information: http://rubyforge.org/projects/ruby-dbi/
60
+ Downloads: http://rubyforge.org/frs/?group_id=234
61
+
62
+ = Installation
63
+ There are many database drivers (DBDs) available. You only need to install
64
+ the DBDs for the database software that you will be using.
65
+
66
+ == Gem setup:
67
+
68
+ gem install dbi
69
+ # One or more of:
70
+ gem install dbd-mysql
71
+ gem install dbd-pg
72
+ gem install dbd-sqlite3
73
+ gem install dbd-sqlite
74
+
75
+ == Without rubygems:
76
+
77
+ ruby setup.rb config
78
+ ruby setup.rb setup
79
+ ruby setup.rb install
80
+
81
+ == The bleeding edge:
82
+
83
+ git clone git://hollensbe.org/git/ruby-dbi.git
84
+ git checkout -b development origin/development
85
+
86
+ Also available at
87
+
88
+ git clone git://github.com/erikh/ruby-dbi.git
89
+
90
+ = Available Database Drivers (DBDs)
91
+
92
+ == DBD::MySQL
93
+ MySQL
94
+ Depends on the mysql-ruby package from http://www.tmtm.org/mysql or
95
+ available from the RAA.
96
+
97
+ == DBD::ODBC
98
+ ODBC
99
+ Depends on the ruby-odbc package (0.5 or later, 0.9.3 or later recommended) at
100
+ http://www.ch-werner.de/rubyodbc or available from the RAA. Works together
101
+ with unix-odbc.
102
+
103
+ == DBD::OCI8
104
+ OCI8 (Oracle)
105
+ Depends on the the ruby-oci8 package, available on the RAA and RubyForge.
106
+
107
+ == DBD::Pg
108
+ PostgreSQL
109
+ Depends on the pg package, available on RubyForge.
110
+
111
+ == DBD::SQLite
112
+ SQLite (versions 2.x and earlier)
113
+ Depends on the sqlite-ruby package, available on rubyforge.
114
+
115
+ == DBD::SQLite3
116
+ SQLite 3.x
117
+ Depends on the sqlite3-ruby package, available on rubyforge.
118
+
119
+ = Additional Documentation
120
+ See the directories doc/* for DBI and DBD specific information.
121
+ The DBI specification is at doc/DBI_SPEC.rdoc.
122
+ The DBD specification is at doc/DBD_SPEC.rdoc.
123
+
124
+ = Articles
125
+ == Tutorial: Using the Ruby DBI Module
126
+ http://www.kitebird.com/articles/ruby-dbi.html
127
+
128
+ = Applications
129
+ == dbi
130
+ The SQL command line interpreter dbi is available in directory
131
+ bin/. It gets installed by default.
132
+
133
+ = License
134
+
135
+ Copyright (c) 2008 Erik Hollensbe
136
+
137
+ Copyright (c) 2005-2006 Kirk Haines, Francis Hwang, Patrick May and Daniel
138
+ Berger.
139
+
140
+ Copyright (c) 2001, 2002, 2003, 2004 Michael Neumann <mneumann@ntecs.de>
141
+ and others (see the beginning of each file for copyright holder information).
142
+
143
+ All rights reserved.
144
+
145
+ Redistribution and use in source and binary forms, with or without
146
+ modification, are permitted provided that the following conditions are met:
147
+
148
+ 1. Redistributions of source code must retain the above copyright notice,
149
+ this list of conditions and the following disclaimer.
150
+ 2. Redistributions in binary form must reproduce the above copyright notice,
151
+ this list of conditions and the following disclaimer in the documentation
152
+ and/or other materials provided with the distribution.
153
+ 3. The name of the author may not be used to endorse or promote products
154
+ derived from this software without specific prior written permission.
155
+
156
+ THIS SOFTWARE IS PROVIDED 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES,
157
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
158
+ AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
159
+ THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
160
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
161
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
162
+ OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
163
+ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
164
+ OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
165
+ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
166
+
167
+ This is the BSD license which is less restrictive than GNU's GPL
168
+ (General Public License).
169
+
170
+ = Contributors
171
+
172
+ Pistos
173
+ Too much to specify. Infinite patience and help.
174
+
175
+ Christopher Maujean
176
+ Lots of initial help when reviving the project.
177
+
178
+ Jun Mukai <mukai@jmuk.org>
179
+ Contributed initial SQLite3 DBD.
180
+
181
+ John J. Fox IV
182
+ Lots of help testing on multiple platforms.
183
+
184
+ Kirk Haines
185
+ One of the authors of the rewrite effort (January 2006).
186
+
187
+ Francis Hwang
188
+ One of the authors of the rewrite effort (January 2006).
189
+
190
+ Patrick May
191
+ One of the authors of the rewrite effort (January 2006).
192
+
193
+ Daniel Berger
194
+ One of the authors of the rewrite effort (January 2006).
195
+
196
+ Michael Neumann
197
+ Original author of Ruby/DBI; wrote the DBI and most of the DBDs.
198
+
199
+ Rainer Perl
200
+ Author of Ruby/DBI 0.0.4 from which many good ideas were taken.
201
+
202
+ Jim Weirich
203
+ Original author of DBD::Pg. Wrote additional code (e.g. sql.rb,
204
+ testcases). Gave many helpful hints and comments.
205
+
206
+ Eli Green
207
+ Implemented DatabaseHandle#columns for Mysql and Pg.
208
+
209
+ Masatoshi SEKI
210
+ For his version of module BasicQuote in sql.rb.
211
+
212
+ John Gorman
213
+ For his case insensitive load_driver patch and parameter parser.
214
+
215
+ David Muse
216
+ For testing the DBD::SQLRelay and for his initial DBD.
217
+
218
+ Jim Menard
219
+ Extended DBD::Oracle for method columns.
220
+
221
+ Joseph McDonald
222
+ Fixed bug in DBD::Pg (default values in method columns).
223
+
224
+ Norbert Gawor
225
+ Fixed bug in DBD::ODBC (method columns) and proxyserver.
226
+
227
+ James F. Hranicky
228
+ Patch for DBD::Pg (cache PGResult#result in Tuples) which increased
229
+ performance by a factor around 100.
230
+
231
+ Stephen Davies
232
+ Added method Statement#fetch_scroll for DBD::Pg.
233
+
234
+ Dave Thomas
235
+ Several enhancements.
236
+
237
+ Brad Hilton
238
+ Column coercing patch for DBD::Mysql.
239
+
240
+ Sean Chittenden
241
+ Originally a co-owner of the project. Submitted several patches
242
+ and helped with lots of comments.
243
+
244
+ MoonWolf
245
+ Provided the quote/escape_byte patch for DBD::Pg, DBD::SQLite patch and
246
+ Database#columns implementation. Further patches.
247
+
248
+ Paul DuBois
249
+ Fixed typos and formatting. Maintains DBD::Mysql.
250
+
251
+ Tim Bates
252
+ Bug fixes for Mysql and DBI.
253
+
254
+ Brian Candler
255
+ Zero-padding date/time/timestamps fix.
256
+
257
+ Florian G. Pflug
258
+ Discussion and helpful comments/benchmarks about DBD::Pg async_exec vs.
259
+ exec.
260
+
261
+ Oliver M. Bolzer
262
+ Patches to support Postgres arrays for DBD::Pg.
263
+
264
+ Stephen R. Veit
265
+ ruby-db2 and DBD::DB2 enhancements.
266
+
267
+ Dennis Vshivkov
268
+ DBD::Pg patches
269
+
270
+ Cail Borrell from frontbase.com
271
+ For the Frontbase DBD and C interface.
@@ -0,0 +1,122 @@
1
+ #--
2
+ # DBD::SQLite3
3
+ #
4
+ # copyright (c) 2005 Jun Mukai <mukai@jmuk.org>
5
+ # Compatibility patches by Erik Hollensbe <erik@hollensbe.org>
6
+ #
7
+ # All rights reserved.
8
+ #
9
+ # Redistribution and use in source and binary forms, with or without
10
+ # modification, are permitted provided that the following conditions
11
+ # are met:
12
+ # 1. Redistributions of source code must retain the above copyright
13
+ # notice, this list of conditions and the following disclaimer.
14
+ # 2. Redistributions in binary form must reproduce the above copyright
15
+ # notice, this list of conditions and the following disclaimer in the
16
+ # documentation and/or other materials provided with the distribution.
17
+ # 3. The name of the author may not be used to endorse or promote products
18
+ # derived from this software without specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
+ # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
+ # AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23
+ # THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26
+ # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27
+ # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28
+ # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29
+ # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ #++
31
+
32
+ begin
33
+ require 'rubygems'
34
+ gem 'sqlite3-ruby'
35
+ gem 'dbi'
36
+ rescue LoadError
37
+ end
38
+
39
+ require 'dbi'
40
+ require 'sqlite3'
41
+ require 'sqlite3/version'
42
+
43
+ module DBI
44
+ module DBD
45
+ #
46
+ # DBD::SQLite3 - Database Driver for SQLite versions 3.x
47
+ #
48
+ # Requires DBI and the 'sqlite3-ruby' gem to work.
49
+ #
50
+ # Only things that extend DBI's results are documented.
51
+ #
52
+ module SQLite3
53
+ VERSION = ::SQLite3::Version::STRING
54
+ DESCRIPTION = "SQLite 3.x DBD for DBI"
55
+
56
+ #
57
+ # returns 'SQLite3'
58
+ #
59
+ # See DBI::TypeUtil#convert for more information.
60
+ #
61
+ def self.driver_name
62
+ "SQLite3"
63
+ end
64
+
65
+ #
66
+ # Validates that the SQL has no literal NUL characters. (ASCII 0)
67
+ #
68
+ # SQLite apparently really hates it when you do that.
69
+ #
70
+ # It will raise DBI::DatabaseError should it find any.
71
+ #
72
+ def self.parse_type(type_name)
73
+ # FIXME plucked from SQLite driver, this needs to be in DBI proper
74
+ return ['varchar'] unless type_name
75
+ type_name.match(/^([^\(]+)(\((\d+)(,(\d+))?\))?$/)
76
+ end
77
+
78
+ #
79
+ # See DBI::BaseDriver.
80
+ #
81
+ class Driver < DBI::BaseDriver
82
+ def initialize
83
+ @dbs = []
84
+ super "0.4.0"
85
+ end
86
+
87
+ def connect(dbname, user, auth, attr)
88
+ raise DBI::InterfaceError, "dbname must be a string" unless dbname.kind_of? String
89
+ raise DBI::InterfaceError, "dbname must have some length" unless dbname.length > 0
90
+ raise DBI::InterfaceError, "attrs must be a hash" unless attr.kind_of? Hash
91
+ db = DBI::DBD::SQLite3::Database.new(dbname, attr)
92
+ @dbs.push(db)
93
+ db
94
+ end
95
+
96
+ def disconnect_all()
97
+ @dbs.each{|db| db.disconnect()}
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ require 'dbd/sqlite3/database'
105
+ require 'dbd/sqlite3/statement'
106
+
107
+ DBI::TypeUtil.register_conversion(DBI::DBD::SQLite3.driver_name) do |obj|
108
+ newobj = case obj
109
+ when ::TrueClass
110
+ '1'
111
+ when ::FalseClass
112
+ '0'
113
+ else
114
+ # SQLite3 is managing its own conversion right now, until I'm happy let's keep it that way
115
+ obj.dup rescue obj
116
+ end
117
+ if newobj.object_id == obj.object_id
118
+ [newobj, true]
119
+ else
120
+ [newobj, false]
121
+ end
122
+ end
@@ -0,0 +1,192 @@
1
+ #
2
+ # See DBI::BaseDatabase.
3
+ #
4
+ class DBI::DBD::SQLite3::Database < DBI::BaseDatabase
5
+ #
6
+ # Constructor. Valid attributes:
7
+ #
8
+ # * AutoCommit: Commit after every statement execution.
9
+ #
10
+ # The following attributes go directly to the low-level SQLite3 driver.
11
+ # Please consult it's documentation for more information.
12
+ #
13
+ # * auto_vacuum
14
+ # * cache_size
15
+ # * default_cache_size
16
+ # * default_synchronous
17
+ # * default_temp_store
18
+ # * full_column_names
19
+ # * synchronous
20
+ # * temp_store
21
+ # * type_translation
22
+ #
23
+ def initialize(dbname, attr)
24
+ @db = ::SQLite3::Database.new(dbname)
25
+
26
+ @db.type_translation = false
27
+
28
+ @attr = {'AutoCommit' => true}
29
+ if attr then
30
+ attr.each_pair do |key, value|
31
+ begin
32
+ self[key] = value
33
+ rescue DBI::NotSupportedError
34
+ end
35
+ end
36
+ end
37
+ __generate_attr__
38
+ end
39
+
40
+ def disconnect()
41
+ @db.rollback if @db.transaction_active?
42
+ @db.close
43
+ end
44
+
45
+ def prepare(statement)
46
+ DBI::DBD::SQLite3::Statement.new(statement, @db)
47
+ end
48
+
49
+ def ping()
50
+ not @db.closed?
51
+ end
52
+
53
+ def commit()
54
+ if @db.transaction_active?
55
+ @db.commit
56
+ @db.transaction
57
+ else
58
+ raise DBI::ProgrammingError.new("No active transaction.")
59
+ end
60
+ end
61
+
62
+ #
63
+ # See DBI::BaseDatabase#rollback.
64
+ #
65
+ # If all statements were not closed before the rollback occurs, a
66
+ # DBI::Warning may be raised if the database encounters an error because of
67
+ # it.
68
+ #
69
+ # This method will also raise DBI::ProgrammingError if not in a
70
+ # transaction.
71
+ #
72
+ def rollback()
73
+ if @db.transaction_active?
74
+ begin
75
+ @db.rollback
76
+ @db.transaction
77
+ rescue Exception => e
78
+ raise DBI::Warning, "Statements were not closed prior to rollback"
79
+ end
80
+ else
81
+ raise DBI::ProgrammingError.new("No active transaction.")
82
+ end
83
+ end
84
+
85
+ def tables()
86
+ ret = []
87
+ result = @db.execute(%q(
88
+ SELECT name FROM sqlite_master WHERE type IN ('table', 'view')
89
+ UNION ALL
90
+ SELECT name FROM sqlite_temp_master WHERE type in ('table', 'view') ORDER BY 1
91
+ ))
92
+ result.each{|row| ret.push(row[0])}
93
+ ret
94
+ end
95
+
96
+ #
97
+ # See DBI::BaseDatabase#columns.
98
+ #
99
+ # Additional Attributes:
100
+ #
101
+ # * sql_type: XOPEN integer SQL Type.
102
+ # * nullable: true if NULL is allowed in this column.
103
+ # * default: the value that will be used in new rows if this column
104
+ # receives no data.
105
+ #
106
+ def columns(table)
107
+ @db.type_translation = false
108
+ ret =
109
+ @db.table_info(table).map do |hash|
110
+ m = DBI::DBD::SQLite3.parse_type(hash['type'])
111
+ h = {
112
+ 'name' => hash['name'],
113
+ 'type_name' => m[1],
114
+ 'sql_type' =>
115
+ begin
116
+ DBI.const_get('SQL_'+hash['type'].upcase)
117
+ rescue NameError
118
+ DBI::SQL_OTHER
119
+ end,
120
+ 'nullable' => (hash['notnull'] == '0'),
121
+ 'default' => (@attr['type_translation'] && (not hash['dflt_value'])) ?
122
+ @db.translator.translate(hash['type'], hash['dflt_value']) :
123
+ hash['dflt_value']
124
+ }
125
+
126
+ h['precision'] = m[3].to_i if m[3]
127
+ h['scale'] = m[5].to_i if m[5]
128
+
129
+ h
130
+ end
131
+ @db.type_translation = @attr['type_translation']
132
+ ret
133
+ end
134
+
135
+ def quote(value)
136
+ ::SQLite3::Database.quote(value.to_s)
137
+ end
138
+
139
+ #
140
+ # This method is used to aid the constructor and probably should not be
141
+ # used independently.
142
+ #
143
+ def __generate_attr__()
144
+ tt = @db.type_translation
145
+ @db.type_translation = false
146
+ [ 'auto_vacuum', 'cache_size', 'default_cache_size',
147
+ 'default_synchronous', 'default_temp_store', 'full_column_names',
148
+ 'synchronous', 'temp_store', 'type_translation' ].each do |key|
149
+ unless @attr.has_key?(key) then
150
+ @attr[key] = @db.__send__(key)
151
+ end
152
+ end
153
+ @db.type_translation = tt
154
+ end
155
+
156
+ #
157
+ # See #new for valid attributes.
158
+ #
159
+ # If Autocommit is set to true, commit happens immediately if a transaction
160
+ # is open.
161
+ #
162
+ def []=(attr, value)
163
+ case attr
164
+ when 'AutoCommit'
165
+ if value
166
+ @db.commit if @db.transaction_active?
167
+ else
168
+ @db.transaction unless @db.transaction_active?
169
+ end
170
+ @attr[attr] = value
171
+ when 'auto_vacuum', 'cache_size', 'count_changes',
172
+ 'default_cache_size', 'encoding', 'full_column_names',
173
+ 'page_size', 'short_column_names', 'synchronous',
174
+ 'temp_store', 'temp_store_directory'
175
+ @db.__send__((attr+'='), value)
176
+ @attr[attr] = @db.__send__(attr)
177
+ when 'busy_timeout'
178
+ @db.busy_timeout(value)
179
+ @attr[attr] = value
180
+ when 'busy_handler'
181
+ @db.busy_timeout(&value)
182
+ @attr[attr] = value
183
+ when 'type_translation'
184
+ @db.type_translation = value
185
+ @attr[attr] = value
186
+ else
187
+ raise DBI::NotSupportedError
188
+ end
189
+
190
+ return value
191
+ end
192
+ end