sqlanywhere 0.1.0-i486-linux

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -0,0 +1,412 @@
1
+ #====================================================
2
+ #
3
+ # Copyright 2008 iAnywhere Solutions, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ #
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+ # While not a requirement of the license, if you do modify this file, we
20
+ # would appreciate hearing about it. Please email sqlany_interfaces@sybase.com
21
+ #
22
+ #
23
+ #====================================================
24
+
25
+ require 'test/unit'
26
+
27
+ begin
28
+ require 'rubygems'
29
+ gem 'sqlanywhere'
30
+ unless defined? SQLAnywhere
31
+ require 'sqlanywhere'
32
+ end
33
+ end
34
+
35
+ class Types
36
+ A_INVALID_TYPE= 0
37
+ A_BINARY = 1
38
+ A_STRING = 2
39
+ A_DOUBLE = 3
40
+ A_VAL64 = 4
41
+ A_UVAL64 = 5
42
+ A_VAL32 = 6
43
+ A_UVAL32 = 7
44
+ A_VAL16 = 8
45
+ A_UVAL16 = 9
46
+ A_VAL8 = 10
47
+ A_UVAL8 = 11
48
+ end
49
+
50
+ class Direction
51
+ DD_INVALID = 0
52
+ DD_INPUT = 1
53
+ DD_OUTPUT = 2
54
+ DD_INPUT_OUTPUT = 3
55
+ end
56
+
57
+ class SQLAnywhere_Test < Test::Unit::TestCase
58
+ def setup
59
+ @api = SQLAnywhere::SQLAnywhereInterface.new()
60
+ assert_not_nil @api
61
+ assert_nothing_raised do
62
+ SQLAnywhere::API.sqlany_initialize_interface( @api )
63
+ end
64
+ assert_nothing_raised do
65
+ @api.sqlany_init()
66
+ end
67
+ @conn = @api.sqlany_new_connection()
68
+ assert_not_nil @conn
69
+ conn_str = "eng=test;uid=dba;pwd=sql"
70
+ assert_succeeded @api.sqlany_connect(@conn, conn_str)
71
+ end
72
+
73
+ def teardown
74
+ assert_succeeded @api.sqlany_execute_immediate(@conn, 'SELECT * FROM dummy')
75
+ assert_nil @api.sqlany_disconnect(@conn)
76
+ assert_failed @api.sqlany_execute_immediate(@conn, 'SELECT * FROM dummy')
77
+ assert_nil @api.sqlany_free_connection(@conn)
78
+ assert_nothing_raised do
79
+ @api.sqlany_fini()
80
+ end
81
+ assert_nothing_raised do
82
+ SQLAnywhere::API.sqlany_finalize_interface( @api )
83
+ end
84
+ end
85
+
86
+ def test_execute_immediate
87
+ assert_succeeded @api.sqlany_execute_immediate(@conn, 'SELECT * FROM dummy')
88
+ end
89
+
90
+ def test_errors
91
+ sql = "INSERT INTO test(\"id\") VALUES('test');"
92
+ assert_failed @api.sqlany_execute_immediate(@conn, sql)
93
+ code, msg = @api.sqlany_error(@conn)
94
+ assert_equal -157, code
95
+ assert_not_equal "", msg
96
+ assert_equal "53018\000", @api.sqlany_sqlstate(@conn)
97
+ assert_nil @api.sqlany_clear_error(@conn)
98
+ code, msg = @api.sqlany_error(@conn)
99
+ assert_equal 0, code
100
+ assert_equal "", msg
101
+ end
102
+
103
+ def test_rollback
104
+ id = setup_transaction
105
+ @api.sqlany_rollback(@conn)
106
+ sql = "SELECT * FROM test where \"id\" = " + id.to_s + ";"
107
+ rs = exec_direct_with_test(sql)
108
+ assert_failed @api.sqlany_fetch_next(rs)
109
+ end
110
+
111
+ def test_commit
112
+ id = setup_transaction
113
+ @api.sqlany_commit(@conn)
114
+ sql = "SELECT * FROM test where \"id\" = " + id.to_s + ";"
115
+ rs = exec_direct_with_test(sql)
116
+ assert_succeeded @api.sqlany_fetch_next(rs)
117
+ res, ret_id = @api.sqlany_get_column(rs, 0)
118
+ assert_succeeded res
119
+ assert_not_nil ret_id
120
+ assert_equal id, ret_id
121
+ assert_failed @api.sqlany_fetch_next(rs)
122
+ end
123
+
124
+ def test_column_info
125
+ rs = exec_direct_with_test("SELECT TOP 2 * FROM \"types\" ORDER BY \"id\"")
126
+ assert_equal 22, @api.sqlany_num_cols(rs)
127
+ assert_column_info(rs, 0, "id", Types::A_VAL32, 4)
128
+ assert_column_info(rs, 1, "_binary_", Types::A_BINARY, 8)
129
+ assert_column_info(rs, 2, "_numeric_", Types::A_STRING, 2)
130
+ assert_column_info(rs, 3, "_decimal_", Types::A_STRING, 2)
131
+ assert_column_info(rs, 4, "_bounded_string_", Types::A_STRING, 255)
132
+ assert_column_info(rs, 5, "_unbounded_string_", Types::A_STRING, (2 * (2**30)) - 1)
133
+ assert_column_info(rs, 6, "_signed_bigint_", Types::A_VAL64, 8)
134
+ assert_column_info(rs, 7, "_unsigned_bigint_", Types::A_UVAL64, 8)
135
+ assert_column_info(rs, 8, "_signed_int_", Types::A_VAL32, 4)
136
+ assert_column_info(rs, 9, "_unsigned_int_", Types::A_UVAL32, 4)
137
+ assert_column_info(rs, 10, "_signed_smallint_", Types::A_VAL16, 2)
138
+ assert_column_info(rs, 11, "_unsigned_smallint_", Types::A_UVAL16, 2)
139
+ assert_column_info(rs, 12, "_signed_tinyint_", Types::A_VAL8, 1)
140
+ assert_column_info(rs, 13, "_unsigned_tinyint_", Types::A_VAL8, 1)
141
+ assert_column_info(rs, 14, "_bit_", Types::A_UVAL8, 1)
142
+ assert_column_info(rs, 15, "_date_", Types::A_STRING, 10)
143
+ assert_column_info(rs, 16, "_datetime_", Types::A_STRING, 23)
144
+ assert_column_info(rs, 17, "_smalldatetime_", Types::A_STRING, 23)
145
+ assert_column_info(rs, 18, "_timestamp_", Types::A_STRING, 23)
146
+ assert_column_info(rs, 19, "_double_", Types::A_DOUBLE, 8)
147
+ assert_column_info(rs, 20, "_float_", Types::A_DOUBLE, 4)
148
+ assert_column_info(rs, 21, "_real_", Types::A_DOUBLE, 4)
149
+ assert_nil @api.sqlany_free_stmt(rs)
150
+ end
151
+
152
+ def test_bounds_on_types
153
+ rs = exec_direct_with_test("SELECT TOP 2 * FROM \"types\" ORDER BY \"id\"")
154
+ assert_succeeded @api.sqlany_fetch_next(rs)
155
+ assert_class_and_value(rs, String, 1, "x")
156
+ assert_class_and_value(rs, String, 2, "1.1")
157
+ assert_class_and_value(rs, String, 3, "1.1")
158
+ assert_class_and_value(rs, String, 4, 'Bounded String Test')
159
+ assert_class_and_value(rs, String, 5, 'Unbounded String Test')
160
+ assert_class_and_value(rs, Bignum, 6, 9223372036854775807)
161
+ assert_class_and_value(rs, Bignum, 7, 18446744073709551615)
162
+ assert_class_and_value(rs, Bignum, 8, 2147483647)
163
+ assert_class_and_value(rs, Bignum, 9, 4294967295)
164
+ assert_class_and_value(rs, Fixnum, 10, 32767)
165
+ assert_class_and_value(rs, Fixnum, 11, 65535)
166
+ assert_class_and_value(rs, Fixnum, 12, 255)
167
+ assert_class_and_value(rs, Fixnum, 13, 255)
168
+ assert_class_and_value(rs, Fixnum, 14, 1)
169
+ assert_date_and_time(rs, Date, 15, Date.new(1999, 1, 2))
170
+ assert_date_and_time(rs, DateTime, 16, DateTime.new(1999, 1, 2, 21, 20, 53))
171
+ assert_date_and_time(rs, DateTime, 17, DateTime.new(1999, 1, 2, 21, 20, 53))
172
+ assert_date_and_time(rs, DateTime, 18, DateTime.new(1999, 1, 2, 21, 20, 53))
173
+ assert_class_and_float_value(rs, Float, 19, 1.79769313486231e+308, 1e+293 )
174
+ assert_class_and_float_value(rs, Float, 20, 3.402823e+38, 1e+32 )
175
+ assert_class_and_float_value(rs, Float, 21, 3.402823e+38, 1e+32 )
176
+
177
+ assert_succeeded @api.sqlany_fetch_next(rs)
178
+ assert_class_and_value(rs, String, 1, 255.chr)
179
+ assert_class_and_value(rs, String, 2, "-1.1")
180
+ assert_class_and_value(rs, String, 3, "-1.1")
181
+ assert_class_and_value(rs, String, 4, '')
182
+ assert_class_and_value(rs, String, 5, '')
183
+ assert_class_and_value(rs, Bignum, 6, -9223372036854775808)
184
+ assert_class_and_value(rs, Fixnum, 7, 0)
185
+ assert_class_and_value(rs, Bignum, 8, -2147483648)
186
+ assert_class_and_value(rs, Fixnum, 9, 0)
187
+ assert_class_and_value(rs, Fixnum, 10, -32768)
188
+ assert_class_and_value(rs, Fixnum, 11, 0)
189
+ assert_class_and_value(rs, Fixnum, 12, 0)
190
+ assert_class_and_value(rs, Fixnum, 13, 0)
191
+ assert_class_and_value(rs, Fixnum, 14, 0)
192
+ assert_class_and_value(rs, NilClass, 15, nil)
193
+ assert_class_and_value(rs, NilClass, 16, nil)
194
+ assert_class_and_value(rs, NilClass, 17, nil)
195
+ assert_class_and_value(rs, NilClass, 18, nil)
196
+ assert_class_and_float_value(rs, Float, 19, -1.79769313486231e+308, 1e+293 )
197
+ assert_class_and_float_value(rs, Float, 20, -3.402823e+38, 1e+32 )
198
+ assert_class_and_float_value(rs, Float, 21, -3.402823e+38, 1e+32 )
199
+ assert_nil @api.sqlany_free_stmt(rs)
200
+ end
201
+
202
+ def test_prepared_stmt
203
+ stmt = @api.sqlany_prepare(@conn, "SELECT * FROM \"types\" WHERE \"id\" = ?")
204
+ assert_not_nil stmt
205
+ assert_failed @api.sqlany_execute(stmt)
206
+ assert_equal 1, @api.sqlany_num_params(stmt)
207
+ res, param = @api.sqlany_describe_bind_param(stmt, 0)
208
+ assert_not_equal 0, res
209
+ assert_equal "?", param.get_name()
210
+ assert_equal Direction::DD_INPUT, param.get_direction()
211
+
212
+ assert_nil param.set_value(0);
213
+ @api.sqlany_bind_param(stmt, 0, param)
214
+ assert_succeeded @api.sqlany_execute(stmt)
215
+ assert_succeeded @api.sqlany_fetch_next(stmt)
216
+ assert_class_and_value(stmt, String, 4, "Bounded String Test")
217
+
218
+ assert_nil param.set_value(1);
219
+ @api.sqlany_bind_param(stmt, 0, param)
220
+ assert_succeeded @api.sqlany_execute(stmt)
221
+ assert_succeeded @api.sqlany_fetch_next(stmt)
222
+ assert_class_and_value(stmt, String, 4, "")
223
+
224
+ assert_nil @api.sqlany_free_stmt(stmt)
225
+ end
226
+
227
+ def test_insert_binary
228
+ assert_insert("_binary_", "x", String)
229
+ end
230
+
231
+ def test_insert_numeric
232
+ assert_insert("_numeric_", "1.1", String)
233
+ end
234
+
235
+ def test_insert_decimal
236
+ assert_insert("_decimal_", "1.1", String)
237
+ end
238
+
239
+ def test_insert_bounded_string
240
+ assert_insert("_bounded_string_", "Bounded String Test", String)
241
+ end
242
+
243
+ def test_insert_unbounded_string
244
+ assert_insert("_unbounded_string_", "Unbounded String Test", String)
245
+ end
246
+
247
+ def test_insert_int64
248
+ assert_insert("_signed_bigint_", 9223372036854775807, Bignum)
249
+ assert_insert("_signed_bigint_", -9223372036854775808, Bignum)
250
+ end
251
+
252
+ def test_insert_uint64
253
+ assert_insert("_unsigned_bigint_", 9223372036854775807, Bignum)
254
+ assert_insert("_unsigned_bigint_", 0, Fixnum)
255
+ end
256
+
257
+ def test_insert_int32
258
+ assert_insert("_signed_int_", 2147483647, Bignum)
259
+ assert_insert("_signed_int_", -2147483648, Bignum)
260
+ end
261
+
262
+ def test_insert_uint32
263
+ assert_insert("_unsigned_int_", 4294967295, Bignum)
264
+ assert_insert("_unsigned_int_", 0, Fixnum)
265
+ end
266
+
267
+ def test_insert_int16
268
+ assert_insert("_signed_smallint_", 32767, Fixnum)
269
+ assert_insert("_signed_smallint_", -32768, Fixnum)
270
+ end
271
+
272
+ def test_insert_uint16
273
+ assert_insert("_unsigned_smallint_", 65535, Fixnum)
274
+ assert_insert("_unsigned_smallint_", 0, Fixnum)
275
+ end
276
+
277
+ def test_insert_int8
278
+ assert_insert("_signed_smallint_", 255, Fixnum)
279
+ assert_insert("_signed_smallint_", 0, Fixnum)
280
+ end
281
+
282
+ def test_insert_uint8
283
+ assert_insert("_unsigned_smallint_", 255, Fixnum)
284
+ assert_insert("_unsigned_smallint_", 0, Fixnum)
285
+ end
286
+
287
+ def test_insert_date
288
+ assert_insert("_date_", Date.new(1999, 1, 2), Date)
289
+ end
290
+
291
+ def test_insert_datetime
292
+ assert_insert("_datetime_", DateTime.new(1999, 1, 2, 21, 20, 53), DateTime)
293
+ end
294
+
295
+ def test_insert_smalldate
296
+ assert_insert("_smalldatetime_", DateTime.new(1999, 1, 2, 21, 20, 53), DateTime)
297
+ end
298
+
299
+ def test_insert_timestamp
300
+ assert_insert("_timestamp_", DateTime.new(1999, 1, 2, 21, 20, 53), DateTime)
301
+ end
302
+
303
+ def test_insert_double
304
+ assert_insert("_double_", 1.79769313486231e+308, Float, 1e+293)
305
+ end
306
+
307
+ def test_insert_float
308
+ assert_insert("_float_", 3.402823e+38, Float, 1e+32)
309
+ end
310
+
311
+ def test_insert_real
312
+ assert_insert("_real_", 3.402823e+38, Float, 1e+32)
313
+ end
314
+
315
+ def assert_insert(column_name, value, type, delta = nil)
316
+ stmt = @api.sqlany_prepare(@conn, 'INSERT INTO "types"("id", "' + column_name + '", "_bit_") VALUES(3, ?, 1)')
317
+ assert_not_nil stmt
318
+ res, param = @api.sqlany_describe_bind_param(stmt, 0)
319
+ if type == Date or type == DateTime then
320
+ assert_nil param.set_value(value.strftime("%F %T"));
321
+ else
322
+ assert_nil param.set_value(value);
323
+ end
324
+ @api.sqlany_bind_param(stmt, 0, param)
325
+ assert_succeeded @api.sqlany_execute(stmt)
326
+ assert_nil @api.sqlany_free_stmt(stmt)
327
+
328
+ rs = exec_direct_with_test('SELECT "' + column_name + '" FROM "types" WHERE "id" = 3')
329
+ assert_succeeded @api.sqlany_fetch_next(rs)
330
+ if type == Date or type == DateTime then
331
+ assert_date_and_time(rs, type, 0, value)
332
+ elsif type == Float
333
+ assert_class_and_float_value(rs, type, 0, value, delta)
334
+ else
335
+ assert_class_and_value(rs, type, 0, value)
336
+ end
337
+
338
+ assert_nil @api.sqlany_free_stmt(rs)
339
+
340
+ @api.sqlany_rollback(@conn)
341
+ end
342
+
343
+ def assert_column_info(rs, pos, expected_col_name, expected_col_type, expected_col_size)
344
+ res, col_num, col_name, col_type, col_native_type, col_precision, col_scale, col_size, col_nullable = @api.sqlany_get_column_info(rs, pos);
345
+ assert_succeeded res
346
+ assert_equal expected_col_name, col_name
347
+ assert_equal expected_col_type, col_type
348
+ assert_equal expected_col_size, col_size
349
+ end
350
+
351
+ def assert_class_and_float_value(rs, cl, pos, expected_value, allowed_delta)
352
+ res, val = @api.sqlany_get_column(rs, pos)
353
+ assert_succeeded res
354
+ assert_not_nil val unless expected_value.nil?
355
+ assert_in_delta expected_value, val, allowed_delta
356
+ assert_instance_of cl, val
357
+ end
358
+
359
+ def assert_date_and_time(rs, cl, pos, expected_value)
360
+ res, val = @api.sqlany_get_column(rs, pos)
361
+ assert_succeeded res
362
+ assert_not_nil val unless expected_value.nil?
363
+ parsed = cl.parse(val)
364
+ assert_equal expected_value, parsed
365
+ assert_instance_of cl, parsed
366
+ end
367
+
368
+ def assert_class_and_value(rs, cl, pos, expected_value)
369
+ res, val = @api.sqlany_get_column(rs, pos)
370
+ assert_succeeded res
371
+ assert_not_nil val unless expected_value.nil?
372
+ assert_equal expected_value, val
373
+ assert_instance_of cl, val
374
+ end
375
+
376
+ def setup_transaction
377
+ sql = "INSERT INTO test VALUES( DEFAULT );"
378
+ assert_succeeded @api.sqlany_execute_immediate(@conn, sql)
379
+
380
+ rs = exec_direct_with_test("SELECT @@identity")
381
+ assert_succeeded @api.sqlany_fetch_next(rs)
382
+ res, id = @api.sqlany_get_column(rs, 0)
383
+ assert_succeeded res
384
+ assert_not_nil id
385
+
386
+ sql = "SELECT * FROM test where \"id\" = " + id.to_s + ";"
387
+ rs = @api.sqlany_execute_direct(@conn, sql)
388
+ assert_not_nil rs
389
+
390
+ assert_succeeded @api.sqlany_fetch_next(rs)
391
+ assert_failed @api.sqlany_fetch_next(rs)
392
+ assert_nil @api.sqlany_free_stmt(rs)
393
+ id
394
+ end
395
+
396
+ def exec_direct_with_test(sql)
397
+ rs = @api.sqlany_execute_direct(@conn, sql)
398
+ code, msg = @api.sqlany_error(@conn)
399
+ assert_not_nil rs, "SQL Code: #{code}; Message: #{msg}"
400
+ rs
401
+ end
402
+
403
+ def assert_succeeded(val)
404
+ assert_not_equal 0, val, @api.sqlany_error(@conn)
405
+ end
406
+
407
+ def assert_failed(val)
408
+ assert_equal 0, val, @api.sqlany_error(@conn)
409
+ end
410
+
411
+ end
412
+
data/test/test.sql ADDED
@@ -0,0 +1,87 @@
1
+ IF EXISTS( SELECT * FROM "systab" WHERE "table_name" = 'test') THEN
2
+ DROP TABLE "test";
3
+ END IF;
4
+
5
+ IF EXISTS( SELECT * FROM "systab" WHERE "table_name" = 'types') THEN
6
+ DROP TABLE "types";
7
+ END IF;
8
+
9
+ CREATE TABLE "test" (
10
+ "id" INTEGER NOT NULL DEFAULT AUTOINCREMENT,
11
+ PRIMARY KEY("id")
12
+ );
13
+
14
+ CREATE TABLE "types" (
15
+ "id" INTEGER PRIMARY KEY,
16
+ "_binary_" BINARY(8) DEFAULT NULL,
17
+ "_numeric_" NUMERIC(2,1),
18
+ "_decimal_" DECIMAL(2,1),
19
+ "_bounded_string_" CHAR(255) DEFAULT NULL,
20
+ "_unbounded_string_" LONG VARCHAR DEFAULT NULL,
21
+ "_signed_bigint_" BIGINT DEFAULT NULL,
22
+ "_unsigned_bigint_" UNSIGNED BIGINT DEFAULT NULL,
23
+ "_signed_int_" INTEGER DEFAULT NULL,
24
+ "_unsigned_int_" UNSIGNED INTEGER DEFAULT NULL,
25
+ "_signed_smallint_" SMALLINT DEFAULT NULL,
26
+ "_unsigned_smallint_" UNSIGNED SMALLINT DEFAULT NULL,
27
+ "_signed_tinyint_" TINYINT DEFAULT NULL,
28
+ "_unsigned_tinyint_" UNSIGNED TINYINT DEFAULT NULL,
29
+ "_bit_" BIT,
30
+ "_date_" DATE DEFAULT NULL,
31
+ "_datetime_" DATETIME DEFAULT NULL,
32
+ "_smalldatetime_" SMALLDATETIME DEFAULT NULL,
33
+ "_timestamp_" TIMESTAMP DEFAULT NULL,
34
+ "_double_" DOUBLE DEFAULT NULL,
35
+ "_float_" FLOAT DEFAULT NULL,
36
+ "_real_" REAL DEFAULT NULL
37
+ );
38
+
39
+ INSERT INTO types VALUES
40
+ ( 0,
41
+ CAST ( 0x78 AS BINARY ),
42
+ 1.1,
43
+ 1.1,
44
+ 'Bounded String Test',
45
+ 'Unbounded String Test',
46
+ 9223372036854775807,
47
+ 18446744073709551615,
48
+ 2147483647,
49
+ 4294967295,
50
+ 32767,
51
+ 65535,
52
+ 255,
53
+ 255,
54
+ 1,
55
+ DATE( '1999-01-02 21:20:53' ),
56
+ DATETIME( '1999-01-02 21:20:53' ),
57
+ DATETIME( '1999-01-02 21:20:53' ),
58
+ DATETIME( '1999-01-02 21:20:53' ),
59
+ 1.79769313486231e+308,
60
+ 3.402823e+38,
61
+ 3.402823e+38
62
+ )
63
+
64
+ INSERT INTO types VALUES
65
+ ( 1,
66
+ CAST ( 0xFF AS BINARY ),
67
+ -1.1,
68
+ -1.1,
69
+ '',
70
+ '',
71
+ -9223372036854775808,
72
+ 0,
73
+ -2147483648,
74
+ 0,
75
+ -32768,
76
+ 0,
77
+ 0,
78
+ 0,
79
+ 0,
80
+ NULL,
81
+ NULL,
82
+ NULL,
83
+ NULL,
84
+ -1.79769313486231e+308,
85
+ -3.402823e+38,
86
+ -3.402823e+38
87
+ )