rubyfb 0.5.5 → 0.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ext/Database.c CHANGED
@@ -3,20 +3,20 @@
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
@@ -54,23 +54,19 @@ VALUE cDatabase;
54
54
  * @return A reference to the new Database class instance.
55
55
  *
56
56
  */
57
- static VALUE allocateDatabase(VALUE klass)
58
- {
59
- VALUE instance = Qnil;
60
- DatabaseHandle *database = ALLOC(DatabaseHandle);
61
-
62
- if(database != NULL)
63
- {
64
- /* Wrap the structure in a class. */
65
- instance = Data_Wrap_Struct(klass, NULL, databaseFree, database);
66
- }
67
- else
68
- {
69
- rb_raise(rb_eNoMemError,
70
- "Memory allocation failure creating a Database object.");
71
- }
72
-
73
- return(instance);
57
+ static VALUE allocateDatabase(VALUE klass) {
58
+ VALUE instance = Qnil;
59
+ DatabaseHandle *database = ALLOC(DatabaseHandle);
60
+
61
+ if(database != NULL) {
62
+ /* Wrap the structure in a class. */
63
+ instance = Data_Wrap_Struct(klass, NULL, databaseFree, database);
64
+ } else {
65
+ rb_raise(rb_eNoMemError,
66
+ "Memory allocation failure creating a Database object.");
67
+ }
68
+
69
+ return(instance);
74
70
  }
75
71
 
76
72
 
@@ -87,29 +83,26 @@ static VALUE allocateDatabase(VALUE klass)
87
83
  * @return A reference to the new initialized object instance.
88
84
  *
89
85
  */
90
- VALUE initializeDatabase(int argc, VALUE *argv, VALUE self)
91
- {
92
- VALUE options = rb_hash_new();
93
-
94
- /* Check the number of parameters. */
95
- if(argc < 1)
96
- {
97
- rb_raise(rb_eArgError, "Wrong number of arguments (%d for %d).", argc, 1);
98
- }
99
-
100
- /* Perform type checks on the input parameters. */
101
- Check_Type(argv[0], T_STRING);
102
- if(argc > 1)
103
- {
104
- Check_Type(argv[1], T_STRING);
105
- rb_hash_aset(options, INT2FIX(isc_dpb_lc_ctype), argv[1]);
106
- }
107
-
108
- /* Store the database details. */
109
- rb_iv_set(self, "@file", argv[0]);
110
- rb_iv_set(self, "@options", options);
111
-
112
- return(self);
86
+ VALUE initializeDatabase(int argc, VALUE *argv, VALUE self) {
87
+ VALUE options = rb_hash_new();
88
+
89
+ /* Check the number of parameters. */
90
+ if(argc < 1) {
91
+ rb_raise(rb_eArgError, "Wrong number of arguments (%d for %d).", argc, 1);
92
+ }
93
+
94
+ /* Perform type checks on the input parameters. */
95
+ Check_Type(argv[0], T_STRING);
96
+ if(argc > 1) {
97
+ Check_Type(argv[1], T_STRING);
98
+ rb_hash_aset(options, INT2FIX(isc_dpb_lc_ctype), argv[1]);
99
+ }
100
+
101
+ /* Store the database details. */
102
+ rb_iv_set(self, "@file", argv[0]);
103
+ rb_iv_set(self, "@options", options);
104
+
105
+ return(self);
113
106
  }
114
107
 
115
108
 
@@ -122,9 +115,8 @@ VALUE initializeDatabase(int argc, VALUE *argv, VALUE self)
122
115
  * object
123
116
  *
124
117
  */
125
- static VALUE getDatabaseFile(VALUE self)
126
- {
127
- return(rb_iv_get(self, "@file"));
118
+ static VALUE getDatabaseFile(VALUE self) {
119
+ return(rb_iv_get(self, "@file"));
128
120
  }
129
121
 
130
122
 
@@ -140,38 +132,31 @@ static VALUE getDatabaseFile(VALUE self)
140
132
  * @return A reference to a Connection object or nil if an error occurs.
141
133
  *
142
134
  */
143
- static VALUE connectToDatabase(int argc, VALUE *argv, VALUE self)
144
- {
145
- VALUE result = Qnil,
146
- user = Qnil,
147
- password = Qnil,
148
- options = rb_iv_get(self, "@options"),
149
- connection = Qnil;
150
-
151
- if(argc > 0)
152
- {
153
- user = argv[0];
154
- }
155
- if(argc > 1)
156
- {
157
- password = argv[1];
158
- }
159
- if(argc > 2)
160
- {
161
- rb_funcall(options, rb_intern("update"), 1, argv[2]);
162
- }
163
-
164
- connection = rb_connection_new(self, user, password, options);
165
- if(rb_block_given_p())
166
- {
167
- result = rb_ensure(connectBlock, connection, connectEnsure, connection);
168
- }
169
- else
170
- {
171
- result = connection;
172
- }
173
-
174
- return(result);
135
+ static VALUE connectToDatabase(int argc, VALUE *argv, VALUE self) {
136
+ VALUE result = Qnil,
137
+ user = Qnil,
138
+ password = Qnil,
139
+ options = rb_iv_get(self, "@options"),
140
+ connection = Qnil;
141
+
142
+ if(argc > 0) {
143
+ user = argv[0];
144
+ }
145
+ if(argc > 1) {
146
+ password = argv[1];
147
+ }
148
+ if(argc > 2) {
149
+ rb_funcall(options, rb_intern("update"), 1, argv[2]);
150
+ }
151
+
152
+ connection = rb_connection_new(self, user, password, options);
153
+ if(rb_block_given_p()) {
154
+ result = rb_ensure(connectBlock, connection, connectEnsure, connection);
155
+ } else {
156
+ result = connection;
157
+ }
158
+
159
+ return(result);
175
160
  }
176
161
 
177
162
 
@@ -192,99 +177,87 @@ static VALUE connectToDatabase(int argc, VALUE *argv, VALUE self)
192
177
  * @return A reference to a Database object on success, nil on failure.
193
178
  *
194
179
  */
195
- static VALUE createDatabase(int argc, VALUE *argv, VALUE unused)
196
- {
197
- VALUE database = Qnil,
198
- set = Qnil,
199
- tmp_str = Qnil;
200
- char sql[512] = "",
201
- *text = NULL;
202
- int value = 1024;
203
- isc_db_handle connection = 0;
204
- isc_tr_handle transaction = 0;
205
- ISC_STATUS status[20];
206
-
207
- /* Check that sufficient parameters have been provided. */
208
- if(argc < 3)
209
- {
210
- rb_raise(rb_eArgError, "Wrong number of parameters (%d for %d).", argc,
211
- 3);
212
- }
213
-
214
- /* Check values that are permitted restricted values. */
215
- if(argc > 3)
216
- {
217
- value = FIX2INT(argv[3]);
218
- if(value != 1024 && value != 2048 && value != 4096 && value != 8192)
219
- {
220
- rb_raise(rb_eException,
221
- "Invalid database page size value . Valid values are 1024, "\
222
- "2048 4096 or 8192.");
223
- }
224
- }
225
-
226
- /* Prepare the SQL statement. */
227
- tmp_str = rb_funcall(argv[0], rb_intern("to_s"), 0);
228
- sprintf(sql, "CREATE DATABASE '%s'", StringValuePtr(tmp_str));
229
-
230
- tmp_str = rb_funcall(argv[1], rb_intern("to_s"), 0);
231
- text = StringValuePtr(tmp_str);
232
- if(strlen(text) > 0)
233
- {
234
- strcat(sql, " USER '");
235
- strcat(sql, text);
236
- strcat(sql, "'");
237
- }
238
-
239
- tmp_str = rb_funcall(argv[2], rb_intern("to_s"), 0);
240
- text = StringValuePtr(tmp_str);
241
- if(strlen(text) > 0)
242
- {
243
- strcat(sql, " PASSWORD '");
180
+ static VALUE createDatabase(int argc, VALUE *argv, VALUE unused) {
181
+ VALUE database = Qnil,
182
+ set = Qnil,
183
+ tmp_str = Qnil;
184
+ char sql[512] = "",
185
+ *text = NULL;
186
+ int value = 1024;
187
+ isc_db_handle connection = 0;
188
+ isc_tr_handle transaction = 0;
189
+ ISC_STATUS status[ISC_STATUS_LENGTH];
190
+
191
+ /* Check that sufficient parameters have been provided. */
192
+ if(argc < 3) {
193
+ rb_raise(rb_eArgError, "Wrong number of parameters (%d for %d).", argc,
194
+ 3);
195
+ }
196
+
197
+ /* Check values that are permitted restricted values. */
198
+ if(argc > 3) {
199
+ value = FIX2INT(argv[3]);
200
+ if(value != 1024 && value != 2048 && value != 4096 && value != 8192) {
201
+ rb_raise(rb_eException,
202
+ "Invalid database page size value . Valid values are 1024, " \
203
+ "2048 4096 or 8192.");
204
+ }
205
+ }
206
+
207
+ /* Prepare the SQL statement. */
208
+ tmp_str = rb_funcall(argv[0], rb_intern("to_s"), 0);
209
+ sprintf(sql, "CREATE DATABASE '%s'", StringValuePtr(tmp_str));
210
+
211
+ tmp_str = rb_funcall(argv[1], rb_intern("to_s"), 0);
212
+ text = StringValuePtr(tmp_str);
213
+ if(strlen(text) > 0) {
214
+ strcat(sql, " USER '");
215
+ strcat(sql, text);
216
+ strcat(sql, "'");
217
+ }
218
+
219
+ tmp_str = rb_funcall(argv[2], rb_intern("to_s"), 0);
220
+ text = StringValuePtr(tmp_str);
221
+ if(strlen(text) > 0) {
222
+ strcat(sql, " PASSWORD '");
223
+ strcat(sql, text);
224
+ strcat(sql, "'");
225
+ }
226
+
227
+ if(argc > 3) {
228
+ char text[50];
229
+
230
+ sprintf(text, " PAGE_SIZE = %d", value);
231
+ strcat(sql, text);
232
+ }
233
+
234
+ if(argc > 4 && argv[4] != Qnil) {
235
+ char *text = NULL;
236
+
237
+ set = rb_funcall(argv[4], rb_intern("to_s"), 0);
238
+ text = StringValuePtr(set);
239
+ if(strlen(text) > 0) {
240
+ strcat(sql, " DEFAULT CHARACTER SET ");
244
241
  strcat(sql, text);
245
- strcat(sql, "'");
246
- }
247
-
248
- if(argc > 3)
249
- {
250
- char text[50];
251
-
252
- sprintf(text, " PAGE_SIZE = %d", value);
253
- strcat(sql, text);
254
- }
255
-
256
- if(argc > 4 && argv[4] != Qnil)
257
- {
258
- char *text = NULL;
259
-
260
- set = rb_funcall(argv[4], rb_intern("to_s"), 0);
261
- text = StringValuePtr(set);
262
- if(strlen(text) > 0)
263
- {
264
- strcat(sql, " DEFAULT CHARACTER SET ");
265
- strcat(sql, text);
266
- }
267
- }
268
- strcat(sql, ";");
269
-
270
- if(isc_dsql_execute_immediate(status, &connection, &transaction, 0, sql,
271
- 3, NULL) != 0)
272
- {
273
- rb_fireruby_raise(status, "Database creation error.");
274
- }
275
-
276
- if(connection != 0)
277
- {
278
- isc_detach_database(status, &connection);
279
- }
280
-
281
- database = rb_database_new(argv[0]);
282
- if(set != Qnil)
283
- {
284
- setDatabaseCharacterSet(database, set);
285
- }
286
-
287
- return(database);
242
+ }
243
+ }
244
+ strcat(sql, ";");
245
+
246
+ if(isc_dsql_execute_immediate(status, &connection, &transaction, 0, sql,
247
+ 3, NULL) != 0) {
248
+ rb_fireruby_raise(status, "Database creation error.");
249
+ }
250
+
251
+ if(connection != 0) {
252
+ isc_detach_database(status, &connection);
253
+ }
254
+
255
+ database = rb_database_new(argv[0]);
256
+ if(set != Qnil) {
257
+ setDatabaseCharacterSet(database, set);
258
+ }
259
+
260
+ return(database);
288
261
  }
289
262
 
290
263
 
@@ -302,19 +275,17 @@ static VALUE createDatabase(int argc, VALUE *argv, VALUE unused)
302
275
  * @return Always returns nil.
303
276
  *
304
277
  */
305
- static VALUE dropDatabase(VALUE self, VALUE user, VALUE password)
306
- {
307
- VALUE connection = rb_connection_new(self, user, password, Qnil);
308
- ConnectionHandle *cHandle = NULL;
309
- ISC_STATUS status[20];
310
-
311
- Data_Get_Struct(connection, ConnectionHandle, cHandle);
312
- if(isc_drop_database(status, &cHandle->handle) != 0)
313
- {
314
- rb_fireruby_raise(status, "Error dropping database.");
315
- }
316
-
317
- return(Qnil);
278
+ static VALUE dropDatabase(VALUE self, VALUE user, VALUE password) {
279
+ VALUE connection = rb_connection_new(self, user, password, Qnil);
280
+ ConnectionHandle *cHandle = NULL;
281
+ ISC_STATUS status[ISC_STATUS_LENGTH];
282
+
283
+ Data_Get_Struct(connection, ConnectionHandle, cHandle);
284
+ if(isc_drop_database(status, &cHandle->handle) != 0) {
285
+ rb_fireruby_raise(status, "Error dropping database.");
286
+ }
287
+
288
+ return(Qnil);
318
289
  }
319
290
 
320
291
 
@@ -327,11 +298,10 @@ static VALUE dropDatabase(VALUE self, VALUE user, VALUE password)
327
298
  * @return A reference to the database character set. May be nil.
328
299
  *
329
300
  */
330
- static VALUE getDatabaseCharacterSet(VALUE self)
331
- {
332
- VALUE options = rb_iv_get(self, "@options");
333
-
334
- return(rb_hash_aref(options, INT2FIX(isc_dpb_lc_ctype)));
301
+ static VALUE getDatabaseCharacterSet(VALUE self) {
302
+ VALUE options = rb_iv_get(self, "@options");
303
+
304
+ return(rb_hash_aref(options, INT2FIX(isc_dpb_lc_ctype)));
335
305
  }
336
306
 
337
307
 
@@ -345,24 +315,20 @@ static VALUE getDatabaseCharacterSet(VALUE self)
345
315
  * @return A reference to the updated Database object.
346
316
  *
347
317
  */
348
- static VALUE setDatabaseCharacterSet(VALUE self, VALUE set)
349
- {
350
- VALUE options = rb_iv_get(self, "@options");
351
-
352
- if(set != Qnil)
353
- {
354
- rb_hash_aset(options, INT2FIX(isc_dpb_lc_ctype), set);
355
- }
356
- else
357
- {
358
- rb_hash_delete(options, INT2FIX(isc_dpb_lc_ctype));
359
- }
360
-
361
- return(self);
318
+ static VALUE setDatabaseCharacterSet(VALUE self, VALUE set) {
319
+ VALUE options = rb_iv_get(self, "@options");
320
+
321
+ if(set != Qnil) {
322
+ rb_hash_aset(options, INT2FIX(isc_dpb_lc_ctype), set);
323
+ } else {
324
+ rb_hash_delete(options, INT2FIX(isc_dpb_lc_ctype));
325
+ }
326
+
327
+ return(self);
362
328
  }
363
329
 
364
330
 
365
- /**
331
+ /**
366
332
  * This function is used to integrate with the Ruby garbage collection system
367
333
  * to guarantee the release of the resources associated with a Database object.
368
334
  *
@@ -370,12 +336,10 @@ static VALUE setDatabaseCharacterSet(VALUE self, VALUE set)
370
336
  * a Database object being destroyed.
371
337
  *
372
338
  */
373
- void databaseFree(void *database)
374
- {
375
- if(database != NULL)
376
- {
377
- free((DatabaseHandle *)database);
378
- }
339
+ void databaseFree(void *database) {
340
+ if(database != NULL) {
341
+ free((DatabaseHandle *)database);
342
+ }
379
343
  }
380
344
 
381
345
 
@@ -390,9 +354,8 @@ void databaseFree(void *database)
390
354
  * @return The return value provided by execution of the provided block.
391
355
  *
392
356
  */
393
- VALUE connectBlock(VALUE connection)
394
- {
395
- return(rb_yield(connection));
357
+ VALUE connectBlock(VALUE connection) {
358
+ return(rb_yield(connection));
396
359
  }
397
360
 
398
361
 
@@ -406,10 +369,9 @@ VALUE connectBlock(VALUE connection)
406
369
  * @return Always Qnil.
407
370
  *
408
371
  */
409
- VALUE connectEnsure(VALUE connection)
410
- {
411
- rb_funcall(connection, rb_intern("close"), 0);
412
- return(Qnil);
372
+ VALUE connectEnsure(VALUE connection) {
373
+ rb_funcall(connection, rb_intern("close"), 0);
374
+ return(Qnil);
413
375
  }
414
376
 
415
377
 
@@ -421,14 +383,13 @@ VALUE connectEnsure(VALUE connection)
421
383
  * @return A reference to the Database object created.
422
384
  *
423
385
  */
424
- VALUE rb_database_new(VALUE file)
425
- {
426
- VALUE database = allocateDatabase(cDatabase),
427
- parameters[] = {file};
428
-
429
- initializeDatabase(1, parameters, database);
430
-
431
- return(database);
386
+ VALUE rb_database_new(VALUE file) {
387
+ VALUE database = allocateDatabase(cDatabase),
388
+ parameters[] = {file};
389
+
390
+ initializeDatabase(1, parameters, database);
391
+
392
+ return(database);
432
393
  }
433
394
 
434
395
 
@@ -439,15 +400,14 @@ VALUE rb_database_new(VALUE file)
439
400
  * @param module A reference to the module to create the class within.
440
401
  *
441
402
  */
442
- void Init_Database(VALUE module)
443
- {
444
- cDatabase = rb_define_class_under(module, "Database", rb_cObject);
445
- rb_define_alloc_func(cDatabase, allocateDatabase);
446
- rb_define_method(cDatabase, "initialize", initializeDatabase, -1);
447
- rb_define_method(cDatabase, "file", getDatabaseFile, 0);
448
- rb_define_method(cDatabase, "connect", connectToDatabase, -1);
449
- rb_define_method(cDatabase, "drop", dropDatabase, 2);
450
- rb_define_method(cDatabase, "character_set", getDatabaseCharacterSet, 0);
451
- rb_define_method(cDatabase, "character_set=", setDatabaseCharacterSet, 1);
452
- rb_define_module_function(cDatabase, "create", createDatabase, -1);
403
+ void Init_Database(VALUE module) {
404
+ cDatabase = rb_define_class_under(module, "Database", rb_cObject);
405
+ rb_define_alloc_func(cDatabase, allocateDatabase);
406
+ rb_define_method(cDatabase, "initialize", initializeDatabase, -1);
407
+ rb_define_method(cDatabase, "file", getDatabaseFile, 0);
408
+ rb_define_method(cDatabase, "connect", connectToDatabase, -1);
409
+ rb_define_method(cDatabase, "drop", dropDatabase, 2);
410
+ rb_define_method(cDatabase, "character_set", getDatabaseCharacterSet, 0);
411
+ rb_define_method(cDatabase, "character_set=", setDatabaseCharacterSet, 1);
412
+ rb_define_module_function(cDatabase, "create", createDatabase, -1);
453
413
  }
data/ext/Database.h CHANGED
@@ -3,20 +3,20 @@
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
@@ -25,7 +25,7 @@
25
25
  #ifndef FIRERUBY_DATABASE_H
26
26
  #define FIRERUBY_DATABASE_H
27
27
 
28
- /* Includes. */
28
+ /* Includes. */
29
29
  #ifndef FIRERUBY_FIRE_RUBY_H
30
30
  #include "FireRuby.h"
31
31
  #endif
@@ -34,15 +34,14 @@
34
34
  #include "FireRubyException.h"
35
35
  #endif
36
36
 
37
- /* Structure definitions. */
38
- typedef struct
39
- {
40
- int unused;
41
- } DatabaseHandle;
42
-
43
- /* Function prototypes. */
44
- void Init_Database(VALUE);
45
- void databaseFree(void *);
46
- VALUE rb_database_new(VALUE);
37
+ /* Structure definitions. */
38
+ typedef struct {
39
+ int unused;
40
+ } DatabaseHandle;
41
+
42
+ /* Function prototypes. */
43
+ void Init_Database(VALUE);
44
+ void databaseFree(void *);
45
+ VALUE rb_database_new(VALUE);
47
46
 
48
47
  #endif /* FIRERUBY_DATABASE_H */