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