Class: Advantage::a_ads_bind_param

Inherits:
Object
  • Object
show all
Defined in:
ext/advantage.c

Instance Method Summary collapse

Instance Method Details

#finish(VALUEbind) ⇒ nil

Frees the resources associated with the bound parameter.

<b>Parameters</b>:
- <tt>VALUE bind</tt> -- Bound parameter retrieved from ads_describe_bind_param().

<b>Returns</b>:
- <tt>nil</tt>.

Returns:

  • (nil)


1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
# File 'ext/advantage.c', line 1668

static VALUE
static_Bind_finish(VALUE bind)
{
    a_ads_bind_param* s_bind;

    Data_Get_Struct(bind, a_ads_bind_param, s_bind);

    // FIXME: use Ruby's allocation routines?
    if (s_bind) { free(s_bind);};

    return (Qnil);
}

#get_direction(VALUEbind) ⇒ Object

Gets the direction of the bound parameter.

<b>Parameters</b>:
- <tt>VALUE bind</tt> -- Bound parameter retrieved from ads_describe_bind_param().

<b>Returns</b>:
- <tt>VALUE name</tt>: The direction of the bound parameter.


1646
1647
1648
1649
1650
1651
1652
1653
# File 'ext/advantage.c', line 1646

static VALUE
static_Bind_get_direction(VALUE bind)
{
    a_ads_bind_param* s_bind;
    Data_Get_Struct(bind, a_ads_bind_param, s_bind);

    return (CHR2FIX(s_bind->direction));
}

#get_name(VALUEbind) ⇒ Object

Gets the name of the bound parameter.

<b>Parameters</b>:
- <tt>VALUE bind</tt> -- Bound parameter retrieved from ads_describe_bind_param().

<b>Returns</b>:
- <tt>VALUE name</tt>: The bound variable's name.


1434
1435
1436
1437
1438
1439
1440
# File 'ext/advantage.c', line 1434

static VALUE
static_Bind_get_name(VALUE bind)
{
    a_ads_bind_param* s_bind;
    Data_Get_Struct(bind, a_ads_bind_param, s_bind);
    return (rb_str_new2(s_bind->name));
}

#set_buffer_size(VALUEbind, VALUEsize) ⇒ nil

Sets the buffer size of INOUT and OUT parameters for string and binary

data.

<b>Parameters</b>:
- <tt>VALUE bind</tt> -- Bound parameter retrieved from ads_describe_bind_param().
- <tt>VALUE size</tt> -- The size of the buffer to hold the INOUT or OUT parameter.

<b>Returns</b>:
- <tt>nil</tt>.

Returns:

  • (nil)


1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
# File 'ext/advantage.c', line 1621

static VALUE
static_Bind_set_buffer_size(VALUE bind, VALUE size)
{
    a_ads_bind_param* s_bind;

    Data_Get_Struct(bind, a_ads_bind_param, s_bind);

    s_bind->value.buffer_size = NUM2INT(size);

    return (Qnil);
}

#set_direction(VALUEbind, VALUEdirection) ⇒ nil

Sets the direction of the bound parameter before binding.

<b>Parameters</b>:
- <tt>VALUE bind</tt> -- Bound parameter retrieved from ads_describe_bind_param().
- <tt>VALUE direction</tt> -- The direction of the binding variable.

<b>Returns</b>:
- <tt>nil</tt>.

Returns:

  • (nil)


1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
# File 'ext/advantage.c', line 1594

static VALUE
static_Bind_set_direction(VALUE bind, VALUE direction)
{
    a_ads_bind_param* s_bind;

    Data_Get_Struct(bind, a_ads_bind_param, s_bind);

    s_bind->direction = NUM2CHR(direction);

    return (Qnil);
}

#set_value(VALUEbind, VALUEval) ⇒ nil

Sets the value of a bind parameter.

This function is used to bind a Ruby VALUE to a given parameter in a
prepared statement. If the bind_direction is INPUT only, the type INPUT
will be bound based on the RUBY type:

  +-------------+------------+
  | Ruby Type   |  Bind Type |
  +-------------+------------+
  | T_STRING   =>  A_STRING  |
  | T_FIXNUM   =>  A_VAL_32  |
  | T_BIGNUM   =>  A_DOUBLE  |
  | T_FLOAT    =>  A_DOUBLE  |
  | T_STRING   =>  A_STRING  |
  | T_NIL      => isnull = 1 |
  +--------------------------+

