Subject | Re: [firebird-support] Using the API |
---|---|
Author | Peter Faulks |
Post date | 2010-11-18T22:37:41Z |
The function below used to work.
The only changes I've made is to go from global XSQLDA's to declaring
them locally.
I get a numeric overflow error.
When I hard code all the values into the SQL statement it works as
expected.
I've tried hard-coding the date and leaving the other two params, and as
you can see hard-coding the date and trk and leaving one param.
Is there something stupid that I'm doing wrong?
//---------------------------------------------------------------------------
#define START_READ_ONLY_TRANS { \
if (isc_start_transaction(status, &read_only_trans, 1, &db, read_only_tpb_len, read_only_tpb)) \
{ \
errorMsg_isc_interprete("isc_start_transaction"); \
} }
#define COMMIT_READ_ONLY_TRANS { \
if (isc_commit_transaction(status, &read_only_trans)) \
{ \
errorMsg_isc_interprete("isc_commit_transaction"); \
} }
void TFormMain::Function_that_doesnt_work_and_is_giving_me_a_ton_of_grief()
{
ISC_DATE Hrcdate;
char HtrkStr[8], Hstrtime[16];
short Hcatgry, Hraceno, Hrdist, Hstrs;
int Hraceref, Hprzmny;
SQL_VARCHAR(6) Hcls;
SQL_VARCHAR(3) Hage;
SQL_VARCHAR(4) Hsex;
SQL_VARCHAR(61) Hracename;
isc_stmt_handle stmt = NULL;
strcpy(sqlBuff, "SELECT r.raceref, r.raceno, CAST(CAST(DATEADD(tz.diffmins MINUTE TO r.utc) AS TIME) AS CHAR(13)), "
"r.dist, r.cls, r.age, r.sex, r.przmny, r.strs, r.racename "
"FROM races r JOIN tracks t ON r.trk = t.trk JOIN timezones tz ON t.state = tz.state "
"WHERE r.rcdate = '2009-05-01' AND r.trk = 'D''Ben' AND r.catgry = ? "
"ORDER BY r.raceno ");
/*
strcpy(sqlBuff, "SELECT r.raceref, r.raceno, CAST(CAST(DATEADD(tz.diffmins MINUTE TO r.utc) AS TIME) AS CHAR(13)), "
"r.dist, r.cls, r.age, r.sex, r.przmny, r.strs, r.racename "
"FROM races r JOIN tracks t ON r.trk = t.trk JOIN timezones tz ON t.state = tz.state "
"WHERE r.rcdate = '2009-05-01' AND r.trk = 'D''Ben' AND r.catgry = 1 "
"ORDER BY r.raceno ");
*/
XSQLDA *sqldaIn = (XSQLDA *) malloc(XSQLDA_LENGTH(1));
XSQLDA *sqldaOut = (XSQLDA *) malloc(XSQLDA_LENGTH(10));
sqldaIn->version = SQLDA_VERSION1;
sqldaOut->version = SQLDA_VERSION1;
sqldaIn->sqld = 1;
sqldaIn->sqln = 1;
sqldaOut->sqld = 10;
sqldaOut->sqln = 10;
START_READ_ONLY_TRANS
if (isc_dsql_allocate_statement(status, &db, &stmt))
{
errorMsg_isc_interprete("isc_dsql_allocate_statement");
}
if (isc_dsql_prepare(status, &read_only_trans, &stmt, 0, sqlBuff, 3, sqldaOut))
{
errorMsg_isc_interprete("isc_dsql_prepare");
}
/*
if (isc_dsql_describe_bind(status, &stmt, 1, sqldaIn))
{
errorMsg_isc_interprete("isc_dsql_describe_bind");
}
*/
/*
tDateTime_2_ISC_DATE(&Hrcdate, DTraceDay);
sqldaIn->sqlvar[0].sqltype = SQL_TYPE_DATE;
sqldaIn->sqlvar[0].sqllen = sizeof(ISC_DATE);
sqldaIn->sqlvar[0].sqldata = (char *)&Hrcdate;
*/
//strcpy(HtrkStr, trks->Strings[LVmeetings->Selected->Index].t_str());
/*
strcpy(HtrkStr, "D'Ben");
sqldaIn->sqlvar[0].sqltype = SQL_TEXT;
sqldaIn->sqlvar[0].sqllen = strlen(HtrkStr);
sqldaIn->sqlvar[0].sqldata = HtrkStr;
//Hcatgry = (short) trks->Objects[LVmeetings->Selected->Index];
*/
Hcatgry = 1;
sqldaIn->sqlvar[0].sqltype = SQL_SHORT;
sqldaIn->sqlvar[0].sqllen = sizeof(SQL_SHORT);
sqldaIn->sqlvar[0].sqldata = (char *)&Hcatgry;
// ShowMessage(String(ISC_DATE_2_Dd_dd_Mmm_YY(strFmtBuf, Hrcdate)) + String(", ") + String(HtrkStr) + String(", ") + String(Hcatgry));
if (isc_dsql_describe(status, &stmt, 1, sqldaOut))
{
errorMsg_isc_interprete("isc_dsql_describe");
}
sqldaOut->sqlvar[0].sqldata = (char *)&Hraceref;
sqldaOut->sqlvar[1].sqldata = (char *)&Hraceno;
sqldaOut->sqlvar[2].sqldata = (char *)&Hstrtime;
sqldaOut->sqlvar[3].sqldata = (char *)&Hrdist;
sqldaOut->sqlvar[4].sqldata = (char *)&Hcls;
sqldaOut->sqlvar[5].sqldata = (char *)&Hage;
sqldaOut->sqlvar[6].sqldata = (char *)&Hsex;
sqldaOut->sqlvar[7].sqldata = (char *)&Hprzmny;
sqldaOut->sqlvar[8].sqldata = (char *)&Hstrs;
sqldaOut->sqlvar[9].sqldata = (char *)&Hracename;
if (isc_dsql_execute(status, &read_only_trans, &stmt, 1, sqldaIn))
{
errorMsg_isc_interprete("isc_dsql_execute");
}
int i = 0, fetch_stat;
while ((fetch_stat = isc_dsql_fetch(status, &stmt, 1, sqldaOut)) == 0)
{
TabCtrlRaces->Tabs->AddObject(" " + String(Hraceno), (System::TObject*) Hraceref);
LVraceDtls->Items->Add();
LVraceDtls->Items->Item[i]->Data = (void *) Hraceref;
LVraceDtls->Items->Item[i]->Caption = Hraceno;
*(Hstrtime + 5) = '\0';
LVraceDtls->Items->Item[i]->SubItems->Add(Hstrtime);
LVraceDtls->Items->Item[i]->SubItems->Add(Hrdist);
*(Hcls.data + Hcls.len) = '\0';
*(Hage.data + Hage.len) = '\0';
*(Hsex.data + Hsex.len) = '\0';
sprintf(strFmtBuf, "%-5s %s %s", Hcls.data, Hage.data, Hsex.data);
LVraceDtls->Items->Item[i]->SubItems->Add(strFmtBuf);
sprintf(strFmtBuf, "%.1f", ((float)Hprzmny*0.001));
LVraceDtls->Items->Item[i]->SubItems->Add(strFmtBuf);
LVraceDtls->Items->Item[i]->SubItems->Add(Hstrs);
*(Hracename.data + (int)Hracename.len) = '\0';
LVraceDtls->Items->Item[i]->SubItems->Add(Hracename.data);
i++;
}
ShowMessage(fetch_stat);
LVraceDtls->Items->EndUpdate();
COMMIT_READ_ONLY_TRANS
isc_dsql_free_statement(status, &stmt, DSQL_drop);
free(sqldaIn);
free(sqldaOut);
}
The only changes I've made is to go from global XSQLDA's to declaring
them locally.
I get a numeric overflow error.
When I hard code all the values into the SQL statement it works as
expected.
I've tried hard-coding the date and leaving the other two params, and as
you can see hard-coding the date and trk and leaving one param.
Is there something stupid that I'm doing wrong?
//---------------------------------------------------------------------------
#define START_READ_ONLY_TRANS { \
if (isc_start_transaction(status, &read_only_trans, 1, &db, read_only_tpb_len, read_only_tpb)) \
{ \
errorMsg_isc_interprete("isc_start_transaction"); \
} }
#define COMMIT_READ_ONLY_TRANS { \
if (isc_commit_transaction(status, &read_only_trans)) \
{ \
errorMsg_isc_interprete("isc_commit_transaction"); \
} }
void TFormMain::Function_that_doesnt_work_and_is_giving_me_a_ton_of_grief()
{
ISC_DATE Hrcdate;
char HtrkStr[8], Hstrtime[16];
short Hcatgry, Hraceno, Hrdist, Hstrs;
int Hraceref, Hprzmny;
SQL_VARCHAR(6) Hcls;
SQL_VARCHAR(3) Hage;
SQL_VARCHAR(4) Hsex;
SQL_VARCHAR(61) Hracename;
isc_stmt_handle stmt = NULL;
strcpy(sqlBuff, "SELECT r.raceref, r.raceno, CAST(CAST(DATEADD(tz.diffmins MINUTE TO r.utc) AS TIME) AS CHAR(13)), "
"r.dist, r.cls, r.age, r.sex, r.przmny, r.strs, r.racename "
"FROM races r JOIN tracks t ON r.trk = t.trk JOIN timezones tz ON t.state = tz.state "
"WHERE r.rcdate = '2009-05-01' AND r.trk = 'D''Ben' AND r.catgry = ? "
"ORDER BY r.raceno ");
/*
strcpy(sqlBuff, "SELECT r.raceref, r.raceno, CAST(CAST(DATEADD(tz.diffmins MINUTE TO r.utc) AS TIME) AS CHAR(13)), "
"r.dist, r.cls, r.age, r.sex, r.przmny, r.strs, r.racename "
"FROM races r JOIN tracks t ON r.trk = t.trk JOIN timezones tz ON t.state = tz.state "
"WHERE r.rcdate = '2009-05-01' AND r.trk = 'D''Ben' AND r.catgry = 1 "
"ORDER BY r.raceno ");
*/
XSQLDA *sqldaIn = (XSQLDA *) malloc(XSQLDA_LENGTH(1));
XSQLDA *sqldaOut = (XSQLDA *) malloc(XSQLDA_LENGTH(10));
sqldaIn->version = SQLDA_VERSION1;
sqldaOut->version = SQLDA_VERSION1;
sqldaIn->sqld = 1;
sqldaIn->sqln = 1;
sqldaOut->sqld = 10;
sqldaOut->sqln = 10;
START_READ_ONLY_TRANS
if (isc_dsql_allocate_statement(status, &db, &stmt))
{
errorMsg_isc_interprete("isc_dsql_allocate_statement");
}
if (isc_dsql_prepare(status, &read_only_trans, &stmt, 0, sqlBuff, 3, sqldaOut))
{
errorMsg_isc_interprete("isc_dsql_prepare");
}
/*
if (isc_dsql_describe_bind(status, &stmt, 1, sqldaIn))
{
errorMsg_isc_interprete("isc_dsql_describe_bind");
}
*/
/*
tDateTime_2_ISC_DATE(&Hrcdate, DTraceDay);
sqldaIn->sqlvar[0].sqltype = SQL_TYPE_DATE;
sqldaIn->sqlvar[0].sqllen = sizeof(ISC_DATE);
sqldaIn->sqlvar[0].sqldata = (char *)&Hrcdate;
*/
//strcpy(HtrkStr, trks->Strings[LVmeetings->Selected->Index].t_str());
/*
strcpy(HtrkStr, "D'Ben");
sqldaIn->sqlvar[0].sqltype = SQL_TEXT;
sqldaIn->sqlvar[0].sqllen = strlen(HtrkStr);
sqldaIn->sqlvar[0].sqldata = HtrkStr;
//Hcatgry = (short) trks->Objects[LVmeetings->Selected->Index];
*/
Hcatgry = 1;
sqldaIn->sqlvar[0].sqltype = SQL_SHORT;
sqldaIn->sqlvar[0].sqllen = sizeof(SQL_SHORT);
sqldaIn->sqlvar[0].sqldata = (char *)&Hcatgry;
// ShowMessage(String(ISC_DATE_2_Dd_dd_Mmm_YY(strFmtBuf, Hrcdate)) + String(", ") + String(HtrkStr) + String(", ") + String(Hcatgry));
if (isc_dsql_describe(status, &stmt, 1, sqldaOut))
{
errorMsg_isc_interprete("isc_dsql_describe");
}
sqldaOut->sqlvar[0].sqldata = (char *)&Hraceref;
sqldaOut->sqlvar[1].sqldata = (char *)&Hraceno;
sqldaOut->sqlvar[2].sqldata = (char *)&Hstrtime;
sqldaOut->sqlvar[3].sqldata = (char *)&Hrdist;
sqldaOut->sqlvar[4].sqldata = (char *)&Hcls;
sqldaOut->sqlvar[5].sqldata = (char *)&Hage;
sqldaOut->sqlvar[6].sqldata = (char *)&Hsex;
sqldaOut->sqlvar[7].sqldata = (char *)&Hprzmny;
sqldaOut->sqlvar[8].sqldata = (char *)&Hstrs;
sqldaOut->sqlvar[9].sqldata = (char *)&Hracename;
if (isc_dsql_execute(status, &read_only_trans, &stmt, 1, sqldaIn))
{
errorMsg_isc_interprete("isc_dsql_execute");
}
int i = 0, fetch_stat;
while ((fetch_stat = isc_dsql_fetch(status, &stmt, 1, sqldaOut)) == 0)
{
TabCtrlRaces->Tabs->AddObject(" " + String(Hraceno), (System::TObject*) Hraceref);
LVraceDtls->Items->Add();
LVraceDtls->Items->Item[i]->Data = (void *) Hraceref;
LVraceDtls->Items->Item[i]->Caption = Hraceno;
*(Hstrtime + 5) = '\0';
LVraceDtls->Items->Item[i]->SubItems->Add(Hstrtime);
LVraceDtls->Items->Item[i]->SubItems->Add(Hrdist);
*(Hcls.data + Hcls.len) = '\0';
*(Hage.data + Hage.len) = '\0';
*(Hsex.data + Hsex.len) = '\0';
sprintf(strFmtBuf, "%-5s %s %s", Hcls.data, Hage.data, Hsex.data);
LVraceDtls->Items->Item[i]->SubItems->Add(strFmtBuf);
sprintf(strFmtBuf, "%.1f", ((float)Hprzmny*0.001));
LVraceDtls->Items->Item[i]->SubItems->Add(strFmtBuf);
LVraceDtls->Items->Item[i]->SubItems->Add(Hstrs);
*(Hracename.data + (int)Hracename.len) = '\0';
LVraceDtls->Items->Item[i]->SubItems->Add(Hracename.data);
i++;
}
ShowMessage(fetch_stat);
LVraceDtls->Items->EndUpdate();
COMMIT_READ_ONLY_TRANS
isc_dsql_free_statement(status, &stmt, DSQL_drop);
free(sqldaIn);
free(sqldaOut);
}