-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathSQLiteMigrationSqlGenerator.cs
More file actions
401 lines (318 loc) · 15.5 KB
/
SQLiteMigrationSqlGenerator.cs
File metadata and controls
401 lines (318 loc) · 15.5 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
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity.Core.Common;
using System.Data.Entity.Core.Common.CommandTrees;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Migrations.Model;
using System.Data.Entity.Migrations.Sql;
using System.Diagnostics;
namespace System.Data.SQLite.EF6.Migrations
{
/// <summary>
/// Migration Ddl generator for SQLite
/// </summary>
public class SQLiteMigrationSqlGenerator : MigrationSqlGenerator
{
const string BATCHTERMINATOR = ";\r\n";
/// <summary>
/// Initializes a new instance of the <see cref="SQLiteMigrationSqlGenerator"/> class.
/// </summary>
public SQLiteMigrationSqlGenerator()
{
base.ProviderManifest = ((DbProviderServices)(new SQLiteProviderFactory()).GetService(typeof(DbProviderServices))).GetProviderManifest("");
}
/// <summary>
/// Converts a set of migration operations into database provider specific SQL.
/// </summary>
/// <param name="migrationOperations">The operations to be converted.</param>
/// <param name="providerManifestToken">Token representing the version of the database being targeted.</param>
/// <returns>
/// A list of SQL statements to be executed to perform the migration operations.
/// </returns>
public override IEnumerable<MigrationStatement> Generate(IEnumerable<MigrationOperation> migrationOperations, string providerManifestToken)
{
List<MigrationStatement> migrationStatements = new List<MigrationStatement>();
foreach (MigrationOperation migrationOperation in migrationOperations)
migrationStatements.Add(GenerateStatement(migrationOperation));
return migrationStatements;
}
private MigrationStatement GenerateStatement(MigrationOperation migrationOperation)
{
MigrationStatement migrationStatement = new MigrationStatement();
migrationStatement.BatchTerminator = BATCHTERMINATOR;
migrationStatement.Sql = GenerateSqlStatement(migrationOperation);
return migrationStatement;
}
private string GenerateSqlStatement(MigrationOperation migrationOperation)
{
dynamic concreteMigrationOperation = migrationOperation;
return GenerateSqlStatementConcrete(concreteMigrationOperation);
}
private string GenerateSqlStatementConcrete(MigrationOperation migrationOperation)
{
if (migrationOperation is SqlOperation sqlOperation)
{
return sqlOperation.Sql;
}
throw new Exception("Not found SqlOperation");
}
#region History operations
private string GenerateSqlStatementConcrete(HistoryOperation migrationOperation)
{
SQLiteDdlBuilder ddlBuilder = new SQLiteDdlBuilder();
foreach (DbModificationCommandTree commandTree in migrationOperation.CommandTrees)
{
List<DbParameter> parameters;
// Take care because here we have several queries so we can't use parameters...
switch (commandTree.CommandTreeKind)
{
case DbCommandTreeKind.Insert:
ddlBuilder.AppendSql(SQLiteDmlBuilder.GenerateInsertSql((DbInsertCommandTree)commandTree, out parameters, true));
break;
case DbCommandTreeKind.Delete:
ddlBuilder.AppendSql(SQLiteDmlBuilder.GenerateDeleteSql((DbDeleteCommandTree)commandTree, out parameters, true));
break;
case DbCommandTreeKind.Update:
ddlBuilder.AppendSql(SQLiteDmlBuilder.GenerateUpdateSql((DbUpdateCommandTree)commandTree, out parameters, true));
break;
case DbCommandTreeKind.Function:
case DbCommandTreeKind.Query:
default:
throw new InvalidOperationException(string.Format("Command tree of type {0} not supported in migration of history operations", commandTree.CommandTreeKind));
}
ddlBuilder.AppendSql(BATCHTERMINATOR);
}
return ddlBuilder.GetCommandText();
}
#endregion
#region Move operations (not supported by Jet)
private string GenerateSqlStatementConcrete(MoveProcedureOperation migrationOperation)
{
throw new NotSupportedException("Move operations not supported by SQLite");
}
private string GenerateSqlStatementConcrete(MoveTableOperation migrationOperation)
{
throw new NotSupportedException("Move operations not supported by SQLite");
}
#endregion
#region Procedure related operations (not supported by Jet)
private string GenerateSqlStatementConcrete(AlterProcedureOperation migrationOperation)
{
throw new NotSupportedException("Procedures are not supported by SQLite");
}
private string GenerateSqlStatementConcrete(CreateProcedureOperation migrationOperation)
{
throw new NotSupportedException("Procedures are not supported by SQLite");
}
private string GenerateSqlStatementConcrete(DropProcedureOperation migrationOperation)
{
throw new NotSupportedException("Procedures are not supported by SQLite");
}
private string GenerateSqlStatementConcrete(RenameProcedureOperation migrationOperation)
{
throw new NotSupportedException("Procedures are not supported by SQLite");
}
#endregion
#region Rename operations
private string GenerateSqlStatementConcrete(RenameColumnOperation migrationOperation)
{
throw new NotSupportedException("Cannot rename objects with Jet");
}
private string GenerateSqlStatementConcrete(RenameIndexOperation migrationOperation)
{
throw new NotSupportedException("Cannot rename objects with Jet");
}
private string GenerateSqlStatementConcrete(RenameTableOperation migrationOperation)
{
SQLiteDdlBuilder ddlBuilder = new SQLiteDdlBuilder();
ddlBuilder.AppendSql("ALTER TABLE ");
ddlBuilder.AppendIdentifier(migrationOperation.Name);
ddlBuilder.AppendSql(" RENAME TO ");
ddlBuilder.AppendIdentifier(migrationOperation.NewName);
return ddlBuilder.GetCommandText();
}
#endregion
#region Columns
private string GenerateSqlStatementConcrete(AddColumnOperation migrationOperation)
{
SQLiteDdlBuilder ddlBuilder = new SQLiteDdlBuilder();
ddlBuilder.AppendSql("ALTER TABLE ");
ddlBuilder.AppendIdentifier(migrationOperation.Table);
ddlBuilder.AppendSql(" ADD COLUMN ");
ColumnModel column = migrationOperation.Column;
ddlBuilder.AppendIdentifier(column.Name);
TypeUsage storeType = ProviderManifest.GetStoreType(column.TypeUsage);
ddlBuilder.AppendType(storeType, column.IsNullable ?? true, column.IsIdentity);
//DEFAULT VALUE
if (column.DefaultValue != null)
{
if (column.DefaultValue is DateTime defaultValue)
{
var format = "yyyy-MM-dd HH:mm:ss.ffffff";
if (defaultValue.Kind == DateTimeKind.Utc) format += 'Z';
ddlBuilder.AppendSql($" DEFAULT '{defaultValue.ToString(format)}'");
}
else if (column.DefaultValue is bool boolValue)
{
ddlBuilder.AppendSql($" DEFAULT {(boolValue ? 1 : 0)}");
}
else if (column.DefaultValue is string stringValue)
{
ddlBuilder.AppendSql($" DEFAULT '{stringValue}'");
}
else if (column.DefaultValue is Guid guidValue)
{
ddlBuilder.AppendSql($" DEFAULT '{guidValue}'");
}
else if (column.DefaultValue.GetType().IsEnum)
{
ddlBuilder.AppendSql($" DEFAULT {(int)column.DefaultValue}");
}
else if (column.DefaultValue.GetType().IsValueType)
{
ddlBuilder.AppendSql($" DEFAULT {column.DefaultValue}");
}
}
ddlBuilder.AppendNewLine();
return ddlBuilder.GetCommandText();
}
private string GenerateSqlStatementConcrete(DropColumnOperation migrationOperation)
{
throw new NotSupportedException("Drop column not supported by SQLite");
}
private string GenerateSqlStatementConcrete(AlterColumnOperation migrationOperation)
{
throw new NotSupportedException("Alter column not supported by SQLite");
}
#endregion
#region Foreign keys creation
private string GenerateSqlStatementConcrete(AddForeignKeyOperation migrationOperation)
{
/*
* SQLite supports foreign key creation only during table creation
* At least, during table creation we could try to create relationships but it
* Requires that we sort tables in dependency order (and that there is not a circular reference
*
* Actually we do not create relationship at all
*/
return "";
}
#endregion
#region Primary keys creation
private string GenerateSqlStatementConcrete(AddPrimaryKeyOperation migrationOperation)
{
// Actually primary key creation is supported only during table creation
SQLiteDdlBuilder ddlBuilder = new SQLiteDdlBuilder();
ddlBuilder.AppendSql(" PRIMARY KEY (");
ddlBuilder.AppendIdentifierList(migrationOperation.Columns);
ddlBuilder.AppendSql(")");
return ddlBuilder.GetCommandText();
}
#endregion
#region Table operations
private string GenerateSqlStatementConcrete(AlterTableOperation migrationOperation)
{
/*
* SQLite does not support alter table
* We should rename old table, create the new table, copy old data to new table and drop old table
*/
throw new NotSupportedException("Alter column not supported by SQLite");
}
private string GenerateSqlStatementConcrete(CreateTableOperation migrationOperation)
{
SQLiteDdlBuilder ddlBuilder = new SQLiteDdlBuilder();
ddlBuilder.AppendSql("CREATE TABLE ");
ddlBuilder.AppendIdentifier(migrationOperation.Name);
ddlBuilder.AppendSql(" (");
ddlBuilder.AppendNewLine();
bool first = true;
string autoincrementColumnName = null;
foreach (ColumnModel column in migrationOperation.Columns)
{
if (first)
first = false;
else
ddlBuilder.AppendSql(",");
ddlBuilder.AppendSql(" ");
ddlBuilder.AppendIdentifier(column.Name);
ddlBuilder.AppendSql(" ");
if (column.IsIdentity)
{
autoincrementColumnName = column.Name;
ddlBuilder.AppendSql(" integer constraint ");
ddlBuilder.AppendIdentifier(ddlBuilder.CreateConstraintName("PK", migrationOperation.Name));
ddlBuilder.AppendSql(" primary key autoincrement");
ddlBuilder.AppendNewLine();
}
else
{
TypeUsage storeTypeUsage = ProviderManifest.GetStoreType(column.TypeUsage);
ddlBuilder.AppendType(storeTypeUsage, column.IsNullable ?? true, column.IsIdentity);
ddlBuilder.AppendNewLine();
}
}
if (migrationOperation.PrimaryKey != null && autoincrementColumnName == null)
{
ddlBuilder.AppendSql(",");
ddlBuilder.AppendSql(GenerateSqlStatementConcrete(migrationOperation.PrimaryKey));
}
ddlBuilder.AppendSql(")");
return ddlBuilder.GetCommandText();
}
#endregion
#region Index
private string GenerateSqlStatementConcrete(CreateIndexOperation migrationOperation)
{
SQLiteDdlBuilder ddlBuilder = new SQLiteDdlBuilder();
ddlBuilder.AppendSql("CREATE ");
if (migrationOperation.IsUnique)
ddlBuilder.AppendSql("UNIQUE ");
ddlBuilder.AppendSql("INDEX ");
ddlBuilder.AppendIdentifier(SQLiteProviderManifestHelper.GetFullIdentifierName(migrationOperation.Table, migrationOperation.Name));
ddlBuilder.AppendSql(" ON ");
ddlBuilder.AppendIdentifier(migrationOperation.Table);
ddlBuilder.AppendSql(" (");
ddlBuilder.AppendIdentifierList(migrationOperation.Columns);
ddlBuilder.AppendSql(")");
return ddlBuilder.GetCommandText();
}
#endregion
#region Drop
private string GenerateSqlStatementConcrete(DropForeignKeyOperation migrationOperation)
{
SQLiteDdlBuilder ddlBuilder = new SQLiteDdlBuilder();
ddlBuilder.AppendSql("ALTER TABLE ");
ddlBuilder.AppendIdentifier(migrationOperation.PrincipalTable);
ddlBuilder.AppendSql(" DROP CONSTRAINT ");
ddlBuilder.AppendIdentifier(migrationOperation.Name);
return ddlBuilder.GetCommandText();
}
private string GenerateSqlStatementConcrete(DropPrimaryKeyOperation migrationOperation)
{
SQLiteDdlBuilder ddlBuilder = new SQLiteDdlBuilder();
ddlBuilder.AppendSql("ALTER TABLE ");
ddlBuilder.AppendIdentifier(migrationOperation.Table);
ddlBuilder.AppendSql(" DROP CONSTRAINT ");
ddlBuilder.AppendIdentifier(migrationOperation.Name);
return ddlBuilder.GetCommandText();
}
private string GenerateSqlStatementConcrete(DropIndexOperation migrationOperation)
{
SQLiteDdlBuilder ddlBuilder = new SQLiteDdlBuilder();
ddlBuilder.AppendSql("DROP INDEX ");
ddlBuilder.AppendIdentifier(SQLiteProviderManifestHelper.GetFullIdentifierName(migrationOperation.Table, migrationOperation.Name));
ddlBuilder.AppendSql(" ON ");
ddlBuilder.AppendIdentifier(migrationOperation.Table);
return ddlBuilder.GetCommandText();
}
private string GenerateSqlStatementConcrete(DropTableOperation migrationOperation)
{
SQLiteDdlBuilder ddlBuilder = new SQLiteDdlBuilder();
ddlBuilder.AppendSql("DROP TABLE ");
ddlBuilder.AppendIdentifier(migrationOperation.Name);
return ddlBuilder.GetCommandText();
}
#endregion
}
}