If the bind direction is OUTPUT, instead use the DBCAPI type to set the
size of the buffer.

In the case of INOUT parameters, it is the application's job to call this
method twice, once to bind the INPUT, and once to bind the OUTPUT.

<b>Parameters</b>:
- <tt>VALUE bind</tt> -- Bound parameter retrieved from ads_describe_bind_param().
- <tt>VALUE val</tt> -- The value to bind into the bound variable.

<b>Returns</b>:
- <tt>nil</tt>.

Returns:

  • (nil)


1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
# File 'ext/advantage.c', line 1476

static VALUE
static_Bind_set_value(VALUE bind, VALUE val)
{
    a_ads_bind_param* s_bind;
    int length;

    Data_Get_Struct(bind, a_ads_bind_param, s_bind);

    // FIXME: use Ruby's allocation routines?
    s_bind->value.is_null = malloc(sizeof(UNSIGNED32));
    *s_bind->value.is_null = 0;

    if (s_bind->direction == DD_INPUT)
    {
       switch(TYPE(val))
       {
    case T_STRING:
       s_bind->value.length  = malloc(sizeof(size_t));
       length = RSTRING_LEN(val);
       *s_bind->value.length = length;
       s_bind->value.buffer = malloc(length);
       memcpy(s_bind->value.buffer, RSTRING_PTR(val), length);
       s_bind->value.type = A_STRING;
       break;
    case T_FIXNUM:
       if(sizeof(void*) == 4){ //32-bit
           s_bind->value.length  = malloc(sizeof(size_t));
           s_bind->value.buffer = malloc(sizeof(int));
           *((int*)s_bind->value.buffer) = FIX2INT(val);
           s_bind->value.type = A_VAL32;
           length = 1;
           *s_bind->value.length = length;
       }else{ //64-bit
            s_bind->value.length  = malloc(sizeof(size_t));
            s_bind->value.buffer = malloc(sizeof(long));
            *((long*)s_bind->value.buffer) = FIX2LONG(val);
            s_bind->value.type = A_VAL64;
            length = 1;
            *s_bind->value.length = length;
       }
       break;
    case T_BIGNUM:
       s_bind->value.length  = malloc(sizeof(size_t));
       s_bind->value.buffer = malloc(sizeof(LONG_LONG));
       *((LONG_LONG*)s_bind->value.buffer) = rb_num2ll(val);
       s_bind->value.type = A_VAL64;
       length = 1;
       *s_bind->value.length = length;
       break;
    case T_FLOAT:
       s_bind->value.length  = malloc(sizeof(size_t));
       s_bind->value.buffer = malloc(sizeof(double));
       *((double*)s_bind->value.buffer) = NUM2DBL(val);
       s_bind->value.type = A_DOUBLE;
       length = 1;
       *s_bind->value.length = length;
       break;
    case T_NIL:
       s_bind->value.length  = malloc(sizeof(size_t));
       *s_bind->value.is_null = 1;
       s_bind->value.buffer = malloc(sizeof(int));
       s_bind->value.type = A_VAL32;
       length = 1;
       *s_bind->value.length = length;
       break;
    default:
       rb_raise(rb_eTypeError, "Cannot convert type. Must be STRING, FIXNUM, BIGNUM, FLOAT, or NIL");
       break;
       }
    }
    else
    {
      switch (s_bind->value.type)
      {
    case A_STRING:
       s_bind->value.buffer = malloc(s_bind->value.buffer_size);
       break;
    case A_DOUBLE:
       s_bind->value.buffer = malloc(sizeof(float));
       break;
       case A_VAL64:
       case A_UVAL64:
       s_bind->value.buffer = malloc(sizeof(LONG_LONG));
       break;
         case A_VAL32:
         case A_UVAL32:
       s_bind->value.buffer = malloc(sizeof(int));
       break;
    case A_VAL16:
    case A_UVAL16:
       s_bind->value.buffer = malloc(sizeof(short));
       break;
    case A_VAL8:
    case A_UVAL8:
          s_bind->value.buffer = malloc(sizeof(char));
       break;
    default:
       rb_raise(rb_eTypeError, "Type unknown");
       break;
      }
    }
    return (Qnil);
}