-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUClaseForeignKey.pas
More file actions
163 lines (130 loc) · 4.24 KB
/
Copy pathUClaseForeignKey.pas
File metadata and controls
163 lines (130 loc) · 4.24 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
unit UClaseForeignKey;
interface // CLASE PARA MANEJAR LA CREACION DE UN CONSTRAINT DE TIPO FOREIGN KEY
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
// Enumerados para acciones de integridad referencial
TAccionReferencial = (arNoAction, arCascade, arSetNull, arSetDefault);
TForeignKey = class
private
FNombre : string;
FTablaOrigen : string;
FCamposOrigen : TStringList;
FTablaDestino : string;
FCamposDestino : TStringList;
FAccionUpdate : TAccionReferencial;
FAccionDelete : TAccionReferencial;
FDescripcion : string;
public
constructor Create;
destructor Destroy; override;
property Nombre : string read FNombre write FNombre;
property TablaOrigen : string read FTablaOrigen write FTablaOrigen;
property CamposOrigen : TStringList read FCamposOrigen;
property TablaDestino : string read FTablaDestino write FTablaDestino;
property CamposDestino : TStringList read FCamposDestino;
property AccionUpdate : TAccionReferencial read FAccionUpdate write FAccionUpdate;
property AccionDelete : TAccionReferencial read FAccionDelete write FAccionDelete;
property Descripcion : string read FDescripcion write FDescripcion;
function GenerarScriptCreacion : string;
function GenerarScriptEliminacion : string;
end;
implementation
// Implementación del constructor de TForeignKey
constructor TForeignKey.Create;
begin
inherited;
FCamposOrigen := TStringList.Create;
FCamposDestino := TStringList.Create;
FAccionUpdate := arNoAction;
FAccionDelete := arNoAction;
end;
destructor TForeignKey.Destroy;
begin
FCamposOrigen.Free;
FCamposDestino.Free;
inherited;
end;
function TForeignKey.GenerarScriptCreacion : string;
var
SB : TStringBuilder;
I : Integer;
CamposOrigenStr, CamposDestinoStr, AccionUpdateStr, AccionDeleteStr : string;
begin
SB := TStringBuilder.Create;
try
// Comentario descriptivo
if FDescripcion <> '' then
begin
SB.AppendLine('/* ');
SB.AppendLine(FDescripcion);
SB.AppendLine('*/');
SB.AppendLine;
end;
// Encabezado ALTER TABLE
SB.AppendLine('ALTER TABLE ' + FTablaOrigen);
SB.Append('ADD CONSTRAINT ' + FNombre + ' FOREIGN KEY');
// Campos origen
SB.Append(' (');
CamposOrigenStr := '';
for I := 0 to FCamposOrigen.Count - 1 do
begin
if I > 0 then
CamposOrigenStr := CamposOrigenStr + ', ';
CamposOrigenStr := CamposOrigenStr + FCamposOrigen[I];
end;
SB.Append(CamposOrigenStr + ')');
// Referencias a tabla destino
SB.AppendLine();
SB.Append('REFERENCES ' + FTablaDestino + ' (');
CamposDestinoStr := '';
for I := 0 to FCamposDestino.Count - 1 do
begin
if I > 0 then
CamposDestinoStr := CamposDestinoStr + ', ';
CamposDestinoStr := CamposDestinoStr + FCamposDestino[I];
end;
SB.Append(CamposDestinoStr + ')');
// Acciones referenciales
case FAccionUpdate of
arNoAction: AccionUpdateStr := 'NO ACTION';
arCascade: AccionUpdateStr := 'CASCADE';
arSetNull: AccionUpdateStr := 'SET NULL';
arSetDefault: AccionUpdateStr := 'SET DEFAULT';
end;
case FAccionDelete of
arNoAction: AccionDeleteStr := 'NO ACTION';
arCascade: AccionDeleteStr := 'CASCADE';
arSetNull: AccionDeleteStr := 'SET NULL';
arSetDefault: AccionDeleteStr := 'SET DEFAULT';
end;
SB.AppendLine();
SB.Append('ON UPDATE ' + AccionUpdateStr);
SB.AppendLine();
SB.Append('ON DELETE ' + AccionDeleteStr);
SB.AppendLine();
SB.Append('GO');
Result := SB.ToString;
finally
SB.Free;
end;
end;
function TForeignKey.GenerarScriptEliminacion : string;
var
SB : TStringBuilder;
begin
SB := TStringBuilder.Create;
try
SB.AppendLine('IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N''' +
FNombre + ''') AND parent_object_id = OBJECT_ID(N''' + FTablaOrigen + '''))');
SB.AppendLine(' ALTER TABLE ' + FTablaOrigen);
SB.AppendLine(' DROP CONSTRAINT ' + FNombre + ';');
SB.AppendLine('GO');
Result := SB.ToString;
finally
SB.Free;
end;
end;
end.