home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / sybaserx.zip / SQLCMD.CMD < prev    next >
OS/2 REXX Batch file  |  1995-12-11  |  6KB  |  178 lines

  1. /*******************************************************************/
  2. /*                                                                 */
  3. /* SQLCMD.CMD                                                      */
  4. /*                                                                 */
  5. /* This program calls the various REXX external functions          */
  6. /* provided in the SYBREXX.C function package.                     */
  7. /*                                                                 */
  8. /* Each function is called once to illustrate how it is used.      */
  9. /* Some of the functions have multiple options and multiple        */
  10. /* types of output, but not all options are used here.             */
  11. /*                                                                 */
  12. /*******************************************************************/
  13. '@echo off'
  14.  
  15. Arg SQLCMD
  16.  
  17. if SQLCMD == '' then do
  18.    Say 'Enter SQL command:'
  19.    Pull SQLCMD
  20. end
  21.  
  22. /* Modify these variables to suite your environment */
  23.  
  24. UserID = 'sa'
  25. Password = 'alicee'
  26. Database = 'IVI'
  27. Server = 'DUNX_NB'
  28.  
  29. /* RxSybLoadFuncs:  This function will load all the rest,           */
  30. /* so we do not have to make many calls to RxFuncAdd.               */
  31.  
  32. call RxFuncAdd 'SysSleep', 'RexxUtil', 'SysSleep'
  33. call RxFuncAdd 'RxSybLoadFuncs', 'SYBREXX', 'RxSybLoadFuncs'
  34. call RxSybLoadFuncs
  35.  
  36. /* Set return codes */
  37. SUCCEED = 1
  38. FAIL = 0
  39. NO_MORE_ROWS = -2
  40. MORE_ROWS = -1
  41. SYBCHAR = 47
  42. SYBDATETIME = 61
  43. SYBTEXT = 35
  44. TRUE = 1
  45. FALSE = 0
  46.  
  47. /*** Demonstrate RxDbsethandlers ***/
  48. Call RxDbsethandlers('')
  49.  
  50. /*** Demonstrate RxDbsetlogintime ***/
  51. Call RxDbsetlogintime('5')
  52.  
  53. /*** Demonstrate RxDbsettime ***/
  54. Call RxDbsettime('0')
  55.  
  56. pLoginRec = RxDblogin()                /* Get a Login Structure */
  57.  
  58. Call RxDbsetluser pLoginRec, UserID   /* Set the User ID in the Login Struct */
  59.  
  60. Call RxDbsetlpwd pLoginRec, Password  /* Set the Password in the Login Struct */
  61.  
  62. Call RxDbsetlhost pLoginRec, 'SYBREXX' /* Set the Host in the Login Struct */
  63.  
  64. Call RxDbsetlapp pLoginRec, 'SYBRX.CMD'/* Set the Applic in the Login Struct */
  65.  
  66. Call RxDbsetlencrypt pLoginRec, TRUE   /* Set password encryption */
  67.  
  68. dbproc = RxDbopen(pLoginRec, Server)   /* Open a DBPROCESS */
  69.  
  70. if (dbproc == 0) then do               /* Be sure to check we have a valid */
  71.     Say 'dbopen failed!'               /* DBPROCESS before continuing!     */
  72.     exit
  73. end
  74.  
  75. RetCode = RxDbuse(dbproc, Database)       /* Set the Database */
  76.  
  77.  
  78. /* Set up a SQL command string */
  79. RetCode = RxDbcmd(dbproc, SQLCMD)
  80.  
  81.  
  82. if (RxDbsqlexec(dbproc) <> SUCCEED) then do    /* Execute it              */
  83.     Say 'dbsqlexec failed!'                    /* Note that we could have */
  84.     Call RxDbclose(dbproc)                     /* used RxDbsqlsend        */
  85.     exit
  86. end
  87.  
  88. /*****************************************************************************/
  89. /* Would use this code if we had called RxDbsqlend()                         */
  90. /* Using OS/2 I would prefer to make the call in a separate thread and just  */
  91. /* let the thread wait until the call returns                                */
  92. /* It is a bit of a HACK polling to check if the call has returned yet and   */
  93. /* if it is done often from the main thread then some user interruption may  */
  94. /* be experienced (especially on low resource machines).                     */
  95. /*****************************************************************************/
  96. /* do while (RxDbsqlok(dbproc) <> SUCCEED)        Check if results are ready */
  97. /*     Call SysSleep 1                            would not be needed if we  */
  98. /*     Call RxDbclose(dbproc)                     called RxDbsqlexec         */
  99. /*    exit                                                                   */
  100. /* end                                                                       */
  101. /*****************************************************************************/
  102.  
  103. if (RxDbresults(dbproc) <> SUCCEED) then do    /* Check results */
  104.     Say 'dbresults failed!'
  105.     Call RxDbclose(dbproc)
  106.     exit
  107. end
  108.  
  109.  
  110. if (RxDbrows(dbproc) <> SUCCEED) then do       /* Check if rows are returned */
  111.     Say 'dbresults failed!'
  112.     Call RxDbclose(dbproc)
  113.     exit
  114. end
  115.  
  116.  
  117. /*** Write out the Titles ***/
  118. NumCols = RxDbnumcols(dbproc)
  119.  
  120. Outputline = ''
  121. OutputlineU = ''
  122.  
  123. do iCol = 1 to NumCols
  124.     ColType = RxDbcoltype(dbproc,iCol)             /* get the column type   */
  125.     if (ColType <> SYBCHAR & ColType <> SYBDATETIME & ColType <> SYBTEXT) then
  126.         ColLen.iCol = RxDbcollen(dbproc,iCol) * 2  /* get the column length */
  127.     else
  128.         ColLen.iCol = RxDbcollen(dbproc,iCol)      /* get the column length */
  129.  
  130.     ColName.iCol = RxDbcolname(dbproc, iCol)       /* get the column name   */
  131.  
  132.     Outputline = Outputline || substr(ColName.iCol,1,Collen.iCol,' ') || ' '
  133.     OutputlineU = OutputlineU || substr('',1,Collen.iCol, '-') || ' '
  134.  
  135. end
  136. Say Outputline
  137. Say OutputlineU
  138.  
  139.  
  140. do while (RxDbnextrow(dbproc) == MORE_ROWS)
  141.  
  142.     /*** Get the Data ***/
  143.     /* NOTE: At this stage RxDbdata only allocates 256 bytes for the returned */
  144.     /* data - I still need to make it smarter so that it will allocate        */
  145.     /* additional storage if required                                         */
  146.     /* This will probably only impact TEXT and BINARY columns                 */
  147.  
  148.     Outputline = ''
  149.  
  150.     do iCol = 1 to NumCols
  151.         Outputline = Outputline || substr(RxDbdata(dbproc, iCol),1,Collen.iCol,' ') || ' '
  152.     end
  153.  
  154.     Say Outputline
  155. end
  156.  
  157. Call RxDbclose(dbproc)        /* Close the DBPROCESS */
  158.  
  159.  
  160. Call RxSybDropFuncs
  161. exit
  162.  
  163.  
  164.  
  165. SysPause:
  166. parse arg prompt
  167.   if prompt='' then
  168.     prompt='Press Enter key when ready . . .'
  169.   call SysSay prompt
  170.   Pull .
  171.   say
  172. return
  173.  
  174. SysSay:
  175. parse arg string
  176.   call charout 'STDOUT', string
  177. return
  178.