/* * Tests saving a value of type SQLDOUBLE and then retrieving it back. */ #include #include #include "common.h" int main(int argc, char** argv) { SQLRETURN rc; HSTMT hstmt = SQL_NULL_HSTMT; SQLCHAR *sql; //Raw byte data for a double of value 2.0990256436450165 unsigned char double_data[sizeof(SQLDOUBLE)] = { 0x96, 0x5d, 0xe7, 0xf4, 0xcd, 0xca, 0x00, 0x40 }; SQLDOUBLE double_get = 0.0; //Raw byte data for a float of value 1.90480649 unsigned char float_data[sizeof(SQLREAL)] = { 0xb3, 0xd0, 0xf3, 0x3f }; SQLREAL float_get = 0.0f; test_connect(); rc = SQLAllocHandle(SQL_HANDLE_STMT, conn, &hstmt); if (!SQL_SUCCEEDED(rc)) { print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn); exit(1); } sql = "DROP TABLE IF EXISTS float_save"; rc = SQLExecDirect(hstmt, sql, SQL_NTS); CHECK_STMT_RESULT(rc, "SQLExecDirect failed while dropping previous table", hstmt); sql = "CREATE TABLE float_save (mydouble double precision, myfloat float4)"; rc = SQLExecDirect(hstmt, sql, SQL_NTS); CHECK_STMT_RESULT(rc, "SQLExecDirect failed while creating table", hstmt); sql = "INSERT INTO float_save values(?, ?)"; rc = SQLPrepare(hstmt, sql, SQL_NTS); CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt); rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, 0, 0, double_data, sizeof(SQLDOUBLE), NULL); CHECK_STMT_RESULT(rc, "SQLBindParam (SQLDOUBLE) failed", hstmt); rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_REAL, 0, 0, float_data, sizeof(SQLREAL), NULL); CHECK_STMT_RESULT(rc, "SQLBindParam (SQLREAL) failed", hstmt); rc = SQLExecute(hstmt); CHECK_STMT_RESULT(rc, "SQLExec failed", hstmt); rc = SQLFreeStmt(hstmt, SQL_CLOSE); CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt); sql = "SELECT mydouble, myfloat FROM float_save"; rc = SQLPrepare(hstmt, sql, SQL_NTS); CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt); rc = SQLBindCol(hstmt, 1, SQL_C_DOUBLE, &double_get, 0, NULL); CHECK_STMT_RESULT(rc, "SQLBindCol (SQLDOUBLE) failed", hstmt); rc = SQLBindCol(hstmt, 2, SQL_C_FLOAT, &float_get, 0, NULL); CHECK_STMT_RESULT(rc, "SQLBindCol (SQLREAL) failed", hstmt); rc = SQLExecute(hstmt); CHECK_STMT_RESULT(rc, "SQLExec failed", hstmt); rc = SQLFetch(hstmt); CHECK_STMT_RESULT(rc, "SQLFetch failed", hstmt); int fail = 0; if (memcmp(double_data, &double_get, sizeof(SQLDOUBLE)) != 0) { printf("Fetched double value differs from saved double value.\n"); fail = 1; } if (memcmp(float_data, &float_get, sizeof(SQLREAL)) != 0) { printf("Fetched float value differs from saved float value.\n"); fail = 1; } if (fail) exit(1); SQLFreeStmt(hstmt, SQL_CLOSE); SQLFreeHandle(SQL_HANDLE_STMT, hstmt); test_disconnect(); }