ruby-informix 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (10) hide show
  1. data/Changelog +47 -0
  2. data/README +8 -5
  3. data/extconf.rb +1 -1
  4. data/ifx_assert.h +45 -0
  5. data/ifx_except.c +523 -0
  6. data/ifx_except.ec +467 -0
  7. data/ifx_except.h +82 -0
  8. data/informix.c +555 -541
  9. data/informix.ec +173 -159
  10. metadata +9 -3
@@ -0,0 +1,467 @@
1
+ /* $Id: ifx_except.ec,v 1.1 2007/01/31 02:16:32 santana Exp $ */
2
+ /*
3
+ * Copyright (c) 2006, 2007 Edwin M. Fine <efine@finecomputerconsultants.com>
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions
8
+ * are met:
9
+ *
10
+ * 1. Redistributions of source code must retain the above copyright
11
+ * notice, this list of conditions and the following disclaimer.
12
+ * 2. Redistributions in binary form must reproduce the above copyright
13
+ * notice, this list of conditions and the following disclaimer in the
14
+ * documentation and/or other materials provided with the distribution.
15
+ * 3. The name of the author may not be used to endorse or promote products
16
+ * derived from this software without specific prior written permission.
17
+ *
18
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ * POSSIBILITY OF SUCH DAMAGE.
29
+ */
30
+
31
+ #include "ifx_except.h"
32
+ #include "ifx_assert.h"
33
+
34
+ #include <ruby.h>
35
+ #include <stdlib.h>
36
+ #include <stdio.h>
37
+ #include <sqlstype.h>
38
+ #include <sqltypes.h>
39
+
40
+ /* Convenience macros */
41
+ #define TRIM_BLANKS(s) ((s)[byleng(s, stleng(s))] = '\0')
42
+ #define NUM_ELEMS(arr) (sizeof(arr) / sizeof(*arr))
43
+
44
+ /* Future use: definition of interpretation of sql_state */
45
+ #define IS_SQL_SUCCESS(sql_state) ((sql_state)[0] == '0' && (sql_state)[1] == '0')
46
+ #define IS_SQL_WARNING(sql_state) ((sql_state)[0] == '0' && (sql_state)[1] == '1')
47
+ #define IS_SQL_NO_DATA_FOUND(sql_state) ((sql_state)[0] == '0' && (sql_state)[1] == '2')
48
+ #define IS_SQL_ERROR(sql_state) ((sql_state)[0] > '0' || (sql_state)[1] > '2')
49
+
50
+ /* Constants */
51
+ #define NUM_SQL_EXCEPTION_ARGS 7 /* Number of things we get from GET EXCEPTION */
52
+
53
+ static const char * const vcs_id = "$Id: ifx_except.ec,v 1.1 2007/01/31 02:16:32 santana Exp $";
54
+
55
+ /*
56
+ * Ruby object/class/module handles
57
+ */
58
+ static ifx_except_symbols_t sym;
59
+
60
+ /**
61
+ * Implementation
62
+ */
63
+
64
+ /* class Informix::Error ------------------------------------------------ */
65
+
66
+ /**
67
+ * call-seq:
68
+ * Informix::Error.new([string|array]) => obj
69
+ *
70
+ * Optional string is the exception message.
71
+ * Optional array must contain only instances of Informix::ExcInfo structs.
72
+ *
73
+ * Examples:
74
+ * exc = Informix::Error.new
75
+ * arr = [ExcInfo.new(x,y,z...), ExcInfo.new(a,b,c...)]
76
+ * exc = Informix::Error.new(arr)
77
+ */
78
+ static VALUE ifx_exc_init(int argc, VALUE *argv, VALUE self)
79
+ {
80
+ VALUE arr;
81
+ VALUE arg;
82
+
83
+ if (rb_scan_args(argc, argv, "01", &arg) == 0) {
84
+ arr = rb_ary_new();
85
+ }
86
+ else if (TYPE(arg) == T_STRING) {
87
+ arr = rb_ary_new();
88
+ rb_call_super(argc, argv);
89
+ }
90
+ else if (TYPE(arg) == T_ARRAY) {
91
+ arr = arg;
92
+ if (RARRAY(arg)->len > 0) {
93
+ long i;
94
+ for (i = 0; i < RARRAY(arg)->len; ++i)
95
+ if (!rb_obj_is_instance_of(rb_ary_entry(arg, i), sym.sExcInfo))
96
+ rb_raise(rb_eTypeError, "Array may contain only Informix::ExcInfo structs");
97
+ }
98
+ }
99
+ else {
100
+ rb_raise(rb_eTypeError,
101
+ "Expected string, or array of Informix::ExcInfo, as argument");
102
+ }
103
+
104
+ rb_iv_set(self, "@info", arr);
105
+
106
+ return self;
107
+ }
108
+
109
+ /* Implementation note:
110
+ * argv must contain the following values in the order given:
111
+ * sql_code FIXNUM
112
+ * sql_state STRING
113
+ * class_origin STRING
114
+ * subclass_origin STRING
115
+ * message STRING
116
+ * server_name STRING
117
+ * connection_name STRING
118
+ */
119
+
120
+ /**
121
+ * call-seq:
122
+ * exc.add_info(sql_code, sql_state, class_origin, subclass_origin, message, server_name, connection_name) => self
123
+ *
124
+ * Appends the given information to the exception.
125
+ */
126
+ static VALUE ifx_exc_add_info(int argc, VALUE *argv, VALUE self)
127
+ {
128
+ VALUE info_arr = rb_iv_get(self, "@info");
129
+ VALUE sInfo;
130
+
131
+ #if defined(DEBUG)
132
+ printf("%s:%d argc = %d\n", "ifx_exc_add_info", __LINE__, argc);
133
+ #endif
134
+
135
+ if (argc != NUM_SQL_EXCEPTION_ARGS)
136
+ rb_raise(rb_eArgError, "Invalid number of arguments (got %d, need %d)", argc, NUM_SQL_EXCEPTION_ARGS);
137
+ if (info_arr == Qnil) { /* Add the array if missing */
138
+ info_arr = rb_ary_new();
139
+ rb_iv_set(self, "@info", info_arr);
140
+ }
141
+
142
+ sInfo =
143
+ rb_struct_new(sym.sExcInfo,
144
+ argv[0], argv[1], argv[2], argv[3],
145
+ argv[4], argv[5], argv[6], NULL);
146
+
147
+ /* Add the new struct instance to end of our array */
148
+ rb_ary_push(info_arr, sInfo);
149
+
150
+ return self;
151
+ }
152
+
153
+ /**
154
+ * call-seq:
155
+ * exc.size => num
156
+ *
157
+ * Returns the number of Informix exception messages in the exception.
158
+ */
159
+ static VALUE ifx_exc_size(VALUE self)
160
+ {
161
+ VALUE info_arr = rb_iv_get(self, "@info");
162
+ return info_arr != Qnil ? LONG2NUM(RARRAY(info_arr)->len) : Qnil;
163
+ }
164
+
165
+ /**
166
+ * call-seq:
167
+ * exc.each {|exc_info| block } => exc_info
168
+ *
169
+ * Calls block once for each Informix::ExcInfo object in the exception.
170
+ */
171
+ static VALUE ifx_exc_each(VALUE self)
172
+ {
173
+ VALUE info_arr = rb_iv_get(self, "@info");
174
+ return info_arr != Qnil ? rb_iterate(rb_each, info_arr, rb_yield, 0) : Qnil;
175
+ }
176
+
177
+ /**
178
+ * call-seq:
179
+ * exc.at(index) => info
180
+ *
181
+ * Returns the ExcInfo object at index.
182
+ */
183
+ static VALUE ifx_exc_at(VALUE self, VALUE index)
184
+ {
185
+ VALUE info_arr = rb_iv_get(self, "@info");
186
+ long n = NUM2LONG(rb_Integer(index));
187
+
188
+ #if defined(DEBUG)
189
+ printf("Getting value at %ld\n", n);
190
+ #endif
191
+
192
+ return info_arr != Qnil ? rb_ary_entry(info_arr, n) : Qnil;
193
+ }
194
+
195
+ /**
196
+ * call-seq:
197
+ * exc.to_s => string
198
+ *
199
+ * Returns a string representation of self.
200
+ */
201
+ static VALUE ifx_exc_to_s(VALUE self)
202
+ {
203
+ const VALUE nl = rb_str_new2("\n");
204
+ VALUE s;
205
+ VALUE info_arr = rb_iv_get(self, "@info");
206
+ long info_arr_len;
207
+ VALUE sInfo;
208
+ long i;
209
+ size_t j;
210
+
211
+ info_arr_len = info_arr == Qnil ? 0 : RARRAY(info_arr)->len;
212
+
213
+ if (info_arr_len > 0) {
214
+ VALUE fmt_str = rb_str_new2("%-15s: %s\n");
215
+
216
+ ID fields[] = { /* Fields will be displayed in this order */
217
+ sym.id_message,
218
+ sym.id_sql_code,
219
+ sym.id_sql_state,
220
+ sym.id_class_origin,
221
+ sym.id_subclass_origin,
222
+ sym.id_server_name,
223
+ sym.id_connection_name
224
+ };
225
+
226
+ s = rb_str_new2("\n");
227
+
228
+ for (i = 0; i < info_arr_len; ++i) {
229
+ sInfo = RARRAY(info_arr)->ptr[i];
230
+
231
+ for (j = 0; j < NUM_ELEMS(fields); ++j) {
232
+ ID field = fields[j];
233
+ VALUE struct_ref = rb_struct_getmember(sInfo, field);
234
+ VALUE item_value = rb_String(struct_ref);
235
+ VALUE args[] = { fmt_str, rb_String(ID2SYM(field)), item_value };
236
+
237
+ if (RSTRING(item_value)->len != 0) { /* Skip empty fields */
238
+ rb_str_concat(s, rb_f_sprintf(NUM_ELEMS(args), args));
239
+ }
240
+ }
241
+
242
+ rb_str_concat(s, nl);
243
+ }
244
+ }
245
+ else { /* Call super's to_s */
246
+ s = rb_call_super(0, 0);
247
+ }
248
+
249
+ return s;
250
+ }
251
+
252
+ /**
253
+ * Overrides Exception#message. Returns first message in ExcInfo array,
254
+ * or if the array is empty, delegates back to the parent class.
255
+ */
256
+ static VALUE ifx_exc_message(VALUE self)
257
+ {
258
+ VALUE info_arr = rb_iv_get(self, "@info");
259
+
260
+ return (info_arr != Qnil && RARRAY(info_arr)->len > 0)
261
+ ? rb_struct_getmember(RARRAY(info_arr)->ptr[0], sym.id_message)
262
+ : rb_call_super(0, 0);
263
+ }
264
+
265
+ /**
266
+ * call-seq:
267
+ * exc.sqlcode => fixnum
268
+ *
269
+ * Returns the SQLCODE for the first stored ExcInfo struct, or 0
270
+ * if none are stored.
271
+ */
272
+ static VALUE ifx_exc_sql_code(VALUE self)
273
+ {
274
+ VALUE info_arr = rb_iv_get(self, "@info");
275
+
276
+ return (info_arr != Qnil && RARRAY(info_arr)->len > 0)
277
+ ? rb_struct_getmember(RARRAY(info_arr)->ptr[0], sym.id_sql_code)
278
+ : INT2FIX(0);
279
+ }
280
+
281
+ /*
282
+ * C helper functions (see ifx_except.h for documentation)
283
+ */
284
+ void raise_ifx_extended(void)
285
+ {
286
+ rb_exc_raise(rbifx_ext_exception(sym.eDatabaseError));
287
+ }
288
+
289
+ VALUE rbifx_ext_exception(VALUE exception_class)
290
+ {
291
+ VALUE new_instance;
292
+
293
+ EXEC SQL BEGIN DECLARE SECTION;
294
+ /* All field sizes defined in IBM Informix ESQL/C Programmer's Manual */
295
+ int4 sql_code;
296
+
297
+ char sql_state[5 + 1];
298
+ char class_origin_val[255 + 1];
299
+ char subclass_origin_val[255 + 1];
300
+ char message[8191 + 1];
301
+ char server_name[255 + 1];
302
+ char connection_name[255 + 1];
303
+
304
+ mint sql_exception_number;
305
+ mint exc_count = 0;
306
+ mint message_len;
307
+ EXEC SQL END DECLARE SECTION;
308
+
309
+ new_instance = rb_class_new_instance(0, 0, exception_class);
310
+
311
+ /* Check that instance of exception_class is derived from
312
+ * Informix::Error
313
+ */
314
+ if (!rb_obj_is_kind_of(new_instance, sym.eError) &&
315
+ !rb_obj_is_kind_of(new_instance, sym.eWarning)) {
316
+ rb_raise(rb_eRuntimeError,
317
+ "Can't instantiate exception from %s, only from %s or %s or their children",
318
+ rb_class2name(exception_class),
319
+ rb_class2name(sym.eWarning),
320
+ rb_class2name(sym.eError));
321
+ }
322
+
323
+ EXEC SQL GET DIAGNOSTICS :exc_count = NUMBER;
324
+
325
+ if (exc_count == 0) { /* Something went wrong */
326
+ char message[128];
327
+ snprintf(message,
328
+ sizeof(message),
329
+ "SQL ERROR: SQLCODE %d (sorry, no GET DIAGNOSTICS information available)",
330
+ SQLCODE);
331
+
332
+ {
333
+ VALUE argv[] = { rb_str_new2(message) };
334
+ return rb_class_new_instance(NUM_ELEMS(argv), argv, sym.eOperationalError);
335
+ }
336
+ }
337
+
338
+ mint i;
339
+
340
+ for (i = 0; i < exc_count; ++i) {
341
+ sql_exception_number = i + 1;
342
+
343
+ EXEC SQL GET DIAGNOSTICS EXCEPTION :sql_exception_number
344
+ :sql_code = INFORMIX_SQLCODE,
345
+ :sql_state = RETURNED_SQLSTATE,
346
+ :class_origin_val = CLASS_ORIGIN,
347
+ :subclass_origin_val = SUBCLASS_ORIGIN,
348
+ :message = MESSAGE_TEXT,
349
+ :message_len = MESSAGE_LENGTH,
350
+ :server_name = SERVER_NAME,
351
+ :connection_name = CONNECTION_NAME
352
+ ;
353
+
354
+ TRIM_BLANKS(class_origin_val);
355
+ TRIM_BLANKS(subclass_origin_val);
356
+ TRIM_BLANKS(server_name);
357
+ TRIM_BLANKS(connection_name);
358
+ message[message_len - 1] = '\0';
359
+ TRIM_BLANKS(message);
360
+
361
+ {
362
+ VALUE sprintf_args[] = { rb_str_new2(message), rb_str_new2(sqlca.sqlerrm) };
363
+ VALUE argv[] = {
364
+ INT2FIX(sql_code),
365
+ rb_str_new2(sql_state),
366
+ rb_str_new2(class_origin_val),
367
+ rb_str_new2(subclass_origin_val),
368
+ rb_f_sprintf(NUM_ELEMS(sprintf_args), sprintf_args),
369
+ rb_str_new2(server_name),
370
+ rb_str_new2(connection_name)
371
+ };
372
+
373
+ ifx_exc_add_info(NUM_ELEMS(argv), argv, new_instance);
374
+ }
375
+ }
376
+
377
+ return new_instance;
378
+ }
379
+
380
+ /**
381
+ * Raises Informix::AssertionFailure exception
382
+ */
383
+ void ifx_assertion_exception(const char *failure_type,
384
+ const char *what_failed,
385
+ const char *file,
386
+ int line)
387
+ {
388
+ VALUE sprintf_args[] = {
389
+ rb_str_new2("%s failed on line %d of file %s: %s"),
390
+ rb_str_new2(failure_type),
391
+ INT2FIX(line),
392
+ rb_str_new2(file),
393
+ rb_str_new2(what_failed)
394
+ };
395
+
396
+ VALUE args[] = { rb_f_sprintf(NUM_ELEMS(sprintf_args), sprintf_args) };
397
+
398
+ rb_exc_raise(rb_class_new_instance(NUM_ELEMS(args), args, sym.lib_eAssertion));
399
+ }
400
+
401
+ /* Init module with shared value(s) from main informix classes */
402
+ void rbifx_except_init(VALUE mInformix, ifx_except_symbols_t *syms)
403
+ {
404
+ VALUE sym_ExcInfo;
405
+
406
+ sym.mInformix = mInformix; // Informix module object handle
407
+
408
+ /* class Error --------------------------------------------------------- */
409
+ sym.eError = rb_define_class_under(mInformix, "Error", rb_eStandardError);
410
+ sym.eWarning = rb_define_class_under(mInformix, "Warning", rb_eStandardError);
411
+
412
+ sym.eInterfaceError = rb_define_class_under(mInformix, "InterfaceError", sym.eError);
413
+ sym.eDatabaseError = rb_define_class_under(mInformix, "DatabaseError", sym.eError);
414
+ sym.eDataError = rb_define_class_under(mInformix, "DataError", sym.eError);
415
+ sym.eOperationalError = rb_define_class_under(mInformix, "OperationalError", sym.eError);
416
+ sym.eIntegrityError = rb_define_class_under(mInformix, "IntegrityError", sym.eError);
417
+ sym.eInternalError = rb_define_class_under(mInformix, "InternalError", sym.eError);
418
+ sym.eProgrammingError = rb_define_class_under(mInformix, "ProgrammingError", sym.eError);
419
+ sym.eNotSupportedError = rb_define_class_under(mInformix, "NotSupportedError", sym.eError);
420
+
421
+ /* Make base class enumerable */
422
+ rb_include_module(sym.eError, rb_mEnumerable);
423
+
424
+ /* Precondition exception class */
425
+ sym.lib_eAssertion = rb_define_class_under(mInformix, "AssertionFailure", rb_eStandardError);
426
+
427
+ rb_define_method(sym.eError, "initialize", ifx_exc_init, -1);
428
+ rb_define_method(sym.eError, "message", ifx_exc_message, 0);
429
+ rb_define_method(sym.eError, "sql_code", ifx_exc_sql_code, 0);
430
+ rb_define_method(sym.eError, "add_info", ifx_exc_add_info, -1);
431
+ rb_define_method(sym.eError, "[]", ifx_exc_at, 1);
432
+ rb_define_method(sym.eError, "each", ifx_exc_each, 0);
433
+ rb_define_method(sym.eError, "to_s", ifx_exc_to_s, 0);
434
+ rb_define_method(sym.eError, "size", ifx_exc_size, 0);
435
+ rb_define_alias(sym.eError, "length", "size");
436
+
437
+ sym_ExcInfo = rb_intern("ExcInfo");
438
+
439
+ sym.id_sql_code = rb_intern("sql_code");
440
+ sym.id_sql_state = rb_intern("sql_state");
441
+ sym.id_class_origin = rb_intern("class_origin");
442
+ sym.id_subclass_origin = rb_intern("subclass_origin");
443
+ sym.id_message = rb_intern("message");
444
+ sym.id_server_name = rb_intern("server_name");
445
+ sym.id_connection_name = rb_intern("connection_name");
446
+
447
+ /* Define ExcInfo as a struct in the Informix module */
448
+ rb_define_const(mInformix,
449
+ "ExcInfo",
450
+ rb_struct_define(NULL,
451
+ rb_id2name(sym.id_sql_code),
452
+ rb_id2name(sym.id_sql_state),
453
+ rb_id2name(sym.id_class_origin),
454
+ rb_id2name(sym.id_subclass_origin),
455
+ rb_id2name(sym.id_message),
456
+ rb_id2name(sym.id_server_name),
457
+ rb_id2name(sym.id_connection_name),
458
+ NULL));
459
+
460
+ sym.sExcInfo = rb_const_get(mInformix, sym_ExcInfo);
461
+
462
+ if (syms)
463
+ {
464
+ *syms = sym;
465
+ }
466
+ }
467
+