Subject Re: [firebird-support] compiling embeded sql (real newbie)
Author kubanych
Stanislav Kunc ?????:

>Windows 2000.
>MinGW compiler is in directory: C:\Temp\Dev-C++\bin\
>Firebird 1.5 is unzipped in directory: C:\Temp\firebird\
>
>
>
Hello. I found how to use firebird with mingw. May be it will be usefull.
It was described at message " possible bug in 1.5 (4290) DSQL XSQLDA
insert inserts, but kills app" sent at 10.05.2004 15:46

Here are its contents
++++++++++++++++++++++++++

Dear members.

If there is real firebird bug found here,
then the message is for fb-devel
else the message is for fb-support.


I tried to alter table and insert data using dsql with parameters.

1. firebird 1.5 (4290) embedded inserted data, but next
connect to database just after disconnecting failed.
2. firebird 1.5 (4290) SS inserted data, but the inserting
application died after disconnecting from database.
(msvc7.0 and gcc\mingw compiled apps worked equally)
3. firebird 1.0.3 (972) insertes data and next
connect to database just after disconnecting worked.


Here is small sample simulating the situation.
OS - winxp pro, sp1.
compilers: msvc 7.0 and gcc\mingw

firebird 1.5 (4290) is installed at c:\firebird1_5
firebird 1.0.3 (972) is installed at c:\firebird

desription of files:
-----------------------
init.sql sql script to create database
test.e e source for app running sql/dsql

1_5make_db.bat batch to build database with fb 1.5
1_5make_exe_gcc.bat batch to compile app with gcc with fb 1.5
1_5make_exe_msvc.bat batch to compile app with msvc 7.0 with fb 1.5

1_03make_db.bat batch to build database with fb 1.0.3
1_03make_exe_msvc.bat batch to compile app with msvc 7.0 with fb 1.0.3

clear.bat batch to clear generated files

fbclient.def from fb 1.5 sources used to link
vsvars32.bat generated by MSVS 7 installer
-----------------------

1_03make_exe_msvc.bat didn't find some functions, but IDE project
application was normally compiled with fb 1.0.3 and normally worked.

Here are the files.

===========file:init.sql sql script to create database
create database 'test.fdb' ;

/*--ELEMENT TOPOLOGY-------------------------------------------------*/
create table giaElTpg(
giaElTpg integer
,gsaElements integer /*--this table is filled in order given by gsaElements*/
,giaElIndex integer
);


commit;


===========EOF
===========file:init.test.e e source for app running sql/dsql
#include<stdio.h>


EXEC SQL
SET DATABASE DB2 = 'test.fdb';

int DSQL_INPUT(
int max_iElNumNode
,int records_inserted
)
{
XSQLDA *in_sqlda;
XSQLVAR *var;

char sql[65536];
int dtype;
char sqlplus[256];
int n=0;
int i,j,k;

n=max_iElNumNode+3;

in_sqlda = (XSQLDA *)malloc(XSQLDA_LENGTH(n));
in_sqlda->version = SQLDA_VERSION1;
in_sqlda->sqln = n;

sprintf(sql,"INSERT INTO giaElTpg (giaElIndex, gsaElements,giaElTpg ");
for (i=0; i<n-3; i++ )
{
sprintf(sqlplus," , N%d",i+1);
strcat(sql,sqlplus);
}
strcat(sql," ) VALUES ( ");

for (i=0; i<n; i++ )
{ if (i>0)
{
strcat(sql,",");
}
strcat(sql,"?");
}
strcat(sql," );");

printf("slq=|%s|\n",sql);

EXEC SQL PREPARE SQL_STMT FROM :sql;

EXEC SQL DESCRIBE INPUT SQL_STMT USING SQL DESCRIPTOR in_sqlda;

for (i=0; i<n; i++ )
{
in_sqlda->sqlvar[i].sqldata = (char *)malloc(sizeof(long));
in_sqlda->sqlvar[i].sqltype = SQL_LONG;
}


for (j=0;j<records_inserted;j++) // in gsael loop
{

*(long *)(in_sqlda->sqlvar[0].sqldata) = j-1; //giaElIndex
*(long *)(in_sqlda->sqlvar[1].sqldata) = j+0; //gsaElements
*(long *)(in_sqlda->sqlvar[2].sqldata) = j+1; //giaElTpg

for (k=0; k<max_iElNumNode; k++)
{
*(long *)(in_sqlda->sqlvar[3+k].sqldata) = k+j*max_iElNumNode;
}

EXEC SQL EXECUTE SQL_STMT USING SQL DESCRIPTOR in_sqlda;
}


for (i=0; i<n; i++ )
{
free(in_sqlda->sqlvar[i].sqldata );
}
free(in_sqlda);

return SQLCODE;
}


