ruby-informix 0.6.2 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,466 +0,0 @@
1
- /* $Id: ifx_except.ec,v 1.2 2007/10/20 10:17:35 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.2 2007/10/20 10:17:35 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
- mint i;
308
- EXEC SQL END DECLARE SECTION;
309
-
310
- new_instance = rb_class_new_instance(0, 0, exception_class);
311
-
312
- /* Check that instance of exception_class is derived from
313
- * Informix::Error
314
- */
315
- if (!rb_obj_is_kind_of(new_instance, sym.eError) &&
316
- !rb_obj_is_kind_of(new_instance, sym.eWarning)) {
317
- rb_raise(rb_eRuntimeError,
318
- "Can't instantiate exception from %s, only from %s or %s or their children",
319
- rb_class2name(exception_class),
320
- rb_class2name(sym.eWarning),
321
- rb_class2name(sym.eError));
322
- }
323
-
324
- EXEC SQL GET DIAGNOSTICS :exc_count = NUMBER;
325
-
326
- if (exc_count == 0) { /* Something went wrong */
327
- char message[128];
328
- snprintf(message,
329
- sizeof(message),
330
- "SQL ERROR: SQLCODE %d (sorry, no GET DIAGNOSTICS information available)",
331
- SQLCODE);
332
-
333
- {
334
- VALUE argv[] = { rb_str_new2(message) };
335
- return rb_class_new_instance(NUM_ELEMS(argv), argv, sym.eOperationalError);
336
- }
337
- }
338
-
339
- for (i = 0; i < exc_count; ++i) {
340
- sql_exception_number = i + 1;
341
-
342
- EXEC SQL GET DIAGNOSTICS EXCEPTION :sql_exception_number
343
- :sql_code = INFORMIX_SQLCODE,
344
- :sql_state = RETURNED_SQLSTATE,
345
- :class_origin_val = CLASS_ORIGIN,
346
- :subclass_origin_val = SUBCLASS_ORIGIN,
347
- :message = MESSAGE_TEXT,
348
- :message_len = MESSAGE_LENGTH,
349
- :server_name = SERVER_NAME,
350
- :connection_name = CONNECTION_NAME
351
- ;
352
-
353
- TRIM_BLANKS(class_origin_val);
354
- TRIM_BLANKS(subclass_origin_val);
355
- TRIM_BLANKS(server_name);
356
- TRIM_BLANKS(connection_name);
357
- message[message_len - 1] = '\0';
358
- TRIM_BLANKS(message);
359
-
360
- {
361
- VALUE sprintf_args[] = { rb_str_new2(message), rb_str_new2(sqlca.sqlerrm) };
362
- VALUE argv[] = {
363
- INT2FIX(sql_code),
364
- rb_str_new2(sql_state),
365
- rb_str_new2(class_origin_val),
366
- rb_str_new2(subclass_origin_val),
367
- rb_f_sprintf(NUM_ELEMS(sprintf_args), sprintf_args),
368
- rb_str_new2(server_name),
369
- rb_str_new2(connection_name)
370
- };
371
-
372
- ifx_exc_add_info(NUM_ELEMS(argv), argv, new_instance);
373
- }
374
- }
375
-
376
- return new_instance;
377
- }
378
-
379
- /**
380
- * Raises Informix::AssertionFailure exception
381
- */
382
- void ifx_assertion_exception(const char *failure_type,
383
- const char *what_failed,
384
- const char *file,
385
- int line)
386
- {
387
- VALUE sprintf_args[] = {
388
- rb_str_new2("%s failed on line %d of file %s: %s"),
389
- rb_str_new2(failure_type),
390
- INT2FIX(line),
391
- rb_str_new2(file),
392
- rb_str_new2(what_failed)
393
- };
394
-
395
- VALUE args[] = { rb_f_sprintf(NUM_ELEMS(sprintf_args), sprintf_args) };
396
-
397
- rb_exc_raise(rb_class_new_instance(NUM_ELEMS(args), args, sym.lib_eAssertion));
398
- }
399
-
400
- /* Init module with shared value(s) from main informix classes */
401
- void rbifx_except_init(VALUE mInformix, ifx_except_symbols_t *syms)
402
- {
403
- VALUE sym_ExcInfo;
404
-
405
- sym.mInformix = mInformix; // Informix module object handle
406
-
407
- /* class Error --------------------------------------------------------- */
408
- sym.eError = rb_define_class_under(mInformix, "Error", rb_eStandardError);
409
- sym.eWarning = rb_define_class_under(mInformix, "Warning", rb_eStandardError);
410
-
411
- sym.eInterfaceError = rb_define_class_under(mInformix, "InterfaceError", sym.eError);
412
- sym.eDatabaseError = rb_define_class_under(mInformix, "DatabaseError", sym.eError);
413
- sym.eDataError = rb_define_class_under(mInformix, "DataError", sym.eError);
414
- sym.eOperationalError = rb_define_class_under(mInformix, "OperationalError", sym.eError);
415
- sym.eIntegrityError = rb_define_class_under(mInformix, "IntegrityError", sym.eError);
416
- sym.eInternalError = rb_define_class_under(mInformix, "InternalError", sym.eError);
417
- sym.eProgrammingError = rb_define_class_under(mInformix, "ProgrammingError", sym.eError);
418
- sym.eNotSupportedError = rb_define_class_under(mInformix, "NotSupportedError", sym.eError);
419
-
420
- /* Make base class enumerable */
421
- rb_include_module(sym.eError, rb_mEnumerable);
422
-
423
- /* Precondition exception class */
424
- sym.lib_eAssertion = rb_define_class_under(mInformix, "AssertionFailure", rb_eStandardError);
425
-
426
- rb_define_method(sym.eError, "initialize", ifx_exc_init, -1);
427
- rb_define_method(sym.eError, "message", ifx_exc_message, 0);
428
- rb_define_method(sym.eError, "sql_code", ifx_exc_sql_code, 0);
429
- rb_define_method(sym.eError, "add_info", ifx_exc_add_info, -1);
430
- rb_define_method(sym.eError, "[]", ifx_exc_at, 1);
431
- rb_define_method(sym.eError, "each", ifx_exc_each, 0);
432
- rb_define_method(sym.eError, "to_s", ifx_exc_to_s, 0);
433
- rb_define_method(sym.eError, "size", ifx_exc_size, 0);
434
- rb_define_alias(sym.eError, "length", "size");
435
-
436
- sym_ExcInfo = rb_intern("ExcInfo");
437
-
438
- sym.id_sql_code = rb_intern("sql_code");
439
- sym.id_sql_state = rb_intern("sql_state");
440
- sym.id_class_origin = rb_intern("class_origin");
441
- sym.id_subclass_origin = rb_intern("subclass_origin");
442
- sym.id_message = rb_intern("message");
443
- sym.id_server_name = rb_intern("server_name");
444
- sym.id_connection_name = rb_intern("connection_name");
445
-
446
- /* Define ExcInfo as a struct in the Informix module */
447
- rb_define_const(mInformix,
448
- "ExcInfo",
449
- rb_struct_define(NULL,
450
- rb_id2name(sym.id_sql_code),
451
- rb_id2name(sym.id_sql_state),
452
- rb_id2name(sym.id_class_origin),
453
- rb_id2name(sym.id_subclass_origin),
454
- rb_id2name(sym.id_message),
455
- rb_id2name(sym.id_server_name),
456
- rb_id2name(sym.id_connection_name),
457
- NULL));
458
-
459
- sym.sExcInfo = rb_const_get(mInformix, sym_ExcInfo);
460
-
461
- if (syms)
462
- {
463
- *syms = sym;
464
- }
465
- }
466
-