-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUFRSelectQueryBuilder.pas
More file actions
744 lines (647 loc) · 21.8 KB
/
Copy pathUFRSelectQueryBuilder.pas
File metadata and controls
744 lines (647 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
unit UFRSelectQueryBuilder;
interface
uses
System.SysUtils, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms,
Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.ExtCtrls, System.UITypes,
Vcl.CheckLst, Data.DB, System.Generics.Collections, System.StrUtils, IniFiles,
Winapi.Windows, Winapi.Messages, ShellApi, Vcl.Menus, System.RegularExpressions,
FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Param,
FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf,
FireDAC.Stan.Async, FireDAC.DApt, FireDAC.Comp.DataSet,
FireDAC.Comp.Client,
dxSkinMoneyTwins, dxSkinOffice2007Blue, dxSkinOffice2010Silver, dxSkinVS2010,
dxSkinDevExpressDarkStyle, dxSkinOffice2019Black, SkinAzul,
dxSkinVisualStudio2013Dark, dxSkinVisualStudio2013Light,
cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxContainer,
cxEdit, dxSkinsCore, dxSkinBlue, cxLabel, cxMemo, cxRichEdit, cxCustomListBox,
cxListBox, cxButtons, cxTextEdit, cxMaskEdit, cxDropDownEdit,
dxBarBuiltInMenu, cxCheckBox, cxCheckListBox, cxPC, dxBevel, dxCore,
cxClasses, dxSkinsForm, cxGeometry, dxFramedControl, dxPanel;
type
TFRSelectQueryBuilder = class(TForm)
Label1: TcxLabel;
Label2: TcxLabel;
Label3: TcxLabel;
CbTipoJoin: TcxComboBox;
CbTablaSecundaria: TcxComboBox;
CbCampoTablaSecundaria: TcxComboBox;
BtnAgregarJoin: TcxButton;
MmJoin: TcxRichEdit;
cxPageControl1: TcxPageControl;
cxTabSheet1: TcxTabSheet;
cxTabSheet2: TcxTabSheet;
ListCamposSeleccionados: TcxListBox;
BtnFormatearConAlias: TcxButton;
CheckListBox1: TcxCheckListBox;
CheckListBox2: TcxCheckListBox;
BalloonHint1: TBalloonHint;
cxLabel1: TcxLabel;
dxBevel1: TdxPanel;
BtRemoveItem: TcxButton;
BtnConvertirDelphi: TcxButton;
LsbTablas: TcxListBox;
cxRichEdit1: TcxRichEdit;
PnTablaPrincipal: TPanel;
PnTablas: TPanel;
PnTablaSecundaria: TPanel;
dxPanel1: TdxPanel;
BtnAgregarCampos: TcxButton;
dxPanel2: TdxPanel;
BtnConstruir: TcxButton;
BtnSalir: TcxButton;
procedure FormCreate(Sender: TObject);
procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
procedure BtnAgregarJoinClick(Sender: TObject);
procedure BtnConstruirClick(Sender: TObject);
procedure BtnAgregarCamposClick(Sender: TObject);
procedure BtnSalirClick(Sender: TObject);
procedure BtnFormatearConAliasClick(Sender: TObject);
procedure CbTablaSecundariaPropertiesChange(Sender: TObject);
procedure BtnAgregarJoinMouseEnter(Sender: TObject);
procedure BtnAgregarJoinMouseLeave(Sender: TObject);
procedure TreeView1CustomDrawItem(Sender: TCustomTreeView; Node: TTreeNode;
State: TCustomDrawState; var DefaultDraw: Boolean);
procedure BtRemoveItemClick(Sender: TObject);
procedure BtnConvertirDelphiClick(Sender: TObject);
procedure LsbTablasClick(Sender: TObject);
procedure FormShow(Sender: TObject);
private
procedure LoadTables;
// procedure LoadTablesAndFields;
procedure LoadFieldsForTable(const TableName : string);
procedure LoadTablesIntoComboBox(ComboBox : TcxComboBox);
procedure LoadFieldsIntoComboBox(ComboBox : TcxComboBox; const TableName : string);
// function ValorExisteEnListBox(ListBox : TListBox; Valor : string) : Integer;
// function ExtractWord(WordIndex : Integer; const S : string; Delimiters : TSysCharSet) : string;
function SeleccionCheckListBox(CheckListBox : TcxCheckListBox) : Boolean;
procedure AplicarFormato(RichEdit : TcxRichEdit);
procedure ConvertirAMayusculasPreservandoFormato(ARichEdit : TcxRichEdit);
public
end;
var
FRSelectQueryBuilder: TFRSelectQueryBuilder;
implementation
{$R *.dfm}
{ REFERENCIAMOS A LA UNIDAD "ModoOscuroClaro" PARA SABER QUE TEMA TENEMOS
APLICADO Y MANIPULAR LOS COLORES DEL TREEVIEW EN SU EVENTO ONCUSTOMDRAWITEM
}
uses SelectQueryBuilder, SQLAliasGenerator, ModoOscuroClaro, Utileria,
VisualSorting, UFRBuilder;
procedure TFRSelectQueryBuilder.FormCreate(Sender: TObject);
begin
// Cargar las tablas
LoadTables;
// Ordenamos el ListBox de las tablas
TVisualSortHelper.SortListBox(LsbTablas);
//Ejecutamos el OnClick para obtener los campos de la primera tabla
if LsbTablas.Items.Count > 0 then
begin
LsbTablas.ItemIndex := 0; // Selecciona el primer item
LsbTablasClick(nil); // Llama al evento OnClick
end;
// Cargar las tablas en el ComboBox CbTablaSecundaria
LoadTablesIntoComboBox(CbTablaSecundaria);
// Ordenamos ComboBox de las tablas secundarias
TVisualSortHelper.SortComboBox(CbTablaSecundaria);
end;
procedure TFRSelectQueryBuilder.FormShow(Sender: TObject);
begin
Self.Color := $00FFF5EC;
end;
{procedure TFRSelectQueryBuilder.LoadTablesAndFields;
var
Tables, Fields : TFDQuery;
TableNode : TTreeNode;
begin
Tables := TFDQuery.Create(nil);
Fields := TFDQuery.Create(nil);
try
Tables.Connection := DataBase;
Tables.SQL.Text := 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES';
Tables.Open;
while not Tables.Eof do
begin
TableNode := TreeView1.Items.Add(nil, Tables.FieldByName('TABLE_NAME').AsString);
Fields.Connection := DataBase;
Fields.SQL.Text := 'SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ' + QuotedStr(Tables.FieldByName('TABLE_NAME').AsString);
Fields.Open;
while not Fields.Eof do
begin
TreeView1.Items.AddChild(TableNode, Fields.FieldByName('COLUMN_NAME').AsString);
Fields.Next;
end;
Tables.Next;
end;
finally
Tables.Free;
Fields.Free;
end;
end;
}
procedure TFRSelectQueryBuilder.TreeView1Change(Sender: TObject; Node: TTreeNode);
//var
// I : Integer;
begin
{ if Node.Level = 0 then
begin
// Seleccionó una tabla, cargar campos en el CheckListBox
CheckListBox1.Items.Clear;
LbTablaPrincipal.Caption := Node.Text; // Mostramos la tabla seleccionada en el CheckListBox1
CheckListBox1.Items.BeginUpdate;
for I:= 0 to Node.Count - 1 do
begin
CheckListBox1.Items.Add.Text := Node.Item[I].Text;
end;
CheckListBox1.Items.EndUpdate;
end; }
end;
procedure TFRSelectQueryBuilder.TreeView1CustomDrawItem(Sender: TCustomTreeView;
Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
begin //FORZAMOS EL CAMBIO DE LOS COLORES EN EL TREEVIEW
{ with Sender.Canvas do
begin
if cdsSelected in State then
begin
// Estilo para el nodo seleccionado
Brush.Color := $00C56A31; // Fondo del nodo seleccionado
Font.Color := clHighlightText; // Texto del nodo seleccionado
end
else
begin // Estilo para los nodos no seleccionados
if TemaActual = TAppTema.Oscuro then
begin
Brush.Color := $00262626; // Fondo del nodo no seleccionado
Font.Color := clWhite; // Texto del nodo no seleccionado
end
else
begin
Brush.Color := clWhite;
Font.Color := clBlack;
end;
end;
FillRect(Node.DisplayRect(True)); // Dibuja el fondo
TextOut(Node.DisplayRect(False).Left + 2, Node.DisplayRect(False).Top, Node.Text); // Dibuja el texto
DefaultDraw := False; // Evita que Delphi dibuje por defecto
end; }
end;
procedure TFRSelectQueryBuilder.BtnAgregarCamposClick(Sender: TObject);
var
I : Integer;
Campo : string;
begin
cxPageControl1.ActivePageIndex := 0;
if SeleccionCheckListBox(CheckListBox1) = False then
begin
Mensaje('Debe seleccionar al menos un campo de la tabla ' + PnTablaPrincipal.Caption,
'Campo no seleccionado', Advertencia);
Exit;
end;
// Agregar campos de la tabla principal (CheckListBox1)
for I := 0 to CheckListBox1.Items.Count - 1 do
begin
// if CheckListBox1.Checked[I] then
if CheckListBox1.Items.Items[I].State = cbsChecked then
begin
Campo := PnTablaPrincipal.Caption + '.' + CheckListBox1.Items.Items[I].Text;
if ListCamposSeleccionados.Items.IndexOf(Campo) = -1 then
ListCamposSeleccionados.Items.Add(Campo);
end;
end;
// Agregar campos de la tabla secundaria (CheckListBox2)
for I := 0 to CheckListBox2.Items.Count - 1 do
begin
if CheckListBox2.Items.Items[I].State = cbsChecked then
begin
// Campo := CbTablaSecundaria.Text + '.' + CheckListBox2.Items.Strings[I];
Campo := CbTablaSecundaria.Text + '.' + CheckListBox2.Items.Items[I].Text;
if ListCamposSeleccionados.Items.IndexOf(Campo) = -1 then
ListCamposSeleccionados.Items.Add(Campo);
end;
end;
end;
procedure TFRSelectQueryBuilder.BtnAgregarJoinClick(Sender: TObject);
var
JoinType, NewCondition : string;
I : Integer;
JoinExists, ConditionExists : Boolean;
begin
if (CbTipoJoin.Text = '') then
begin
Mensaje('Debe seleccionar el tipo de join.',
'Tipo de join no seleccionado', Advertencia);
CbTipoJoin.SetFocus;
Exit;
end;
if (CbTablaSecundaria.Text = '') then
begin
Mensaje('Debe seleccionar la tabla secundaria.',
'Tabla no seleccionada', Advertencia);
CbTablaSecundaria.SetFocus;
Exit;
end;
if (CbCampoTablaSecundaria.Text = '') then
begin
Mensaje('Debe seleccionar el campo por el cual se hara el join.',
'Campo no seleccionado', Advertencia);
CbCampoTablaSecundaria.SetFocus;
Exit;
end;
JoinType := CbTipoJoin.Text;
// Crear la nueva condición de join
NewCondition := UpperCase(CbTablaSecundaria.Text) + '.' + UpperCase(CbCampoTablaSecundaria.Text) +
' = ' + UpperCase(PnTablaPrincipal.Caption) + '.' + UpperCase(CbCampoTablaSecundaria.Text);
// Verificar si ya existe un JOIN para esta tabla
JoinExists := False;
ConditionExists := False;
for I:= 0 to MmJoin.Lines.Count - 1 do
begin
// Buscar el JOIN inicial o las condiciones AND
if (Pos(JoinType + ' ' + UpperCase(CbTablaSecundaria.Text) + ' ON ', MmJoin.Lines[I]) > 0) then
begin
JoinExists := True;
// Verificar si la condición ya existe en la línea del JOIN
if (Pos(NewCondition, MmJoin.Lines[I]) > 0) then
begin
ConditionExists := True;
Break;
end;
end
else
if (Pos(NewCondition, MmJoin.Lines[I]) > 0)
or (Pos('AND ' + NewCondition, MmJoin.Lines[I]) > 0) then
begin
ConditionExists := True;
Break;
end;
end;
// Verificar si la condición ya existe
if ConditionExists then
begin
Mensaje('Ya existe una condición de join para este campo.',
'Condición ya existe', Advertencia);
Exit;
end;
// Si no existe un JOIN para esta tabla, crear uno nuevo
if not JoinExists then
begin
MmJoin.Lines.Add(JoinType + ' ' + UpperCase(CbTablaSecundaria.Text) + ' ON ' + NewCondition);
end
else
begin
// Agregar la nueva condición al final del Memo
MmJoin.Lines.Add('AND ' + NewCondition);
end;
AplicarFormato(MmJoin); //Le aplicamos el formato para el resaltado de sintaxis
{COMO EL RichEdit NO TIENE LA PROPIEDAD CHARCASE SE DEBE
CREAR ESTA FUNCIONALIDAD EN FORMA MANUAL. }
ConvertirAMayusculasPreservandoFormato(MmJoin);
end;
procedure TFRSelectQueryBuilder.BtnAgregarJoinMouseEnter(Sender: TObject);
begin
TcxButton(Sender).Font.Style := [TFontStyle.fsBold];
end;
procedure TFRSelectQueryBuilder.BtnAgregarJoinMouseLeave(Sender: TObject);
begin
TcxButton(Sender).Font.Style := [];
end;
procedure TFRSelectQueryBuilder.BtnConstruirClick(Sender: TObject);
var
I, CampoCount : Integer;
SelectFields, FromClause, JoinClause : string;
begin
if MmJoin.Lines.Count = 0 then
begin
Mensaje('Debe establecer el join con la tabla.',
'join no establecido', Advertencia);
BtnAgregarJoin.SetFocus;
Exit;
end;
// Verificar si hay campos agregados al ListBox
if ListCamposSeleccionados.Items.Count = 0 then
begin
Mensaje('Debe agregar al menos un campo.',
'Campo no agregado', Advertencia);
Exit;
end;
// Construir la cláusula SELECT
SelectFields := '';
CampoCount := 0;
for I:= 0 to ListCamposSeleccionados.Items.Count - 1 do
begin
Inc(CampoCount);
SelectFields := SelectFields + ListCamposSeleccionados.Items[I] + ', ';
if (CampoCount mod 5) = 0 then
begin
SelectFields := SelectFields + sLineBreak;
end;
end;
SelectFields := Copy(SelectFields, 1, Length(SelectFields) - 2); // Eliminar la última coma
// Construir la cláusula FROM
FromClause := 'FROM ' + PnTablaPrincipal.Caption;
// Construir la cláusula JOIN
JoinClause := '';
for I := 0 to MmJoin.Lines.Count - 1 do
begin
JoinClause := JoinClause + MmJoin.Lines[I] + sLineBreak;
end;
// Construir el SQL final
cxRichEdit1.Lines.Clear;
cxRichEdit1.Lines.Add('SELECT ' + SelectFields);
cxRichEdit1.Lines.Add(FromClause);
cxRichEdit1.Lines.Add(JoinClause);
AplicarFormato(cxRichEdit1); //Le aplicamos el formato para el resaltado de sintaxis
cxPageControl1.ActivePageIndex := 1;
end;
procedure TFRSelectQueryBuilder.BtnConvertirDelphiClick(Sender: TObject);
var
I : Integer;
myFile : TextFile;
begin
if cxRichEdit1.Lines.Count = 0 then
begin
Mensaje('Debe generar el select',
'Operación Invalida', Advertencia);
Abort;
end;
//SCRIPT NATIVO PARA DELPHI
AssignFile(myFile, GetCurrentDir + '\SQL_SELECT_JOIN.txt');
ReWrite(myFile);
WriteLn(myFile, 'with QryDataset, SQL do ');
WriteLn(myFile, ' begin');
WriteLn(myFile, ' Close;');
WriteLn(myFile, ' Clear;');
for I:= 0 to cxRichEdit1.Lines.Count - 1 do
begin
WriteLn(myFile, ' Add(''' + cxRichEdit1.Lines.Strings[I] + ''');');
end;
WriteLn(myFile, ' Open;');
WriteLn(myFile, ' end;');
CloseFile(myFile);
if Mensaje('¿Desea ver el Query en Pantalla?',
'SQL SELECT JOIN',Pregunta, 2) = True then
begin
//ABRIMOS EL ARCHIVO GENERADO
ShellExecute(Handle,'open','c:\windows\notepad.exe',
PWideChar(GetCurrentDir + '\SQL_SELECT_JOIN.txt'), nil, SW_SHOWNORMAL);
end;
end;
procedure TFRSelectQueryBuilder.BtnSalirClick(Sender: TObject);
begin
Close;
end;
procedure TFRSelectQueryBuilder.BtRemoveItemClick(Sender: TObject);
begin
if ListCamposSeleccionados.Items.Count > -1 then
ListCamposSeleccionados.Items.Delete(ListCamposSeleccionados.ItemIndex);
end;
procedure TFRSelectQueryBuilder.BtnFormatearConAliasClick(Sender: TObject);
var
SQLProcessor : TSQLAliasProcessor;
begin
if cxRichEdit1.Lines.Count = 0 then
begin
Mensaje('Debe generar el select',
'Operación Invalida', Advertencia);
Abort;
end;
SQLProcessor := TSQLAliasProcessor.Create;
try
// Aplica los aliases al cxRichEdit1
SQLProcessor.ApplySQLAliases(cxRichEdit1);
finally
SQLProcessor.Free;
end;
AplicarFormato(cxRichEdit1); //Le aplicamos el formato para el resaltado de sintaxis
end;
procedure TFRSelectQueryBuilder.CbTablaSecundariaPropertiesChange(Sender: TObject);
begin //Cargamos en el ComboBox los campos de la tabla secundaria
if CbTablaSecundaria.Text <> EmptyStr then
begin
// Cargar campos de la tabla secundaria en el CheckListBox2
LoadFieldsForTable(CbTablaSecundaria.Text);
{ Cargar los campos de la tabla secundaria seleccionada en el
CbCampoTablaSecundaria para seleccionar el campo por el cual
se hara el join. }
CbCampoTablaSecundaria.Clear;
LoadFieldsIntoComboBox(CbCampoTablaSecundaria, CbTablaSecundaria.Text);
PnTablaSecundaria.Caption := CbTablaSecundaria.Text;
end
else
begin //Si se limpia el ComboBox de la tabla secundaria se limpian los componentes
CbCampoTablaSecundaria.Properties.Items.Clear;
CbCampoTablaSecundaria.Clear;
CheckListBox2.Items.Clear;
PnTablaSecundaria.Caption := '';
end;
end;
procedure TFRSelectQueryBuilder.LoadTables;
var
Tables : TFDQuery;
begin
LsbTablas.Items.Clear;
Tables := TFDQuery.Create(nil);
try
Tables.Connection := FRBuilder.DataBase;
Tables.SQL.Text := 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES';
Tables.Open;
while not Tables.Eof do
begin
LsbTablas.Items.Add(Tables.FieldByName('TABLE_NAME').AsString);
Tables.Next;
end;
finally
Tables.Free;
end;
end;
procedure TFRSelectQueryBuilder.LoadFieldsForTable(const TableName : string);
var
Fields : TFDQuery;
begin
Fields := TFDQuery.Create(nil);
try
Fields.Connection := FRBuilder.DataBase;
Fields.SQL.Text := 'SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ' + QuotedStr(TableName);
Fields.Open;
CheckListBox2.Items.Clear;
CheckListBox2.Items.BeginUpdate;
while not Fields.Eof do
begin
// CheckListBox2.Items.Add(Fields.FieldByName('COLUMN_NAME').AsString);
CheckListBox2.Items.Add.Text := Fields.FieldByName('COLUMN_NAME').AsString;
Fields.Next;
end;
finally
Fields.Free;
CheckListBox2.Items.EndUpdate;
end;
end;
procedure TFRSelectQueryBuilder.LoadTablesIntoComboBox(ComboBox : TcxComboBox);
var
Tables : TFDQuery;
begin
ComboBox.Properties.Items.Clear;
Tables := TFDQuery.Create(nil);
try
Tables.Connection := FRBuilder.DataBase;
Tables.SQL.Text := 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES';
Tables.Open;
ComboBox.Properties.Items.Clear;
while not Tables.Eof do
begin
ComboBox.Properties.Items.Add(Tables.FieldByName('TABLE_NAME').AsString);
Tables.Next;
end;
finally
Tables.Free;
end;
end;
procedure TFRSelectQueryBuilder.LsbTablasClick(Sender: TObject);
var
Fields : TFDQuery;
begin
// Seleccionó una tabla, cargar campos en el CheckListBox
CheckListBox1.Items.Clear;
PnTablaPrincipal.Caption := LsbTablas.Items.Strings[LsbTablas.ItemIndex]; // Mostramos la tabla seleccionada en el CheckListBox1
CheckListBox1.Items.BeginUpdate;
Fields := TFDQuery.Create(nil);
try
Fields.Connection := FRBuilder.DataBase;
Fields.SQL.Text := 'SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ' + QuotedStr(LsbTablas.Items.Strings[LsbTablas.ItemIndex]);
Fields.Open;
while not Fields.Eof do
begin
CheckListBox1.Items.Add.Text := Fields.FieldByName('COLUMN_NAME').AsString;
Fields.Next;
end;
finally
Fields.Free;
end;
CheckListBox1.Items.EndUpdate;
end;
procedure TFRSelectQueryBuilder.LoadFieldsIntoComboBox(ComboBox : TcxComboBox; const TableName : string);
var
Fields : TFDQuery;
begin
Fields := TFDQuery.Create(nil);
try
Fields.Connection := FRBuilder.DataBase;
Fields.SQL.Text := 'SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ' + QuotedStr(TableName);
Fields.Open;
ComboBox.Properties.Items.Clear;
while not Fields.Eof do
begin
ComboBox.Properties.Items.Add(Fields.FieldByName('COLUMN_NAME').AsString);
Fields.Next;
end;
finally
Fields.Free;
end;
end;
{
function TForm1.ValorExisteEnListBox(ListBox : TListBox; Valor : string) : Integer;
var
I : Integer;
begin
Result := -1;
for I := 0 to ListBox.Count - 1 do
begin
if AnsiUpperCase(ListBox.Items[I]) = AnsiUpperCase(Valor) then
begin
Result := I;
Exit;
end;
end;
end;
function TForm1.ExtractWord(WordIndex : Integer; const S : string; Delimiters : TSysCharSet) : string;
var
I, WordCount : Integer;
begin
WordCount := 1;
Result := '';
I := 1;
while I <= Length(S) do
begin
if not CharInSet(S[I], Delimiters) then
begin
if WordCount = WordIndex then
Result := Result + S[I]
else
Inc(WordCount);
end
else
begin
if WordCount = WordIndex then
Break;
end;
Inc(I);
end;
end;
}
function TFRSelectQueryBuilder.SeleccionCheckListBox(CheckListBox : TcxCheckListBox) : Boolean;
var
I : Integer;
begin
Result := False;
for I:= 0 to CheckListBox.Count - 1 do
begin
// if CheckListBox.Checked[I] then
if CheckListBox.Items.Items[I].State = cbsChecked then
begin
Result := True;
Break;
end;
end;
end;
procedure TFRSelectQueryBuilder.AplicarFormato(RichEdit : TcxRichEdit);
var
tmp : string;
Mc : TMatchCollection;
I : Integer;
begin
tmp := StringReplace(RichEdit.Text, #13#10, ' ', [rfReplaceAll]);
Mc := TRegEx.Matches(tmp, 'SELECT|FROM|WHERE|GROUP|BY|HAVING|ORDER|TOP|DISTINCT|ALL|AS|' +
'INNER|OUTER|LEFT|RIGHT|FULL|JOIN|CROSS|ON|AND|OR|NOT|IN|BETWEEN|' +
'LIKE|IS|NULL|CASE|WHEN|THEN|ELSE|END|OVER|PARTITION|WITH|UNION|' +
'EXCEPT|INTERSECT|PIVOT|UNPIVOT|FOR|OFFSET|FETCH');
// Desactivar redibujado para mejorar rendimiento
RichEdit.Lines.BeginUpdate;
try
for I := 0 to Mc.Count - 1 do
begin
RichEdit.SelStart := Mc.Item[I].Index - 1;
RichEdit.SelLength := Mc.Item[I].Length;
RichEdit.SelAttributes.Color := clBlue;
RichEdit.SelAttributes.Style := [fsBold];
end;
finally
RichEdit.Lines.EndUpdate;
RichEdit.SelLength := 0;
end;
end;
procedure TFRSelectQueryBuilder.ConvertirAMayusculasPreservandoFormato(ARichEdit : TcxRichEdit);
var
I, LongitudOriginal : Integer;
TextoOriginal, PalabraActual : string;
begin
TextoOriginal := ARichEdit.Text;
LongitudOriginal := Length(TextoOriginal);
ARichEdit.Lines.BeginUpdate;
try
// Procesar cada carácter individualmente para preservar el formato
for I := 0 to LongitudOriginal - 1 do
begin
ARichEdit.SelStart := I;
ARichEdit.SelLength := 1;
PalabraActual := ARichEdit.SelText;
// Solo reemplazar si es una letra en minúscula
if (PalabraActual <> UpperCase(PalabraActual)) then
begin
ARichEdit.SelText := UpperCase(PalabraActual);
end;
end;
finally
ARichEdit.Lines.EndUpdate;
ARichEdit.SelLength := 0;
end;
end;
end.