Skip to content

Commit

Permalink
refactor-custom-query
Browse files Browse the repository at this point in the history
  • Loading branch information
MrN1ghtmare committed May 4, 2023
1 parent ac0ad96 commit 6808d8c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 62 deletions.
107 changes: 47 additions & 60 deletions EzQuery.pas
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ interface

Utils.Errors;

function GetEntityName(const DataSet: TFDTable): String;
procedure FetchOrError(const DataSet: TFDDataSet; const Error: Exception);
procedure FetchOrNotFound(const DataSet: TFDDataSet; const Entity: String);
function GetEntityName(const Query: TFDCustomQuery): String;
procedure FetchOrError(const Query: TFDCustomQuery; const Error: Exception);
procedure FetchOrNotFound(const Query: TFDCustomQuery; const Entity: String);

type

iEzQuery = interface ['{A83CF569-9B87-46AC-9EEF-CC3E6BD9B24B}']

function Table(
const Table: TFDTable;
function Query(
const Query: TFDCustomQuery;
const ExcludeDeleted: Boolean = True;
const DeleteAtColumnName: String = 'DELETED_AT'
): iEzQuery;
Expand All @@ -37,7 +37,7 @@ procedure FetchOrNotFound(const DataSet: TFDDataSet; const Entity: String);
function &Or(const Condition: String): iEzQuery;
function Join(
const Alias: String;
const DataSet: TFDTable;
const Query: TFDCustomQuery;
const DataSourse: TDataSource;
const MasterFields: String
): iEzQuery;
Expand All @@ -48,28 +48,28 @@ procedure FetchOrNotFound(const DataSet: TFDDataSet; const Entity: String);
const JSON: TJSONObject;
const CreatedAtColumnName: String = 'CREATED_AT';
const ReadOnly: Boolean = True
): TFDDataSet;
): TFDCustomQuery;

function UpdateOne(
const JSON: TJSONObject;
const UpdatedAtColumnName: String = 'UPDATED_AT';
const ReadOnly: Boolean = True
): TFDDataSet; overload;
): TFDCustomQuery; overload;

function UpdateOne(
const Script: TProc;
const UpdatedAtColumnName: String = 'UPDATED_AT';
const ReadOnly: Boolean = True
): TFDDataSet; overload;
): TFDCustomQuery; overload;

function SoftDelete(
const ColumnName: String = 'DELETED_AT';
const ReadOnly: Boolean = True
): TFDDataSet; overload;
): TFDCustomQuery; overload;

function Fetch(): TFDDataSet;
function FetchOrError(const Error: Exception): TFDDataSet;
function FetchOrNotFound(): TFDDataSet;
function Fetch(): TFDCustomQuery;
function FetchOrError(const Error: Exception): TFDCustomQuery;
function FetchOrNotFound(): TFDCustomQuery;

end;

Expand All @@ -78,7 +78,7 @@ TEzQuery = class(TInterfacedObject, iEzQuery)
strict private

FConnection : TFDConnection;
FTable : TFDTable;
FTable : TFDCustomQuery;

BeforeExecEvent : TProc;

Expand All @@ -92,8 +92,8 @@ TEzQuery = class(TInterfacedObject, iEzQuery)

class function New(const Connection: TFDConnection): iEzQuery;

function Table(
const Table: TFDTable;
function Query(
const Query: TFDCustomQuery;
const ExcludeDeleted: Boolean = True;
const DeleteAtColumnName: String = 'DELETED_AT'
): iEzQuery;
Expand All @@ -102,7 +102,7 @@ TEzQuery = class(TInterfacedObject, iEzQuery)
function &Or(const Condition: String): iEzQuery;
function Join(
const Alias: String;
const DataSet: TFDTable;
const Query: TFDCustomQuery;
const DataSourse: TDataSource;
const MasterFields: String
): iEzQuery;
Expand All @@ -113,54 +113,54 @@ TEzQuery = class(TInterfacedObject, iEzQuery)
const JSON: TJSONObject;
const CreatedAtColumnName: String = 'CREATED_AT';
const ReadOnly: Boolean = True
): TFDDataSet;
): TFDCustomQuery;

function UpdateOne(
const JSON: TJSONObject;
const UpdatedAtColumnName: String = 'UPDATED_AT';
const ReadOnly: Boolean = True
): TFDDataSet; overload;
): TFDCustomQuery; overload;

function UpdateOne(
const Script: TProc;
const UpdatedAtColumnName: String = 'UPDATED_AT';
const ReadOnly: Boolean = True
): TFDDataSet; overload;
): TFDCustomQuery; overload;

function SoftDelete(
const ColumnName: String = 'DELETED_AT';
const ReadOnly: Boolean = True
): TFDDataSet; overload;
): TFDCustomQuery; overload;

function Fetch(): TFDDataSet;
function FetchOrError(const Error: Exception): TFDDataSet;
function FetchOrNotFound(): TFDDataSet;
function Fetch(): TFDCustomQuery;
function FetchOrError(const Error: Exception): TFDCustomQuery;
function FetchOrNotFound(): TFDCustomQuery;

end;

implementation

function GetEntityName(const DataSet: TFDTable): String;
function GetEntityName(const Query: TFDCustomQuery): String;
begin

Result := DataSet.TableName;
Result := Query.Name;

