Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit UModel.DAO.SQL;
- interface
- uses
- Data.DB,
- System.Generics.Collections,
- System.SysUtils,
- SimpleInterface,
- SimpleDAO,
- SimpleAttributes,
- SimpleQueryFireDac,
- UModel.DAOGeneric.Interfaces,
- UModel.Connection.Firedac,
- UModel.FactoryConnectionFireDac,
- FireDAC.Comp.Client;
- type
- TModelDAOSQL<T : class, constructor> = class(TInterfacedObject, iDAOGeneric<T>)
- private
- [weak]
- FQuery : iSimpleQuery;
- FConn : TFDConnection;
- FDAO : iSimpleDAO<T>;
- FDataSource : TDataSource;
- i : integer;
- FObject : T;
- public
- constructor Create;
- destructor Destroy; override;
- class function New : iDAOGeneric<T>;
- function Insert : iDAOGeneric<T>;
- function Update : iDAOGeneric<T>;
- function Find : iDAOGeneric<T>; overload;
- function Find(const aID : String) : iDAOGeneric<T>; overload;
- function Find(var aList : TObjectList<T>) : iDAOGeneric<T>; overload;
- function Find(aParam : String; aValue : String) : iDAOGeneric<T>; overload;
- function Fields(aFields : String) : iDAOGeneric<T>;
- function Where(aWhere : String) : iDAOGeneric<T>;
- function OrderBy(aField : String) : iDAOGeneric<T>;
- function Delete : iDAOGeneric<T>;
- function DataSource(aValue : TDataSource) : iDAOGeneric<T>;
- function Current : T;
- function NewObject : T;
- function DataSet : TDataSet;
- end;
- implementation
- { TModelDAOSQL<T> }
- constructor TModelDAOSQL<T>.Create;
- begin
- FDataSource := TDataSource.Create(nil);
- FConn := TModelFactoryConnectionFireDac.New
- .Connection
- .Parameters
- .DriveName('FB')
- .DriveID('FB')
- .DataBase('C:\Trabalho\Dados\TABELAS_SISCOM.FDB')
- .UserName('SYSDBA')
- .Password('masterkey')
- .Server('localhost')
- .Porta(3050)
- .&EndParams
- .Connect
- .GetConnection;
- FQuery := TSimplequeryFireDac.New(FConn);
- FDAO := TSimpleDAO<T>.New(FQuery).DataSource(FDataSource);
- end;
- function TModelDAOSQL<T>.Current: T;
- begin
- Result := FObject;
- end;
- function TModelDAOSQL<T>.DataSet: TDataSet;
- begin
- Result := FDataSource.DataSet;
- end;
- function TModelDAOSQL<T>.DataSource(aValue: TDataSource): iDAOGeneric<T>;
- begin
- Result := Self;
- aValue.DataSet := FDataSource.DataSet;
- end;
- function TModelDAOSQL<T>.Delete: iDAOGeneric<T>;
- begin
- Result := Self;
- FDAO.Delete(FObject);
- end;
- destructor TModelDAOSQL<T>.Destroy;
- begin
- FDataSource.Free;
- if Assigned(FObject) then
- FObject.Free;
- inherited;
- end;
- function TModelDAOSQL<T>.Fields(aFields: String): iDAOGeneric<T>;
- begin
- Result := Self;
- FDAO.SQL.Fields(aFields);
- end;
- function TModelDAOSQL<T>.Find(aParam, aValue: String): iDAOGeneric<T>;
- begin
- Result := Self;
- FDAO.SQL.Where(aParam + ' = ' + aValue).&End.Find;
- end;
- function TModelDAOSQL<T>.Find(var aList: TObjectList<T>): iDAOGeneric<T>;
- begin
- Result := Self;
- FDAO.Find(aList);
- end;
- function TModelDAOSQL<T>.Find(const aID: String): iDAOGeneric<T>;
- begin
- Result := Self;
- FDAO.Find(StrToInt(aID));
- end;
- function TModelDAOSQL<T>.Find: iDAOGeneric<T>;
- begin
- Result := Self;
- FDAO.Find;
- end;
- function TModelDAOSQL<T>.Insert: iDAOGeneric<T>;
- begin
- Result := Self;
- FDAO.Insert(FObject);
- end;
- class function TModelDAOSQL<T>.New: iDAOGeneric<T>;
- begin
- Result := Self.Create;
- end;
- function TModelDAOSQL<T>.NewObject: T;
- begin
- if Assigned(FObject) then
- FObject.Free;
- FObject := T.Create;
- Result := FObject;
- end;
- function TModelDAOSQL<T>.OrderBy(aField: String): iDAOGeneric<T>;
- begin
- Result := Self;
- FDAO.SQL.OrderBy(aField).&End;
- end;
- function TModelDAOSQL<T>.Update: iDAOGeneric<T>;
- begin
- Result := Self;
- FDAO.Update(FObject);
- end;
- function TModelDAOSQL<T>.Where(aWhere: String): iDAOGeneric<T>;
- begin
- Result := Self;
- FDAO.SQL.Where(aWhere).&End;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement