{ $Id: PDOConnection.pas 23 2007-01-19 22:06:28Z jmarino $

############################################################################################

	Pascal Data Objects Library
	Copyright © 2006 John Marino, http://www.synsport.com
	Project site: http://pdo.sourceforge.net

	Note: This file contains code fragments from Zeoslib
	Copyright © 1999-2004 Sergey Seroukhov

############################################################################################

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

############################################################################################}



unit PDOConnection;

interface

{$I directives.inc}

uses
  Classes, SysUtils,
  PDOClasses, PDOInterfaces, PDOMessages, PDOStatement;

type

  {** Implements Abstract Database Connection. }
  TAbstractConnection = class(TInterfacedObject, IZConnection)
  private

    FHostName: AnsiString;
    FPort: Integer;
    FDatabase: AnsiString;
    FUser: AnsiString;
    FPassword: AnsiString;
    FSocket:   AnsiString;
    FAutoCommit: Boolean;
    FReadOnly: Boolean;
    FTransactIsolationLevel: TZTransactIsolationLevel;
    FClosed: Boolean;
  public

    constructor Create(HostName: AnsiString;
      Port: Integer; Database, User, Password, Socket: AnsiString);
    destructor Destroy; override;
    procedure SetAutoCommit(AutoCommit: Boolean); virtual;
    function GetAutoCommit: Boolean; virtual;

    procedure Commit; virtual;
    procedure Rollback; virtual;

    procedure Open; virtual;
    procedure Close; virtual;
    function IsClosed: Boolean; virtual;

    procedure SetTransactionIsolation(Level: TZTransactIsolationLevel); virtual;
    function GetTransactionIsolation: TZTransactIsolationLevel; virtual;

    function execute_without_result (SQL: AnsiString): int64; virtual; abstract;
    function GetConnectionHandle: PCONNECTION; virtual; abstract;
    function InitializeStatement: IPDOStatement; virtual; abstract;

    function GetSQLDialect: TSQLDialect; virtual; abstract;
    function GetDescription: AnsiString; virtual; abstract;

  protected
    procedure RaiseUnsupportedException;

    property HostName: AnsiString read FHostName write FHostName;
    property Port: Integer read FPort write FPort;
    property Database: AnsiString read FDatabase write FDatabase;
    property Socket: AnsiString read FSocket write FSocket;
    property User: AnsiString read FUser write FUser;
    property Password: AnsiString read FPassword write FPassword;
    property AutoCommit: Boolean read FAutoCommit write FAutoCommit;
    property ReadOnly: Boolean read FReadOnly write FReadOnly;
    property TransactIsolationLevel: TZTransactIsolationLevel
      read FTransactIsolationLevel write FTransactIsolationLevel;
    property Closed: Boolean read FClosed write FClosed;

    function GetSilentMode: Byte; virtual; abstract;
    function GetCaseMode: Byte; virtual; abstract;
    function GetOracleNull: Byte; virtual; abstract;
    function GetBufferQuery: Boolean; virtual; abstract;
    function GetMaximumBLOBSize: Int64; virtual; abstract;
    function GetCompression: Boolean; virtual; abstract;
    function GetMultipleQuery: Boolean; virtual; abstract;

    procedure SetSilentMode (mode: Byte); virtual; abstract;
    procedure SetCaseMode (mode: Byte); virtual; abstract;
    procedure SetOracleNull (mode: Byte); virtual; abstract;
    procedure setBufferQuery (mode: Boolean); virtual; abstract;
    procedure setMaximumBLOBSize (BLOBSize: Int64); virtual; abstract;
    procedure setCompression (mode: Boolean); virtual; abstract;
    procedure SetMultipleQuery (mode: Boolean); virtual; abstract;

    function GetLastInsertID: Int64; virtual; abstract;
    function GetSQLState: AnsiString; virtual; abstract;
    function GetLastErrorCode: Integer; virtual; abstract;
    function GetLastErrorMessage: AnsiString; virtual; abstract;
    function GetServerVersion: AnsiString; virtual; abstract;
    function GetClientVersion: AnsiString; virtual; abstract;
    function GetServerInfo: AnsiString; virtual; abstract;
    function GetClientInfo: AnsiString; virtual; abstract;
  end;


implementation


{**
  Constructs this object and assignes the main properties.
  @param Driver a PDO driver interface.
  @param Url a connection URL.
  @param HostName a name of the host.
  @param Port a port number (0 for default port).
  @param Database a name pof the database.
  @param User a user name.
  @param Password a user password.
  @param Info a string list with extra connection parameters.
}
constructor TAbstractConnection.Create(HostName: AnsiString; Port: Integer; Database, User, Password, Socket: AnsiString);
begin
  FHostName := HostName;
  FPort := Port;
  FDatabase := Database;
  self.FSocket := Socket;

  self.FUser := User;
  self.FPassword := Password;

  FAutoCommit := True;
  FClosed := True;
  FReadOnly := True;
  FTransactIsolationLevel := tiNone;
end;

{**
  Destroys this object and cleanups the memory.
}
destructor TAbstractConnection.Destroy;
begin
  if not FClosed then Close;
  //FInfo.Free;
  inherited Destroy;
end;

{**
  Opens a connection to database server with specified parameters.
}
procedure TAbstractConnection.Open;
begin
  FClosed := False;
end;

{**
  Raises unsupported operation exception.
}
procedure TAbstractConnection.RaiseUnsupportedException;
begin
  raise EZSQLException.Create(SUnsupportedOperation);
end;


procedure TAbstractConnection.SetAutoCommit(AutoCommit: Boolean);
begin
  FAutoCommit := AutoCommit;
end;

{**
  Gets the current auto-commit state.
  @return the current state of auto-commit mode
  @see #setAutoCommit
}
function TAbstractConnection.GetAutoCommit: Boolean;
begin
  Result := FAutoCommit;
end;


{**
  Makes all changes made since the previous
  commit/rollback permanent and releases any database locks
  currently held by the Connection. This method should be
  used only when auto-commit mode has been disabled.
  @see #setAutoCommit
}
procedure TAbstractConnection.Commit;
begin
  RaiseUnsupportedException;
end;

{**
  Drops all changes made since the previous
  commit/rollback and releases any database locks currently held
  by this Connection. This method should be used only when auto-
  commit has been disabled.
  @see #setAutoCommit
}
procedure TAbstractConnection.Rollback;
begin
  RaiseUnsupportedException;
end;

{**
  Releases a Connection's database and JDBC resources
  immediately instead of waiting for
  them to be automatically released.

  <P><B>Note:</B> A Connection is automatically closed when it is
  garbage collected. Certain fatal errors also result in a closed
  Connection.
}
procedure TAbstractConnection.Close;
begin
  FClosed := True;
end;

{**
  Tests to see if a Connection is closed.
  @return true if the connection is closed; false if it's still open
}
function TAbstractConnection.IsClosed: Boolean;
begin
  Result := FClosed;
end;


{**
  Attempts to change the transaction isolation level to the one given.
  The constants defined in the interface <code>Connection</code>
  are the possible transaction isolation levels.

  <P><B>Note:</B> This method cannot be called while
  in the middle of a transaction.

  @param level one of the TRANSACTION_* isolation values with the
    exception of TRANSACTION_NONE; some databases may not support other values
  @see DatabaseMetaData#supportsTransactionIsolationLevel
}
procedure TAbstractConnection.SetTransactionIsolation(
  Level: TZTransactIsolationLevel);
begin
  FTransactIsolationLevel := Level;
end;

{**
  Gets this Connection's current transaction isolation level.
  @return the current TRANSACTION_* mode value
}
function TAbstractConnection.GetTransactionIsolation: TZTransactIsolationLevel;
begin
  Result := FTransactIsolationLevel;
end;



end.
