rails-dbi 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/ChangeLog +3694 -0
- data/LICENSE +25 -0
- data/README +271 -0
- data/bin/dbi +518 -0
- data/bin/test_broken_dbi +37 -0
- data/build/Rakefile.dbi.rb +60 -0
- data/examples/test1.pl +39 -0
- data/examples/test1.rb +20 -0
- data/examples/xmltest.rb +8 -0
- data/lib/dbi/base_classes/database.rb +135 -0
- data/lib/dbi/base_classes/driver.rb +47 -0
- data/lib/dbi/base_classes/statement.rb +171 -0
- data/lib/dbi/base_classes.rb +12 -0
- data/lib/dbi/binary.rb +25 -0
- data/lib/dbi/columninfo.rb +107 -0
- data/lib/dbi/exceptions.rb +65 -0
- data/lib/dbi/handles/database.rb +241 -0
- data/lib/dbi/handles/driver.rb +60 -0
- data/lib/dbi/handles/statement.rb +408 -0
- data/lib/dbi/handles.rb +49 -0
- data/lib/dbi/row.rb +270 -0
- data/lib/dbi/sql/preparedstatement.rb +115 -0
- data/lib/dbi/sql.rb +22 -0
- data/lib/dbi/sql_type_constants.rb +75 -0
- data/lib/dbi/trace.rb +91 -0
- data/lib/dbi/types.rb +218 -0
- data/lib/dbi/typeutil.rb +109 -0
- data/lib/dbi/utils/date.rb +59 -0
- data/lib/dbi/utils/tableformatter.rb +112 -0
- data/lib/dbi/utils/time.rb +52 -0
- data/lib/dbi/utils/timestamp.rb +96 -0
- data/lib/dbi/utils/xmlformatter.rb +73 -0
- data/lib/dbi/utils.rb +60 -0
- data/lib/dbi.rb +337 -0
- data/test/dbi/tc_columninfo.rb +94 -0
- data/test/dbi/tc_date.rb +88 -0
- data/test/dbi/tc_dbi.rb +184 -0
- data/test/dbi/tc_row.rb +256 -0
- data/test/dbi/tc_sqlbind.rb +168 -0
- data/test/dbi/tc_statementhandle.rb +29 -0
- data/test/dbi/tc_time.rb +77 -0
- data/test/dbi/tc_timestamp.rb +142 -0
- data/test/dbi/tc_types.rb +268 -0
- data/test/ts_dbi.rb +15 -0
- metadata +132 -0
data/bin/dbi
ADDED
@@ -0,0 +1,518 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Copyright (c) 2001, 2002 Michael Neumann <neumann@s-direktnet.de>
|
4
|
+
#
|
5
|
+
# All rights reserved.
|
6
|
+
#
|
7
|
+
# Redistribution and use in source and binary forms, with or without
|
8
|
+
# modification, are permitted provided that the following conditions
|
9
|
+
# are met:
|
10
|
+
# 1. Redistributions of source code must retain the above copyright
|
11
|
+
# notice, this list of conditions and the following disclaimer.
|
12
|
+
# 2. 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
|
+
# 3. The name of the author may not be used to endorse or promote products
|
16
|
+
# derived from this software without specific prior written permission.
|
17
|
+
#
|
18
|
+
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
19
|
+
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
20
|
+
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
21
|
+
# THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
22
|
+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
23
|
+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
24
|
+
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
25
|
+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
26
|
+
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
27
|
+
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28
|
+
#
|
29
|
+
# $Id: sqlsh.rb,v 1.2 2006/01/24 04:20:01 francis Exp $
|
30
|
+
#
|
31
|
+
|
32
|
+
begin
|
33
|
+
require 'rubygems'
|
34
|
+
gem 'dbi'
|
35
|
+
rescue LoadError => e
|
36
|
+
end
|
37
|
+
|
38
|
+
require "dbi"
|
39
|
+
|
40
|
+
begin
|
41
|
+
require "readline"
|
42
|
+
$use_readline = true
|
43
|
+
rescue LoadError
|
44
|
+
$use_readline = false
|
45
|
+
end
|
46
|
+
|
47
|
+
require "irb"
|
48
|
+
require "irb/completion"
|
49
|
+
|
50
|
+
$paging = true
|
51
|
+
$irb_completion = Readline.completion_proc
|
52
|
+
|
53
|
+
require "getoptlong"
|
54
|
+
|
55
|
+
class ReadlineControl
|
56
|
+
|
57
|
+
attr_accessor :keywords
|
58
|
+
|
59
|
+
def initialize
|
60
|
+
@keywords = []
|
61
|
+
set_prompt
|
62
|
+
initCompletion
|
63
|
+
end
|
64
|
+
|
65
|
+
def initCompletion
|
66
|
+
if $use_readline
|
67
|
+
Readline.completion_proc = proc {|str| complete(str) }
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def complete(str)
|
72
|
+
@keywords.grep(/^#{Regexp.escape(str)}/i)
|
73
|
+
end
|
74
|
+
|
75
|
+
def set_prompt(prompt="> ")
|
76
|
+
@prompt = prompt
|
77
|
+
end
|
78
|
+
|
79
|
+
def readline
|
80
|
+
if $use_readline
|
81
|
+
Readline.readline(@prompt, true)
|
82
|
+
else
|
83
|
+
print @prompt
|
84
|
+
$stdin.readline
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
class Command
|
91
|
+
|
92
|
+
def tokens(sql)
|
93
|
+
DBI::SQL::PreparedStatement.tokens(sql)
|
94
|
+
end
|
95
|
+
|
96
|
+
def readCommand
|
97
|
+
line = ""
|
98
|
+
|
99
|
+
$rd.set_prompt(PROMPT)
|
100
|
+
begin
|
101
|
+
if $input.nil?
|
102
|
+
# no source file to read from
|
103
|
+
l = $rd.readline
|
104
|
+
else
|
105
|
+
# source file has still data
|
106
|
+
l = $input.gets
|
107
|
+
if l.nil?
|
108
|
+
$input = nil
|
109
|
+
next
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
next if l.strip.empty?
|
114
|
+
l = l.chomp + "\n"
|
115
|
+
line << l
|
116
|
+
|
117
|
+
puts $file + INPUT + l unless $input.nil?
|
118
|
+
$rd.set_prompt(PROMPT_CONT)
|
119
|
+
end until complete?(line)
|
120
|
+
|
121
|
+
return line.strip
|
122
|
+
end
|
123
|
+
|
124
|
+
private
|
125
|
+
|
126
|
+
def complete?(line)
|
127
|
+
line =~ /^\s*\\/ or (tokens(line).last || "") =~ /;\s*$/
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
class Actions
|
132
|
+
ACTIONS = [
|
133
|
+
[ /^\\q(uit)?\s*$/i, :quit ],
|
134
|
+
[ /^\\h(elp)?\s*$/i, :help ],
|
135
|
+
[ /^\\t(ables)?/i, :tables ],
|
136
|
+
[ /^\\dt/i, :describeTable ],
|
137
|
+
[ /^\\s(elect)?/i, :select ],
|
138
|
+
|
139
|
+
[ /^\\rb/i, :ruby ],
|
140
|
+
[ /^\\irb/i, :irb ],
|
141
|
+
|
142
|
+
[ /^\\c(ommit)?\s*$/i, :commit ],
|
143
|
+
[ /^\\r(ollback)?\s*$/i, :rollback ],
|
144
|
+
[ /^\\a(utocommit)?(\s+(on|off)?)?\s*$/i, :autocommit ],
|
145
|
+
[ /^\\i(nput)?/i, :input ],
|
146
|
+
[ /^\\o(utput)?/i, :output ],
|
147
|
+
[ /^\\pl/i, :pageLength ],
|
148
|
+
[ /^\\p/i, :togglePaging ],
|
149
|
+
|
150
|
+
[ //, :unknownCommand ]
|
151
|
+
]
|
152
|
+
|
153
|
+
def dispatchCommand(line)
|
154
|
+
ACTIONS.each do |regexp, action|
|
155
|
+
if line =~ regexp then
|
156
|
+
send(action, $~)
|
157
|
+
return
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def quit(match)
|
163
|
+
puts
|
164
|
+
puts "BYE"
|
165
|
+
puts
|
166
|
+
|
167
|
+
begin
|
168
|
+
Conn.disconnect
|
169
|
+
rescue DBI::Error => err
|
170
|
+
puts
|
171
|
+
puts err.message
|
172
|
+
p err.backtrace if $DEBUG
|
173
|
+
puts
|
174
|
+
end
|
175
|
+
|
176
|
+
exit
|
177
|
+
end
|
178
|
+
|
179
|
+
def help(match)
|
180
|
+
head = %w(Function Description)
|
181
|
+
rows = [
|
182
|
+
["\\h[elp]", "Display this help screen"],
|
183
|
+
["", ""],
|
184
|
+
|
185
|
+
["\\t[ables]", "Display all available tables"],
|
186
|
+
["\\dt table", "Describe columns of 'table'"],
|
187
|
+
["\\s[elect] table", "short for SELECT * FROM 'table'"],
|
188
|
+
|
189
|
+
["", ""],
|
190
|
+
["\\c[ommit]", "Commits the current transaction"],
|
191
|
+
["\\r[ollback]", "Rolls back the current transaction"],
|
192
|
+
["\\a[utocommit]", "Show current autocommit mode"],
|
193
|
+
["\\a[utocommit] on|off", "Switch autocommit mode on/off"],
|
194
|
+
["", ""],
|
195
|
+
|
196
|
+
["\\i[nput] filename", "Read and execute lines from 'filename'"],
|
197
|
+
["\\o[utput]", "Disable output"],
|
198
|
+
["\\o[utput] filename", "Store SQL statments the user inputs into 'filename'"],
|
199
|
+
["", ""],
|
200
|
+
|
201
|
+
["\\pl n", "Set page length to 'n'"],
|
202
|
+
["\\p", "Toggle paging"],
|
203
|
+
["", ""],
|
204
|
+
["\\rb ...", "Execute the rest of the line as Ruby sourcecode"],
|
205
|
+
["\\irb", "Execute irb within this context"],
|
206
|
+
|
207
|
+
["", ""],
|
208
|
+
|
209
|
+
["\\q[uit]", "Quit this program"]
|
210
|
+
]
|
211
|
+
|
212
|
+
puts
|
213
|
+
puts "Help: "
|
214
|
+
output_table(head, rows)
|
215
|
+
puts
|
216
|
+
end
|
217
|
+
|
218
|
+
def tables(match)
|
219
|
+
head = ["Table name"]
|
220
|
+
rows = Conn.tables.collect {|name| [name]}
|
221
|
+
|
222
|
+
puts
|
223
|
+
puts "Tables: "
|
224
|
+
output_table(head, rows)
|
225
|
+
puts
|
226
|
+
end
|
227
|
+
|
228
|
+
def describeTable(match)
|
229
|
+
table = match.post_match.strip
|
230
|
+
|
231
|
+
head = %w(name type_name precision scale default nullable indexed primary unique)
|
232
|
+
|
233
|
+
rows = Conn.columns(table).collect {|col| head.collect{|a| col[a]} }
|
234
|
+
|
235
|
+
puts
|
236
|
+
puts "Table '#{table}': "
|
237
|
+
output_table(head, rows)
|
238
|
+
puts
|
239
|
+
end
|
240
|
+
|
241
|
+
def select(match)
|
242
|
+
executeSQL("SELECT * FROM #{match.post_match};")
|
243
|
+
end
|
244
|
+
|
245
|
+
def commit(match)
|
246
|
+
Conn.commit
|
247
|
+
puts
|
248
|
+
puts "COMMIT"
|
249
|
+
puts
|
250
|
+
end
|
251
|
+
|
252
|
+
def rollback(match)
|
253
|
+
Conn.rollback
|
254
|
+
puts
|
255
|
+
puts "ROLLBACK"
|
256
|
+
puts
|
257
|
+
end
|
258
|
+
|
259
|
+
def autocommit(match)
|
260
|
+
mode = match[3]
|
261
|
+
if mode =~ /on/i
|
262
|
+
Conn['AutoCommit'] = true
|
263
|
+
puts
|
264
|
+
puts "AUTOCOMMIT IS NOW ON"
|
265
|
+
puts
|
266
|
+
elsif mode =~ /off/i
|
267
|
+
Conn['AutoCommit'] = false
|
268
|
+
puts
|
269
|
+
puts "AUTOCOMMIT IS NOW OFF"
|
270
|
+
puts
|
271
|
+
else
|
272
|
+
puts
|
273
|
+
if Conn['AutoCommit'] == true
|
274
|
+
puts "AUTOCOMMIT is currently switched ON"
|
275
|
+
elsif Conn['AutoCommit'] == false
|
276
|
+
puts "AUTOCOMMIT is currently switched OFF"
|
277
|
+
else
|
278
|
+
puts "AUTOCOMMIT is in unknown state"
|
279
|
+
end
|
280
|
+
puts
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
def input(match)
|
285
|
+
puts
|
286
|
+
$file = match.post_match.strip
|
287
|
+
|
288
|
+
begin
|
289
|
+
$input = File.open($file)
|
290
|
+
puts "EXECUTE file #{$file}"
|
291
|
+
puts
|
292
|
+
rescue
|
293
|
+
puts "Couldn't read from file #{$file}"
|
294
|
+
puts
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
def output(match)
|
299
|
+
puts
|
300
|
+
file = match.post_match.strip
|
301
|
+
|
302
|
+
if file.empty?
|
303
|
+
$output.close if $output
|
304
|
+
$output = nil
|
305
|
+
puts "Disabled OUTPUT"
|
306
|
+
puts
|
307
|
+
else
|
308
|
+
begin
|
309
|
+
$output = File.new(file, "w+")
|
310
|
+
puts "Set OUTPUT to file #{file}"
|
311
|
+
puts
|
312
|
+
rescue
|
313
|
+
puts "Couldn't set OUTPUT to file #{file}"
|
314
|
+
puts
|
315
|
+
end
|
316
|
+
end
|
317
|
+
end
|
318
|
+
|
319
|
+
def togglePaging(match)
|
320
|
+
$paging = !$paging
|
321
|
+
|
322
|
+
puts "Paging is now " + ($paging ? "on" : "off") + "."
|
323
|
+
end
|
324
|
+
|
325
|
+
def pageLength(match)
|
326
|
+
puts
|
327
|
+
$page_len = match.post_match.strip.to_i
|
328
|
+
$page_len = DEFAULT_PAGE_LENGTH if $page_len <= 0
|
329
|
+
|
330
|
+
puts "New page length is #{$page_len}."
|
331
|
+
puts
|
332
|
+
end
|
333
|
+
|
334
|
+
def irb(match)
|
335
|
+
Readline.completion_proc = $irb_completion
|
336
|
+
puts
|
337
|
+
puts "================================== IRB ==============================="
|
338
|
+
begin
|
339
|
+
IRB.start
|
340
|
+
rescue SystemExit
|
341
|
+
end
|
342
|
+
puts "======================================================================"
|
343
|
+
$rd.initCompletion
|
344
|
+
end
|
345
|
+
|
346
|
+
def ruby(match)
|
347
|
+
puts
|
348
|
+
eval match.post_match
|
349
|
+
puts
|
350
|
+
end
|
351
|
+
|
352
|
+
def unknownCommand(match)
|
353
|
+
puts
|
354
|
+
puts "Unknown command!"
|
355
|
+
puts
|
356
|
+
end
|
357
|
+
|
358
|
+
end
|
359
|
+
|
360
|
+
def output_table(header, rows)
|
361
|
+
DBI::Utils::TableFormatter.ascii(header, rows, nil, nil, nil, nil, $page_len) do
|
362
|
+
if $paging
|
363
|
+
print "[enter to continue, a to abort]"
|
364
|
+
break if $stdin.readline.chomp.downcase == "a"
|
365
|
+
end
|
366
|
+
end
|
367
|
+
end
|
368
|
+
|
369
|
+
def executeSQL(sql)
|
370
|
+
sql = $` if sql =~ /;\s*$/
|
371
|
+
|
372
|
+
start = ::Time.now
|
373
|
+
stmt = Conn.execute(sql)
|
374
|
+
|
375
|
+
head = stmt.column_names
|
376
|
+
|
377
|
+
# DDL, DCL
|
378
|
+
if head.empty?
|
379
|
+
puts
|
380
|
+
nr = stmt.rows
|
381
|
+
if nr == 0
|
382
|
+
puts " No rows affected"
|
383
|
+
elsif nr == 1
|
384
|
+
puts " 1 row affected"
|
385
|
+
else
|
386
|
+
puts " #{nr} rows affected"
|
387
|
+
end
|
388
|
+
puts
|
389
|
+
else
|
390
|
+
rows = stmt.fetch_all
|
391
|
+
tm = ::Time.now - start
|
392
|
+
|
393
|
+
puts
|
394
|
+
output_table(head, rows || [])
|
395
|
+
print " "
|
396
|
+
if rows.nil?
|
397
|
+
print "No rows in set"
|
398
|
+
elsif rows.size == 1
|
399
|
+
print "1 row in set"
|
400
|
+
else
|
401
|
+
print "#{rows.size} rows in set"
|
402
|
+
end
|
403
|
+
|
404
|
+
puts " (#{(tm.to_f*1000).to_i / 1000.0} sec)"
|
405
|
+
puts
|
406
|
+
end
|
407
|
+
|
408
|
+
$rd.keywords = SQL_KEYWORDS + Conn.tables
|
409
|
+
end
|
410
|
+
|
411
|
+
DEFAULT_PAGE_LENGTH = 37
|
412
|
+
|
413
|
+
$output = nil
|
414
|
+
$input = nil
|
415
|
+
$page_len = DEFAULT_PAGE_LENGTH
|
416
|
+
PROMPT = "dbi => "
|
417
|
+
PROMPT_CONT = "dbi -> "
|
418
|
+
INPUT = " >> "
|
419
|
+
|
420
|
+
SQL_KEYWORDS = %w(
|
421
|
+
INSERT DELETE UPDATE SELECT FROM WHERE IN LIKE SET VALUES INTO
|
422
|
+
CREATE TABLE DROP
|
423
|
+
COMMIT ROLLBACK
|
424
|
+
CHAR VARCHAR VARCHAR2 INT INTEGER NUMBER FLOAT REAL LONG CLOB BLOB DECIMAL
|
425
|
+
DBCLOB DBBLOB
|
426
|
+
)
|
427
|
+
|
428
|
+
# ---------------------------------------------------------------------------
|
429
|
+
|
430
|
+
opts = GetoptLong.new(
|
431
|
+
["--file", "-f", GetoptLong::REQUIRED_ARGUMENT ]
|
432
|
+
)
|
433
|
+
opts.each do |opt, arg|
|
434
|
+
case opt
|
435
|
+
when "--file"
|
436
|
+
$input_file_name = arg
|
437
|
+
end
|
438
|
+
end
|
439
|
+
|
440
|
+
if ARGV.size < 1 or ARGV.size > 3
|
441
|
+
puts
|
442
|
+
puts "USAGE: #{$0} [--file file] driver_url [user [password] ]"
|
443
|
+
puts
|
444
|
+
|
445
|
+
puts "Available driver and datasources:"
|
446
|
+
puts
|
447
|
+
for driver in DBI.available_drivers do
|
448
|
+
puts driver
|
449
|
+
begin
|
450
|
+
ds = DBI.data_sources(driver)
|
451
|
+
for datasource in ds
|
452
|
+
puts " " + datasource
|
453
|
+
end
|
454
|
+
rescue => err
|
455
|
+
end
|
456
|
+
puts
|
457
|
+
end
|
458
|
+
puts
|
459
|
+
|
460
|
+
exit 1
|
461
|
+
else
|
462
|
+
DRIVER_URL = ARGV.shift
|
463
|
+
USER = ARGV.shift
|
464
|
+
PASS = ARGV.shift
|
465
|
+
end
|
466
|
+
|
467
|
+
puts
|
468
|
+
begin
|
469
|
+
Conn = DBI.connect(DRIVER_URL, USER, PASS)
|
470
|
+
print "CONNECT TO #{DRIVER_URL} "
|
471
|
+
print "USER #{USER} " unless USER.nil?
|
472
|
+
print "PASS #{PASS} " unless PASS.nil?
|
473
|
+
print "\n"
|
474
|
+
|
475
|
+
rescue DBI::Error, DBI::Warning => err
|
476
|
+
p err
|
477
|
+
exit
|
478
|
+
end
|
479
|
+
|
480
|
+
puts
|
481
|
+
|
482
|
+
$rd = ReadlineControl.new
|
483
|
+
$rd.keywords = SQL_KEYWORDS + Conn.tables
|
484
|
+
|
485
|
+
cmd = Command.new
|
486
|
+
act = Actions.new
|
487
|
+
|
488
|
+
# --file option
|
489
|
+
if $input_file_name
|
490
|
+
def $input_file_name.post_match
|
491
|
+
$input_file_name
|
492
|
+
end
|
493
|
+
act.input($input_file_name)
|
494
|
+
end
|
495
|
+
|
496
|
+
# Main-Loop -----------------------------------
|
497
|
+
|
498
|
+
loop do
|
499
|
+
line = cmd.readCommand
|
500
|
+
|
501
|
+
$output.puts line unless $output.nil?
|
502
|
+
|
503
|
+
begin
|
504
|
+
if line =~ /^\\/ then
|
505
|
+
# Internal Command
|
506
|
+
act.dispatchCommand(line)
|
507
|
+
else
|
508
|
+
# SQL Command
|
509
|
+
executeSQL(line)
|
510
|
+
end
|
511
|
+
rescue DBI::Error => err
|
512
|
+
puts
|
513
|
+
puts err.message
|
514
|
+
p err.backtrace if $DEBUG
|
515
|
+
puts
|
516
|
+
end
|
517
|
+
end
|
518
|
+
|
data/bin/test_broken_dbi
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
load_path = false
|
4
|
+
gems = false
|
5
|
+
redefined = false
|
6
|
+
|
7
|
+
# these clauses are for installations which have RUBYOPT=-rubygems, etc.
|
8
|
+
if Object.const_defined? "Gem"
|
9
|
+
redefined = true
|
10
|
+
module Kernel
|
11
|
+
alias gem_require require
|
12
|
+
alias require gem_original_require
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
begin
|
17
|
+
require 'dbi'
|
18
|
+
load_path = true
|
19
|
+
rescue LoadError => e
|
20
|
+
end
|
21
|
+
|
22
|
+
if Object.const_defined? "Gem" and redefined
|
23
|
+
module Kernel
|
24
|
+
alias gem_original_require require
|
25
|
+
alias require gem_require
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
begin
|
30
|
+
require 'rubygems'
|
31
|
+
gem 'dbi'
|
32
|
+
gems = true
|
33
|
+
rescue LoadError
|
34
|
+
rescue Gem::LoadError
|
35
|
+
end
|
36
|
+
|
37
|
+
puts "Your installation of DBI is broken" if gems and load_path
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'rake_task_lib'
|
2
|
+
require 'dbi'
|
3
|
+
|
4
|
+
DBD_PACKAGES = Dir['lib/dbd/*.rb'].collect { |x| File.basename(x, '.rb') }
|
5
|
+
|
6
|
+
# creates a number of tasks like dbi:task_name, dbd_mysql:task_name, so on.
|
7
|
+
# Builds these out into an array that can be used as a prereq for other tasks.
|
8
|
+
def map_task(task_name)
|
9
|
+
namespaces = (['dbi'] + DBD_PACKAGES.collect { |x| dbd_namespace(x) }).flatten
|
10
|
+
namespaces.collect { |x| [x, task_name].join(":") }
|
11
|
+
end
|
12
|
+
|
13
|
+
task :package => (map_task("package") + map_task("gem"))
|
14
|
+
task :clobber_package => map_task("clobber_package")
|
15
|
+
|
16
|
+
desc 'Run interface tests (no database connectivity required)'
|
17
|
+
task :test_dbi do
|
18
|
+
ruby("test/ts_dbi.rb")
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'Run database-specific tests'
|
22
|
+
task :test_dbd do
|
23
|
+
ruby("test/ts_dbd.rb")
|
24
|
+
end
|
25
|
+
|
26
|
+
desc 'Run full test suite'
|
27
|
+
task :test => [:test_dbi, :test_dbd]
|
28
|
+
|
29
|
+
build_dbi_tasks
|
30
|
+
|
31
|
+
#
|
32
|
+
# There's probably a better way to do this, but here's a boilerplate spec that we dup and modify.
|
33
|
+
#
|
34
|
+
|
35
|
+
task :dbi => DEFAULT_TASKS.collect { |x| "dbi:#{x.to_s}" }
|
36
|
+
|
37
|
+
namespace :dbi do
|
38
|
+
code_files = %w(examples/**/* bin/dbi build/Rakefile.dbi.rb lib/dbi.rb lib/dbi/**/*.rb test/ts_dbi.rb test/dbi/*)
|
39
|
+
|
40
|
+
spec = boilerplate_spec
|
41
|
+
spec.name = 'dbi'
|
42
|
+
spec.version = DBI::VERSION
|
43
|
+
spec.test_file = 'test/ts_dbi.rb'
|
44
|
+
spec.executables = ['dbi', 'test_broken_dbi']
|
45
|
+
spec.files = gem_files(code_files)
|
46
|
+
spec.summary = 'A vendor independent interface for accessing databases, similar to Perl\'s DBI'
|
47
|
+
spec.description = 'A vendor independent interface for accessing databases, similar to Perl\'s DBI'
|
48
|
+
spec.add_dependency 'deprecated', '= 2.0.1'
|
49
|
+
|
50
|
+
build_package_tasks(spec, code_files)
|
51
|
+
end
|
52
|
+
|
53
|
+
DBD_PACKAGES.each do |dbd|
|
54
|
+
my_namespace = dbd_namespace(dbd)
|
55
|
+
|
56
|
+
task my_namespace => DEFAULT_TASKS.collect { |x| "#{my_namespace}:#{x.to_s}" }
|
57
|
+
namespace my_namespace do
|
58
|
+
build_dbd_tasks(dbd)
|
59
|
+
end
|
60
|
+
end
|
data/examples/test1.pl
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
use DBI;
|
2
|
+
|
3
|
+
$dbh = DBI->connect("dbi:Oracle:oracle.neumann", "scott", "tiger", {RaiseError => 1, AutoCommit => 0} );
|
4
|
+
|
5
|
+
|
6
|
+
$dbh->do("DROP TABLE MYTEST");
|
7
|
+
$dbh->do("CREATE TABLE MYTEST (a INT, b VARCHAR2(256), c FLOAT, d VARCHAR2(256))");
|
8
|
+
|
9
|
+
$sth = $dbh->prepare("INSERT INTO MYTEST VALUES (:1, :2, :3, :4)");
|
10
|
+
|
11
|
+
$i = 1;
|
12
|
+
|
13
|
+
while ($i <= 10000) {
|
14
|
+
|
15
|
+
$sth->execute($i, "Michael der $i. von Neumann", 5.6 * $i, "HALLO LEUTE WIE GEHTS DENN SO?");
|
16
|
+
|
17
|
+
$i = $i + 1;
|
18
|
+
#print $i, "\n";
|
19
|
+
}
|
20
|
+
|
21
|
+
|
22
|
+
$dbh->commit();
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
#$sth = $dbh->prepare("SELECT * FROM EMP");
|
29
|
+
|
30
|
+
#$sth->execute();
|
31
|
+
|
32
|
+
#while (@row = $sth->fetchrow_array()) {
|
33
|
+
# print(@row);
|
34
|
+
#}
|
35
|
+
|
36
|
+
|
37
|
+
$dbh->disconnect();
|
38
|
+
|
39
|
+
|
data/examples/test1.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "dbi"
|
2
|
+
|
3
|
+
dbh = DBI.connect("dbi:Oracle:oracle.neumann", "scott", "tiger", 'AutoCommit' => false)
|
4
|
+
|
5
|
+
dbh.do("DROP TABLE MYTEST")
|
6
|
+
dbh.do("CREATE TABLE MYTEST (a INT, b VARCHAR2(256), c FLOAT, d VARCHAR2(256))")
|
7
|
+
|
8
|
+
sth = dbh.prepare("INSERT INTO MYTEST VALUES (:1, :2, :3, :4)")
|
9
|
+
|
10
|
+
1.upto(10000) do |i|
|
11
|
+
sth.execute(i.to_s, "Michael der #{i}. von Neumann", (5.6 * i).to_s, "HALLO LEUTE WIE GEHTS DENN SO?")
|
12
|
+
#print i, "\n"
|
13
|
+
end
|
14
|
+
|
15
|
+
sth.finish
|
16
|
+
|
17
|
+
dbh.commit
|
18
|
+
|
19
|
+
dbh.disconnect
|
20
|
+
|