home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 May / CMCD0505.ISO / Software / Shareware / Programare / pgedri / Source / pgeDriverUtils.pas
Encoding:
Pascal/Delphi Source File  |  2005-04-01  |  7.4 KB  |  247 lines

  1. {*******************************************************************}
  2. {                                                                   }
  3. {       Vita Voom Software                                          }
  4. {       pgeDriverUtils.pas unit                                     }
  5. {                                                                   }
  6. {       Copyright (c) 1998-2003 Vita Voom Software                  }
  7. {       ALL RIGHTS RESERVED                                         }
  8. {*******************************************************************}
  9. {                                                                   }
  10. { This unit contains utility functions for use with the pgExpress   }
  11. { driver.                                                           }
  12. {                                                                   }
  13. { Please refer to the pgeExpress documentation for details.         }
  14. {                                                                   }
  15. {*******************************************************************}
  16.  
  17. unit pgeDriverUtils;
  18.  
  19. interface
  20.  
  21. uses
  22.   SqlExpr, DB, DBXpress;
  23.  
  24. type
  25.   EPGECancel = class(EDatabaseError);
  26.   TCancelQuery = procedure(Handle: Pointer);
  27.   PPGconn = Pointer;
  28.   TCSQLConnection = class(TSQLConnection);
  29.  
  30. const
  31.   strTTY = 'TTY';
  32.   strBackendPID = 'BackendPID';
  33.   strRetainCursor = 'RetainCursor';
  34.   strPGVersion = 'PGVersion';
  35.   strLanguage = 'Language';
  36.  
  37.   {$IF (RTLVersion >= 14.5) and Defined(MSWINDOWS)}
  38.     {$DEFINE DELPHI7_OR_HIGHER}
  39.   {$IFEND}
  40.  
  41.   {$IF RTLVersion < 16}
  42.     // Before Delphi 8, the DBXERR_NONE constant was called 'SQL_SUCCESS'
  43.     DBXERR_NONE = SQL_SUCCESS;
  44.   {$IFEND}
  45.   
  46. {$IFDEF DELPHI7_OR_HIGHER}
  47.   // Retrieves the PostgreSQL version number. D7+ Only.
  48.   function GetPGVersion(Connection: TSQLConnection): Extended;
  49.  
  50.   // Returns the file or TTY debug for the connection. D7+ Only.
  51.   function GetTTY(Connection: TSQLConnection): AnsiString;
  52.  
  53.   // Returns the server PID (process ID) for the connection. D7+ Only.
  54.   function GetBackendPID(Connection: TSQLConnection): Integer;
  55.  
  56.   // Returns True if a connection to the server exists. D7+ Only.
  57.   function GetOnline(Connection: TSQLConnection): Boolean;
  58.  
  59.   // Gets the internal RetainCursor variable. D7+ Only.
  60.   function GetRetainCursor(Connection: TSQLConnection): Boolean;
  61.  
  62.   // Sets the internal RetainCursor variable. D7+ Only.
  63.   procedure SetRetainCursor(Connection: TSQLConnection; Value: Boolean);
  64.  
  65.   // Gets the internal driver Language, for the error messages.
  66.   function GetLanguage(Connection: TSQLConnection): AnsiString;
  67.  
  68.   // Sets the internal driver Language, for the error messages.
  69.   // D7+ Only, and will work only for multilanguage driver, if language <> enUS.
  70.   procedure SetLanguage(Connection: TSQLConnection; Value: AnsiString);
  71.  
  72.   procedure CancelQuery(Connection: TSQLConnection);
  73. {$ENDIF}
  74.  
  75. implementation
  76.  
  77. uses
  78.   Types, SysUtils
  79.   {$IFDEF MSWINDOWS}
  80.     , Windows
  81.   {$ELSE}
  82.     , Libc
  83.   {$ENDIF};
  84.  
  85. // Auxiliar function.
  86. procedure Check(Value: Word; Connection: TSQLConnection);
  87. var
  88.   S: AnsiString;
  89.   Len: SmallInt;
  90. begin
  91.   if Value <> DBXERR_NONE then
  92.   begin
  93.     Len := Connection.SQLConnection.getErrorMessageLen(Len);
  94.     SetLength(S, Len);
  95.     Connection.SQLConnection.getErrorMessage(PChar(S));
  96.     DatabaseError(S);
  97.   end;
  98. end;
  99.  
  100. function GetConn(Connection: TSQLConnection): PPGconn;
  101. var
  102.   Len: SmallInt;
  103. begin
  104.   if Connection.SQLConnection = nil then
  105.     Result := nil
  106.   else
  107.     Connection.SQLConnection.GetOption(eConnNativeHandle, @Result, SizeOf(Result), Len)
  108. end;
  109.  
  110. {$IFDEF DELPHI7_OR_HIGHER}
  111.   // Retrieves the PostgreSQL version number. D7+ Only.
  112.   function GetPGVersion(Connection: TSQLconnection): Extended;
  113.   const
  114.     BufSize: Byte = 50;
  115.   var
  116.     Len: SmallInt;
  117.     Buffer: TByteDynArray;
  118.   begin
  119.     SetLength(Buffer, BufSize);
  120.     StrCopy(@Buffer[0], strPGVersion);
  121.     Check(Connection.SQLConnection.GetOption(eConnCustomInfo, @Buffer[0], Length(Buffer), Len), Connection);
  122.     Result := PExtended(Buffer)^;
  123.   end;
  124.  
  125.   // Returns the file or TTY debug for the connection. D7+ Only.
  126.   function GetTTY(Connection: TSQLConnection): AnsiString;
  127.   const
  128.     BufSize: Byte = 50;
  129.   var
  130.     Buffer: TByteDynArray;
  131.     Len: SmallInt;
  132.   begin
  133.     SetLength(Buffer, BufSize);
  134.     try
  135.       StrCopy(@Buffer[0], strTTY);
  136.       Check(Connection.SQLConnection.GetOption(eConnCustomInfo, @Buffer[0], BufSize, Len), Connection);
  137.       Result := StrPas(@Buffer[0]);
  138.     finally
  139.       Buffer := nil;
  140.     end;
  141.   end;
  142.  
  143.   // Returns the file or TTY debug for the connection. D7+ Only.
  144.   function GetBackendPID(Connection: TSQLConnection): Integer;
  145.   const
  146.     BufSize: Byte = 20;
  147.   var
  148.     Buffer: TByteDynArray;
  149.     Len: SmallInt;
  150.   begin
  151.     SetLength(Buffer, BufSize);
  152.     try
  153.       StrCopy(@Buffer[0], strBackendPID);
  154.       Check(Connection.SQLConnection.GetOption(eConnCustomInfo, @Buffer[0], BufSize, Len), Connection);
  155.       Result := PInteger(@Buffer[0])^;
  156.     finally
  157.       Buffer := nil;
  158.     end;
  159.   end;
  160.  
  161.   // Returns True if a connection to the server exists. D7+ Only.
  162.   function GetOnline(Connection: TSQLConnection): Boolean;
  163.   var
  164.     Len: SmallInt;
  165.     Value: Boolean;
  166.   begin
  167.     Check(Connection.SQLConnection.GetOption(eConnCustomInfo, @Value, SizeOf(Boolean), Len), Connection);
  168.     Result := PBoolean(@Value)^;
  169.   end;
  170.  
  171.   // Returns the internal RetainCursor variable. D7+ Only.
  172.   function GetRetainCursor(Connection: TSQLConnection): Boolean;
  173.   const
  174.     BufSize: Byte = 20;
  175.   var
  176.     Buffer: TByteDynArray;
  177.     Len: SmallInt;
  178.   begin
  179.     SetLength(Buffer, BufSize);
  180.     try
  181.       StrCopy(@Buffer[0], strRetainCursor);
  182.       Check(Connection.SQLConnection.GetOption(eConnCustomInfo, @Buffer[0], BufSize, Len), Connection);
  183.       Result := PBoolean(@Buffer[0])^;
  184.     finally
  185.       Buffer := nil;
  186.     end;
  187.   end;
  188.  
  189.   // Sets the internal RetainCursor variable. D7+ Only.
  190.   procedure SetRetainCursor(Connection: TSQLConnection; Value: Boolean);
  191.   var
  192.     S: AnsiString;
  193.   begin
  194.     S := strRetainCursor + '=' + BoolToStr(Value, True);
  195.     Check(Connection.SQLConnection.SetOption(eConnCustomInfo, Integer(PChar(S))), Connection);
  196.   end;
  197.  
  198.   // Gets the internal driver Language, for the error messages.
  199.   function GetLanguage(Connection: TSQLConnection): AnsiString;
  200.   const
  201.     BufSize: Byte = 10;
  202.   var
  203.     Buffer: TByteDynArray;
  204.     Len: SmallInt;
  205.   begin
  206.     SetLength(Buffer, BufSize);
  207.     try
  208.       StrCopy(@Buffer[0], strLanguage);
  209.       Check(Connection.SQLConnection.GetOption(eConnCustomInfo, @Buffer[0], BufSize, Len), Connection);
  210.       Result := StrPas(@Buffer[0]);
  211.     finally
  212.       Buffer := nil;
  213.     end;
  214.   end;
  215.  
  216.   // Sets the internal driver Language, for the error messages.
  217.   procedure SetLanguage(Connection: TSQLConnection; Value: AnsiString);
  218.   var
  219.     S: AnsiString;
  220.   begin
  221.     S := strLanguage + '=' + Value;
  222.     Check(Connection.SQLConnection.SetOption(eConnCustomInfo, Integer(PChar(S))), Connection);
  223.   end;
  224. {$ENDIF}
  225.  
  226. // Cancels a query
  227. procedure CancelQuery(Connection: TSQLConnection);
  228. const
  229.   ProcName = 'CancelQuery';
  230. var
  231.   LibHandle: THandle;
  232.   CQ: TCancelQuery;
  233. begin
  234.   LibHandle := TCSQLConnection(Connection).SQLDllHandle;
  235.   if LibHandle <> 0 then
  236.   begin
  237.     {$IFDEF MSWINDOWS}
  238.       @CQ := GetProcAddress(LibHandle, ProcName);
  239.     {$ELSE}
  240.       @CQ := dlsym(Pointer(LibHandle), ProcName);
  241.     {$ENDIF}
  242.     CQ(GetConn(Connection));
  243.   end
  244. end;
  245.  
  246. end.
  247.