OBS/Auswertung/SQL Beispiel 1

Aus OBS Wiki
Wechseln zu: Navigation, Suche

Beispiel 1


//-----------------------------------------------------------------------------
// Unit Name: SqlExporter
// Author:    Jr
// Date:      04-Aug-2011
// Purpose:
// History:
//
// Folgende implementiere Funktionen stehen dem SQL-Exporter zur Verfügung
//
// function  GetSqlExportStr(oObj:TxDCScripter):String;
// procedure SetSQlExportStr(oObj:TxDCScripter; cSql:String);
// function  RunSQlExporter(oObj:TxDCScripter; lAuto:Boolean):Boolean;
// function  SaveDataSQlExporter(oDCScripter:TxDCScripter;cFileName:String; nWahl:Integer;lAuto:Boolean):Boolean;
// function  ShowDataSQlExporter(oDCScripter:TxDCScripter):Boolean;
//
//-----------------------------------------------------------------------------

program SQLEXport;

uses dialogs;

var oForm  : TForm;
    lCheck : Boolean;
    oDC    : TDCScripter;

Const CSV    = 2;
      EXCEL  = 3;
      HTML   = 4; //Nur im Grid Möglich

implementation

function FindControl(oControl:TControl;cName:String):TControl;
var i : integer;
begin
    Result := NIL;

    if (Assigned(oControl)) then begin

        cName  := UpperCase(cName);

        i := 0;
        while (i<oControl.ControlCount) do begin
            //DebugLine(oControl.Controls[i].ClassName+'->'+oControl.Controls[i].Name);
            if (UpperCase(oControl.Controls[i].Name) = cName) then begin
                //DebugLine('Found '+cName);
                Result := oControl.Controls[i];
                i:=oControl.ControlCount;
            end else if (oControl.Controls[i].ClassName = 'TPanel') then begin
                Result := FindControl(oControl.Controls[i],cName);
                if (Assigned(Result)) then begin
                    i:=oControl.ControlCount;
                end;
            end;
            Inc(i);
        end;

    end;
end;

//-----------------------------------------------------------------------------
//  Procedure: Not available
//  Author:    Jr
//  Date:      04-Aug-2011
//  Arguments: Not available
//  Result:    Not available
//-----------------------------------------------------------------------------

procedure ConvertResourceData(cName:String;oComponent:TComponent);
begin
    //Hier können noch veränderungen vorgenommen werden
    //DebugLine(oComponent.ClassName);

    if (oComponent.ClassName = 'TPanel') then begin
        oComponent.Caption := '';
    end;
end;

//-----------------------------------------------------------------------------
//  Procedure: Not available
//  Author:    Jr
//  Date:      04-Aug-2011
//  Arguments: Not available
//  Result:    Not available
//-----------------------------------------------------------------------------

// Die nächste Zeile nicht entfernen
//$RESOURCE START//


Procedure Init_Resource(Sender:TObject);
var
MainPanel:TPanel;
Label1:TLabel;
Label2:TLabel;
bt_ok:TSpeedButton;
bt_abbruch:TSpeedButton;
MaskEdit1:TMaskEdit;
MaskEdit2:TMaskEdit;
begin
     MainPanel:=TPanel.Create(Sender);
     with MainPanel do begin
          name:='MainPanel';
          parent := Sender;
          Left := 5;
          Top := 7;
          Width := 223;
          Height := 119;
          Cursor := crArrow;
          TabOrder := 0;
          Label1:=TLabel.Create(Sender);
          with Label1 do begin
               name:='Label1';
               parent:=MainPanel;
               Left := 22;
               Top := 24;
               Width := 87;
               Height := 13;
               Cursor := crArrow;
               Caption := 'Von Warengruppe';
               ConvertResourceData('Label1',Label1);
          end;
          Label2:=TLabel.Create(Sender);
          with Label2 do begin
               name:='Label2';
               parent:=MainPanel;
               Left := 22;
               Top := 45;
               Width := 94;
               Height := 13;
               Cursor := crArrow;
               Caption := 'Nach Warengruppe';
               ConvertResourceData('Label2',Label2);
          end;
          bt_ok:=TSpeedButton.Create(Sender);
          with bt_ok do begin
               name:='bt_ok';
               parent:=MainPanel;
               Left := 22;
               Top := 73;
               Width := 91;
               Height := 27;
               Cursor := crArrow;
               Caption := '&OK';
               ConvertResourceData('bt_ok',bt_ok);
          end;
          bt_abbruch:=TSpeedButton.Create(Sender);
          with bt_abbruch do begin
               name:='bt_abbruch';
               parent:=MainPanel;
               Left := 126;
               Top := 73;
               Width := 78;
               Height := 28;
               Cursor := crArrow;
               Caption := '&Abbruch';
               ConvertResourceData('bt_abbruch',bt_abbruch);
          end;
          MaskEdit1:=TMaskEdit.Create(Sender);
          with MaskEdit1 do begin
               name:='MaskEdit1';
               parent:=MainPanel;
               Left := 130;
               Top := 17;
               Width := 53;
               Height := 21;
               Cursor := crArrow;
               EditMask := ' 000;1;_';
               TabOrder := 0;
               Text := '    ';
               ConvertResourceData('MaskEdit1',MaskEdit1);
          end;
          MaskEdit2:=TMaskEdit.Create(Sender);
          with MaskEdit2 do begin
               name:='MaskEdit2';
               parent:=MainPanel;
               Left := 129;
               Top := 40;
               Width := 53;
               Height := 21;
               Cursor := crArrow;
               Constraints.MaxHeight := 111;
               EditMask := ' 000;1;_';
               TabOrder := 1;
               Text := '    ';
               ConvertResourceData('MaskEdit2',MaskEdit2);
          end;
          ConvertResourceData('MainPanel',MainPanel);
     end;
