ydbd-pg 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,274 @@
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
+ Branch by ywesee.com as our patches never got integrated upstream.
6
+ The Gem is named ydbi, but internally we still use dbi as name
7
+
8
+ # Synopsis
9
+
10
+ require 'dbi'
11
+
12
+ # Connect to a database, old style
13
+ dbh = DBI.connect('DBI:Mysql:test', 'testuser', 'testpwd')
14
+
15
+ # Insert some rows, use placeholders
16
+ 1.upto(13) do |i|
17
+ sql = "insert into simple01 (SongName, SongLength_s) VALUES (?, ?)"
18
+ dbh.do(sql, "Song #{i}", "#{i*10}")
19
+ end
20
+
21
+ # Select all rows from simple01
22
+ sth = dbh.prepare('select * from simple01')
23
+ sth.execute
24
+
25
+ # Print out each row
26
+ while row=sth.fetch do
27
+ p row
28
+ end
29
+
30
+ # Close the statement handle when done
31
+ sth.finish
32
+
33
+ # Don't prepare, just do it!
34
+ dbh.do('delete from simple01 where internal_id > 10')
35
+
36
+ # And finally, disconnect
37
+ dbh.disconnect
38
+
39
+ # Same example, but a little more Ruby-ish
40
+ DBI.connect('DBI:Mysql:test', 'testuser', 'testpwd') do | dbh |
41
+
42
+ sql = "insert into simple01 (SongName, SongLength_s) VALUES (?, ?)"
43
+
44
+ dbh.prepare(sql) do | sth |
45
+ 1.upto(13) { |i| sth.execute("Song #{i}", "#{i*10}") }
46
+ end
47
+
48
+ dbh.select_all('select * from simple01') do | row |
49
+ p row
50
+ end
51
+
52
+ dbh.do('delete from simple01 where internal_id > 10')
53
+
54
+ end
55
+
56
+ # Prerequisites
57
+ Ruby 1.8.6 or later is the test target, however you may have success with
58
+ earlier 1.8.x versions of Ruby.
59
+
60
+ # RubyForge Project
61
+ General information: http://ruby-dbi.rubyforge.org
62
+ Project information: http://rubyforge.org/projects/ruby-dbi/
63
+ Downloads: http://rubyforge.org/frs/?group_id=234
64
+
65
+ # Installation
66
+ There are many database drivers (DBDs) available. You only need to install
67
+ the DBDs for the database software that you will be using.
68
+
69
+ ## Gem setup:
70
+
71
+ gem install dbi
72
+ # One or more of:
73
+ gem install dbd-mysql
74
+ gem install dbd-pg
75
+ gem install dbd-sqlite3
76
+ gem install dbd-sqlite
77
+
78
+ ## Without rubygems:
79
+
80
+ ruby setup.rb config
81
+ ruby setup.rb setup
82
+ ruby setup.rb install
83
+
84
+ ## The bleeding edge:
85
+
86
+ git clone git://hollensbe.org/git/ruby-dbi.git
87
+ git checkout -b development origin/development
88
+
89
+ Also available at
90
+
91
+ git clone git://github.com/erikh/ruby-dbi.git
92
+
93
+ # Available Database Drivers (DBDs)
94
+
95
+ ## DBD::MySQL
96
+ MySQL
97
+ Depends on the mysql-ruby package from http://www.tmtm.org/mysql or
98
+ available from the RAA.
99
+
100
+ ## DBD::ODBC
101
+ ODBC
102
+ Depends on the ruby-odbc package (0.5 or later, 0.9.3 or later recommended) at
103
+ http://www.ch-werner.de/rubyodbc or available from the RAA. Works together
104
+ with unix-odbc.
105
+
106
+ ## DBD::OCI8
107
+ OCI8 (Oracle)
108
+ Depends on the the ruby-oci8 package, available on the RAA and RubyForge.
109
+
110
+ ## DBD::Pg
111
+ PostgreSQL
112
+ Depends on the pg package, available on RubyForge.
113
+
114
+ ## DBD::SQLite
115
+ SQLite (versions 2.x and earlier)
116
+ Depends on the sqlite-ruby package, available on rubyforge.
117
+
118
+ ## DBD::SQLite3
119
+ SQLite 3.x
120
+ Depends on the sqlite3-ruby package, available on rubyforge.
121
+
122
+ # Additional Documentation
123
+ See the directories doc/* for DBI and DBD specific information.
124
+ The DBI specification is at doc/DBI_SPEC.rdoc.
125
+ The DBD specification is at doc/DBD_SPEC.rdoc.
126
+
127
+ # Articles
128
+ ## Tutorial: Using the Ruby DBI Module
129
+ http://www.kitebird.com/articles/ruby-dbi.html
130
+
131
+ # Applications
132
+ ## dbi
133
+ The SQL command line interpreter dbi is available in directory
134
+ bin/. It gets installed by default.
135
+
136
+ # License
137
+
138
+ Copyright (c) 2008 Erik Hollensbe
139
+
140
+ Copyright (c) 2005-2006 Kirk Haines, Francis Hwang, Patrick May and Daniel
141
+ Berger.
142
+
143
+ Copyright (c) 2001, 2002, 2003, 2004 Michael Neumann <mneumann@ntecs.de>
144
+ and others (see the beginning of each file for copyright holder information).
145
+
146
+ All rights reserved.
147
+
148
+ Redistribution and use in source and binary forms, with or without
149
+ modification, are permitted provided that the following conditions are met:
150
+
151
+ 1. Redistributions of source code must retain the above copyright notice,
152
+ this list of conditions and the following disclaimer.
153
+ 2. Redistributions in binary form must reproduce the above copyright notice,
154
+ this list of conditions and the following disclaimer in the documentation
155
+ and/or other materials provided with the distribution.
156
+ 3. The name of the author may not be used to endorse or promote products
157
+ derived from this software without specific prior written permission.
158
+
159
+ THIS SOFTWARE IS PROVIDED 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES,
160
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
161
+ AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
162
+ THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
163
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
164
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
165
+ OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
166
+ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
167
+ OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
168
+ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
169
+
170
+ This is the BSD license which is less restrictive than GNU's GPL
171
+ (General Public License).
172
+
173
+ # Contributors
174
+
175
+ Pistos
176
+ Too much to specify. Infinite patience and help.
177
+
178
+ Christopher Maujean
179
+ Lots of initial help when reviving the project.
180
+
181
+ Jun Mukai <mukai@jmuk.org>
182
+ Contributed initial SQLite3 DBD.
183
+
184
+ John J. Fox IV
185
+ Lots of help testing on multiple platforms.
186
+
187
+ Kirk Haines
188
+ One of the authors of the rewrite effort (January 2006).
189
+
190
+ Francis Hwang
191
+ One of the authors of the rewrite effort (January 2006).
192
+
193
+ Patrick May
194
+ One of the authors of the rewrite effort (January 2006).
195
+
196
+ Daniel Berger
197
+ One of the authors of the rewrite effort (January 2006).
198
+
199
+ Michael Neumann
200
+ Original author of Ruby/DBI; wrote the DBI and most of the DBDs.
201
+
202
+ Rainer Perl
203
+ Author of Ruby/DBI 0.0.4 from which many good ideas were taken.
204
+
205
+ Jim Weirich
206
+ Original author of DBD::Pg. Wrote additional code (e.g. sql.rb,
207
+ testcases). Gave many helpful hints and comments.
208
+
209
+ Eli Green
210
+ Implemented DatabaseHandle#columns for Mysql and Pg.
211
+
212
+ Masatoshi SEKI
213
+ For his version of module BasicQuote in sql.rb.
214
+
215
+ John Gorman
216
+ For his case insensitive load_driver patch and parameter parser.
217
+
218
+ David Muse
219
+ For testing the DBD::SQLRelay and for his initial DBD.
220
+
221
+ Jim Menard
222
+ Extended DBD::Oracle for method columns.
223
+
224
+ Joseph McDonald
225
+ Fixed bug in DBD::Pg (default values in method columns).
226
+
227
+ Norbert Gawor
228
+ Fixed bug in DBD::ODBC (method columns) and proxyserver.
229
+
230
+ James F. Hranicky
231
+ Patch for DBD::Pg (cache PGResult#result in Tuples) which increased
232
+ performance by a factor around 100.
233
+
234
+ Stephen Davies
235
+ Added method Statement#fetch_scroll for DBD::Pg.
236
+
237
+ Dave Thomas
238
+ Several enhancements.
239
+
240
+ Brad Hilton
241
+ Column coercing patch for DBD::Mysql.
242
+
243
+ Sean Chittenden
244
+ Originally a co-owner of the project. Submitted several patches
245
+ and helped with lots of comments.
246
+
247
+ MoonWolf
248
+ Provided the quote/escape_byte patch for DBD::Pg, DBD::SQLite patch and
249
+ Database#columns implementation. Further patches.
250
+
251
+ Paul DuBois
252
+ Fixed typos and formatting. Maintains DBD::Mysql.
253
+
254
+ Tim Bates
255
+ Bug fixes for Mysql and DBI.
256
+
257
+ Brian Candler
258
+ Zero-padding date/time/timestamps fix.
259
+
260
+ Florian G. Pflug
261
+ Discussion and helpful comments/benchmarks about DBD::Pg async_exec vs.
262
+ exec.
263
+
264
+ Oliver M. Bolzer
265
+ Patches to support Postgres arrays for DBD::Pg.
266
+
267
+ Stephen R. Veit
268
+ ruby-db2 and DBD::DB2 enhancements.
269
+
270
+ Dennis Vshivkov
271
+ DBD::Pg patches
272
+
273
+ Cail Borrell from frontbase.com
274
+ For the Frontbase DBD and C interface.
@@ -0,0 +1,50 @@
1
+ ================================================================================
2
+ Using DBD tests
3
+ ================================================================================
4
+
5
+ Before you do anything, read the TESTING file.
6
+
7
+ Create a YAML file named .ruby-dbi.test-config.yaml in your home directory.
8
+
9
+ This file is a hash of keys that determine what you want to test and how you
10
+ access the databases related to those tests.
11
+
12
+ The key 'dbtypes' is an array which determines what tests you want to run. They
13
+ *do not* correspond to the driver names, they correspond to the test
14
+ directories that were made for them.
15
+
16
+ Each 'dbtype' has a key that contains a hash of values:
17
+ - username: the username of your account
18
+ - password: the password for your account
19
+ - dbname: the name of the database to connect to
20
+
21
+ NOTE that tests expect to connect to a database on localhost currently. This
22
+ may be fixed in the future, especially when we start writing Oracle and
23
+ SQLServer tests.
24
+
25
+ Each DBD test relies on database semantics which may not match up entirely with
26
+ this configuration. For instance, the postgresql tests expect you to be able to
27
+ work with the database directly via the `psql' client. This is something which
28
+ will eventually be remedied as time and ability allows.
29
+
30
+ Here is a sample configuration to get you started with the postgresql tests:
31
+
32
+ ################################################################################
33
+
34
+ ---
35
+ dbtypes:
36
+ - postgresql
37
+ postgresql:
38
+ username: erikh
39
+ password: monkeys
40
+ dbname: rubytest
41
+
42
+ ################################################################################
43
+
44
+ NOTE the --- is part of the file and is not a separator.
45
+
46
+ ================================================================================
47
+ Writing DBD tests
48
+ ================================================================================
49
+
50
+ Coming soon.
@@ -0,0 +1,206 @@
1
+ @class = Class.new(DBDConfig.testbase(DBDConfig.current_dbtype)) do
2
+
3
+ def test_last_statement
4
+ @sth = @dbh.prepare("select * from names")
5
+ @sth.finish
6
+ assert_equal "select * from names", @dbh.last_statement
7
+
8
+ @sth = @dbh.execute("select * from names")
9
+ @sth.finish
10
+ assert_equal "select * from names", @dbh.last_statement
11
+
12
+ @dbh.do("select * from names")
13
+ assert_equal "select * from names", @dbh.last_statement
14
+ end
15
+
16
+ def test_empty_query
17
+ ["", " ", "\t"].each do |str|
18
+ [:do, :prepare, :execute, :select_one, :select_all].each do |call|
19
+ assert_raises(DBI::InterfaceError) do
20
+ @dbh.send(call, str)
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ def test_ping
27
+ assert @dbh.ping
28
+ # XXX if it isn't obvious, this should be tested better. Not sure what
29
+ # good behavior is yet.
30
+ end
31
+
32
+ def test_columns
33
+ assert_nothing_raised do
34
+ cols = @dbh.columns("precision_test")
35
+
36
+ assert(cols)
37
+ assert_kind_of(Array, cols)
38
+ assert_equal(4, cols.length)
39
+
40
+ # the first column should always be "text_field" and have the following
41
+ # properties:
42
+ assert_equal("text_field", cols[0]["name"])
43
+ assert(!cols[0]["nullable"])
44
+
45
+ assert_equal(20, cols[0]["precision"])
46
+ # scale can be either nil or 0 for character types.
47
+ case cols[0]["scale"]
48
+ when nil
49
+ assert_equal(nil, cols[0]["scale"])
50
+ when 0
51
+ assert_equal(0, cols[0]["scale"])
52
+ else
53
+ flunk "scale can be either 0 or nil for character types"
54
+ end
55
+
56
+ assert_equal(
57
+ DBI::Type::Varchar.object_id,
58
+ DBI::TypeUtil.type_name_to_module(cols[0]["type_name"]).object_id
59
+ )
60
+
61
+ # the second column should always be "integer_field" and have the following
62
+ # properties:
63
+ assert_equal("integer_field", cols[1]["name"])
64
+ assert(cols[1]["nullable"])
65
+ assert_equal(1, cols[2]["scale"])
66
+ assert_equal(2, cols[2]["precision"])
67
+
68
+ assert_equal(
69
+ DBI::Type::Integer.object_id,
70
+ DBI::TypeUtil.type_name_to_module(cols[1]["type_name"]).object_id
71
+ )
72
+
73
+ # the second column should always be "integer_field" and have the following
74
+ # properties:
75
+ assert_equal("decimal_field", cols[2]["name"])
76
+ assert(cols[2]["nullable"])
77
+ assert_equal(1, cols[2]["scale"])
78
+ assert_equal(2, cols[2]["precision"])
79
+ assert_equal(
80
+ DBI::Type::Decimal.object_id,
81
+ DBI::TypeUtil.type_name_to_module(cols[2]["type_name"]).object_id
82
+ )
83
+
84
+ # the second column should always be "numeric_field" and have the following
85
+ # properties:
86
+ assert_equal("numeric_field", cols[3]["name"])
87
+ assert(cols[3]["nullable"])
88
+ assert_equal(6, cols[3]["scale"])
89
+ assert_equal(30, cols[3]["precision"])
90
+ assert_equal(
91
+ DBI::Type::Decimal.object_id,
92
+ DBI::TypeUtil.type_name_to_module(cols[3]["type_name"]).object_id
93
+ )
94
+
95
+ # finally, we ensure that every column in the array is a ColumnInfo
96
+ # object
97
+ cols.each { |col| assert_kind_of(DBI::ColumnInfo, col) }
98
+ end
99
+ end
100
+
101
+ def test_prepare
102
+ @sth = @dbh.prepare('select * from names')
103
+
104
+ assert @sth
105
+ assert_kind_of DBI::StatementHandle, @sth
106
+
107
+ @sth.finish
108
+ end
109
+
110
+ def test_do
111
+ assert_equal 1, @dbh.do("insert into names (name, age) values (?, ?)", "Billy", 21)
112
+ @sth = @dbh.prepare("select * from names where name = ?")
113
+ @sth.execute("Billy")
114
+ assert_equal ["Billy", 21], @sth.fetch
115
+ @sth.finish
116
+ end
117
+
118
+ def test_tables
119
+ tables = @dbh.tables.sort
120
+
121
+ # since this is a general test, let's prune the system tables
122
+ # FIXME not so sure if this should be a general test anymore.
123
+ if dbtype == "odbc"
124
+ tables -= [
125
+ "administrable_role_authorizations",
126
+ "applicable_roles",
127
+ "attributes",
128
+ "check_constraint_routine_usage",
129
+ "check_constraints",
130
+ "column_domain_usage",
131
+ "column_privileges",
132
+ "column_udt_usage",
133
+ "columns",
134
+ "constraint_column_usage",
135
+ "constraint_table_usage",
136
+ "data_type_privileges",
137
+ "domain_constraints",
138
+ "domain_udt_usage",
139
+ "domains",
140
+ "element_types",
141
+ "enabled_roles",
142
+ "information_schema_catalog_name",
143
+ "key_column_usage",
144
+ "parameters",
145
+ "referential_constraints",
146
+ "role_column_grants",
147
+ "role_routine_grants",
148
+ "role_table_grants",
149
+ "role_usage_grants",
150
+ "routine_privileges",
151
+ "routines",
152
+ "schemata",
153
+ "sequences",
154
+ "sql_features",
155
+ "sql_implementation_info",
156
+ "sql_languages",
157
+ "sql_packages",
158
+ "sql_parts",
159
+ "sql_sizing",
160
+ "sql_sizing_profiles",
161
+ "table_constraints",
162
+ "table_privileges",
163
+ "tables",
164
+ "triggered_update_columns",
165
+ "triggers",
166
+ "usage_privileges",
167
+ "view_column_usage",
168
+ "view_routine_usage",
169
+ "view_table_usage",
170
+ "views"
171
+ ]
172
+ end
173
+
174
+ case dbtype
175
+ when "postgresql"
176
+ tables.reject! { |x| x =~ /^pg_/ }
177
+ assert_equal %w(array_test bit_test blob_test boolean_test bytea_test db_specific_types_test enum_type_test field_types_test names precision_test time_test timestamp_test view_names), tables
178
+ when 'sqlite3'
179
+ assert_equal %w(bit_test blob_test boolean_test db_specific_types_test field_types_test names names_defined_with_spaces precision_test time_test timestamp_test view_names), tables
180
+ else
181
+ assert_equal %w(bit_test blob_test boolean_test db_specific_types_test field_types_test names precision_test time_test timestamp_test view_names), tables
182
+ end
183
+ end
184
+
185
+ def test_attrs
186
+ # test defaults
187
+ assert @dbh["AutoCommit"] # should be true
188
+
189
+ # test setting
190
+ assert !(@dbh["AutoCommit"] = false)
191
+ assert !@dbh["AutoCommit"]
192
+
193
+ # test committing an outstanding transaction
194
+
195
+ @sth = @dbh.prepare("insert into names (name, age) values (?, ?)")
196
+ @sth.execute("Billy", 22)
197
+ @sth.finish
198
+
199
+ assert @dbh["AutoCommit"] = true # should commit at this point
200
+
201
+ @sth = @dbh.prepare("select * from names where name = ?")
202
+ @sth.execute("Billy")
203
+ assert_equal [ "Billy", 22 ], @sth.fetch
204
+ @sth.finish
205
+ end
206
+ end