int DB_ALTER_giaElTpg(
int max_iElNumNode
)
{
char sql[65536];
int i;

for (i=0;i<max_iElNumNode;i++)
{
sprintf(sql,"ALTER TABLE giaElTpg ADD N%d INTEGER;",i+1);

EXEC SQL EXECUTE IMMEDIATE :sql;

}

return SQLCODE;
}


int main(){
int max_iElNumNode=20;
char sql[256];
int i;
int error_code;

EXEC SQL CONNECT DB2;
printf("1 CONNECT %d\n",SQLCODE);


EXEC SQL SET TRANSACTION READ WRITE USING DB2;
printf("SET TRANSACTION %d\n",SQLCODE);

error_code=DB_ALTER_giaElTpg(20);

EXEC SQL COMMIT;
printf("COMMIT %d\n",SQLCODE);

EXEC SQL DISCONNECT DB2;
printf("DISCONNECT %d\n",SQLCODE);

EXEC SQL CONNECT DB2;
printf("2 CONNECT %d\n",SQLCODE);

EXEC SQL SET TRANSACTION READ WRITE USING DB2;
printf("SET TRANSACTION %d\n",SQLCODE);
/*
EXEC SQL EXECUTE IMMEDIATE 'INSERT INTO giaElTpg (N1, N2, N3, N4, N5, N6, N7) VALUES (3,1,4,1,5,9,2);';
*/
error_code=DSQL_INPUT(20,30);

EXEC SQL COMMIT;
printf("COMMIT %d\n",SQLCODE);

EXEC SQL DISCONNECT DB2;
printf("DISCONNECT %d\n",SQLCODE);
/*
EXEC SQL CONNECT DB2;
printf("3 CONNECT %d\n",SQLCODE);

EXEC SQL SET TRANSACTION READ WRITE USING DB2;
printf("SET TRANSACTION %d\n",SQLCODE);

EXEC SQL INSERT INTO giaElTpg (giaElTpg) VALUES (3141592);

EXEC SQL COMMIT;
printf("COMMIT %d\n",SQLCODE);

EXEC SQL DISCONNECT DB2;
printf("DISCONNECT %d\n",SQLCODE);
*/

return SQLCODE;
}
===========EOF
===========file:init.1_5make_db.bat batch to build database with fb 1.5
del test.fdb
c:\firebird1_5\bin\isql.exe -i init.sql -user SYSDBA -pw masterkey
===========EOF
===========file:init.1_5make_exe_gcc.bat batch to compile app with gcc with fb 1.5
del *.exe
del *.a

c:\firebird1_5\bin\gpre test.e -u SYSDBA -password masterkey -m
dlltool --dllname fbclient.dll --def fbclient.def --output-lib fbclient.dll.a
gcc -o 1_5test_gcc.exe fbclient.def fbclient.dll.a test.c
===========EOF
===========file:init.1_5make_exe_msvc.bat batch to compile app with msvc 7.0 with fb 1.5
del *.exe
del *.obj

call vsvars32.bat ! generated by MSVS 7 installer
c:\firebird1_5\bin\gpre test.e -u SYSDBA -password masterkey -m
cl -Fe1_5test_msvc.exe -Ic:\firebird1_5\include test.c c:\firebird1_5\lib\fbclient_ms.lib
===========EOF
===========file:init.1_03make_db.bat batch to build database with fb 1.0.3
del test.fdb
c:\firebird\bin\isql.exe -i init.sql -user SYSDBA -pw masterkey
===========EOF
===========file:init.1_03make_exe_msvc.bat batch to compile app with msvc 7.0 with fb 1.0.3
del *.exe
del *.obj

call vsvars32.bat
c:\firebird\bin\gpre test.e -u SYSDBA -password masterkey -m
cl -Fe1_03test_msvc.exe -Ic:\firebird\include test.c c:\firebird\lib\gds32_ms.lib
===========EOF
===========file:init.clear.bat batch to clear generated files
del *.exe
del *.fdb
del *.a
del *.obj
===========EOF


Thanks and Best regards,
Kubanychbek Tajmamat oolu.