freetds AT lists.ibiblio.org
Subject: FreeTDS Development Group
List archive
Re: [freetds] Prepared Statement is losing the seconds valu
- From: <joseph.blough AT thomson.com>
- To: <freetds AT lists.ibiblio.org>
- Subject: Re: [freetds] Prepared Statement is losing the seconds valu
- Date: Thu, 21 Dec 2006 06:35:51 -0500
Here is a relatively small program that illustrates the problem. It
attempts to set a DATETIME field to "2006-12-21 05:34:12.000" but the
actual value that goes into the database is "2006-12-21 05:34:00.000".
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#include "tds.h"
#include "tdsconvert.h"
void FreeAllocatedResultSets(TDSSOCKET* socket);
static int tsql_handle_error(const TDSCONTEXT * pContext, TDSSOCKET *
pTdsSocket, TDSMESSAGE * pMessage)
{
fprintf(stderr, "Msg %d, Level %d, State %d, Server %s, Line
%d\n%s\n",
pMessage->msgno, pMessage->severity, pMessage->state,
pMessage->server, pMessage->line_number, pMessage->message);
return 0;
}
int main(int argc, char* argv[])
{
// Authentication
TDSLOGIN* login = tds_alloc_login();
if (login == NULL)
{
fprintf(stderr, "tds_alloc_login() failed.\n");
return -1;
}
tds_set_server(login, "127.0.0.1");
tds_set_user(login, "user");
tds_set_passwd(login, "password");
tds_set_app(login, "test_app");
tds_set_library(login, "TDS-Library");
tds_set_client_charset(login, "UTF-8");
tds_set_version(login, 8, 0);
TDSCONTEXT* context = tds_alloc_context(NULL);
if (context == NULL)
{
fprintf(stderr, "tds_alloc_context() failed.\n");
return -2;
}
context->msg_handler = tsql_handle_error;
context->err_handler = tsql_handle_error;
TDSSOCKET* socket = tds_alloc_socket(context, 512);
if (socket == NULL)
{
fprintf(stderr, "tds_alloc_socket() failed.\n");
return -3;
}
tds_set_parent(socket, NULL);
TDSCONNECTION* connection = tds_read_config_info(NULL, login,
context->locale);
if (!connection || tds_connect(socket, connection) == TDS_FAIL)
{
if (connection)
{
tds_free_socket(socket);
tds_free_connection(connection);
}
fprintf(stderr, "tds_connect() failed\n");
return -4;
}
tds_free_connection(connection);
// Set the database
int nReturn = tds_submit_query(socket, "USE test_db");
if (nReturn != TDS_SUCCEED)
{
fprintf(stderr, "tds_submit_query() failed for query\n");
FreeAllocatedResultSets(socket);
return -5;
}
FreeAllocatedResultSets(socket);
// Create the table
nReturn = tds_submit_query(socket, "CREATE TABLE table3 (IntCol int,
DateCol DATETIME)");
if (nReturn != TDS_SUCCEED)
{
fprintf(stderr, "tds_submit_query() failed for creating table3\n");
FreeAllocatedResultSets(socket);
return -5;
}
FreeAllocatedResultSets(socket);
// Create the prepared statement
TDSDYNAMIC* statement = NULL;
TDSPARAMINFO* parameters = NULL;
nReturn = tds_submit_prepare(socket, "INSERT INTO table3 (IntCol,
DateCol) VALUES (1, ?)", NULL, &statement, parameters);
if (nReturn != TDS_SUCCEED)
{
fprintf(stderr, "tds_submit_prepare() failed\n");
if (statement != NULL)
tds_free_dynamic(socket, statement);
if (parameters != NULL)
tds_free_param_results(parameters);
FreeAllocatedResultSets(socket);
return -6;
}
FreeAllocatedResultSets(socket);
tds_free_input_params(statement);
// Allocat the parameter
parameters = tds_alloc_param_result(statement->params);
if (parameters == NULL)
{
fprintf(stderr, "out of memory!\n");
return -7;
}
statement->params = parameters;
// Set the parameter
char* date_string = "2006-12-21 05:34:12.000";
int date_string_length = strlen(date_string);
CONV_RESULT cr;
int ret = tds_convert(socket->tds_ctx, SYBVARCHAR,
(TDS_CHAR*)date_string, date_string_length, SYBDATETIME, &cr);
TDSCOLUMN* curcol = parameters->columns[0];
curcol->column_type = SYBDATETIMN;
curcol->on_server.column_type = SYBDATETIMN;
curcol->column_size = ret;
curcol->on_server.column_size = ret;
curcol->column_varint_size = 1;
curcol->column_cur_size = ret;
tds_alloc_param_data(parameters, curcol);
memcpy(curcol->column_data, &cr.dt, ret);
// Execute the statement
nReturn = tds_submit_execute(socket, statement);
if (nReturn != TDS_SUCCEED)
{
fprintf(stderr, "tds_submit_execute() failed for statement\n");
FreeAllocatedResultSets(socket);
return -7;
}
FreeAllocatedResultSets(socket);
// Close the database connection
tds_free_socket(socket);
tds_free_login(login);
tds_free_context(context);
}
void FreeAllocatedResultSets(TDSSOCKET* socket)
{
int rc;
int result_type;
while ((rc = tds_process_tokens(socket, &result_type, NULL,
TDS_TOKEN_RESULTS)) == TDS_SUCCEED)
{
switch (result_type)
{
case TDS_DONE_RESULT:
case TDS_DONEPROC_RESULT:
case TDS_DONEINPROC_RESULT:
/* ignore possible spurious result (TDS7+ send it) */
case TDS_STATUS_RESULT:
break;
default:
fprintf(stderr, "Query should not return results\n");
break;
}
}
// Make sure to clean up after ourselves
if (socket != NULL)
tds_free_all_results(socket);
if (rc == TDS_FAIL)
{
fprintf(stderr, "tds_process_tokens() returned TDS_FAIL\n");
return;
}
else if (rc != TDS_NO_MORE_RESULTS)
{
fprintf(stderr, "tds_process_tokens() unexpected return\n");
return;
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
-
Re: [freetds] Prepared Statement is losing the seconds valu,
joseph.blough, 12/21/2006
- <Possible follow-up(s)>
-
Re: [freetds] Prepared Statement is losing the seconds valu,
ZIGLIO, Frediano, VF-IT, 12/22/2006
- Re: [freetds] Prepared Statement is losing the seconds valu, joseph.blough, 12/22/2006
Archive powered by MHonArc 2.6.24.