delphimvcframework/tools/entitygenerator/MainFormU.pas

534 lines
16 KiB
ObjectPascal
Raw Normal View History

2018-09-27 12:26:50 +02:00
unit MainFormU;
interface
uses
Winapi.Windows,
Winapi.Messages,
System.SysUtils,
System.Variants,
System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
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.Stan.Param,
FireDAC.DatS,
FireDAC.DApt.Intf,
FireDAC.DApt,
FireDAC.VCLUI.Wait,
FireDAC.Comp.UI,
FireDAC.Phys.IBBase,
FireDAC.Phys.FB,
Data.DB,
FireDAC.Comp.DataSet,
FireDAC.Comp.Client,
Vcl.StdCtrls,
Vcl.ExtCtrls,
FireDAC.Phys.ODBCBase,
FireDAC.Phys.MSSQL,
FireDAC.Phys.MSSQLDef,
FireDAC.Phys.FBDef,
Vcl.ComCtrls,
Vcl.Grids,
Vcl.ValEdit,
FireDAC.Phys.MySQLDef,
FireDAC.Phys.MySQL,
FireDAC.Phys.PGDef,
FireDAC.Phys.PG,
FireDAC.Phys.IBDef,
FireDAC.Phys.IB,
FireDAC.Stan.ExprFuncs,
FireDAC.Phys.SQLiteDef,
2019-11-04 11:14:25 +01:00
FireDAC.Phys.SQLite, Vcl.DBGrids;
2018-09-27 12:26:50 +02:00
type
TMainForm = class(TForm)
FDConnection1: TFDConnection;
Panel1: TPanel;
Panel2: TPanel;
qry: TFDQuery;
FDPhysFBDriverLink1: TFDPhysFBDriverLink;
FDGUIxWaitCursor1: TFDGUIxWaitCursor;
Splitter1: TSplitter;
mmConnectionParams: TMemo;
Label2: TLabel;
FDPhysMSSQLDriverLink1: TFDPhysMSSQLDriverLink;
cboConnectionDefs: TComboBox;
Panel3: TPanel;
Panel4: TPanel;
btnGenEntities: TButton;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
btnGetTables: TButton;
mmOutput: TMemo;
Panel5: TPanel;
btnSaveCode: TButton;
FileSaveDialog1: TFileSaveDialog;
FDPhysMySQLDriverLink1: TFDPhysMySQLDriverLink;
FDPhysPgDriverLink1: TFDPhysPgDriverLink;
FDPhysFBDriverLink2: TFDPhysFBDriverLink;
FDPhysIBDriverLink1: TFDPhysIBDriverLink;
FDPhysMySQLDriverLink2: TFDPhysMySQLDriverLink;
FDPhysSQLiteDriverLink1: TFDPhysSQLiteDriverLink;
2019-11-04 11:14:25 +01:00
dsTablesMapping: TFDMemTable;
dsTablesMappingTABLE_NAME: TStringField;
dsTablesMappingCLASS_NAME: TStringField;
DBGrid1: TDBGrid;
dsrcTablesMapping: TDataSource;
Panel6: TPanel;
GroupBox1: TGroupBox;
lstSchema: TListBox;
lstCatalog: TListBox;
btnRefreshCatalog: TButton;
Label1: TLabel;
chGenerateMapping: TCheckBox;
2018-09-27 12:26:50 +02:00
procedure btnGenEntitiesClick(Sender: TObject);
procedure btnGetTablesClick(Sender: TObject);
procedure btnSaveCodeClick(Sender: TObject);
procedure cboConnectionDefsChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
2019-11-04 11:14:25 +01:00
procedure lstCatalogDblClick(Sender: TObject);
procedure btnRefreshCatalogClick(Sender: TObject);
procedure mmConnectionParamsChange(Sender: TObject);
procedure lstSchemaDblClick(Sender: TObject);
2018-09-27 12:26:50 +02:00
private
2019-11-04 11:14:25 +01:00
fCatalog: string;
fSchema: string;
fIntfBuff, fImplBuff, fInitializationBuff: TStringStream;
2018-09-27 12:26:50 +02:00
FHistoryFileName: string;
lTypesName: TArray<string>;
procedure EmitHeaderComments;
2018-09-27 12:26:50 +02:00
function GetClassName(const aTableName: string): string;
procedure EmitUnit;
procedure EmitUnitEnd;
procedure EmitProperty(F: TField);
2019-11-04 11:14:25 +01:00
procedure EmitField(F: TField; const IsPK: Boolean);
2018-09-27 12:26:50 +02:00
procedure EmitClass(const aTableName, aClassName: string);
procedure EmitClassEnd;
function GetDelphiType(FT: TFieldType): string;
function GetFieldName(const Value: string): string;
2018-09-27 12:26:50 +02:00
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
uses
2019-11-04 11:14:25 +01:00
{Spring.SystemUtils,} System.IOUtils, System.TypInfo;
2018-09-27 12:26:50 +02:00
{$R *.dfm}
2019-11-04 11:14:25 +01:00
const
INDENT = ' ';
2018-09-27 12:26:50 +02:00
procedure TMainForm.btnGenEntitiesClick(Sender: TObject);
var
I: Integer;
lTableName: string;
lClassName: string;
F: Integer;
lFieldsName: TArray<string>;
2019-11-04 11:14:25 +01:00
lKeyFields: TStringList;
2018-09-27 12:26:50 +02:00
begin
fIntfBuff.Clear;
2019-11-04 11:14:25 +01:00
fImplBuff.Clear;
fInitializationBuff.Clear;
2019-11-04 11:14:25 +01:00
lKeyFields := TStringList.Create;
try
EmitHeaderComments;
EmitUnit;
dsTablesMapping.First;
I := 0;
while not dsTablesMapping.Eof do
2018-09-27 12:26:50 +02:00
begin
2019-11-04 11:14:25 +01:00
lTableName := dsTablesMappingTABLE_NAME.AsString;
lClassName := dsTablesMappingCLASS_NAME.AsString;
EmitClass(lTableName, lClassName);
qry.Open('select * from ' + lTableName + ' where 1=0');
FDConnection1.GetKeyFieldNames(fCatalog, fSchema, lTableName, '', lKeyFields);
lFieldsName := [];
lTypesName := [];
fIntfBuff.WriteString(INDENT + 'private' + sLineBreak);
for F := 0 to qry.Fields.Count - 1 do
begin
EmitField(qry.Fields[F], lKeyFields.IndexOf(qry.Fields[F].FieldName) > -1);
2019-11-04 11:14:25 +01:00
if GetDelphiType(qry.Fields[F].DataType) = 'TStream' then
begin
lFieldsName := lFieldsName + [GetFieldName(qry.Fields[F].FieldName)];
lTypesName := lTypesName + ['TMemoryStream'];
end;
end;
2019-11-04 11:14:25 +01:00
fIntfBuff.WriteString(INDENT + 'public' + sLineBreak);
fIntfBuff.WriteString(INDENT + ' constructor Create; override;' + sLineBreak);
2019-11-04 11:14:25 +01:00
fImplBuff.WriteString('constructor ' + lClassName + '.Create;' + sLineBreak);
fImplBuff.WriteString('begin' + sLineBreak);
fImplBuff.WriteString(' inherited Create;' + sLineBreak);
for F := low(lFieldsName) to high(lFieldsName) do
begin
fImplBuff.WriteString(' ' + lFieldsName[F] + ' := ' + lTypesName[F] + '.Create;' + sLineBreak);
end;
fImplBuff.WriteString('end;' + sLineBreak + sLineBreak);
2019-11-04 11:14:25 +01:00
fIntfBuff.WriteString(INDENT + ' destructor Destroy; override;' + sLineBreak);
fImplBuff.WriteString('destructor ' + lClassName + '.Destroy;' + sLineBreak);
fImplBuff.WriteString('begin' + sLineBreak);
for F := low(lFieldsName) to high(lFieldsName) do
begin
fImplBuff.WriteString(' ' + lFieldsName[F] + '.Free;' + sLineBreak);
end;
fImplBuff.WriteString(' inherited;' + sLineBreak);
fImplBuff.WriteString('end;' + sLineBreak + sLineBreak);
2019-11-04 11:14:25 +01:00
for F := 0 to qry.Fields.Count - 1 do
begin
EmitProperty(qry.Fields[F]);
end;
2018-09-27 12:26:50 +02:00
2019-11-04 11:14:25 +01:00
EmitClassEnd;
dsTablesMapping.Next;
2018-09-27 12:26:50 +02:00
end;
2019-11-04 11:14:25 +01:00
EmitUnitEnd;
mmOutput.Lines.Text := fIntfBuff.DataString + fImplBuff.DataString + fInitializationBuff.DataString;
2018-09-27 12:26:50 +02:00
2019-11-04 11:14:25 +01:00
finally
lKeyFields.Free;
2018-09-27 12:26:50 +02:00
end;
// mmOutput.Lines.SaveToFile(
// mmConnectionParams.Lines.SaveToFile(FHistoryFileName);
end;
procedure TMainForm.btnGetTablesClick(Sender: TObject);
var
lTables: TStringList;
lTable: string;
lClassName: string;
begin
FDConnection1.Connected := True;
lTables := TStringList.Create;
try
2019-11-04 11:14:25 +01:00
fCatalog := '';
if lstCatalog.ItemIndex > -1 then
begin
2019-11-04 11:14:25 +01:00
fCatalog := lstCatalog.Items[lstCatalog.ItemIndex];
end;
fSchema := '';
if lstSchema.ItemIndex > -1 then
begin
2019-11-04 11:14:25 +01:00
fSchema := lstSchema.Items[lstSchema.ItemIndex];
end;
2019-11-04 11:14:25 +01:00
FDConnection1.GetTableNames(fCatalog, fSchema, '', lTables);
// FDConnection1.GetTableNames('', 'public', '', lTables);
2019-11-04 11:14:25 +01:00
// FDConnection1.GetTableNames('', '', '', lTables);
// if lTables.Count = 0 then
// FDConnection1.GetTableNames('', 'dbo', '', lTables);
2019-11-04 11:14:25 +01:00
dsTablesMapping.EmptyDataSet;
2018-09-27 12:26:50 +02:00
for lTable in lTables do
begin
lClassName := GetClassName(lTable);
2019-11-04 11:14:25 +01:00
dsTablesMapping.AppendRecord([lTable, lClassName]);
2018-09-27 12:26:50 +02:00
end;
2019-11-04 11:14:25 +01:00
dsTablesMapping.First;
2018-09-27 12:26:50 +02:00
finally
lTables.Free;
end;
end;
2019-11-04 11:14:25 +01:00
procedure TMainForm.btnRefreshCatalogClick(Sender: TObject);
begin
FDConnection1.Params.Clear;
FDConnection1.Params.Text := mmConnectionParams.Text;
FDConnection1.Open;
lstCatalog.Items.Clear;
FDConnection1.GetCatalogNames('', lstCatalog.Items);
2020-03-20 23:08:45 +01:00
PageControl1.ActivePageIndex := 0;
2019-11-04 11:14:25 +01:00
end;
2018-09-27 12:26:50 +02:00
procedure TMainForm.btnSaveCodeClick(Sender: TObject);
begin
FileSaveDialog1.FileName := 'EntitiesU.pas';
if FileSaveDialog1.Execute then
begin
mmOutput.Lines.SaveToFile(FileSaveDialog1.FileName);
end;
end;
procedure TMainForm.cboConnectionDefsChange(Sender: TObject);
begin
2019-11-04 11:14:25 +01:00
FDConnection1.Close;
2018-09-27 12:26:50 +02:00
FDManager.GetConnectionDefParams(cboConnectionDefs.Text, mmConnectionParams.Lines);
2019-11-04 11:14:25 +01:00
lstCatalog.Items.Clear;
lstSchema.Items.Clear;
FDConnection1.Params.Clear;
FDConnection1.Params.Text := mmConnectionParams.Text;
2018-09-27 12:26:50 +02:00
end;
procedure TMainForm.EmitClass(const aTableName, aClassName: string);
begin
2019-11-04 11:14:25 +01:00
fIntfBuff.WriteString(INDENT + '[MVCNameCase(ncLowerCase)]' + sLineBreak);
fIntfBuff.WriteString(INDENT + Format('[MVCTable(''%s'')]', [aTableName]) + sLineBreak);
2018-09-27 12:26:50 +02:00
if trim(aClassName) = '' then
raise Exception.Create('Invalid class name');
2019-11-04 11:14:25 +01:00
fIntfBuff.WriteString(INDENT + aClassName + ' = class(TMVCActiveRecord)' + sLineBreak);
if chGenerateMapping.Checked then
fInitializationBuff.WriteString(INDENT + Format('ActiveRecordMappingRegistry.AddEntity(''%s'',%s);',
[aTableName.ToLower, aClassName]) + sLineBreak);
2018-09-27 12:26:50 +02:00
end;
procedure TMainForm.EmitClassEnd;
begin
2019-11-04 11:14:25 +01:00
fIntfBuff.WriteString(INDENT + 'end;' + sLineBreak + sLineBreak);
2018-09-27 12:26:50 +02:00
end;
2019-11-04 11:14:25 +01:00
procedure TMainForm.EmitField(F: TField; const IsPK: Boolean);
var
2020-03-20 23:08:45 +01:00
lAttrib, lField: string;
2018-09-27 12:26:50 +02:00
begin
2019-11-04 11:14:25 +01:00
if IsPK then
begin
lAttrib := Format('[MVCTableField(''%s'', [foPrimaryKey, foAutoGenerated])]', [F.FieldName]);
end
else
begin
2019-11-04 11:14:25 +01:00
lAttrib := Format('[MVCTableField(''%s'')]', [F.FieldName]);
end;
lField := GetFieldName(F.FieldName) + ': ' + GetDelphiType(F.DataType) + ';' + sLineBreak;
if GetDelphiType(F.DataType).ToUpper.Contains('UNSUPPORTED TYPE') then
begin
2019-11-04 11:14:25 +01:00
lAttrib := '//' + lAttrib;
lField := '//' + lField;
end
else
begin
lField := ' ' + lField;
lAttrib := ' ' + lAttrib;
end;
2019-11-04 11:14:25 +01:00
fIntfBuff.WriteString(INDENT + lAttrib + sLineBreak + INDENT + lField);
2018-09-27 12:26:50 +02:00
end;
procedure TMainForm.EmitHeaderComments;
begin
2019-11-04 11:14:25 +01:00
fIntfBuff.WriteString('// *************************************************************************** }' +
sLineBreak);
fIntfBuff.WriteString('//' + sLineBreak);
fIntfBuff.WriteString('// Delphi MVC Framework' + sLineBreak);
fIntfBuff.WriteString('//' + sLineBreak);
fIntfBuff.WriteString('// Copyright (c) 2010-2020 Daniele Teti and the DMVCFramework Team' + sLineBreak);
fIntfBuff.WriteString('//' + sLineBreak);
fIntfBuff.WriteString('// https://github.com/danieleteti/delphimvcframework' + sLineBreak);
fIntfBuff.WriteString('//' + sLineBreak);
2019-11-04 11:14:25 +01:00
fIntfBuff.WriteString('// ***************************************************************************' + sLineBreak);
fIntfBuff.WriteString('//' + sLineBreak);
2019-11-04 11:14:25 +01:00
fIntfBuff.WriteString('// Licensed under the Apache License, Version 2.0 (the "License");' + sLineBreak);
fIntfBuff.WriteString('// you may not use this file except in compliance with the License.' + sLineBreak);
fIntfBuff.WriteString('// You may obtain a copy of the License at' + sLineBreak);
fIntfBuff.WriteString('//' + sLineBreak);
fIntfBuff.WriteString('// http://www.apache.org/licenses/LICENSE-2.0' + sLineBreak);
fIntfBuff.WriteString('//' + sLineBreak);
2019-11-04 11:14:25 +01:00
fIntfBuff.WriteString('// Unless required by applicable law or agreed to in writing, software' + sLineBreak);
fIntfBuff.WriteString('// distributed under the License is distributed on an "AS IS" BASIS,' + sLineBreak);
fIntfBuff.WriteString('// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.' + sLineBreak);
fIntfBuff.WriteString('// See the License for the specific language governing permissions and' + sLineBreak);
fIntfBuff.WriteString('// limitations under the License.' + sLineBreak);
fIntfBuff.WriteString('//' + sLineBreak);
2019-11-04 11:14:25 +01:00
fIntfBuff.WriteString('// ***************************************************************************' + sLineBreak);
fIntfBuff.WriteString(sLineBreak);
end;
2018-09-27 12:26:50 +02:00
procedure TMainForm.EmitProperty(F: TField);
var
2020-03-20 23:08:45 +01:00
lProp: string;
2018-09-27 12:26:50 +02:00
begin
2020-03-20 23:08:45 +01:00
if GetFieldName(F.FieldName).Substring(1).ToLower <> F.FieldName then
begin
lProp := Format('[MVCNameAs(''%s'')]', [F.FieldName]) + sLineBreak + INDENT + INDENT;
end;
lProp := lProp + 'property ' + GetFieldName(F.FieldName).Substring(1) { remove f } + ': ' +
GetDelphiType(F.DataType) + ' read ' + GetFieldName(F.FieldName) + ' write ' + GetFieldName(F.FieldName) + ';' +
sLineBreak;
if GetDelphiType(F.DataType).ToUpper.Contains('UNSUPPORTED TYPE') then
begin
lProp := ' //' + lProp
end
else
begin
lProp := ' ' + lProp;
end;
2019-11-04 11:14:25 +01:00
fIntfBuff.WriteString(INDENT + lProp)
2018-09-27 12:26:50 +02:00
end;
procedure TMainForm.EmitUnit;
begin
2019-11-04 11:14:25 +01:00
fIntfBuff.WriteString('unit EntitiesU;' + sLineBreak);
fIntfBuff.WriteString('' + sLineBreak);
fIntfBuff.WriteString('interface' + sLineBreak);
fIntfBuff.WriteString('' + sLineBreak);
fIntfBuff.WriteString('uses' + sLineBreak);
fIntfBuff.WriteString(' MVCFramework.Serializer.Commons,' + sLineBreak);
fIntfBuff.WriteString(' MVCFramework.ActiveRecord,' + sLineBreak);
fIntfBuff.WriteString(' System.Classes;' + sLineBreak);
fIntfBuff.WriteString('' + sLineBreak);
fIntfBuff.WriteString('type' + sLineBreak);
fIntfBuff.WriteString('' + sLineBreak);
fImplBuff.WriteString('implementation' + sLineBreak + sLineBreak);
fInitializationBuff.WriteString('initialization' + sLineBreak + sLineBreak);
2018-09-27 12:26:50 +02:00
end;
procedure TMainForm.EmitUnitEnd;
begin
fInitializationBuff.WriteString(sLineBreak + 'end.');
2018-09-27 12:26:50 +02:00
end;
procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
fIntfBuff.Free;
fImplBuff.Free;
fInitializationBuff.Free;
2018-09-27 12:26:50 +02:00
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
fIntfBuff := TStringStream.Create;
fImplBuff := TStringStream.Create;
fInitializationBuff := TStringStream.Create;
2019-11-04 11:14:25 +01:00
FHistoryFileName := TPath.Combine(TPath.GetDocumentsPath, TPath.GetFileNameWithoutExtension(ParamStr(0)) +
'.history');
2018-09-27 12:26:50 +02:00
try
if TFile.Exists(FHistoryFileName) then
2019-11-04 11:14:25 +01:00
begin
2018-09-27 12:26:50 +02:00
mmConnectionParams.Lines.LoadFromFile(FHistoryFileName)
2019-11-04 11:14:25 +01:00
end;
2018-09-27 12:26:50 +02:00
except
end;
FDManager.LoadConnectionDefFile;
FDManager.GetConnectionNames(cboConnectionDefs.Items);
end;
function TMainForm.GetClassName(const aTableName: string): string;
var
lTableName: string;
lNextLetter: Integer;
lNextLetterChar: string;
begin
lTableName := aTableName.ToLower.DeQuotedString('"').Replace(' ', '_', [rfReplaceAll]);
2018-09-27 12:26:50 +02:00
Result := 'T' + lTableName.Substring(0, 1).ToUpper + lTableName.Substring(1).ToLower;
while Result.IndexOf('_') > -1 do
begin
lNextLetter := Result.IndexOf('_') + 1;
lNextLetterChar := UpperCase(Result.Chars[lNextLetter]);
Result := Result.Remove(Result.IndexOf('_') + 1, 1);
Result := Result.Insert(Result.IndexOf('_') + 1, lNextLetterChar);
Result := Result.Remove(Result.IndexOf('_'), 1);
end;
end;
function TMainForm.GetDelphiType(FT: TFieldType): string;
begin
case FT of
2020-03-20 23:08:45 +01:00
ftString, ftMemo, ftFmtMemo, ftWideMemo:
2018-09-27 12:26:50 +02:00
Result := 'String';
ftSmallint, ftInteger, ftWord, ftLongWord, ftShortint:
Result := 'Integer';
ftByte:
Result := 'Byte';
ftLargeint:
Result := 'Int64';
ftBoolean:
Result := 'Boolean';
2019-11-04 11:14:25 +01:00
ftFloat, TFieldType.ftSingle, TFieldType.ftExtended:
2018-09-27 12:26:50 +02:00
Result := 'Double';
ftCurrency, ftBCD, ftFMTBcd:
2018-09-27 12:26:50 +02:00
Result := 'Currency';
ftDate:
Result := 'TDate';
ftTime:
Result := 'TTime';
ftDateTime:
Result := 'TDateTime';
ftTimeStamp:
Result := 'TDateTime {timestamp}';
ftAutoInc:
Result := 'Integer {autoincrement}';
2020-03-20 23:08:45 +01:00
ftBlob, { ftMemo, } ftGraphic, { ftFmtMemo, ftWideMemo, } ftStream:
2018-09-27 12:26:50 +02:00
Result := 'TStream';
ftFixedChar:
Result := 'String {fixedchar}';
2018-09-27 12:26:50 +02:00
ftWideString:
Result := 'String';
ftGuid:
Result := 'TGuid';
2018-09-27 12:26:50 +02:00
else
2019-11-04 11:14:25 +01:00
Result := '<UNSUPPORTED TYPE: ' + GetEnumName(TypeInfo(TFieldType), Ord(FT)) + '>';
2018-09-27 12:26:50 +02:00
end;
end;
function TMainForm.GetFieldName(const Value: string): string;
2018-09-27 12:26:50 +02:00
var
Pieces: TArray<string>;
s: string;
begin
if Value.Length <= 2 then
exit('f' + Value.ToUpper);
2018-09-27 12:26:50 +02:00
Result := '';
Pieces := Value.ToLower.Split(['_'], TStringSplitOptions.ExcludeEmpty);
2018-09-27 12:26:50 +02:00
for s in Pieces do
begin
if s = 'id' then
Result := Result + 'ID'
else
Result := Result + UpperCase(s.Chars[0]) + s.Substring(1);
end;
Result := 'f' + Result;
2018-09-27 12:26:50 +02:00
end;
2019-11-04 11:14:25 +01:00
procedure TMainForm.lstCatalogDblClick(Sender: TObject);
begin
lstSchema.Items.Clear;
FDConnection1.GetSchemaNames(lstCatalog.Items[lstCatalog.ItemIndex], '', lstSchema.Items);
end;
procedure TMainForm.lstSchemaDblClick(Sender: TObject);
begin
btnGetTablesClick(Self);
end;
procedure TMainForm.mmConnectionParamsChange(Sender: TObject);
begin
FDConnection1.Close;
lstSchema.Clear;
lstCatalog.Clear;
end;
2018-09-27 12:26:50 +02:00
end.