Show/Hide Toolbars

TMS Aurelius Documentation

Navigation: Database Connectivity

Component Adapters

Scroll Prev Top Next More

There is an adapter for each data-access component. For dbExpress, for example, you have TDBExpressConnectionAdapter, which is declared in unit Aurelius.Drivers.dbExpress. All adapters are declared in unit Aurelius.Drivers.XXX where XXX is the name of data-access technology you're using. You can create your own adapter by implementing IDBConnection interfaces, but Aurelius already has the following adapters available:

 

Technology

Adapter class

Declared in unit

Adapted Component

Vendor Site

Absolute Database

TAbsoluteDBConnectionAdapter

Aurelius.Drivers.AbsoluteDB

TABSDatabase

http://www.componentace.com

AnyDac

TAnyDacConnectionAdapter

Aurelius.Drivers.AnyDac

TADConnection

http://www.da-soft.com/anydac

dbExpress

TDBExpressConnectionAdapter

Aurelius.Drivers.dbExpress

TSQLConnection

Delphi Native

dbGo (ADO)

TDbGoConnectionAdapter

Aurelius.Drivers.dbGo

TADOConnection

Delphi Native

Direct Oracle Access (DOA)

TDoaConnectionAdapter

Aurelius.Drivers.Doa

TOracleSession

http://www.allroundautomations.com

ElevateDB

TElevateDBConnectionAdapter

Aurelius.Drivers.ElevateDB

TEDBDatabase

http://elevatesoftware.com/

FIBPlus

TFIBPlusConnectionAdapter

Aurelius.Drivers.FIBPlus

TFIBDatabase

http://www.devrace.com/

FireDac

TFireDacConnectionAdapter

Aurelius.Drivers.FireDac

TFDConnection

Delphi native

IBObjects (IBO)

TIBObjectsConnectionAdapter

Aurelius.Drivers.IBObjects

TIBODatabase

http://www.ibobjects.com/

Interbase Express (IBX)

TIBExpressConnectionAdapter

Aurelius.Drivers.IBExpress

TIBDatabase

Delphi Native

NativeDB

TNativeDBConnectionAdapter

Aurelius.Drivers.NativeDB

TASASession

https://www.nativedb.com

NexusDB

TNexusDBConnectionAdapter

Aurelius.Drivers.NexusDB

TnxDatabase

http://www.nexusdb.com

SQL-Direct

TSQLDirectConnectionAdapter

Aurelius.Drivers.SqlDirect

TSDDatabase

http://www.sqldirect-soft.com

SQLite

TSQLiteNativeConnectionAdapter

Aurelius.Drivers.SQLite

(not applicable)

TMS Aurelius Native

UniDac

TUniDacConnectionAdapter

Aurelius.Drivers.UniDac

TUniConnection

http://www.devart.com/unidac

Unified Interbase (UIB)

TUIBConnectionAdapter

Aurelius.Drivers.UIB

TUIBDatabase

http://sourceforge.net/projects/uib/

TMS RemoteDB Server

TRemoteDBConnectionAdapter

Aurelius.Drivers.RemoteDB

TRemoteDBDatabase

http://www.tmssoftware.com/site/remotedb.asp

ZeosLib

TZeosLibConnectionAdapter

Aurelius.Drivers.ZeosLib

TZConnection

https://sourceforge.net/projects/zeoslib

 

Creating the adapter

 

To create the adapter, you just need to instantiate it, passing an instance of the component to be adapted. In the example below, a FireDAC adapter constructor receives a TFDConnection component.

 

MyConnection := TFireDacConnectionAdapter.Create(FDConnection1, False);

 

The adapter usually detects the SQL Dialect automatically, but you can force the adapter to use a specific dialect, using one of the following overloaded constructors

 

Overloaded constructors

 

There are some overloaded versions of the constructor for all adapters:

 

constructor Create(AConnection: T; AOwnsConnection: boolean); overloadvirtual;

constructor Create(AConnection: T; ASQLDialect: string; AOwnsConnection: boolean); overloadvirtual;

constructor Create(AConnection: T; OwnedComponent: TComponent); overloadvirtual;

constructor Create(AConnection: T; ASQLDialect: string; OwnedComponent: TComponent); overloadvirtual;

 

AConnection: specify the database-access component to be adapted.

