* Allow to create pascal constants when creating SQL statements, allow copying of constants to clipboard

This commit is contained in:
Michaël Van Canneyt 2024-07-22 22:07:48 +02:00
parent e98a5d2b44
commit c4e3b93d4c
2 changed files with 387 additions and 75 deletions

View File

@ -1,19 +1,18 @@
object GenerateSQLForm: TGenerateSQLForm
Left = 364
Height = 423
Height = 529
Top = 295
Width = 812
ActiveControl = PCSQL
Width = 914
Caption = 'Generate SQL statements'
ClientHeight = 423
ClientWidth = 812
OnCreate = FormCreate
ClientHeight = 529
ClientWidth = 914
LCLVersion = '3.99.0.0'
OnCreate = FormCreate
object PCSQL: TPageControl
Left = 6
Height = 371
Height = 473
Top = 6
Width = 800
Width = 902
ActivePage = TSFields
Align = alClient
BorderSpacing.Around = 6
@ -26,17 +25,17 @@ object GenerateSQLForm: TGenerateSQLForm
ChildSizing.ShrinkHorizontal = crsScaleChilds
ChildSizing.ShrinkVertical = crsScaleChilds
ChildSizing.ControlsPerLine = 3
ClientHeight = 343
ClientWidth = 792
ClientHeight = 443
ClientWidth = 892
OnResize = TSResize
object POptions: TPanel
Left = 0
Height = 343
Height = 443
Top = 0
Width = 254
Align = alLeft
BevelOuter = bvNone
ClientHeight = 343
ClientHeight = 443
ClientWidth = 254
Constraints.MinWidth = 180
TabOrder = 0
@ -77,30 +76,30 @@ object GenerateSQLForm: TGenerateSQLForm
end
object CBTables: TComboBox
Left = 4
Height = 23
Height = 30
Top = 26
Width = 238
Anchors = [akTop, akLeft, akRight]
ItemHeight = 15
OnChange = CBTablesChange
ItemHeight = 0
Sorted = True
Style = csDropDownList
TabOrder = 0
OnChange = CBTablesChange
end
object BGenerate: TButton
Left = 4
Height = 25
Top = 314
Top = 414
Width = 238
Anchors = [akLeft, akRight, akBottom]
BorderSpacing.InnerBorder = 4
Caption = '&Generate SQL'
OnClick = BGenerateClick
TabOrder = 1
OnClick = BGenerateClick
end
object SEindent: TTISpinEdit
Left = 184
Height = 23
Height = 28
Top = 216
Width = 56
Anchors = [akTop, akRight]
@ -109,7 +108,7 @@ object GenerateSQLForm: TGenerateSQLForm
end
object SELineLength: TTISpinEdit
Left = 184
Height = 23
Height = 28
Top = 245
Width = 56
Anchors = [akTop, akRight]
@ -122,7 +121,7 @@ object GenerateSQLForm: TGenerateSQLForm
Top = 61
Width = 234
Anchors = [akTop, akLeft, akRight]
Caption = 'Options'
Caption = 'SQL Options'
Link.TIPropertyName = 'Options'
Link.AliasValuesStrings = (
'eoLineFeedAfterField=Linefeed after each field'
@ -135,31 +134,70 @@ object GenerateSQLForm: TGenerateSQLForm
end
object CBIgnoreSelection: TCheckBox
Left = 4
Height = 19
Height = 23
Top = 272
Width = 171
Width = 202
Caption = 'Create full table creation SQL'
TabOrder = 5
end
object GBCode: TGroupBox
AnchorSideLeft.Control = CLBOptions
AnchorSideRight.Control = CLBOptions
AnchorSideRight.Side = asrBottom
Left = 6
Height = 105
Top = 301
Width = 234
Anchors = [akTop, akLeft, akRight]
Caption = 'Code options'
ClientHeight = 88
ClientWidth = 232
TabOrder = 6
object CBQuoteSQL: TCheckBox
Left = 16
Height = 23
Top = 8
Width = 91
Caption = 'Quote SQL'
TabOrder = 0
end
object CBPreserveLineFeeds: TCheckBox
Left = 16
Height = 23
Top = 56
Width = 136
Caption = 'Preserve linefeeds'
TabOrder = 1
end
object CBAddConst: TCheckBox
AnchorSideLeft.Control = CBQuoteSQL
Left = 16
Height = 23
Top = 32
Width = 152
Caption = 'Constant declaration'
TabOrder = 2
end
end
end
object PKeyFields: TPanel
AnchorSideLeft.Control = POptions
AnchorSideLeft.Side = asrBottom
AnchorSideRight.Control = PSelectFields
Left = 254
Height = 343
Height = 443
Top = 0
Width = 250
Width = 294
Align = alClient
BevelOuter = bvNone
ClientHeight = 343
ClientWidth = 250
ClientHeight = 443
ClientWidth = 294
TabOrder = 1
object LLBKeyFields: TLabel
Left = 0
Height = 26
Top = 0
Width = 250
Width = 294
Align = alTop
Alignment = taCenter
AutoSize = False
@ -169,32 +207,42 @@ object GenerateSQLForm: TGenerateSQLForm
end
object LBKeyFields: TListBox
Left = 2
Height = 298
Height = 374
Top = 34
Width = 244
Width = 288
Anchors = [akTop, akLeft, akRight, akBottom]
ItemHeight = 0
MultiSelect = True
ScrollWidth = 252
Sorted = True
TabOrder = 0
TopIndex = -1
end
object BCopyAllToClipboard: TButton
Left = 0
Height = 25
Top = 414
Width = 195
Caption = 'Copy all SQLs to clipboard'
TabOrder = 1
OnClick = BCopyAllToClipboardClick
end
end
object PSelectFields: TPanel
Left = 504
Height = 343
Left = 548
Height = 443
Top = 0
Width = 288
Width = 344
Align = alRight
BevelOuter = bvNone
ClientHeight = 343
ClientWidth = 288
ClientHeight = 443
ClientWidth = 344
TabOrder = 2
object Label2: TLabel
Left = 0
Height = 26
Top = 0
Width = 288
Width = 344
Align = alTop
Alignment = taCenter
AutoSize = False
@ -204,27 +252,28 @@ object GenerateSQLForm: TGenerateSQLForm
end
object LBFields: TListBox
Left = 12
Height = 298
Height = 374
Top = 34
Width = 266
Width = 322
Anchors = [akTop, akLeft, akRight, akBottom]
ItemHeight = 0
MultiSelect = True
ScrollWidth = 264
Sorted = True
TabOrder = 0
TopIndex = -1
end
end
end
object TSSelect: TTabSheet
Caption = '&Select'
ClientHeight = 343
ClientWidth = 792
ClientHeight = 443
ClientWidth = 892
object MSelect: TMemo
Left = 8
Height = 327
Height = 385
Top = 8
Width = 776
Width = 876
Align = alClient
BorderSpacing.Around = 8
Lines.Strings = (
@ -232,16 +281,45 @@ object GenerateSQLForm: TGenerateSQLForm
)
TabOrder = 0
end
object pSQLSelect: TPanel
Left = 0
Height = 42
Top = 401
Width = 892
Align = alBottom
BevelOuter = bvNone
ClientHeight = 42
ClientWidth = 892
TabOrder = 1
object BCopyAllToClipboard1: TButton
Left = 8
Height = 25
Top = 8
Width = 195
Caption = 'Copy all SQLs to clipboard'
TabOrder = 0
OnClick = BCopyAllToClipboardClick
end
object BCopySelectToClipboard: TButton
Left = 224
Height = 25
Top = 8
Width = 195
Caption = 'Copy Select SQL to clipboard'
TabOrder = 1
OnClick = BCopySelectToClipboardClick
end
end
end
object TSInsert: TTabSheet
Caption = '&Insert'
ClientHeight = 343
ClientWidth = 792
ClientHeight = 443
ClientWidth = 892
object MInsert: TMemo
Left = 8
Height = 327
Height = 385
Top = 8
Width = 776
Width = 876
Align = alClient
BorderSpacing.Around = 8
Lines.Strings = (
@ -249,16 +327,36 @@ object GenerateSQLForm: TGenerateSQLForm
)
TabOrder = 0
end
object pSQLSelect1: TPanel
Left = 0
Height = 42
Top = 401
Width = 892
Align = alBottom
BevelOuter = bvNone
ClientHeight = 42
ClientWidth = 892
TabOrder = 1
object BCopyInsertSQLToClipboard: TButton
Left = 10
Height = 25
Top = 8
Width = 195
Caption = 'Copy Insert SQL to clipboard'
TabOrder = 0
OnClick = BCopyInsertSQLToClipboardClick
end
end
end
object TSUpdate: TTabSheet
Caption = '&Update'
ClientHeight = 343
ClientWidth = 792
ClientHeight = 443
ClientWidth = 892
object MUpdate: TMemo
Left = 8
Height = 327
Height = 385
Top = 8
Width = 776
Width = 876
Align = alClient
BorderSpacing.Around = 8
Lines.Strings = (
@ -266,16 +364,36 @@ object GenerateSQLForm: TGenerateSQLForm
)
TabOrder = 0
end
object pSQLSelect2: TPanel
Left = 0
Height = 42
Top = 401
Width = 892
Align = alBottom
BevelOuter = bvNone
ClientHeight = 42
ClientWidth = 892
TabOrder = 1
object BCopyUpdateSQLToClipboard: TButton
Left = 10
Height = 25
Top = 8
Width = 195
Caption = 'Copy Update SQL to clipboard'
TabOrder = 0
OnClick = BCopyUpdateSQLToClipboardClick
end
end
end
object TSDelete: TTabSheet
Caption = '&Delete'
ClientHeight = 343
ClientWidth = 792
ClientHeight = 443
ClientWidth = 892
object MDelete: TMemo
Left = 8
Height = 327
Height = 385
Top = 8
Width = 776
Width = 876
Align = alClient
BorderSpacing.Around = 8
Lines.Strings = (
@ -283,16 +401,36 @@ object GenerateSQLForm: TGenerateSQLForm
)
TabOrder = 0
end
object pSQLSelect3: TPanel
Left = 0
Height = 42
Top = 401
Width = 892
Align = alBottom
BevelOuter = bvNone
ClientHeight = 42
ClientWidth = 892
TabOrder = 1
object BCopyDeleteSQLToClipboard1: TButton
Left = 10
Height = 25
Top = 8
Width = 195
Caption = 'Copy Delete SQL to clipboard'
TabOrder = 0
OnClick = BCopyDeleteSQLToClipboard1Click
end
end
end
object TSCreate: TTabSheet
Caption = 'Create table'
ClientHeight = 343
ClientWidth = 792
ClientHeight = 443
ClientWidth = 892
object MCreate: TMemo
Left = 8
Height = 327
Height = 385
Top = 8
Width = 776
Width = 876
Align = alClient
BorderSpacing.Around = 8
Lines.Strings = (
@ -300,13 +438,33 @@ object GenerateSQLForm: TGenerateSQLForm
)
TabOrder = 0
end
object pSQLSelect4: TPanel
Left = 0
Height = 42
Top = 401
Width = 892
Align = alBottom
BevelOuter = bvNone
ClientHeight = 42
ClientWidth = 892
TabOrder = 1
object BCopyCreateSQLToClipboard: TButton
Left = 10
Height = 25
Top = 8
Width = 195
Caption = 'Copy Create SQL to clipboard'
TabOrder = 0
OnClick = BCopyCreateSQLToClipboardClick
end
end
end
end
object ButtonPanel: TButtonPanel
Left = 6
Height = 34
Top = 383
Width = 800
Height = 38
Top = 485
Width = 902
OKButton.Name = 'OKButton'
OKButton.DefaultCaption = True
HelpButton.Name = 'HelpButton'

View File

@ -34,10 +34,21 @@ type
{ TGenerateSQLForm }
TGenerateSQLForm = class(TForm)
BCopyAllToClipboard1: TButton;
BCopyCreateSQLToClipboard: TButton;
BCopySelectToClipboard: TButton;
BCopyInsertSQLToClipboard: TButton;
BCopyUpdateSQLToClipboard: TButton;
BCopyDeleteSQLToClipboard1: TButton;
BGenerate: TButton;
BCopyAllToClipboard: TButton;
ButtonPanel: TButtonPanel;
CBTables: TComboBox;
CBIgnoreSelection: TCheckBox;
CBQuoteSQL: TCheckBox;
CBPreserveLineFeeds: TCheckBox;
CBAddConst: TCheckBox;
GBCode: TGroupBox;
LBKeyFields: TListBox;
LCBTables: TLabel;
Label2: TLabel;
@ -50,10 +61,15 @@ type
MUpdate: TMemo;
MInsert: TMemo;
MSelect: TMemo;
pSQLSelect: TPanel;
PKeyFields: TPanel;
POptions: TPanel;
PSelectFields: TPanel;
PCSQL: TPageControl;
pSQLSelect1: TPanel;
pSQLSelect2: TPanel;
pSQLSelect3: TPanel;
pSQLSelect4: TPanel;
SELineLength: TTISpinEdit;
SEIndent: TTISpinEdit;
CLBOptions: TTICheckGroup;
@ -63,6 +79,12 @@ type
TSInsert: TTabSheet;
TSUpdate: TTabSheet;
TSDelete: TTabSheet;
procedure BCopyCreateSQLToClipboardClick(Sender: TObject);
procedure BCopyDeleteSQLToClipboard1Click(Sender: TObject);
procedure BCopyInsertSQLToClipboardClick(Sender: TObject);
procedure BCopySelectToClipboardClick(Sender: TObject);
procedure BCopyAllToClipboardClick(Sender: TObject);
procedure BCopyUpdateSQLToClipboardClick(Sender: TObject);
procedure BGenerateClick(Sender: TObject);
procedure CBTablesChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
@ -71,10 +93,12 @@ type
FTableDefs : TDDTableDefs;
FGenerator : TFPDDSQLEngine;
FSQLGenerated : Boolean;
procedure CopySQLToClipBoard(aSelectedMemo: TMemo);
function GetAS: Boolean;
function GetSQLStatement(Index: integer): TStrings;
function GetTableDef: TDDTableDef;
function GetTableName: String;
procedure PostProcess(SQL: TStrings; aConstName: string; Dest: TStrings);
procedure SetAS(AValue: Boolean);
procedure SetTableDefs(const AValue: TDDTableDefs);
procedure SetTableName(const AValue: String);
@ -103,6 +127,8 @@ var
implementation
uses Clipbrd;
Class Function TGenerateSQLFOrm.GenerateSQLDialog(TDS : TDDTableDefs; TN : String; AllowChangeTable : Boolean = True) : Boolean;
begin
@ -142,7 +168,7 @@ end;
function TGenerateSQLForm.GetTableName: String;
begin
Result:=CBTables.Text;
Result:=Trim(CBTables.Text);
end;
procedure TGenerateSQLForm.SetAS(AValue: Boolean);
@ -181,13 +207,13 @@ Var
TN : String;
I : Integer;
begin
TN:=CBTables.Text;
TN:=GetTableName;
With CBTables.Items do
begin
Clear;
If Assigned(FTableDefs) then
For I:=0 to FTableDefs.Count-1 do
AddObject(FTableDefs[i].TableName,FTableDefs[i]);
AddObject(Trim(FTableDefs[i].TableName),FTableDefs[i]);
end;
With CBTables do
If (TN<>'') then
@ -203,10 +229,63 @@ begin
MDelete.Clear;
MCreate.Clear;
FSQLGenerated:=False;
BCopyAllToClipboard.Enabled:=False;
ButtonPanel.OKButton.Default:=False;
BGenerate.Default:=True;
end;
procedure TGenerateSQLForm.PostProcess(SQL: TStrings; aConstName : string; Dest : TStrings);
Var
lIndent : String;
i : Integer;
lLine,lConst : String;
lAddConst,lPreserveLF : Boolean;
begin
lIndent:=' ';
Dest.BeginUpdate;
try
Dest.Clear;
lAddConst:=CBAddConst.Checked;
lPreserveLF:=CBPreserveLineFeeds.Checked;
if not CBQuoteSQL.Checked then
Dest.Assign(SQL)
else
begin
if lAddConst then
begin
lConst:='SQL'+aConstName+GetTableName;
Dest.Add(' '+lConst+' = ');
lIndent:=lIndent+' ';
end;
For I:=0 to SQL.Count-1 do
begin
lLine:=SQL[I];
lLine:=''''+StringReplace(lLine,'''','''''',[rfReplaceAll]);
if I=SQL.Count-1 then
begin
lLine:=lLine+'''';
if lAddConst then
lLine:=lLine+';';
end
else
begin
if lPreserveLF then
lLine:=lLine+''' + sLineBreak+'
else
lLine:=lLine+' '' + ';
end;
Dest.Add(lIndent+lLine);
end;
end;
SQL.Clear;
finally
Dest.EndUpdate;
end;
end;
procedure TGenerateSQLForm.GenerateSQL;
Function CreateFieldListFromLB(LB : TListBox) : TFPDDFieldList;
@ -225,11 +304,11 @@ procedure TGenerateSQLForm.GenerateSQL;
Result.Free;
Raise;
end;
end;
Var
KL,FL : TFPDDFieldList;
lSQL : TStringList;
begin
ClearSQL;
@ -237,30 +316,38 @@ begin
Raise Exception.Create(SErrSelectTable);
If (LBFields.SelCount=0) then
Raise Exception.Create(SErrSelectFields);
lSQL:=Nil;
FL:=Nil;
KL:=CreateFieldListFromLB(LBKeyFields);
try
lSQL:=TStringList.Create;
FL:=CreateFieldListFromLB(LBFields);
try
With FGenerator do
begin
TableDef:=Self.TableDef;
CreateSelectSQLStrings(FL,KL,MSelect.Lines);
CreateInsertSQLStrings(FL,MInsert.Lines);
CreateUpdateSQLStrings(FL,KL,MUpdate.Lines);
CreateDeleteSQLStrings(KL,MDelete.Lines);
TableDef.TableName:=Trim(TableDef.TableName);
CreateSelectSQLStrings(FL,KL,lSQL);
PostProcess(lSQL,'Select',MSelect.Lines);
CreateInsertSQLStrings(FL,lSQL);
PostProcess(lSQL,'Insert',MInsert.Lines);
CreateUpdateSQLStrings(FL,KL,lSQL);
PostProcess(lSQL,'Update',MUpdate.Lines);
CreateDeleteSQLStrings(KL,lSQL);
PostProcess(lSQL,'Delete',MDelete.Lines);
If CBIgnoreSelection.Checked then
CreateTableSQLStrings(MCreate.Lines)
CreateTableSQLStrings(lSQL)
else
CreateCreateSQLStrings(FL,KL,MCreate.Lines);
CreateCreateSQLStrings(FL,KL,lSQL);
PostProcess(lSQL,'Create',MCreate.Lines);
end;
FSQLGenerated:=True;
BCopyAllToClipboard.Enabled:=True;
BGenerate.Default:=False;
ButtonPanel.OKButton.Default:=True;
PCSQL.ActivePage:=TSSelect;
finally
FL.Free;
end;
finally
lSQL.Free;
FL.Free;
KL.Free;
end;
end;
@ -270,7 +357,7 @@ procedure TGenerateSQLForm.SetTableName(const AValue: String);
begin
With CBTables do
begin
ItemIndex:=Items.IndexOf(AValue);
ItemIndex:=Items.IndexOf(Trim(AValue));
CBTablesChange(CBTables);
end;
end;
@ -348,6 +435,73 @@ begin
GenerateSQL;
end;
procedure TGenerateSQLForm.BCopyAllToClipboardClick(Sender: TObject);
begin
CopySQLToClipBoard(Nil);
end;
procedure TGenerateSQLForm.BCopyUpdateSQLToClipboardClick(Sender: TObject);
begin
CopySQLToClipBoard(MUpdate);
end;
procedure TGenerateSQLForm.BCopySelectToClipboardClick(Sender: TObject);
begin
CopySQLToClipBoard(MSelect);
end;
procedure TGenerateSQLForm.BCopyInsertSQLToClipboardClick(Sender: TObject);
begin
CopySQLToClipBoard(MInsert);
end;
procedure TGenerateSQLForm.BCopyDeleteSQLToClipboard1Click(Sender: TObject);
begin
CopySQLToClipBoard(MDelete);
end;
procedure TGenerateSQLForm.BCopyCreateSQLToClipboardClick(Sender: TObject);
begin
CopySQLToClipBoard(MCreate);
end;
Procedure TGenerateSQLForm.CopySQLToClipBoard(aSelectedMemo: TMemo);
Var
L: TStrings;
procedure AddLines(M : TMemo; aComment : string);
begin
if (aSelectedMemo<>Nil) and (M<>aSelectedMemo) then
Exit;
if not CBQuoteSQL.Checked then
L.Add('-- '+GetTableName+': '+aComment)
else if not CBAddConst.Checked then
L.Add('// '+GetTableName+': '+aComment);
L.Add('');
L.AddStrings(M.Lines);
end;
begin
L:=TStringList.Create;
try
AddLines(MSelect,'Select');
L.Add('');
AddLines(MInsert,'Insert');
L.Add('');
AddLines(MUpdate,'Update');
L.Add('');
AddLines(MDelete,'Delete');
L.Add('');
AddLines(MCreate,'Create');
Clipboard.AsText:=L.Text;
finally
L.Free;
end;
end;
end.