home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: InfoMgt / InfoMgt.zip / genmb108.zip / dbtest.nrx < prev    next >
Text File  |  1996-12-18  |  7KB  |  235 lines

  1. /* A simple netrexx program to test RXDbase. Forza Juve! */
  2.  
  3. options binary
  4. import RXDbase
  5.  
  6. /* Here lies the variables declaration */
  7.  
  8. dbMine = RXDbase() /* instance which will handle the db */
  9.  
  10. rRec = Rexx[3,3]   /* Used to store the record's structure - 
  11.                       the first '3' means that we have 2 fields here,
  12.                       and the second is a fixed index (you always will
  13.                       want to put '3' as the second index) */
  14.  
  15. rRec2 = Rexx[2]    /* Used to handle (un)delete - we will delete only 1 item */
  16. rArr = Rexx[3,3]   /* Used to store data to be written in the archive */
  17. rArr2 = Rexx[,]    /* Used to retrieve data from the archive (could have done
  18.                       it with rArr as well, but did this way for clarity) */
  19. rFilter = Rexx[5]
  20. rStats = Rexx[4]   /* Used to gather stats on the db */
  21. rTemp = Rexx ''    /* Sorry, I got short of names */
  22. rTemp2 = Rexx ''
  23. rTemp3 = Rexx ''
  24. rFileName = Rexx null /* The DB name */
  25. iCount = int 0        /* counters used in the program */
  26. iCount2 = int 0
  27.  
  28.  
  29. /*
  30.    From now on, the body of the example.
  31.    If you get depressed learning by example, refer
  32.    to the docs. Life is wonderful.
  33. */
  34.  
  35. rFileName = 'db'   /* Name of the db - I got imagination, heh? */
  36. rRec[0,0] = 2        /* Number of fields */
  37. rRec[1,0] = 12       /* The first field has size.. */
  38. rRec[1,1] = 'Name'   /* The first field has name.. */
  39. rRec[1,2] = 'ISAM10'   /* The indexing method: ISAM, accepting NULLs, but not duplicates */
  40. rRec[2,0] = 12       /* The second field has size.. */
  41. rRec[2,1] = 'Surname'/* The second field has name.. */
  42. rRec[2,2] = 'ISAM01'   /* The indexing method: Acceptin duplicates, but not NULLs */
  43.  
  44.  
  45.  
  46.  
  47. /* 
  48.   Delete previous db - it will report ERROR:FILE ACCESS PROBLEMS if no
  49.   db is found, but it's not a real problem.
  50. */
  51. say 'Previous db deletion reported: 'dbMine.deletedb('db')
  52.  
  53.  
  54.  
  55.  
  56.  
  57. /* First of all, let's connect to the database */
  58. say 'After the connection the DB is: 'dbMine.connect(rFileName, rRec)
  59.  
  60.  
  61.  
  62.  
  63.  
  64. rFilter[0] = 2
  65. rFilter[1] = 'Name = M*'
  66. rFilter[2] = 'Surname <> co*w'
  67.  
  68. say 'After defining a filter: 'dbMine.definefilter(rFilter)
  69.  
  70.  
  71.  
  72.  
  73.  
  74. /* Now, let's write a couple of records */
  75. rArr[0,0] = 2              /* Number of records to be saved on disk */
  76. rArr[1,1] = 'Max'          /* First field of the first record. */
  77. rArr[2,1] = 'Marsiglietti' /* Second field of the first record. */
  78. rArr[1,2] = 'Mike'         /* First field of the second record. */
  79. rArr[2,2] = 'Cowlishaw'    /* Second field of the second record. */
  80. say 'The write operation reported: 'dbMine.saverec(rArr, "OVERWRITE")
  81.  
  82.  
  83.  
  84.  
  85.  
  86. say 'Now retrieving data sorted by name:'
  87. /* Retrieve those records and display them on screen */
  88. rArr2 = dbMine.loadrec("Name", "all", "1", 1)
  89. /*
  90.    The former line uses "name" as the index.
  91.    Also, "1" means 1st record in the specified index.
  92. */
  93.  
  94. say ' '
  95.  
  96. /*
  97.    Let's build the top row of the db dump.
  98. */
  99.  
  100. rTemp3 = ' IDX Number.REC Number .' 
  101. loop iCount = 1 to rRec[0,0]
  102.  rTemp3 = rTemp3 || (rRec[iCount,1]).right(rRec[iCount,0],' ') || '.'
  103. end
  104. say rTemp3
  105.  
  106. /*
  107.    A little reminder: (useful for the following babele) 
  108.  
  109.     GENERIC LOADREC RETURN:
  110.  
  111.     rArr2      = where records have been stored by loadrec.
  112.     rArr2[0,0] = how many records were returned? Here you know.
  113.     rArr2[0,1] = how many ACTIVE (not 'N') records were returned? (Not used here)
  114.     rArr2[n,m] = field 'n' of record 'm'; n,m start from 1.
  115.  
  116.     If 'z' is the latest field:
  117.     rArr2[z+1, m] = status of the m-th record: active? ('Y' or 'N')
  118.     rArr2[z+2, m] = record position in the database
  119.     rArr2[z+3, m] = record position in the index used by the just issued loadrec
  120.  
  121.     INFOS ON THE ARCHIVE:
  122.  
  123.     rRec       = structure of the archive.
  124.     rRec[0,0]  = number of fields.
  125.     rRec[n,0]  = length of the n-th field.
  126.     rRec[n,1]  = name of the n-th field.
  127.     rRec[n,2]  = indexing of the n-th field (NONE, ISAM, B+TREE). B+TREE Not yet implemented.
  128. */
  129.  
  130. if rArr2[0, 0] > 0 then
  131. do
  132.  loop iCount = 1 to rArr2[0,0]
  133.   if rArr2[3, iCount] \='N' then  /* If it's 'N' then it's deleted (shouldn't be any) */
  134.   rTemp2 = rArr2[rArr2[0,0]+3, iCount] /* IDX position */
  135.   rTemp2 = rTemp2.right(11, ' ')
  136.   rTemp = rTemp2 || '.' || (rArr2[4, iCount]).right(11, ' ') || '.' /* 4=REC pos */
  137.   loop iCount2 = 1 to rRec[0,0]
  138.    rTemp = rTemp || (rArr2[iCount2, iCount]).left(rRec[iCount2, 0], ' ') || '.' 
  139.   end
  140.   say rTemp
  141.  end
  142. end
  143.  
  144.  
  145.  
  146.  
  147.  
  148. say '\nNow deleting Mike!! (haha)'
  149. rRec2[0]=1 /* How many rec to delete/undelete? */
  150. rRec2[1]=2 /* Which one to delete/undelete? */
  151. dbMine.delerec(rRec2, 1) /* 1 = Delete it */
  152.  
  153.  
  154.  
  155.  
  156. rStats = dbMine.getdbstats()
  157. say 'DB size: '||rStats[0]||' Record size: '||rStats[1]||' Records: '||rStats[2]||' Active records: '||rStats[3]
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164. /* Retrieve those records and display them on screen */
  165. rArr2 = dbMine.loadrec("Name", "all", "1", 1)
  166.  
  167. say ' '
  168. say rTemp3 -- write the header that we built before.
  169.  
  170. if rArr2[0, 0] > 0 then
  171. do
  172.  loop iCount = 1 to rArr2[0,0]
  173.   if rArr2[3, iCount] \='N' then  /* If it's 'N' then it's deleted */
  174.   rTemp2 = rArr2[rArr2[0,0]+3, iCount] /* IDX position */
  175.   rTemp2 = rTemp2.right(11, ' ')
  176.   rTemp = rTemp2 || '.' || (rArr2[4, iCount]).right(11, ' ') || '.'
  177.   loop iCount2 = 1 to rRec[0,0]
  178.    rTemp = rTemp || (rArr2[iCount2, iCount]).left(rRec[iCount2, 0], ' ') || '.' 
  179.   end
  180.   say rTemp
  181.  end
  182. end
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189. say '\nNow un-deleting Mike, changing Max to Massimiliano,'
  190. say 'and retrieving elements sorted by surname:'
  191.  
  192. rRec2[0]=1 /* How many rec to delete/undelete? */
  193. rRec2[1]=2 /* Which one to delete/undelete? */
  194. dbMine.delerec(rRec2, 0) /* 0 = Undelete it */
  195.  
  196.  
  197.  
  198.  
  199.  
  200. rArr[0,0] = 1              /* Number of records to be modified */
  201. rArr[0,1] = 1              /* Where has this record to be put? */
  202. rArr[1,1] = 'Massimiliano' /* First field of the first record. */
  203. rArr[2,1] = 'Marsiglietti' /* Second field of the first record. */
  204. dbMine.modirec(rArr)
  205.  
  206.  
  207.  
  208. /* Retrieve those records and display them on screen */
  209. rArr2 = dbMine.loadrec("Surname", "all", "1", 1)
  210.  
  211. say ' '
  212. say rTemp3 -- write the header that we built before.
  213.  
  214. if rArr2[0, 0] > 0 then
  215. do
  216.  loop iCount = 1 to rArr2[0,0]
  217.   if rArr2[3, iCount] \='N' then  /* If it's 'N' then it's deleted */
  218.   rTemp2 = rArr2[rArr2[0,0]+3, iCount] /* IDX position */
  219.   rTemp2 = rTemp2.right(11, ' ')
  220.   rTemp = rTemp2 || '.' || (rArr2[4, iCount]).right(11, ' ') || '.'
  221.   loop iCount2 = 1 to rRec[0,0]
  222.    rTemp = rTemp || (rArr2[iCount2, iCount]).left(rRec[iCount2, 0], ' ') || '.' 
  223.   end
  224.   say rTemp
  225.  end
  226. end
  227.  
  228.  
  229.  
  230.  
  231.  
  232. /* We just got enough */
  233. say '\nDisconnection returned: 'dbMine.disconnect()
  234.  
  235.