dbd-sqlite 0.1

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.
data/lib/dbd/SQLite.rb ADDED
@@ -0,0 +1,97 @@
1
+ #--
2
+ ###############################################################################
3
+ #
4
+ # DBD::SQLite - a DBD for SQLite for versions < 3
5
+ #
6
+ # Uses Jamis Buck's 'sqlite-ruby' driver to interface with SQLite directly
7
+ #
8
+ # (c) 2008 Erik Hollensbe & Christopher Maujean.
9
+ #
10
+ # TODO
11
+ #
12
+ # fetch_scroll implementation?
13
+ # columns and column_info differ too much and have too much copied code, refactor
14
+ # there are probably some edge cases with transactions
15
+ #
16
+ ################################################################################
17
+ #++
18
+
19
+ begin
20
+ require 'rubygems'
21
+ gem 'sqlite-ruby'
22
+ gem 'dbi'
23
+ rescue Exception => e
24
+ end
25
+
26
+ require 'dbi'
27
+ require 'sqlite'
28
+
29
+ module DBI
30
+ module DBD
31
+ #
32
+ # DBD::SQLite - Database Driver for SQLite versions 2.x and lower.
33
+ #
34
+ # Requires DBI and the 'sqlite-ruby' gem to work.
35
+ #
36
+ # Only things that extend DBI's results are documented.
37
+ #
38
+ class SQLite
39
+ VERSION = "0.1"
40
+ DESCRIPTION = "SQLite 2.x DBI DBD"
41
+
42
+ #
43
+ # returns 'SQLite'
44
+ #
45
+ # See DBI::TypeUtil#convert for more information.
46
+ #
47
+ def self.driver_name
48
+ "SQLite"
49
+ end
50
+
51
+ #
52
+ # Validates that the SQL has no literal NUL characters. (ASCII 0)
53
+ #
54
+ # SQLite apparently really hates it when you do that.
55
+ #
56
+ # It will raise DBI::DatabaseError should it find any.
57
+ #
58
+ def self.check_sql(sql)
59
+ # XXX I'm starting to think this is less of a problem with SQLite
60
+ # and more with the old C DBD
61
+ raise DBI::DatabaseError, "Bad SQL: SQL cannot contain nulls" if sql =~ /\0/
62
+ end
63
+
64
+ #
65
+ # Split a type definition into parts via String#match and return the whole result.
66
+ #
67
+ def self.parse_type(type_name)
68
+ type_name.match(/^([^\(]+)(\((\d+)(,(\d+))?\))?$/)
69
+ end
70
+
71
+ #
72
+ # See DBI::BaseDriver.
73
+ #
74
+ class Driver < DBI::BaseDriver
75
+ def initialize
76
+ super "0.4.0"
77
+ end
78
+
79
+ def connect(dbname, user, auth, attr_hash)
80
+ return Database.new(dbname, user, auth, attr_hash)
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ require 'dbd/sqlite/database'
88
+ require 'dbd/sqlite/statement'
89
+
90
+ DBI::TypeUtil.register_conversion(DBI::DBD::SQLite.driver_name) do |obj|
91
+ case obj
92
+ when ::NilClass
93
+ ["NULL", false]
94
+ else
95
+ [obj, true]
96
+ end
97
+ end
@@ -0,0 +1,142 @@
1
+ #
2
+ # See DBI::BaseDatabase.
3
+ #
4
+ class DBI::DBD::SQLite::Database < DBI::BaseDatabase
5
+ attr_reader :db
6
+ attr_reader :attr_hash
7
+ attr_accessor :open_handles
8
+
9
+ #
10
+ # Constructor. Valid attributes:
11
+ #
12
+ # * AutoCommit: Commit after every statement execution.
13
+ #
14
+ def initialize(dbname, user, auth, attr_hash)
15
+ # FIXME why isn't this crap being done in DBI?
16
+ unless dbname.kind_of? String
17
+ raise DBI::InterfaceError, "Database Name must be a string"
18
+ end
19
+
20
+ unless dbname.length > 0
21
+ raise DBI::InterfaceError, "Database Name needs to be length > 0"
22
+ end
23
+
24
+ unless attr_hash.kind_of? Hash
25
+ raise DBI::InterfaceError, "Attributes should be a hash"
26
+ end
27
+
28
+ # FIXME handle busy_timeout in SQLite driver
29
+ # FIXME handle SQLite pragmas in SQLite driver
30
+ @attr_hash = attr_hash
31
+ @open_handles = 0
32
+
33
+ self["AutoCommit"] = true if self["AutoCommit"].nil?
34
+
35
+ # open the database
36
+ begin
37
+ @db = ::SQLite::Database.new(dbname)
38
+ rescue Exception => e
39
+ raise DBI::OperationalError, "Couldn't open database #{dbname}: #{e.message}"
40
+ end
41
+ end
42
+
43
+ def disconnect
44
+ rollback rescue nil
45
+ @db.close if @db and !@db.closed?
46
+ @db = nil
47
+ end
48
+
49
+ def prepare(stmt)
50
+ return DBI::DBD::SQLite::Statement.new(stmt, self)
51
+ end
52
+
53
+ def ping
54
+ return !@db.closed?
55
+ end
56
+
57
+ def tables
58
+ sth = prepare("select name from sqlite_master where type in ('table', 'view')")
59
+ sth.execute
60
+ tables = sth.fetch_all.flatten
61
+ sth.finish
62
+ return tables
63
+ # FIXME does sqlite use views too? not sure, but they need to be included according to spec
64
+ end
65
+
66
+ def commit
67
+ @db.commit if @db.transaction_active?
68
+ end
69
+
70
+ #
71
+ # Rollback the transaction. SQLite has some issues with open statement
72
+ # handles when this happens. If there are still open handles, a
73
+ # DBI::Warning exception will be raised.
74
+ #
75
+ def rollback
76
+ if @open_handles > 0
77
+ raise DBI::Warning, "Leaving unfinished select statement handles while rolling back a transaction can corrupt your database or crash your program"
78
+ end
79
+
80
+ @db.rollback if @db.transaction_active?
81
+ end
82
+
83
+ def [](key)
84
+ return @attr_hash[key]
85
+ end
86
+
87
+ #
88
+ # See DBI::BaseDatabase#[]=.
89
+ #
90
+ # If AutoCommit is set to +true+ using this method, was previously +false+,
91
+ # and we are currently in a transaction, The act of setting this will cause
92
+ # an immediate commit.
93
+ #
94
+ def []=(key, value)
95
+
96
+ old_value = @attr_hash[key]
97
+
98
+ @attr_hash[key] = value
99
+
100
+ # special handling of settings
101
+ case key
102
+ when "AutoCommit"
103
+ # if the value being set is true and the previous value is false,
104
+ # commit the current transaction (if any)
105
+ # FIXME I still think this is a horrible way of handling this.
106
+ if value and !old_value
107
+ begin
108
+ @dbh.commit
109
+ rescue Exception => e
110
+ end
111
+ end
112
+ end
113
+
114
+ return @attr_hash[key]
115
+ end
116
+
117
+ def columns(tablename)
118
+ return nil unless tablename and tablename.kind_of? String
119
+
120
+ sth = prepare("PRAGMA table_info(?)")
121
+ sth.bind_param(1, tablename)
122
+ sth.execute
123
+ columns = [ ]
124
+ while row = sth.fetch
125
+ column = { }
126
+ column["name"] = row[1]
127
+
128
+ m = DBI::DBD::SQLite.parse_type(row[2])
129
+ column["type_name"] = m[1]
130
+ column["precision"] = m[3].to_i if m[3]
131
+ column["scale"] = m[5].to_i if m[5]
132
+
133
+ column["nullable"] = row[3].to_i == 0
134
+ column["default"] = row[4]
135
+ columns.push column
136
+ end
137
+
138
+ sth.finish
139
+ return columns
140
+ # XXX it'd be nice if the spec was changed to do this k/v with the name as the key.
141
+ end
142
+ end