home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / rxhll.zip / DATABASE.REX < prev    next >
OS/2 REXX Batch file  |  1994-01-26  |  5KB  |  214 lines

  1.  
  2. /* #include <database.rex> */
  3.  
  4. /**
  5. *** ╔═══════════════════════════════════════════════════════════════════════╗
  6. *** ║ OS/2 Database Manager & DB2/2 Support routines                        ║
  7. *** ╚═══════════════════════════════════════════════════════════════════════╝
  8. **/
  9.  
  10.  
  11. LoadDBMFunctions: procedure
  12.    /**
  13.    ***  Load the Database Manager REXX DLL
  14.    **/
  15.  
  16.    if RxFuncQuery('SQLEXEC') <> 0 then
  17.       do
  18.       RCode = RxFuncAdd('SQLEXEC','SQLAR','SQLEXEC')
  19.       if RCode <> 0 then
  20.          call Error 1001,0,RCode
  21.       end
  22.  
  23.    if RxFuncQuery('SQLDBS') <> 0 then
  24.       do
  25.       RCode = RxFuncAdd('SQLDBS', 'SQLAR', 'SQLDBS')
  26.       if RCode <> 0 then
  27.          call Error 1001,0,RCode
  28.       end
  29.    return
  30.  
  31.  
  32. StartDatabase: procedure
  33.    /**
  34.    ***  This will start the Database Manager and open the database with
  35.    ***  the name that was passed
  36.    **/
  37.  
  38.    arg Database
  39.  
  40.    call sqldbs 'start database manager'
  41.    if Database <> '' then
  42.       call sqldbs 'start using database' Database
  43.    return result
  44.  
  45.  
  46. StopDatabase: procedure
  47.    /**
  48.    ***  This will stop the use of the current database.
  49.    **/
  50.  
  51.    call sqldbs 'stop using database'
  52.    return result
  53.  
  54.  
  55. SqlDynamic: procedure expose MsgQ.
  56.    /**
  57.    ***  This will upadate a record in the database
  58.    **/
  59.  
  60.    parse arg SqlCommand
  61.  
  62.    call SqlExec "EXECUTE IMMEDIATE :SqlCommand"
  63.    if result <> 0 then
  64.       call Error 1002,0,result
  65.    else
  66.       select
  67.          when SQLCA.SQLCODE =   0 then nop /* Ok         */
  68.          when SQLCA.SQLCODE = 100 then nop /* Not found. */
  69.          otherwise
  70.             call Error 1003,0,SQLCA.SQLCODE,SQLCA.SQLMSG
  71.       end /* select */
  72.    return SQLCA.SQLCODE
  73.  
  74.  
  75. SqlCommit: procedure  expose MsgQ.
  76.    /**
  77.    ***  This will commit the changes to the database
  78.    **/
  79.  
  80.    SqlCommit = 'COMMIT'
  81.    call SqlExec "EXECUTE IMMEDIATE :SqlCommit"
  82.    return SQLCA.SQLCODE
  83.  
  84.  
  85. SqlGetData: procedure expose sqlda.
  86.    /**
  87.    ***  This will get the information from the sqlda structure while looking
  88.    ***  for NULL values.
  89.    **/
  90.  
  91.    arg j
  92.  
  93.    if sqlda.j.sqlind < 0 then
  94.       Data = ""
  95.    else
  96.       Data = strip(sqlda.j.sqldata)
  97.    return Data
  98.  
  99.  
  100. Sql:
  101.    /**
  102.    ***  This will issue the SqlExec API call and check the return codes and
  103.    ***  results.  It will terminate on error.
  104.    **/
  105.  
  106.    arg SqlCommand
  107.  
  108.    call SqlExec SqlCommand
  109.    if result <> 0 then
  110.       call Error 1002,0,result
  111.    else
  112.       select
  113.          when SQLCA.SQLCODE =   0 then nop /* Ok         */
  114.          when SQLCA.SQLCODE = 100 then nop /* Not found. */
  115.          otherwise
  116.             call Error 1003,0,SQLCA.SQLCODE,SQLCA.SQLMSG
  117.       end /* select */
  118.    return SQLCA.SQLCODE
  119.  
  120.  
  121. DbMakeValue: procedure
  122.    /**
  123.    ***  This will cleanup of the information from the windows to make sure
  124.    ***  they are valid for the database.
  125.    **/
  126.  
  127.    parse arg field, type
  128.  
  129.    if field = '' then
  130.       return "NULL"
  131.  
  132.    type = translate(type)
  133.    select
  134.       when left(type, 1) = 'C' then
  135.          return "'"DbDoubleApostrophe(field)"'"
  136.       when left(type, 1) = 'N' then
  137.          return field
  138.       otherwise
  139.          return field
  140.    end /* select */
  141.    return
  142.  
  143.  
  144. DbDate: procedure
  145.    /**
  146.    ***  This will return today's date in Database Manager date format
  147.    **/
  148.  
  149.    today = date('Sorted')
  150.    parse var today yyyy 5 mm 7 dd
  151.    today = mm'-'dd'-'yyyy
  152.    return today
  153.  
  154.  
  155. DbDoubleApostrophe: procedure
  156.    /**
  157.    ***  This will convert a single apostrophe in a string to double
  158.    ***  apostrophe's so that it is correctly used in the SQL calls
  159.    **/
  160.  
  161.    parse arg Field
  162.  
  163.    Start = 1
  164.    Found = pos("'", Field, Start)
  165.    do while Found > 0
  166.       Field = insert("'", Field, Found)
  167.       Start = Found + 2
  168.       Found = pos("'", Field, Start)
  169.    end
  170.    return Field
  171.  
  172.  
  173. DbValidNum: procedure
  174.    /**
  175.    ***  This will verify that the number passed is a valid number or null
  176.    **/
  177.  
  178.    arg number
  179.  
  180.    if number = ''              then return 1
  181.    if datatype(number,'N') = 0 then return 0
  182.    return 1
  183.  
  184.  
  185. DbValidDate: procedure
  186.    /**
  187.    ***  This will return a boolean based on whether this is a valid date
  188.    ***  for a database record
  189.    **/
  190.  
  191.    parse arg CheckDate .
  192.  
  193.    parse var CheckDate mm '-' dd '-' yyyy
  194.  
  195.    if CheckDate = mm then
  196.       parse var CheckDate mm '/' dd '/' yyyy
  197.  
  198.    /* A NULL is a valid date in this case */
  199.  
  200.    if mm = '' then
  201.       return 1
  202.  
  203.    if datatype(mm  ,'N') = 0 then return 0
  204.    if datatype(dd  ,'N') = 0 then return 0
  205.    if datatype(yyyy,'N') = 0 then return 0
  206.  
  207.    if (mm < 1) | (mm > 12) then
  208.       return 0
  209.    if (dd < 1) | (dd > 31) then
  210.       return 0
  211.    if (yyyy < 1990) | (yyyy > 1999) then
  212.       return 0
  213.    return 1
  214.