Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Connection_u;
- interface
- uses
- sysutils,Vcl.StdCtrls, Vcl.ExtCtrls,vcl.forms,winapi.windows,
- 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.FB, FireDAC.Phys.FBDef, Data.DB,
- FireDAC.Comp.Client;
- var
- Fconnetion:TFDConnection;
- FTransaction:TFDTransaction;
- function ExecuteQuery(const queryStr:string):boolean;//execute query on database Err=0 its ok!
- function ExecuteQueryGetId(const queryStr:string):integer;//get last id
- procedure FillMarks(combo:TComboBox);
- procedure FillSousFamille(combo:TComboBox);
- procedure FillUnitys(combo:TComboBox);
- procedure FillNomFamille(combo:Tcombobox);
- function GetDEBTCLIENT(CodeClient: integer): real;
- function GetDEBTFOURN(CODEFOURN: integer): real;
- implementation
- uses Messagestring_u;
- function GetDEBTFOURN(CODEFOURN: integer): real;
- var
- query:TFDQuery;
- begin
- result:=0;
- query:=TFDQuery.Create(nil);
- try
- if (not Assigned(Fconnetion)) or(not Fconnetion.Connected) then
- begin
- Application.MessageBox(Error_hapenNotConnected,'error',MB_OK+MB_ICONINFORMATION);
- raise Exception.Create('(not Assigned(Fconnetion)) or(not Fconnetion.Connected)');
- end;
- query.Connection:=Fconnetion;
- with query do
- begin
- sql.Clear;
- sql.Add(' SELECT coalesce(a.DEBTFOURN,0) as DEBTFOURN FROM TBLFOURNISSEUR a ');
- SQL.Add(' where a.CODEFOURN='+IntToStr(CODEFOURN));
- open;
- end;
- if query.RecordCount>0 then
- begin
- query.First;
- result:=query.FieldByName('DEBTFOURN').AsFloat;
- end;
- finally
- FreeAndNil(Query);
- end;
- end;
- //---------------------------------------------
- function GetDEBTCLIENT(CodeClient: integer): real;
- var
- query:TFDQuery;
- begin
- result:=0;
- query:=TFDQuery.Create(nil);
- try
- if (not Assigned(Fconnetion)) or(not Fconnetion.Connected) then
- begin
- Application.MessageBox(Error_hapenNotConnected,'error',MB_OK+MB_ICONINFORMATION);
- raise Exception.Create('(not Assigned(Fconnetion)) or(not Fconnetion.Connected)');
- end;
- query.Connection:=Fconnetion;
- with query do
- begin
- sql.Clear;
- sql.Add(' SELECT coalesce(a.DEBTCLIENT,0) as DEBTCLIENT FROM TBLCLIENT a ');
- SQL.Add(' where a.CODECLIENT='+IntToStr(CodeClient));
- open;
- end;
- if query.RecordCount>0 then
- begin
- query.First;
- result:=query.FieldByName('DEBTCLIENT').AsFloat;
- end;
- finally
- FreeAndNil(Query);
- end;
- end;
- //------------------------------------------------------------------
- procedure FillNomFamille(combo:Tcombobox);
- var
- query:TFDQuery;
- begin
- combo.Clear;
- query:=TFDQuery.Create(nil);
- try
- if (not Assigned(Fconnetion)) or(not Fconnetion.Connected) then
- begin
- Application.MessageBox(Error_hapenNotConnected,'error',MB_OK+MB_ICONINFORMATION);
- raise Exception.Create('(not Assigned(Fconnetion)) or(not Fconnetion.Connected)');
- end;
- query.Connection:=Fconnetion;
- with query do
- begin
- sql.Clear;
- sql.Add('SELECT a.NOMFAM FROM TBLFAMILLE a;');
- open;
- end;
- if query.RecordCount>0 then
- begin
- query.First;
- while not Query.Eof do
- begin
- combo.Items.Add(query.FieldByName('NOMFAM').AsString);
- query.Next;
- end;
- end;
- finally
- FreeAndNil(Query);
- end;
- end;
- //------------------------------------------------------------------
- //------------------------------------------------------------------
- procedure FillUnitys(combo:TComboBox);
- var
- query:TFDQuery;
- begin
- combo.Clear;
- query:=TFDQuery.Create(nil);
- try
- if (not Assigned(Fconnetion)) or(not Fconnetion.Connected) then
- begin
- Application.MessageBox(Error_hapenNotConnected,'error',MB_OK+MB_ICONINFORMATION);
- raise Exception.Create('(not Assigned(Fconnetion)) or(not Fconnetion.Connected)');
- end;
- query.Connection:=Fconnetion;
- with query do
- begin
- sql.Clear;
- sql.Add('SELECT a.NAMEUNIT FROM TBLUNIT a;');
- open;
- end;
- if query.RecordCount>0 then
- begin
- query.First;
- while not Query.Eof do
- begin
- combo.Items.Add(query.FieldByName('NAMEUNIT').AsString);
- query.Next;
- end;
- end;
- finally
- FreeAndNil(Query);
- end;
- end;
- //------------------------------------------------------------------
- //------------------------------------------------------------------
- procedure FillMarks(combo:TComboBox);
- var
- query:TFDQuery;
- begin
- combo.Clear;
- query:=TFDQuery.Create(nil);
- try
- if (not Assigned(Fconnetion)) or(not Fconnetion.Connected) then
- begin
- Application.MessageBox(Error_hapenNotConnected,'error',MB_OK+MB_ICONINFORMATION);
- raise Exception.Create('(not Assigned(Fconnetion)) or(not Fconnetion.Connected)');
- end;
- query.Connection:=Fconnetion;
- with query do
- begin
- sql.Clear;
- sql.Add('SELECT a.NOMMRQ FROM TBLMARQUE a;');
- open;
- end;
- if query.RecordCount>0 then
- begin
- query.First;
- while not Query.Eof do
- begin
- combo.Items.Add(query.FieldByName('NOMMRQ').AsString);
- query.Next;
- end;
- end;
- finally
- FreeAndNil(Query);
- end;
- end;
- //------------------------------------------------------------------
- //------------------------------------------------------------------
- procedure FillSousFamille(combo:TComboBox);
- var
- query:TFDQuery;
- begin
- combo.Clear;
- query:=TFDQuery.Create(nil);
- try
- if (not Assigned(Fconnetion)) or(not Fconnetion.Connected) then
- begin
- Application.MessageBox(Error_hapenNotConnected,'error',MB_OK+MB_ICONINFORMATION);
- raise Exception.Create('(not Assigned(Fconnetion)) or(not Fconnetion.Connected)');
- end;
- query.Connection:=Fconnetion;
- with query do
- begin
- sql.Clear;
- sql.Add('SELECT a.NOMSOUSFAM FROM TBLSOUSFAMILLE a;');
- open;
- end;
- if query.RecordCount>0 then
- begin
- query.First;
- while not Query.Eof do
- begin
- combo.Items.Add(query.FieldByName('NOMSOUSFAM').AsString);
- query.Next;
- end;
- end;
- finally
- FreeAndNil(Query);
- end;
- end;
- //------------------------------------------------------------------
- //-----------------------------------------------------------------------------------
- function ExecuteQueryGetId(const queryStr:string):integer;//get last id
- var
- ObjQuery:TFDQuery;
- begin
- result:=-1;
- ObjQuery := TFDQuery.Create(nil);
- try
- try
- if not Assigned(Fconnetion) then
- raise Exception.Create('not Assigned(Fconnetion) ');
- ObjQuery.Connection:=Fconnetion;
- with ObjQuery do
- begin
- close;
- SQL.Clear;
- SQL.Add(queryStr);
- open;
- end;
- if ObjQuery.RecordCount>0 then
- result:=ObjQuery.fields[0].AsInteger;
- except
- on E:Exception do
- begin
- result:=-1;
- Application.MessageBox(pwidechar(e.Message),'Error',mb_ok+MB_ICONINFORMATION);
- end;
- end;
- finally
- FreeAndNil(ObjQuery);
- end;
- end;
- //-----------------------------------------------------------------------------------
- //-----------------------------------------------------------------------------------
- function ExecuteQuery(const queryStr:string):boolean;
- var
- ObjQuery:TFDQuery;
- begin
- result:=false;
- ObjQuery := TFDQuery.Create(nil);
- try
- try
- if not Assigned(Fconnetion) then
- raise Exception.Create('not Assigned(Fconnetion) ');
- ObjQuery.Connection:=Fconnetion;
- with ObjQuery do
- begin
- close;
- SQL.Clear;
- SQL.Add(queryStr);
- open;
- end;
- if ObjQuery.RecordCount>0 then
- result:=ObjQuery.FieldByName('Err').AsInteger=0;
- except
- on E:Exception do
- begin
- result:=false;
- Application.MessageBox(pwidechar(e.Message),'Error',mb_ok+MB_ICONINFORMATION);
- end;
- end;
- finally
- FreeAndNil(ObjQuery);
- end;
- end;
- //-----------------------------------------------------------------------------------
- initialization
- if not Assigned(FTransaction) then
- begin
- FTransaction:=TFDTransaction.Create(nil);
- FTransaction.Options.AutoCommit:=True;
- FTransaction.Options.AutoStart:=True;
- end;
- if not Assigned(Fconnetion) then
- begin
- Fconnetion:=TFDConnection.Create(nil);
- Fconnetion.DriverName:='FB';
- Fconnetion.Transaction:=FTransaction;
- FTransaction.Connection:=Fconnetion;//transaction
- Fconnetion.Params.UserName:='sysdba';
- Fconnetion.Params.Password:='masterkey';
- Fconnetion.Params.Add('Characterset=Utf8');//utf8
- Fconnetion.LoginPrompt:=False;
- Fconnetion.Params.Database:='';
- Fconnetion.Connected:=false;
- Fconnetion.Close;
- end;
- finalization
- if Assigned(Fconnetion) then
- begin
- Fconnetion.Close;
- FreeAndNil(Fconnetion);
- end;
- if Assigned(FTransaction) then
- begin
- FreeAndNil(FTransaction);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement