rubyfb 0.5.5 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
data/ext/Statement.c CHANGED
@@ -3,26 +3,26 @@
3
3
  *----------------------------------------------------------------------------*/
4
4
  /**
5
5
  * Copyright � Peter Wood, 2005
6
- *
6
+ *
7
7
  * The contents of this file are subject to the Mozilla Public License Version
8
8
  * 1.1 (the "License"); you may not use this file except in compliance with the
9
- * License. You may obtain a copy of the License at
9
+ * License. You may obtain a copy of the License at
10
10
  *
11
11
  * http://www.mozilla.org/MPL/
12
- *
12
+ *
13
13
  * Software distributed under the License is distributed on an "AS IS" basis,
14
14
  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
15
15
  * the specificlanguage governing rights and limitations under the License.
16
- *
16
+ *
17
17
  * The Original Code is the FireRuby extension for the Ruby language.
18
- *
19
- * The Initial Developer of the Original Code is Peter Wood. All Rights
18
+ *
19
+ * The Initial Developer of the Original Code is Peter Wood. All Rights
20
20
  * Reserved.
21
21
  *
22
22
  * @author Peter Wood
23
23
  * @version 1.0
24
24
  */
25
-
25
+
26
26
  /* Includes. */
27
27
  #include "Statement.h"
28
28
  #include "Common.h"
@@ -58,23 +58,21 @@ VALUE cStatement;
58
58
  * @return A reference to the newly allocated Statement object.
59
59
  *
60
60
  */
61
- VALUE allocateStatement(VALUE klass)
62
- {
63
- StatementHandle *statement = ALLOC(StatementHandle);
64
-
65
- if(statement == NULL)
66
- {
67
- rb_raise(rb_eNoMemError,
68
- "Memory allocation failure allocating a statement.");
69
- }
70
-
71
- statement->handle = 0;
72
- statement->type = -1;
73
- statement->inputs = 0;
74
- statement->dialect = 0;
75
- statement->parameters = NULL;
76
-
77
- return(Data_Wrap_Struct(klass, NULL, statementFree, statement));
61
+ VALUE allocateStatement(VALUE klass) {
62
+ StatementHandle *statement = ALLOC(StatementHandle);
63
+
64
+ if(statement == NULL) {
65
+ rb_raise(rb_eNoMemError,
66
+ "Memory allocation failure allocating a statement.");
67
+ }
68
+
69
+ statement->handle = 0;
70
+ statement->type = -1;
71
+ statement->inputs = 0;
72
+ statement->dialect = 0;
73
+ statement->parameters = NULL;
74
+
75
+ return(Data_Wrap_Struct(klass, NULL, statementFree, statement));
78
76
  }
79
77
 
80
78
 
@@ -96,65 +94,52 @@ VALUE allocateStatement(VALUE klass)
96
94
  *
97
95
  */
98
96
  VALUE initializeStatement(VALUE self, VALUE connection, VALUE transaction,
99
- VALUE sql, VALUE dialect)
100
- {
101
- StatementHandle *statement = NULL;
102
- short setting = 0;
103
- VALUE value = Qnil;
104
-
105
- /* Validate the inputs. */
106
- if(TYPE(connection) == T_DATA &&
107
- RDATA(connection)->dfree == (RUBY_DATA_FUNC)connectionFree)
108
- {
109
- if(rb_funcall(connection, rb_intern("open?"), 0) == Qfalse)
110
- {
111
- rb_fireruby_raise(NULL, "Closed connection specified for statement.");
112
- }
113
- }
114
- else
115
- {
116
- rb_fireruby_raise(NULL, "Invalid connection specified for statement.");
117
- }
118
-
119
- if(TYPE(transaction) == T_DATA &&
120
- RDATA(transaction)->dfree == (RUBY_DATA_FUNC)transactionFree)
121
- {
122
- if(rb_funcall(transaction, rb_intern("active?"), 0) == Qfalse)
123
- {
124
- rb_fireruby_raise(NULL, "Inactive transaction specified for statement.");
125
- }
126
- }
127
- else
128
- {
129
- rb_fireruby_raise(NULL, "Invalid transaction specified for statement.");
130
- }
131
-
132
- value = rb_funcall(dialect, rb_intern("to_i"), 0);
133
- if(TYPE(value) == T_FIXNUM)
134
- {
135
- setting = FIX2INT(value);
136
- if(setting < 1 || setting > 3)
137
- {
138
- rb_fireruby_raise(NULL,
139
- "Invalid dialect value specified for statement. "\
140
- "The dialect value must be between 1 and 3.");
141
- }
142
- }
143
- else
144
- {
97
+ VALUE sql, VALUE dialect) {
98
+ StatementHandle *statement = NULL;
99
+ short setting = 0;
100
+ VALUE value = Qnil;
101
+
102
+ /* Validate the inputs. */
103
+ if(TYPE(connection) == T_DATA &&
104
+ RDATA(connection)->dfree == (RUBY_DATA_FUNC)connectionFree) {
105
+ if(rb_funcall(connection, rb_intern("open?"), 0) == Qfalse) {
106
+ rb_fireruby_raise(NULL, "Closed connection specified for statement.");
107
+ }
108
+ } else {
109
+ rb_fireruby_raise(NULL, "Invalid connection specified for statement.");
110
+ }
111
+
112
+ if(TYPE(transaction) == T_DATA &&
113
+ RDATA(transaction)->dfree == (RUBY_DATA_FUNC)transactionFree) {
114
+ if(rb_funcall(transaction, rb_intern("active?"), 0) == Qfalse) {
115
+ rb_fireruby_raise(NULL, "Inactive transaction specified for statement.");
116
+ }
117
+ } else {
118
+ rb_fireruby_raise(NULL, "Invalid transaction specified for statement.");
119
+ }
120
+
121
+ value = rb_funcall(dialect, rb_intern("to_i"), 0);
122
+ if(TYPE(value) == T_FIXNUM) {
123
+ setting = FIX2INT(value);
124
+ if(setting < 1 || setting > 3) {
145
125
  rb_fireruby_raise(NULL,
146
- "Invalid dialect value specified for statement. The "\
147
- "dialect value must be between 1 and 3.");
148
- }
149
-
150
- Data_Get_Struct(self, StatementHandle, statement);
151
- rb_iv_set(self, "@connection", connection);
152
- rb_iv_set(self, "@transaction", transaction);
153
- rb_iv_set(self, "@sql", rb_funcall(sql, rb_intern("to_s"), 0));
154
- rb_iv_set(self, "@dialect", value);
155
- statement->dialect = setting;
156
-
157
- return(self);
126
+ "Invalid dialect value specified for statement. " \
127
+ "The dialect value must be between 1 and 3.");
128
+ }
129
+ } else {
130
+ rb_fireruby_raise(NULL,
131
+ "Invalid dialect value specified for statement. The " \
132
+ "dialect value must be between 1 and 3.");
133
+ }
134
+
135
+ Data_Get_Struct(self, StatementHandle, statement);
136
+ rb_iv_set(self, "@connection", connection);
137
+ rb_iv_set(self, "@transaction", transaction);
138
+ rb_iv_set(self, "@sql", rb_funcall(sql, rb_intern("to_s"), 0));
139
+ rb_iv_set(self, "@dialect", value);
140
+ statement->dialect = setting;
141
+
142
+ return(self);
158
143
  }
