fb 0.7.4 → 0.7.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 56b9a209c188f5a267c3dda503f72226a467eaa2
4
- data.tar.gz: e443a11b2c99d410856eabdc463cf8146e9fb261
3
+ metadata.gz: 8d355708819fa90d7b6d797ea6a66d4df6bc724c
4
+ data.tar.gz: 6ec6b2d0ec8ca6725a69378ad511712cb68b2841
5
5
  SHA512:
6
- metadata.gz: 2b1d66d743e5649eb9fcf65406b8e1d78b747cac4e420867954b02b792875ddb2acf5b8c06a396d7c674869afdb2d0c24b4f903c940f062c8c622f1be24bbe1a
7
- data.tar.gz: dda09a0abc589816ce41e88a13d40d335787ee9d9b6c3978f56f96b129fe1ca6b849e5e7d2955737c823cc40dac221b7197a6cd7e733b6e41c5f978f911af3a6
6
+ metadata.gz: c6438765aa02be661b1ca49b21459b200329dc4466d925509be5967ea3bc9658b239b55a6206a4100ee0c0f69a6a56f25fd34eb0a6304be06303cfb288f069d1
7
+ data.tar.gz: 642f27e3b5d0d5adbe28ed6692453278a9b6beb2c5e1f9f715679e72a699655a4d494f13356e37e4664f658fa71ca55303832a75d03ba1f81f5a6a59fd7ebee7
data/fb.c CHANGED
@@ -141,10 +141,6 @@ typedef struct trans_opts
141
141
  struct trans_opts *sub_opts;
142
142
  } trans_opts;
143
143
 
144
- /* global data */
145
- /* static isc_tr_handle global_transact = 0; */ /* transaction handle */
146
- /* static int connection_count = 0; */
147
-
148
144
  /* global utilities */
149
145
 
150
146
  #define FB_ALIGN(n, b) ((n + b - 1) & ~(b - 1))
@@ -325,24 +321,22 @@ static void tm_from_timestamp(struct tm *tm, VALUE obj)
325
321
  #endif
326
322
  }
327
323
 
328
- static VALUE long_from_obj(VALUE obj)
329
- {
330
- ID id_to_str = rb_intern("to_str");
331
- if (TYPE(obj) != T_FIXNUM && rb_respond_to(obj, id_to_str))
332
- {
333
- VALUE s = rb_funcall(obj, id_to_str, 0);
334
- obj = rb_funcall(s, rb_intern("to_i"), 0);
335
- }
336
- return obj;
337
- }
338
-
339
- static VALUE ll_from_obj(VALUE obj)
324
+ static VALUE object_to_fixnum(VALUE object)
340
325
  {
341
- if (TYPE(obj) == T_STRING)
326
+ if (TYPE(object) != T_FIXNUM && TYPE(object) != T_BIGNUM)
342
327
  {
343
- obj = rb_funcall(obj, rb_intern("to_i"), 0);
328
+ if (TYPE(object) == T_FLOAT || !strcmp(rb_class2name(CLASS_OF(object)), "BigDecimal"))
329
+ object = rb_funcall(object, rb_intern("round"), 0);
330
+ else if (TYPE(object) == T_STRING)
331
+ object = rb_funcall(rb_funcall(rb_mKernel, rb_intern("BigDecimal"), 1, object), rb_intern("round"), 0);
332
+ else if (!strcmp(rb_class2name(CLASS_OF(object)), "Time"))
333
+ rb_raise(rb_eTypeError, "Time value not allowed as Integer");
334
+ else if (rb_respond_to(object, rb_intern("to_i")))
335
+ object = rb_funcall(object, rb_intern("to_i"), 0);
336
+ else
337
+ rb_raise(rb_eTypeError, "Value doesn't respond to 'to_i' for conversion");
344
338
  }
345
- return obj;
339
+ return (object);
346
340
  }
347
341
 
348
342
  static VALUE double_from_obj(VALUE obj)
@@ -507,21 +501,6 @@ static void fb_connection_check(struct FbConnection *fb_connection)
507
501
  }
508
502
  }
509
503
 
510
- /*
511
- static void global_close_cursors()
512
- {
513
- struct FbConnection *list = fb_connection_list;
514
- int i;
515
-
516
- while (list) {
517
- for (i = 0; i < RARRAY(list->cursor)->len; i++) {
518
- cursor_close(RARRAY(list->cursor)->ptr[i]);
519
- }
520
- list = list->next;
521
- }
522
- }
523
- */
524
-
525
504
  static void fb_connection_close_cursors(struct FbConnection *fb_connection)
526
505
  {
527
506
  int i;
@@ -541,28 +520,6 @@ static void fb_connection_drop_cursors(struct FbConnection *fb_connection)
541
520
  rb_ary_clear(fb_connection->cursor);
542
521
  }
543
522
 
544
- /*
545
- static void fb_connection_remove(struct FbConnection *fb_connection)
546
- {
547
- if (fb_connection_list != NULL) {
548
- if (fb_connection_list == fb_connection) {
549
- fb_connection_list = fb_connection_list->next;
550
- } else {
551
- struct FbConnection *list = fb_connection_list;
552
- while (list->next) {
553
- if (list->next == fb_connection) {
554
- list->next = fb_connection->next;
555
- break;
556
- }
557
- list = list->next;
558
- }
559
- }
560
- fb_connection->db = 0;
561
- connection_count--;
562
- }
563
- }
564
- */
565
-
566
523
  static void fb_connection_disconnect(struct FbConnection *fb_connection)
567
524
  {
568
525
  if (fb_connection->transact) {
@@ -575,7 +532,6 @@ static void fb_connection_disconnect(struct FbConnection *fb_connection)
575
532
  isc_detach_database(fb_connection->isc_status, &fb_connection->db);
576
533
  }
577
534
  fb_error_check(fb_connection->isc_status);
578
- /* fb_connection_remove(fb_connection); */
579
535
  }
580
536
 
581
537
  static void fb_connection_disconnect_warn(struct FbConnection *fb_connection)
@@ -586,7 +542,6 @@ static void fb_connection_disconnect_warn(struct FbConnection *fb_connection)
586
542
  }
587
543
  isc_detach_database(fb_connection->isc_status, &fb_connection->db);
588
544
  fb_error_check_warn(fb_connection->isc_status);
589
- /* fb_connection_remove(fb_connection); */
590
545
  }
591
546
 
592
547
  static void fb_connection_mark(struct FbConnection *fb_connection)
@@ -957,145 +912,6 @@ error:
957
912
  rb_raise(rb_eFbError, "%s", desc);
958
913
  }
959
914
 
