-
-
Notifications
You must be signed in to change notification settings - Fork 584
Expand file tree
/
Copy pathdbstructures.mssql.pas
More file actions
515 lines (504 loc) · 18 KB
/
Copy pathdbstructures.mssql.pas
File metadata and controls
515 lines (504 loc) · 18 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
unit dbstructures.mssql;
interface
uses
dbstructures, StrUtils, SysUtils;
type
TMsSqlProvider = class(TSqlProvider)
public
function GetSql(AId: TQueryId): string; override;
end;
var
MSSQLDatatypes: array [0..33] of TDBDatatype =
(
(
Index: dbdtUnknown;
NativeTypes: '99999';
Name: 'UNKNOWN';
Description: 'Unknown data type';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcOther;
),
(
Index: dbdtTinyint;
Name: 'TINYINT';
Description: 'Integer data from 0 through 255.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: True;
LoadPart: False;
Category: dtcInteger;
),
(
Index: dbdtSmallint;
Name: 'SMALLINT';
Description: 'Integer data from -2^15 (-32,768) through 2^15 - 1 (32,767).';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: True;
LoadPart: False;
Category: dtcInteger;
),
(
Index: dbdtInt;
Name: 'INT';
Description: 'Integer (whole number) data from -2^31 (-2,147,483,648) through 2^31 - 1 (2,147,483,647).';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: True;
LoadPart: False;
Category: dtcInteger;
),
(
Index: dbdtBigint;
Name: 'BIGINT';
Description: 'Integer (whole number) data from -2^63 (-9,223,372,036,854,775,808) through 2^63-1 (9,223,372,036,854,775,807).';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: True;
LoadPart: False;
Category: dtcInteger;
),
(
Index: dbdtBit;
Name: 'BIT';
Description: '0 or 1';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: True;
LoadPart: False;
Category: dtcInteger;
),
(
Index: dbdtDecimal;
Name: 'DECIMAL';
Description: 'Fixed precision and scale numeric data from -10^38 +1 through 10^38 1.';
HasLength: True;
RequiresLength: True;
HasBinary: False;
HasDefault: True;
LoadPart: False;
DefLengthSet: '10,0';
Category: dtcReal;
),
(
Index: dbdtNumeric;
Name: 'NUMERIC';
Description: 'Functionally equivalent to decimal.';
HasLength: True;
RequiresLength: True;
HasBinary: False;
HasDefault: True;
LoadPart: False;
DefLengthSet: '10,0';
Category: dtcReal;
),
(
Index: dbdtMoney;
Name: 'MONEY';
Description: 'Monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.';
HasLength: True;
RequiresLength: False;
HasBinary: False;
HasDefault: True;
LoadPart: False;
Category: dtcReal;
),
(
Index: dbdtSmallmoney;
Name: 'SMALLMONEY';
Description: 'Monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit.';
HasLength: True;
RequiresLength: False;
HasBinary: False;
HasDefault: True;
LoadPart: False;
Category: dtcReal;
),
(
Index: dbdtFloat;
Name: 'FLOAT';
Description: 'Floating precision number data with the following valid values: -1.79E + 308 through -2.23E - 308, 0 and 2.23E + 308 through 1.79E + 308.';
HasLength: True;
RequiresLength: False;
HasBinary: False;
HasDefault: True;
LoadPart: False;
Category: dtcReal;
),
(
Index: dbdtReal;
Name: 'REAL';
Description: 'Floating precision number data with the following valid values: -3.40E + 38 through -1.18E - 38, 0 and 1.18E - 38 through 3.40E + 38.';
HasLength: True;
RequiresLength: False;
HasBinary: False;
HasDefault: True;
LoadPart: False;
Category: dtcReal;
),
(
Index: dbdtTime;
Name: 'TIME';
Description: 'The time data type stores time values only, based on a 24-hour clock. '+
'The time data type has a range of 00:00:00.0000000 through 23:59:59.9999999 with an '+
'accuracy of 100 nanoseconds. The default value is 00:00:00.0000000 (midnight). The '+
'time data type supports user-defined fractional second precision, and the storage '+
'size varies from 3 to 6 bytes, based on the precision specified.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: True;
LoadPart: False;
Format: 'hh:nn:ss';
Category: dtcTemporal;
),
(
Index: dbdtDate;
Name: 'DATE';
Description: 'The date data type has a range of January 1, 01 through December 31, '+
'9999 with an accuracy of 1 day. The default value is January 1, 1900. The storage size '+
'is 3 bytes.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: True;
LoadPart: False;
Format: 'yyyy-mm-dd';
Category: dtcTemporal;
),
(
Index: dbdtDatetime;
Name: 'DATETIME';
Description: 'Date and time data from January 1, 1753, through December 31, 9999, with an accuracy of three-hundredths of a second, or 3.33 milliseconds.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: True;
LoadPart: False;
Format: 'yyyy-mm-dd hh:nn:ss.zzz';
Category: dtcTemporal;
),
(
Index: dbdtDatetime2;
Name: 'DATETIME2';
Description: 'Date and time data from January 1,1 AD through December 31, 9999 AD, with an accuracy of three-hundredths of a second, or 3.33 milliseconds.';
HasLength: True;
RequiresLength: False;
HasBinary: False;
HasDefault: True;
LoadPart: False;
Format: 'yyyy-mm-dd hh:nn:ss.zzzzzzz';
Category: dtcTemporal;
),
(
Index: dbdtDatetimeOffset;
Name: 'DATETIMEOFFSET';
Description: 'Defines a date that is combined with a time of a day that has time zone awareness and is based on a 24-hour clock.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: True;
LoadPart: False;
Format: 'yyyy-mm-dd hh:nn:ss.zzzzzzz';
Category: dtcTemporal;
),
(
Index: dbdtSmalldatetime;
Name: 'SMALLDATETIME';
Description: 'Date and time data from January 1, 1900, through June 6, 2079, with an accuracy of one minute.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: True;
LoadPart: False;
Format: 'yyyy-mm-dd hh:nn:ss';
Category: dtcTemporal;
),
(
Index: dbdtTimestamp;
Name: 'TIMESTAMP';
Description: 'A database-wide unique number that gets updated every time a row gets updated.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcInteger;
),
(
Index: dbdtChar;
Name: 'CHAR';
Description: 'Fixed-length non-Unicode character data with a maximum length of 8,000 characters.';
HasLength: True;
RequiresLength: True;
HasBinary: False;
HasDefault: True;
LoadPart: True;
DefLengthSet: '50';
Category: dtcText;
),
(
Index: dbdtVarchar;
Name: 'VARCHAR';
Description: 'Variable-length non-Unicode data with a maximum of 8,000 characters.';
HasLength: True;
RequiresLength: True;
HasBinary: False;
HasDefault: True;
LoadPart: True;
DefLengthSet: '50';
Category: dtcText;
),
(
Index: dbdtText;
Name: 'TEXT';
Description: 'Variable-length non-Unicode data with a maximum length of 2^31 - 1 (2,147,483,647) characters.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: True;
Category: dtcText;
),
(
Index: dbdtNchar;
Name: 'NCHAR';
Description: 'Fixed-length Unicode data with a maximum length of 4,000 characters.';
HasLength: True;
RequiresLength: True;
HasBinary: False;
HasDefault: True;
LoadPart: True;
DefLengthSet: '50';
Category: dtcText;
),
(
Index: dbdtNvarchar;
Name: 'NVARCHAR';
Description: 'Variable-length Unicode data with a maximum length of 4,000 characters. sysname is a system-supplied user-defined data type that is functionally equivalent to nvarchar(128) and is used to reference database object names.';
HasLength: True;
RequiresLength: True;
HasBinary: False;
HasDefault: True;
LoadPart: True;
DefLengthSet: '50';
Category: dtcText;
),
(
Index: dbdtNtext;
Name: 'NTEXT';
Description: 'Variable-length Unicode data with a maximum length of 2^30 - 1 (1,073,741,823) characters.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: True;
Category: dtcText;
),
(
Index: dbdtBinary;
Name: 'BINARY';
Description: 'Fixed-length binary data with a maximum length of 8,000 bytes.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: True;
Category: dtcBinary;
),
(
Index: dbdtVarbinary;
Name: 'VARBINARY';
Description: 'Variable-length binary data with a maximum length of 8,000 bytes.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: True;
Category: dtcBinary;
),
(
Index: dbdtImage;
Name: 'IMAGE';
Description: 'Variable-length binary data with a maximum length of 2^31 - 1 (2,147,483,647) bytes.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcBinary;
),
(
Index: dbdtCursor;
Name: 'CURSOR';
Description: 'A reference to a cursor.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcOther;
),
(
Index: dbdtSqlvariant;
Name: 'SQL_VARIANT';
Description: 'A data type that stores values of various SQL Server-supported data types, except text, ntext, timestamp, and sql_variant.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcOther;
),
(
Index: dbdtTable;
Name: 'TABLE';
Description: 'A special data type used to store a result set for later processing .';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcOther;
),
(
Index: dbdtUniqueidentifier;
Name: 'UNIQUEIDENTIFIER';
Description: 'A globally unique identifier (GUID).';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcOther;
),
(
Index: dbdtHierarchyid;
Name: 'HIERARCHYID';
Description: 'Represents a position in a hierarchy.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcOther;
),
(
Index: dbdtXML;
Name: 'XML';
Description: 'Lets you store XML documents and fragments.';
HasLength: False;
RequiresLength: False;
HasBinary: False;
HasDefault: False;
LoadPart: False;
Category: dtcOther;
)
);
implementation
{$I const.inc}
function TMsSqlProvider.GetSql(AId: TQueryId): string;
begin
case AId of
qDatabaseTable: Result := IfThen(
ServerVersion<=899,
'master..sysdatabases',
'sys.databases'
);
qDatabaseTableId: Result := IfThen(
ServerVersion<=899,
'dbid',
'database_id'
);
qDatabaseDrop: Result := 'DROP DATABASE %s';
qDbObjectsTable: Result := IfThen(
ServerVersion<=899,
'..sysobjects',
'.sys.objects'
);
qDbObjectsCreateCol: Result := IfThen(
ServerVersion<=899,
'crdate',
'create_date'
);
qDbObjectsUpdateCol: Result := IfThen(
ServerVersion<=899,
'',
'modify_date'
);
qDbObjectsTypeCol: Result := IfThen(
ServerVersion<=899,
'xtype',
'type'
);
qRenameTable: Result := 'EXEC sp_rename %s, %s';
qRenameView: Result := 'EXEC sp_rename %s, %s';
qCurrentUserHost: Result := 'SELECT SYSTEM_USER';
qLikeCompare: Result := '%s LIKE %s';
qAddColumn: Result := 'ADD %s';
qChangeColumn: Result := 'ALTER COLUMN %s %s';
qSetColumnComment: Result := 'IF NOT EXISTS (' + sLineBreak +
' SELECT 1' + sLineBreak +
' FROM fn_listextendedproperty (' + sLineBreak +
' N''MS_Description'',' + sLineBreak +
' N''SCHEMA'', :EscapedSchema,' + sLineBreak +
' N''TABLE'', :EscapedName,' + sLineBreak +
' N''COLUMN'', :TargetColumn' + sLineBreak +
' )' + sLineBreak +
')' + sLineBreak +
'BEGIN' + sLineBreak +
' EXEC sys.sp_addextendedproperty' + sLineBreak +
' @name = N''MS_Description'',' + sLineBreak +
' @value = :NewComment,' + sLineBreak +
' @level0type = N''SCHEMA'', @level0name = :EscapedSchema,' + sLineBreak +
' @level1type = N''TABLE'', @level1name = :EscapedName,' + sLineBreak +
' @level2type = N''COLUMN'', @level2name = :TargetColumn' + sLineBreak +
'END' + sLineBreak +
'ELSE BEGIN' + sLineBreak +
' EXEC sys.sp_updateextendedproperty' + sLineBreak +
' @name = N''MS_Description'',' + sLineBreak +
' @value = :NewComment,' + sLineBreak +
' @level0type = N''SCHEMA'', @level0name = :EscapedSchema,' + sLineBreak +
' @level1type = N''TABLE'', @level1name = :EscapedName,' + sLineBreak +
' @level2type = N''COLUMN'', @level2name = :TargetColumn' + sLineBreak +
'END';
qSessionVariables: Result := 'SELECT comment, value FROM master.dbo.syscurconfigs ORDER BY comment';
qGlobalVariables: Result := 'SELECT comment, value FROM master.dbo.syscurconfigs ORDER BY comment';
qISSchemaCol: Result := '%s_CATALOG';
qUSEQuery: Result := 'USE %s';
qKillQuery: Result := 'KILL %d';
qKillProcess: Result := 'KILL %d';
qFuncLength: Result := 'LEN';
qFuncCeil: Result := 'CEILING';
qFuncLeft: Result := 'LEFT(%s, %d)';
qFuncNow: Result := 'GETDATE()';
qFuncLastAutoIncNumber: Result := 'LAST_INSERT_ID()';
qLockedTables: Result := '';
qDisableForeignKeyChecks: Result := '';
qEnableForeignKeyChecks: Result := '';
qForeignKeyDrop: Result := 'DROP FOREIGN KEY %s';
qGetTableColumns: Result := '';
qGetCollations: Result := 'SELECT '''' AS "Collation", '+
''''' AS "Charset", 0 AS "Id", '+
''''' AS "Default", '''' AS "Compiled", '+
'1 AS "Sortlen"';
qGetCharsets: Result := 'SELECT name AS Charset, description AS Description FROM master.sys.syscharsets';
qGetRowCountApprox: Result := IfThen(
FServerVersion >= 900,
'SELECT SUM("rows") FROM "sys"."partitions" WHERE "index_id" IN (0, 1) AND "object_id" = object_id(:EscapedDbSchemaName)',
''
);
qCastAsText: Result := 'CAST(%s AS NVARCHAR('+IntToStr(GRIDMAXDATA)+'))';
else Result := inherited;
end;
end;
end.