Simple SQL query builder for Delphi, working with enumerated types.
- In development.
-
Do you need one enumerated type.
-
One class (that inherits
TEnumAbstract<T>
and implementsIEnum<T>
) to map the database fields based on the enumerated type. -
One model that inherits
TModel<T>
and implements Set/GetValue functions declared onIModel<T>
that are virtual on base model. -
Use classes of NowaImpl.pas unit to build the queries.
An Simple select example:
procedure NowaExample.TestExample;
var
oIPerson: IModel<TEPerson>;
begin
oIPerson := TPerson.Create;
oIPerson.PrepareModel('', [tepSequential, tepName, tepEmail]);
TSQLSelect.Create.Ref
.Fields([oIPerson.Fields])
.From(oIPerson.Table)
.Build;
end;
Example select with InnerJoin:
procedure NowaExample.TestExampleInnerJoin;
var
oIPerson: IPerson<TEPerson>;
oIMatriculation: IModel<TEMatriculation>;
SelectCommand: String;
begin
oIPerson := TPerson.Create;
oIMatriculation := TMatriculation.Create;
oIPerson.PrepareModel('', []);
oIMatriculation.PrepareModel('', []);
SelectCommand := TSQLSelect.Create.Ref
.Fields([oIPerson.Fields, oIMatriculation.Fields])
.From(oIPerson.Table)
.InnerJoin(
TSQLJoin.Create.Ref
.Table(oIMatriculation.Table)
.&On(
TSQLCondition.Create.Ref
.LeftTerm(oIMatriculation.Field(temPersonSequential))
.Op(opEqual)
.RightTerm(oIPerson.Field(tepSequential))
)
)
.Build;
end;
The output assigned to SelectCommand
is:
SELECT PERSON.NR_SEQUENTIAL AS PERSON_SEQUENTIAL,
PERSON.FL_NAME AS PERSON_NAME,
PERSON.DT_BIRTHDATE AS PERSON_BIRTHDATE,
PERSON.TX_EMAIL AS PERSON_EMAIL,
PERSON.TX_PASSWORD AS PERSON_PASSWORD,
MATRICULATION.NR_SEQUENTIAL AS MATRICULATION_SEQUENTIAL,
MATRICULATION.NR_PERSONSEQUENTIAL AS MATRICULATION_PERSONSEQUENTIAL,
MATRICULATION.DT_DATE AS MATRICULATION_DATE,
MATRICULATION.CD_USER AS PERSON_USER
FROM TB_PERSON AS PERSON
INNER JOIN TB_MATRICULATION AS MATRICULATION ON (MATRICULATION.NR_PERSONSEQUENTIAL = PERSON.NR_SEQUENTIAL)
A. Create a unit named "Enumerator.Person".
B. Create an enumerated TEPerson, see:
unit Enumerator.Person
interface
uses
Enumerator;
type
TEPerson = (tepSequential, tepName, tepBirthDate, tepEmail, tepPassword);
implementation
end.
C. Create a class who inherit TEnumAbstract and implements IEnum, see the class and function declarations:
unit Enumerator.Person
interface
uses
Enumerator;
type
TEPerson = (tepSequential, tepName, tepBirthDate, tepEmail, tepPassword);
TEnumPessoa = class(TEnumAbstract<TEPerson>, IEnum<TEPerson>)
public
function Column(const AEnumeratedField: TEPerson): String; override;
function Table: String; override;
function TableAlias(const AAlias: String = ''): String; override;
function Sequence: String; override;
function AllColumns: TArray<TEPerson>; override;
function Ref: IEnum<TEPerson>; override;
end;
implementation
end.