home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a002 / 6.ddi / SAMPLE / APTFORMS / FSSQL_D.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-24  |  3.7 KB  |  175 lines

  1. #include <sybfront.h>
  2. #include <sybdb.h>
  3. #include <screenio.h>
  4. #include <sybfrs.h>
  5.  
  6. #if MSDOS
  7. #    include    "aforms.h"
  8. #endif /* MSDOS */
  9.  
  10. /*
  11. **    FSSQL DEMO
  12. **
  13. **    Run a form specified by command line arguments.  Execute 
  14. **    SQL text entered by the user and print any resulting data.
  15. */
  16.  
  17. extern   int      printit();
  18. extern   int      runsql();
  19.  
  20. static FSPROCEDURE procarray[] =           {
  21.     {"printit",  printit,  NULL,  SYB_C},
  22.     {"runsql",   runsql,   NULL,  SYB_C},
  23.     {NULL,       NULL,     NULL,  SYB_C},   };
  24.  
  25. main(argc, argv)
  26. int      argc;
  27. char     *argv[];
  28. {
  29.  
  30.     LOGINREC        *loginrec;
  31.     DBPROCESS       *dbproc;
  32.     FORM            *testform;
  33.     char            *formname;
  34.     char            *formvers;
  35.     POINTER         argarray[1];
  36.     char            buf[80];
  37.  
  38.     /* Initialize the terminal environment.  Fsopenscreen uses
  39.     ** non-NULL parameters if it is running on a bitmap terminal.
  40.     ** In other environments, non-NULL parameters are simply ignored.
  41.     */
  42.     fsopenscreen(&argc, argv, "FssqlDemo");
  43.  
  44.  
  45. #if BSD42 || VMS    
  46.     if (dbinit() == FAIL)
  47.         exit(ERREXIT);
  48. #endif /* BDS42 || VMS */
  49.     dbproc = fsdblogin(&loginrec, "Fssql Demo Login");
  50.  
  51.     /* Give the forms run-time system a LOGINREC and environment to use 
  52.     ** when opening up a new DBPROCESS for its own purposes. 
  53.     */
  54.     fsdbrec(loginrec);
  55.     fsdbenviron(dbproc);
  56.  
  57.     /* Get the form from the command line arguments. */
  58.     if (argc >= 3)
  59.     {
  60.         formname = argv[1];
  61.         formvers = argv[2];
  62.     }
  63.     else
  64.     {
  65.         formname = "basic";
  66.         formvers = "1";
  67.     }
  68.  
  69.     /* Load the form. */
  70.     if ( (testform = fsgetform(formname, formvers) ) == NULL )
  71.     {
  72.         sprintf(buf, "Unable to open form %s %s.", formname, formvers);
  73.         dbclose(dbproc);
  74.         dbexit();
  75.         fsabort(buf);
  76.     }
  77.  
  78.     /* Install the procedures and arguments. */
  79.     fsinstallproc(procarray);
  80.     argarray[0] = (POINTER) dbproc;
  81.     fsprocargs("runsql", argarray);
  82.  
  83.     /* Call the form. */
  84.  
  85.     fscallform(testform);
  86.  
  87.     dbclose(dbproc);
  88.     dbexit();
  89.     fsclosescreen();
  90.     exit(STDEXIT);
  91.   
  92. }
  93.  
  94.  
  95. /*
  96. **    RUNSQL
  97. **    Read user-entered SQL from the screen and execute it via fssql().
  98. **    This routine is triggered by menu processing.
  99. */
  100. runsql(context, argarray)
  101. FRSCONTEXT     *context;
  102. POINTER        *argarray[];
  103. {
  104. #define MYMSGSIZE 1024
  105.     FORM             *form;
  106.     DBPROCESS        *dbproc;
  107.     char             *val;
  108.     int              rows;
  109.     int              len;
  110.     char             buf[MYMSGSIZE];
  111.  
  112.     form = fscurform(context);
  113.     dbproc = (DBPROCESS *)argarray[0];
  114.  
  115.     /*
  116.     ** Get the SQL text from the screen, checking for empty text.
  117.     */
  118.     val = fsfreadval(form, "sqltext", -1, NULL, NULL, NULL);
  119.     if (val == NULL)
  120.     {
  121.         fsmessage("Please enter the SQL statement.");
  122.         return;
  123.     }
  124.  
  125.     /* Send the SQL text to the SQL Server. */
  126.     rows = fssql(dbproc, val);
  127.  
  128.     /* Print the SQL text resulting from field value
  129.     ** substitution -- the actual SQL sent to the SQL Server.
  130.     */
  131.     sprintf(buf, "SQL sent was\n");
  132.     len = strlen(buf);
  133.     dbstrcpy(dbproc, 0, MYMSGSIZE - len, buf + len);
  134.     if (strlen(buf + len)  != 0)
  135.     {
  136.         fsmessage(buf);
  137.     }
  138.  
  139.     /* Check the fssql() return code. */
  140.     if  (rows == -1)
  141.     {
  142.         /*
  143.         ** An error: fssql() printed any DB-LIBRARY errors that 
  144.         ** were not consumed by a DB-LIBRARY error handler.  You 
  145.         ** may want to put error handling code here.
  146.         */
  147.     }
  148.     else if (rows < -1)
  149.     {
  150.         fsmessage("Unable to show all of the rows of data.");
  151.     }
  152.     return;
  153. }
  154.  
  155.  
  156. /*
  157. **    PRINTIT
  158. **    Print the data on the form and present a message.
  159. **    This routine is triggered by menu processing.
  160. */
  161. printit(context, argarray)
  162. FRSCONTEXT      *context;
  163. POINTER         *argarray[];
  164. {
  165.     if (fsprint(fscurform(context), FRS_HEADER) == SUCCEED)
  166.     {
  167.         fsmessage("The data will be printed.");
  168.     }
  169.     else
  170.     {
  171.         fsmessage("Sorry, cannot print the data.");
  172.     }
  173.     return;
  174. }
  175.