Subject Re: [ib-support] isc_dsql_exec_immed2()
Author Paul Beach
Carlos,

I have to eat my hat here :-), we have just de-selected a muppet, and I'll
volunteeer to take his place.
It looked like the call was falling through, but that is not the case, with
some tweaking of my sloppy code, and some input from Ann, I put together the
following and it works.

#include <ibase.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

long sql_numcols( isc_db_handle *, isc_tr_handle *, char *);

isc_db_handle db = 0L;
isc_tr_handle trans = 0L;

ISC_STATUS isc_status[20], return_code;

int main(int argc, char* argv[])
{


char db_name[128] = "f:\\employee.gdb";
long counter;

printf("Connecting to Database\n");

isc_attach_database(isc_status,
strlen (db_name),
db_name,
&db,
0,
NULL);

if (isc_status[0] == 1 && isc_status[1])
{
isc_print_status(isc_status);
return (1);
}

printf("Starting a Transaction\n");

isc_start_transaction(isc_status,
&trans,
1,
&db,
0,
NULL);

if (isc_status[0] == 1 && isc_status[1])
{
isc_print_status(isc_status);
return (1);
}

counter = sql_numcols (&db,
&trans,
"RDB$RELATIONS");

printf ("columns: %d\n", counter );



printf("Committing a Transaction\n");
isc_commit_transaction(isc_status,
&trans);

if (isc_status[0] == 1 && isc_status[1])
{
isc_print_status(isc_status);
return (1);
}

printf("Detaching from Database\n");
isc_detach_database(isc_status,
&db);

if (isc_status[0] == 1 && isc_status[1])
{
isc_print_status(isc_status);
return (1);
}

return 0;
}


long sql_numcols(
isc_db_handle *dbhandle,
isc_tr_handle *trhandle,
char *tablename )

{
short rc;
char cols;
char query[100];
XSQLDA *out_xsqlda;
XSQLVAR *xsqlvar;

/* 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 */
out_xsqlda = (XSQLDA *)malloc(XSQLDA_LENGTH(1));
if ( out_xsqlda == NULL )
{
return(0);
}
out_xsqlda->version = SQLDA_VERSION1;
out_xsqlda->sqln = 1;
out_xsqlda->sqld = 1;
xsqlvar = out_xsqlda->sqlvar;
xsqlvar->sqltype = SQL_LONG;
xsqlvar->sqlscale = 0;
xsqlvar->sqlsubtype = 0;
xsqlvar->sqllen = 4;
xsqlvar->sqldata = &cols;
xsqlvar->sqlind = &rc;
xsqlvar->sqlname_length = strlen ("COUNT");
strncpy (xsqlvar->sqlname, "COUNT", sizeof (xsqlvar->sqlname));

printf("Executing Query\n");
isc_dsql_exec_immed2(isc_status,
dbhandle,
trhandle,
0,
query,
1,
NULL,
out_xsqlda );

if (isc_status[0] == 1 && isc_status[1])
{
isc_print_status(isc_status);
return (1);
}

printf("sqld: %d\n", out_xsqlda->sqld); // -> 0
printf("sqllen: %d\n", out_xsqlda->sqlvar[0].sqllen); // -> 0
printf("sqltype: %d\n", out_xsqlda->sqlvar[0].sqltype); // -> 0
printf("numcols: %ld\n", *(long *)out_xsqlda->sqlvar[0].sqldata); // -> 0

cols = *(char *)out_xsqlda->sqlvar[0].sqldata;

return(cols);
}


Regards
Paul Beach
Main Tel (UK):+44 (0) 1844 354465
Mobile: (UK): +44 (0) 7764 188603
http://www.ibphoenix.com
For all your upto date Firebird and
InterBase information