end;

//$RESOURCE END//
// Diese vorherige Zeile nicht entfernen

//-----------------------------------------------------------------------------
//  Procedure: Not available
//  Author:    Jr
//  Date:      04-Aug-2011
//  Arguments: Not available
//  Result:    Not available
//-----------------------------------------------------------------------------

procedure MyFormClose(Sender: TObject;  var Action: TCloseAction);
var oControl : TControl;
    cVonWGR  : String;
    cBisWGR  : String;
    cSql     : String;
    ooo : TfrmRunSQLExport;
begin

    if (lCheck) then begin

        Action  := caNone;

        cBisWGR := '';
        cVonWGR := '';

        oControl := FindControl(oForm,'MaskEdit1');

        if (Assigned(oControl)) then begin
            cVonWGR := oControl.Text;
        end;

        oControl := FindControl(oForm,'MaskEdit2');

        if (Assigned(oControl)) then begin
            cBisWGR := oControl.Text;
        end;

        if (Empty(cVonWgr) or Empty(cBisWgr)) then begin
            Meldung('Von und Bis müssen gefüllt sein!');
        end else begin

            cSQL := GetSqlExportStr(oDC);

            if (InStr('$1',cSQL) and InStr('$2',cSQL)) then begin

                cSQL := StrTran(cSql,'$1',cVonWGR);
                cSQL := StrTran(cSql,'$2',cBisWGR);

                SetSqlExportStr(oDC,cSQL);

                Action := caFree;
            end else begin
                Meldung('SQL Statement enthält keine Platzhalter');
                Action := caFree;
            end;

        end;

    end;
end;

procedure My_Bt_OK(Sender: TObject);
begin
    lCheck := True;
    oForm.Close;
end;

procedure My_Bt_Abbruch(Sender: TObject);
begin
    lCheck := False;
    oForm.Close;
end;

//-----------------------------------------------------------------------------
//  Procedure: Not available
//  Author:    Jr
//  Date:      04-Aug-2011
//  Arguments: Not available
//  Result:    Not available
//-----------------------------------------------------------------------------

procedure Init_MainForm();
var oControl : TControl;
begin
   oForm        := TForm.Create(Application);
   Init_Resource(oForm);

   oDC.CreateEvent(oForm,'OnClose','MyFormClose',True);

   oControl := FindControl(oForm,'bt_ok');
   oDC.CreateEvent(oControl,'OnClick','My_Bt_OK',True);

   oControl := FindControl(oForm,'bt_abbruch');
   oDC.CreateEvent(oControl,'OnClick','My_Bt_Abbruch',True);

   oControl := FindControl(oForm,'MainPanel');

   if (Assigned(oControl)) then begin

       oControl.Left := 0;
       oControl.Top  := 0;

       oForm.ClientHeight := oControl.Height;
       oForm.ClientWidth  := oControl.Width;

       oForm.Constraints.MinHeight := oForm.Height;
       oForm.Constraints.MaxHeight := oForm.Height;
       oForm.Constraints.MinWidth  := oForm.Width;
       oForm.Constraints.MaxWidth  := oForm.Width;

   end else begin
       oForm.Width  := 400;
       oForm.Height := 200;
   end;

end;

//-----------------------------------------------------------------------------
//  Procedure: Not available
//  Author:    Jr
//  Date:      04-Aug-2011
//  Arguments: Not available
//  Result:    Not available
//-----------------------------------------------------------------------------

procedure StartProc(oMyDC:TDCScripter; cParameter:String);
begin
    oDC    := oMyDC;
    lCheck := False;

    Init_MainForm();

    oForm.Position := poMainFormCenter;
    oForm.ShowModal;

end;

//-----------------------------------------------------------------------------

end.

Auto import 15:33, 25. Aug. 2011 (CEST)