159
144
 
160
145
 
@@ -167,9 +152,8 @@ VALUE initializeStatement(VALUE self, VALUE connection, VALUE transaction,
167
152
  * @return A reference to a String containing the SQL statement.
168
153
  *
169
154
  */
170
- VALUE getStatementSQL(VALUE self)
171
- {
172
- return(rb_iv_get(self, "@sql"));
155
+ VALUE getStatementSQL(VALUE self) {
156
+ return(rb_iv_get(self, "@sql"));
173
157
  }
174
158
 
175
159
 
@@ -182,9 +166,8 @@ VALUE getStatementSQL(VALUE self)
182
166
  * @return A reference to a Connection object.
183
167
  *
184
168
  */
185
- VALUE getStatementConnection(VALUE self)
186
- {
187
- return(rb_iv_get(self, "@connection"));
169
+ VALUE getStatementConnection(VALUE self) {
170
+ return(rb_iv_get(self, "@connection"));
188
171
  }
189
172
 
190
173
 
@@ -197,9 +180,8 @@ VALUE getStatementConnection(VALUE self)
197
180
  * @return A reference to a Transaction object.
198
181
  *
199
182
  */
200
- VALUE getStatementTransaction(VALUE self)
201
- {
202
- return(rb_iv_get(self, "@transaction"));
183
+ VALUE getStatementTransaction(VALUE self) {
184
+ return(rb_iv_get(self, "@transaction"));
203
185
  }
204
186
 
205
187
 
@@ -212,9 +194,8 @@ VALUE getStatementTransaction(VALUE self)
212
194
  * @return A reference to an integer containing the SQL dialect setting.
213
195
  *
214
196
  */
215
- VALUE getStatementDialect(VALUE self)
216
- {
217
- return(rb_iv_get(self, "@dialect"));
197
+ VALUE getStatementDialect(VALUE self) {
198
+ return(rb_iv_get(self, "@dialect"));
218
199
  }
219
200
 
220
201
 
@@ -228,27 +209,25 @@ VALUE getStatementDialect(VALUE self)
228
209
  * @return A reference to an integer containing the SQL type details.
229
210
  *
230
211
  */
231
- VALUE getStatementType(VALUE self)
232
- {
233
- StatementHandle *statement = NULL;
234
- ConnectionHandle *connection = NULL;
235
- TransactionHandle *transaction = NULL;
236
- int outputs = 0;
237
- VALUE tmp_str = Qnil;
238
-
239
- Data_Get_Struct(self, StatementHandle, statement);
240
- Data_Get_Struct(rb_iv_get(self, "@connection"), ConnectionHandle, connection);
241
- Data_Get_Struct(rb_iv_get(self, "@transaction"), TransactionHandle, transaction);
242
- if(statement->handle == 0)
243
- {
244
- tmp_str = rb_iv_get(self, "@sql");
245
- prepare(&connection->handle, &transaction->handle,
246
- StringValuePtr(tmp_str), &statement->handle,
247
- statement->dialect, &statement->type, &statement->inputs,
248
- &outputs);
249
- }
250
-
251
- return(INT2FIX(statement->type));
212
+ VALUE getStatementType(VALUE self) {
213
+ StatementHandle *statement = NULL;
214
+ ConnectionHandle *connection = NULL;
215
+ TransactionHandle *transaction = NULL;
216
+ int outputs = 0;
217
+ VALUE tmp_str = Qnil;
218
+
219
+ Data_Get_Struct(self, StatementHandle, statement);
220
+ Data_Get_Struct(rb_iv_get(self, "@connection"), ConnectionHandle, connection);
221
+ Data_Get_Struct(rb_iv_get(self, "@transaction"), TransactionHandle, transaction);
222
+ if(statement->handle == 0) {
223
+ tmp_str = rb_iv_get(self, "@sql");
224
+ prepare(&connection->handle, &transaction->handle,
225
+ StringValuePtr(tmp_str), &statement->handle,
226
+ statement->dialect, &statement->type, &statement->inputs,
227
+ &outputs);
228
+ }
229
+
230
+ return(INT2FIX(statement->type));
252
231
  }
253
232
 
254
233
 
@@ -261,17 +240,15 @@ VALUE getStatementType(VALUE self)
261
240
  * @return A reference to an integer containing the statement parameter count.
262
241
  *
263
242
  */
264
- VALUE getStatementParameterCount(VALUE self)
265
- {
266
- StatementHandle *statement = NULL;
267
-
268
- Data_Get_Struct(self, StatementHandle, statement);
269
- if(statement->handle == 0)
270
- {
271
- getStatementType(self);
272
- }
273
-
274
- return(INT2NUM(statement->inputs));
243
+ VALUE getStatementParameterCount(VALUE self) {
244
+ StatementHandle *statement = NULL;
245
+
246
+ Data_Get_Struct(self, StatementHandle, statement);
247
+ if(statement->handle == 0) {
248
+ getStatementType(self);
249
+ }
250
+
251
+ return(INT2NUM(statement->inputs));
275
252
  }
276
253
 
277
254
 
@@ -281,50 +258,48 @@ VALUE getStatementParameterCount(VALUE self)
281
258
  * @param self A reference to the Statement object to call the method on.
282
259
  *
283
260
  * @return One of a count of the number of rows affected by the SQL statement,
284
- * a ResultSet object for a query or nil.
261
+ * a ResultSet object for a query or nil.
285
262
  *
286
263
  */
287
- VALUE executeStatement(VALUE self)
288
- {
289
- VALUE result;
290
- int type = FIX2INT(getStatementType(self));
291
- long affected = 0;
292
- StatementHandle *statement = NULL;
293
- TransactionHandle *transaction = NULL;
294
-
295
- switch(type)
296
- {
297
- case isc_info_sql_stmt_select :
298
- case isc_info_sql_stmt_select_for_upd :
299
- case isc_info_sql_stmt_exec_procedure :
300
- result = rb_result_set_new(rb_iv_get(self, "@connection"),
301
- rb_iv_get(self, "@transaction"),
302
- rb_iv_get(self, "@sql"),
303
- rb_iv_get(self, "@dialect"),
304
- rb_ary_new());
305
- break;
306
-
307
- case isc_info_sql_stmt_insert :
308
- case isc_info_sql_stmt_update :
309
- case isc_info_sql_stmt_delete :
310
- Data_Get_Struct(self, StatementHandle, statement);
311
- Data_Get_Struct(rb_iv_get(self, "@transaction"), TransactionHandle,
312
- transaction);
313
- execute(&transaction->handle, &statement->handle, statement->dialect,
314
- NULL, statement->type, &affected);
315
- result = INT2NUM(affected);
316
- break;
317
-
318
- default :
319
- Data_Get_Struct(self, StatementHandle, statement);
320
- Data_Get_Struct(rb_iv_get(self, "@transaction"), TransactionHandle,
321
- transaction);
322
- execute(&transaction->handle, &statement->handle, statement->dialect,
323
- NULL, statement->type, &affected);
324
- result = Qnil;
325
- }
326
-
327
- return(result);
264
+ VALUE executeStatement(VALUE self) {
265
+ VALUE result;
266
+ int type = FIX2INT(getStatementType(self));
267
+ long affected = 0;
268
+ StatementHandle *statement = NULL;
269
+ TransactionHandle *transaction = NULL;
270
+
271
+ switch(type) {
272
+ case isc_info_sql_stmt_select:
273
+ case isc_info_sql_stmt_select_for_upd:
274
+ case isc_info_sql_stmt_exec_procedure:
275
+ result = rb_result_set_new(rb_iv_get(self, "@connection"),
276
+ rb_iv_get(self, "@transaction"),
277
+ rb_iv_get(self, "@sql"),
278
+ rb_iv_get(self, "@dialect"),
279
+ rb_ary_new());
280
+ break;
281
+
282
+ case isc_info_sql_stmt_insert:
283
+ case isc_info_sql_stmt_update:
284
+ case isc_info_sql_stmt_delete:
285
+ Data_Get_Struct(self, StatementHandle, statement);
286
+ Data_Get_Struct(rb_iv_get(self, "@transaction"), TransactionHandle,
287
+ transaction);
288
+ execute(&transaction->handle, &statement->handle, statement->dialect,
289
+ NULL, statement->type, &affected);
290
+ result = INT2NUM(affected);
291
+ break;
292
+
293
+ default:
294
+ Data_Get_Struct(self, StatementHandle, statement);
295
+ Data_Get_Struct(rb_iv_get(self, "@transaction"), TransactionHandle,
296
+ transaction);
297
+ execute(&transaction->handle, &statement->handle, statement->dialect,
298
+ NULL, statement->type, &affected);
299
+ result = Qnil;
300
+ }
301
+
302
+ return(result);
328
303
  }
329
304
 
330
305
 
@@ -337,73 +312,65 @@ VALUE executeStatement(VALUE self)
337
312
  * executing the statement.
338
313
  *
339
314
  * @return One of a count of the number of rows affected by the SQL statement,
340
- * a ResultSet object for a query or nil.
315
+ * a ResultSet object for a query or nil.
341
316
  *
342
317
  */
343
- VALUE executeStatementFor(VALUE self, VALUE parameters)
344
- {
345
- VALUE result = Qnil;
346
- int type = FIX2INT(getStatementType(self));
347
- long affected = 0;
348
- StatementHandle *statement = NULL;
349
- TransactionHandle *transaction = NULL;
350
-
351
- if(type == isc_info_sql_stmt_select ||
352
- type == isc_info_sql_stmt_select_for_upd)
353
- {
354
- /* Execute the statement via a ResultSet object. */
355
- result = rb_result_set_new(rb_iv_get(self, "@connection"),
356
- rb_iv_get(self, "@transaction"),
357
- rb_iv_get(self, "@sql"),
358
- rb_iv_get(self, "@dialect"),
359
- parameters);
360
- }
361
- else
362
- {
363
- /* Check that sufficient parameters have been specified. */
364
- Data_Get_Struct(self, StatementHandle, statement);
365
- if(statement->inputs > 0)
366
- {
367
- VALUE value = Qnil;
368
- int size = 0;
369
-
370
- if(parameters == Qnil)
371
- {
372
- rb_fireruby_raise(NULL,
373
- "Empty parameter list specified for statement.");
374
- }
375
-
376
- value = rb_funcall(parameters, rb_intern("size"), 0);
377
- size = TYPE(value) == T_FIXNUM ? FIX2INT(value) : NUM2INT(value);
378
- if(size < statement->inputs)
379
- {
380
- rb_fireruby_raise(NULL,
381
- "Insufficient parameters specified for statement.");
382
- }
383
-
384
- /* Allocate the XSQLDA and populate it. */
385
- statement->parameters = allocateInXSQLDA(statement->inputs,
386
- &statement->handle,
387
- statement->dialect);
388
- prepareDataArea(statement->parameters);
389
- setParameters(statement->parameters, parameters, self);
318
+ VALUE executeStatementFor(VALUE self, VALUE parameters) {
319
+ VALUE result = Qnil;
320
+ int type = FIX2INT(getStatementType(self));
321
+ long affected = 0;
322
+ StatementHandle *statement = NULL;
323
+ TransactionHandle *transaction = NULL;
324
+
325
+ if(type == isc_info_sql_stmt_select ||
326
+ type == isc_info_sql_stmt_select_for_upd) {
327
+ /* Execute the statement via a ResultSet object. */
328
+ result = rb_result_set_new(rb_iv_get(self, "@connection"),
329
+ rb_iv_get(self, "@transaction"),
330
+ rb_iv_get(self, "@sql"),
331
+ rb_iv_get(self, "@dialect"),
332
+ parameters);
333
+ } else {
334
+ /* Check that sufficient parameters have been specified. */
335
+ Data_Get_Struct(self, StatementHandle, statement);
336
+ if(statement->inputs > 0) {
337
+ VALUE value = Qnil;
338
+ int size = 0;
339
+
340
+ if(parameters == Qnil) {
341
+ rb_fireruby_raise(NULL,
342
+ "Empty parameter list specified for statement.");
390
343
  }
391
-
392
- /* Execute the statement. */
393
- Data_Get_Struct(self, StatementHandle, statement);
394
- Data_Get_Struct(rb_iv_get(self, "@transaction"), TransactionHandle,
395
- transaction);
396
- execute(&transaction->handle, &statement->handle, statement->dialect,
397
- statement->parameters, statement->type, &affected);
398
- if(type == isc_info_sql_stmt_insert ||
399
- type == isc_info_sql_stmt_update ||
400
- type == isc_info_sql_stmt_delete)
401
- {
402
- result = INT2NUM(affected);
344
+
345
+ value = rb_funcall(parameters, rb_intern("size"), 0);
346
+ size = TYPE(value) == T_FIXNUM ? FIX2INT(value) : NUM2INT(value);
347
+ if(size < statement->inputs) {
348
+ rb_fireruby_raise(NULL,
349
+ "Insufficient parameters specified for statement.");
403
350
  }
404
- }
405
-
406
- return(result);
351
+
352
+ /* Allocate the XSQLDA and populate it. */
353
+ statement->parameters = allocateInXSQLDA(statement->inputs,
354
+ &statement->handle,
355
+ statement->dialect);
356
+ prepareDataArea(statement->parameters);
357
+ setParameters(statement->parameters, parameters, self);
358
+ }
359
+
360
+ /* Execute the statement. */
361
+ Data_Get_Struct(self, StatementHandle, statement);
362
+ Data_Get_Struct(rb_iv_get(self, "@transaction"), TransactionHandle,
363
+ transaction);
364
+ execute(&transaction->handle, &statement->handle, statement->dialect,
365
+ statement->parameters, statement->type, &affected);
366
+ if(type == isc_info_sql_stmt_insert ||
367
+ type == isc_info_sql_stmt_update ||
368
+ type == isc_info_sql_stmt_delete) {
369
+ result = INT2NUM(affected);
370
+ }
371
+ }
372
+
373
+ return(result);
407
374
  }
408
375
 
409
376
 
@@ -416,27 +383,23 @@ VALUE executeStatementFor(VALUE self, VALUE parameters)
416
383
  * @return A reference to the newly closed Statement object.
417
384
  *
418
385
  */
419
- VALUE closeStatement(VALUE self)
420
- {
421
- StatementHandle *statement = NULL;
422
-
423
- Data_Get_Struct(self, StatementHandle, statement);
424
- if(statement->handle != 0)
425
- {
426
- ISC_STATUS status[20];
427
-
428
- if(isc_dsql_free_statement(status, &statement->handle, DSQL_drop))
429
- {
430
- rb_fireruby_raise(status, "Error closing statement.");
431
- }
432
-
433
- if(statement->parameters != NULL)
434
- {
435
- releaseDataArea(statement->parameters);
436
- }
437
- }
438
-
439
- return(self);
386
+ VALUE closeStatement(VALUE self) {
387
+ StatementHandle *statement = NULL;
388
+
389
+ Data_Get_Struct(self, StatementHandle, statement);
390
+ if(statement->handle != 0) {
391
+ ISC_STATUS status[ISC_STATUS_LENGTH];
392
+
393
+ if(isc_dsql_free_statement(status, &statement->handle, DSQL_drop)) {
394
+ rb_fireruby_raise(status, "Error closing statement.");
395
+ }
396
+
397
+ if(statement->parameters != NULL) {
398
+ releaseDataArea(statement->parameters);
399
+ }
400
+ }
401
+
402
+ return(self);
440
403
  }
441
404
 
442
405
 
@@ -461,52 +424,46 @@ VALUE closeStatement(VALUE self)
461
424
  */
462
425
  void prepare(isc_db_handle *connection, isc_tr_handle *transaction,
463
426
  char *sql, isc_stmt_handle *statement, short dialect,
464
- int *type, int *inputs, int *outputs)
465
- {
466
- ISC_STATUS status[20];
467
- XSQLDA *da = NULL;
468
- char list[] = {isc_info_sql_stmt_type},
469
- info[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
470
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
471
-
472
- /* Prepare the statement. */
473
- if(isc_dsql_allocate_statement(status, connection, statement))
474
- {
475
- rb_fireruby_raise(status, "Error allocating a SQL statement.");
476
- }
477
-
478
- da = (XSQLDA *)ALLOC_N(char, XSQLDA_LENGTH(1));
479
- if(da == NULL)
480
- {
481
- rb_raise(rb_eNoMemError,
482
- "Memory allocation failure preparing a statement.");
483
- }
484
- da->version = SQLDA_VERSION1;
485
- da->sqln = 1;
486
- if(isc_dsql_prepare(status, transaction, statement, 0, sql, dialect,
487
- da))
488
- {
489
- free(da);
490
- rb_fireruby_raise(status, "Error preparing a SQL statement.");
491
- }
492
- *outputs = da->sqld;
493
-
494
- /* Get the parameter count. */
495
- if(isc_dsql_describe_bind(status, statement, dialect, da))
496
- {
497
- free(da);
498
- rb_fireruby_raise(status, "Error determining statement parameters.");
499
- }
500
- *inputs = da->sqld;
501
- free(da);
502
-
503
- /* Get the statement type details. */
504
- if(isc_dsql_sql_info(status, statement, 1, list, 20, info) ||
505
- info[0] != isc_info_sql_stmt_type)
506
- {
507
- rb_fireruby_raise(status, "Error determining SQL statement type.");
508
- }
509
- *type = isc_vax_integer(&info[3], isc_vax_integer(&info[1], 2));
427
+ int *type, int *inputs, int *outputs) {
428
+ ISC_STATUS status[ISC_STATUS_LENGTH];
429
+ XSQLDA *da = NULL;
430
+ char list[] = {isc_info_sql_stmt_type},
431
+ info[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
432
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
433
+
434
+ /* Prepare the statement. */
435
+ if(isc_dsql_allocate_statement(status, connection, statement)) {
436
+ rb_fireruby_raise(status, "Error allocating a SQL statement.");
437
+ }
438
+
439
+ da = (XSQLDA *)ALLOC_N(char, XSQLDA_LENGTH(1));
440
+ if(da == NULL) {
441
+ rb_raise(rb_eNoMemError,
442
+ "Memory allocation failure preparing a statement.");
443
+ }
444
+ da->version = SQLDA_VERSION1;
445
+ da->sqln = 1;
446
+ if(isc_dsql_prepare(status, transaction, statement, 0, sql, dialect,
447
+ da)) {
448
+ free(da);
449
+ rb_fireruby_raise(status, "Error preparing a SQL statement.");
450
+ }
451
+ *outputs = da->sqld;
452
+
453
+ /* Get the parameter count. */
454
+ if(isc_dsql_describe_bind(status, statement, dialect, da)) {
455
+ free(da);
456
+ rb_fireruby_raise(status, "Error determining statement parameters.");
457
+ }
458
+ *inputs = da->sqld;
459
+ free(da);
460
+
461
+ /* Get the statement type details. */
462
+ if(isc_dsql_sql_info(status, statement, 1, list, 20, info) ||
463
+ info[0] != isc_info_sql_stmt_type) {
464
+ rb_fireruby_raise(status, "Error determining SQL statement type.");
465
+ }
466
+ *type = isc_vax_integer(&info[3], isc_vax_integer(&info[1], 2));
510
467
  }
511
468
 
512
469
 
@@ -517,8 +474,8 @@ void prepare(isc_db_handle *connection, isc_tr_handle *transaction,
517
474
  * executing the statement.
518
475
  * @param statement A pointer to the Firebird statement handle to be used in
519
476
  * executing the statement.
520
- * @param dialect Database dialect used in the statement
521
- *
477
+ * @param dialect Database dialect used in the statement
478
+ *
522
479
  * @param parameters A pointer to the XSQLDA block that contains the input
523
480
  * parameters for the SQL statement.
524
481
  * @param type A integer containing the type details relating to the
@@ -529,69 +486,62 @@ void prepare(isc_db_handle *connection, isc_tr_handle *transaction,
529
486
  * data generated by the execution.
530
487
  */
531
488
  void execute_2(isc_tr_handle *transaction, isc_stmt_handle *statement,
532
- short dialect, XSQLDA *parameters, int type, long *affected, XSQLDA *output)
533
- {
534
- ISC_STATUS status[20];
535
- ISC_STATUS execute_result;
536
-
537
- if(output) {
538
- execute_result = isc_dsql_execute2(status, transaction, statement, dialect, parameters, output);
539
- } else {
540
- execute_result = isc_dsql_execute(status, transaction, statement, dialect, parameters);
541
- }
542
- if(execute_result)
543
- {
544
- rb_fireruby_raise(status, "Error executing SQL statement.");
545
- }
546
-
547
- /* Check if a row count is needed. */
548
- if(type == isc_info_sql_stmt_update || type == isc_info_sql_stmt_delete ||
549
- type == isc_info_sql_stmt_insert)
550
- {
551
- int info = 0,
552
- done = 0;
553
- char items[] = {isc_info_sql_records},
554
- buffer[40],
555
- *position = buffer + 3;
556
-
557
- switch(type)
558
- {
559
- case isc_info_sql_stmt_update :
560
- info = isc_info_req_update_count;
561
- break;
562
-
563
- case isc_info_sql_stmt_delete :
564
- info = isc_info_req_delete_count;
565
- break;
566
-
567
- case isc_info_sql_stmt_insert :
568
- info = isc_info_req_insert_count;
569
- break;
570
- }
571
-
572
- if(isc_dsql_sql_info(status, statement, sizeof(items), items,
573
- sizeof(buffer), buffer))
574
- {
575
- rb_fireruby_raise(status, "Error retrieving affected row count.");
576
- }
577
-
578
- while(*position != isc_info_end && done == 0)
579
- {
580
- char current = *position++;
581
- long temp[] = {0, 0};
582
-
583
- temp[0] = isc_vax_integer(position, 2);
584
- position += 2;
585
- temp[1] = isc_vax_integer(position, temp[0]);
586
- position += temp[0];
587
-
588
- if(current == info)
589
- {
590
- *affected = temp[1];
591
- done = 1;
592
- }
489
+ short dialect, XSQLDA *parameters, int type, long *affected, XSQLDA *output) {
490
+ ISC_STATUS status[ISC_STATUS_LENGTH];
491
+ ISC_STATUS execute_result;
492
+
493
+ if(output) {
494
+ execute_result = isc_dsql_execute2(status, transaction, statement, dialect, parameters, output);
495
+ } else {
496
+ execute_result = isc_dsql_execute(status, transaction, statement, dialect, parameters);
497
+ }
498
+ if(execute_result) {
499
+ rb_fireruby_raise(status, "Error executing SQL statement.");
500
+ }
501
+
502
+ /* Check if a row count is needed. */
503
+ if(type == isc_info_sql_stmt_update || type == isc_info_sql_stmt_delete ||
504
+ type == isc_info_sql_stmt_insert) {
505
+ int info = 0,
506
+ done = 0;
507
+ char items[] = {isc_info_sql_records},
508
+ buffer[40],
509
+ *position = buffer + 3;
510
+
511
+ switch(type) {
512
+ case isc_info_sql_stmt_update:
513
+ info = isc_info_req_update_count;
514
+ break;
515
+
516
+ case isc_info_sql_stmt_delete:
517
+ info = isc_info_req_delete_count;
518
+ break;
519
+
520
+ case isc_info_sql_stmt_insert:
521
+ info = isc_info_req_insert_count;
522
+ break;
523
+ }
524
+
525
+ if(isc_dsql_sql_info(status, statement, sizeof(items), items,
526
+ sizeof(buffer), buffer)) {
527
+ rb_fireruby_raise(status, "Error retrieving affected row count.");
528
+ }
529
+
530
+ while(*position != isc_info_end && done == 0) {
531
+ char current = *position++;
532
+ long temp[] = {0, 0};
533
+
534
+ temp[0] = isc_vax_integer(position, 2);
535
+ position += 2;
536
+ temp[1] = isc_vax_integer(position, temp[0]);
537
+ position += temp[0];
538
+
539
+ if(current == info) {
540
+ *affected = temp[1];
541
+ done = 1;
593
542
  }
594
- }
543
+ }
544
+ }
595
545
  }
596
546
 
597
547
 
@@ -602,8 +552,8 @@ void execute_2(isc_tr_handle *transaction, isc_stmt_handle *statement,
602
552
  * executing the statement.
603
553
  * @param statement A pointer to the Firebird statement handle to be used in
604
554
  * executing the statement.
605
- * @param dialect Database dialect used in the statement
606
- *
555
+ * @param dialect Database dialect used in the statement
556
+ *
607
557
  * @param parameters A pointer to the XSQLDA block that contains the input
608
558
  * parameters for the SQL statement.
609
559
  * @param type A integer containing the type details relating to the
@@ -612,9 +562,8 @@ void execute_2(isc_tr_handle *transaction, isc_stmt_handle *statement,
612
562
  * of rows affected by inserts, updates or deletes.
613
563
  */
614
564
  void execute(isc_tr_handle *transaction, isc_stmt_handle *statement,
615
- short dialect, XSQLDA *parameters, int type, long *affected)
616
- {
617
- execute_2(transaction, statement, dialect, parameters, type, affected, NULL);
565
+ short dialect, XSQLDA *parameters, int type, long *affected) {
566
+ execute_2(transaction, statement, dialect, parameters, type, affected, NULL);
618
567
  }
619
568
 
620
569
  /**
@@ -633,13 +582,12 @@ void execute(isc_tr_handle *transaction, isc_stmt_handle *statement,
633
582
  *
634
583
  */
635
584
  VALUE rb_statement_new(VALUE connection, VALUE transaction, VALUE sql,
636
- VALUE dialect)
637
- {
638
- VALUE statement = allocateStatement(cStatement);
639
-
640
- initializeStatement(statement, connection, transaction, sql, dialect);
641
-
642
- return(statement);
585
+ VALUE dialect) {
586
+ VALUE statement = allocateStatement(cStatement);
587
+
588
+ initializeStatement(statement, connection, transaction, sql, dialect);
589
+
590
+ return(statement);
643
591
  }
644
592
 
645
593
 
@@ -652,9 +600,8 @@ VALUE rb_statement_new(VALUE connection, VALUE transaction, VALUE sql,
652
600
  * @return A reference to the results of executing the statement.
653
601
  *
654
602
  */
655
- VALUE rb_execute_statement(VALUE statement)
656
- {
657
- return(executeStatement(statement));
603
+ VALUE rb_execute_statement(VALUE statement) {
604
+ return(executeStatement(statement));
658
605
  }
659
606
 
660
607
 
@@ -669,9 +616,8 @@ VALUE rb_execute_statement(VALUE statement)
669
616
  * @return A reference to the results of executing the statement.
670
617
  *
671
618
  */
672
- VALUE rb_execute_statement_for(VALUE statement, VALUE parameters)
673
- {
674
- return(executeStatementFor(statement, parameters));
619
+ VALUE rb_execute_statement_for(VALUE statement, VALUE parameters) {
620
+ return(executeStatementFor(statement, parameters));
675
621
  }
676
622
 
677
623
 
@@ -683,9 +629,8 @@ VALUE rb_execute_statement_for(VALUE statement, VALUE parameters)
683
629
  * @return A reference to an integer containing the statement type details.
684
630
  *
685
631
  */
686
- VALUE rb_get_statement_type(VALUE statement)
687
- {
688
- return(getStatementType(statement));
632
+ VALUE rb_get_statement_type(VALUE statement) {
633
+ return(getStatementType(statement));
689
634
  }
690
635
 
691
636
 
@@ -695,9 +640,8 @@ VALUE rb_get_statement_type(VALUE statement)
695
640
  * @param statement A reference to the Statement object to be closed.
696
641
  *
697
642
  */
698
- void rb_statement_close(VALUE statement)
699
- {
700
- closeStatement(statement);
643
+ void rb_statement_close(VALUE statement) {
644
+ closeStatement(statement);
701
645
  }
702
646
 
703
647
 
@@ -709,25 +653,21 @@ void rb_statement_close(VALUE statement)
709
653
  * object being collected.
710
654
  *
711
655
  */
712
- void statementFree(void *handle)
713
- {
714
- if(handle != NULL)
715
- {
716
- StatementHandle *statement = (StatementHandle *)handle;
717
-
718
- if(statement->handle != 0)
719
- {
720
- ISC_STATUS status[20];
721
-
722
- isc_dsql_free_statement(status, &statement->handle, DSQL_drop);
723
- }
724
-
725
- if(statement->parameters)
726
- {
727
- releaseDataArea(statement->parameters);
728
- }
729
- free(statement);
730
- }
656
+ void statementFree(void *handle) {
657
+ if(handle != NULL) {
658
+ StatementHandle *statement = (StatementHandle *)handle;
659
+
660
+ if(statement->handle != 0) {
661
+ ISC_STATUS status[ISC_STATUS_LENGTH];
662
+
663
+ isc_dsql_free_statement(status, &statement->handle, DSQL_drop);
664
+ }
665
+
666
+ if(statement->parameters) {
667
+ releaseDataArea(statement->parameters);
668
+ }
669
+ free(statement);
670
+ }
731
671
  }
732
672
 
733
673
 
@@ -740,48 +680,47 @@ void statementFree(void *handle)
740
680
  * @param module A reference to the module to create the class within.
741
681
  *
742
682
  */
743
- void Init_Statement(VALUE module)
744
- {
745
- cStatement = rb_define_class_under(module, "Statement", rb_cObject);
746
- rb_define_alloc_func(cStatement, allocateStatement);
747
- rb_define_method(cStatement, "initialize", initializeStatement, 4);
748
- rb_define_method(cStatement, "initialize_copy", forbidObjectCopy, 1);
749
- rb_define_method(cStatement, "sql", getStatementSQL, 0);
750
- rb_define_method(cStatement, "connection", getStatementConnection, 0);
751
- rb_define_method(cStatement, "transaction", getStatementTransaction, 0);
752
- rb_define_method(cStatement, "dialect", getStatementDialect, 0);
753
- rb_define_method(cStatement, "type", getStatementType, 0);
754
- rb_define_method(cStatement, "execute", executeStatement, 0);
755
- rb_define_method(cStatement, "execute_for", executeStatementFor, 1);
756
- rb_define_method(cStatement, "close", closeStatement, 0);
757
- rb_define_method(cStatement, "parameter_count", getStatementParameterCount, 0);
758
-
759
- rb_define_const(cStatement, "SELECT_STATEMENT",
760
- INT2FIX(isc_info_sql_stmt_select));
761
- rb_define_const(cStatement, "INSERT_STATEMENT",
762
- INT2FIX(isc_info_sql_stmt_insert));
763
- rb_define_const(cStatement, "UPDATE_STATEMENT",
764
- INT2FIX(isc_info_sql_stmt_update));
765
- rb_define_const(cStatement, "DELETE_STATEMENT",
766
- INT2FIX(isc_info_sql_stmt_delete));
767
- rb_define_const(cStatement, "DDL_STATEMENT",
768
- INT2FIX(isc_info_sql_stmt_ddl));
769
- rb_define_const(cStatement, "GET_SEGMENT_STATEMENT",
770
- INT2FIX(isc_info_sql_stmt_get_segment));
771
- rb_define_const(cStatement, "PUT_SEGMENT_STATEMENT",
772
- INT2FIX(isc_info_sql_stmt_put_segment));
773
- rb_define_const(cStatement, "EXECUTE_PROCEDURE_STATEMENT",
774
- INT2FIX(isc_info_sql_stmt_exec_procedure));
775
- rb_define_const(cStatement, "START_TRANSACTION_STATEMENT",
776
- INT2FIX(isc_info_sql_stmt_start_trans));
777
- rb_define_const(cStatement, "COMMIT_STATEMENT",
778
- INT2FIX(isc_info_sql_stmt_commit));
779
- rb_define_const(cStatement, "ROLLBACK_STATEMENT",
780
- INT2FIX(isc_info_sql_stmt_rollback));
781
- rb_define_const(cStatement, "SELECT_FOR_UPDATE_STATEMENT",
782
- INT2FIX(isc_info_sql_stmt_select_for_upd));
783
- rb_define_const(cStatement, "SET_GENERATOR_STATEMENT",
784
- INT2FIX(isc_info_sql_stmt_set_generator));
785
- rb_define_const(cStatement, "SAVE_POINT_STATEMENT",
786
- INT2FIX(isc_info_sql_stmt_savepoint));
683
+ void Init_Statement(VALUE module) {
684
+ cStatement = rb_define_class_under(module, "Statement", rb_cObject);
685
+ rb_define_alloc_func(cStatement, allocateStatement);
686
+ rb_define_method(cStatement, "initialize", initializeStatement, 4);
687
+ rb_define_method(cStatement, "initialize_copy", forbidObjectCopy, 1);
688
+ rb_define_method(cStatement, "sql", getStatementSQL, 0);
689
+ rb_define_method(cStatement, "connection", getStatementConnection, 0);
690
+ rb_define_method(cStatement, "transaction", getStatementTransaction, 0);
691
+ rb_define_method(cStatement, "dialect", getStatementDialect, 0);
692
+ rb_define_method(cStatement, "type", getStatementType, 0);
693
+ rb_define_method(cStatement, "execute", executeStatement, 0);
694
+ rb_define_method(cStatement, "execute_for", executeStatementFor, 1);
695
+ rb_define_method(cStatement, "close", closeStatement, 0);
696
+ rb_define_method(cStatement, "parameter_count", getStatementParameterCount, 0);
697
+
698
+ rb_define_const(cStatement, "SELECT_STATEMENT",
699
+ INT2FIX(isc_info_sql_stmt_select));
700
+ rb_define_const(cStatement, "INSERT_STATEMENT",
701
+ INT2FIX(isc_info_sql_stmt_insert));
702
+ rb_define_const(cStatement, "UPDATE_STATEMENT",
703
+ INT2FIX(isc_info_sql_stmt_update));
704
+ rb_define_const(cStatement, "DELETE_STATEMENT",
705
+ INT2FIX(isc_info_sql_stmt_delete));
706
+ rb_define_const(cStatement, "DDL_STATEMENT",
707
+ INT2FIX(isc_info_sql_stmt_ddl));
708
+ rb_define_const(cStatement, "GET_SEGMENT_STATEMENT",
709
+ INT2FIX(isc_info_sql_stmt_get_segment));
710
+ rb_define_const(cStatement, "PUT_SEGMENT_STATEMENT",
711
+ INT2FIX(isc_info_sql_stmt_put_segment));
712
+ rb_define_const(cStatement, "EXECUTE_PROCEDURE_STATEMENT",
713
+ INT2FIX(isc_info_sql_stmt_exec_procedure));
714
+ rb_define_const(cStatement, "START_TRANSACTION_STATEMENT",
715
+ INT2FIX(isc_info_sql_stmt_start_trans));
716
+ rb_define_const(cStatement, "COMMIT_STATEMENT",
717
+ INT2FIX(isc_info_sql_stmt_commit));
718
+ rb_define_const(cStatement, "ROLLBACK_STATEMENT",
719
+ INT2FIX(isc_info_sql_stmt_rollback));
720
+ rb_define_const(cStatement, "SELECT_FOR_UPDATE_STATEMENT",
721
+ INT2FIX(isc_info_sql_stmt_select_for_upd));
722
+ rb_define_const(cStatement, "SET_GENERATOR_STATEMENT",
723
+ INT2FIX(isc_info_sql_stmt_set_generator));
724
+ rb_define_const(cStatement, "SAVE_POINT_STATEMENT",
725
+ INT2FIX(isc_info_sql_stmt_savepoint));
787
726
  }