home *** CD-ROM | disk | FTP | other *** search
- ///////////////////////////////////////////////////////////////////////////////
- //
- // FILE: example8.sqc
- //
- // Sample Embedded SQL for C application
- //
- // FUNCTIONS:
- //
- // main() - Main program
- // ErrorHandler - Embedded SQL for C error handler
- //
- // COMMENTS:
- //
- // This program is a primitive ISQL.EXE clone. It basically loops in the
- // doit procedure until the user enters "quit". When it receives an SQL
- // statement from the user, it prepares it and calls select-statement which
- // opens a cursor and fetches the results. The rest of the code deals with
- // displaying the data and any error messages.
- //
- // Copyright (C) 1992 - 1994 Microsoft Corporation
- //
- ///////////////////////////////////////////////////////////////////////////////
-
- // function prototypes (instead of header file)
- void ErrorHandler (void);
- void doit(void);
- void select_statement(void);
- void initda(void);
- void fetch_data(void);
- void displayda_2(void);
- void displayca(void);
-
- #include <stddef.h> // standard C run-time header
- #include <stdio.h> // standard C run-time header
- #include <stdlib.h> // standard C run-time header
- #include "gcutil.h" // utility header
-
- // GLOBAL VARIABLES
-
- #define MAX_RETCOLS 20
-
- struct
- {
- short dyn_null;
- int smallint_var;
- long integer_var;
- double decimal_var;
- float sfloat_var;
- double lfloat_var;
- char char_var[51];
- } dyn_vars[255];
-
- struct sqlda *mysqlda;
-
- div_t sql_div;
- int sql_type;
- int remain_1;
- int index1;
- char stmt[81];
-
- EXEC SQL BEGIN DECLARE SECTION;
- char stmtbuf[81];
- char dbname[33];
- char uname[33];
- EXEC SQL END DECLARE SECTION;
-
- EXEC SQL DECLARE cur CURSOR FOR st2;
-
- int colno;
- int coltype;
- int collen;
- char colname[31];
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // FUNCTION: main()
- //
- // Main application
- //
- // PARAMETERS:
- //
- // argc - count of command line args
- // argv - array of command line argument strings
- // envp - array of environment strings
- //
- // RETURNS: 0 if successful, 1 if error
- //
- // COMMENTS:
- //
- ///////////////////////////////////////////////////////////////////////////////
-
- int main (
- int argc,
- char** argv,
- char** envp)
- {
- int nRet; // for return values
-
- EXEC SQL BEGIN DECLARE SECTION;
- // for CONNECT TO
- char szServerDatabase[(SQLID_MAX * 2)+2] = "";
- char szLoginPassword[(SQLID_MAX * 2)+2] = "";
- EXEC SQL END DECLARE SECTION;
-
- // install Embedded SQL for C error handler
- EXEC SQL WHENEVER SQLERROR CALL ErrorHandler();
- // set Embedded SQL for C options
- EXEC SQL SET OPTION LOGINTIME 10;
- EXEC SQL SET OPTION QUERYTIME 100;
-
- // display logo
- printf("Sample Embedded SQL for C application\n");
-
- // get info for CONNECT TO statement
- nRet = GetConnectToInfo(argc, argv,
- szServerDatabase,
- szLoginPassword);
- if (!nRet)
- {
- return (1);
- }
-
- // attempt connection to SQL Server
- EXEC SQL CONNECT TO :szServerDatabase
- USER :szLoginPassword;
- if (SQLCODE == 0)
- {
- printf("Connection to SQL Server established\n");
- }
- else
- {
- // problem connecting to SQL Server
- printf("ERROR: Connection to SQL Server failed\n");
- return (1);
- }
-
- EXEC SQL SET CURSORTYPE CUR_BROWSE;
-
- mysqlda = malloc(SQLDASIZE(MAX_RETCOLS));
- if (mysqlda == NULL)
- {
- printf("Unable to allocate memory for SQLDA structure.\n");
- return 0;
- }
-
- while (strcmp(stmtbuf, "quit") != 0)
- doit();
-
- // disconnect from SQL Server
- EXEC SQL DISCONNECT ALL;
-
- return (0);
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // FUNCTION: ErrorHandler()
- //
- // Called on Embedded SQL for C error, displays fields from SQLCA
- //
- // PARAMETERS: none
- //
- // RETURNS: none
- //
- // COMMENTS:
- //
- ///////////////////////////////////////////////////////////////////////////////
-
- void ErrorHandler (void)
- {
- // display error information from SQLCA
- printf("Error Handler called:\n");
- printf(" SQL Code = %li\n", SQLCODE);
- printf(" SQL Server Message %li: '%Fs'\n", SQLERRD1, SQLERRMC);
- }
-
- void doit(void)
- {
- printf("Enter SQL or 'quit' to terminate: ");
- gets(stmtbuf);
- if (strcmp(stmtbuf, "quit") == 0)
- exit(0);
- mysqlda->sqln = MAX_RETCOLS;
- mysqlda->sqld = 0;
- mysqlda->sqldabc = SQLDASIZE(MAX_RETCOLS);
-
- EXEC SQL PREPARE st INTO :mysqlda FROM :stmtbuf;
- strcpy(stmt, "Prepare statement");
- if (mysqlda->sqld != 0)
- select_statement();
- else
- {
- EXEC SQL EXECUTE st;
- if (SQLCODE != 0)
- displayca();
- else
- {
- printf("Rows Affected=%d\n", SQLERRD3);
- strcpy(stmt, "Non-Select statement");
- displayca();
- }
- }
- }
-
- void select_statement(void)
- {
- mysqlda->sqln = mysqlda->sqld;
- EXEC SQL PREPARE st2 INTO :mysqlda FROM :stmtbuf;
- printf("Col Type Len Name \n");
- mysqlda->sqldabc = SQLDASIZE(mysqlda->sqld);
- // for (index1 = 1; index1 > mysqlda->sqld; index1++)
- for (index1 = 0; index1 < mysqlda->sqld; index1++)
- initda();
- EXEC SQL OPEN cur;
- if (SQLCODE != 0)
- displayca();
- else
- EXEC SQL FETCH cur USING DESCRIPTOR :mysqlda;
- strcpy(stmt, "Fetch the first row");
- while ((SQLCODE >= 0) && (SQLCODE != 100))
- fetch_data();
- EXEC SQL CLOSE cur;
- }
-
- void initda()
- {
- colno = index1;
- strcpy(colname, mysqlda->sqlvar[index1].sqlname.data);
- coltype = mysqlda->sqlvar[index1].sqltype;
- collen = mysqlda->sqlvar[index1].sqllen;
- printf("%d %d %d %s\n", colno, coltype, collen, colname);
- remain_1 = 0;
- sql_div = div(mysqlda->sqlvar[index1].sqltype,2);
- sql_type = coltype;
- remain_1 = sql_div.rem;
- dyn_vars[index1].dyn_null = 0;
- if (remain_1 != 0)
- {
- sql_type = sql_type - 1;
- mysqlda->sqlvar[index1].sqlind = &dyn_vars[index1].dyn_null;
- }
-
- switch (sql_type)
- {
- case 500: mysqlda->sqlvar[index1].sqldata = (unsigned char *)&dyn_vars[index1].smallint_var;
- break;
- case 496: mysqlda->sqlvar[index1].sqldata = (unsigned char *)&dyn_vars[index1].integer_var;
- break;
- case 480: mysqlda->sqlvar[index1].sqldata = (unsigned char *)&dyn_vars[index1].lfloat_var;
- break;
- case 482: mysqlda->sqlvar[index1].sqldata = (unsigned char *)&dyn_vars[index1].sfloat_var;
- break;
- case 484: mysqlda->sqlvar[index1].sqldata = (unsigned char *)&dyn_vars[index1].decimal_var;
- break;
- case 452: mysqlda->sqlvar[index1].sqldata = (unsigned char *)&dyn_vars[index1].char_var;
- break;
- case 392: mysqlda->sqlvar[index1].sqldata = (unsigned char *)&dyn_vars[index1].char_var;
- break;
- }
- }
-
- void fetch_data(void)
- {
- int i;
-
- index1 = 0;
- for (i = 0; i < mysqlda->sqld; i++)
- displayda_2();
- displayca();
- EXEC SQL FETCH cur USING DESCRIPTOR :mysqlda;
- strcpy(stmt, "Fetch the next row");
- }
-
- void displayda_2(void)
- {
- index1++;
- strcpy(colname, mysqlda->sqlvar[index1].sqlname.data);
- printf("%s:", colname);
- if (dyn_vars[index1].dyn_null < 0)
- printf("* * * NULL * * *\n");
- else
- switch (mysqlda->sqlvar[index1].sqltype)
- {
- case 500:
- case 501: printf("%d\n", dyn_vars[index1].smallint_var);
- break;
- case 496:
- case 497: printf("%ld\n", dyn_vars[index1].integer_var);
- break;
- case 480:
- case 481: printf("%lf\n", dyn_vars[index1].lfloat_var);
- break;
- case 482:
- case 483: printf("%f\n", dyn_vars[index1].sfloat_var);
- break;
- case 484:
- case 485: printf("%lf\n", dyn_vars[index1].decimal_var);
- break;
- case 452:
- case 453:
- case 392:
- case 393: printf("%s\n", dyn_vars[index1].char_var);
- break;
- }
- }
-
- void displayca(void)
- {
- if ((SQLCODE != 0) && (SQLCODE != 1))
- printf("SQLCODE=%d\n", SQLCODE);
- if (SQLCODE == -1)
- printf("message=%Fs\n", SQLERRMC);
-
- printf("SQLERRD[1]=%d\tSQLERRD[2]=%d\n",SQLERRD1, SQLERRD2);
- }
-