Reputation: 2603
I am trying to create a simple Rad Server with delphi. I started by adding a Datamodule for database connection. It does connect and work, Created a singleton so other data modules could use the connection. When I run and the connection is called, I get error TFDConnection not found.
This is my db connection module
unit databaseConnection;
interface
uses
System.SysUtils, System.Classes, System.JSON,
EMS.Services, EMS.ResourceAPI, EMS.ResourceTypes, FireDAC.Stan.Intf,
FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf,
FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys,
FireDAC.Phys.MySQL, FireDAC.Phys.MySQLDef, FireDAC.ConsoleUI.Wait,
FireDAC.Phys.MSSQLDef, FireDAC.Phys.ODBCBase, FireDAC.Phys.MSSQL, Data.DB,
FireDAC.Comp.Client;
type
[ResourceName('DatabaseConnection')]
TDatabaseConnectionResource1 = class(TDataModule)
private
class var FInstance: TDatabaseConnectionResource1; // Singleton instance
public
FDConnection1: TFDConnection;
FDPhysMSSQLDriverLink1: TFDPhysMSSQLDriverLink;
class function Instance: TDatabaseConnectionResource1;
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
published
end;
implementation
{%CLASSGROUP 'System.Classes.TPersistent'}
{$R *.dfm}
{ TDatabaseConnectionResource1 }
class function TDatabaseConnectionResource1.Instance: TDatabaseConnectionResource1;
begin
if FInstance = nil then
FInstance := TDatabaseConnectionResource1.Create(nil);
Result := FInstance;
end;
procedure TDatabaseConnectionResource1.AfterConstruction;
begin
inherited;
// Initialization code if needed
end;
procedure TDatabaseConnectionResource1.BeforeDestruction;
begin
// Clean-up code if needed
inherited;
end;
procedure Register;
begin
RegisterResource(TypeInfo(TDatabaseConnectionResource1));
end;
initialization
Register;
Now in the next module I have added the unit to the uses
uses ... databaseConnection;
Now I try to connect connection to query below.
// Initialize query
LQuery := TFDQuery.Create(nil);
try
LQuery.Connection := TDatabaseConnectionResource1.Instance.FDConnection1;
// Insert new user
LSQL := 'INSERT INTO Teams (Company, Status, admin_rank, phone) VALUES (:Company, :Status, :Admin_Rank, :Phone)';
LQuery.SQL.Text := LSQL;
LQuery.ParamByName('Company').AsString := LCompany;
LQuery.ParamByName('Status').AsString := LStatus;
LQuery.ParamByName('Admin_Rank').AsInteger := Ladminrank;
LQuery.ParamByName('Phone').AsString := LPhone;
LQuery.ExecSQL;
AResponse.StatusCode := 200; // OK
AResponse.Body.SetValue(TJSONString.Create('Team created successfully'), True);
finally
LQuery.Free;
end;
finally
LJSON.Free;
end
else
begin
AResponse.RaiseBadRequest('Invalid JSON');
end;
everything compiles and builds, but can not seem to find why its giving this error? Am I missing something?
Upvotes: 1
Views: 173