unit Editor; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.Grids, Vcl.ExtCtrls, EmpMst, NeolTestMain, System.TypInfo; type TfrmEditor = class(TForm) Panel1: TPanel; StringGrid1: TStringGrid; Panel2: TPanel; BitBtn1: TBitBtn; BitBtn2: TBitBtn; BitBtn3: TBitBtn; procedure FormCreate(Sender: TObject); procedure FormShow(Sender: TObject); procedure BitBtn2Click(Sender: TObject); procedure BitBtn1Click(Sender: TObject); private { Private declarations } FobjEmp: TEmpMst; procedure xShowEmpMstInfo; procedure xUpdateObjEmpMst; procedure xCreateUpdateSql; procedure xCreateInsertSql(curEmp: TEmpMst); function xNewObjEmpMst: TEmpMst; public { Public declarations } property objEmp: TEmpMst read FobjEmp write FobjEmp; end; var frmEditor: TfrmEditor; implementation {$R *.dfm} procedure TfrmEditor.BitBtn1Click(Sender: TObject); var TimeStamp: string; Rand3Digit: string; curEmp: TEmpMst; begin curEmp := xNewObjEmpMst; TimeStamp := FormatDateTime('yyyymmddhhnnsszzz', Now); Rand3Digit := Format('%.3d', [Random(1000)]); curEmp.OBID := curEmp.wClassName + '-' + TimeStamp + '-' + Rand3Digit + '-NEOL'; xCreateInsertSql(curEmp); ModalResult := mrOk; curEmp.Free; end; procedure TfrmEditor.BitBtn2Click(Sender: TObject); begin xUpdateObjEmpMst; xCreateUpdateSql; ModalResult := mrOk; end; procedure TfrmEditor.FormCreate(Sender: TObject); begin FobjEmp := nil; end; procedure TfrmEditor.FormShow(Sender: TObject); var tmpStrs: TStrings; begin tmpStrs := TStringList.Create; tmpStrs.Add('¼Ó¼º'); tmpStrs.Add('OBID'); tmpStrs.Add('Name'); tmpStrs.Add('wClassName'); tmpStrs.Add('CreateDate'); tmpStrs.Add('CreateTime'); tmpStrs.Add('CreatorID'); tmpStrs.Add('IsValid'); tmpStrs.Add('OrdCode'); tmpStrs.Add('Position'); tmpStrs.Add('Role'); tmpStrs.Add('InDate'); tmpStrs.Add('OutDate'); tmpStrs.Add('Status'); tmpStrs.Add('EmpTelNum'); tmpStrs.Add('EmpAddress'); tmpStrs.Add('Descriptions'); StringGrid1.RowCount := tmpStrs.Count; StringGrid1.Cols[0].AddStrings(tmpStrs); StringGrid1.Cells[1,0] := '°ª'; tmpStrs.Free; xShowEmpMstInfo; frmNeolTestMain.xAutoResizeStringGridColumns(StringGrid1, Canvas); StringGrid1.ColWidths[1] := 280; end; procedure TfrmEditor.xCreateInsertSql(curEmp: TEmpMst); var i, cnt: Integer; sqlStr, attrStr, valStr: string; attrStrs, valStrs: TStrings; PropList: PPropList; begin sqlStr := 'INSERT INTO ' + frmNeolTestMain.xAddSqlDoubleMark(curEmp.wClassName); attrStrs := TStringList.Create; valStrs := TStringList.Create; cnt := GetPropList(curEmp,PropList); for i := 0 to cnt-1 do begin attrStrs.Add(PropList[i].Name); valStrs.Add(GetPropValue(curEmp,PropList[i].Name,True)); end; FreeMem(PropList); attrStr := ''; for i := 0 to attrStrs.Count-1 do begin if i = 0 then attrStr := ' (' + frmNeolTestMain.xAddSqlDoubleMark(attrStrs.Strings[i]) else attrStr := attrStr + ', ' + frmNeolTestMain.xAddSqlDoubleMark(attrStrs.Strings[i]); end; attrStr := attrStr + ') '; sqlStr := sqlStr + attrStr; valStr := ''; for i := 0 to valStrs.Count-1 do begin if i = 0 then valStr := 'VALUES (' + frmNeolTestMain.xAddSqlMark(valStrs.Strings[i]) else valStr := valStr + ', ' + frmNeolTestMain.xAddSqlMark(valStrs.Strings[i]); end; sqlStr := sqlStr + valStr; sqlStr := sqlStr + ')'; sqlStr := sqlStr + ';'; frmNeolTestMain.Memo2.Lines.Add(sqlStr); if frmNeolTestMain.FDConnection1.Connected then begin frmNeolTestMain.FDQuery1.SQL.Text := sqlStr; frmNeolTestMain.FDQuery1.ExecSQL; end; attrStrs.Free; valStrs.Free; end; procedure TfrmEditor.xCreateUpdateSql; var i, cnt, idx: Integer; upStrs, attrStrs, valStrs, setStrs: TStrings; attrStr, valStr, sqlStr, setStr: string; PropList: PPropList; begin upStrs := TStringList.Create; upStrs.Add('Name'); upStrs.Add('CreateDate'); upStrs.Add('CreateTime'); upStrs.Add('CreatorID'); upStrs.Add('IsValid'); upStrs.Add('OrdCode'); upStrs.Add('Position'); upStrs.Add('Role'); upStrs.Add('InDate'); upStrs.Add('OutDate'); upStrs.Add('Status'); upStrs.Add('EmpTelNum'); upStrs.Add('EmpAddress'); upStrs.Add('Descriptions'); attrStrs := TStringList.Create; valStrs := TStringList.Create; //°´Ã¼ÀÇ ¼Ó¼º°ú °ªÀ» ºÐ¸®ÇÑ´Ù.. cnt := GetPropList(FobjEmp,PropList); for i := 0 to cnt-1 do begin attrStrs.Add(PropList[i].Name); valStrs.Add(GetPropValue(FobjEmp,PropList[i].Name,True)); end; FreeMem(PropList); //¾÷µ« ¹®ÀåÀ» ¸¸µç´Ù.. setStrs := TStringList.Create; for i := 0 to upStrs.Count-1 do begin idx := attrStrs.IndexOf(upStrs.Strings[i]); if idx > -1 then begin attrStr := attrStrs.Strings[idx]; valStr := valStrs.Strings[idx]; if Length(Trim(valStr)) = 0 then setStrs.Add(frmNeolTestMain.xAddSqlDoubleMark(attrStr) + ' = NULL') else setStrs.Add(frmNeolTestMain.xAddSqlDoubleMark(attrStr) + ' = ' + frmNeolTestMain.xAddSqlMark(valStr)); end; end; upStrs.Free; //update Àý.. sqlStr := 'UPDATE ' + frmNeolTestMain.xAddSqlDoubleMark(FobjEmp.wClassName); //set Àý.. for i := 0 to setStrs.Count-1 do begin if i = 0 then setStr := setStrs.Strings[i] else setStr := setStr + ', ' + setStrs.Strings[i]; end; sqlStr := sqlStr + ' SET ' + setStr; //WHERE Àý.. sqlStr := sqlStr + ' WHERE ' + frmNeolTestMain.xAddSqlDoubleMark('OBID') + ' = ' + frmNeolTestMain.xAddSqlMark(FobjEmp.OBID); sqlStr := sqlStr + ';'; frmNeolTestMain.Memo2.Lines.Add(sqlStr); if frmNeolTestMain.FDConnection1.Connected then begin frmNeolTestMain.FDQuery1.SQL.Text := sqlStr; frmNeolTestMain.FDQuery1.ExecSQL; end; attrStrs.Free; valStrs.Free; end; function TfrmEditor.xNewObjEmpMst: TEmpMst; var cidx, ridx: Integer; begin Result := TEmpMst.Create(Self); cidx := 1; ridx := StringGrid1.Cols[0].IndexOf('OBID'); Result.OBID := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('Name'); Result.Name := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('wClassName'); Result.wClassName := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('CreateDate'); Result.CreateDate := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('CreateTime'); Result.CreateTime := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('CreatorID'); Result.CreatorID := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('IsValid'); Result.IsValid := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('OrdCode'); Result.OrdCode := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('Position'); Result.Position := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('Role'); Result.Role := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('InDate'); Result.InDate := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('OutDate'); Result.OutDate := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('Status'); Result.Status := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('EmpTelNum'); Result.EmpTelNum := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('EmpAddress'); Result.EmpAddress := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('Descriptions'); Result.Descriptions := Trim(StringGrid1.Cells[cidx,ridx]); end; procedure TfrmEditor.xShowEmpMstInfo; var cidx, ridx: Integer; begin cidx := 1; ridx := StringGrid1.Cols[0].IndexOf('OBID'); StringGrid1.Objects[cidx,ridx] := FobjEmp; StringGrid1.Cells[cidx,ridx] := FobjEmp.OBID; ridx := StringGrid1.Cols[0].IndexOf('Name'); StringGrid1.Cells[cidx,ridx] := FobjEmp.Name; ridx := StringGrid1.Cols[0].IndexOf('wClassName'); StringGrid1.Cells[cidx,ridx] := FobjEmp.wClassName; ridx := StringGrid1.Cols[0].IndexOf('CreateDate'); StringGrid1.Cells[cidx,ridx] := FobjEmp.CreateDate; ridx := StringGrid1.Cols[0].IndexOf('CreateTime'); StringGrid1.Cells[cidx,ridx] := FobjEmp.CreateTime; ridx := StringGrid1.Cols[0].IndexOf('CreatorID'); StringGrid1.Cells[cidx,ridx] := FobjEmp.CreatorID; ridx := StringGrid1.Cols[0].IndexOf('IsValid'); StringGrid1.Cells[cidx,ridx] := FobjEmp.IsValid; ridx := StringGrid1.Cols[0].IndexOf('OrdCode'); StringGrid1.Cells[cidx,ridx] := FobjEmp.OrdCode; ridx := StringGrid1.Cols[0].IndexOf('Position'); StringGrid1.Cells[cidx,ridx] := FobjEmp.Position; ridx := StringGrid1.Cols[0].IndexOf('Role'); StringGrid1.Cells[cidx,ridx] := FobjEmp.Role; ridx := StringGrid1.Cols[0].IndexOf('InDate'); StringGrid1.Cells[cidx,ridx] := FobjEmp.InDate; ridx := StringGrid1.Cols[0].IndexOf('OutDate'); StringGrid1.Cells[cidx,ridx] := FobjEmp.OutDate; ridx := StringGrid1.Cols[0].IndexOf('Status'); StringGrid1.Cells[cidx,ridx] := FobjEmp.Status; ridx := StringGrid1.Cols[0].IndexOf('EmpTelNum'); StringGrid1.Cells[cidx,ridx] := FobjEmp.EmpTelNum; ridx := StringGrid1.Cols[0].IndexOf('EmpAddress'); StringGrid1.Cells[cidx,ridx] := FobjEmp.EmpAddress; ridx := StringGrid1.Cols[0].IndexOf('Descriptions'); StringGrid1.Cells[cidx,ridx] := FobjEmp.Descriptions; end; procedure TfrmEditor.xUpdateObjEmpMst; var cidx, ridx: Integer; begin cidx := 1; ridx := StringGrid1.Cols[0].IndexOf('OBID'); FobjEmp.OBID := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('Name'); FobjEmp.Name := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('wClassName'); FobjEmp.wClassName := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('CreateDate'); FobjEmp.CreateDate := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('CreateTime'); FobjEmp.CreateTime := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('CreatorID'); FobjEmp.CreatorID := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('IsValid'); FobjEmp.IsValid := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('OrdCode'); FobjEmp.OrdCode := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('Position'); FobjEmp.Position := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('Role'); FobjEmp.Role := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('InDate'); FobjEmp.InDate := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('OutDate'); FobjEmp.OutDate := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('Status'); FobjEmp.Status := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('EmpTelNum'); FobjEmp.EmpTelNum := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('EmpAddress'); FobjEmp.EmpAddress := Trim(StringGrid1.Cells[cidx,ridx]); ridx := StringGrid1.Cols[0].IndexOf('Descriptions'); FobjEmp.Descriptions := Trim(StringGrid1.Cells[cidx,ridx]); end; end.