dbd-sqlanywhere 0.1.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +21 -12
- data/LICENSE +23 -23
- data/README +150 -150
- data/lib/dbd/SQLAnywhere.rb +176 -176
- data/lib/dbd/sqlanywhere/database.rb +266 -255
- data/lib/dbd/sqlanywhere/driver.rb +108 -102
- data/lib/dbd/sqlanywhere/statement.rb +216 -211
- data/test/dbd/sqlanywhere/base.rb +26 -26
- data/test/dbd/sqlanywhere/down.sql +19 -19
- data/test/dbd/sqlanywhere/up.sql +28 -28
- metadata +77 -67
- data/test/DBD_TESTS +0 -48
- data/test/dbd/general/test_database.rb +0 -157
- data/test/dbd/general/test_statement.rb +0 -249
- data/test/dbd/general/test_types.rb +0 -253
- data/test/ts_dbd.rb +0 -118
@@ -1,255 +1,266 @@
|
|
1
|
-
#====================================================
|
2
|
-
#
|
3
|
-
# Copyright
|
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
|
-
module DBI::DBD::SQLAnywhere
|
26
|
-
class Database < DBI::BaseDatabase
|
27
|
-
include Utility
|
28
|
-
|
29
|
-
COLUMN_SELECT_STRING = <<END_OF_STATEMENT
|
30
|
-
SELECT SYS.SYSTABCOL.column_name,
|
31
|
-
SYS.SYSIDXCOL.
|
32
|
-
SYS.SYSTABCOL."nulls",
|
33
|
-
SYS.SYSTABCOL."default",
|
34
|
-
SYS.SYSTABCOL.scale,
|
35
|
-
SYS.SYSTABCOL.width,
|
36
|
-
SYS.SYSDOMAIN.type_id,
|
37
|
-
SYS.SYSDOMAIN.domain_name,
|
38
|
-
SYS.SYSIDX."unique"
|
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
|
-
-2 => DBI::
|
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
|
-
res
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
raise error() if
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
raise error() if
|
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
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
1
|
+
#====================================================
|
2
|
+
#
|
3
|
+
# Copyright 2012 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
|
+
module DBI::DBD::SQLAnywhere
|
26
|
+
class Database < DBI::BaseDatabase
|
27
|
+
include Utility
|
28
|
+
|
29
|
+
COLUMN_SELECT_STRING = <<END_OF_STATEMENT
|
30
|
+
SELECT SYS.SYSTABCOL.column_name,
|
31
|
+
MIN (SYS.SYSIDXCOL.index_id) AS "index_id",
|
32
|
+
SYS.SYSTABCOL."nulls",
|
33
|
+
SYS.SYSTABCOL."default",
|
34
|
+
SYS.SYSTABCOL.scale,
|
35
|
+
SYS.SYSTABCOL.width,
|
36
|
+
SYS.SYSDOMAIN.type_id,
|
37
|
+
SYS.SYSDOMAIN.domain_name,
|
38
|
+
MIN (SYS.SYSIDX."unique") AS "unique",
|
39
|
+
SYS.SYSTABCOL.column_id
|
40
|
+
FROM (SYS.SYSTABLE join SYS.SYSTABCOL join SYS.SYSDOMAIN) left outer join (SYS.SYSIDXCOL join SYS.SYSIDX)
|
41
|
+
WHERE table_name = ?
|
42
|
+
GROUP BY
|
43
|
+
SYS.SYSTABCOL.column_name,
|
44
|
+
SYS.SYSTABCOL."nulls",
|
45
|
+
SYS.SYSTABCOL."default",
|
46
|
+
SYS.SYSTABCOL.scale,
|
47
|
+
SYS.SYSTABCOL.width,
|
48
|
+
SYS.SYSDOMAIN.type_id,
|
49
|
+
SYS.SYSDOMAIN.domain_name,
|
50
|
+
SYS.SYSTABCOL.column_id
|
51
|
+
ORDER BY SYS.SYSTABCOL.column_id
|
52
|
+
END_OF_STATEMENT
|
53
|
+
|
54
|
+
TABLE_SELECT_STRING = <<END_OF_STATEMENT
|
55
|
+
SELECT table_name
|
56
|
+
FROM SYS.SYSUSER, SYS.SYSTAB
|
57
|
+
WHERE user_name not in ('SYS', 'dbo', 'rs_systabgroup')
|
58
|
+
AND SYS.SYSUSER.user_id = SYS.SYSTAB.creator
|
59
|
+
END_OF_STATEMENT
|
60
|
+
|
61
|
+
SQLANY_to_DBI = {
|
62
|
+
5 => DBI::SQL_SMALLINT,
|
63
|
+
4 => DBI::SQL_INTEGER,
|
64
|
+
2 => DBI::SQL_NUMERIC,
|
65
|
+
7 => DBI::SQL_FLOAT,
|
66
|
+
8 => DBI::SQL_DOUBLE,
|
67
|
+
9 => DBI::SQL_DATE,
|
68
|
+
1 => DBI::SQL_CHAR,
|
69
|
+
12 => DBI::SQL_VARCHAR,
|
70
|
+
-1 => DBI::SQL_LONGVARCHAR,
|
71
|
+
-2 => DBI::SQL_BINARY,
|
72
|
+
-4 => DBI::SQL_LONGVARBINARY,
|
73
|
+
11 => DBI::SQL_TIMESTAMP,
|
74
|
+
10 => DBI::SQL_TIME,
|
75
|
+
-6 => DBI::SQL_TINYINT,
|
76
|
+
-5 => DBI::SQL_BIGINT,
|
77
|
+
-9 => DBI::SQL_INTEGER,
|
78
|
+
-10 => DBI::SQL_SMALLINT,
|
79
|
+
-11 => DBI::SQL_BIGINT,
|
80
|
+
-7 => DBI::SQL_BIT,
|
81
|
+
2 => DBI::SQL_DECIMAL,
|
82
|
+
-2 => DBI::SQL_VARBINARY,
|
83
|
+
-15 => DBI::SQL_BINARY,
|
84
|
+
-16 => DBI::SQL_VARBINARY,
|
85
|
+
-17 => DBI::SQL_LONGVARBINARY,
|
86
|
+
-18 => DBI::SQL_LONGVARCHAR,
|
87
|
+
-12 => DBI::SQL_CHAR,
|
88
|
+
-13 => DBI::SQL_VARCHAR,
|
89
|
+
-14 => DBI::SQL_LONGVARCHAR,
|
90
|
+
}
|
91
|
+
|
92
|
+
|
93
|
+
def disconnect
|
94
|
+
SA.instance.api.sqlany_rollback(@handle)
|
95
|
+
SA.instance.api.sqlany_disconnect(@handle)
|
96
|
+
SA.instance.api.sqlany_free_connection(@handle)
|
97
|
+
end
|
98
|
+
|
99
|
+
def prepare( statement )
|
100
|
+
stmt = SA.instance.api.sqlany_prepare(@handle, statement)
|
101
|
+
if stmt.nil?
|
102
|
+
raise error()
|
103
|
+
else
|
104
|
+
return Statement.new(stmt, @handle)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def ping
|
109
|
+
res = SA.instance.api.sqlany_execute_immediate(@handle, 'SELECT * FROM dummy')
|
110
|
+
raise error() if res == 0
|
111
|
+
return res
|
112
|
+
end
|
113
|
+
|
114
|
+
def commit
|
115
|
+
res = SA.instance.api.sqlany_commit(@handle)
|
116
|
+
raise error() if res == 0
|
117
|
+
return res
|
118
|
+
end
|
119
|
+
|
120
|
+
def rollback
|
121
|
+
res = SA.instance.api.sqlany_rollback(@handle)
|
122
|
+
raise error() if res == 0
|
123
|
+
return res
|
124
|
+
end
|
125
|
+
|
126
|
+
def quote(value)
|
127
|
+
value.gsub("'", "''")
|
128
|
+
end
|
129
|
+
|
130
|
+
def execute(sql, *bindvars)
|
131
|
+
bound = {}
|
132
|
+
prep_stmt = SA.instance.api.sqlany_prepare(@handle, sql);
|
133
|
+
raise error() if prep_stmt.nil?
|
134
|
+
num_params = SA.instance.api.sqlany_num_params(prep_stmt)
|
135
|
+
raise error("Wrong number of parameters. Supplied #{bindvars.length} but expecting #{num_params}") if (num_params != bindvars.length)
|
136
|
+
num_params.times do |i|
|
137
|
+
res, param = SA.instance.api.sqlany_describe_bind_param(prep_stmt, i)
|
138
|
+
raise error() if res == 0 or param.nil?
|
139
|
+
do_bind!(prep_stmt, param, bindvars[i], i, bound)
|
140
|
+
param.finish
|
141
|
+
end
|
142
|
+
|
143
|
+
res = SA.instance.api.sqlany_execute(prep_stmt)
|
144
|
+
raise error() if res == 0
|
145
|
+
return Statement.new(prep_stmt, @handle, bound)
|
146
|
+
end
|
147
|
+
|
148
|
+
def do(sql, *bindvars)
|
149
|
+
prep_stmt = SA.instance.api.sqlany_prepare(@handle, sql);
|
150
|
+
raise error() if prep_stmt.nil?
|
151
|
+
num_params = SA.instance.api.sqlany_num_params(prep_stmt)
|
152
|
+
raise error("Wrong number of parameters. Supplied #{bindvars.length} but expecting #{num_params}") if (num_params != bindvars.length)
|
153
|
+
num_params.times do |i|
|
154
|
+
res, param = SA.instance.api.sqlany_describe_bind_param(prep_stmt, i)
|
155
|
+
raise error() if res == 0 or param.nil?
|
156
|
+
do_bind!(prep_stmt, param, bindvars[i], i, nil)
|
157
|
+
param.finish
|
158
|
+
end
|
159
|
+
res = SA.instance.api.sqlany_execute(prep_stmt)
|
160
|
+
raise error() if res == 0
|
161
|
+
affected_rows = SA.instance.api.sqlany_affected_rows(prep_stmt)
|
162
|
+
return affected_rows
|
163
|
+
end
|
164
|
+
|
165
|
+
def tables
|
166
|
+
rs = SA.instance.api.sqlany_execute_direct(@handle, TABLE_SELECT_STRING)
|
167
|
+
if rs.nil?
|
168
|
+
return nil
|
169
|
+
else
|
170
|
+
tables = []
|
171
|
+
while (SA.instance.api.sqlany_fetch_next(rs) == 1)
|
172
|
+
res, cols = SA.instance.api.sqlany_get_column(rs, 0)
|
173
|
+
raise error() if res == 0 or cols.nil?
|
174
|
+
tables << cols
|
175
|
+
end
|
176
|
+
|
177
|
+
return tables
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
def columns( table )
|
182
|
+
prep_stmt = SA.instance.api.sqlany_prepare(@handle, COLUMN_SELECT_STRING)
|
183
|
+
raise error() if prep_stmt.nil?
|
184
|
+
res, param = SA.instance.api.sqlany_describe_bind_param(prep_stmt, 0)
|
185
|
+
raise error() if res == 0 or param.nil?
|
186
|
+
param.set_value(table)
|
187
|
+
|
188
|
+
raise error() if SA.instance.api.sqlany_bind_param(prep_stmt, 0, param) == 0
|
189
|
+
|
190
|
+
res = SA.instance.api.sqlany_execute(prep_stmt)
|
191
|
+
|
192
|
+
raise error() if res == 0 or prep_stmt.nil?
|
193
|
+
columns = []
|
194
|
+
col_count = 0
|
195
|
+
while (SA.instance.api.sqlany_fetch_next(prep_stmt) == 1)
|
196
|
+
columns << {}
|
197
|
+
|
198
|
+
res, col_val = SA.instance.api.sqlany_get_column(prep_stmt, 0)
|
199
|
+
raise error() if res == 0
|
200
|
+
columns[col_count]['name'] = col_val
|
201
|
+
|
202
|
+
res, col_val = SA.instance.api.sqlany_get_column(prep_stmt, 1)
|
203
|
+
raise error() if res == 0
|
204
|
+
columns[col_count]['pkey'] = (col_val == 0)
|
205
|
+
|
206
|
+
res, col_val = SA.instance.api.sqlany_get_column(prep_stmt, 2)
|
207
|
+
raise error() if res == 0
|
208
|
+
if col_val == 'Y'
|
209
|
+
columns[col_count]['nullable'] = true
|
210
|
+
else
|
211
|
+
columns[col_count]['nullable'] = false
|
212
|
+
end
|
213
|
+
|
214
|
+
res, col_val = SA.instance.api.sqlany_get_column(prep_stmt, 3)
|
215
|
+
raise error() if res == 0
|
216
|
+
columns[col_count]['default'] = col_val
|
217
|
+
|
218
|
+
res, col_val = SA.instance.api.sqlany_get_column(prep_stmt, 4)
|
219
|
+
raise error() if res == 0
|
220
|
+
columns[col_count]['scale'] = col_val
|
221
|
+
|
222
|
+
res, col_val = SA.instance.api.sqlany_get_column(prep_stmt, 5)
|
223
|
+
raise error() if res == 0
|
224
|
+
columns[col_count]['precision'] = col_val
|
225
|
+
|
226
|
+
res, col_val = SA.instance.api.sqlany_get_column(prep_stmt, 6)
|
227
|
+
raise error() if res == 0
|
228
|
+
columns[col_count]['sql_type'] = SQLANY_to_DBI[col_val]
|
229
|
+
|
230
|
+
res, col_val = SA.instance.api.sqlany_get_column(prep_stmt, 7)
|
231
|
+
raise error() if res == 0
|
232
|
+
columns[col_count]['type_name'] = col_val.downcase
|
233
|
+
|
234
|
+
res, col_val = SA.instance.api.sqlany_get_column(prep_stmt, 8)
|
235
|
+
raise error() if res == 0
|
236
|
+
columns[col_count]['unique'] = (col_val == 1 or col_val == 2)
|
237
|
+
|
238
|
+
col_count += 1
|
239
|
+
end
|
240
|
+
param.finish
|
241
|
+
return columns
|
242
|
+
end
|
243
|
+
|
244
|
+
def [] (attr)
|
245
|
+
@attr[attr]
|
246
|
+
end
|
247
|
+
|
248
|
+
def []= (attr, val)
|
249
|
+
@attr[attr] = val
|
250
|
+
end
|
251
|
+
|
252
|
+
protected
|
253
|
+
def error(*custom_msg)
|
254
|
+
code, msg = SA.instance.api.sqlany_error(@handle)
|
255
|
+
state = SA.instance.api.sqlany_sqlstate(@handle)
|
256
|
+
SA.instance.api.sqlany_clear_error(@handle)
|
257
|
+
if !custom_msg.nil?
|
258
|
+
if custom_msg.length != 0
|
259
|
+
msg = "#{custom_msg}. #{msg}"
|
260
|
+
end
|
261
|
+
end
|
262
|
+
return DBI::DatabaseError.new(msg, code, state)
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
end
|