-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathAbstract.cfc
More file actions
executable file
·350 lines (320 loc) · 11.6 KB
/
Copy pathAbstract.cfc
File metadata and controls
executable file
·350 lines (320 loc) · 11.6 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
component extends="wheels.migrator.Base"{
/**
* generates sql for a column's data type definition
*/
public string function typeToSQL(required string type, struct options = {}) {
var sql = '';
if (IsDefined("variables.sqlTypes") && StructKeyExists(variables.sqlTypes, arguments.type)) {
if (IsStruct(variables.sqlTypes[arguments.type])) {
sql = variables.sqlTypes[arguments.type]['name'];
if (arguments.type == 'decimal') {
if (
!StructKeyExists(arguments.options, 'precision')
&& StructKeyExists(variables.sqlTypes[arguments.type], 'precision')
) {
arguments.options.precision = variables.sqlTypes[arguments.type]['precision'];
}
if (
!StructKeyExists(arguments.options, 'scale') && StructKeyExists(variables.sqlTypes[arguments.type], 'scale')
) {
arguments.options.scale = variables.sqlTypes[arguments.type]['scale'];
}
if (StructKeyExists(arguments.options, 'precision')) {
if (StructKeyExists(arguments.options, 'scale')) {
sql = sql & '(#arguments.options.precision#,#arguments.options.scale#)';
} else {
sql = sql & '(#arguments.options.precision#)';
}
}
} else {
if (
!StructKeyExists(arguments.options, 'limit') && StructKeyExists(variables.sqlTypes[arguments.type], 'limit')
) {
arguments.options.limit = variables.sqlTypes[arguments.type]['limit'];
}
if (StructKeyExists(arguments.options, 'limit')) {
sql = sql & '(#arguments.options.limit#)';
}
}
} else {
sql = variables.sqlTypes[arguments.type];
}
}
return sql;
}
/**
* throw an exception for adapters without its own addPrimaryKeyOptions implementation
*/
public string function addPrimaryKeyOptions() {
Throw(message = "The `addPrimaryKeyOptions` must be implemented in the storage specific adapter.");
}
/**
* generates sql for a primary key constraint
*/
public string function primaryKeyConstraint(required string name, required array primaryKeys) {
local.sql = "PRIMARY KEY (";
for (local.i = 1; local.i lte ArrayLen(arguments.primaryKeys); local.i++) {
if (local.i != 1) {
local.sql = local.sql & ", ";
}
local.sql = local.sql & arguments.primaryKeys[local.i].toColumnNameSQL();
}
local.sql = local.sql & ")";
return local.sql;
}
/**
* generates sql for column options
*/
public string function addColumnOptions(required string sql, struct options = "#StructNew()#") {
if (StructKeyExists(arguments.options, 'type') && arguments.options.type != 'primaryKey') {
if (StructKeyExists(arguments.options, 'default') && optionsIncludeDefault(argumentCollection = arguments.options)) {
if (
arguments.options.default eq "NULL"
|| (
arguments.options.default eq ""
&& ListFindNoCase("boolean,date,datetime,time,timestamp,decimal,float,integer", arguments.options.type)
)
) {
arguments.sql = arguments.sql & " DEFAULT NULL";
} else if (arguments.options.type == 'boolean') {
arguments.sql = arguments.sql & " DEFAULT #IIf(arguments.options.default, 1, 0)#";
} else if (
arguments.options.default eq ""
&& ListFindNoCase("string,text,char", arguments.options.type)
) {
// Symmetric handling for all string-like types: an empty
// `default=""` means "no default clause" (not `DEFAULT ''`).
// Without this, `t.string("a", default="")` and
// `t.text("b", default="")` produced asymmetric DDL and
// the presence-check skip in validatesPresenceOf fired
// inconsistently between equivalent column types. See
// fresh-VM journal F17.
arguments.sql = arguments.sql;
} else {
arguments.sql = arguments.sql & " DEFAULT #quote(value = arguments.options.default, options = arguments.options)#";
}
}
if (StructKeyExists(arguments.options, 'allowNull')) {
if (arguments.options.allowNull) {
arguments.sql = arguments.sql & " NULL";
} else {
arguments.sql = arguments.sql & " NOT NULL";
}
}
}
if (StructKeyExists(arguments.options, "afterColumn") And Len(Trim(arguments.options.afterColumn)) GT 0) {
arguments.sql = arguments.sql & " AFTER #arguments.options.afterColumn#";
}
return arguments.sql;
}
// what's the purpose of this?
public boolean function optionsIncludeDefault(string type, default = "", boolean allowNull = true) {
return true;
}
/**
* quote value if required
*/
public string function quote(required string value, struct options = {}) {
if (ListFindNoCase("CURRENT_TIMESTAMP", arguments.value)) {
return arguments.value;
}
if (
StructKeyExists(arguments.options, 'type') && ListFindNoCase(
"binary,char,date,datetime,time,timestamp,text,string",
arguments.options.type
)
) {
// Escape every embedded single quote (not just the first) so the
// generated DDL stays balanced for values like "O'Brien's".
arguments.value = "'#Replace(arguments.value, "'", "''", "all")#'";
}
return arguments.value;
}
/**
* surrounds table or index names with quotes
*/
public string function quoteTableName(required string name) {
return "'#Replace(objectCase(arguments.name), ".", "`.`", "ALL")#'";
}
/**
* surrounds column names with quotes
*/
public string function quoteColumnName(required string name) {
return "'#objectCase(arguments.name)#'";
}
/**
* generates sql to create a table
*/
public string function createTable(
required string name,
required array columns,
array primaryKeys = [],
array foreignKeys = []
) {
local.sql = "CREATE TABLE #quoteTableName(arguments.name)# (#Chr(13)##Chr(10)#";
local.iEnd = ArrayLen(arguments.primaryKeys);
if (local.iEnd == 1) {
// if we have a single primary key, define the column with the primaryKey adapter method
local.sql = local.sql & " " & arguments.primaryKeys[1].toPrimaryKeySQL() & ",#Chr(13)##Chr(10)#";
} else if (local.iEnd > 1) {
// add the primary key columns like we would normal columns
for (local.i = 1; local.i <= local.iEnd; local.i++) {
local.sql = local.sql & " " & arguments.primaryKeys[local.i].toSQL();
if (local.i != local.iEnd || ArrayLen(arguments.columns)) {
local.sql = local.sql & ",#Chr(13)##Chr(10)#";
}
}
}
// define the columns in the sql
local.iEnd = ArrayLen(arguments.columns);
for (local.i = 1; local.i <= local.iEnd; local.i++) {
local.sql = local.sql & " " & arguments.columns[local.i].toSQL();
if (local.i != local.iEnd) {
local.sql = local.sql & ",#Chr(13)##Chr(10)#";
}
}
// if we have multiple primarykeys the adapter might need to add a constraint here
if (ArrayLen(arguments.primaryKeys) > 1) {
local.sql = local.sql & ",#Chr(13)##Chr(10)# " & primaryKeyConstraint(argumentCollection = arguments);
}
// define the foreign keys
local.iEnd = ArrayLen(arguments.foreignKeys);
for (local.i = 1; local.i <= local.iEnd; local.i++) {
local.sql = local.sql & ",#Chr(13)##Chr(10)# " & arguments.foreignKeys[local.i].toForeignKeySQL();
}
local.sql = local.sql & "#Chr(13)##Chr(10)#)";
return local.sql;
}
/**
* generates sql to rename a table
*/
public string function renameTable(required string oldName, required string newName) {
return "ALTER TABLE #quoteTableName(arguments.oldName)# RENAME #quoteTableName(arguments.newName)#";
}
/**
* generates sql to drop a table
*/
public string function dropTable(required string name) {
return "DROP TABLE IF EXISTS #quoteTableName(objectCase(arguments.name))#";
}
/**
* generates sql to add a new column to a table
*/
public string function addColumnToTable(required string name, required any column) {
return "ALTER TABLE #quoteTableName(objectCase(arguments.name))# ADD COLUMN #arguments.column.toSQL()#";
}
/**
* generates sql to change an existing column in a table
*/
public string function changeColumnInTable(required string name, required any column) {
return "ALTER TABLE #quoteTableName(objectCase(arguments.name))# CHANGE #quoteColumnName(arguments.column.name)# #arguments.column.toSQL()#";
}
/**
* generates sql to rename an existing column in a table
*/
public string function renameColumnInTable(
required string name,
required string columnName,
required string newColumnName
) {
return "ALTER TABLE #quoteTableName(objectCase(arguments.name))# RENAME COLUMN #quoteColumnName(arguments.columnName)# TO #quoteColumnName(arguments.newColumnName)#";
}
/**
* generates sql to drop a column from a table
*/
public string function dropColumnFromTable(required string name, required string columnName) {
return "ALTER TABLE #quoteTableName(objectCase(arguments.name))# DROP COLUMN #quoteColumnName(arguments.columnName)#";
}
/**
* generates sql to add a foreign key constraint to a table
*/
public string function addForeignKeyToTable(required string name, required any foreignKey) {
return "ALTER TABLE #quoteTableName(objectCase(arguments.name))# ADD #arguments.foreignKey.toSQL()#";
}
/**
* generates sql to drop a foreign key constraint from a table
*/
public string function dropForeignKeyFromTable(required string name, required string keyName) {
return "ALTER TABLE #quoteTableName(objectCase(arguments.name))# DROP FOREIGN KEY #quoteTableName(arguments.keyname)#";
}
/**
* generates sql for foreign key constraint
*/
public string function foreignKeySQL(
required string name,
required string table,
required string referenceTable,
required string column,
required string referenceColumn,
string onUpdate = "",
string onDelete = ""
) {
local.sql = "CONSTRAINT #quoteTableName(arguments.name)# FOREIGN KEY (#quoteColumnName(arguments.column)#) REFERENCES #objectCase(arguments.referenceTable)#(#quoteColumnName(arguments.referenceColumn)#)";
for (local.item in ListToArray("onUpdate,onDelete")) {
if (Len(arguments[local.item])) {
switch (arguments[local.item]) {
case "none":
local.sql = local.sql & " " & UCase(humanize(local.item)) & " NO ACTION";
break;
case "null":
local.sql = local.sql & " " & UCase(humanize(local.item)) & " SET NULL";
break;
default:
local.sql = local.sql & " " & UCase(humanize(local.item)) & " CASCADE";
break;
}
}
}
return local.sql;
}
/**
* generates sql to add database index on a table column
*/
public string function addIndex(
required string table,
string columnNames,
boolean unique = false,
string indexName = "#objectCase(arguments.table)#_#ListFirst(arguments.columnNames)#"
) {
$combineArguments(args = arguments, combine = "columnNames,columnName", required = true);
var sql = "CREATE ";
if (arguments.unique) {
sql = sql & "UNIQUE ";
}
sql = sql & "INDEX #quoteTableName(arguments.indexName)# ON #quoteTableName(arguments.table)#(";
local.columnNamesArray = ListToArray(arguments.columnNames);
local.iEnd = ArrayLen(local.columnNamesArray);
for (local.i = 1; local.i <= local.iEnd; local.i++) {
sql = sql & quoteColumnName(local.columnNamesArray[local.i]);
if (local.i != local.iEnd) {
sql = sql & ",";
}
}
sql = sql & ")";
return sql;
}
/**
* generates sql to remove a database index
*/
public any function removeIndex(required string table, string indexName = "") {
return "DROP INDEX #quoteTableName(arguments.indexName)#";
}
/**
* generates sql to create a view
*/
public string function createView(required string name, required string sql) {
return "CREATE VIEW #quoteTableName(arguments.name)# AS " & arguments.sql;
}
/**
* generates sql to drop a view
*/
public string function dropView(required string name) {
return "DROP VIEW IF EXISTS #quoteTableName(arguments.name)#";
}
public string function addRecordPrefix() {
return "";
}
public string function addRecordSuffix() {
return "";
}
}