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/Transaction.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 "Transaction.h"
28
28
  #include "Common.h"
@@ -45,11 +45,10 @@ void transactionFree(void *);
45
45
  VALUE cTransaction;
46
46
 
47
47
  /* Type definitions. */
48
- typedef struct
49
- {
50
- isc_db_handle *database;
51
- long length;
52
- char *tpb;
48
+ typedef struct {
49
+ isc_db_handle *database;
50
+ long length;
51
+ char *tpb;
53
52
  } ISC_TEB;
54
53
 
55
54
  static char DEFAULT_TEB[] = {isc_tpb_version3,
@@ -57,7 +56,7 @@ static char DEFAULT_TEB[] = {isc_tpb_version3,
57
56
  isc_tpb_wait,
58
57
  isc_tpb_rec_version,
59
58
  isc_tpb_read_committed};
60
- static int DEFAULT_TEB_SIZE = 5;
59
+ static int DEFAULT_TEB_SIZE = 5;
61
60
 
62
61
 
63
62
  /**
@@ -69,23 +68,19 @@ static int DEFAULT_TEB_SIZE = 5;
69
68
  * @return A reference to the newly allocated object.
70
69
  *
71
70
  */
72
- static VALUE allocateTransaction(VALUE klass)
73
- {
74
- VALUE transaction = Qnil;
75
- TransactionHandle *handle = ALLOC(TransactionHandle);
76
-
77
- if(handle != NULL)
78
- {
79
- handle->handle = 0;
80
- transaction = Data_Wrap_Struct(klass, NULL, transactionFree, handle);
81
- }
82
- else
83
- {
84
- rb_raise(rb_eNoMemError,
85
- "Memory allocation failure allocating a transaction.");
86
- }
87
-
88
- return(transaction);
71
+ static VALUE allocateTransaction(VALUE klass) {
72
+ VALUE transaction = Qnil;
73
+ TransactionHandle *handle = ALLOC(TransactionHandle);
74
+
75
+ if(handle != NULL) {
76
+ handle->handle = 0;
77
+ transaction = Data_Wrap_Struct(klass, NULL, transactionFree, handle);
78
+ } else {
79
+ rb_raise(rb_eNoMemError,
80
+ "Memory allocation failure allocating a transaction.");
81
+ }
82
+
83
+ return(transaction);
89
84
  }
90
85
 
91
86
 
@@ -94,41 +89,35 @@ static VALUE allocateTransaction(VALUE klass)
94
89
  *
95
90
  * @param self A reference to the new Transaction class instance.
96
91
  * @param connections Either a reference to a single Connection object or to
97
- * an array of Connection objects that the transaction
92
+ * an array of Connection objects that the transaction
98
93
  * will apply to.
99
94
  *
100
95
  */
101
- static VALUE transactionInitialize(VALUE self, VALUE connections)
102
- {
103
- TransactionHandle *transaction = NULL;
104
- VALUE array = Qnil;
105
-
106
- /* Determine if an array has been passed as a parameter. */
107
- if(TYPE(connections) == T_ARRAY)
108
- {
109
- array = connections;
110
- }
111
- else if(TYPE(connections) == T_DATA &&
112
- RDATA(connections)->dfree == (RUBY_DATA_FUNC)connectionFree)
113
- {
114
- array = rb_ary_new();
115
- rb_ary_push(array, connections);
116
- }
117
- else
118
- {
119
- rb_fireruby_raise(NULL,
120
- "Invalid connection parameter(s) for transaction.");
121
- }
122
-
123
- /* Store the database details. */
124
- rb_iv_set(self, "@connections", array);
125
-
126
- /* Fetch the data structure and start the transaction. */
127
- Data_Get_Struct(self, TransactionHandle, transaction);
128
- startTransaction(transaction, array, 0, NULL);
129
- rb_tx_started(self, array);
130
-
131
- return(self);
96
+ static VALUE transactionInitialize(VALUE self, VALUE connections) {
97
+ TransactionHandle *transaction = NULL;
98
+ VALUE array = Qnil;
99
+
100
+ /* Determine if an array has been passed as a parameter. */
101
+ if(TYPE(connections) == T_ARRAY) {
102
+ array = connections;
103
+ } else if(TYPE(connections) == T_DATA &&
104
+ RDATA(connections)->dfree == (RUBY_DATA_FUNC)connectionFree) {
105
+ array = rb_ary_new();
106
+ rb_ary_push(array, connections);
107
+ } else {
108
+ rb_fireruby_raise(NULL,
109
+ "Invalid connection parameter(s) for transaction.");
110
+ }
111
+
112
+ /* Store the database details. */
113
+ rb_iv_set(self, "@connections", array);
114
+
115
+ /* Fetch the data structure and start the transaction. */
116
+ Data_Get_Struct(self, TransactionHandle, transaction);
117
+ startTransaction(transaction, array, 0, NULL);
118
+ rb_tx_started(self, array);
119
+
120
+ return(self);
132
121
  }
133
122
 
134
123
 
@@ -140,37 +129,32 @@ static VALUE transactionInitialize(VALUE self, VALUE connections)
140
129
  * @return A reference to self if successful, nil otherwise.
141
130
  *
142
131
  */
143
- static VALUE commitTransaction(VALUE self)
144
- {
145
- TransactionHandle *transaction = NULL;
146
-
147
- Data_Get_Struct(self, TransactionHandle, transaction);
148
-
149
- /* Commit the transaction. */
150
- if(transaction->handle != 0)
151
- {
152
- ISC_STATUS status[20];
153
-
154
- if(isc_commit_transaction(status, &transaction->handle) != 0)
155
- {
156
- /* Generate an error. */
157
- rb_fireruby_raise(status, "Error committing transaction.");
158
- }
159
- transaction->handle = 0;
160
- }
161
- else
162
- {
132
+ static VALUE commitTransaction(VALUE self) {
133
+ TransactionHandle *transaction = NULL;
134
+
135
+ Data_Get_Struct(self, TransactionHandle, transaction);
136
+
137
+ /* Commit the transaction. */
138
+ if(transaction->handle != 0) {
139
+ ISC_STATUS status[ISC_STATUS_LENGTH];
140
+
141
+ if(isc_commit_transaction(status, &transaction->handle) != 0) {
163
142
  /* Generate an error. */
164
- rb_fireruby_raise(NULL, "1. Transaction is not active.");
165
- }
166
-
167
- /* Notify each connection of the transactions end. */
168
- rb_tx_released(rb_iv_get(self, "@connections"), self);
169
-
170
- /* Clear the connections list. */
171
- rb_iv_set(self, "@connections", rb_ary_new());
172
-
173
- return(self);
143
+ rb_fireruby_raise(status, "Error committing transaction.");
144
+ }
145
+ transaction->handle = 0;
146
+ } else {
147
+ /* Generate an error. */
148
+ rb_fireruby_raise(NULL, "1. Transaction is not active.");
149
+ }
150
+
151
+ /* Notify each connection of the transactions end. */
152
+ rb_tx_released(rb_iv_get(self, "@connections"), self);
153
+
154
+ /* Clear the connections list. */
155
+ rb_iv_set(self, "@connections", rb_ary_new());
156
+
157
+ return(self);
174
158
  }
175
159
 
176
160
 
@@ -182,37 +166,32 @@ static VALUE commitTransaction(VALUE self)
182
166
  * @return A reference to self if successful, nil otherwise.
183
167
  *
184
168
  */
185
- static VALUE rollbackTransaction(VALUE self)
186
- {
187
- TransactionHandle *transaction = NULL;
188
-
189
- Data_Get_Struct(self, TransactionHandle, transaction);
190
-
191
- /* Roll back the transaction. */
192
- if(transaction->handle != 0)
193
- {
194
- ISC_STATUS status[20];
195
-
196
- if(isc_rollback_transaction(status, &transaction->handle) != 0)
197
- {
198
- /* Generate an error. */
199
- rb_fireruby_raise(status, "Error rolling back transaction.");
200
- }
201
- transaction->handle = 0;
202
- }
203
- else
204
- {
169
+ static VALUE rollbackTransaction(VALUE self) {
170
+ TransactionHandle *transaction = NULL;
171
+
172
+ Data_Get_Struct(self, TransactionHandle, transaction);
173
+
174
+ /* Roll back the transaction. */
175
+ if(transaction->handle != 0) {
176
+ ISC_STATUS status[ISC_STATUS_LENGTH];
177
+
178
+ if(isc_rollback_transaction(status, &transaction->handle) != 0) {
205
179
  /* Generate an error. */
206
- rb_fireruby_raise(NULL, "2. Transaction is not active.");
207
- }
208
-
209
- /* Notify each connection of the transactions end. */
210
- rb_tx_released(rb_iv_get(self, "@connections"), self);
211
-
212
- /* Clear the connections list. */
213
- rb_iv_set(self, "@connections", rb_ary_new());
214
-
215
- return(self);
180
+ rb_fireruby_raise(status, "Error rolling back transaction.");
181
+ }
182
+ transaction->handle = 0;
183
+ } else {
184
+ /* Generate an error. */
185
+ rb_fireruby_raise(NULL, "2. Transaction is not active.");
186
+ }
187
+
188
+ /* Notify each connection of the transactions end. */
189
+ rb_tx_released(rb_iv_get(self, "@connections"), self);
190
+
191
+ /* Clear the connections list. */
192
+ rb_iv_set(self, "@connections", rb_ary_new());
193
+
194
+ return(self);
216
195
  }
217
196
 
218
197
 
@@ -224,18 +203,16 @@ static VALUE rollbackTransaction(VALUE self)
224
203
  * @return Qtrue if the transaction is active, Qfalse otherwise.
225
204
  *
226
205
  */
227
- static VALUE transactionIsActive(VALUE self)
228
- {
229
- VALUE result = Qfalse;
230
- TransactionHandle *transaction = NULL;
231
-
232
- Data_Get_Struct(self, TransactionHandle, transaction);
233
- if(transaction->handle != 0)
234
- {
235
- result = Qtrue;
236
- }
237
-
238
- return(result);
206
+ static VALUE transactionIsActive(VALUE self) {
207
+ VALUE result = Qfalse;
208
+ TransactionHandle *transaction = NULL;
209
+
210
+ Data_Get_Struct(self, TransactionHandle, transaction);
211
+ if(transaction->handle != 0) {
212
+ result = Qtrue;
213
+ }
214
+
215
+ return(result);
239
216
  }
240
217
 
241
218
 
@@ -247,12 +224,11 @@ static VALUE transactionIsActive(VALUE self)
247
224
  * for.
248
225
  *
249
226
  * @return An Array containing the connections associated with the Transaction
250
- * object.
227
+ * object.
251
228
  *
252
229
  */
253
- static VALUE getTransactionConnections(VALUE self)
254
- {
255
- return(rb_iv_get(self, "@connections"));
230
+ static VALUE getTransactionConnections(VALUE self) {
231
+ return(rb_iv_get(self, "@connections"));
256
232
  }
257
233
 
258
234
 
@@ -267,30 +243,28 @@ static VALUE getTransactionConnections(VALUE self)
267
243
  * Qfalse otherwise.
268
244
  *
269
245
  */
270
- static VALUE isTransactionFor(VALUE self, VALUE connection)
271
- {
272
- VALUE result = Qfalse,
273
- array = rb_iv_get(self, "@connections"),
274
- value = rb_funcall(array, rb_intern("size"), 0);
275
- int size = 0,
276
- index;
277
- ConnectionHandle *instance = NULL;
278
- TransactionHandle *transaction = NULL;
279
-
280
- size = (TYPE(value) == T_FIXNUM ? FIX2INT(value) : NUM2INT(value));
281
- Data_Get_Struct(self, TransactionHandle, transaction);
282
- Data_Get_Struct(connection, ConnectionHandle, instance);
283
-
284
- for(index = 0; index < size && result == Qfalse; index++)
285
- {
286
- ConnectionHandle *handle;
287
-
288
- value = rb_ary_entry(array, index);
289
- Data_Get_Struct(value, ConnectionHandle, handle);
290
- result = (handle == instance ? Qtrue : Qfalse);
291
- }
292
-
293
- return(result);
246
+ static VALUE isTransactionFor(VALUE self, VALUE connection) {
247
+ VALUE result = Qfalse,
248
+ array = rb_iv_get(self, "@connections"),
249
+ value = rb_funcall(array, rb_intern("size"), 0);
250
+ int size = 0,
251
+ index;
252
+ ConnectionHandle *instance = NULL;
253
+ TransactionHandle *transaction = NULL;
254
+
255
+ size = (TYPE(value) == T_FIXNUM ? FIX2INT(value) : NUM2INT(value));
256
+ Data_Get_Struct(self, TransactionHandle, transaction);
257
+ Data_Get_Struct(connection, ConnectionHandle, instance);
258
+
259
+ for(index = 0; index < size && result == Qfalse; index++) {
260
+ ConnectionHandle *handle;
261
+
262
+ value = rb_ary_entry(array, index);
263
+ Data_Get_Struct(value, ConnectionHandle, handle);
264
+ result = (handle == instance ? Qtrue : Qfalse);
265
+ }
266
+
267
+ return(result);
294
268
  }
295
269
 
296
270
 
@@ -306,57 +280,51 @@ static VALUE isTransactionFor(VALUE self, VALUE connection)
306
280
  * query, nil otherwise.
307
281
  *
308
282
  */
309
- static VALUE executeOnTransaction(VALUE self, VALUE sql)
310
- {
311
- VALUE results = Qnil,
312
- list = rb_iv_get(self, "@connections"),
313
- value = 0,
314
- connection = Qnil,
315
- statement = Qnil;
316
- TransactionHandle *transaction = NULL;
317
- int size = 0;
318
-
319
- /* Check that the transaction is active. */
320
- Data_Get_Struct(self, TransactionHandle, transaction);
321
- if(transaction->handle == 0)
322
- {
323
- rb_fireruby_raise(NULL, "Executed called on inactive transaction.");
324
- }
325
-
326
- /* Check that we have only one connection for the transaction. */
327
- value = rb_funcall(list, rb_intern("size"), 0);
328
- size = (TYPE(value) == T_FIXNUM ? FIX2INT(value) : NUM2INT(value));
329
- if(size > 1)
330
- {
331
- rb_fireruby_raise(NULL,
332
- "Execute called on a transaction that spans multiple "\
333
- "connections. Unable to determine which connection to "\
334
- "execute the SQL statement through.");
335
- }
336
-
337
- connection = rb_ary_entry(list, 0);
338
- statement = rb_statement_new(connection, self, sql, INT2FIX(3));
339
- results = rb_execute_statement(statement);
340
-
341
- if(results != Qnil && rb_obj_is_kind_of(results, rb_cInteger) == Qfalse)
342
- {
343
- if(rb_block_given_p())
344
- {
345
- VALUE row = rb_funcall(results, rb_intern("fetch"), 0),
346
- last = Qnil;
347
-
348
- while(row != Qnil)
349
- {
350
- last = rb_yield(row);
351
- row = rb_funcall(results, rb_intern("fetch"), 0);
352
- }
353
- rb_funcall(results, rb_intern("close"), 0);
354
- results = last;
283
+ static VALUE executeOnTransaction(VALUE self, VALUE sql) {
284
+ VALUE results = Qnil,
285
+ list = rb_iv_get(self, "@connections"),
286
+ value = 0,
287
+ connection = Qnil,
288
+ statement = Qnil;
289
+ TransactionHandle *transaction = NULL;
290
+ int size = 0;
291
+
292
+ /* Check that the transaction is active. */
293
+ Data_Get_Struct(self, TransactionHandle, transaction);
294
+ if(transaction->handle == 0) {
295
+ rb_fireruby_raise(NULL, "Executed called on inactive transaction.");
296
+ }
297
+
298
+ /* Check that we have only one connection for the transaction. */
299
+ value = rb_funcall(list, rb_intern("size"), 0);
300
+ size = (TYPE(value) == T_FIXNUM ? FIX2INT(value) : NUM2INT(value));
301
+ if(size > 1) {
302
+ rb_fireruby_raise(NULL,
303
+ "Execute called on a transaction that spans multiple " \
304
+ "connections. Unable to determine which connection to " \
305
+ "execute the SQL statement through.");
306
+ }
307
+
308
+ connection = rb_ary_entry(list, 0);
309
+ statement = rb_statement_new(connection, self, sql, INT2FIX(3));
310
+ results = rb_execute_statement(statement);
311
+
312
+ if(results != Qnil && rb_obj_is_kind_of(results, rb_cInteger) == Qfalse) {
313
+ if(rb_block_given_p()) {
314
+ VALUE row = rb_funcall(results, rb_intern("fetch"), 0),
315
+ last = Qnil;
316
+
317
+ while(row != Qnil) {
318
+ last = rb_yield(row);
319
+ row = rb_funcall(results, rb_intern("fetch"), 0);
355
320
  }
356
- }
357
- rb_statement_close(statement);
358
-
359
- return(results);
321
+ rb_funcall(results, rb_intern("close"), 0);
322
+ results = last;
323
+ }
324
+ }
325
+ rb_statement_close(statement);
326
+
327
+ return(results);
360
328
  }
361
329
 
362
330
 
@@ -374,76 +342,63 @@ static VALUE executeOnTransaction(VALUE self, VALUE sql)
374
342
  * @return A reference to the newly created Transaction object.
375
343
  *
376
344
  */
377
- static VALUE createTransaction(VALUE unused, VALUE connections, VALUE parameters)
378
- {
379
- VALUE instance = Qnil,
380
- list = Qnil;
381
- TransactionHandle *transaction = ALLOC(TransactionHandle);
382
-
383
- if(transaction == NULL)
384
- {
385
- rb_raise(rb_eNoMemError,
386
- "Memory allocation failure allocating a transaction.");
387
- }
388
- transaction->handle = 0;
389
-
390
- if(TYPE(connections) != T_ARRAY)
391
- {
392
- list = rb_ary_new();
393
- rb_ary_push(list, connections);
394
- }
395
- else
396
- {
397
- list = connections;
398
- }
399
-
400
- if(TYPE(parameters) != T_ARRAY)
401
- {
402
- rb_fireruby_raise(NULL,
403
- "Invalid transaction parameter set specified.");
404
- }
405
-
406
- if(transaction != NULL)
407
- {
408
- VALUE value = rb_funcall(parameters, rb_intern("size"), 0);
409
- long size = 0,
410
- index;
411
- char *buffer = NULL;
412
-
413
- size = TYPE(value) == T_FIXNUM ? FIX2INT(value) : NUM2INT(value);
414
- if((buffer = ALLOC_N(char, size)) != NULL)
415
- {
416
- for(index = 0; index < size; index++)
417
- {
418
- VALUE entry = rb_ary_entry(parameters, index);
419
- int number = 0;
420
-
421
- number = TYPE(entry) == T_FIXNUM ? FIX2INT(entry) : NUM2INT(entry);
422
- buffer[index] = number;
423
- }
424
-
425
- startTransaction(transaction, list, size, buffer);
426
- free(buffer);
427
-
428
- instance = Data_Wrap_Struct(cTransaction, NULL, transactionFree,
429
- transaction);
430
- rb_iv_set(instance, "@connections", list);
431
- rb_tx_started(instance, connections);
432
- }
433
- else
434
- {
435
- rb_fireruby_raise(NULL,
436
- "Memory allocation failure allocating transaction "\
437
- "parameter buffer.");
345
+ static VALUE createTransaction(VALUE unused, VALUE connections, VALUE parameters) {
346
+ VALUE instance = Qnil,
347
+ list = Qnil;
348
+ TransactionHandle *transaction = ALLOC(TransactionHandle);
349
+
350
+ if(transaction == NULL) {
351
+ rb_raise(rb_eNoMemError,
352
+ "Memory allocation failure allocating a transaction.");
353
+ }
354
+ transaction->handle = 0;
355
+
356
+ if(TYPE(connections) != T_ARRAY) {
357
+ list = rb_ary_new();
358
+ rb_ary_push(list, connections);
359
+ } else {
360
+ list = connections;
361
+ }
362
+
363
+ if(TYPE(parameters) != T_ARRAY) {
364
+ rb_fireruby_raise(NULL,
365
+ "Invalid transaction parameter set specified.");
366
+ }
367
+
368
+ if(transaction != NULL) {
369
+ VALUE value = rb_funcall(parameters, rb_intern("size"), 0);
370
+ long size = 0,
371
+ index;
372
+ char *buffer = NULL;
373
+
374
+ size = TYPE(value) == T_FIXNUM ? FIX2INT(value) : NUM2INT(value);
375
+ if((buffer = ALLOC_N(char, size)) != NULL) {
376
+ for(index = 0; index < size; index++) {
377
+ VALUE entry = rb_ary_entry(parameters, index);
378
+ int number = 0;
379
+
380
+ number = TYPE(entry) == T_FIXNUM ? FIX2INT(entry) : NUM2INT(entry);
381
+ buffer[index] = number;
438
382
  }
439
- }
440
- else
441
- {
442
- rb_raise(rb_eNoMemError,
443
- "Memory allocation failure allocating transaction.");
444
- }
445
-
446
- return(instance);
383
+
384
+ startTransaction(transaction, list, size, buffer);
385
+ free(buffer);
386
+
387
+ instance = Data_Wrap_Struct(cTransaction, NULL, transactionFree,
388
+ transaction);
389
+ rb_iv_set(instance, "@connections", list);
390
+ rb_tx_started(instance, connections);
391
+ } else {
392
+ rb_fireruby_raise(NULL,
393
+ "Memory allocation failure allocating transaction " \
394
+ "parameter buffer.");
395
+ }
396
+ } else {
397
+ rb_raise(rb_eNoMemError,
398
+ "Memory allocation failure allocating transaction.");
399
+ }
400
+
401
+ return(instance);
447
402
  }
448
403
 
449
404
 
@@ -463,108 +418,86 @@ static VALUE createTransaction(VALUE unused, VALUE connections, VALUE parameters
463
418
  void startTransaction(TransactionHandle *transaction,
464
419
  VALUE connections,
465
420
  long size,
466
- char *buffer)
467
- {
468
- ISC_TEB *teb = NULL;
469
- VALUE value = rb_funcall(connections, rb_intern("size"), 0),
470
- head = rb_ary_entry(connections, 0);
471
- short length = 0;
472
- ConnectionHandle *first = NULL;
473
-
474
- /* Attempt a retrieval of the first connection. */
475
- if(value != Qnil)
476
- {
477
- /* Check that we have a connection. */
478
- if(TYPE(head) == T_DATA &&
479
- RDATA(head)->dfree == (RUBY_DATA_FUNC)connectionFree)
480
- {
481
- Data_Get_Struct(head, ConnectionHandle, first);
482
- }
483
- }
484
-
485
- /* Generate the list of connections. */
486
- length = (TYPE(value) == T_FIXNUM ? FIX2INT(value) : NUM2INT(value));
487
- if(length > 0)
488
- {
489
- if((teb = ALLOC_N(ISC_TEB, length)) != NULL)
490
- {
491
- int i;
492
-
493
- for(i = 0; i < length; i++)
494
- {
495
- VALUE entry = rb_ary_entry(connections, i);
496
-
497
- /* Check that we have a connection. */
498
- if(TYPE(entry) == T_DATA &&
499
- RDATA(entry)->dfree == (RUBY_DATA_FUNC)connectionFree)
500
- {
501
- ConnectionHandle *connection = NULL;
502
-
503
- Data_Get_Struct(entry, ConnectionHandle, connection);
504
- if(connection->handle != 0)
505
- {
506
- /* Store the connection details. */
507
- teb[i].database = &connection->handle;
508
- if(size > 0)
509
- {
510
- teb[i].length = size;
511
- teb[i].tpb = buffer;
512
- }
513
- else
514
- {
515
- teb[i].length = DEFAULT_TEB_SIZE;
516
- teb[i].tpb = DEFAULT_TEB;
517
- }
518
- }
519
- else
520
- {
521
- /* Clean up and raise an exception. */
522
- free(teb);
523
- rb_fireruby_raise(NULL,
524
- "Disconnected connection specified "\
525
- "starting a transaction.");
526
- }
527
- }
528
- else
529
- {
530
- /* Clean up and thrown an exception. */
531
- free(teb);
532
- rb_fireruby_raise(NULL,
533
- "Invalid connection specified starting a "\
534
- "transaction.");
421
+ char *buffer) {
422
+ ISC_TEB *teb = NULL;
423
+ VALUE value = rb_funcall(connections, rb_intern("size"), 0),
424
+ head = rb_ary_entry(connections, 0);
425
+ short length = 0;
426
+ ConnectionHandle *first = NULL;
427
+
428
+ /* Attempt a retrieval of the first connection. */
429
+ if(value != Qnil) {
430
+ /* Check that we have a connection. */
431
+ if(TYPE(head) == T_DATA &&
432
+ RDATA(head)->dfree == (RUBY_DATA_FUNC)connectionFree) {
433
+ Data_Get_Struct(head, ConnectionHandle, first);
434
+ }
435
+ }
436
+
437
+ /* Generate the list of connections. */
438
+ length = (TYPE(value) == T_FIXNUM ? FIX2INT(value) : NUM2INT(value));
439
+ if(length > 0) {
440
+ if((teb = ALLOC_N(ISC_TEB, length)) != NULL) {
441
+ int i;
442
+
443
+ for(i = 0; i < length; i++) {
444
+ VALUE entry = rb_ary_entry(connections, i);
445
+
446
+ /* Check that we have a connection. */
447
+ if(TYPE(entry) == T_DATA &&
448
+ RDATA(entry)->dfree == (RUBY_DATA_FUNC)connectionFree) {
449
+ ConnectionHandle *connection = NULL;
450
+
451
+ Data_Get_Struct(entry, ConnectionHandle, connection);
452
+ if(connection->handle != 0) {
453
+ /* Store the connection details. */
454
+ teb[i].database = &connection->handle;
455
+ if(size > 0) {
456
+ teb[i].length = size;
457
+ teb[i].tpb = buffer;
458
+ } else {
459
+ teb[i].length = DEFAULT_TEB_SIZE;
460
+ teb[i].tpb = DEFAULT_TEB;
535
461
  }
536
- }
462
+ } else {
463
+ /* Clean up and raise an exception. */
464
+ free(teb);
465
+ rb_fireruby_raise(NULL,
466
+ "Disconnected connection specified " \
467
+ "starting a transaction.");
468
+ }
469
+ } else {
470
+ /* Clean up and thrown an exception. */
471
+ free(teb);
472
+ rb_fireruby_raise(NULL,
473
+ "Invalid connection specified starting a " \
474
+ "transaction.");
475
+ }
537
476
  }
538
- else
539
- {
540
- rb_raise(rb_eNoMemError,
541
- "Memory allocation error starting transaction.");
542
- }
543
- }
544
- else
545
- {
546
- /* Generate an exception. */
547
- rb_fireruby_raise(NULL, "No connections specified for transaction.");
548
- }
549
-
550
- /* Check that theres been no errors and that we have a connection list. */
551
- if(teb != NULL)
552
- {
553
- ISC_STATUS status[20];
554
-
555
- /* Attempt a transaction start. */
556
- if(isc_start_multiple(status, &transaction->handle, length, teb) != 0)
557
- {
558
- /* Generate an error. */
559
- rb_fireruby_raise(status, "Error starting transaction.");
560
- }
561
- }
562
-
563
- /* Free the database details list if need be. */
564
- if(teb != NULL)
565
- {
566
- free(teb);
567
- }
477
+ } else {
478
+ rb_raise(rb_eNoMemError,
479
+ "Memory allocation error starting transaction.");
480
+ }
481
+ } else {
482
+ /* Generate an exception. */
483
+ rb_fireruby_raise(NULL, "No connections specified for transaction.");
484
+ }
485
+
486
+ /* Check that theres been no errors and that we have a connection list. */
487
+ if(teb != NULL) {
488
+ ISC_STATUS status[ISC_STATUS_LENGTH];
489
+
490
+ /* Attempt a transaction start. */
491
+ if(isc_start_multiple(status, &transaction->handle, length, teb) != 0) {
492
+ /* Generate an error. */
493
+ rb_fireruby_raise(status, "Error starting transaction.");
494
+ }
495
+ }
496
+
497
+ /* Free the database details list if need be. */
498
+ if(teb != NULL) {
499
+ free(teb);
500
+ }
568
501
  }
569
502
 
570
503
 
@@ -577,20 +510,17 @@ void startTransaction(TransactionHandle *transaction,
577
510
  * with the Transaction object being collected.
578
511
  *
579
512
  */
580
- void transactionFree(void *transaction)
581
- {
582
- if(transaction != NULL)
583
- {
584
- TransactionHandle *handle = (TransactionHandle *)transaction;
585
-
586
- if(handle->handle != 0)
587
- {
588
- ISC_STATUS status[20];
589
-
590
- isc_rollback_transaction(status, &handle->handle);
591
- }
592
- free(handle);
593
- }
513
+ void transactionFree(void *transaction) {
514
+ if(transaction != NULL) {
515
+ TransactionHandle *handle = (TransactionHandle *)transaction;
516
+
517
+ if(handle->handle != 0) {
518
+ ISC_STATUS status[ISC_STATUS_LENGTH];
519
+
520
+ isc_rollback_transaction(status, &handle->handle);
521
+ }
522
+ free(handle);
523
+ }
594
524
  }
595
525
 
596
526
 
@@ -605,13 +535,12 @@ void transactionFree(void *transaction)
605
535
  * @return A reference to the Transaction object, or nil if an error occurs.
606
536
  *
607
537
  */
608
- VALUE rb_transaction_new(VALUE connections)
609
- {
610
- VALUE transaction = allocateTransaction(cTransaction);
611
-
612
- transactionInitialize(transaction, connections);
613
-
614
- return(transaction);
538
+ VALUE rb_transaction_new(VALUE connections) {
539
+ VALUE transaction = allocateTransaction(cTransaction);
540
+
541
+ transactionInitialize(transaction, connections);
542
+
543
+ return(transaction);
615
544
  }
616
545
 
617
546
  /**
@@ -625,17 +554,15 @@ VALUE rb_transaction_new(VALUE connections)
625
554
  * it is not.
626
555
  *
627
556
  */
628
- int coversConnection(VALUE transaction, VALUE connection)
629
- {
630
- int result = 0;
631
- VALUE boolean = isTransactionFor(transaction, connection);
632
-
633
- if(boolean == Qtrue)
634
- {
635
- result = 1;
636
- }
637
-
638
- return(result);
557
+ int coversConnection(VALUE transaction, VALUE connection) {
558
+ int result = 0;
559
+ VALUE boolean = isTransactionFor(transaction, connection);
560
+
561
+ if(boolean == Qtrue) {
562
+ result = 1;
563
+ }
564
+
565
+ return(result);
639
566
  }
640
567
 
641
568
 
@@ -646,39 +573,38 @@ int coversConnection(VALUE transaction, VALUE connection)
646
573
  * @param module A reference to the module to create the class within.
647
574
  *
648
575
  */
649
- void Init_Transaction(VALUE module)
650
- {
651
- cTransaction = rb_define_class_under(module, "Transaction", rb_cObject);
652
- rb_define_alloc_func(cTransaction, allocateTransaction);
653
- rb_define_method(cTransaction, "initialize", transactionInitialize, 1);
654
- rb_define_method(cTransaction, "initialize_copy", forbidObjectCopy, 1);
655
- rb_define_method(cTransaction, "active?", transactionIsActive, 0);
656
- rb_define_method(cTransaction, "commit", commitTransaction, 0);
657
- rb_define_method(cTransaction, "rollback", rollbackTransaction, 0);
658
- rb_define_method(cTransaction, "connections", getTransactionConnections, 0);
659
- rb_define_method(cTransaction, "for_connection?", isTransactionFor, 1);
660
- rb_define_module_function(cTransaction, "create", createTransaction, 2);
661
- rb_define_method(cTransaction, "execute", executeOnTransaction, 1);
662
- rb_define_const(cTransaction, "TPB_VERSION_1", INT2FIX(isc_tpb_version1));
663
- rb_define_const(cTransaction, "TPB_VERSION_3", INT2FIX(isc_tpb_version3));
664
- rb_define_const(cTransaction, "TPB_CONSISTENCY", INT2FIX(isc_tpb_consistency));
665
- rb_define_const(cTransaction, "TPB_CONCURRENCY", INT2FIX(isc_tpb_concurrency));
666
- rb_define_const(cTransaction, "TPB_SHARED", INT2FIX(isc_tpb_shared));
667
- rb_define_const(cTransaction, "TPB_PROTECTED", INT2FIX(isc_tpb_protected));
668
- rb_define_const(cTransaction, "TPB_EXCLUSIVE", INT2FIX(isc_tpb_exclusive));
669
- rb_define_const(cTransaction, "TPB_WAIT", INT2FIX(isc_tpb_wait));
670
- rb_define_const(cTransaction, "TPB_NO_WAIT", INT2FIX(isc_tpb_nowait));
671
- rb_define_const(cTransaction, "TPB_READ", INT2FIX(isc_tpb_read));
672
- rb_define_const(cTransaction, "TPB_WRITE", INT2FIX(isc_tpb_write));
673
- rb_define_const(cTransaction, "TPB_LOCK_READ", INT2FIX(isc_tpb_lock_read));
674
- rb_define_const(cTransaction, "TPB_LOCK_WRITE", INT2FIX(isc_tpb_lock_write));
675
- rb_define_const(cTransaction, "TPB_VERB_TIME", INT2FIX(isc_tpb_verb_time));
676
- rb_define_const(cTransaction, "TPB_COMMIT_TIME", INT2FIX(isc_tpb_commit_time));
677
- rb_define_const(cTransaction, "TPB_IGNORE_LIMBO", INT2FIX(isc_tpb_ignore_limbo));
678
- rb_define_const(cTransaction, "TPB_READ_COMMITTED", INT2FIX(isc_tpb_read_committed));
679
- rb_define_const(cTransaction, "TPB_AUTO_COMMIT", INT2FIX(isc_tpb_autocommit));
680
- rb_define_const(cTransaction, "TPB_REC_VERSION", INT2FIX(isc_tpb_rec_version));
681
- rb_define_const(cTransaction, "TPB_NO_REC_VERSION", INT2FIX(isc_tpb_no_rec_version));
682
- rb_define_const(cTransaction, "TPB_RESTART_REQUESTS", INT2FIX(isc_tpb_restart_requests));
683
- rb_define_const(cTransaction, "TPB_NO_AUTO_UNDO", INT2FIX(isc_tpb_no_auto_undo));
576
+ void Init_Transaction(VALUE module) {
577
+ cTransaction = rb_define_class_under(module, "Transaction", rb_cObject);
578
+ rb_define_alloc_func(cTransaction, allocateTransaction);
579
+ rb_define_method(cTransaction, "initialize", transactionInitialize, 1);
580
+ rb_define_method(cTransaction, "initialize_copy", forbidObjectCopy, 1);
581
+ rb_define_method(cTransaction, "active?", transactionIsActive, 0);
582
+ rb_define_method(cTransaction, "commit", commitTransaction, 0);
583
+ rb_define_method(cTransaction, "rollback", rollbackTransaction, 0);
584
+ rb_define_method(cTransaction, "connections", getTransactionConnections, 0);
585
+ rb_define_method(cTransaction, "for_connection?", isTransactionFor, 1);
586
+ rb_define_module_function(cTransaction, "create", createTransaction, 2);
587
+ rb_define_method(cTransaction, "execute", executeOnTransaction, 1);
588
+ rb_define_const(cTransaction, "TPB_VERSION_1", INT2FIX(isc_tpb_version1));
589
+ rb_define_const(cTransaction, "TPB_VERSION_3", INT2FIX(isc_tpb_version3));
590
+ rb_define_const(cTransaction, "TPB_CONSISTENCY", INT2FIX(isc_tpb_consistency));
591
+ rb_define_const(cTransaction, "TPB_CONCURRENCY", INT2FIX(isc_tpb_concurrency));
592
+ rb_define_const(cTransaction, "TPB_SHARED", INT2FIX(isc_tpb_shared));
593
+ rb_define_const(cTransaction, "TPB_PROTECTED", INT2FIX(isc_tpb_protected));
594
+ rb_define_const(cTransaction, "TPB_EXCLUSIVE", INT2FIX(isc_tpb_exclusive));
595
+ rb_define_const(cTransaction, "TPB_WAIT", INT2FIX(isc_tpb_wait));
596
+ rb_define_const(cTransaction, "TPB_NO_WAIT", INT2FIX(isc_tpb_nowait));
597
+ rb_define_const(cTransaction, "TPB_READ", INT2FIX(isc_tpb_read));
598
+ rb_define_const(cTransaction, "TPB_WRITE", INT2FIX(isc_tpb_write));
599
+ rb_define_const(cTransaction, "TPB_LOCK_READ", INT2FIX(isc_tpb_lock_read));
600
+ rb_define_const(cTransaction, "TPB_LOCK_WRITE", INT2FIX(isc_tpb_lock_write));
601
+ rb_define_const(cTransaction, "TPB_VERB_TIME", INT2FIX(isc_tpb_verb_time));
602
+ rb_define_const(cTransaction, "TPB_COMMIT_TIME", INT2FIX(isc_tpb_commit_time));
603
+ rb_define_const(cTransaction, "TPB_IGNORE_LIMBO", INT2FIX(isc_tpb_ignore_limbo));
604
+ rb_define_const(cTransaction, "TPB_READ_COMMITTED", INT2FIX(isc_tpb_read_committed));
605
+ rb_define_const(cTransaction, "TPB_AUTO_COMMIT", INT2FIX(isc_tpb_autocommit));
606
+ rb_define_const(cTransaction, "TPB_REC_VERSION", INT2FIX(isc_tpb_rec_version));
607
+ rb_define_const(cTransaction, "TPB_NO_REC_VERSION", INT2FIX(isc_tpb_no_rec_version));
608
+ rb_define_const(cTransaction, "TPB_RESTART_REQUESTS", INT2FIX(isc_tpb_restart_requests));
609
+ rb_define_const(cTransaction, "TPB_NO_AUTO_UNDO", INT2FIX(isc_tpb_no_auto_undo));
684
610
  }