Reputation: 63
I am using DataStax Cassandra driver I am trying to insert progrmatically rows into cassandra table. One of the columns if the table is of decimal type. My query is
insert into thetable (id, price) values (?,?)
I prepare the statement cass_session_prepare, and then i bind cass_statement_bind_int32_by_name the id.
The issue starts when i try to bind decimal. The prototype of the method is
CassError cass_statement_bind_decimal ( statement, index, varint, varint_size, scale )
where varint is pointer to bytes, varint_size number of bytes, and scale is scale.
So, finally my question is how to calculate the content of the byte array i am supposed to pass to the method? First i naively thought that if I pass just a character representation of number ( "1234.56") it would work. But of course i doesn't work. Now I understand that byte array I supposed to pass is something what is called BigInteger in Java. But how to convert string or double to this BigInteger array I don't know. I don't think Cassandra driver API provides anything that can help me with this conversion. So, if somebody can help me in resolving this issue I would be very grateful
Upvotes: 1
Views: 281
Reputation: 406
There are many libraries out there you can utilize to perform the conversion:
If you look into the integration tests for the DataStax C/C++ driver, you will find a rudimentary BigNumber implementation that utilizes the OpenSSL BIGNUM library. This implementation does not perform arithmetic operations, but will allow you to easily convert the byte array (with scale) into a BigNumber for comparison and display.
NOTE: The OpenSSL BIGNUM implementation is known to be slower than many of the other libraries. A more complete implementation can be found in the DataStax PHP driver GitHub repository which uses the GMP library.
Upvotes: 2