home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a006 / 1.ddi / PASSAMP.ZIP / SEARCH.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1991-03-11  |  6.0 KB  |  327 lines

  1. {$M 20000,0,655360}
  2. {$DEFINE SearchMain}
  3.  
  4. {$IFDEF SearchMain}
  5.  
  6. program Search;
  7.  
  8. {$ELSE}
  9.  
  10. unit Search;
  11.  
  12. interface
  13.  
  14. function PxPartialSearch( TblHandle: TableHandle; SearchString: String;
  15.                           FldHandle: FieldHandle; Mode: Integer;
  16.                           CaseSensitive: Boolean): Integer;
  17.  
  18. implementation
  19. {$ENDIF}
  20.  
  21. uses PxEngine;
  22. const
  23.    CaseSensitive = TRUE;
  24.    NetDir        = '';
  25.    NetType       = NotOnNet;
  26.  
  27. (*
  28. ** Function:        UpCaseStr
  29. **
  30. ** Parameters:
  31. **
  32. **        S string to convert to upper case
  33. **
  34. ** Returns:
  35. **        Upper case equivalent of the passed parameter
  36. **
  37. *)
  38. function UpCaseStr(S: String): String;
  39. var
  40.    Result: String;
  41.    I: Integer;
  42.  
  43. begin
  44.  
  45.   Result := '';
  46.   for I := 1 to Length(S) do
  47.     Result := Result + UpCase(S[I]);
  48.  
  49.   UpCaseStr := Result;
  50.  
  51. end;
  52.  
  53.  
  54. (*
  55. ** Function:        PxPartialSearch
  56. **
  57. ** Parameters:
  58. **
  59. **                      TblHandle    table to search
  60. **
  61. **            SearchString    string to locate
  62. **
  63. **            FldHandle    field to search
  64. **
  65. **            Mode        SEARCHFIRST or SEARCHNEXT
  66. **
  67. **                      CaseSensitive    True if CaseSensitive
  68. **
  69. ** Returns:
  70. **
  71. **            PXSUCCESS if record found
  72. **            PXERR_RECNOTFOUND if record not found
  73. **
  74. *)
  75. function PxPartialSearch( TblHandle: TableHandle; SearchString: String;
  76.                           FldHandle: FieldHandle; Mode: Integer;
  77.                           CaseSensitive: Boolean): Integer;
  78. const
  79.    Found: Boolean = FALSE;
  80.  
  81. var
  82.    PxErr: Integer;
  83.    Buf, Buf2: String;
  84.    RecHandle: RecordHandle;
  85.    LckHandle: LockHandle;
  86.  
  87. begin
  88.  
  89. (* Make a copy of the search string, switch to uppercase if case-insensitive
  90.    search *)
  91.   Buf := SearchString;
  92.  
  93.   if CaseSensitive then
  94.     Buf := UpCaseStr(Buf);
  95.  
  96. (* Lock the table so no changes can occur while we search *)
  97.   PxErr := PxNetTblLock(TblHandle,WL);
  98.   if PxErr<>PxSuccess then
  99.     begin
  100.  
  101.       PxPartialSearch := PxErr;
  102.       Exit;
  103.  
  104.     end;
  105.  
  106. (* save current record in case we need to return if string not found *)
  107.   PxErr := PxNetRecLock(TblHandle, LckHandle);
  108.   if PxErr = PxSuccess then
  109.     if Mode = SearchFirst then
  110.       PxErr := PxRecFirst(TblHandle)
  111.     else
  112.       PxErr := PxRecNext(TblHandle);
  113.  
  114.   if PxErr = PxSuccess then
  115.  
  116.     begin
  117.  
  118.       PxErr := PxRecBufOpen(TblHandle,RecHandle);
  119.  
  120.       if PxErr = PxSuccess then
  121.  
  122.         repeat
  123.  
  124.           PxErr := PxRecGet(TblHandle,RecHandle);
  125.           if PxErr = PxSuccess then
  126.             begin
  127.  
  128.             PxErr := PxGetAlpha(RecHandle,FldHandle,Buf2);
  129.             if PxErr = PxSuccess then
  130.               begin
  131.  
  132.                 if CaseSensitive then
  133.                   Buf2 := UpCaseStr(Buf2);
  134.  
  135.                 if Pos(Buf,Buf2)<>0 then
  136.                   Found := True
  137.                 else
  138.                   PxErr := PxRecNext(TblHandle);
  139.                 end;
  140.  
  141.               end;
  142.  
  143.         until Found or (PxErr<>PxSuccess);
  144.  
  145.    end;
  146.  
  147.   if Found then
  148.  
  149.     begin
  150.  
  151.       PxPartialSearch := PxSuccess;
  152.       Exit;
  153.  
  154.     end
  155.  
  156.   else
  157.     if PxErr = PxErr_EndofTable then
  158.        PxPartialSearch := PXErr_RecNotFound
  159.  
  160.     else
  161.  
  162.        PxPartialSearch := PxErr;
  163.  
  164.   end; { of Function PxPartialSearch }
  165.  
  166. {$IFDEF SEARCHMAIN}
  167. const
  168.      Data: array[0..6] of String = ( 'Paradox','Engine','Leveraging',
  169.                                      'The','Power','Of','Paradox!' );
  170. const
  171.      NumberOfElements = 7;
  172.  
  173. (*
  174. **    Function:    AddRecords
  175. **
  176. **    Parameters:
  177. **            TblName        Name of table to append records to
  178. **
  179. **    Returns:
  180. **            PXSUCCESS if successful
  181. **            Appropriate PXERR_... if unsuccessful
  182. **
  183. *)
  184. function AddRecords( TblName: String ): Integer;
  185.  
  186. var
  187.    TblHandle: TableHandle;
  188.    RecHandle: RecordHandle;
  189.    PxErr, I: Integer;
  190.  
  191. begin
  192.  
  193.   PxErr := PxTblOpen(TblName,TblHandle,0,FALSE);
  194.   if PxErr = PxSuccess then
  195.  
  196.     begin
  197.  
  198.       PxErr := PxRecBufOpen(TblHandle,RecHandle);
  199.       if PxErr = PxSuccess then
  200.       I := 0;
  201.       while (I<NumberOfElements) and (PxErr=PxSuccess) do
  202.  
  203.         begin
  204.  
  205.           PxErr := PxPutAlpha(RecHandle,1,Data[I]);
  206.           if PxErr = PxSuccess then
  207.             PxErr := PxRecAppend(TblHandle,RecHandle);
  208.           Inc(I);
  209.  
  210.         end;
  211.  
  212.     end;
  213.  
  214.   if PxErr = PxSuccess then
  215.  
  216.     begin
  217.  
  218.       PxErr := PxRecBufClose(RecHandle);
  219.       if PxErr = PxSuccess then
  220.         PxErr := PxTblClose(TblHandle);
  221.  
  222.     end;
  223.  
  224.   AddRecords := PxErr;
  225.  
  226. end;
  227.  
  228. (*
  229. **    Function:     MakeTable
  230. **
  231. **    Parameters:
  232. **            TableName    Name of table to create
  233. **
  234. **    Return Value:
  235. **
  236. **            PXSUCCESS if successful
  237. **            Appropriate PXERR_... if unsuccessful
  238. **
  239. *)
  240. function MakeTable(TableName: String): Integer;
  241.  
  242. var
  243.    PxErr: Integer;
  244.    Fields: NameArray;
  245.    Types: NameArray;
  246.    F: WordArray;
  247.    TblSize: Word;
  248.  
  249. begin
  250.  
  251.   Fields[1] := 'Field 1';
  252.   Fields[2] := 'Field 2';
  253.   Types[1] := 'A20';
  254.   Types[2] := 'A20';
  255.   F[1] := 1;
  256.   TblSize := 2;
  257.   PxErr := PxTblCreate(TableName, TblSize, @Fields, @Types);
  258.   if PxErr = PxSuccess then
  259.  
  260.     begin
  261.  
  262.       PxErr := PxKeyAdd(TableName,1,f,PRIMARY);
  263.       if PxErr = PxSuccess then
  264.         PxErr := AddRecords(TableName);
  265.  
  266.     end;
  267.  
  268.   MakeTable := PxErr;
  269.  
  270. end;
  271.  
  272. var
  273.  
  274.    TblHandle: TableHandle;
  275.    RecHandle: RecordHandle;
  276.    Buf: String;
  277.    PxErr: Integer;
  278.  
  279. begin{ of Main Program}
  280.  
  281. PxErr := PxNetInit(NetDir,NetType,DefUserName);
  282.  
  283. if PxErr = PxSuccess then
  284.  
  285.    begin
  286.  
  287.      PxErr := MakeTable('Table');
  288.      if PxErr = PxSuccess then
  289.  
  290.        begin
  291.  
  292.          PxErr := PxTblOpen('Table',TblHandle,0,FALSE);
  293.          if PxErr = PxSuccess then
  294.            PxErr := PxRecBufOpen(TblHandle,RecHandle);
  295.  
  296.         end;
  297.  
  298.    end;
  299.  
  300. if PxErr <> PxSuccess then
  301.  
  302.   begin
  303.  
  304.     Writeln('Error ',PxErrMsg(PxErr));
  305.     Halt;
  306.  
  307.   end;
  308.  
  309. PxErr := PxPartialSearch(TblHandle,'aging',1,SEARCHFIRST,CASESENSITIVE);
  310. if PxErr = PxSuccess then
  311.  
  312.   begin
  313.  
  314.     PxErr := PxRecGet(TblHandle,RecHandle);
  315.     PxErr := PxGetAlpha(RecHandle,1,Buf);
  316.     Writeln('Found [',Buf,']');
  317.  
  318.   end
  319.  
  320. else
  321.  
  322.   Writeln('Search: ',PxErrMsg(PxErr));
  323.  
  324. {$ENDIF}
  325.  
  326. end.
  327.