960
- /*
961
- static void set_teb_vec(ISC_TEB *vec, struct FbConnection *fb_connection, char *tpb, int len)
962
- {
963
- vec->dbb_ptr = &fb_connection->db;
964
- if (tpb) {
965
- vec->tpb_ptr = tpb;
966
- vec->tpb_len = len;
967
- } else {
968
- vec->tpb_ptr = 0;
969
- vec->tpb_len = 0;
970
- }
971
- }
972
- */
973
-
974
- /*
975
- static void global_transaction_start(VALUE opt, int argc, VALUE *argv)
976
- {
977
- ISC_STATUS isc_status[20];
978
- struct FbConnection *fb_connection;
979
- ISC_TEB *teb_vec = ALLOCA_N(ISC_TEB, connection_count);
980
- ISC_TEB *vec = teb_vec;
981
- char *tpb = 0;
982
- short n;
983
- int tpb_len;
984
-
985
- if (global_transact) {
986
- rb_raise(rb_eFbError, "The transaction has been already started");
987
- }
988
-
989
- if (!NIL_P(opt)) {
990
- tpb = trans_parseopts(opt, &tpb_len);
991
- }
992
-
993
- if (argc > connection_count) {
994
- rb_raise(rb_eFbError, "Too many databases specified for the transaction");
995
- }
996
- if (argc == 0) {
997
- n = connection_count;
998
- for (fb_connection = fb_connection_list; fb_connection; fb_connection = fb_connection->next) {
999
- set_teb_vec(vec, fb_connection, tpb, tpb_len);
1000
- vec++;
1001
- }
1002
- } else {
1003
- for (n = 0; n < argc; n++) {
1004
- fb_connection = fb_connection_check_retrieve(argv[n]);
1005
- set_teb_vec(vec, fb_connection, tpb, tpb_len);
1006
- vec++;
1007
- }
1008
- }
1009
-
1010
- isc_start_multiple(isc_status, &global_transact, n, teb_vec);
1011
- xfree(tpb);
1012
- fb_error_check(isc_status);
1013
- }
1014
- */
1015
-
1016
- /* call-seq:
1017
- * commit() -> nil
1018
- *
1019
- * Commit the current (global) transaction.
1020
- */
1021
- /*
1022
- static VALUE global_commit()
1023
- {
1024
- ISC_STATUS isc_status[20];
1025
-
1026
- if (global_transact) {
1027
- global_close_cursors();
1028
- isc_commit_transaction(isc_status, &global_transact);
1029
- fb_error_check(isc_status);
1030
- }
1031
- return Qnil;
1032
- }
1033
- */
1034
-
1035
- /* call-seq:
1036
- * rollback() -> nil
1037
- *
1038
- * Rollback the current (global) transaction.
1039
- */
1040
- /*
1041
- static VALUE global_rollback()
1042
- {
1043
- ISC_STATUS isc_status[20];
1044
-
1045
- if (global_transact) {
1046
- global_close_cursors();
1047
- isc_rollback_transaction(isc_status, &global_transact);
1048
- fb_error_check(isc_status);
1049
- }
1050
- return Qnil;
1051
- }
1052
- */
1053
-
1054
- /* call-seq:
1055
- * transaction(options, *connections) -> true
1056
- * transaction(options, *connections) { } -> block result
1057
- *
1058
- * Start a (global) transaction.
1059
- */
1060
- /*
1061
- static VALUE global_transaction(int argc, VALUE *argv, VALUE self)
1062
- {
1063
- VALUE opt = Qnil;
1064
-
1065
- if (argc > 0) {
1066
- opt = *argv++;
1067
- argc--;
1068
- }
1069
- global_transaction_start(opt, argc, argv);
1070
-
1071
- if (rb_block_given_p()) {
1072
- int state;
1073
- VALUE result = rb_protect(rb_yield, 0, &state);
1074
- if (state) {
1075
- global_rollback();
1076
- return rb_funcall(rb_mKernel, rb_intern("raise"), 0);
1077
- } else {
1078
- global_commit();
1079
- return result;
1080
- }
1081
- } else {
1082
- return Qtrue;
1083
- }
1084
- }
1085
- */
1086
-
1087
- /* call-seq:
1088
- * transaction_started()? -> true or false
1089
- *
1090
- * Returns true if a transaction is currently active.
1091
- */
1092
- /*
1093
- static VALUE global_transaction_started()
1094
- {
1095
- return global_transact ? Qtrue : Qfalse;
1096
- }
1097
- */
1098
-
1099
915
  static void fb_connection_transaction_start(struct FbConnection *fb_connection, VALUE opt)
1100
916
  {
1101
917
  char *tpb = 0;
@@ -1467,6 +1283,32 @@ static void fb_cursor_free(struct FbCursor *fb_cursor)
1467
1283
  xfree(fb_cursor);
1468
1284
  }
1469
1285
 
1286
+ static VALUE sql_decimal_to_bigdecimal(long long sql_data, int scale)
1287
+ {
1288
+ int i;
1289
+ char bigdecimal_buffer[23];
1290
+ int bigdecimal_dot;
1291
+ sprintf(bigdecimal_buffer, "%022lld", sql_data);
1292
+ bigdecimal_dot = strlen(bigdecimal_buffer) + scale;
1293
+ for (i = strlen(bigdecimal_buffer); i > bigdecimal_dot; i--)
1294
+ bigdecimal_buffer[i] = bigdecimal_buffer[i-1];
1295
+ bigdecimal_buffer[bigdecimal_dot] = '.';
1296
+ return rb_funcall(rb_path2class("BigDecimal"), rb_intern("new"), 1, rb_str_new2(bigdecimal_buffer));
1297
+ }
1298
+
1299
+ static VALUE object_to_unscaled_bigdecimal(VALUE object, int scale)
1300
+ {
1301
+ int i;
1302
+ long ratio = 1;
1303
+ for (i = 0; i > scale; i--)
1304
+ ratio *= 10;
1305
+ if (TYPE(object) == T_FLOAT)
1306
+ object = rb_funcall(object, rb_intern("to_s"), 0);
1307
+ object = rb_funcall(rb_path2class("BigDecimal"), rb_intern("new"), 1, object);
1308
+ object = rb_funcall(object, rb_intern("*"), 1, LONG2NUM(ratio));
1309
+ return rb_funcall(object, rb_intern("round"), 0);
1310
+ }
1311
+
1470
1312
  static void fb_cursor_set_inputparams(struct FbCursor *fb_cursor, long argc, VALUE *argv)
