dbd-sqlanywhere 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,253 +1,255 @@
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
- module DBI::DBD::SQLAnywhere
26
- class Database < DBI::BaseDatabase
27
- include Utility
28
-
29
- COLUMN_SELECT_STRING = <<END_OF_STATEMENT
30
- SELECT systabcol.column_name,
31
- sysidxcol.sequence,
32
- systabcol."nulls",
33
- systabcol."default",
34
- systabcol.scale,
35
- systabcol.width,
36
- sysdomain.type_id,
37
- sysdomain.domain_name,
38
- sysidx."unique"
39
- FROM (systable join systabcol join sysdomain) left outer join (sysidxcol join sysidx)
40
- WHERE table_name = ?
41
- END_OF_STATEMENT
42
-
43
- TABLE_SELECT_STRING = <<END_OF_STATEMENT
44
- SELECT table_name
45
- FROM sysuser, systab
46
- WHERE user_name not in ('SYS', 'DBO', 'rs_systabgroup')
47
- AND sysuser.user_id = 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
- msg = "#{custom_msg}. #{msg}"
248
- end
249
- return DBI::DatabaseError.new(code, msg, state)
250
- end
251
- end
252
-
253
- end
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