AOwnsConnection: if true, the component specified in AConnection parameter will be destroyed when the IDBConnection interface is released. If false, the component will stay in memory.

ASQLDialect: defines the SQL dialect to use when using this connection. If not specified, Aurelius will try to discover the SQL Dialect based on the settings in the component being adapted.

OwnedComponent: specifies the component to be destroyed when the IDBConnection interface is released. This is useful when using data modules (see below).

 

Memory Management

 

Note the second boolean parameter in the Create constructor of the adapter. It indicates if the underlying connection component will be destroyed when the IDBConnection interface is destroyed. In the example above ("Creating the adapter"), the SQLConnection1 component will remain in memory, even after MyConnection interface is out of scope and released. If you want the component to be destroyed, just pass the second parameter as true. You will usually use this option when you create a connection component just for Aurelius usage. If you are using an existing component from your application, use false. Quick examples below:

 

var
  MyConnection: IDBConnection;
begin
  MyConnection := TDBExpressConnectionAdapter.Create(SQLConnection1, False);
  // ...
  MyConnection := nil;
  { MyConection is nil, the TDBExpressConnectionAdapter component is destroyed, 
    but SQLconnection1 component remains in memory}    
end;

 

 

var
  MyConnection: IDBConnection;
  SQLConnection1: TSQLConnection;
begin
  SQLConnection1 := TSQLConnection.Create(nil);
  // Set SQLConnection1 properties here in code
  MyConnection := TDBExpressConnectionAdapter.Create(SQLConnection1, True);
  // ...
  MyConnection := nil;
  { MyConection is nil, the TDBExpressConnectionAdapter component is destroyed, 
    and SQLConnection1 is also destroyed }
end;

 

Alternatively, you can inform a component to be destroyed when the interface is released. This is useful when you want to create an instance of a TDataModule (or TForm) and use an adapted component that is owned by it. For example:

 

MyDataModule := TConnectionDataModule.Create(nil);

MyConnection := TDBExpressConnectionAdapter.Create(MyDataModule.SQLConnection1, MyDataModule);

 

The previous code will create a new instance of data module TConnectionDataModule, then create a IDBConnection by adapting the SQLConnection1 component that is in the data module. When MyConnection is released, the data module (MyDataModule) will be destroyed. This is useful if you want to setup the connection settings at design-time, but want to reuse many instances of the data module in different connections (for multi-threading purposes, for example).

 

Referencing original component

 

If the component adapter also implements the IDBConnectionAdapter interface, you can retrieve the original adapted component. For example, given an IDBConnection that you know was created from a TFireDacConnectionAdapter, you can retrieve the TFDConnection object using the following code:

 

var
  MyConnection: IDBConnection;
  FDConnection: TFDConnection;
{...}
  FDConnection := (MyConnection as IDBConnectionAdapter).AdaptedConnection as TFDConnection;

 

 

Native SQLite Adapter

 

Aurelius provides native SQLite database adapter. You just need to have sqlite3.dll in a path Windows/Mac can find. Creating SQLite adapter is a little different than other adapters, since you don't need to pass a component to be adapter. With the SQLite adapter, you just pass the name of the database file to be open (or created if it doesn't exist):

 

 MySQLiteConnection := TSQLiteNativeConnectionAdapter.Create('C:\Database\SQLite\MyDatabase.sdb');

 

TSQLiteNativeConnectionAdapter class also has two additional methods that you can use to manually disable or enable foreign keys in SQLite (foreign keys are enforced at connection level, not database level in SQLite!).

 

  procedure EnableForeignKeys;

  procedure DisableForeignKeys;

 

So if you want to use SQLite with foreign keys, do this to retrieve your connection:

 

var 

  SQLiteAdapter: TSQLiteNativeConnectionAdapter;

  MySQLiteConnection: IDBConnection;

begin

  SQLiteAdapter := TSQLiteNativeConnectionAdapter.Create('C:\Database\SQLite\MyDatabase.sdb');

  SQLiteAdapter.EnableForeignKeys;

  MySQLiteConnection := SQLiteAdapter;

  // Use MySQLiteConnection interface from now on

 

dbGo (ADO) Adapter

Currently dbGo (ADO) is only officially supported when connecting to Microsoft SQL Server databases. Drivers for other databases might work but were not tested.