Quantcast
Channel: TMS Software
Viewing all articles
Browse latest Browse all 1008

Crash Course TMS Aurelius – AnyDAC or dbExpress?

$
0
0

In the example provided in the previous post, we saved a TCustomer instance in a local SQLite database which was accessed natively by TMS Aurelius. Let’s refactor that code a little bit:

procedure SaveCustomer(Connection: IDBconnection; CustomerName: string);
var
  Manager: TObjectManager;
  Customer: TCustomer;
begin
  Manager := TObjectManager.Create(Connection);
  Customer := TCustomer.Create;
  Customer.Name := CustomerName;
  Manager.Save(Customer);
  Manager.Free;
end;
With the procedure above, to save a customer in the SQLite database, we used a code similar to this (non-relevant lines removed):
uses
  {…}, Aurelius.Drivers.SQLite,   Aurelius.SQL.SQLite;

  Connection := TSQLiteNativeConnectionAdapter.Create('test.db');
  SaveCustomer(Connection, 'Jack');
What if we want to change it completely and save our objects in a MySQL database, using dbExpress to connect to it? That can be done this way:
uses
  {…}, Aurelius.Drivers.dbExpress,   Aurelius.SQL.MySQL;

  Connection := TDBExpressConnectionAdapter.Create(SQLConnection1, 'MySQL', False);
  SaveCustomer(Connection, 'Joe');
Note that besides the code that retrieves an IDBConnection interface, all other code remains the same. And that is true for any database you want to connect to, using any component, because all the object manager needs is an IDBConnection interface.

To retrieve that interface, we used a component adapter (TDBExpressConnectionAdapter, declared in unit Aurelius.Drivers.dbExpress) that takes our current dbExpress connection component (a TSQLConnection named SQLConnection1) and retrieves the interface. The second parameter indicates which database we are connecting to (more specifically, which SQL dialect Aurelius needs to use to execute SQL statements). That dialect, 'MySQL', is available after you use the unit Aurelius.SQL.MySQL. Finally, the third parameter (False) indicates that when the IDBConnection interface is destroyed, the adapted component (SQLConnection1) should not be destroyed. Optionally you can set it to true, which can be useful if you are creating the component only to be used by IDBConnection, so that the component is destroyed when interface is destroyed.

Now that Embarcadero has purchased AnyDac library and it will probably be provided natively in Delphi, using it instead of dbExpress will be a matter of changing a couple of lines:
uses
  {…}, Aurelius.Drivers.AnyDac,   Aurelius.SQL.MySQL;

  Connection := TDBExpressConnectionAdapter.Create(ADConnection1, False);
  SaveCustomer(Connection, 'Phil');
You might be missing the second parameter indicating that we are connecting to a MySQL database. This is because the adapters are able to automatically identify the database being connected to (Both TSQLConnection and TADConnection components have a property DriverName which Aurelius uses to identify the database). So the second parameter is optional.

Another thing is worth noting is that with this approach, code is very abstract and flexible. Aurelius doesn’t have any connection parameters that you need to configure like server name, password, etc. Everything is configured in the same components you already use. Any database connection configuration, including advanced ones, provided by each database access components, is still available.

So, if you don’t know if you should use AnyDac or dbExpress, you can use both and change them as you want to. Not only those, but at the current version (1.8) Aurelius also supports ADO components, Direct Oracle Access, ElevateDB, NexusDB, Absolute Database, FIBPlus, IBObjects, IBX components, SQL-Direct, UniDac and of course the native SQLite adapter. Aurelius documentation also provides the unit names and the name of adapter classes in its topic about component adapters.

As for the supported databases, you can use not only SQLite and MySQL, but also Firebird, MS SQL Server, Interbase, Oracle, PostgreSQL, Absolute Database, DB2, ElevateDB, NexusDB and SQLite. The names of units and SQL dialects are available in the topic “SQL Dialects” in documentation.

To conclude, I would like to mention that not only those databases and components are supported, but they are also extensively tested in each Aurelius release, with almost all possible combinations (dbExpress connecting to SQL Server, AnyDac connecting to PostgreSQL, and so on). You can check in the documentation which minimum versions of were used for tests for each combination. So most of little problems here are there with field types, SQL syntax, among other common problems that we usually find when switching components and databases are already solved, making Aurelius code effectively database/component agnostic, not only in theory, but also in practice.


Viewing all articles
Browse latest Browse all 1008

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>