-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuSQLFunctionBuilder.pas
More file actions
290 lines (250 loc) · 7.89 KB
/
Copy pathuSQLFunctionBuilder.pas
File metadata and controls
290 lines (250 loc) · 7.89 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
unit uSQLFunctionBuilder;
interface
uses
System.SysUtils, System.Classes, System.Generics.Collections, Data.DB,
FireDAC.Comp.Client, System.IOUtils, System.Threading;
type
TFunctionType = (ftScalar, ftTable);
TParameterDirection = (pdInput, pdOutput);
TFunctionParameter = class
private
FName : string;
FDataType : string;
FDirection : TParameterDirection;
FLength : Integer;
FPrecision : Integer;
FScale : Integer;
public
constructor Create(const AName, ADataType : string; ADirection : TParameterDirection = pdInput;
ALength : Integer = 0; APrecision : Integer = 0; AScale : Integer = 0);
function ToSQLString : string;
property Name : string read FName write FName;
property DataType : string read FDataType write FDataType;
property Direction : TParameterDirection read FDirection write FDirection;
property Length : Integer read FLength write FLength;
property Precision : Integer read FPrecision write FPrecision;
property Scale : Integer read FScale write FScale;
end;
TSQLFunctionBuilder = class
private
FFunctionName : string;
FFunctionType : TFunctionType;
FParameters : TObjectList<TFunctionParameter>;
FReturnType : string;
FReturnLength : Integer;
FReturnPrecision : Integer;
FReturnScale : Integer;
FTableColumns : TObjectList<TFunctionParameter>;
FBody : TStringList;
FSchema : string;
function GetParametersSQL : string;
function GetReturnTypeSQL : string;
function GetTableDefinitionSQL : string;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
function AddParameter(const AName, ADataType : string; ADirection : TParameterDirection = pdInput;
ALength : Integer = 0; APrecision : Integer = 0; AScale : Integer = 0) : TFunctionParameter;
function AddTableColumn(const AName, ADataType : string;
ALength : Integer = 0; APrecision : Integer = 0; AScale : Integer = 0) : TFunctionParameter;
procedure SetReturnType(const AType : string; ALength : Integer = 0;
APrecision : Integer = 0; AScale : Integer = 0);
procedure SetFunctionBody(const ABody : string);
function GenerateSQL : string;
property FunctionName : string read FFunctionName write FFunctionName;
property FunctionType : TFunctionType read FFunctionType write FFunctionType;
property Schema : string read FSchema write FSchema;
property Body : TStringList read FBody;
end;
implementation
{ TFunctionParameter }
constructor TFunctionParameter.Create(const AName, ADataType : string;
ADirection : TParameterDirection; ALength, APrecision, AScale : Integer);
begin
FName := AName;
FDataType := ADataType;
FDirection := ADirection;
FLength := ALength;
FPrecision := APrecision;
FScale := AScale;
end;
function TFunctionParameter.ToSQLString : string;
var
TypeStr : string;
begin
TypeStr := FDataType;
if (FDataType = 'VARCHAR') or (FDataType = 'NVARCHAR')
or (FDataType = 'CHAR') or (FDataType = 'NCHAR') or (FDataType = 'BINARY')
or (FDataType = 'VARBINARY') then
begin
if FLength = -1 then
TypeStr := TypeStr + '(MAX)'
else if FLength > 0 then
TypeStr := TypeStr + '(' + FLength.ToString + ')';
end
else
if (FDataType = 'DECIMAL') or (FDataType = 'NUMERIC') then
begin
if (FPrecision > 0) then
begin
if FScale > 0 then
TypeStr := TypeStr + '(' + FPrecision.ToString + ',' + FScale.ToString + ')'
else
TypeStr := TypeStr + '(' + FPrecision.ToString + ')';
end;
end;
Result := '@' + FName + ' ' + TypeStr;
end;
{ TSQLFunctionBuilder }
constructor TSQLFunctionBuilder.Create;
begin
inherited;
FParameters := TObjectList<TFunctionParameter>.Create(True);
FTableColumns := TObjectList<TFunctionParameter>.Create(True);
FBody := TStringList.Create;
FSchema := 'dbo';
Clear;
end;
destructor TSQLFunctionBuilder.Destroy;
begin
FParameters.Free;
FTableColumns.Free;
FBody.Free;
inherited;
end;
procedure TSQLFunctionBuilder.Clear;
begin
FFunctionName := '';
FFunctionType := ftScalar;
FParameters.Clear;
FReturnType := '';
FReturnLength := 0;
FReturnPrecision := 0;
FReturnScale := 0;
FTableColumns.Clear;
FBody.Clear;
end;
function TSQLFunctionBuilder.AddParameter(const AName, ADataType : string;
ADirection : TParameterDirection; ALength, APrecision, AScale : Integer) : TFunctionParameter;
var
Param: TFunctionParameter;
begin
Param := TFunctionParameter.Create(AName, ADataType, ADirection, ALength, APrecision, AScale);
FParameters.Add(Param);
Result := Param;
end;
function TSQLFunctionBuilder.AddTableColumn(const AName, ADataType : string;
ALength, APrecision, AScale : Integer) : TFunctionParameter;
var
Column: TFunctionParameter;
begin
Column := TFunctionParameter.Create(AName, ADataType, pdInput, ALength, APrecision, AScale);
FTableColumns.Add(Column);
Result := Column;
end;
procedure TSQLFunctionBuilder.SetReturnType(const AType : string;
ALength, APrecision, AScale : Integer);
begin
FReturnType := AType;
FReturnLength := ALength;
FReturnPrecision := APrecision;
FReturnScale := AScale;
end;
procedure TSQLFunctionBuilder.SetFunctionBody(const ABody: string);
begin
FBody.Text := ABody;
end;
function TSQLFunctionBuilder.GetParametersSQL : string;
var
I : Integer;
Params : TStringList;
begin
Params := TStringList.Create;
try
for I := 0 to FParameters.Count - 1 do
Params.Add(FParameters[I].ToSQLString);
Result := String.Join(',' + #13#10, Params.ToStringArray);
finally
Params.Free;
end;
end;
function TSQLFunctionBuilder.GetReturnTypeSQL : string;
var
TypeStr : string;
begin
if FReturnType = '' then
Exit('');
TypeStr := FReturnType;
if (FReturnType = 'VARCHAR') or (FReturnType = 'NVARCHAR')
or (FReturnType = 'CHAR') or (FReturnType = 'NCHAR')
or (FReturnType = 'BINARY') or (FReturnType = 'VARBINARY') then
begin
if FReturnLength = -1 then
TypeStr := TypeStr + '(MAX)'
else
if FReturnLength > 0 then
TypeStr := TypeStr + '(' + FReturnLength.ToString + ')';
end
else
if (FReturnType = 'DECIMAL') or (FReturnType = 'NUMERIC') then
begin
if (FReturnPrecision > 0) then
begin
if FReturnScale > 0 then
TypeStr := TypeStr + '(' + FReturnPrecision.ToString + ',' + FReturnScale.ToString + ')'
else
TypeStr := TypeStr + '(' + FReturnPrecision.ToString + ')';
end;
end;
Result := TypeStr;
end;
function TSQLFunctionBuilder.GetTableDefinitionSQL : string;
var
I : Integer;
Columns : TStringList;
begin
if FTableColumns.Count = 0 then
Exit('');
Columns := TStringList.Create;
try
for I := 0 to FTableColumns.Count - 1 do
begin
Columns.Add(FTableColumns[I].Name + ' ' + FTableColumns[I].DataType);
end;
Result := #13#10 + 'RETURNS TABLE' + #13#10 + '(' + #13#10 +
' ' + String.Join(',' + #13#10 + ' ', Columns.ToStringArray) + #13#10 +
')';
finally
Columns.Free;
end;
end;
function TSQLFunctionBuilder.GenerateSQL : string;
var
SQL : TStringList;
ParametersSQL : string;
begin
SQL := TStringList.Create;
try
SQL.Add('CREATE OR ALTER FUNCTION ' + FSchema + '.' + FFunctionName);
SQL.Add('(');
ParametersSQL := GetParametersSQL;
if ParametersSQL <> '' then
SQL.Add(' ' + ParametersSQL);
SQL.Add(')');
if FFunctionType = ftScalar then
SQL.Add('RETURNS ' + GetReturnTypeSQL)
else
SQL.Add(GetTableDefinitionSQL);
SQL.Add('AS');
SQL.Add('BEGIN');
// Indent body
FBody.Text := ' ' + StringReplace(FBody.Text, #13#10, #13#10 + ' ', [rfReplaceAll]);
SQL.Add(FBody.Text);
SQL.Add('END;');
Result := SQL.Text;
finally
SQL.Free;
end;
end;
end.