end;

procedure FetchOrError(const DataSet: TFDDataSet; const Error: Exception);
procedure FetchOrError(const Query: TFDCustomQuery; const Error: Exception);
begin

if DataSet.FetchNext() = 0 then
if Query.FetchNext() = 0 then
raise Error
else
FreeAndNil(Error);

end;

procedure FetchOrNotFound(const DataSet: TFDDataSet; const Entity: String);
procedure FetchOrNotFound(const Query: TFDCustomQuery; const Entity: String);
begin

FetchOrError(DataSet, EEntidadeNaoEncontrada.Create(Entity));
FetchOrError(Query, EEntidadeNaoEncontrada.Create(Entity));

end;

Expand All @@ -185,7 +185,7 @@ function TEzQuery.&Or(const Condition: String): iEzQuery;
function TEzQuery.SoftDelete(
const ColumnName: String = 'DELETED_AT';
const ReadOnly: Boolean = True
): TFDDataSet;
): TFDCustomQuery;
var
DeletedAt : TField;

Expand All @@ -206,16 +206,16 @@ function TEzQuery.SoftDelete(

end;

function TEzQuery.Table(
const Table: TFDTable;
function TEzQuery.Query(
const Query: TFDCustomQuery;
const ExcludeDeleted: Boolean = True;
const DeleteAtColumnName: String = 'DELETED_AT'
): iEzQuery;
begin

Result := Self;

FTable := Table;
FTable := Query;
FTable.Connection := FConnection;
FTable.FetchOptions.Mode := TFDFetchMode.fmManual;
FTable.Open();
Expand All @@ -229,7 +229,7 @@ function TEzQuery.UpdateOne(
const Script: TProc;
const UpdatedAtColumnName: String = 'UPDATED_AT';
const ReadOnly: Boolean = True
): TFDDataSet;
): TFDCustomQuery;
var
UpdatedAt : TField;

Expand Down Expand Up @@ -271,7 +271,7 @@ constructor TEzQuery.Create(const Connection: TFDConnection);

end;

function TEzQuery.Fetch: TFDDataSet;
function TEzQuery.Fetch: TFDCustomQuery;
begin

Result := FTable;
Expand All @@ -280,7 +280,7 @@ function TEzQuery.Fetch: TFDDataSet;

end;

function TEzQuery.FetchOrError(const Error: Exception): TFDDataSet;
function TEzQuery.FetchOrError(const Error: Exception): TFDCustomQuery;
begin

Result := FTable;
Expand All @@ -294,7 +294,7 @@ function TEzQuery.FetchOrError(const Error: Exception): TFDDataSet;

end;

function TEzQuery.FetchOrNotFound: TFDDataSet;
function TEzQuery.FetchOrNotFound: TFDCustomQuery;
begin

Result := FTable;
Expand Down Expand Up @@ -328,7 +328,7 @@ function TEzQuery.Insert(
const JSON: TJSONObject;
const CreatedAtColumnName: String = 'CREATED_AT';
const ReadOnly: Boolean = True
): TFDDataSet;
): TFDCustomQuery;
var
CreatedAt : TField;

Expand Down Expand Up @@ -356,20 +356,20 @@ function TEzQuery.Insert(

function TEzQuery.Join(
const Alias: String;
const DataSet: TFDTable;
const Query: TFDCustomQuery;
const DataSourse: TDataSource;
const MasterFields: String
): iEzQuery;
begin

Result := Self;

DataSet.Connection := FConnection;
DataSet.FetchOptions.Mode := TFDFetchMode.fmManual;
DataSet.Name := Alias;
DataSet.MasterSource := DataSourse;
DataSet.MasterFields := MasterFields;
DataSet.Open();
Query.Connection := FConnection;
Query.FetchOptions.Mode := TFDFetchMode.fmManual;
Query.Name := Alias;
Query.MasterSource := DataSourse;
Query.MasterFields := MasterFields;
Query.Open();

end;

Expand All @@ -393,7 +393,7 @@ function TEzQuery.UpdateOne(
const JSON: TJSONObject;
const UpdatedAtColumnName: String = 'UPDATED_AT';
const ReadOnly: Boolean = True
): TFDDataSet;
): TFDCustomQuery;
var
UpdatedAt : TField;

Expand All @@ -420,17 +420,4 @@ function TEzQuery.UpdateOne(

{$ENDREGION}

{$REGION 'EntityName'}

constructor EntityName.Create(const pName: String);
begin

inherited Create();

FName := Name;

end;

{$ENDREGION}

end.
2 changes: 1 addition & 1 deletion boss.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "EzQuery",
"description": "EzQuery is a library that provides orm-like sql operations for Delphi with FireDAC",
"version": "1.0.0",
"version": "1.1.1",
"homepage": "https://github.com/Mateus-SF/EzQuery",
"mainsrc": "./",
"projects": [],
Expand Down
2 changes: 1 addition & 1 deletion boss.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "EzQuery",
"description": "EzQuery is a library that provides orm-like sql operations for Delphi with FireDAC",
"version": "1.0.0",
"homepage": "",
"homepage": "https://github.com/Mateus-SF/EzQuery",
"mainsrc": "./",
"projects": [],
"dependencies": {}
Expand Down

0 comments on commit 6808d8c

Please sign in to comment.