home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / rexxgdb2.zip / Run-Exec.CMD < prev    next >
OS/2 REXX Batch file  |  1997-08-10  |  9KB  |  310 lines

  1. /* rexx */
  2. arg numrows .
  3. /*
  4. ------------------------------------------------------------------------
  5.                                 Run-EXEC
  6.                  DB2/2 Benchmark with REXX/Dynamic SQL
  7.     To insert 'numrows' rows to the STAFF table of the DB2/2 SAMPLE
  8.             Database & to read those back into REXX program
  9.  
  10.                    Utilizing IBM DB2/2 API (SQLEXEC)
  11.  
  12.                 Copyright (C) 1995 Global Automation Co.
  13.                           All Rights Reserved
  14.  
  15.                          Written By Simon Husin
  16.                          March - December, 1995
  17. ------------------------------------------------------------------------
  18.  
  19.          To use from an OS/2 Full Screen Command Prompt Screen:
  20.                 Type Run-EXEC 'numrows' and press Enter
  21.         where 'numrows' is number of rows to insert w/ ID > 1000
  22.        if not entered or invalid (<1 or > 9999), it is set to 100
  23. ------------------------------------------------------------------------
  24. */
  25. '@echo off'
  26. line = copies('-', 79)
  27. call ShowTitle
  28.  
  29. /*
  30. Validate 'numrows'
  31. */
  32. if numrows = '' then numrows = 100
  33. if datatype(numrows) \= 'NUM' then numrows = 100
  34. if numrows < 1 | numrows > 9999 then numrows = 100
  35. numrows = numrows % 1
  36.  
  37. /*
  38. Initialize all SQL statements to be prepared later in the program
  39. Also the SQLEXEC to execute those statements
  40. */
  41. /* SQL Select data from table Staff */
  42. sqlquery = 'SELECT ID,NAME,DEPT,JOB,YEARS,SALARY,COMM' ||,
  43.            '  FROM STAFF'                              ||,
  44.            ' WHERE ID > ?'
  45.  
  46. /* SQL Fetch C1 */
  47. sqlfetch = 'FETCH C1' ||,
  48.            ' INTO :ID,:NAME,:DEPT,:JOB,:YEARS:YEARSI,:SALARY,'||,
  49.            '      :COMM:COMMI'
  50.  
  51. /* SQL Insert w/ placeholders */
  52. sqlinsert = ' INSERT' ||,
  53.             '   INTO STAFF(ID,NAME,DEPT,JOB,YEARS,SALARY,COMM)'||,
  54.             '       VALUES( ?,   ?,   ?,  ?,    ?,     ?,   ?)'
  55.  
  56. /* SQL Execute S2 */
  57. sqlexins =  'EXECUTE S2 USING' ||,
  58.             '    :COUNT,:NAME,:DEPT,:JOB,:YEARS:YEARSI,:SALARY,'||,
  59.             '    :COMM:COMMI'
  60.  
  61. /* 2 SQL Select COUNTs in a UNION */
  62. sqlcount = 'SELECT COUNT(*)' ||,
  63.            '  FROM STAFF'    ||,
  64.            ' UNION '         ||,
  65.            'SELECT COUNT(*)' ||,
  66.            '  FROM STAFF'    ||,
  67.            ' WHERE ID > 1000'
  68.  
  69. /* SQL Delete created rows */
  70. sqldelete = 'DELETE FROM STAFF WHERE ID > 1000'
  71.  
  72. /*
  73. Attach REXX functions SQLDBS and SQLEXEC to access the Database 2 for
  74. OS/2 (DB2/2)
  75. */
  76. if rxfuncquery('SQLDBS') \= 0 then do
  77.    say 'Attaching REXX DB2/2 Database Manager APIs...'
  78.    call rxfuncadd 'SQLDBS', 'DB2AR', 'SQLDBS'
  79.    end
  80. if rxfuncquery('SQLEXEC') \= 0 then do
  81.    say 'Attaching REXX DB2/2 SQL APIs...'
  82.    call rxfuncadd 'SQLEXEC', 'DB2AR', 'SQLEXEC'
  83.    end
  84.  
  85. /* Activate DB2/2 via API*/
  86. say 'Starting Database Manager...'
  87. call SQLDBS 'START DATABASE MANAGER'
  88. if sqlca.sqlcode \= 0 &,
  89.    sqlca.sqlcode \= -1026 &,           /* DB2/2 is already active     */
  90.    sqlca.sqlcode \= -1063 then         /* STARTDBM was successful     */
  91.    call sqlerror 100
  92. /*
  93. ------------------------------------------------------------------------
  94. */
  95. call SQLEXEC 'CONNECT RESET'           /* disconnecting prev. connect.*/
  96. /*
  97. ------------------------------------------------------------------------
  98. */
  99. /* Open the database */
  100. say 'Connecting to Sample Database...'
  101. call time 'R'
  102. call SQLEXEC 'CONNECT TO SAMPLE IN SHARE MODE'
  103. elapse.connect = time('E')
  104.  
  105. /* Change Current Query Optimization to 0 */
  106. say 'Set Current Query Optimization to 0...'
  107. call time 'R'
  108. sql = 'SET CURRENT QUERY OPTIMIZATION 0'
  109. call SQLEXEC 'EXECUTE IMMEDIATE :sql'
  110. elapse.queryopt= time('E')
  111.  
  112. select
  113.         /*unit of work not done*/
  114.    when sqlca.sqlcode = -752 then call sqlerror -752
  115.         /*database not found*/
  116.    when sqlca.sqlcode = -1013 then call sqlerror -1013
  117.         /*db dir. not found*/
  118.    when sqlca.sqlcode = -1031 then call sqlerror -1031
  119.    when sqlca.sqlcode \= 0 then
  120.         say 'SQLCA.SQLCODE='sqlca.sqlcode
  121.    otherwise nop
  122.    end
  123. /*
  124. ------------------------------------------------------------------------
  125. */
  126. /* Prepare the query */
  127. CALL SQLEXEC 'PREPARE S1 FROM :SQLQUERY'
  128.  
  129. /* Declare cursor */
  130. CALL SQLEXEC 'DECLARE C1 CURSOR FOR S1'
  131.  
  132. /* Open cursor */
  133. startid = 0
  134. CALL SQLEXEC 'OPEN C1 USING :STARTID'
  135.  
  136. /* Fetch one row from the STAFF table */
  137. CALL SQLEXEC sqlfetch
  138. if sqlca.sqlcode \= 0 then
  139.    call sqlerror 101
  140.  
  141. /* Make corrections based on the NULL indicators */
  142. if yearsi = -1 then years = 0
  143. if commi = -1  then comm  = 0
  144.  
  145. /* Display the first (fetched) STAFF rows */
  146. say 'ID.......' id
  147. say 'NAME.....' name
  148. say 'DEPT.....' dept
  149. say 'JOB......' job
  150. say 'YEARS....' left(format(years, 2), 20) ||,
  151.                 '(NULL indicator='yearsi')'
  152. say 'SALARY...' format(salary, 6, 2)
  153. say 'COMM.....' left(format(comm, 6, 2), 20) ||,
  154.                 '(NULL indicator='commi')'
  155.  
  156. /* Close the cursor */
  157. CALL SQLEXEC 'CLOSE C1'
  158. /*
  159. ------------------------------------------------------------------------
  160. */
  161. /* Inserting 'NUMROWS' rows with a commit for every 100 inserts */
  162. maxid = numrows + 1000
  163.  
  164. say 'Inserting first Staff row' numrows' times w/ a commit @100 rows...'
  165. call Time 'R'
  166.  
  167. CALL SQLEXEC 'PREPARE S2 FROM :SQLINSERT'
  168.  
  169. /* Do 'numrows' inserts w/ commits every 100 times */
  170. do count = 1001 to maxid while sqlca.sqlcode = 0
  171.    CALL SQLEXEC sqlexins
  172.    if right(count, 2) \= '00' then iterate
  173.  
  174.    if count = maxid then leave
  175.  
  176.    CALL SQLEXEC 'COMMIT'
  177.    CALL SQLEXEC 'PREPARE S2 FROM :SQLINSERT'
  178.    end
  179.  
  180. if sqlca.sqlcode \= 0 then
  181.    call sqlerror 201
  182.  
  183. CALL SQLEXEC 'COMMIT'
  184. elapse.insert = time('E')
  185. /*
  186. ------------------------------------------------------------------------
  187. */
  188. /* Reading the 'NUMROWS' rows just inserted */
  189. say 'Reading all rows with ID > 1000 (into memory only)...'
  190. call Time 'R'
  191.  
  192. CALL SQLEXEC 'PREPARE S1 FROM :SQLQUERY'
  193.  
  194. startid = 1000
  195. CALL SQLEXEC 'OPEN C1 USING :STARTID'
  196.  
  197. do count = 1 until sqlca.sqlcode \= 0
  198.    CALL SQLEXEC sqlfetch
  199.    end
  200.  
  201. CALL SQLEXEC 'CLOSE C1'
  202. elapse.readnew = time('E')
  203.  
  204. say 'Number of rows read..:' count-1
  205. /*
  206. ------------------------------------------------------------------------
  207. */
  208. /* Reading rows from staff table using DB2/2 COUNT(*) and UNION */
  209. say 'Obtaining #rows w/ ID > 0 & 1000 w/ SQL COUNT & UNION...'
  210. call Time 'R'
  211.  
  212. CALL SQLEXEC 'PREPARE S2 FROM :SQLCOUNT'
  213. CALL SQLEXEC 'DECLARE C2 CURSOR FOR S2'
  214.  
  215. CALL SQLEXEC 'OPEN C2'
  216.  
  217. CALL SQLEXEC 'FETCH C2 INTO :COUNT'
  218.  
  219. do forever while sqlca.sqlcode = 0
  220.    say '- Found' count 'rows...'
  221.    CALL SQLEXEC 'FETCH C2 INTO :COUNT'
  222.    end
  223.  
  224. CALL SQLEXEC 'CLOSE C2'
  225.  
  226. elapse.count = time('E')
  227. /*
  228. ------------------------------------------------------------------------
  229. */
  230. /* Deleting all (new) rows */
  231. say 'Deleting all rows with ID > 1000...'
  232. call Time 'R'
  233. CALL SQLEXEC 'EXECUTE IMMEDIATE :SQLDELETE'
  234. elapse.delete = time('E')
  235. /*
  236. ------------------------------------------------------------------------
  237. */
  238. /* Committing the unit of works */
  239. say 'Committing the unit of works...'
  240. call Time 'R'
  241. CALL SQLEXEC 'COMMIT'
  242. elapse.commit = time('E')
  243. /*
  244. ------------------------------------------------------------------------
  245. */
  246. /* Close the database */
  247. say 'Disconnecting from Sample Database...'
  248. call time 'R'
  249. call SQLEXEC 'CONNECT RESET'
  250. elapse.disconnect = time('E')
  251. /*
  252. ------------------------------------------------------------------------
  253. */
  254. call ShowTitle
  255. tottime = 0
  256. tottime = tottime + elapse.connect
  257. tottime = tottime + elapse.queryopt
  258. tottime = tottime + elapse.insert
  259. tottime = tottime + elapse.readnew
  260. tottime = tottime + elapse.count
  261. tottime = tottime + elapse.delete
  262. tottime = tottime + elapse.commit
  263. tottime = tottime + elapse.disconnect
  264.  
  265. tottimefactor = tottime / 100
  266.  
  267. say line
  268. say left('Results', 20) right('Seconds', 12) right('Total %', 12)
  269. say line
  270. say left('Connect', 20) format(elapse.connect, 9, 2) format(elapse.connect / tottimefactor, 9, 2)
  271. say left('Query Opt.', 20) format(elapse.queryopt, 9, 2) format(elapse.queryopt / tottimefactor, 9, 2)
  272. say left('Insert', 20) format(elapse.insert, 9, 2) format(elapse.insert / tottimefactor, 9, 2)
  273. say left('Read New', 20) format(elapse.readnew, 9, 2) format(elapse.readnew / tottimefactor, 9, 2)
  274. say left('Count', 20) format(elapse.count, 9, 2) format(elapse.count / tottimefactor, 9, 2)
  275. say left('Delete', 20) format(elapse.delete, 9, 2) format(elapse.delete / tottimefactor, 9, 2)
  276. say left('Commit', 20) format(elapse.commit, 9, 2) format(elapse.commit / tottimefactor, 9, 2)
  277. say left('Disconnect', 20) format(elapse.disconnect, 9, 2) format(elapse.disconnect / tottimefactor, 9, 2)
  278. say line
  279. say left('Total', 20) format(tottime, 9, 2) format(tottime / tottimefactor, 9, 2)
  280. say line
  281. return 0
  282.  
  283.  
  284. ShowTitle:
  285. 'cls'
  286. say
  287. say 'RUN-EXEC --- Benchmarking REXX - DB2/2 Interface'
  288. say '             Using IBM DB2/2 REXX API (only)'
  289. say 'Copyright (C) 1995 Global Automation Co.'
  290. say 'All Rights Reserved'
  291. say
  292. return
  293.  
  294.  
  295. sqlerror:
  296. arg errorcode
  297. say
  298. say 'SQL/DBS Related errors detected.'
  299. say 'ERRORCODE  =' errorcode
  300. say 'RESULT     =' result
  301. say 'SQLCODE    =' sqlca.sqlcode
  302. say 'SQLSTATE   =' sqlca.sqlstate
  303.  
  304. CALL SQLDBS 'GET MESSAGE INTO :MSG'
  305. say 'SQLDBS MSG ='msg
  306.  
  307. say 'Program terminated!'
  308.  
  309. exit 0
  310.