-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUClaseCreateIndex.pas
More file actions
132 lines (106 loc) · 3.4 KB
/
Copy pathUClaseCreateIndex.pas
File metadata and controls
132 lines (106 loc) · 3.4 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
unit UClaseCreateIndex;
interface // CLASE PARA MANEJAR LA CREACION DE INDICES
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus,
Vcl.StdCtrls, Vcl.ExtCtrls;
type
// Enumerado para el tipo de índice
TTipoIndice = (tiNormal, tiUnico, tiClusterizado, tiUnicoClusterizado);
// Clase para representar un índice
TIndice = class
private
FNombre : string;
FTabla : string;
FCampos : TStringList;
FCamposIncluidos : TStringList;
FTipo : TTipoIndice;
FFactorRelleno : Integer;
FDescripcion : string;
public
constructor Create;
destructor Destroy; override;
property Nombre : string read FNombre write FNombre;
property Tabla : string read FTabla write FTabla;
property Campos : TStringList read FCampos;
property CamposIncluidos : TStringList read FCamposIncluidos;
property Tipo : TTipoIndice read FTipo write FTipo;
property FactorRelleno : Integer read FFactorRelleno write FFactorRelleno;
property Descripcion : string read FDescripcion write FDescripcion;
function GenerarScriptSQL : string;
end;
implementation
// IMPLEMENTACION DE LA CLASE INDICE
// Implementación del constructor de TIndice
constructor TIndice.Create;
begin
inherited;
FCampos := TStringList.Create;
FCamposIncluidos := TStringList.Create;
FTipo := tiNormal;
FFactorRelleno := 0; // 0 significa no usar factor de relleno
end;
destructor TIndice.Destroy;
begin
FCampos.Free;
FCamposIncluidos.Free;
inherited;
end;
function TIndice.GenerarScriptSQL : string;
var
SB : TStringBuilder;
I : Integer;
TipoIndiceStr, CamposStr, CamposIncluidos : string;
begin
SB := TStringBuilder.Create;
try
// Comentario descriptivo
if FDescripcion <> '' then
begin
SB.AppendLine('/* ');
SB.AppendLine(FDescripcion);
SB.AppendLine('*/');
SB.AppendLine;
end;
// Tipo de índice
case FTipo of
tiNormal : TipoIndiceStr := 'NONCLUSTERED';
tiUnico : TipoIndiceStr := 'UNIQUE NONCLUSTERED';
tiClusterizado : TipoIndiceStr := 'CLUSTERED';
tiUnicoClusterizado : TipoIndiceStr := 'UNIQUE CLUSTERED';
end;
// Armar consulta CREATE INDEX
SB.AppendLine('CREATE ' + TipoIndiceStr + ' INDEX ' + FNombre);
SB.AppendLine('ON ' + FTabla);
// Campos del índice
CamposStr := '';
for I := 0 to FCampos.Count - 1 do
begin
if I > 0 then
CamposStr := CamposStr + ', ';
// Aquí se podría añadir ASC/DESC si se implementa la elección de dirección
CamposStr := CamposStr + FCampos[I];
end;
SB.AppendLine('(' + CamposStr + ')');
// Campos incluidos (INCLUDE)
if FCamposIncluidos.Count > 0 then
begin
CamposIncluidos := '';
for I := 0 to FCamposIncluidos.Count - 1 do
begin
if I > 0 then
CamposIncluidos := CamposIncluidos + ', ';
CamposIncluidos := CamposIncluidos + FCamposIncluidos[I];
end;
SB.AppendLine('INCLUDE (' + CamposIncluidos + ')');
end;
// Factor de relleno (FILLFACTOR)
if FFactorRelleno > 0 then
SB.AppendLine('WITH (FILLFACTOR = ' + IntToStr(FFactorRelleno) + ')');
SB.Append('GO');
Result := SB.ToString;
finally
SB.Free;
end;
end;
end.