1471
1313
  {
1472
1314
  struct FbConnection *fb_connection;
@@ -1478,9 +1320,7 @@ static void fb_cursor_set_inputparams(struct FbCursor *fb_cursor, long argc, VAL
1478
1320
  long lvalue;
1479
1321
  ISC_INT64 llvalue;
1480
1322
  long alignment;
1481
- double ratio;
1482
1323
  double dvalue;
1483
- long scnt;
1484
1324
  double dcheck;
1485
1325
  VARY *vary;
1486
1326
  XSQLVAR *var;
@@ -1547,21 +1387,11 @@ static void fb_cursor_set_inputparams(struct FbCursor *fb_cursor, long argc, VAL
1547
1387
  offset = FB_ALIGN(offset, alignment);
1548
1388
  var->sqldata = (char *)(fb_cursor->i_buffer + offset);
1549
1389
  if (var->sqlscale < 0) {
1550
- ratio = 1;
1551
- for (scnt = 0; scnt > var->sqlscale; scnt--)
1552
- ratio *= 10;
1553
- obj = double_from_obj(obj);
1554
- dvalue = NUM2DBL(obj) * ratio;
1555
- if (dvalue >= 0.0) {
1556
- lvalue = (ISC_LONG)(dvalue + 0.5);
1557
- } else {
1558
- lvalue = (ISC_LONG)(dvalue - 0.5);
1559
- }
1390
+ lvalue = NUM2LONG(object_to_unscaled_bigdecimal(obj, var->sqlscale));
1560
1391
  } else {
1561
- obj = long_from_obj(obj);
1562
- lvalue = NUM2LONG(obj);
1392
+ lvalue = NUM2LONG(object_to_fixnum(obj));
1563
1393
  }
1564
- if (lvalue < SHRT_MIN || lvalue > SHRT_MAX) {
1394
+ if (lvalue < -32768 || lvalue > 32767) {
1565
1395
  rb_raise(rb_eRangeError, "short integer overflow");
1566
1396
  }
1567
1397
  *(short *)var->sqldata = lvalue;
@@ -1572,22 +1402,12 @@ static void fb_cursor_set_inputparams(struct FbCursor *fb_cursor, long argc, VAL
1572
1402
  offset = FB_ALIGN(offset, alignment);
1573
1403
  var->sqldata = (char *)(fb_cursor->i_buffer + offset);
1574
1404
  if (var->sqlscale < 0) {
1575
- ratio = 1;
1576
- for (scnt = 0; scnt > var->sqlscale; scnt--)
1577
- ratio *= 10;
1578
- obj = double_from_obj(obj);
1579
- dvalue = NUM2DBL(obj) * ratio;
1580
- if (dvalue >= 0.0) {
1581
- lvalue = (ISC_LONG)(dvalue + 0.5);
1582
- } else {
1583
- lvalue = (ISC_LONG)(dvalue - 0.5);
1584
- }
1405
+ lvalue = NUM2LONG(object_to_unscaled_bigdecimal(obj, var->sqlscale));
1585
1406
  } else {
1586
- obj = long_from_obj(obj);
1587
- lvalue = NUM2LONG(obj);
1407
+ lvalue = NUM2LONG(object_to_fixnum(obj));
1588
1408
  }
1589
- if (lvalue < -2147483647 || lvalue > 2147483647) {
1590
- rb_raise(rb_eRangeError, "integer overflow");
1409
+ if (lvalue < -2147483648 || lvalue > 2147483647) {
1410
+ rb_raise(rb_eRangeError, "integer overflow");
1591
1411
  }
1592
1412
  *(ISC_LONG *)var->sqldata = (ISC_LONG)lvalue;
1593
1413
  offset += alignment;
@@ -1624,19 +1444,9 @@ static void fb_cursor_set_inputparams(struct FbCursor *fb_cursor, long argc, VAL
1624
1444
  var->sqldata = (char *)(fb_cursor->i_buffer + offset);
1625
1445
 
1626
1446
  if (var->sqlscale < 0) {
1627
- ratio = 1;
1628
- for (scnt = 0; scnt > var->sqlscale; scnt--)
1629
- ratio *= 10;
1630
- obj = double_from_obj(obj);
1631
- dvalue = NUM2DBL(obj) * ratio;
1632
- if (dvalue >= 0.0) {
1633
- llvalue = (ISC_INT64)(dvalue + 0.5);
1634
- } else {
1635
- llvalue = (ISC_INT64)(dvalue - 0.5);
1636
- }
1447
+ llvalue = NUM2LL(object_to_unscaled_bigdecimal(obj, var->sqlscale));
1637
1448
  } else {
1638
- obj = ll_from_obj(obj);
1639
- llvalue = NUM2LL(obj);
1449
+ llvalue = NUM2LL(object_to_fixnum(obj));
1640
1450
  }
1641
1451
 
1642
1452
  *(ISC_INT64 *)var->sqldata = llvalue;
@@ -1941,9 +1751,6 @@ static VALUE fb_cursor_fetch(struct FbCursor *fb_cursor)
1941
1751
  long dtp;
1942
1752
  VALUE val;
1943
1753
  VARY *vary;
1944
- double ratio;
1945
- double dval;
1946
- long scnt;
1947
1754
  struct tm tms;
1948
1755
 
1949
1756
  isc_blob_handle blob_handle;
@@ -2008,10 +1815,7 @@ static VALUE fb_cursor_fetch(struct FbCursor *fb_cursor)
2008
1815
 
2009
1816
  case SQL_SHORT:
2010
1817
  if (var->sqlscale < 0) {
2011
- ratio = 1;
2012
- for (scnt = 0; scnt > var->sqlscale; scnt--) ratio *= 10;
2013
- dval = (double)*(short*)var->sqldata/ratio;
2014
- val = rb_float_new(dval);
1818
+ val = sql_decimal_to_bigdecimal((long long)*(ISC_SHORT*)var->sqldata, var->sqlscale);
2015
1819
  } else {
2016
1820
  val = INT2NUM((long)*(short*)var->sqldata);
2017
1821
  }
@@ -2019,10 +1823,7 @@ static VALUE fb_cursor_fetch(struct FbCursor *fb_cursor)
2019
1823
 
2020
1824
  case SQL_LONG:
2021
1825
  if (var->sqlscale < 0) {
2022
- ratio = 1;
2023
- for (scnt = 0; scnt > var->sqlscale; scnt--) ratio *= 10;
2024
- dval = (double)*(ISC_LONG*)var->sqldata/ratio;
2025
- val = rb_float_new(dval);
1826
+ val = sql_decimal_to_bigdecimal((long long)*(ISC_LONG*)var->sqldata, var->sqlscale);
2026
1827
  } else {
2027
1828
  val = INT2NUM(*(ISC_LONG*)var->sqldata);
2028
1829
  }
@@ -2037,14 +1838,11 @@ static VALUE fb_cursor_fetch(struct FbCursor *fb_cursor)
2037
1838
  break;
2038
1839
 
2039
1840
  case SQL_INT64:
2040
- if (var->sqlscale < 0) {
2041
- ratio = 1;
2042
- for (scnt = 0; scnt > var->sqlscale; scnt--) ratio *= 10;
2043
- dval = (double)*(ISC_INT64*)var->sqldata/ratio;
2044
- val = rb_float_new(dval);
2045
- } else {
2046
- val = LL2NUM(*(ISC_INT64*)var->sqldata);
2047
- }
1841
+ if (var->sqlscale < 0) {
1842
+ val = sql_decimal_to_bigdecimal(*(ISC_INT64*)var->sqldata, var->sqlscale);
1843
+ } else {
1844
+ val = LL2NUM(*(ISC_INT64*)var->sqldata);
1845
+ }
2048
1846
  break;
2049
1847
 
2050
1848
  case SQL_TIMESTAMP:
@@ -2375,6 +2173,7 @@ static VALUE cursor_fetch(int argc, VALUE* argv, VALUE self)
2375
2173
  fb_cursor_fetch_prep(fb_cursor);
2376
2174
 
2377
2175
  ary = fb_cursor_fetch(fb_cursor);
2176
+ if (NIL_P(ary)) return Qnil;
2378
2177
  return hash_row ? fb_hash_from_ary(fb_cursor->fields_ary, ary) : ary;
2379
2178
  }
2380
2179
 
@@ -2609,11 +2408,6 @@ static VALUE connection_create(isc_db_handle handle, VALUE db)
2609
2408
  fb_connection->db = handle;
2610
2409
  fb_connection->transact = 0;
2611
2410
  fb_connection->cursor = rb_ary_new();
2612
- /*
2613
- connection_count++;
2614
- fb_connection->next = fb_connection_list;
2615
- fb_connection_list = fb_connection;
2616
- */
2617
2411
  dialect = SQL_DIALECT_CURRENT;
2618
2412
  db_dialect = fb_connection_db_SQL_Dialect(fb_connection);
2619
2413
 
@@ -3120,6 +2914,8 @@ static VALUE database_s_drop(int argc, VALUE *argv, VALUE klass)
3120
2914
 
3121
2915
  void Init_fb()
3122
2916
  {
2917
+ rb_funcall(rb_mKernel, rb_intern("require"), 1, rb_str_new2("bigdecimal"));
2918
+
3123
2919
  rb_mFb = rb_define_module("Fb");
3124
2920
 
3125
2921
  rb_cFbDatabase = rb_define_class_under(rb_mFb, "Database", rb_cData);
@@ -3182,14 +2978,6 @@ void Init_fb()
3182
2978
  rb_cFbSqlType = rb_define_class_under(rb_mFb, "SqlType", rb_cData);
3183
2979
  rb_define_singleton_method(rb_cFbSqlType, "from_code", sql_type_from_code, 2);
3184
2980
 
3185
- /*
3186
- rb_cFbGlobal = rb_define_class_under(rb_mFb, "Global", rb_cData);
3187
- rb_define_singleton_method(rb_cFbGlobal, "transaction", global_transaction, -1);
3188
- rb_define_singleton_method(rb_cFbGlobal, "transaction_started", global_transaction_started, 0);
3189
- rb_define_singleton_method(rb_cFbGlobal, "commit", global_commit, 0);
3190
- rb_define_singleton_method(rb_cFbGlobal, "rollback", global_rollback, 0);
3191
- */
3192
-
3193
2981
  rb_eFbError = rb_define_class_under(rb_mFb, "Error", rb_eStandardError);
3194
2982
  rb_define_method(rb_eFbError, "error_code", error_error_code, 0);
3195
2983
 
@@ -11,6 +11,12 @@ class CursorTestCases < FbTestCase
11
11
  assert_instance_of Array, row
12
12
  assert_equal 4, row.size
13
13
  end
14
+ connection.execute("select * from rdb$database where rdb$description = 'bogus'") do |cursor|
15
+ assert_instance_of Cursor, cursor
16
+ row = cursor.fetch :array
17
+ assert_instance_of NilClass, row
18
+ assert_equal nil, row
19
+ end
14
20
  connection.drop
15
21
  end
16
22
  end
@@ -23,6 +29,12 @@ class CursorTestCases < FbTestCase
23
29
  assert_instance_of Hash, row
24
30
  assert_equal 4, row.size
25
31
  end
32
+ connection.execute("select * from rdb$database where rdb$description = 'bogus'") do |cursor|
33
+ assert_instance_of Cursor, cursor
34
+ row = cursor.fetch :hash
35
+ assert_instance_of NilClass, row
36
+ assert_equal nil, row
37
+ end
26
38
  connection.drop
27
39
  end
28
40
  end
@@ -1,3 +1,4 @@
1
+ require 'bigdecimal'
1
2
  require 'test/FbTestCases'
2
3
 
3
4
  class DataTypesTestCases < FbTestCase
@@ -482,7 +483,7 @@ class DataTypesTestCases < FbTestCase
482
483
  connection.execute(sql_insert, "12345.12")
483
484
  connection.execute(sql_insert, -12345.12)
484
485
  vals = connection.query(sql_select)
485
- # puts vals.inspect
486
+ assert vals[0][0].is_a?(BigDecimal), "Numeric(9, 2) must return BigDecimal"
486
487
  assert_equal 12345.12, vals[0][0], "NUMERIC (decimal)"
487
488
  assert_equal 12345.12, vals[1][0], "NUMERIC (string)"
488
489
  assert_equal -12345.12, vals[2][0], "NUMERIC (string)"
@@ -491,19 +492,20 @@ class DataTypesTestCases < FbTestCase
491
492
  connection.execute(sql_insert, "12345.12")
492
493
  connection.execute(sql_insert, -12345.12)
493
494
  vals = connection.query(sql_select)
494
- # puts vals.inspect
495
+ assert vals[0][0].is_a?(BigDecimal), "Decimal(9,2) must return BigDecimal"
495
496
  assert_equal 12345.12, vals[0][0], "DECIMAL (decimal)"
496
497
  assert_equal 12345.12, vals[1][0], "DECIMAL (string)"
497
498
  assert_equal -12345.12, vals[2][0], "DECIMAL (string)"
498
499
  elsif cols[i] == 'N154'
499
- connection.execute(sql_insert, 12345.12)
500
- connection.execute(sql_insert, "12345.12")
501
- connection.execute(sql_insert, -12345.12)
500
+ connection.execute(sql_insert, 91520.65)
501
+ connection.execute(sql_insert, "91520.65")
502
+ connection.execute(sql_insert, -91520.65)
502
503
  vals = connection.query(sql_select)
503
- # puts vals.inspect
504
- assert_equal 12345.12, vals[0][0], "NUMERIC (decimal)"
505
- assert_equal 12345.12, vals[1][0], "NUMERIC (string)"
506
- assert_equal -12345.12, vals[2][0], "NUMERIC (string)"
504
+ assert vals[0][0].is_a?(BigDecimal), "Numeric(15,4) must return BigDecimal"
505
+ assert Float(91520.65) != vals[0][0]
506
+ assert_equal BigDecimal('91520.65'), vals[0][0]
507
+ assert_equal BigDecimal('91520.65'), vals[1][0]
508
+ assert_equal BigDecimal('-91520.65'), vals[2][0]
507
509
  end
508
510
  end
509
511
  connection.drop
@@ -5,6 +5,7 @@ require 'DatabaseTestCases'
5
5
  require 'ConnectionTestCases'
6
6
  require 'CursorTestCases'
7
7
  require 'DataTypesTestCases'
8
+ require 'NumericDataTypesTestCases'
8
9
  require 'TransactionTestCases'
9
10
  if RUBY_VERSION =~ /^1.9/
10
11
  require 'EncodingTestCases'
@@ -0,0 +1,591 @@
1
+ require 'bigdecimal'
2
+ require 'test/FbTestCases'
3
+
4
+ class NumericDataTypesTestCases < FbTestCase
5
+ include FbTestCases
6
+
7
+ def setup
8
+ super
9
+ @connection = Database.create(@parms).connect
10
+ end
11
+
12
+ def teardown
13
+ @connection.drop
14
+ end
15
+
16
+ def prepare_test_table(datatype)
17
+ @table = "test#{@id ||= 0 + 1}"
18
+ @connection.execute("create table #{@table} (val #{datatype})")
19
+ end
20
+
21
+ def write_and_read_value(insert_value)
22
+ @connection.execute("insert into #{@table} (val) values (?)", insert_value)
23
+ read_value = @connection.query("select * from #{@table}")[0][0]
24
+ @connection.execute("delete from #{@table}")
25
+ read_value
26
+ end
27
+
28
+ def test_smallint_max
29
+ prepare_test_table("smallint")
30
+ assert_equal 32767, write_and_read_value(32767)
31
+ assert_equal 32767, write_and_read_value("32767")
32
+ assert_equal 32767, write_and_read_value(32767.0)
33
+ assert_equal 32767, write_and_read_value(BigDecimal("32767"))
34
+ assert write_and_read_value(32767).is_a?(Fixnum)
35
+ assert write_and_read_value("32767").is_a?(Fixnum)
36
+ assert write_and_read_value(32767.0).is_a?(Fixnum)
37
+ assert write_and_read_value(BigDecimal("32767")).is_a?(Fixnum)
38
+ end
39
+
40
+ def test_smallint_min
41
+ prepare_test_table("smallint")
42
+ assert_equal -32768, write_and_read_value(-32768)
43
+ assert_equal -32768, write_and_read_value("-32768")
44
+ assert_equal -32768, write_and_read_value(-32768.0)
45
+ assert_equal -32768, write_and_read_value(BigDecimal("-32768"))
46
+ assert write_and_read_value(-32768).is_a?(Fixnum)
47
+ assert write_and_read_value("-32768").is_a?(Fixnum)
48
+ assert write_and_read_value(-32768.0).is_a?(Fixnum)
49
+ assert write_and_read_value(BigDecimal("-32768")).is_a?(Fixnum)
50
+ end
51
+
52
+ def test_smallint_rounding
53
+ prepare_test_table("smallint")
54
+ assert_equal 0, write_and_read_value(0.4)
55
+ assert_equal 0, write_and_read_value("0.4")
56
+ assert_equal 0, write_and_read_value(BigDecimal.new("0.4"))
57
+ assert_equal 0, write_and_read_value(-0.4)
58
+ assert_equal 0, write_and_read_value("-0.4")
59
+ assert_equal 0, write_and_read_value(BigDecimal.new("-0.4"))
60
+ assert_equal 1, write_and_read_value(0.5)
61
+ assert_equal 1, write_and_read_value("0.5")
62
+ assert_equal 1, write_and_read_value(BigDecimal.new("0.5"))
63
+ assert_equal -1, write_and_read_value(-0.5)
64
+ assert_equal -1, write_and_read_value("-0.5")
65
+ assert_equal -1, write_and_read_value(BigDecimal.new("-0.5"))
66
+ end
67
+
68
+ def test_smallint_input_type
69
+ prepare_test_table("smallint")
70
+ #assert_raises(TypeError) { write_and_read_value('abcde') }
71
+ assert_raises(TypeError) { write_and_read_value(Date.new) }
72
+ assert_raises(TypeError) { write_and_read_value(Time.new) }
73
+ assert_raises(TypeError) { write_and_read_value(Object.new) }
74
+ end
75
+
76
+ def test_smallint_input_range
77
+ prepare_test_table("smallint")
78
+ assert_raises(RangeError) { write_and_read_value(32768) }
79
+ assert_raises(RangeError) { write_and_read_value("32768") }
80
+ assert_raises(RangeError) { write_and_read_value(BigDecimal("32768")) }
81
+ assert_raises(RangeError) { write_and_read_value(-32769) }
82
+ assert_raises(RangeError) { write_and_read_value("-32769") }
83
+ assert_raises(RangeError) { write_and_read_value(BigDecimal("-32769")) }
84
+ end
85
+
86
+ def test_integer_max
87
+ prepare_test_table("integer")
88
+ assert_equal 2147483647, write_and_read_value(2147483647)
89
+ assert_equal 2147483647, write_and_read_value("2147483647")
90
+ assert_equal 2147483647, write_and_read_value(2147483647.0)
91
+ assert_equal 2147483647, write_and_read_value(BigDecimal("2147483647"))
92
+ assert write_and_read_value(2147483647).is_a?(Fixnum)
93
+ assert write_and_read_value("2147483647").is_a?(Fixnum)
94
+ assert write_and_read_value(2147483647.0).is_a?(Fixnum)
95
+ assert write_and_read_value(BigDecimal("2147483647")).is_a?(Fixnum)
96
+ end
97
+
98
+ def test_integer_min
99
+ prepare_test_table("integer")
100
+ assert_equal -2147483648, write_and_read_value(-2147483648)
101
+ assert_equal -2147483648, write_and_read_value("-2147483648")
102
+ assert_equal -2147483648, write_and_read_value(-2147483648.0)
103
+ assert_equal -2147483648, write_and_read_value(BigDecimal("-2147483648"))
104
+ assert write_and_read_value(-2147483648).is_a?(Fixnum)
105
+ assert write_and_read_value("-2147483648").is_a?(Fixnum)
106
+ assert write_and_read_value(-2147483648.0).is_a?(Fixnum)
107
+ assert write_and_read_value(BigDecimal("-2147483648")).is_a?(Fixnum)
108
+ end
109
+
110
+ def test_integer_rounding
111
+ prepare_test_table("integer")
112
+ assert_equal 0, write_and_read_value(0.4)
113
+ assert_equal 0, write_and_read_value("0.4")
114
+ assert_equal 0, write_and_read_value(BigDecimal.new("0.4"))
115
+ assert_equal 0, write_and_read_value(-0.4)
116
+ assert_equal 0, write_and_read_value("-0.4")
117
+ assert_equal 0, write_and_read_value(BigDecimal.new("-0.4"))
118
+ assert_equal 1, write_and_read_value(0.5)
119
+ assert_equal 1, write_and_read_value("0.5")
120
+ assert_equal 1, write_and_read_value(BigDecimal.new("0.5"))
121
+ assert_equal -1, write_and_read_value(-0.5)
122
+ assert_equal -1, write_and_read_value("-0.5")
123
+ assert_equal -1, write_and_read_value(BigDecimal.new("-0.5"))
124
+ end
125
+
126
+ def test_integer_input_type
127
+ prepare_test_table("integer")
128
+ #assert_raises(TypeError) { write_and_read_value('abcde') }
129
+ assert_raises(TypeError) { write_and_read_value(Date.new) }
130
+ assert_raises(TypeError) { write_and_read_value(Time.new) }
131
+ assert_raises(TypeError) { write_and_read_value(Object.new) }
132
+ end
133
+
134
+ def test_integer_input_range
135
+ prepare_test_table("integer")
136
+ assert_raises(RangeError) { write_and_read_value(2147483648) }
137
+ assert_raises(RangeError) { write_and_read_value("2147483648") }
138
+ assert_raises(RangeError) { write_and_read_value(BigDecimal("2147483648")) }
139
+ assert_raises(RangeError) { write_and_read_value(-2147483649) }
140
+ assert_raises(RangeError) { write_and_read_value("-2147483649") }
141
+ assert_raises(RangeError) { write_and_read_value(BigDecimal("-2147483649")) }
142
+ end
143
+
144
+ def test_bigint_max
145
+ prepare_test_table("bigint")
146
+ assert_equal 9223372036854775807, write_and_read_value(9223372036854775807)
147
+ assert_equal 9223372036854775807, write_and_read_value("9223372036854775807")
148
+ #assert_equal 9223372036854775807, write_and_read_value(9223372036854775807.0)
149
+ assert_equal 9223372036854775807, write_and_read_value(BigDecimal("9223372036854775807"))
150
+ assert write_and_read_value(9223372036854775807).is_a?(Bignum)
151
+ assert write_and_read_value("9223372036854775807").is_a?(Bignum)
152
+ #assert write_and_read_value(9223372036854775807.0).is_a?(Bignum)
153
+ assert write_and_read_value(BigDecimal("9223372036854775807")).is_a?(Bignum)
154
+ end
155
+
156
+ def test_bigint_min
157
+ prepare_test_table("bigint")
158
+ assert_equal -9223372036854775808, write_and_read_value(-9223372036854775808)
159
+ assert_equal -9223372036854775808, write_and_read_value("-9223372036854775808")
160
+ #assert_equal -9223372036854775808, write_and_read_value(-9223372036854775808.0)
161
+ assert_equal -9223372036854775808, write_and_read_value(BigDecimal("-9223372036854775808"))
162
+ assert write_and_read_value(-9223372036854775808).is_a?(Bignum)
163
+ assert write_and_read_value("-9223372036854775808").is_a?(Bignum)
164
+ #assert write_and_read_value(-9223372036854775808.0).is_a?(Bignum)
165
+ assert write_and_read_value(BigDecimal("-9223372036854775808")).is_a?(Bignum)
166
+ end
167
+
168
+ def test_bigint_rounding
169
+ prepare_test_table("bigint")
170
+ assert_equal 0, write_and_read_value(0.4)
171
+ assert_equal 0, write_and_read_value("0.4")
172
+ assert_equal 0, write_and_read_value(BigDecimal.new("0.4"))
173
+ assert_equal 0, write_and_read_value(-0.4)
174
+ assert_equal 0, write_and_read_value("-0.4")
175
+ assert_equal 0, write_and_read_value(BigDecimal.new("-0.4"))
176
+ assert_equal 1, write_and_read_value(0.5)
177
+ assert_equal 1, write_and_read_value("0.5")
178
+ assert_equal 1, write_and_read_value(BigDecimal.new("0.5"))
179
+ assert_equal -1, write_and_read_value(-0.5)
180
+ assert_equal -1, write_and_read_value("-0.5")
181
+ assert_equal -1, write_and_read_value(BigDecimal.new("-0.5"))
182
+ end
183
+
184
+ def test_bigint_input_type
185
+ prepare_test_table("bigint")
186
+ #assert_raises(TypeError) { write_and_read_value('abcde') }
187
+ assert_raises(TypeError) { write_and_read_value(Date.new) }
188
+ assert_raises(TypeError) { write_and_read_value(Time.new) }
189
+ assert_raises(TypeError) { write_and_read_value(Object.new) }
190
+ end
191
+
192
+ def test_bigint_input_range
193
+ prepare_test_table("bigint")
194
+ assert_raises(RangeError) { write_and_read_value(9223372036854775808) }
195
+ assert_raises(RangeError) { write_and_read_value("9223372036854775808") }
196
+ assert_raises(RangeError) { write_and_read_value(BigDecimal("9223372036854775808")) }
197
+ assert_raises(RangeError) { write_and_read_value(-9223372036854775809) }
198
+ assert_raises(RangeError) { write_and_read_value("-9223372036854775809") }
199
+ assert_raises(RangeError) { write_and_read_value(BigDecimal("-9223372036854775809")) }
200
+ end
201
+
202
+ def test_decimal_4_0_max
203
+ prepare_test_table("decimal(4, 0)")
204
+ assert_equal 32767, write_and_read_value(32767)
205
+ assert_equal 32767, write_and_read_value("32767")
206
+ assert_equal 32767, write_and_read_value(32767.0)
207
+ assert_equal 32767, write_and_read_value(BigDecimal("32767"))
208
+ assert write_and_read_value(32767).is_a?(Fixnum)
209
+ assert write_and_read_value("32767").is_a?(Fixnum)
210
+ assert write_and_read_value(32767.0).is_a?(Fixnum)
211
+ assert write_and_read_value(BigDecimal("32767")).is_a?(Fixnum)
212
+ end
213
+
214
+ def test_decimal_4_0_min
215
+ prepare_test_table("decimal(4, 0)")
216
+ assert_equal -32768, write_and_read_value(-32768)
217
+ assert_equal -32768, write_and_read_value("-32768")
218
+ assert_equal -32768, write_and_read_value(-32768.0)
219
+ assert_equal -32768, write_and_read_value(BigDecimal("-32768"))
220
+ assert write_and_read_value(-32768).is_a?(Fixnum)
221
+ assert write_and_read_value("-32768").is_a?(Fixnum)
222
+ assert write_and_read_value(-32768.0).is_a?(Fixnum)
223
+ assert write_and_read_value(BigDecimal("-32768")).is_a?(Fixnum)
224
+ end
225
+
226
+ def test_decimal_4_0_rounding
227
+ prepare_test_table("decimal(4, 0)")
228
+ assert_equal 0, write_and_read_value(0.4)
229
+ assert_equal 0, write_and_read_value("0.4")
230
+ assert_equal 0, write_and_read_value(BigDecimal.new("0.4"))
231
+ assert_equal 0, write_and_read_value(-0.4)
232
+ assert_equal 0, write_and_read_value("-0.4")
233
+ assert_equal 0, write_and_read_value(BigDecimal.new("-0.4"))
234
+ assert_equal 1, write_and_read_value(0.5)
235
+ assert_equal 1, write_and_read_value("0.5")
236
+ assert_equal 1, write_and_read_value(BigDecimal.new("0.5"))
237
+ assert_equal -1, write_and_read_value(-0.5)
238
+ assert_equal -1, write_and_read_value("-0.5")
239
+ assert_equal -1, write_and_read_value(BigDecimal.new("-0.5"))
240
+ end
241
+
242
+ def test_decimal_4_0_input_type
243
+ prepare_test_table("decimal(4, 0)")
244
+ #assert_raises(TypeError) { write_and_read_value('abcde') }
245
+ assert_raises(TypeError) { write_and_read_value(Date.new) }
246
+ assert_raises(TypeError) { write_and_read_value(Time.new) }
247
+ assert_raises(TypeError) { write_and_read_value(Object.new) }
248
+ end
249
+
250
+ def test_decimal_4_0_input_range
251
+ prepare_test_table("decimal(4, 0)")
252
+ assert_raises(RangeError) { write_and_read_value(2147483648) }
253
+ assert_raises(RangeError) { write_and_read_value("2147483648") }
254
+ assert_raises(RangeError) { write_and_read_value(BigDecimal("2147483648")) }
255
+ assert_raises(RangeError) { write_and_read_value(-2147483649) }
256
+ assert_raises(RangeError) { write_and_read_value("-2147483649") }
257
+ assert_raises(RangeError) { write_and_read_value(BigDecimal("-2147483649")) }
258
+ end
259
+
260
+ def test_decimal_9_0_max
261
+ prepare_test_table("decimal(9, 0)")
262
+ assert_equal 2147483647, write_and_read_value(2147483647)
263
+ assert_equal 2147483647, write_and_read_value("2147483647")
264
+ assert_equal 2147483647, write_and_read_value(2147483647.0)
265
+ assert_equal 2147483647, write_and_read_value(BigDecimal("2147483647"))
266
+ assert write_and_read_value(2147483647).is_a?(Fixnum)
267
+ assert write_and_read_value("2147483647").is_a?(Fixnum)
268
+ assert write_and_read_value(2147483647.0).is_a?(Fixnum)
269
+ assert write_and_read_value(BigDecimal("2147483647")).is_a?(Fixnum)
270
+ end
271
+
272
+ def test_decimal_9_0_min
273
+ prepare_test_table("decimal(9, 0)")
274
+ assert_equal 2147483647, write_and_read_value(2147483647)
275
+ assert_equal 2147483647, write_and_read_value("2147483647")
276
+ assert_equal 2147483647, write_and_read_value(2147483647.0)
277
+ assert_equal 2147483647, write_and_read_value(BigDecimal("2147483647"))
278
+ assert write_and_read_value(2147483647).is_a?(Fixnum)
279
+ assert write_and_read_value("2147483647").is_a?(Fixnum)
280
+ assert write_and_read_value(2147483647.0).is_a?(Fixnum)
281
+ assert write_and_read_value(BigDecimal("2147483647")).is_a?(Fixnum)
282
+ end
283
+
284
+ def test_decimal_9_0_rounding
285
+ prepare_test_table("decimal(9, 0)")
286
+ assert_equal 0, write_and_read_value(0.4)
287
+ assert_equal 0, write_and_read_value("0.4")
288
+ assert_equal 0, write_and_read_value(BigDecimal.new("0.4"))
289
+ assert_equal 0, write_and_read_value(-0.4)
290
+ assert_equal 0, write_and_read_value("-0.4")
291
+ assert_equal 0, write_and_read_value(BigDecimal.new("-0.4"))
292
+ assert_equal 1, write_and_read_value(0.5)
293
+ assert_equal 1, write_and_read_value("0.5")
294
+ assert_equal 1, write_and_read_value(BigDecimal.new("0.5"))
295
+ assert_equal -1, write_and_read_value(-0.5)
296
+ assert_equal -1, write_and_read_value("-0.5")
297
+ assert_equal -1, write_and_read_value(BigDecimal.new("-0.5"))
298
+ end
299
+
300
+ def test_decimal_9_0_input_type
301
+ prepare_test_table("decimal(9, 0)")
302
+ #assert_raises(TypeError) { write_and_read_value('abcde') }
303
+ assert_raises(TypeError) { write_and_read_value(Date.new) }
304
+ assert_raises(TypeError) { write_and_read_value(Time.new) }
305
+ assert_raises(TypeError) { write_and_read_value(Object.new) }
306
+ end
307
+
308
+ def test_decimal_9_0_input_range
309
+ prepare_test_table("decimal(9, 0)")
310
+ assert_raises(RangeError) { write_and_read_value(2147483648) }
311
+ assert_raises(RangeError) { write_and_read_value("2147483648") }
312
+ assert_raises(RangeError) { write_and_read_value(BigDecimal("2147483648")) }
313
+ assert_raises(RangeError) { write_and_read_value(-2147483649) }
314
+ assert_raises(RangeError) { write_and_read_value("-2147483649") }
315
+ assert_raises(RangeError) { write_and_read_value(BigDecimal("-2147483649")) }
316
+ end
317
+
318
+ def test_decimal_9_4_max
319
+ prepare_test_table("decimal(9, 4)")
320
+ assert_equal BigDecimal("214748.3647"), write_and_read_value("214748.3647")
321
+ assert_equal BigDecimal("214748.3647"), write_and_read_value(214748.3647)
322
+ assert_equal BigDecimal("214748.3647"), write_and_read_value(BigDecimal("214748.3647"))
323
+ assert write_and_read_value("214748.3647").is_a?(BigDecimal)
324
+ assert write_and_read_value(214748.3647).is_a?(BigDecimal)
325
+ assert write_and_read_value(BigDecimal("214748.3647")).is_a?(BigDecimal)
326
+ end
327
+
328
+ def test_decimal_9_4_min
329
+ prepare_test_table("decimal(9, 4)")
330
+ assert_equal BigDecimal("-214748.3648"), write_and_read_value("-214748.3648")
331
+ assert_equal BigDecimal("-214748.3648"), write_and_read_value(-214748.3648)
332
+ assert_equal BigDecimal("-214748.3648"), write_and_read_value(BigDecimal("-214748.3648"))
333
+ assert write_and_read_value("-214748.3648").is_a?(BigDecimal)
334
+ assert write_and_read_value(-214748.3648).is_a?(BigDecimal)
335
+ assert write_and_read_value(BigDecimal("-214748.3648")).is_a?(BigDecimal)
336
+ end
337
+
338
+ def test_decimal_9_4_rounding
339
+ prepare_test_table("decimal(9, 4)")
340
+ assert_equal 0, write_and_read_value(0.00004)
341
+ assert_equal 0, write_and_read_value("0.00004")
342
+ assert_equal 0, write_and_read_value(BigDecimal.new("0.00004"))
343
+ assert_equal 0, write_and_read_value(-0.00004)
344
+ assert_equal 0, write_and_read_value("-0.00004")
345
+ assert_equal 0, write_and_read_value(BigDecimal.new("-0.00004"))
346
+ assert_equal BigDecimal('0.0001'), write_and_read_value(0.00005)
347
+ assert_equal BigDecimal('0.0001'), write_and_read_value("0.00005")
348
+ assert_equal BigDecimal('0.0001'), write_and_read_value(BigDecimal.new("0.00005"))
349
+ assert_equal BigDecimal('-0.0001'), write_and_read_value(-0.00005)
350
+ assert_equal BigDecimal('-0.0001'), write_and_read_value("-0.00005")
351
+ assert_equal BigDecimal('-0.0001'), write_and_read_value(BigDecimal.new("-0.00005"))
352
+ end
353
+
354
+ def test_decimal_9_4_input_type
355
+ prepare_test_table("decimal(9, 4)")
356
+ #assert_raises(TypeError) { write_and_read_value('abcde') }
357
+ assert_raises(TypeError) { write_and_read_value(Date.new) }
358
+ assert_raises(TypeError) { write_and_read_value(Time.new) }
359
+ assert_raises(TypeError) { write_and_read_value(Object.new) }
360
+ end
361
+
362
+ def test_decimal_9_4_input_range
363
+ prepare_test_table("decimal(9, 4)")
364
+ assert_raises(RangeError) { write_and_read_value(214748.3648) }
365
+ assert_raises(RangeError) { write_and_read_value("214748.3648") }
366
+ assert_raises(RangeError) { write_and_read_value(BigDecimal("214748.3648")) }
367
+ assert_raises(RangeError) { write_and_read_value(-214748.3649) }
368
+ assert_raises(RangeError) { write_and_read_value("-214748.3649") }
369
+ assert_raises(RangeError) { write_and_read_value(BigDecimal("-214748.3649")) }
370
+ end
371
+
372
+ def test_decimal_9_9_max
373
+ prepare_test_table("decimal(9, 9)")
374
+ assert_equal BigDecimal("2.147483647"), write_and_read_value("2.147483647")
375
+ assert_equal BigDecimal("2.147483647"), write_and_read_value(2.147483647)
376
+ assert_equal BigDecimal("2.147483647"), write_and_read_value(BigDecimal("2.147483647"))
377
+ assert write_and_read_value("2.147483647").is_a?(BigDecimal)
378
+ assert write_and_read_value(2.147483647).is_a?(BigDecimal)
379
+ assert write_and_read_value(BigDecimal("2.147483647")).is_a?(BigDecimal)
380
+ end
381
+
382
+ def test_decimal_9_9_min
383
+ prepare_test_table("decimal(9, 9)")
384
+ assert_equal BigDecimal("-2.147483648"), write_and_read_value("-2.147483648")
385
+ assert_equal BigDecimal("-2.147483648"), write_and_read_value(-2.147483648)
386
+ assert_equal BigDecimal("-2.147483648"), write_and_read_value(BigDecimal("-2.147483648"))
387
+ assert write_and_read_value("-2.147483648").is_a?(BigDecimal)
388
+ assert write_and_read_value(-2.147483648).is_a?(BigDecimal)
389
+ assert write_and_read_value(BigDecimal("-2.147483648")).is_a?(BigDecimal)
390
+ end
391
+
392
+ def test_decimal_9_9_rounding
393
+ prepare_test_table("decimal(9, 9)")
394
+ assert_equal 0, write_and_read_value(0.0000000004)
395
+ assert_equal 0, write_and_read_value("0.0000000004")
396
+ assert_equal 0, write_and_read_value(BigDecimal.new("0.0000000004"))
397
+ assert_equal 0, write_and_read_value(-0.0000000004)
398
+ assert_equal 0, write_and_read_value("-0.0000000004")
399
+ assert_equal 0, write_and_read_value(BigDecimal.new("-0.0000000004"))
400
+ assert_equal BigDecimal("0.000000001"), write_and_read_value(0.0000000005)
401
+ assert_equal BigDecimal("0.000000001"), write_and_read_value("0.0000000005")
402
+ assert_equal BigDecimal("0.000000001"), write_and_read_value(BigDecimal.new("0.0000000005"))
403
+ assert_equal BigDecimal("-0.000000001"), write_and_read_value(-0.0000000005)
404
+ assert_equal BigDecimal("-0.000000001"), write_and_read_value("-0.0000000005")
405
+ assert_equal BigDecimal("-0.000000001"), write_and_read_value(BigDecimal.new("-0.0000000005"))
406
+ end
407
+
408
+ def test_decimal_9_9_input_type
409
+ prepare_test_table("decimal(9, 9)")
410
+ #assert_raises(TypeError) { write_and_read_value('abcde') }
411
+ assert_raises(TypeError) { write_and_read_value(Date.new) }
412
+ assert_raises(TypeError) { write_and_read_value(Time.new) }
413
+ assert_raises(TypeError) { write_and_read_value(Object.new) }
414
+ end
415
+
416
+ def test_decimal_9_9_input_range
417
+ prepare_test_table("decimal(9, 9)")
418
+ assert_raises(RangeError) { write_and_read_value(2.147483648) }
419
+ assert_raises(RangeError) { write_and_read_value("2.147483648") }
420
+ assert_raises(RangeError) { write_and_read_value(BigDecimal("2.147483648")) }
421
+ assert_raises(RangeError) { write_and_read_value(-2.147483649) }
422
+ assert_raises(RangeError) { write_and_read_value("-2.147483649") }
423
+ assert_raises(RangeError) { write_and_read_value(BigDecimal("-2.147483649")) }
424
+ end
425
+
426
+ def test_decimal_18_0_max
427
+ prepare_test_table("decimal(18, 0)")
428
+ assert_equal 9223372036854775807, write_and_read_value(9223372036854775807)
429
+ assert_equal 9223372036854775807, write_and_read_value("9223372036854775807")
430
+ #assert_equal 9223372036854775807, write_and_read_value(9223372036854775807.0)
431
+ assert_equal 9223372036854775807, write_and_read_value(BigDecimal("9223372036854775807"))
432
+ assert write_and_read_value(9223372036854775807).is_a?(Bignum)
433
+ assert write_and_read_value("9223372036854775807").is_a?(Bignum)
434
+ #assert write_and_read_value(9223372036854775807.0).is_a?(Bignum)
435
+ assert write_and_read_value(BigDecimal("9223372036854775807")).is_a?(Bignum)
436
+ end
437
+
438
+ def test_decimal_18_0_min
439
+ prepare_test_table("decimal(18, 0)")
440
+ assert_equal -9223372036854775808, write_and_read_value(-9223372036854775808)
441
+ assert_equal -9223372036854775808, write_and_read_value("-9223372036854775808")
442
+ #assert_equal -9223372036854775808, write_and_read_value(-9223372036854775808.0)
443
+ assert_equal -9223372036854775808, write_and_read_value(BigDecimal("-9223372036854775808"))
444
+ assert write_and_read_value(-9223372036854775808).is_a?(Bignum)
445
+ assert write_and_read_value("-9223372036854775808").is_a?(Bignum)
446
+ #assert write_and_read_value(-9223372036854775808.0).is_a?(Bignum)
447
+ assert write_and_read_value(BigDecimal("-9223372036854775808")).is_a?(Bignum)
448
+ end
449
+
450
+ def test_decimal_18_0_rounding
451
+ prepare_test_table("decimal(18, 0)")
452
+ assert_equal 0, write_and_read_value(0.4)
453
+ assert_equal 0, write_and_read_value("0.4")
454
+ assert_equal 0, write_and_read_value(BigDecimal.new("0.4"))
455
+ assert_equal 0, write_and_read_value(-0.4)
456
+ assert_equal 0, write_and_read_value("-0.4")
457
+ assert_equal 0, write_and_read_value(BigDecimal.new("-0.4"))
458
+ assert_equal 1, write_and_read_value(0.5)
459
+ assert_equal 1, write_and_read_value("0.5")
460
+ assert_equal 1, write_and_read_value(BigDecimal.new("0.5"))
461
+ assert_equal -1, write_and_read_value(-0.5)
462
+ assert_equal -1, write_and_read_value("-0.5")
463
+ assert_equal -1, write_and_read_value(BigDecimal.new("-0.5"))
464
+ end
465
+
466
+ def test_decimal_18_0_input_types
467
+ prepare_test_table("decimal(18, 0)")
468
+ #assert_raises(TypeError) { write_and_read_value('abcde') }
469
+ assert_raises(TypeError) { write_and_read_value(Date.new) }
470
+ assert_raises(TypeError) { write_and_read_value(Time.new) }
471
+ assert_raises(TypeError) { write_and_read_value(Object.new) }
472
+ end
473
+
474
+ def test_decimal_18_0_input_range
475
+ prepare_test_table("decimal(18, 0)")
476
+ assert_raises(RangeError) { write_and_read_value(9223372036854775808) }
477
+ assert_raises(RangeError) { write_and_read_value("9223372036854775808") }
478
+ assert_raises(RangeError) { write_and_read_value(BigDecimal("9223372036854775808")) }
479
+ assert_raises(RangeError) { write_and_read_value(-9223372036854775809) }
480
+ assert_raises(RangeError) { write_and_read_value("-9223372036854775809") }
481
+ assert_raises(RangeError) { write_and_read_value(BigDecimal("-9223372036854775809")) }
482
+ end
483
+
484
+ def test_decimal_18_9_max
485
+ prepare_test_table("decimal(18, 9)")
486
+ assert_equal BigDecimal("9223372036.854775807"), write_and_read_value("9223372036.854775807")
487
+ #assert_equal BigDecimal("9223372036.854775807"), write_and_read_value(9223372036.854775807)
488
+ assert_equal BigDecimal("9223372036.854775807"), write_and_read_value(BigDecimal("9223372036.854775807"))
489
+ assert write_and_read_value("9223372036.854775807").is_a?(BigDecimal)
490
+ #assert write_and_read_value(9223372036.854775807).is_a?(BigDecimal)
491
+ assert write_and_read_value(BigDecimal("9223372036.854775807")).is_a?(BigDecimal)
492
+ end
493
+
494
+ def test_decimal_18_9_min
495
+ prepare_test_table("decimal(18, 9)")
496
+ assert_equal BigDecimal("-9223372036.854775808"), write_and_read_value("-9223372036.854775808")
497
+ #assert_equal BigDecimal("-9223372036.854775808"), write_and_read_value(-9223372036.854775808)
498
+ assert_equal BigDecimal("-9223372036.854775808"), write_and_read_value(BigDecimal("-9223372036.854775808"))
499
+ assert write_and_read_value("-9223372036.854775808").is_a?(BigDecimal)
500
+ #assert write_and_read_value(-9223372036.854775808).is_a?(BigDecimal)
501
+ assert write_and_read_value(BigDecimal("-9223372036.854775808")).is_a?(BigDecimal)
502
+ end
503
+
504
+ def test_decimal_18_9_rounding
505
+ prepare_test_table("decimal(18, 9)")
506
+ assert_equal 0, write_and_read_value(0.0000000004)
507
+ assert_equal 0, write_and_read_value("0.0000000004")
508
+ assert_equal 0, write_and_read_value(BigDecimal.new("0.0000000004"))
509
+ assert_equal 0, write_and_read_value(-0.0000000004)
510
+ assert_equal 0, write_and_read_value("-0.0000000004")
511
+ assert_equal 0, write_and_read_value(BigDecimal.new("-0.0000000004"))
512
+ assert_equal BigDecimal('0.000000001'), write_and_read_value(0.0000000005)
513
+ assert_equal BigDecimal('0.000000001'), write_and_read_value("0.0000000005")
514
+ assert_equal BigDecimal('0.000000001'), write_and_read_value(BigDecimal.new("0.0000000005"))
515
+ assert_equal BigDecimal('-0.000000001'), write_and_read_value(-0.0000000005)
516
+ assert_equal BigDecimal('-0.000000001'), write_and_read_value("-0.0000000005")
517
+ assert_equal BigDecimal('-0.000000001'), write_and_read_value(BigDecimal.new("-0.0000000005"))
518
+ end
519
+
520
+ def test_decimal_18_9_input_type
521
+ prepare_test_table("decimal(18, 9)")
522
+ #assert_raises(TypeError) { write_and_read_value('abcde') }
523
+ assert_raises(TypeError) { write_and_read_value(Date.new) }
524
+ assert_raises(TypeError) { write_and_read_value(Time.new) }
525
+ assert_raises(TypeError) { write_and_read_value(Object.new) }
526
+ end
527
+
528
+ def test_decimal_18_9_input_range
529
+ prepare_test_table("decimal(18, 9)")
530
+ assert_raises(RangeError) { write_and_read_value(9223372036.854775808) }
531
+ assert_raises(RangeError) { write_and_read_value("9223372036.854775808") }
532
+ assert_raises(RangeError) { write_and_read_value(BigDecimal("9223372036.854775808")) }
533
+ assert_raises(RangeError) { write_and_read_value(-9223372036.854775809) }
534
+ assert_raises(RangeError) { write_and_read_value("-9223372036.854775809") }
535
+ assert_raises(RangeError) { write_and_read_value(BigDecimal("-9223372036.854775809")) }
536
+ end
537
+
538
+ def test_decimal_18_18_max
539
+ prepare_test_table("decimal(18, 18)")
540
+ assert_equal BigDecimal("9.223372036854775807"), write_and_read_value(BigDecimal("9.223372036854775807"))
541
+ #assert_equal BigDecimal("9.223372036854775807"), write_and_read_value(9.223372036854775807)
542
+ assert_equal BigDecimal("9.223372036854775807"), write_and_read_value("9.223372036854775807")
543
+ assert write_and_read_value("9.223372036854775807").is_a?(BigDecimal)
544
+ #assert write_and_read_value(9.223372036854775807).is_a?(BigDecimal)
545
+ assert write_and_read_value(BigDecimal("9.223372036854775807")).is_a?(BigDecimal)
546
+ end
547
+
548
+ def test_decimal_18_18_min
549
+ prepare_test_table("decimal(18, 18)")
550
+ assert_equal BigDecimal("-9.223372036854775808"), write_and_read_value("-9.223372036854775808")
551
+ #assert_equal BigDecimal("-9.223372036854775808"), write_and_read_value(-9.223372036854775808)
552
+ assert_equal BigDecimal("-9.223372036854775808"), write_and_read_value(BigDecimal("-9.223372036854775808"))
553
+ assert write_and_read_value("-9.223372036854775808").is_a?(BigDecimal)
554
+ #assert write_and_read_value(-9.223372036854775808).is_a?(BigDecimal)
555
+ assert write_and_read_value(BigDecimal("-9.223372036854775808")).is_a?(BigDecimal)
556
+ end
557
+
558
+ def test_decimal_18_18_rounding
559
+ prepare_test_table("decimal(18, 18)")
560
+ assert_equal 0, write_and_read_value(0.0000000000000000004)
561
+ assert_equal 0, write_and_read_value("0.0000000000000000004")
562
+ assert_equal 0, write_and_read_value(BigDecimal.new("0.0000000000000000004"))
563
+ assert_equal 0, write_and_read_value(-0.0000000000000000004)
564
+ assert_equal 0, write_and_read_value("-0.0000000000000000004")
565
+ assert_equal 0, write_and_read_value(BigDecimal.new("-0.0000000000000000004"))
566
+ assert_equal BigDecimal("0.000000000000000001"), write_and_read_value(0.0000000000000000005)
567
+ assert_equal BigDecimal("0.000000000000000001"), write_and_read_value("0.0000000000000000005")
568
+ assert_equal BigDecimal("0.000000000000000001"), write_and_read_value(BigDecimal.new("0.0000000000000000005"))
569
+ assert_equal BigDecimal("-0.000000000000000001"), write_and_read_value(-0.0000000000000000005)
570
+ assert_equal BigDecimal("-0.000000000000000001"), write_and_read_value("-0.0000000000000000005")
571
+ assert_equal BigDecimal("-0.000000000000000001"), write_and_read_value(BigDecimal.new("-0.0000000000000000005"))
572
+ end
573
+
574
+ def test_decimal_18_18_input_type
575
+ prepare_test_table("decimal(18, 18)")
576
+ #assert_raises(TypeError) { write_and_read_value('abcde') }
577
+ assert_raises(TypeError) { write_and_read_value(Date.new) }
578
+ assert_raises(TypeError) { write_and_read_value(Time.new) }
579
+ assert_raises(TypeError) { write_and_read_value(Object.new) }
580
+ end
581
+
582
+ def test_decimal_18_18_input_range
583
+ prepare_test_table("decimal(18, 18)")
584
+ assert_raises(RangeError) { write_and_read_value(9.223372036854775808) }
585
+ assert_raises(RangeError) { write_and_read_value("9.223372036854775808") }
586
+ assert_raises(RangeError) { write_and_read_value(BigDecimal("9.223372036854775808")) }
587
+ assert_raises(RangeError) { write_and_read_value(-9.223372036854775809) }
588
+ assert_raises(RangeError) { write_and_read_value("-9.223372036854775809") }
589
+ assert_raises(RangeError) { write_and_read_value(BigDecimal("-9.223372036854775809")) }
590
+ end
591
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.4
4
+ version: 0.7.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brent Rowland
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-22 00:00:00.000000000 Z
11
+ date: 2016-04-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby Firebird Extension Library
14
14
  email: rowland@rowlandresearch.com
@@ -29,6 +29,7 @@ files:
29
29
  - test/EncodingTestCases.rb
30
30
  - test/FbTestCases.rb
31
31
  - test/FbTestSuite.rb
32
+ - test/NumericDataTypesTestCases.rb
32
33
  - test/TransactionTestCases.rb
33
34
  homepage: http://github.com/rowland/fb
34
35
  licenses:
@@ -57,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
58
  requirements:
58
59
  - Firebird client library fbclient.dll, libfbclient.so or Firebird.framework.
59
60
  rubyforge_project:
60
- rubygems_version: 2.2.0
61
+ rubygems_version: 2.2.5
61
62
  signing_key:
62
63
  specification_version: 4
63
64
  summary: Firebird database driver