ruby-oci8 2.1.7 → 2.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@
2
2
  /*
3
3
  * oradate.c
4
4
  *
5
- * Copyright (C) 2002-2012 KUBO Takehiro <kubo@jiubao.org>
5
+ * Copyright (C) 2002-2015 Kubo Takehiro <kubo@jiubao.org>
6
6
  *
7
7
  * date and time between 4712 B.C. and 9999 A.D.
8
8
  */
@@ -61,6 +61,35 @@ typedef struct ora_date ora_date_t;
61
61
  if (sec < 0 || 59 < sec) \
62
62
  rb_raise(rb_eRangeError, "Out of range for second %d (expect 0 .. 59)", sec)
63
63
 
64
+ #ifdef HAVE_RB_DATA_TYPE_T_FUNCTION
65
+ #define check_oradate(obj) ((ora_date_t*)Check_TypedStruct((obj), &odate_data_type))
66
+ #else
67
+ static ora_date_t *check_oradate(VALUE obj)
68
+ {
69
+ if (!rb_obj_is_kind_of(obj, cOraDate)) {
70
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)",
71
+ rb_obj_classname(obj), rb_class2name(cOraDate));
72
+ }
73
+ return DATA_PTR(obj);
74
+ }
75
+ #endif
76
+
77
+ static size_t odate_memsize(const void *ptr)
78
+ {
79
+ return sizeof(ora_date_t);
80
+ }
81
+
82
+ static const rb_data_type_t odate_data_type = {
83
+ "OraDate",
84
+ {NULL, RUBY_DEFAULT_FREE, odate_memsize,},
85
+ #ifdef RUBY_TYPED_FREE_IMMEDIATELY
86
+ NULL, NULL, RUBY_TYPED_FREE_IMMEDIATELY
87
+ #endif
88
+ #ifdef RUBY_TYPED_WB_PROTECTED
89
+ | RUBY_TYPED_WB_PROTECTED
90
+ #endif
91
+ };
92
+
64
93
  static void oci8_set_ora_date(ora_date_t *od, int year, int month, int day, int hour, int minute, int second)
