Show/Hide Toolbars

TMS XData Documentation

Navigation: Getting Started > Creating the Server Manually

Example 2: MySQL Server with dbExpress (from Delphi code)

Scroll Prev Top Next More

The following example is a minimum console application that illustrates how to start an XData server at address "http://localhost:2001/tms/music" using dbExpress to connect to a MySQL database. It is configured in code. It uses an unit named "AureliusEntities" which is not shown here for simplification. Such a unit should just contain the Aurelius mapped classes that will be exposed by the server.

The connection pool is configured to hold up to 25 connections to the database.

 

program DBExpressConsoleServer;
 
{$APPTYPE CONSOLE}
 
uses
  System.SysUtils, SqlExpr, DBXMySQL,
  Aurelius.Drivers.Base,
  Aurelius.Drivers.Interfaces,
  Aurelius.Drivers.dbExpress,
  Aurelius.Sql.MySQL,
  Aurelius.Schema.MySQL,
  Sparkle.HttpSys.Server,
  XData.Aurelius.ConnectionPool,
  XData.Server.Module,
  AureliusEntities in '..\common\AureliusEntities.pas';
 
procedure StartServer;
var
  Server: THttpSysServer;
  ConnFactory: IDBConnectionFactory;
begin
  Server := THttpSysServer.Create;
  try
    // Example using dbExpress. Create a connection factory to use later
    ConnFactory := TDBConnectionFactory.Create(
      function: IDBConnection
      var
        SqlConn: TSQLConnection;
      begin
        SqlConn := TSQLConnection.Create(nil);
        SqlConn.DriverName := 'MySQL';
        SqlConn.GetDriverFunc := 'getSQLDriverMySQL';
        SqlConn.VendorLib := 'libmysql.dll';
        SqlConn.LibraryName := 'dbxmys.dll';
        SqlConn.Params.Values['HostName'] := 'dbserver';
        SqlConn.Params.Values['Database'] := 'xdata';
        SqlConn.Params.Values['User_Name'] := 'user';
        SqlConn.Params.Values['Password'] := 'mysql';
        SqlConn.LoginPrompt := false;
        Result := TDBExpressConnectionAdapter.Create(SqlConn, true);
      end
    );
    Server.AddModule(TXDataServerModule.Create(
      'http://localhost:2001/tms/music',
      TDBConnectionPool.Create(25, ConnFactory)
    ));
    Server.Start;
    WriteLn('Server started. Press ENTER to stop.');
    ReadLn;
  finally
    Server.Free;
  end;
end;
 
begin
  StartServer;
end.