home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue42 / ASP / uSqlObject.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1998-11-19  |  3.5 KB  |  127 lines

  1. unit uSqlObject;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, ComObj, ActiveX, Graphics, ASPTypeLibrary_TLB, delphi_TLB, dmTables;
  7.  
  8. type
  9.   // SQL automation object to manipulate data from the DBDEMOS tables using ASP.
  10.   TSQLObj = class(TAutoObject, ISQLObj)
  11.   private
  12.     fScript : IScriptingContext;
  13.     fSql : WideString;        // User's sql statement retrieved from the client's browser.
  14.     fUseProducer : Integer;   // Indicate wheter to use the TDataSetTableProducer component or not.
  15.     procedure BuildTableData;
  16.   protected
  17.     procedure ExecuteSql; safecall;
  18.     procedure OnStartPage(const unk: IUnknown); safecall;
  19.     procedure OnEndPage; safecall;
  20.     function Get_SQL: WideString; safecall;
  21.     procedure Set_SQL(const Value: WideString); safecall;
  22.     function Get_UseProducer: Integer; safecall;
  23.     procedure Set_UseProducer(Value: Integer); safecall;
  24.   public
  25.     procedure Initialize; override;
  26.   end;
  27.  
  28. implementation
  29.  
  30. uses ComServ;
  31.  
  32. // The BuildTableData method will print the result of the query to the Client's
  33. //  browser using the IScriptingContext interface passed to us from the web server.
  34. procedure TSQLObj.BuildTableData;
  35. var
  36.    i : integer;
  37. begin
  38.   // Build the html response page to be sent to the client's browser.
  39.   with fScript.Response, dmBioLife.qryTable do
  40.     if ( fUseProducer = 0 ) then
  41.     begin
  42.       while not Eof do   // Iterate through the data set.
  43.       begin
  44.         // Write field's values to the browser.
  45.         for i := 0 to Pred( dmBioLife.qryTable.FieldCount ) do
  46.             Write ( Format ( '%s, ', [ dmBioLife.qryTable.Fields[ i ].AsString ] ) );
  47.         Next;
  48.         Write ( '<br>' );
  49.       end;
  50.     end
  51.   else  // Display the data using the page producer.
  52.     write ( dmBioLife.dsProducer.Content );
  53.  
  54. end;
  55.  
  56. // Pre-defined method called from the ASP page to initiate the sql manipulation.
  57. procedure TSQLObj.ExecuteSql;
  58. var
  59.   sValue : string;
  60. begin
  61.   try
  62.     // Add the client's sql sentence.
  63.     if ( Length ( fSql ) > 0 ) then
  64.     begin
  65.       dmBioLife.qryTable.Sql.Clear;
  66.       dmBioLife.qryTable.Sql.Add ( fSql );
  67.     end;
  68.     dmBioLife.qryTable.Active := true;
  69.     with fScript.Response, dmBioLife.qryTable do
  70.     begin
  71.       // Report back to user some custom data.
  72.       Write ( Format ( 'After opening query - Rec No. = %d<br>', [ dmBioLife.qryTable.RecordCount ] )  );
  73.       sValue := UpperCase ( fScript.Request.Form.Item [ 'cbDsProducer' ] );
  74.       if ( sValue = '' ) then
  75.          sValue := 'OFF';
  76.       Write ( Format ( 'Checkbox value in html form = %s<br>', [ sValue ] ) );
  77.       // Build the html response page to be sent to the client's browser.
  78.       BuildTableData;
  79.     end;
  80.   finally
  81.   end;
  82.  
  83. end;
  84.  
  85. procedure TSQLObj.Initialize;
  86. begin
  87.   inherited Initialize;
  88.   fSql := '';
  89.   dmBioLife.qryTable.Active := false;
  90.  
  91. end;
  92.  
  93. procedure TSQLObj.OnStartPage(const unk: IUnknown);
  94. begin
  95.   fScript := unk as IScriptingContext;  // Save the IScriptingContext interface.
  96. end;
  97.  
  98. procedure TSQLObj.OnEndPage;
  99. begin
  100.   fScript := nil;  // Release the IScriptingContext interface.
  101. end;
  102.  
  103. function TSQLObj.Get_SQL: WideString;
  104. begin
  105.   Result := fSql;
  106. end;
  107.  
  108. procedure TSQLObj.Set_SQL(const Value: WideString);
  109. begin
  110.   fSql := Value;
  111. end;
  112.  
  113. function TSQLObj.Get_UseProducer: Integer;
  114. begin
  115.   Result := fUseProducer;
  116. end;
  117.  
  118. procedure TSQLObj.Set_UseProducer(Value: Integer);
  119. begin
  120.  fUseProducer := Value;
  121. end;
  122.  
  123. initialization
  124.   TAutoObjectFactory.Create(ComServer, TSQLObj, Class_SQLObj,
  125.     ciMultiInstance, tmApartment);
  126. end.
  127.