Subject | Re: [ib-support] trhandle changing after isc_dsql_exec_immediate2() |
---|---|
Author | Ann W. Harrison |
Post date | 2001-10-28T16:49:34Z |
At 07:36 PM 10/25/2001 -0200, C R Zamana wrote:
with transactions. I'm running on Windows, but that shouldn't make
a difference. Code follows.
Regards,
Ann
www.ibphoenix.com
We have answers.
#ifndef _JRD_IBASE_H_
#include <ibase.h>
#endif
#include <stdio.h>
#include <string.h>
long sql_numcols( isc_db_handle *, isc_tr_handle *, char *);
static const ISC_QUAD
isc_blob_null = {0,0}; /* initializer for blobs */
static const long *gds__null = 0; /* dummy status vector */
isc_db_handle
db = 0; /* database handle */
isc_tr_handle
gds__trans = 0; /* default transaction handle */
long
isc_status [20], /* status vector */
isc_status2 [20]; /* status vector */
long
isc_array_length, /* array return size */
SQLCODE; /* SQL status code */
static const short
isc_1l = 20;
static const char
isc_1 [] = {
isc_dpb_version1,
isc_dpb_user_name,6, 'S','Y','S','D','B','A',
isc_dpb_password,9, 'm','a','s','t','e','r','k','e','y',
}; /* end of dpb string for request isc_1 */
#define gds__blob_null isc_blob_null /* compatibility symbols */
#define gds__status isc_status
#define gds__status2 isc_status2
#define gds__array_length isc_array_length
#define gds__count isc_count
#define gds__slack isc_slack
#define gds__utility isc_utility /* end of compatibility symbols */
#ifndef isc_version4
Generate a compile-time error.
Picking up a V3 include file after preprocessing with V4 GPRE.
#endif
/**** end of GPRE definitions ****/
#include <stdio.h>
main ()
{
long counter;
isc_attach_database (isc_status, 0, "foo.gdb", &db, isc_1l, isc_1);;
if (isc_status [1])
{
isc_print_status (isc_status);
}
isc_version (&db, 0, 0);
isc_start_transaction ((long*) 0L, &gds__trans, (short) 1, &db, (short) 0,
(char*) 0);
counter = sql_numcols (&db, &gds__trans, "RDB$RELATIONS");
printf ("columns: %d\n", counter );
isc_commit_transaction ((long*) 0L, &gds__trans);
if (db)
isc_detach_database ((long*) 0L, &db);
printf ("finito\n");
}
long sql_numcols(
isc_db_handle *dbhandle,
isc_tr_handle *trhandle,
char *tablename )
{
int rc;
long cols;
char query[100];
XSQLDA *osqlda;
ISC_STATUS status_vector[20];
XSQLVAR *xvar;
/* building the query */
strcpy( query, "select count(*) from rdb$relation_fields ");
strcat( query, "where rdb$relation_name = '" );
/* uppercase assumed here */
strcat( query, tablename );
strcat( query, "'\0" );
/* allocating an osqlda */
osqlda = (XSQLDA *)malloc(XSQLDA_LENGTH(1));
if ( osqlda == NULL )
{
return(0);
}
osqlda->version = SQLDA_VERSION1;
osqlda->sqln = 1;
osqlda->sqld = 1;
xvar = osqlda->sqlvar;
xvar->sqltype = SQL_LONG;
xvar->sqlscale = 0;
xvar->sqlsubtype = 0;
xvar->sqllen = 4;
xvar->sqldata = &cols;
xvar->sqlind = &rc;
xvar->sqlname_length = strlen ("COUNT");
strncpy (xvar->sqlname, "COUNT", sizeof (xvar->sqlname));
/* executing */
isc_dsql_exec_immed2( status_vector, dbhandle,
trhandle, 0, query, 1, NULL, osqlda );
if (status_vector[1])
isc_print_status (status_vector);
printf("sqld: %d\n", osqlda->sqld); // -> 0
printf("sqllen: %d\n", osqlda->sqlvar[0].sqllen); // -> 0
printf("sqltype: %d\n", osqlda->sqlvar[0].sqltype); // -> 0
printf("numcols: %ld\n", *(long *)osqlda->sqlvar[0].sqldata); // -> 0
cols = *(long *)osqlda->sqlvar[0].sqldata;
return(cols);
}
> Hi!I've tried my version of the program and it works fine - no problem
>
> Very strange.
>
> Coming back to my function that returns the number of columns of
>a table, I realize that the function above is changing the previously
>trhandle allocated via isc_start_transaction().
with transactions. I'm running on Windows, but that shouldn't make
a difference. Code follows.
Regards,
Ann
www.ibphoenix.com
We have answers.
#ifndef _JRD_IBASE_H_
#include <ibase.h>
#endif
#include <stdio.h>
#include <string.h>
long sql_numcols( isc_db_handle *, isc_tr_handle *, char *);
static const ISC_QUAD
isc_blob_null = {0,0}; /* initializer for blobs */
static const long *gds__null = 0; /* dummy status vector */
isc_db_handle
db = 0; /* database handle */
isc_tr_handle
gds__trans = 0; /* default transaction handle */
long
isc_status [20], /* status vector */
isc_status2 [20]; /* status vector */
long
isc_array_length, /* array return size */
SQLCODE; /* SQL status code */
static const short
isc_1l = 20;
static const char
isc_1 [] = {
isc_dpb_version1,
isc_dpb_user_name,6, 'S','Y','S','D','B','A',
isc_dpb_password,9, 'm','a','s','t','e','r','k','e','y',
}; /* end of dpb string for request isc_1 */
#define gds__blob_null isc_blob_null /* compatibility symbols */
#define gds__status isc_status
#define gds__status2 isc_status2
#define gds__array_length isc_array_length
#define gds__count isc_count
#define gds__slack isc_slack
#define gds__utility isc_utility /* end of compatibility symbols */
#ifndef isc_version4
Generate a compile-time error.
Picking up a V3 include file after preprocessing with V4 GPRE.
#endif
/**** end of GPRE definitions ****/
#include <stdio.h>
main ()
{
long counter;
isc_attach_database (isc_status, 0, "foo.gdb", &db, isc_1l, isc_1);;
if (isc_status [1])
{
isc_print_status (isc_status);
}
isc_version (&db, 0, 0);
isc_start_transaction ((long*) 0L, &gds__trans, (short) 1, &db, (short) 0,
(char*) 0);
counter = sql_numcols (&db, &gds__trans, "RDB$RELATIONS");
printf ("columns: %d\n", counter );
isc_commit_transaction ((long*) 0L, &gds__trans);
if (db)
isc_detach_database ((long*) 0L, &db);
printf ("finito\n");
}
long sql_numcols(
isc_db_handle *dbhandle,
isc_tr_handle *trhandle,
char *tablename )
{
int rc;
long cols;
char query[100];
XSQLDA *osqlda;
ISC_STATUS status_vector[20];
XSQLVAR *xvar;
/* building the query */
strcpy( query, "select count(*) from rdb$relation_fields ");
strcat( query, "where rdb$relation_name = '" );
/* uppercase assumed here */
strcat( query, tablename );
strcat( query, "'\0" );
/* allocating an osqlda */
osqlda = (XSQLDA *)malloc(XSQLDA_LENGTH(1));
if ( osqlda == NULL )
{
return(0);
}
osqlda->version = SQLDA_VERSION1;
osqlda->sqln = 1;
osqlda->sqld = 1;
xvar = osqlda->sqlvar;
xvar->sqltype = SQL_LONG;
xvar->sqlscale = 0;
xvar->sqlsubtype = 0;
xvar->sqllen = 4;
xvar->sqldata = &cols;
xvar->sqlind = &rc;
xvar->sqlname_length = strlen ("COUNT");
strncpy (xvar->sqlname, "COUNT", sizeof (xvar->sqlname));
/* executing */
isc_dsql_exec_immed2( status_vector, dbhandle,
trhandle, 0, query, 1, NULL, osqlda );
if (status_vector[1])
isc_print_status (status_vector);
printf("sqld: %d\n", osqlda->sqld); // -> 0
printf("sqllen: %d\n", osqlda->sqlvar[0].sqllen); // -> 0
printf("sqltype: %d\n", osqlda->sqlvar[0].sqltype); // -> 0
printf("numcols: %ld\n", *(long *)osqlda->sqlvar[0].sqldata); // -> 0
cols = *(long *)osqlda->sqlvar[0].sqldata;
return(cols);
}