File: rubydbi.rb

package info (click to toggle)
sqlrelay 1%3A0.37.1-3.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 13,084 kB
  • ctags: 6,691
  • sloc: cpp: 48,136; python: 10,118; ansic: 9,673; java: 9,195; php: 8,839; perl: 8,827; sh: 8,554; ruby: 8,516; tcl: 5,039; makefile: 3,665
file content (193 lines) | stat: -rwxr-xr-x 3,140 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env ruby

# Copyright (c) 2001  David Muse
# See the file COPYING for more information.

require 'dbi'

# usage

if ARGV.length < 7
	print "usage: rubydbi.rb host port socket user password query iterations"
	exit(0)
end

for index in 0..ARGV[6].to_i-1 do


	# instantiation
	db=DBI.connect("DBI:SQLRelay:host="+ARGV[0]+";port="+
			ARGV[1]+";socket="+ARGV[2]+";",ARGV[3],ARGV[4])


	# debug and autocommit attributes
	db["sqlrelay_debug"]=true
	db["AutoCommit"]=true


	# query functions
	stmt=db.prepare(ARGV[5])

	print "FETCH ALL\n"
	stmt.execute()
	for i in stmt.fetch_all() do
		for j in i do
			print j+" "
		end
		print "\n"
	end
	print "\n\n"


	print "FETCH ONE\n"
	stmt.execute()
	for j in stmt.fetch() do
		print j+" "
	end
	print "\n\n"


	print "FETCH MANY\n"
	for i in stmt.fetch_many(5) do
		for j in i do
			print j+" "
		end
		print "\n"
	end
	print "\n\n"


	print "FETCH SCROLL\n"
	print "NEXT\n"
	for j in stmt.fetch_scroll(DBI::SQL_FETCH_NEXT) do
		print j+" "
	end
	print "\n"
	print "PRIOR\n"
	for j in stmt.fetch_scroll(DBI::SQL_FETCH_PRIOR) do
		print j+" "
	end
	print "\n"
	print "FIRST\n"
	for j in stmt.fetch_scroll(DBI::SQL_FETCH_FIRST) do
		print j+" "
	end
	print "\n"
	print "LAST\n"
	for j in stmt.fetch_scroll(DBI::SQL_FETCH_LAST) do
		print j+" "
	end
	print "\n"
	print "ABSOLUTE 0\n"
	for j in stmt.fetch_scroll(DBI::SQL_FETCH_ABSOLUTE,0) do
		print j+" "
	end
	print "\n"
	print "RELATIVE 5\n"
	for j in stmt.fetch_scroll(DBI::SQL_FETCH_RELATIVE,5) do
		print j+" "
	end
	print "\n\n"


	# row count
	print "ROWS\n"
	print stmt.rows()
	print "\n\n"


	# column names
	print "COLUMNS\n"
	columns=stmt.column_info()
	for i in columns do
		print i['name']+":"+i['type_name']+":"
		print i['precision']
		print "\n"
	end
	print "\n\n"


	# bind functions
	print "BIND FUNCTIONS\n"
	stmt=db.prepare("select :var1,:var2,:var3 from dual")

	stmt.bind_param("var1",1,false)
	stmt.bind_param("var2","hello",false)
	stmt.bind_param("var3",1.1,false)
	stmt.execute()
	rows=stmt.fetch_all()
	for i in rows do
		for j in i do
			print j+" "
		end
		print "\n"
	end

	stmt.bind_param("var1",2,false)
	stmt.bind_param("var2","hi",false)
	stmt.bind_param("var3",2.22,false)
	stmt.execute()
	rows=stmt.fetch_all()
	for i in rows do
		for j in i do
			print j+" "
		end
		print "\n"
	end

	stmt.bind_param("var1",3,false)
	stmt.bind_param("var2","bye",false)
	stmt.bind_param("var3",3.333,{'precision'=>1,'scale'=>4})
	stmt.execute()
	rows=stmt.fetch_all()
	for i in rows do
		for j in i do
			print j+" "
		end
		print "\n"
	end
	print "\n\n"
	

	# ping
	print "PING\n"
	if db.ping()
		print "ping succeeded\n"
	else
		print "ping failed\n"
	end
	print "\n\n"


	# commit and rollback
	print "COMMIT\n"
	db.commit()
	print "\n\n"

	print "ROLLBACK\n"
	db.rollback()
	print "\n\n"


	# error message
	print "ERROR HANDLING\n"
	begin
		badstmt=db.prepare("invalid query")
		badstmt.execute()
	rescue DBI::ProgrammingError => error
		print error
		print "\n"
	end

    
	# invalid attribute
	begin
		db["invalid"]=true
	rescue DBI::NotSupportedError => error
		print error
		print "\n"
	end


	GC::start()
end