fb 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README +218 -0
- data/extconf.rb +48 -0
- data/fb.c +3008 -0
- data/test/ConnectionTestCases.rb +462 -0
- data/test/CursorTestCases.rb +235 -0
- data/test/DataTypesTestCases.rb +428 -0
- data/test/DatabaseTestCases.rb +180 -0
- data/test/FbTestCases.rb +33 -0
- data/test/FbTestSuite.rb +8 -0
- data/test/TransactionTestCases.rb +259 -0
- metadata +67 -0
@@ -0,0 +1,235 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/FbTestCases'
|
3
|
+
require 'fb'
|
4
|
+
include Fb
|
5
|
+
|
6
|
+
class CursorTestCases < Test::Unit::TestCase
|
7
|
+
include FbTestCases
|
8
|
+
|
9
|
+
def test_fetch_array
|
10
|
+
Database.create(@parms) do |connection|
|
11
|
+
connection.execute("select * from rdb$database") do |cursor|
|
12
|
+
assert_instance_of Cursor, cursor
|
13
|
+
row = cursor.fetch :array
|
14
|
+
assert_instance_of Array, row
|
15
|
+
assert_equal 4, row.size
|
16
|
+
end
|
17
|
+
connection.drop
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_fetch_hash
|
22
|
+
Database.create(@parms) do |connection|
|
23
|
+
connection.execute("select * from rdb$database") do |cursor|
|
24
|
+
assert_instance_of Cursor, cursor
|
25
|
+
row = cursor.fetch :hash
|
26
|
+
assert_instance_of Hash, row
|
27
|
+
assert_equal 4, row.size
|
28
|
+
end
|
29
|
+
connection.drop
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_fetch_all_array
|
34
|
+
Database.create(@parms) do |connection|
|
35
|
+
connection.execute("select * from rdb$database") do |cursor|
|
36
|
+
assert_instance_of Cursor, cursor
|
37
|
+
rows = cursor.fetchall :array
|
38
|
+
assert_instance_of Array, rows
|
39
|
+
assert_equal 1, rows.size
|
40
|
+
assert_instance_of Array, rows[0]
|
41
|
+
assert_equal 4, rows[0].size
|
42
|
+
end
|
43
|
+
connection.drop
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_fetch_all_hash
|
48
|
+
Database.create(@parms) do |connection|
|
49
|
+
connection.execute("select * from rdb$database") do |cursor|
|
50
|
+
assert_instance_of Cursor, cursor
|
51
|
+
rows = cursor.fetchall :hash
|
52
|
+
assert_instance_of Array, rows
|
53
|
+
assert_equal 1, rows.size
|
54
|
+
assert_instance_of Hash, rows[0]
|
55
|
+
assert_equal 4, rows[0].size
|
56
|
+
end
|
57
|
+
connection.drop
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_fields_array
|
62
|
+
Database.create(@parms) do |connection|
|
63
|
+
connection.execute("select * from rdb$database") do |cursor|
|
64
|
+
fields = cursor.fields
|
65
|
+
fields_ary = cursor.fields :array
|
66
|
+
assert_equal fields, fields_ary
|
67
|
+
assert_equal 4, fields.size
|
68
|
+
assert_equal "RDB$DESCRIPTION", fields[0].name;
|
69
|
+
assert_equal "RDB$RELATION_ID", fields[1].name;
|
70
|
+
assert_equal "RDB$SECURITY_CLASS", fields[2].name;
|
71
|
+
assert_equal "RDB$CHARACTER_SET_NAME", fields[3].name;
|
72
|
+
end
|
73
|
+
connection.drop
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_fields_array_downcased
|
78
|
+
Database.create(@parms.merge(:downcase_names => true)) do |connection|
|
79
|
+
connection.execute("select * from rdb$database") do |cursor|
|
80
|
+
fields = cursor.fields
|
81
|
+
fields_ary = cursor.fields :array
|
82
|
+
assert_equal fields, fields_ary
|
83
|
+
assert_equal 4, fields.size
|
84
|
+
assert_equal "rdb$description", fields[0].name;
|
85
|
+
assert_equal "rdb$relation_id", fields[1].name;
|
86
|
+
assert_equal "rdb$security_class", fields[2].name;
|
87
|
+
assert_equal "rdb$character_set_name", fields[3].name;
|
88
|
+
end
|
89
|
+
connection.drop
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_fields_hash
|
94
|
+
Database.create(@parms) do |connection|
|
95
|
+
connection.execute("select * from rdb$database") do |cursor|
|
96
|
+
fields = cursor.fields :hash
|
97
|
+
assert_equal 4, fields.size
|
98
|
+
assert_equal 520, fields["RDB$DESCRIPTION"].type_code
|
99
|
+
assert_equal 500, fields["RDB$RELATION_ID"].type_code
|
100
|
+
assert_equal 452, fields["RDB$SECURITY_CLASS"].type_code
|
101
|
+
assert_equal 452, fields["RDB$CHARACTER_SET_NAME"].type_code
|
102
|
+
end
|
103
|
+
connection.drop
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_fields_hash_downcased
|
108
|
+
Database.create(@parms.merge(:downcase_names => true)) do |connection|
|
109
|
+
connection.execute("select * from rdb$database") do |cursor|
|
110
|
+
fields = cursor.fields :hash
|
111
|
+
assert_equal 4, fields.size
|
112
|
+
assert_equal 520, fields["rdb$description"].type_code
|
113
|
+
assert_equal 500, fields["rdb$relation_id"].type_code
|
114
|
+
assert_equal 452, fields["rdb$security_class"].type_code
|
115
|
+
assert_equal 452, fields["rdb$character_set_name"].type_code
|
116
|
+
end
|
117
|
+
connection.drop
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_each_array
|
122
|
+
Database.create(@parms) do |connection|
|
123
|
+
connection.execute("select * from rdb$database") do |cursor|
|
124
|
+
count = 0
|
125
|
+
cursor.each :array do |row|
|
126
|
+
count += 1
|
127
|
+
assert_instance_of Array, row
|
128
|
+
assert_equal 4, row.size
|
129
|
+
end
|
130
|
+
assert_equal 1, count
|
131
|
+
end
|
132
|
+
connection.drop
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_each_hash
|
137
|
+
Database.create(@parms) do |connection|
|
138
|
+
connection.execute("select * from rdb$database") do |cursor|
|
139
|
+
count = 0
|
140
|
+
cursor.each :hash do |row|
|
141
|
+
count += 1
|
142
|
+
assert_instance_of Hash, row
|
143
|
+
assert_equal 4, row.size
|
144
|
+
end
|
145
|
+
assert_equal 1, count
|
146
|
+
end
|
147
|
+
connection.drop
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_fetch_after_nil
|
152
|
+
Database.create(@parms) do |connection|
|
153
|
+
connection.execute("create generator test_seq");
|
154
|
+
connection.execute("select gen_id(test_seq, 1) from rdb$database") do |cursor|
|
155
|
+
r1 = cursor.fetch
|
156
|
+
assert_not_nil r1
|
157
|
+
r2 = cursor.fetch
|
158
|
+
assert_nil r2
|
159
|
+
assert_raise Error do
|
160
|
+
r3 = cursor.fetch
|
161
|
+
end
|
162
|
+
end
|
163
|
+
connection.execute("select * from rdb$database") do |cursor|
|
164
|
+
r1 = cursor.fetch
|
165
|
+
assert_not_nil r1
|
166
|
+
r2 = cursor.fetch
|
167
|
+
assert_nil r2
|
168
|
+
assert_raise Error do
|
169
|
+
r3 = cursor.fetch
|
170
|
+
end
|
171
|
+
end
|
172
|
+
connection.drop
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
def test_fetch_hash_with_aliased_fields
|
177
|
+
sql = "SELECT RDB$DESCRIPTION DES, RDB$RELATION_ID REL, RDB$SECURITY_CLASS SEC, RDB$CHARACTER_SET_NAME FROM RDB$DATABASE"
|
178
|
+
Database.create(@parms) do |connection|
|
179
|
+
connection.execute(sql) do |cursor|
|
180
|
+
assert_instance_of Cursor, cursor
|
181
|
+
row = cursor.fetch :hash
|
182
|
+
assert_instance_of Hash, row
|
183
|
+
assert_equal 4, row.size
|
184
|
+
assert row.keys.include?("DES"), "No field DES"
|
185
|
+
assert row.keys.include?("REL"), "No field REL"
|
186
|
+
assert row.keys.include?("SEC"), "No field SEC"
|
187
|
+
assert row.keys.include?("RDB$CHARACTER_SET_NAME"), "No field RDB$CHARACTER_SET_NAME"
|
188
|
+
end
|
189
|
+
connection.drop
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
def test_simultaneous_cursors
|
194
|
+
sql_schema = <<-END
|
195
|
+
CREATE TABLE MASTER (ID INT, NAME1 VARCHAR(10));
|
196
|
+
CREATE TABLE DETAIL (ID INT, MASTER_ID INT, NAME2 VARCHAR(10));
|
197
|
+
END
|
198
|
+
sql_insert_master = "INSERT INTO MASTER (ID, NAME1) VALUES (?, ?)"
|
199
|
+
sql_insert_detail = "INSERT INTO DETAIL (ID, MASTER_ID, NAME2) VALUES (?, ?, ?)"
|
200
|
+
sql_select_master = "SELECT * FROM MASTER ORDER BY ID"
|
201
|
+
sql_select_detail = "SELECT * FROM DETAIL ORDER BY ID"
|
202
|
+
Database.create(@parms) do |connection|
|
203
|
+
connection.execute_script(sql_schema)
|
204
|
+
connection.transaction do
|
205
|
+
3.times do |m|
|
206
|
+
connection.execute(sql_insert_master, m, "name_#{m}")
|
207
|
+
end
|
208
|
+
9.times do |d|
|
209
|
+
connection.execute(sql_insert_detail, d, d / 3, "name_#{d / 3}_#{d}")
|
210
|
+
end
|
211
|
+
end
|
212
|
+
master = connection.execute(sql_select_master)
|
213
|
+
begin
|
214
|
+
detail = connection.execute(sql_select_detail)
|
215
|
+
begin
|
216
|
+
3.times do |m|
|
217
|
+
mr = master.fetch
|
218
|
+
assert_equal m, mr[0]
|
219
|
+
assert_equal "name_#{m}", mr[1]
|
220
|
+
3.times do |d|
|
221
|
+
dr = detail.fetch
|
222
|
+
assert_equal m * 3 + d, dr[0]
|
223
|
+
assert_equal m, dr[1]
|
224
|
+
assert_equal "name_#{m}_#{m * 3 + d}", dr[2]
|
225
|
+
end
|
226
|
+
end
|
227
|
+
ensure
|
228
|
+
detail.close
|
229
|
+
end
|
230
|
+
ensure
|
231
|
+
master.close
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
@@ -0,0 +1,428 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/FbTestCases'
|
3
|
+
require 'fb'
|
4
|
+
require 'date'
|
5
|
+
|
6
|
+
include Fb
|
7
|
+
|
8
|
+
class DataTypesTestCases < Test::Unit::TestCase
|
9
|
+
include FbTestCases
|
10
|
+
|
11
|
+
def gen_i(i)
|
12
|
+
i
|
13
|
+
end
|
14
|
+
|
15
|
+
def gen_si(i)
|
16
|
+
i
|
17
|
+
end
|
18
|
+
|
19
|
+
def gen_bi(i)
|
20
|
+
i * 1000000000
|
21
|
+
end
|
22
|
+
|
23
|
+
def gen_f(i)
|
24
|
+
i / 2
|
25
|
+
end
|
26
|
+
|
27
|
+
def gen_d(i)
|
28
|
+
i * 3333 / 2
|
29
|
+
end
|
30
|
+
|
31
|
+
def gen_c(i)
|
32
|
+
"%c" % (i + 64)
|
33
|
+
end
|
34
|
+
|
35
|
+
def gen_c10(i)
|
36
|
+
gen_c(i) * 5
|
37
|
+
end
|
38
|
+
|
39
|
+
def gen_vc(i)
|
40
|
+
gen_c(i)
|
41
|
+
end
|
42
|
+
|
43
|
+
def gen_vc10(i)
|
44
|
+
gen_c(i) * i
|
45
|
+
end
|
46
|
+
|
47
|
+
def gen_vc10000(i)
|
48
|
+
gen_c(i) * i * 1000
|
49
|
+
end
|
50
|
+
|
51
|
+
def gen_dt(i)
|
52
|
+
Date.civil(2000, i+1, i+1)
|
53
|
+
end
|
54
|
+
|
55
|
+
def gen_tm(i)
|
56
|
+
Time.utc(1990, 1, 1, 12, i, i)
|
57
|
+
end
|
58
|
+
|
59
|
+
def gen_ts(i)
|
60
|
+
Time.local(2006, 1, 1, i, i, i)
|
61
|
+
end
|
62
|
+
|
63
|
+
def gen_n92(i)
|
64
|
+
i * 100
|
65
|
+
end
|
66
|
+
|
67
|
+
def gen_d92(i)
|
68
|
+
i * 100
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_insert_basic_types
|
72
|
+
sql_schema = <<-END
|
73
|
+
create table TEST (
|
74
|
+
I INTEGER,
|
75
|
+
SI SMALLINT,
|
76
|
+
BI BIGINT,
|
77
|
+
F FLOAT,
|
78
|
+
D DOUBLE PRECISION,
|
79
|
+
C CHAR,
|
80
|
+
C10 CHAR(10),
|
81
|
+
VC VARCHAR(1),
|
82
|
+
VC10 VARCHAR(10),
|
83
|
+
VC10000 VARCHAR(10000),
|
84
|
+
DT DATE,
|
85
|
+
TM TIME,
|
86
|
+
TS TIMESTAMP,
|
87
|
+
N92 NUMERIC(9,2),
|
88
|
+
D92 DECIMAL(9,2));
|
89
|
+
END
|
90
|
+
sql_insert = <<-END
|
91
|
+
insert into test
|
92
|
+
(I, SI, BI, F, D, C, C10, VC, VC10, VC10000, DT, TM, TS, N92, D92)
|
93
|
+
values
|
94
|
+
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
|
95
|
+
END
|
96
|
+
sql_select = "select * from TEST order by I"
|
97
|
+
Database.create(@parms) do |connection|
|
98
|
+
connection.execute(sql_schema);
|
99
|
+
connection.transaction do
|
100
|
+
10.times do |i|
|
101
|
+
connection.execute(
|
102
|
+
sql_insert,
|
103
|
+
gen_i(i), gen_si(i), gen_bi(i),
|
104
|
+
gen_f(i), gen_d(i),
|
105
|
+
gen_c(i), gen_c10(i), gen_vc(i), gen_vc10(i), gen_vc10000(i),
|
106
|
+
gen_dt(i), gen_tm(i), gen_ts(i),
|
107
|
+
gen_n92(i), gen_d92(i))
|
108
|
+
end
|
109
|
+
end
|
110
|
+
connection.execute(sql_select) do |cursor|
|
111
|
+
i = 0
|
112
|
+
cursor.each :hash do |row|
|
113
|
+
assert_equal gen_i(i), row["I"], "INTEGER"
|
114
|
+
assert_equal gen_si(i), row["SI"], "SMALLINT"
|
115
|
+
assert_equal gen_bi(i), row["BI"], "BIGINT"
|
116
|
+
assert_equal gen_f(i), row["F"], "FLOAT"
|
117
|
+
assert_equal gen_d(i), row["D"], "DOUBLE PRECISION"
|
118
|
+
assert_equal gen_c(i), row["C"], "CHAR"
|
119
|
+
assert_equal gen_c10(i).ljust(10), row["C10"], "CHAR(10)"
|
120
|
+
assert_equal gen_vc(i), row["VC"], "VARCHAR(1)"
|
121
|
+
assert_equal gen_vc10(i), row["VC10"], "VARCHAR(10)"
|
122
|
+
assert_equal gen_vc10000(i), row["VC10000"], "VARCHAR(10000)"
|
123
|
+
assert_equal gen_dt(i), row["DT"], "DATE"
|
124
|
+
#assert_equal gen_tm(i).strftime("%H%M%S"), row["TM"].utc.strftime("%H%M%S"), "TIME"
|
125
|
+
assert_equal gen_ts(i), row["TS"], "TIMESTAMP"
|
126
|
+
assert_equal gen_n92(i), row["N92"], "NUMERIC"
|
127
|
+
assert_equal gen_d92(i), row["D92"], "DECIMAL"
|
128
|
+
i += 1
|
129
|
+
end
|
130
|
+
end
|
131
|
+
connection.drop
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_insert_blobs_text
|
136
|
+
sql_schema = "create table test (id int, name varchar(20), memo blob sub_type text)"
|
137
|
+
sql_insert = "insert into test (id, name, memo) values (?, ?, ?)"
|
138
|
+
sql_select = "select * from test order by id"
|
139
|
+
Database.create(@parms) do |connection|
|
140
|
+
connection.execute(sql_schema);
|
141
|
+
memo = IO.read("fb.c")
|
142
|
+
assert memo.size > 50000
|
143
|
+
connection.transaction do
|
144
|
+
10.times do |i|
|
145
|
+
connection.execute(sql_insert, i, i.to_s, memo);
|
146
|
+
end
|
147
|
+
end
|
148
|
+
connection.execute(sql_select) do |cursor|
|
149
|
+
i = 0
|
150
|
+
cursor.each :hash do |row|
|
151
|
+
assert_equal i, row["ID"]
|
152
|
+
assert_equal i.to_s, row["NAME"]
|
153
|
+
assert_equal memo, row["MEMO"]
|
154
|
+
i += 1
|
155
|
+
end
|
156
|
+
end
|
157
|
+
connection.drop
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_insert_blobs_binary
|
162
|
+
sql_schema = "create table test (id int, name varchar(20), attachment blob segment size 1000)"
|
163
|
+
sql_insert = "insert into test (id, name, attachment) values (?, ?, ?)"
|
164
|
+
sql_select = "select * from test order by id"
|
165
|
+
#filename = "data.dat"
|
166
|
+
filename = "fb.c"
|
167
|
+
Database.create(@parms) do |connection|
|
168
|
+
connection.execute(sql_schema);
|
169
|
+
attachment = File.open(filename,"rb") do |f|
|
170
|
+
f.read * 3
|
171
|
+
end
|
172
|
+
assert((attachment.size > 150000), "Not expected size")
|
173
|
+
connection.transaction do
|
174
|
+
3.times do |i|
|
175
|
+
connection.execute(sql_insert, i, i.to_s, attachment);
|
176
|
+
end
|
177
|
+
end
|
178
|
+
connection.execute(sql_select) do |cursor|
|
179
|
+
i = 0
|
180
|
+
cursor.each :array do |row|
|
181
|
+
assert_equal i, row[0], "ID's do not match"
|
182
|
+
assert_equal i.to_s, row[1], "NAME's do not match"
|
183
|
+
assert_equal attachment.size, row[2].size, "ATTACHMENT sizes do not match"
|
184
|
+
i += 1
|
185
|
+
end
|
186
|
+
end
|
187
|
+
connection.drop
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def test_insert_incorrect_types
|
192
|
+
cols = %w{ I SI BI F D C C10 VC VC10 VC10000 DT TM TS }
|
193
|
+
types = %w{ INTEGER SMALLINT BIGINT FLOAT DOUBLE\ PRECISION CHAR CHAR(10) VARCHAR(1) VARCHAR(10) VARCHAR(10000) DATE TIME TIMESTAMP }
|
194
|
+
sql_schema = "";
|
195
|
+
assert_equal cols.size, types.size
|
196
|
+
cols.size.times do |i|
|
197
|
+
sql_schema << "CREATE TABLE TEST_#{cols[i]} (VAL #{types[i]});\n"
|
198
|
+
end
|
199
|
+
Database.create(@parms) do |connection|
|
200
|
+
connection.execute_script(sql_schema)
|
201
|
+
cols.size.times do |i|
|
202
|
+
sql_insert = "INSERT INTO TEST_#{cols[i]} (VAL) VALUES (?);"
|
203
|
+
if cols[i] == 'I'
|
204
|
+
assert_raise TypeError do
|
205
|
+
connection.execute(sql_insert, {:five => "five"})
|
206
|
+
end
|
207
|
+
assert_raise TypeError do
|
208
|
+
connection.execute(sql_insert, Time.now)
|
209
|
+
end
|
210
|
+
assert_raise RangeError do
|
211
|
+
connection.execute(sql_insert, 5000000000)
|
212
|
+
end
|
213
|
+
elsif cols[i] == 'SI'
|
214
|
+
assert_raise TypeError do
|
215
|
+
connection.execute(sql_insert, {:five => "five"})
|
216
|
+
end
|
217
|
+
assert_raise TypeError do
|
218
|
+
connection.execute(sql_insert, Time.now)
|
219
|
+
end
|
220
|
+
assert_raise RangeError do
|
221
|
+
connection.execute(sql_insert, 100000)
|
222
|
+
end
|
223
|
+
elsif cols[i] == 'BI'
|
224
|
+
assert_raise TypeError do
|
225
|
+
connection.execute(sql_insert, {:five => "five"})
|
226
|
+
end
|
227
|
+
assert_raise TypeError do
|
228
|
+
connection.execute(sql_insert, Time.now)
|
229
|
+
end
|
230
|
+
assert_raise RangeError do
|
231
|
+
connection.execute(sql_insert, 184467440737095516160) # 2^64 * 10
|
232
|
+
end
|
233
|
+
elsif cols[i] == 'F'
|
234
|
+
assert_raise TypeError do
|
235
|
+
connection.execute(sql_insert, {:five => "five"})
|
236
|
+
end
|
237
|
+
assert_raise RangeError do
|
238
|
+
connection.execute(sql_insert, 10 ** 39)
|
239
|
+
end
|
240
|
+
elsif cols[i] == 'D'
|
241
|
+
assert_raise TypeError do
|
242
|
+
connection.execute(sql_insert, {:five => "five"})
|
243
|
+
end
|
244
|
+
elsif cols[i] == 'VC'
|
245
|
+
assert_raise RangeError do
|
246
|
+
connection.execute(sql_insert, "too long")
|
247
|
+
end
|
248
|
+
assert_raise RangeError do
|
249
|
+
connection.execute(sql_insert, 1.0/3.0)
|
250
|
+
end
|
251
|
+
elsif cols[i] == 'VC10'
|
252
|
+
assert_raise RangeError do
|
253
|
+
connection.execute(sql_insert, 1.0/3.0)
|
254
|
+
end
|
255
|
+
elsif cols[i].include?('VC10000')
|
256
|
+
assert_raise RangeError do
|
257
|
+
connection.execute(sql_insert, "X" * 10001)
|
258
|
+
end
|
259
|
+
elsif cols[i] == 'C'
|
260
|
+
assert_raise RangeError do
|
261
|
+
connection.execute(sql_insert, "too long")
|
262
|
+
end
|
263
|
+
elsif cols[i] == 'C10'
|
264
|
+
assert_raise RangeError do
|
265
|
+
connection.execute(sql_insert, Time.now)
|
266
|
+
end
|
267
|
+
elsif cols[i] == 'DT'
|
268
|
+
assert_raise ArgumentError do
|
269
|
+
connection.execute(sql_insert, Date)
|
270
|
+
end
|
271
|
+
assert_raise ArgumentError do
|
272
|
+
connection.execute(sql_insert, 2006)
|
273
|
+
end
|
274
|
+
elsif cols[i] == 'TM'
|
275
|
+
assert_raise TypeError do
|
276
|
+
connection.execute(sql_insert, {:date => "2006/1/1"})
|
277
|
+
end
|
278
|
+
assert_raise TypeError do
|
279
|
+
connection.execute(sql_insert, 10000)
|
280
|
+
end
|
281
|
+
elsif cols[i] == 'TS'
|
282
|
+
assert_raise TypeError do
|
283
|
+
connection.execute(sql_insert, 5.5)
|
284
|
+
end
|
285
|
+
assert_raise TypeError do
|
286
|
+
connection.execute(sql_insert, 10000)
|
287
|
+
end
|
288
|
+
elsif cols[i] == 'N92'
|
289
|
+
assert_raise TypeError do
|
290
|
+
connection.execute(sql_insert, {:five => "five"})
|
291
|
+
end
|
292
|
+
assert_raise TypeError do
|
293
|
+
connection.execute(sql_insert, Time.now)
|
294
|
+
end
|
295
|
+
assert_raise RangeError do
|
296
|
+
connection.execute(sql_insert, 5000000000)
|
297
|
+
end
|
298
|
+
elsif cols[i] == 'D92'
|
299
|
+
assert_raise TypeError do
|
300
|
+
connection.execute(sql_insert, {:five => "five"})
|
301
|
+
end
|
302
|
+
assert_raise TypeError do
|
303
|
+
connection.execute(sql_insert, Time.now)
|
304
|
+
end
|
305
|
+
assert_raise RangeError do
|
306
|
+
connection.execute(sql_insert, 5000000000)
|
307
|
+
end
|
308
|
+
end
|
309
|
+
end
|
310
|
+
connection.drop
|
311
|
+
end
|
312
|
+
end
|
313
|
+
|
314
|
+
def test_insert_correct_types
|
315
|
+
cols = %w{ I SI BI F D C C10 VC VC10 VC10000 DT TM TS N92 D92 }
|
316
|
+
types = %w{ INTEGER SMALLINT BIGINT FLOAT DOUBLE\ PRECISION CHAR CHAR(10) VARCHAR(1) VARCHAR(10) VARCHAR(10000) DATE TIME TIMESTAMP NUMERIC(9,2) DECIMAL(9,2) }
|
317
|
+
sql_schema = "";
|
318
|
+
assert_equal cols.size, types.size
|
319
|
+
cols.size.times do |i|
|
320
|
+
sql_schema << "CREATE TABLE TEST_#{cols[i]} (VAL #{types[i]});\n"
|
321
|
+
end
|
322
|
+
Database.create(@parms) do |connection|
|
323
|
+
connection.execute_script(sql_schema)
|
324
|
+
cols.size.times do |i|
|
325
|
+
sql_insert = "INSERT INTO TEST_#{cols[i]} (VAL) VALUES (?);"
|
326
|
+
sql_select = "SELECT * FROM TEST_#{cols[i]};"
|
327
|
+
if cols[i] == 'I'
|
328
|
+
connection.execute(sql_insert, 500_000)
|
329
|
+
connection.execute(sql_insert, "500_000")
|
330
|
+
vals = connection.query(sql_select)
|
331
|
+
assert_equal 500_000, vals[0][0]
|
332
|
+
assert_equal 500_000, vals[1][0]
|
333
|
+
elsif cols[i] == 'SI'
|
334
|
+
connection.execute(sql_insert, 32_123)
|
335
|
+
connection.execute(sql_insert, "32_123")
|
336
|
+
vals = connection.query(sql_select)
|
337
|
+
assert_equal 32_123, vals[0][0]
|
338
|
+
assert_equal 32_123, vals[1][0]
|
339
|
+
elsif cols[i] == 'BI'
|
340
|
+
connection.execute(sql_insert, 5_000_000_000)
|
341
|
+
connection.execute(sql_insert, "5_000_000_000")
|
342
|
+
vals = connection.query(sql_select)
|
343
|
+
assert_equal 5_000_000_000, vals[0][0]
|
344
|
+
assert_equal 5_000_000_000, vals[1][0]
|
345
|
+
elsif cols[i] == 'F'
|
346
|
+
connection.execute(sql_insert, 5.75)
|
347
|
+
connection.execute(sql_insert, "5.75")
|
348
|
+
vals = connection.query(sql_select)
|
349
|
+
assert_equal 5.75, vals[0][0]
|
350
|
+
assert_equal 5.75, vals[1][0]
|
351
|
+
elsif cols[i] == 'D'
|
352
|
+
connection.execute(sql_insert, 12345.12345)
|
353
|
+
connection.execute(sql_insert, "12345.12345")
|
354
|
+
vals = connection.query(sql_select)
|
355
|
+
assert_equal 12345.12345, vals[0][0]
|
356
|
+
assert_equal 12345.12345, vals[1][0]
|
357
|
+
elsif cols[i] == 'VC'
|
358
|
+
connection.execute(sql_insert, "5")
|
359
|
+
connection.execute(sql_insert, 5)
|
360
|
+
vals = connection.query(sql_select)
|
361
|
+
assert_equal "5", vals[0][0]
|
362
|
+
assert_equal "5", vals[1][0]
|
363
|
+
elsif cols[i] == 'VC10'
|
364
|
+
connection.execute(sql_insert, "1234567890")
|
365
|
+
connection.execute(sql_insert, 1234567890)
|
366
|
+
vals = connection.query(sql_select)
|
367
|
+
assert_equal "1234567890", vals[0][0]
|
368
|
+
assert_equal "1234567890", vals[1][0]
|
369
|
+
elsif cols[i].include?('VC10000')
|
370
|
+
connection.execute(sql_insert, "1" * 100)
|
371
|
+
connection.execute(sql_insert, ("1" * 100).to_i)
|
372
|
+
vals = connection.query(sql_select)
|
373
|
+
assert_equal "1" * 100, vals[0][0]
|
374
|
+
assert_equal "1" * 100, vals[1][0]
|
375
|
+
elsif cols[i] == 'C'
|
376
|
+
connection.execute(sql_insert, "5")
|
377
|
+
connection.execute(sql_insert, 5)
|
378
|
+
vals = connection.query(sql_select)
|
379
|
+
assert_equal "5", vals[0][0]
|
380
|
+
assert_equal "5", vals[1][0]
|
381
|
+
elsif cols[i] == 'C10'
|
382
|
+
connection.execute(sql_insert, "1234567890")
|
383
|
+
connection.execute(sql_insert, 1234567890)
|
384
|
+
vals = connection.query(sql_select)
|
385
|
+
assert_equal "1234567890", vals[0][0]
|
386
|
+
assert_equal "1234567890", vals[1][0]
|
387
|
+
elsif cols[i] == 'DT'
|
388
|
+
connection.execute(sql_insert, Date.civil(2000,2,2))
|
389
|
+
connection.execute(sql_insert, "2000/2/2")
|
390
|
+
connection.execute(sql_insert, "2000-2-2")
|
391
|
+
vals = connection.query(sql_select)
|
392
|
+
assert_equal Date.civil(2000,2,2), vals[0][0]
|
393
|
+
assert_equal Date.civil(2000,2,2), vals[1][0]
|
394
|
+
elsif cols[i] == 'TM'
|
395
|
+
connection.execute(sql_insert, Time.utc(2000,1,1,2,22,22))
|
396
|
+
connection.execute(sql_insert, "2000/1/1 2:22:22")
|
397
|
+
connection.execute(sql_insert, "2000-1-1 2:22:22")
|
398
|
+
vals = connection.query(sql_select)
|
399
|
+
assert_equal Time.utc(2000,1,1,2,22,22), vals[0][0]
|
400
|
+
assert_equal Time.utc(2000,1,1,2,22,22), vals[1][0]
|
401
|
+
elsif cols[i] == 'TS'
|
402
|
+
connection.execute(sql_insert, Time.local(2006,6,6,3,33,33))
|
403
|
+
connection.execute(sql_insert, "2006/6/6 3:33:33")
|
404
|
+
connection.execute(sql_insert, "2006-6-6 3:33:33")
|
405
|
+
vals = connection.query(sql_select)
|
406
|
+
assert_equal Time.local(2006,6,6,3,33,33), vals[0][0]
|
407
|
+
assert_equal Time.local(2006,6,6,3,33,33), vals[1][0]
|
408
|
+
assert_equal Time.local(2006,6,6,3,33,33), vals[2][0]
|
409
|
+
elsif cols[i] == 'N92'
|
410
|
+
connection.execute(sql_insert, 12345.12)
|
411
|
+
connection.execute(sql_insert, "12345.12")
|
412
|
+
vals = connection.query(sql_select)
|
413
|
+
# puts vals.inspect
|
414
|
+
assert_equal 12345.12, vals[0][0], "NUMERIC (decimal)"
|
415
|
+
assert_equal 12345.12, vals[1][0], "NUMERIC (string)"
|
416
|
+
elsif cols[i] == 'D92'
|
417
|
+
connection.execute(sql_insert, 12345.12)
|
418
|
+
connection.execute(sql_insert, "12345.12")
|
419
|
+
vals = connection.query(sql_select)
|
420
|
+
# puts vals.inspect
|
421
|
+
assert_equal 12345.12, vals[0][0], "DECIMAL (decimal)"
|
422
|
+
assert_equal 12345.12, vals[1][0], "DECIMAL (string)"
|
423
|
+
end
|
424
|
+
end
|
425
|
+
connection.drop
|
426
|
+
end
|
427
|
+
end
|
428
|
+
end
|