Repository navigation
Expand file tree
/
Copy pathMigrationFormatter.cs
More file actions
303 lines (273 loc) · 9.79 KB
/
Copy pathMigrationFormatter.cs
File metadata and controls
303 lines (273 loc) · 9.79 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
using System;
using System.CodeDom.Compiler;
using System.Data.Entity.Core.Common;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.Migrations.Model;
using System.Globalization;
using System.IO;
namespace SQLite.CodeFirst.Utility
{
public static class MigrationFormatter
{
const int _defaultStringMaxLength = 255;
const int _defaultNumericPrecision = 10;
const byte _defaultTimePrecision = 7;
const byte _defaultNumericScale = 0;
/// <summary>
/// Builds a column type
/// </summary>
/// <returns> SQL representing the data type. </returns>
public static string BuildColumnType(DbProviderManifest providerManifest, ColumnModel column)
{
if (column == null)
return string.Empty;
return column.IsTimestamp ? "rowversion" : BuildPropertyType(providerManifest, column);
}
/// <summary>
/// Builds a SQL property type fragment from the specified <see cref="ColumnModel"/>.
/// </summary>
public static string BuildPropertyType(DbProviderManifest providerManifest, PropertyModel column)
{
if (providerManifest == null || column == null)
return string.Empty;
var originalStoreType = column.StoreType;
if (string.IsNullOrWhiteSpace(originalStoreType))
{
var typeUsage = providerManifest.GetStoreType(column.TypeUsage).EdmType;
originalStoreType = typeUsage.Name;
}
var storeType = originalStoreType;
const string maxSuffix = "(max)";
if (storeType.EndsWith(maxSuffix, StringComparison.Ordinal))
storeType = storeType.Substring(0, storeType.Length - maxSuffix.Length) + maxSuffix;
switch (originalStoreType.ToUpperInvariant())
{
case "DECIMAL":
case "NUMERIC":
storeType += "(" + (column.Precision ?? _defaultNumericPrecision)
+ ", " + (column.Scale ?? _defaultNumericScale) + ")";
break;
case "DATETIME":
case "TIME":
storeType += "(" + (column.Precision ?? _defaultTimePrecision) + ")";
break;
case "BLOB":
case "VARCHAR2":
case "VARCHAR":
case "CHAR":
case "NVARCHAR":
case "NVARCHAR2":
storeType += "(" + (column.MaxLength ?? _defaultStringMaxLength) + ")";
break;
}
return storeType;
}
/// <summary>
/// Gets an <see cref="IndentedTextWriter" /> object used to format SQL script.
/// </summary>
/// <returns> An empty text writer to use for SQL generation. </returns>
public static IndentedTextWriter CreateIndentedTextWriter()
{
var writer = new StringWriter(CultureInfo.InvariantCulture);
try
{
return new IndentedTextWriter(writer);
}
catch
{
writer.Dispose();
throw;
}
}
/// <summary>
/// Remove occurrences of "dbo." from the supplied string.
/// </summary>
public static string RemoveDbo(string str)
{
if (str == null)
return string.Empty;
return str.Replace("dbo.", string.Empty);
}
/// <summary>
/// Surround with double-quotes Sqlite reserved words.
/// </summary>
public static string ReservedWord(string word)
{
if (word == null)
return string.Empty;
switch (word.ToUpper(CultureInfo.InvariantCulture))
{
case "ABORT":
case "ACTION":
case "ADD":
case "AFTER":
case "ALL":
case "ALTER":
case "ANALYZE":
case "AND":
case "AS":
case "ASC":
case "ATTACH":
case "AUTOINCREMENT":
case "BEFORE":
case "BEGIN":
case "BETWEEN":
case "BY":
case "CASCADE":
case "CASE":
case "CAST":
case "CHECK":
case "COLLATE":
case "COLUMN":
case "COMMIT":
case "CONFLICT":
case "CONSTRAINT":
case "CREATE":
case "CROSS":
case "CURRENT_DATE":
case "CURRENT_TIME":
case "CURRENT_TIMESTAMP":
case "DATABASE":
case "DEFAULT":
case "DEFERRABLE":
case "DEFERRED":
case "DELETE":
case "DESC":
case "DETACH":
case "DISTINCT":
case "DROP":
case "EACH":
case "ELSE":
case "END":
case "ESCAPE":
case "EXCEPT":
case "EXCLUSIVE":
case "EXISTS":
case "EXPLAIN":
case "FAIL":
case "FOR":
case "FOREIGN":
case "FROM":
case "FULL":
case "GLOB":
case "GROUP":
case "HAVING":
case "IF":
case "IGNORE":
case "IMMEDIATE":
case "IN":
case "INDEX":
case "INDEXED":
case "INITIALLY":
case "INNER":
case "INSERT":
case "INSTEAD":
case "INTERSECT":
case "INTO":
case "IS":
case "ISNULL":
case "JOIN":
case "KEY":
case "LEFT":
case "LIKE":
case "LIMIT":
case "MATCH":
case "NATURAL":
case "NO":
case "NOT":
case "NOTNULL":
case "NULL":
case "OF":
case "OFFSET":
case "ON":
case "OR":
case "ORDER":
case "OUTER":
case "PLAN":
case "PRAGMA":
case "PRIMARY":
case "QUERY":
case "RAISE":
case "RECURSIVE":
case "REFERENCES":
case "REGEXP":
case "REINDEX":
case "RELEASE":
case "RENAME":
case "REPLACE":
case "RESTRICT":
case "RIGHT":
case "ROLLBACK":
case "ROW":
case "SAVEPOINT":
case "SELECT":
case "SET":
case "TABLE":
case "TEMP":
case "TEMPORARY":
case "THEN":
case "TO":
case "TRANSACTION":
case "TRIGGER":
case "UNION":
case "UNIQUE":
case "UPDATE":
case "USING":
case "VACUUM":
case "VALUES":
case "VIEW":
case "VIRTUAL":
case "WHEN":
case "WHERE":
case "WITH":
case "WITHOUT":
return '"' + word + '"';
default:
return word;
}
}
public static string UniqueConflictText(AnnotationValues uniqueAnnotation)
{
if (uniqueAnnotation == null)
return string.Empty;
var uniqueText = Convert.ToString(uniqueAnnotation.NewValue, CultureInfo.InvariantCulture);
OnConflictAction action;
if (!uniqueText.StartsWith("OnConflict:", StringComparison.OrdinalIgnoreCase))
return string.Empty;
var actionText = uniqueText.Remove(0, "OnConflict:".Length).Trim();
if (!Enum.TryParse(actionText, out action))
return string.Empty;
if (action == OnConflictAction.None)
return string.Empty;
return " ON CONFLICT " + action.ToString().ToUpperInvariant();
}
public static string CollateFunctionText(AnnotationValues collateAnnotation)
{
if (collateAnnotation == null)
return string.Empty;
var collateAttributeText = Convert.ToString(collateAnnotation.NewValue, CultureInfo.InvariantCulture);
string collateFunction;
string collateCustomFunction;
if (collateAttributeText.IndexOf(':') > -1)
{
collateFunction = collateAttributeText.Substring(0, collateAttributeText.IndexOf(':'));
collateCustomFunction = collateAttributeText.Remove(0, collateAttributeText.IndexOf(':') + 1).Trim();
}
else
{
collateFunction = collateAttributeText;
collateCustomFunction = string.Empty;
}
CollationFunction colatteFunctionType;
if (!Enum.TryParse(collateFunction, out colatteFunctionType))
return string.Empty;
if (colatteFunctionType == CollationFunction.None)
{
return string.Empty;
}
return colatteFunctionType == CollationFunction.Custom
? " COLLATE " + collateCustomFunction
: " COLLATE " + colatteFunctionType.ToString().ToUpperInvariant();
}
}
}