home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Internet Business Development Kit / PRODUCT_CD.iso / sqlsvr / esqlc / esql / samples.c / example8.sqc < prev    next >
Encoding:
Text File  |  1995-11-21  |  8.5 KB  |  316 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  FILE: example8.sqc
  4. //              
  5. //      Sample Embedded SQL for C application
  6. //
  7. //  FUNCTIONS:
  8. //
  9. //      main() - Main program
  10. //      ErrorHandler - Embedded SQL for C error handler
  11. //
  12. //  COMMENTS:
  13. //
  14. //      This program is a primitive ISQL.EXE clone.  It basically loops in the
  15. //      doit procedure until the user enters "quit".  When it receives an SQL
  16. //      statement from the user, it prepares it and calls select-statement which
  17. //      opens a cursor and fetches the results.  The rest of the code deals with
  18. //      displaying the data and any error messages.
  19. //
  20. //      Copyright (C) 1992 - 1994 Microsoft Corporation
  21. //
  22. ///////////////////////////////////////////////////////////////////////////////
  23.  
  24. // function prototypes (instead of header file)
  25. void ErrorHandler (void);
  26. void doit(void);
  27. void select_statement(void);
  28. void initda(void);
  29. void fetch_data(void);
  30. void displayda_2(void);
  31. void displayca(void);
  32.  
  33. #include <stddef.h>         // standard C run-time header
  34. #include <stdio.h>          // standard C run-time header
  35. #include <stdlib.h>         // standard C run-time header
  36. #include "gcutil.h"         // utility header
  37.  
  38. // GLOBAL VARIABLES
  39.  
  40. #define MAX_RETCOLS 20
  41.  
  42. struct
  43. {
  44.     short  dyn_null;
  45.     int    smallint_var;
  46.     long   integer_var;
  47.     double decimal_var;
  48.      float  sfloat_var;
  49.      double  lfloat_var;
  50.     char   char_var[51];
  51. } dyn_vars[255];
  52.  
  53. struct sqlda *mysqlda;
  54.  
  55. div_t sql_div;
  56. int sql_type;
  57. int remain_1;
  58. int index1;
  59. char stmt[81];
  60.  
  61. EXEC SQL BEGIN DECLARE SECTION;
  62. char stmtbuf[81];
  63. char dbname[33];
  64. char uname[33];
  65. EXEC SQL END DECLARE SECTION;
  66.  
  67. EXEC SQL DECLARE cur CURSOR FOR st2;
  68.  
  69. int  colno;
  70. int  coltype;
  71. int  collen;
  72. char colname[31];
  73.  
  74. ///////////////////////////////////////////////////////////////////////////////
  75. //
  76. //  FUNCTION: main()
  77. //
  78. //      Main application
  79. //
  80. //  PARAMETERS:
  81. //
  82. //      argc - count of command line args
  83. //      argv - array of command line argument strings
  84. //      envp - array of environment strings
  85. //
  86. //  RETURNS: 0 if successful, 1 if error
  87. //
  88. //  COMMENTS:
  89. //
  90. ///////////////////////////////////////////////////////////////////////////////
  91.  
  92. int main (
  93.     int argc,
  94.     char** argv,
  95.     char** envp)
  96. {
  97.     int nRet;           // for return values
  98.  
  99.     EXEC SQL BEGIN DECLARE SECTION;
  100.     // for CONNECT TO
  101.     char szServerDatabase[(SQLID_MAX * 2)+2] = "";
  102.     char szLoginPassword[(SQLID_MAX * 2)+2] = "";
  103.     EXEC SQL END DECLARE SECTION;
  104.  
  105.     // install Embedded SQL for C error handler
  106.     EXEC SQL WHENEVER SQLERROR CALL ErrorHandler();
  107.     // set Embedded SQL for C options
  108.     EXEC SQL SET OPTION LOGINTIME 10;
  109.     EXEC SQL SET OPTION QUERYTIME 100;
  110.  
  111.     // display logo
  112.     printf("Sample Embedded SQL for C application\n");
  113.  
  114.     // get info for CONNECT TO statement
  115.     nRet = GetConnectToInfo(argc, argv,
  116.         szServerDatabase,
  117.         szLoginPassword);
  118.     if (!nRet)
  119.     {
  120.         return (1);
  121.     }
  122.  
  123.     // attempt connection to SQL Server
  124.     EXEC SQL CONNECT TO :szServerDatabase
  125.         USER :szLoginPassword;
  126.     if (SQLCODE == 0)
  127.     {
  128.         printf("Connection to SQL Server established\n");
  129.     }
  130.     else
  131.     {
  132.         // problem connecting to SQL Server
  133.         printf("ERROR: Connection to SQL Server failed\n");
  134.         return (1);
  135.     }
  136.  
  137.     EXEC SQL SET CURSORTYPE CUR_BROWSE;
  138.  
  139.     mysqlda = malloc(SQLDASIZE(MAX_RETCOLS));
  140.     if (mysqlda == NULL)
  141.     {
  142.         printf("Unable to allocate memory for SQLDA structure.\n");
  143.         return 0;
  144.     }
  145.  
  146.     while (strcmp(stmtbuf, "quit") != 0)
  147.         doit();
  148.  
  149.     // disconnect from SQL Server
  150.     EXEC SQL DISCONNECT ALL;
  151.  
  152.     return (0);
  153. }
  154.  
  155. ///////////////////////////////////////////////////////////////////////////////
  156. //
  157. //  FUNCTION: ErrorHandler()
  158. //
  159. //      Called on Embedded SQL for C error, displays fields from SQLCA
  160. //
  161. //  PARAMETERS: none
  162. //
  163. //  RETURNS: none
  164. //
  165. //  COMMENTS:
  166. //
  167. ///////////////////////////////////////////////////////////////////////////////
  168.  
  169. void ErrorHandler (void)
  170. {
  171.     // display error information from SQLCA
  172.     printf("Error Handler called:\n");
  173.     printf("    SQL Code = %li\n", SQLCODE);
  174.     printf("    SQL Server Message %li: '%Fs'\n", SQLERRD1, SQLERRMC);
  175. }
  176.  
  177. void doit(void)
  178. {
  179.     printf("Enter SQL or 'quit' to terminate: ");
  180.     gets(stmtbuf);
  181.     if (strcmp(stmtbuf, "quit") == 0)
  182.         exit(0);
  183.     mysqlda->sqln = MAX_RETCOLS;
  184.     mysqlda->sqld = 0;
  185.     mysqlda->sqldabc = SQLDASIZE(MAX_RETCOLS);
  186.  
  187.     EXEC SQL PREPARE st INTO :mysqlda FROM :stmtbuf;
  188.     strcpy(stmt, "Prepare statement");
  189.     if (mysqlda->sqld != 0)
  190.         select_statement();
  191.     else
  192.     {
  193.         EXEC SQL EXECUTE st;
  194.         if (SQLCODE != 0)
  195.             displayca();
  196.         else
  197.         {
  198.             printf("Rows Affected=%d\n", SQLERRD3);
  199.             strcpy(stmt, "Non-Select statement");
  200.             displayca();
  201.         }
  202.     }
  203. }
  204.  
  205. void select_statement(void)
  206. {
  207.     mysqlda->sqln = mysqlda->sqld;
  208.     EXEC SQL PREPARE st2 INTO :mysqlda FROM :stmtbuf;
  209.     printf("Col  Type Len  Name \n");
  210.     mysqlda->sqldabc = SQLDASIZE(mysqlda->sqld);
  211. //  for (index1 = 1; index1 > mysqlda->sqld; index1++)
  212.     for (index1 = 0; index1 < mysqlda->sqld; index1++)
  213.         initda();
  214.     EXEC SQL OPEN cur;
  215.     if (SQLCODE != 0)
  216.         displayca();
  217.     else
  218.         EXEC SQL FETCH cur USING DESCRIPTOR :mysqlda;
  219.     strcpy(stmt, "Fetch the first row");
  220.     while ((SQLCODE >= 0) && (SQLCODE != 100))
  221.         fetch_data();
  222.     EXEC SQL CLOSE cur;
  223. }
  224.  
  225. void initda()
  226. {
  227.     colno = index1;
  228.     strcpy(colname, mysqlda->sqlvar[index1].sqlname.data);
  229.     coltype = mysqlda->sqlvar[index1].sqltype;
  230.     collen = mysqlda->sqlvar[index1].sqllen;
  231.     printf("%d %d %d %s\n", colno, coltype, collen, colname);
  232.     remain_1 = 0;
  233.     sql_div = div(mysqlda->sqlvar[index1].sqltype,2);
  234.     sql_type = coltype;
  235.     remain_1 = sql_div.rem;
  236.     dyn_vars[index1].dyn_null = 0;
  237.     if (remain_1 != 0)
  238.     {
  239.         sql_type = sql_type - 1;
  240.         mysqlda->sqlvar[index1].sqlind = &dyn_vars[index1].dyn_null;
  241.     }
  242.  
  243.     switch (sql_type)
  244.     {
  245.         case 500: mysqlda->sqlvar[index1].sqldata = (unsigned char *)&dyn_vars[index1].smallint_var;
  246.             break;
  247.         case 496: mysqlda->sqlvar[index1].sqldata = (unsigned char *)&dyn_vars[index1].integer_var;
  248.             break;
  249.         case 480: mysqlda->sqlvar[index1].sqldata = (unsigned char *)&dyn_vars[index1].lfloat_var;
  250.             break;
  251.         case 482: mysqlda->sqlvar[index1].sqldata = (unsigned char *)&dyn_vars[index1].sfloat_var;
  252.             break;
  253.         case 484: mysqlda->sqlvar[index1].sqldata = (unsigned char *)&dyn_vars[index1].decimal_var;
  254.             break;
  255.         case 452: mysqlda->sqlvar[index1].sqldata = (unsigned char *)&dyn_vars[index1].char_var;
  256.             break;
  257.         case 392: mysqlda->sqlvar[index1].sqldata = (unsigned char *)&dyn_vars[index1].char_var;
  258.             break;
  259.     }
  260. }
  261.  
  262. void fetch_data(void)
  263. {
  264. int i;
  265.  
  266.     index1 = 0;
  267.     for (i = 0; i < mysqlda->sqld; i++)
  268.         displayda_2();
  269.     displayca();
  270.     EXEC SQL FETCH cur USING DESCRIPTOR :mysqlda;
  271.     strcpy(stmt, "Fetch the next row");
  272. }
  273.  
  274. void displayda_2(void)
  275. {
  276.     index1++;
  277.     strcpy(colname, mysqlda->sqlvar[index1].sqlname.data);
  278.         printf("%s:", colname);
  279.     if (dyn_vars[index1].dyn_null < 0)
  280.         printf("* * * NULL * * *\n");
  281.     else
  282.         switch (mysqlda->sqlvar[index1].sqltype)
  283.         {
  284.             case 500:
  285.             case 501: printf("%d\n", dyn_vars[index1].smallint_var);
  286.                 break;
  287.             case 496:
  288.             case 497: printf("%ld\n", dyn_vars[index1].integer_var);
  289.                 break;
  290.             case 480:
  291.             case 481: printf("%lf\n", dyn_vars[index1].lfloat_var);
  292.                 break;
  293.             case 482:
  294.             case 483: printf("%f\n", dyn_vars[index1].sfloat_var);
  295.                 break;
  296.             case 484:
  297.             case 485: printf("%lf\n", dyn_vars[index1].decimal_var);
  298.                 break;
  299.             case 452:
  300.             case 453:
  301.             case 392:
  302.             case 393: printf("%s\n", dyn_vars[index1].char_var);
  303.                 break;
  304.         }
  305. }
  306.  
  307. void displayca(void)
  308. {
  309.     if ((SQLCODE != 0) && (SQLCODE != 1))
  310.         printf("SQLCODE=%d\n", SQLCODE);
  311.     if (SQLCODE == -1)
  312.         printf("message=%Fs\n", SQLERRMC);
  313.  
  314.     printf("SQLERRD[1]=%d\tSQLERRD[2]=%d\n",SQLERRD1, SQLERRD2);
  315. }
  316.