Improved MVCEntitiesGenerator (WIP)

This commit is contained in:
Daniele Teti 2022-08-19 10:50:45 +02:00
parent eead8deeeb
commit 9212aecb40
4 changed files with 874 additions and 514 deletions

View File

@ -209,8 +209,6 @@ begin
end;
procedure TMVCCache.RemoveItem(const AName: string);
var
lCacheItem: TMVCCacheItem;
begin
FMREW.DoWithWriteLock(
procedure

File diff suppressed because it is too large Load Diff

View File

@ -50,33 +50,18 @@ uses
FireDAC.Stan.ExprFuncs,
FireDAC.Phys.SQLiteDef,
FireDAC.Phys.SQLite, Vcl.DBGrids, FireDAC.Phys.SQLiteWrapper.Stat, Vcl.Buttons,
JsonDataObjects;
JsonDataObjects, System.Actions, Vcl.ActnList, Vcl.Menus, Vcl.StdActns,
Vcl.ExtActns;
type
TSelectionType = (stAll, stNone, stInverse);
TMainForm = class(TForm)
FDConnection1: TFDConnection;
Panel1: TPanel;
Panel2: TPanel;
FDConnection: TFDConnection;
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;
@ -87,21 +72,27 @@ type
dsTablesMapping: TFDMemTable;
dsTablesMappingTABLE_NAME: TStringField;
dsTablesMappingCLASS_NAME: TStringField;
DBGrid1: TDBGrid;
dsrcTablesMapping: TDataSource;
dsTablesMappingGENERATE: TBooleanField;
pcMain: TPageControl;
tsConnectionDefinition: TTabSheet;
tsTablesMapping: TTabSheet;
Panel2: TPanel;
Label2: TLabel;
mmConnectionParams: TMemo;
Panel6: TPanel;
GroupBox1: TGroupBox;
lstSchema: TListBox;
lstCatalog: TListBox;
btnRefreshCatalog: TButton;
Label1: TLabel;
Panel3: TPanel;
Panel4: TPanel;
btnGenEntities: TButton;
chkGenerateMapping: TCheckBox;
dsTablesMappingGENERATE: TBooleanField;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
rgNameCase: TRadioGroup;
rgFieldNameFormatting: TRadioGroup;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
DBGrid1: TDBGrid;
Panel7: TPanel;
btnUZ: TButton;
Button1: TButton;
@ -109,16 +100,53 @@ type
Button3: TButton;
Button4: TButton;
Button5: TButton;
procedure btnGenEntitiesClick(Sender: TObject);
procedure btnGetTablesClick(Sender: TObject);
procedure btnSaveCodeClick(Sender: TObject);
Panel8: TPanel;
btnPrev: TButton;
btnNext: TButton;
ProjectFileOpenDialog: TFileOpenDialog;
MainMenu1: TMainMenu;
ActionList1: TActionList;
actLoadProject: TAction;
actSaveProject: TAction;
actSaveProjectAs: TAction;
File1: TMenuItem;
LoadProject1: TMenuItem;
SaveProject1: TMenuItem;
Saveprojectas1: TMenuItem;
FileExit1: TFileExit;
Exit1: TMenuItem;
N1: TMenuItem;
Panel1: TPanel;
Label1: TLabel;
cboConnectionDefs: TComboBox;
Panel9: TPanel;
btnRefreshCatalog: TButton;
TabNextTab1: TNextTab;
TabPreviousTab1: TPreviousTab;
tsGeneratedCode: TTabSheet;
Panel5: TPanel;
btnSaveCode: TButton;
mmOutput: TMemo;
actSaveGeneratedCode: TAction;
actGenerateCode: TAction;
actRefreshCatalog: TAction;
Panel10: TPanel;
btnGetTables: TButton;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
actRefreshTableList: TAction;
Label3: TLabel;
Panel11: TPanel;
Label4: TLabel;
Label5: TLabel;
FileSaveDialogProject: TFileSaveDialog;
actNewProject: TAction;
NewProject1: TMenuItem;
procedure cboConnectionDefsChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure lstCatalogDblClick(Sender: TObject);
procedure btnRefreshCatalogClick(Sender: TObject);
procedure mmConnectionParamsChange(Sender: TObject);
procedure lstSchemaDblClick(Sender: TObject);
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
procedure DBGrid1CellClick(Column: TColumn);
@ -131,16 +159,30 @@ type
procedure btnSlice1Click(Sender: TObject);
procedure btnUZClick(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure actLoadProjectExecute(Sender: TObject);
procedure actSaveGeneratedCodeExecute(Sender: TObject);
procedure actGenerateCodeExecute(Sender: TObject);
procedure lstCatalogClick(Sender: TObject);
procedure actRefreshCatalogExecute(Sender: TObject);
procedure TabNextTab1AfterTabChange(Sender: TObject);
procedure actRefreshTableListExecute(Sender: TObject);
procedure TabNextTab1Update(Sender: TObject);
procedure actSaveProjectExecute(Sender: TObject);
procedure actSaveProjectAsExecute(Sender: TObject);
procedure actNewProjectExecute(Sender: TObject);
procedure actSaveGeneratedCodeUpdate(Sender: TObject);
private
fConfig: TJSONObject;
fCatalog: string;
fProjectFileName: string;
fSchema: string;
fIntfBuff, fImplBuff, fInitializationBuff: TStringStream;
FHistoryFileName: string;
lTypesName: TArray<string>;
fBookmark: TArray<Byte>;
procedure InitFromLastConfig;
procedure SaveLastConfig;
procedure ResetUI;
procedure LoadProjectFromFile;
procedure SaveProject;
function SelectTables(const FromLetter: AnsiChar; const ToLetter: AnsiChar): Integer;
procedure EmitHeaderComments;
function GetClassName(const aTableName: string): string;
@ -155,8 +197,11 @@ type
function GetDelphiType(FT: TFieldType): string;
function GetFieldName(const Value: string): string;
procedure DoSelection(const SelectionType: TSelectionType);
procedure SetProjectFileName(const Value: String);
function GetProjectFileExists: Boolean;
public
{ Public declarations }
property ProjectFileName: String read fProjectFileName write SetProjectFileName;
property ProjectFileExists: Boolean read GetProjectFileExists;
end;
var
@ -181,7 +226,7 @@ uses
const
INDENT = ' ';
procedure TMainForm.btnGenEntitiesClick(Sender: TObject);
procedure TMainForm.actGenerateCodeExecute(Sender: TObject);
var
I: Integer;
lTableName: string;
@ -191,7 +236,7 @@ var
lKeyFields: TStringList;
lUniqueFieldNames: TArray<String>;
begin
SaveLastConfig;
// SaveProject;
Log.Info('Starting entities generation', LOG_TAG);
fIntfBuff.Clear;
fImplBuff.Clear;
@ -216,10 +261,10 @@ begin
EmitClass(lTableName, lClassName, rgNameCase.Items[rgNameCase.ItemIndex]);
lKeyFields.Clear;
qry.Close;
qry.SQL.Text := 'select * from ' + lTableName + ' where 1=1 limit 1';
qry.SQL.Text := 'select * from ' + lTableName + ' where 1=0';
qry.Open;
try
FDConnection1.GetKeyFieldNames(fCatalog, fSchema, lTableName, '', lKeyFields);
FDConnection.GetKeyFieldNames(fCatalog, fSchema, lTableName, '', lKeyFields);
except
end;
lFieldNamesToInitialize := [];
@ -285,15 +330,49 @@ begin
// mmOutput.Lines.SaveToFile(
// mmConnectionParams.Lines.SaveToFile(FHistoryFileName);
ShowMessage('Generation Completed');
TabNextTab1.Execute;
end;
procedure TMainForm.btnGetTablesClick(Sender: TObject);
procedure TMainForm.actLoadProjectExecute(Sender: TObject);
begin
ProjectFileOpenDialog.DefaultExtension := 'entgen';
if ProjectFileOpenDialog.Execute then
begin
ProjectFileName := ProjectFileOpenDialog.FileName;
LoadProjectFromFile;
end;
end;
procedure TMainForm.actNewProjectExecute(Sender: TObject);
begin
ProjectFileName := DEFAULT_PROJECT_NAME;
LoadProjectFromFile;
end;
procedure TMainForm.actRefreshCatalogExecute(Sender: TObject);
begin
FDConnection.Params.Clear;
FDConnection.Params.Text := mmConnectionParams.Text;
try
FDConnection.Open;
lstCatalog.Items.Clear;
FDConnection.GetCatalogNames('', lstCatalog.Items);
except
on E: Exception do
begin
Application.ShowException(E);
end;
end;
end;
procedure TMainForm.actRefreshTableListExecute(Sender: TObject);
var
lTables: TStringList;
lTable: string;
lClassName: string;
begin
FDConnection1.Connected := True;
FDConnection.Connected := True;
lTables := TStringList.Create;
try
fCatalog := '';
@ -306,7 +385,7 @@ begin
begin
fSchema := lstSchema.Items[lstSchema.ItemIndex];
end;
FDConnection1.GetTableNames(fCatalog, fSchema, '', lTables);
FDConnection.GetTableNames(fCatalog, fSchema, '', lTables);
// FDConnection1.GetTableNames('', 'public', '', lTables);
@ -327,17 +406,7 @@ begin
TabSheet1.Caption := 'Tables (' + dsTablesMapping.RecordCount.ToString + ')';
end;
procedure TMainForm.btnRefreshCatalogClick(Sender: TObject);
begin
FDConnection1.Params.Clear;
FDConnection1.Params.Text := mmConnectionParams.Text;
FDConnection1.Open;
lstCatalog.Items.Clear;
FDConnection1.GetCatalogNames('', lstCatalog.Items);
PageControl1.ActivePageIndex := 0;
end;
procedure TMainForm.btnSaveCodeClick(Sender: TObject);
procedure TMainForm.actSaveGeneratedCodeExecute(Sender: TObject);
begin
FileSaveDialog1.FileName := 'EntitiesU.pas';
if FileSaveDialog1.Execute then
@ -346,6 +415,28 @@ begin
end;
end;
procedure TMainForm.actSaveGeneratedCodeUpdate(Sender: TObject);
begin
actSaveGeneratedCode.Enabled := mmOutput.Lines.Count > 0;
end;
procedure TMainForm.actSaveProjectAsExecute(Sender: TObject);
begin
if FileSaveDialogProject.Execute then
begin
ProjectFileName := FileSaveDialogProject.FileName;
SaveProject;
end;
end;
procedure TMainForm.actSaveProjectExecute(Sender: TObject);
begin
if not ProjectFileExists then
actSaveProjectAs.Execute
else
SaveProject;
end;
procedure TMainForm.btnSlice1Click(Sender: TObject);
begin
ShowMessage('Select ' + SelectTables('R', 'T').ToString + ' new tables');
@ -378,12 +469,13 @@ end;
procedure TMainForm.cboConnectionDefsChange(Sender: TObject);
begin
FDConnection1.Close;
FDConnection.Close;
FDManager.GetConnectionDefParams(cboConnectionDefs.Text, mmConnectionParams.Lines);
lstCatalog.Items.Clear;
lstSchema.Items.Clear;
FDConnection1.Params.Clear;
FDConnection1.Params.Text := mmConnectionParams.Text;
FDConnection.Params.Clear;
FDConnection.Params.Text := mmConnectionParams.Text;
actRefreshCatalog.Execute;
end;
procedure TMainForm.DBGrid1CellClick(Column: TColumn);
@ -570,7 +662,6 @@ end;
procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
SaveLastConfig;
fIntfBuff.Free;
fImplBuff.Free;
fInitializationBuff.Free;
@ -578,11 +669,8 @@ end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
pcMain.ActivePageIndex := 0;
fConfig := TJSONObject.Create;
if TFile.Exists(CONFIG_FILE) then
begin
fConfig.LoadFromFile(CONFIG_FILE);
end;
fIntfBuff := TStringStream.Create;
fImplBuff := TStringStream.Create;
fInitializationBuff := TStringStream.Create;
@ -600,7 +688,9 @@ begin
FDManager.LoadConnectionDefFile;
FDManager.GetConnectionNames(cboConnectionDefs.Items);
InitFromLastConfig;
ProjectFileName := DEFAULT_PROJECT_NAME;
// LoadProjectFromFile;
end;
function TMainForm.GetClassName(const aTableName: string): string;
@ -673,17 +763,12 @@ begin
begin
Exit('f' + Value.ToUpper);
end;
Result := 'f' + Value;
end;
// Result := '';
// Pieces := Value.ToLower.Split(['_'], TStringSplitOptions.ExcludeEmpty);
// for Piece in Pieces do
// begin
// if Piece = 'id' then
// Result := Result + 'ID'
// else
// Result := Result + UpperCase(Piece.Chars[0]) + Piece.Substring(1);
// end;
Result := 'f' + Value; // CamelCase(Value, True);
function TMainForm.GetProjectFileExists: Boolean;
begin
Result := TFile.Exists(ProjectFileName);
end;
function TMainForm.GetUniqueFieldNames(const Fields: TFields; const FormatAsPascalCase: Boolean): TArray<String>;
@ -734,25 +819,33 @@ begin
end;
end;
procedure TMainForm.InitFromLastConfig;
procedure TMainForm.LoadProjectFromFile;
var
I, J: Integer;
lJObj: TJSONObject;
begin
ResetUI;
if not TFile.Exists(fProjectFileName) then
begin
Exit;
end;
fConfig.LoadFromFile(fProjectFileName);
if cboConnectionDefs.Items.IndexOf(fConfig.S[cboConnectionDefs.Name]) = -1 then
Exit;
cboConnectionDefs.ItemIndex := cboConnectionDefs.Items.IndexOf(fConfig.S[cboConnectionDefs.Name]);
cboConnectionDefsChange(self);
btnRefreshCatalogClick(self);
actRefreshCatalog.Execute;
lstCatalog.Update;
if fConfig.IndexOf(lstCatalog.Name) > -1 then
begin
lstCatalog.ItemIndex := lstCatalog.Items.IndexOf(fConfig.S[lstCatalog.Name]);
lstCatalogDblClick(self);
lstCatalogClick(self);
lstSchema.ItemIndex := lstSchema.Items.IndexOf(fConfig.S[lstSchema.Name]);
end;
btnGetTablesClick(self);
actRefreshTableList.Execute;
rgNameCase.ItemIndex := fConfig.I[rgNameCase.Name];
rgFieldNameFormatting.ItemIndex := fConfig.I[rgFieldNameFormatting.Name];
@ -784,25 +877,34 @@ begin
dsTablesMapping.First;
end;
procedure TMainForm.lstCatalogDblClick(Sender: TObject);
procedure TMainForm.lstCatalogClick(Sender: TObject);
begin
lstSchema.Items.Clear;
FDConnection1.GetSchemaNames(lstCatalog.Items[lstCatalog.ItemIndex], '', lstSchema.Items);
end;
FDConnection.GetSchemaNames(lstCatalog.Items[lstCatalog.ItemIndex], '', lstSchema.Items);
procedure TMainForm.lstSchemaDblClick(Sender: TObject);
begin
btnGetTablesClick(self);
end;
procedure TMainForm.mmConnectionParamsChange(Sender: TObject);
begin
FDConnection1.Close;
FDConnection.Close;
lstSchema.Clear;
lstCatalog.Clear;
end;
procedure TMainForm.SaveLastConfig;
procedure TMainForm.ResetUI;
begin
cboConnectionDefs.ItemIndex := -1;
mmConnectionParams.Clear;
lstCatalog.Clear;
lstSchema.Clear;
rgNameCase.ItemIndex := 0;
rgFieldNameFormatting.ItemIndex := 0;
chkGenerateMapping.Checked := False;
dsTablesMapping.EmptyDataSet;
mmOutput.Clear;
end;
procedure TMainForm.SaveProject;
var
lJObj: TJSONObject;
lField: TField;
@ -834,7 +936,7 @@ begin
dsTablesMapping.Next;
end;
dsTablesMapping.First;
fConfig.SaveToFile(CONFIG_FILE, False);
fConfig.SaveToFile(fProjectFileName, False);
end;
function TMainForm.SelectTables(const FromLetter, ToLetter: AnsiChar): Integer;
@ -868,6 +970,12 @@ begin
Result := lSelectedTables;
end;
procedure TMainForm.SetProjectFileName(const Value: String);
begin
fProjectFileName := TPath.ChangeExtension(Value, '.entgen');
Caption := 'DMVCFramework Entities Generator :: ' + fProjectFileName;
end;
procedure TMainForm.SpeedButton1Click(Sender: TObject);
begin
DoSelection(stAll);
@ -883,4 +991,29 @@ begin
DoSelection(stInverse);
end;
procedure TMainForm.TabNextTab1AfterTabChange(Sender: TObject);
begin
if pcMain.ActivePage = tsTablesMapping then
begin
actRefreshTableList.Execute;
end;
if pcMain.ActivePage = tsGeneratedCode then
begin
actGenerateCode.Execute;
end;
end;
procedure TMainForm.TabNextTab1Update(Sender: TObject);
begin
if pcMain.ActivePage = tsConnectionDefinition then
begin
TabNextTab1.Enabled := (cboConnectionDefs.ItemIndex > -1) and (
(lstCatalog.Items.Count = 0)
or
((lstCatalog.ItemIndex > -1) and (lstSchema.ItemIndex > -1))
);
end;
end;
end.

View File

@ -2,6 +2,9 @@ unit UtilsU;
interface
const
DEFAULT_PROJECT_NAME = 'NewEntitiesGeneratorProject';
function IsReservedKeyword(const Value: String): Boolean;
@ -19,6 +22,8 @@ const PASCAL_KEYWORDS = ';and;array;as;as;asm;begin;break;case;class;class;const
'property;raise;record;reference;repeat;self;set;shl;shr;string;then;threadvar;to;' +
'true;try;type;unit;until;uses;var;while;with;xor;';
function IsReservedKeyword(const Value: String): Boolean;
begin
Result := PASCAL_KEYWORDS.Contains(';' + Value.ToLower + ';');