65
94
  {
66
95
  Set_year(od, year);
@@ -74,12 +103,11 @@ static void oci8_set_ora_date(ora_date_t *od, int year, int month, int day, int
74
103
  static VALUE ora_date_s_allocate(VALUE klass)
75
104
  {
76
105
  ora_date_t *od;
77
- return Data_Make_Struct(klass, ora_date_t, NULL, xfree, od);
106
+ return TypedData_Make_Struct(klass, ora_date_t, &odate_data_type, od);
78
107
  }
79
108
 
80
109
  /*
81
- * call-seq:
82
- * initialize(year = 1, month = 1, day = 1, hour = 0, min = 0, sec = 0)
110
+ * @overload initialize(year = 1, month = 1, day = 1, hour = 0, min = 0, sec = 0)
83
111
  *
84
112
  * Returns an <code>OraDate</code> object initialized to the specified date and time.
85
113
  *
@@ -87,11 +115,18 @@ static VALUE ora_date_s_allocate(VALUE klass)
87
115
  * OraDate.new # => 0001-01-01 00:00:00
88
116
  * OraDate.new(2012) # => 2012-01-01 00:00:00
89
117
  * OraDate.new(2012, 3, 4) # => 2012-03-04 00:00:00
118
+ *
119
+ * @param [Fixnum] year year
120
+ * @param [Fixnum] month month
121
+ * @param [Fixnum] day day of month
122
+ * @param [Fixnum] hour hour
123
+ * @param [Fixnum] min minute
124
+ * @param [Fixnum] sec second
90
125
  */
91
126
  static VALUE ora_date_initialize(int argc, VALUE *argv, VALUE self)
92
127
  {
93
128
  VALUE vyear, vmonth, vday, vhour, vmin, vsec;
94
- ora_date_t *od = DATA_PTR(self);
129
+ ora_date_t *od = check_oradate(self);
95
130
  int year, month, day, hour, min, sec;
96
131
 
97
132
  rb_scan_args(argc, argv, "06", &vyear, &vmonth, &vday, &vhour, &vmin, &vsec);
@@ -147,16 +182,17 @@ static VALUE ora_date_initialize(int argc, VALUE *argv, VALUE self)
147
182
  */
148
183
  static VALUE ora_date_initialize_copy(VALUE lhs, VALUE rhs)
149
184
  {
150
- ora_date_t *l, *r;
185
+ ora_date_t *l = check_oradate(lhs);
186
+ ora_date_t *r = check_oradate(rhs);
151
187
 
152
188
  rb_call_super(1, &rhs);
153
- Data_Get_Struct(lhs, ora_date_t, l);
154
- Data_Get_Struct(rhs, ora_date_t, r);
155
189
  memcpy(l, r, sizeof(ora_date_t));
156
190
  return lhs;
157
191
  }
158
192
 
159
193
  /*
194
+ * @overload now
195
+ *
160
196
  * Returns an <code>OraDate</code> object initialized to the
161
197
  * current local time.
162
198
  *
@@ -165,7 +201,7 @@ static VALUE ora_date_initialize_copy(VALUE lhs, VALUE rhs)
165
201
  static VALUE ora_date_s_now(int argc, VALUE *argv, VALUE klass)
166
202
  {
167
203
  VALUE obj = ora_date_s_allocate(klass);
168
- ora_date_t *od = DATA_PTR(obj);
204
+ ora_date_t *od = check_oradate(obj);
169
205
  time_t tm = time(0);
170
206
  int year, month, day, hour, min, sec;
171
207
  #ifdef HAVE_LOCALTIME_R
@@ -188,6 +224,8 @@ static VALUE ora_date_s_now(int argc, VALUE *argv, VALUE klass)
188
224
  }
189
225
 
190
226
  /*
227
+ * @overload to_s
228
+ *
191
229
  * Returns a string representing <i>self</i>.
192
230
  * The string format is 'yyyy/mm/dd hh:mi:ss'.
193
231
  *
@@ -195,26 +233,26 @@ static VALUE ora_date_s_now(int argc, VALUE *argv, VALUE klass)
195
233
  */
196
234
  static VALUE ora_date_to_s(VALUE self)
197
235
  {
198
- ora_date_t *od;
236
+ ora_date_t *od = check_oradate(self);
199
237
  char buf[30];
200
238
 
201
- Data_Get_Struct(self, ora_date_t, od);
202
239
  sprintf(buf, "%04d/%02d/%02d %02d:%02d:%02d", Get_year(od), Get_month(od),
203
240
  Get_day(od), Get_hour(od), Get_minute(od), Get_second(od));
204
241
  return rb_usascii_str_new_cstr(buf);
205
242
  }
206
243
 
207
244
  /*
245
+ * @overload to_a
246
+ *
208
247
  * Returns a 6-element <i>array</i> of year, month, day, hour, minute and second.
209
248
  *
210
249
  * @return [Array]
211
250
  */
212
251
  static VALUE ora_date_to_a(VALUE self)
213
252
  {
214
- ora_date_t *od;
253
+ ora_date_t *od = check_oradate(self);
215
254
  VALUE ary[6];
216
255
 
217
- Data_Get_Struct(self, ora_date_t, od);
218
256
  ary[0] = INT2FIX(Get_year(od));
219
257
  ary[1] = INT2FIX(Get_month(od));
220
258
  ary[2] = INT2FIX(Get_day(od));
@@ -225,39 +263,39 @@ static VALUE ora_date_to_a(VALUE self)
225
263
  }
226
264
 
227
265
  /*
266
+ * @overload year
267
+ *
228
268
  * Returns the year field of <i>self</i>.
229
269
  *
230
270
  * @return [Fixnum]
231
271
  */
232
272
  static VALUE ora_date_year(VALUE self)
233
273
  {
234
- ora_date_t *od;
274
+ ora_date_t *od = check_oradate(self);
235
275
 
236
- Data_Get_Struct(self, ora_date_t, od);
237
276
  return INT2FIX(Get_year(od));
238
277
  }
239
278
 
240
279
  /*
241
- * call-seq:
242
- * year = num
280
+ * @overload year=num
243
281
  *
244
282
  * Assigns <i>num</i> to the year field of <i>self</i>.
245
283
  *
246
- * @param [Fixnum] number between -4712 and 9999
284
+ * @param [Fixnum] num number between -4712 and 9999
247
285
  */
248
286
  static VALUE ora_date_set_year(VALUE self, VALUE val)
249
287
  {
250
- ora_date_t *od;
251
- int v;
288
+ ora_date_t *od = check_oradate(self);
289
+ int v = NUM2INT(val);
252
290
 
253
- v = NUM2INT(val);
254
291
  Check_year(v);
255
- Data_Get_Struct(self, ora_date_t, od);
256
292
  Set_year(od, v);
257
293
  return self;
258
294
  }
259
295
 
260
296
  /*
297
+ * @overload month
298
+ *
261
299
  * Returns the month field of <i>self</i>.
262
300
  * The month starts with one.
263
301
  *
@@ -265,166 +303,157 @@ static VALUE ora_date_set_year(VALUE self, VALUE val)
265
303
  */
266
304
  static VALUE ora_date_month(VALUE self)
267
305
  {
268
- ora_date_t *od;
306
+ ora_date_t *od = check_oradate(self);
269
307
 
270
- Data_Get_Struct(self, ora_date_t, od);
271
308
  return INT2FIX(Get_month(od));
272
309
  }
273
310
 
274
311
  /*
275
- * call-seq:
276
- * month = num
312
+ * @overload month=num
277
313
  *
278
314
  * Assigns <i>num</i> to the month field of <i>self</i>.
279
315
  * The month starts with one.
280
316
  *
281
- * @param [Fixnum] number between 1 and 12
317
+ * @param [Fixnum] num number between 1 and 12
282
318
  */
283
319
  static VALUE ora_date_set_month(VALUE self, VALUE val)
284
320
  {
285
- ora_date_t *od;
286
- int v;
321
+ ora_date_t *od = check_oradate(self);
322
+ int v = NUM2INT(val);
287
323
 
288
- v = NUM2INT(val);
289
324
  Check_month(v);
290
- Data_Get_Struct(self, ora_date_t, od);
291
325
  Set_month(od, v);
292
326
  return self;
293
327
  }
294
328
 
295
329
  /*
330
+ * @overload day
331
+ *
296
332
  * Returns the day of month field of <i>self</i>.
297
333
  *
298
334
  * @return [Fixnum]
299
335
  */
300
336
  static VALUE ora_date_day(VALUE self)
301
337
  {
302
- ora_date_t *od;
338
+ ora_date_t *od = check_oradate(self);
303
339
 
304
- Data_Get_Struct(self, ora_date_t, od);
305
340
  return INT2FIX(Get_day(od));
306
341
  }
307
342
 
308
343
  /*
309
- * call-seq:
310
- * day = num
344
+ * @overload day=num
311
345
  *
312
346
  * Assigns <i>num</i> to the day of month field of <i>self</i>.
313
347
  *
314
- * @param [Fixnum] number between 1 and 31
348
+ * @param [Fixnum] num number between 1 and 31
315
349
  */
316
350
  static VALUE ora_date_set_day(VALUE self, VALUE val)
317
351
  {
318
- ora_date_t *od;
319
- int v;
352
+ ora_date_t *od = check_oradate(self);
353
+ int v = NUM2INT(val);
320
354
 
321
- v = NUM2INT(val);
322
355
  Check_day(v);
323
- Data_Get_Struct(self, ora_date_t, od);
324
356
  Set_day(od, v);
325
357
  return self;
326
358
  }
327
359
 
328
360
  /*
361
+ * @overload hour
362
+ *
329
363
  * Returns the hour field of <i>self</i>.
330
364
  *
331
365
  * @return [Fixnum]
332
366
  */
333
367
  static VALUE ora_date_hour(VALUE self)
334
368
  {
335
- ora_date_t *od;
369
+ ora_date_t *od = check_oradate(self);
336
370
 
337
- Data_Get_Struct(self, ora_date_t, od);
338
371
  return INT2FIX(Get_hour(od));
339
372
  }
340
373
 
341
374
  /*
342
- * call-seq:
343
- * hour = num
375
+ * @overload hour=num
344
376
  *
345
377
  * Assigns <i>num</i> to the hour field of <i>self</i>.
346
378
  *
347
- * @param [Fixnum] number between 0 and 23
379
+ * @param [Fixnum] num number between 0 and 23
348
380
  */
349
381
  static VALUE ora_date_set_hour(VALUE self, VALUE val)
350
382
  {
351
- ora_date_t *od;
352
- int v;
383
+ ora_date_t *od = check_oradate(self);
384
+ int v = NUM2INT(val);
353
385
 
354
- v = NUM2INT(val);
355
386
  Check_hour(v);
356
- Data_Get_Struct(self, ora_date_t, od);
357
387
  Set_hour(od, v);
358
388
  return self;
359
389
  }
360
390
 
361
391
  /*
392
+ * @overload minute
393
+ *
362
394
  * Returns the minute field of <i>self</i>.
363
395
  *
364
396
  * @return [Fixnum]
365
397
  */
366
398
  static VALUE ora_date_minute(VALUE self)
367
399
  {
368
- ora_date_t *od;
400
+ ora_date_t *od = check_oradate(self);
369
401
 
370
- Data_Get_Struct(self, ora_date_t, od);
371
402
  return INT2FIX(Get_minute(od));
372
403
  }
373
404
 
374
405
  /*
375
- * call-seq:
376
- * minute = num
406
+ * @overload minute=num
377
407
  *
378
408
  * Assigns <i>num</i> to the minute field of <i>self</i>.
379
409
  *
380
- * @param [Fixnum] number between 0 and 59
410
+ * @param [Fixnum] num number between 0 and 59
381
411
  */
382
412
  static VALUE ora_date_set_minute(VALUE self, VALUE val)
383
413
  {
384
- ora_date_t *od;
385
- int v;
414
+ ora_date_t *od = check_oradate(self);
415
+ int v = NUM2INT(val);
386
416
 
387
- v = NUM2INT(val);
388
417
  Check_minute(v);
389
- Data_Get_Struct(self, ora_date_t, od);
390
418
  Set_minute(od, v);
391
419
  return self;
392
420
  }
393
421
 
394
422
  /*
423
+ * @overload second
424
+ *
395
425
  * Returns the second field of <i>self</i>.
396
426
  *
397
427
  * @return [Fixnum]
398
428
  */
399
429
  static VALUE ora_date_second(VALUE self)
400
430
  {
401
- ora_date_t *od;
431
+ ora_date_t *od = check_oradate(self);
402
432
 
403
- Data_Get_Struct(self, ora_date_t, od);
404
433
  return INT2FIX(Get_second(od));
405
434
  }
406
435
 
407
436
  /*
408
- * call-seq:
409
- * second = num
437
+ * @overload second=num
410
438
  *
411
439
  * Assigns <i>num</i> to the second field of <i>self</i>.
412
440
  *
413
- * @param [Fixnum] number between 0 and 59
441
+ * @param [Fixnum] num number between 0 and 59
414
442
  */
415
443
  static VALUE ora_date_set_second(VALUE self, VALUE val)
416
444
  {
417
- ora_date_t *od;
445
+ ora_date_t *od = check_oradate(self);
418
446
  int v;
419
447
 
420
448
  v = NUM2INT(val);
421
449
  Check_second(v);
422
- Data_Get_Struct(self, ora_date_t, od);
423
450
  Set_second(od, v);
424
451
  return self;
425
452
  }
426
453
 
427
454
  /*
455
+ * @overload trunc
456
+ *
428
457
  * Truncates hour, minute and second fields to zero.
429
458
  *
430
459
  * @example
@@ -435,9 +464,8 @@ static VALUE ora_date_set_second(VALUE self, VALUE val)
435
464
  */
436
465
  static VALUE ora_date_trunc(VALUE self)
437
466
  {
438
- ora_date_t *od;
467
+ ora_date_t *od = check_oradate(self);
439
468
 
440
- Data_Get_Struct(self, ora_date_t, od);
441
469
  od->hour = 1;
442
470
  od->minute = 1;
443
471
  od->second = 1;
@@ -445,18 +473,18 @@ static VALUE ora_date_trunc(VALUE self)
445
473
  }
446
474
 
447
475
  /*
448
- * call-seq:
449
- * self <=> other
476
+ * @overload <=>(other)
450
477
  *
451
478
  * Returns -1, 0, or +1 depending on whether <i>self</i> is less than,
452
479
  * equal to, or greater than <i>other</i>.
480
+ *
481
+ * @return [-1, 0, +1]
453
482
  */
454
483
  static VALUE ora_date_cmp(VALUE self, VALUE val)
455
484
  {
456
- ora_date_t *od1, *od2;
457
- Data_Get_Struct(self, ora_date_t, od1);
458
- Check_Object(val, cOraDate);
459
- Data_Get_Struct(val, ora_date_t, od2);
485
+ ora_date_t *od1 = check_oradate(self);
486
+ ora_date_t *od2 = check_oradate(val);
487
+
460
488
  if (od1->century < od2->century) return INT2FIX(-1);
461
489
  if (od1->century > od2->century) return INT2FIX(1);
462
490
  if (od1->year < od2->year) return INT2FIX(-1);
@@ -479,10 +507,9 @@ static VALUE ora_date_cmp(VALUE self, VALUE val)
479
507
  */
480
508
  static VALUE ora_date_hash(VALUE self)
481
509
  {
482
- ora_date_t *od;
510
+ ora_date_t *od = check_oradate(self);
483
511
  unsigned int v;
484
512
 
485
- Data_Get_Struct(self, ora_date_t, od);
486
513
  v = (od->century << 8)
487
514
  + (od->year << 15)
488
515
  + (od->month << 22)
@@ -494,6 +521,8 @@ static VALUE ora_date_hash(VALUE self)
494
521
  }
495
522
 
496
523
  /*
524
+ * @overload _dump
525
+ *
497
526
  * Serializes <i>self</i>.
498
527
  * This method is called by Marshal.dump().
499
528
  *
@@ -502,14 +531,13 @@ static VALUE ora_date_hash(VALUE self)
502
531
  */
503
532
  static VALUE ora_date_dump(int argc, VALUE *argv, VALUE self)
504
533
  {
505
- ora_date_t *od;
506
- Data_Get_Struct(self, ora_date_t, od);
534
+ ora_date_t *od = check_oradate(self);
535
+
507
536
  return rb_str_new((const char*)od, sizeof(ora_date_t)); /* ASCII-8BIT */
508
537
  }
509
538
 
510
539
  /*
511
- * call-seq:
512
- * _load(bytes)
540
+ * @overload _load(bytes)
513
541
  *
514
542
  * Restores a byte stream serialized by {OraDate#_dump}.
515
543
  * This method is called by Marshal.load() to deserialize a byte stream
@@ -527,7 +555,7 @@ static VALUE ora_date_s_load(VALUE klass, VALUE str)
527
555
  if (RSTRING_LEN(str) != sizeof(ora_date_t)) {
528
556
  rb_raise(rb_eTypeError, "marshaled OraDate format differ");
529
557
  }
530
- obj = Data_Make_Struct(cOraDate, ora_date_t, NULL, xfree, od);
558
+ obj = TypedData_Make_Struct(cOraDate, ora_date_t, &odate_data_type, od);
531
559
  memcpy(od, RSTRING_PTR(str), sizeof(ora_date_t));
532
560
  return obj;
533
561
  }
@@ -542,17 +570,15 @@ static VALUE bind_oradate_get(oci8_bind_t *obind, void *data, void *null_struct)
542
570
  {
543
571
  ora_date_t *od;
544
572
 
545
- VALUE obj = Data_Make_Struct(cOraDate, ora_date_t, NULL, xfree, od);
573
+ VALUE obj = TypedData_Make_Struct(cOraDate, ora_date_t, &odate_data_type, od);
546
574
  memcpy(od, data, sizeof(ora_date_t));
547
575
  return obj;
548
576
  }
549
577
 
550
578
  static void bind_oradate_set(oci8_bind_t *obind, void *data, void **null_structp, VALUE val)
551
579
  {
552
- ora_date_t *od;
580
+ ora_date_t *od = check_oradate(val);
553
581
 
554
- Check_Object(val, cOraDate);
555
- Data_Get_Struct(val, ora_date_t, od);
556
582
  memcpy(data, od, sizeof(ora_date_t));
557
583
  }
558
584
 
@@ -571,9 +597,20 @@ static void bind_oradate_init_elem(oci8_bind_t *obind, VALUE svc)
571
597
  } while (++idx < obind->maxar_sz);
572
598
  }
573
599
 
574
- static const oci8_bind_vtable_t bind_oradate_vtable = {
600
+ static const oci8_bind_data_type_t bind_oradate_data_type = {
575
601
  {
576
- NULL,
602
+ {
603
+ "OCI8::BindType::OraDate",
604
+ {
605
+ NULL,
606
+ oci8_handle_cleanup,
607
+ oci8_handle_size,
608
+ },
609
+ &oci8_bind_data_type.rb_data_type, NULL,
610
+ #ifdef RUBY_TYPED_WB_PROTECTED
611
+ RUBY_TYPED_WB_PROTECTED,
612
+ #endif
613
+ },
577
614
  oci8_bind_free,
578
615
  sizeof(oci8_bind_t)
579
616
  },
@@ -585,6 +622,11 @@ static const oci8_bind_vtable_t bind_oradate_vtable = {
585
622
  SQLT_DAT,
586
623
  };
587
624
 
625
+ static VALUE bind_oradate_alloc(VALUE klass)
626
+ {
627
+ return oci8_allocate_typeddata(klass, &bind_oradate_data_type.base);
628
+ }
629
+
588
630
  void Init_ora_date(void)
589
631
  {
590
632
  cOraDate = rb_define_class("OraDate", rb_cObject);
@@ -624,5 +666,5 @@ void Init_ora_date(void)
624
666
  rb_define_method(cOraDate, "_dump", ora_date_dump, -1);
625
667
  rb_define_singleton_method(cOraDate, "_load", ora_date_s_load, 1);
626
668
 
627
- oci8_define_bind_class("OraDate", &bind_oradate_vtable);
669
+ oci8_define_bind_class("OraDate", &bind_oradate_data_type, bind_oradate_alloc);
628
670
  }