-
-
Notifications
You must be signed in to change notification settings - Fork 198
/
BaseComandInterpreter.cs
316 lines (266 loc) · 11.7 KB
/
BaseComandInterpreter.cs
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using YesSql.Sql.Schema;
namespace YesSql.Sql
{
public abstract class BaseCommandInterpreter : ICommandInterpreter
{
protected readonly ISqlDialect _dialect;
protected readonly IConfiguration _configuration;
private const char Space = ' ';
public BaseCommandInterpreter(IConfiguration configuration)
{
_dialect = configuration.SqlDialect;
_configuration = configuration;
}
public IEnumerable<string> CreateSql(IEnumerable<ISchemaCommand> commands)
{
var sqlCommands = new List<string>();
foreach (var command in commands)
{
var schemaCommand = command as SchemaCommand;
if (schemaCommand == null)
{
continue;
}
switch (schemaCommand.Type)
{
case SchemaCommandType.CreateTable:
sqlCommands.AddRange(Run((ICreateTableCommand)schemaCommand));
break;
case SchemaCommandType.AlterTable:
sqlCommands.AddRange(Run((IAlterTableCommand)schemaCommand));
break;
case SchemaCommandType.DropTable:
sqlCommands.AddRange(Run((IDropTableCommand)schemaCommand));
break;
case SchemaCommandType.SqlStatement:
sqlCommands.AddRange(Run((ISqlStatementCommand)schemaCommand));
break;
case SchemaCommandType.CreateForeignKey:
sqlCommands.AddRange(Run((ICreateForeignKeyCommand)schemaCommand));
break;
case SchemaCommandType.DropForeignKey:
sqlCommands.AddRange(Run((IDropForeignKeyCommand)schemaCommand));
break;
case SchemaCommandType.CreateSchema:
sqlCommands.AddRange(Run((ICreateSchemaCommand)schemaCommand));
break;
}
}
return sqlCommands;
}
public virtual IEnumerable<string> Run(ICreateTableCommand command)
{
// TODO: Support CreateForeignKeyCommand in a CREATE TABLE (in sqlite they can only be created with the table)
var builder = new StringBuilder();
builder.Append(_dialect.CreateTableString)
.Append(' ')
.Append(_dialect.QuoteForTableName(command.Name, _configuration.Schema))
.Append(" (");
var appendComma = false;
foreach (var createColumn in command.TableCommands.OfType<CreateColumnCommand>())
{
if (appendComma)
{
builder.Append(", ");
}
appendComma = true;
Run(builder, createColumn);
}
// We only create PK statements on columns that don't have IsIdentity since IsIdentity statements also contains the notion of primary key.
var primaryKeys = command.TableCommands.OfType<CreateColumnCommand>().Where(ccc => ccc.IsPrimaryKey && !ccc.IsIdentity).Select(ccc => _dialect.QuoteForColumnName(ccc.ColumnName)).ToArray();
if (primaryKeys.Any())
{
if (appendComma)
{
builder.Append(", ");
}
builder.Append(_dialect.PrimaryKeyString)
.Append(" ( ")
.Append(String.Join(", ", primaryKeys.ToArray()))
.Append(" )");
}
builder.Append(" )");
yield return builder.ToString();
}
public virtual IEnumerable<string> Run(IDropTableCommand command)
{
yield return _dialect.GetDropTableString(command.Name, _configuration.Schema);
}
public virtual IEnumerable<string> Run(ICreateSchemaCommand command)
{
yield return _dialect.GetCreateSchemaString(command.Name);
}
public virtual IEnumerable<string> Run(IAlterTableCommand command)
{
if (command.TableCommands.Count == 0)
{
yield break;
}
// drop columns
foreach (var dropColumn in command.TableCommands.OfType<DropColumnCommand>())
{
var builder = new StringBuilder();
Run(builder, dropColumn);
yield return builder.ToString();
}
// add columns
foreach (var addColumn in command.TableCommands.OfType<AddColumnCommand>())
{
var builder = new StringBuilder();
Run(builder, addColumn);
yield return builder.ToString();
}
// alter columns
foreach (var alterColumn in command.TableCommands.OfType<AlterColumnCommand>())
{
var builder = new StringBuilder();
Run(builder, alterColumn);
yield return builder.ToString();
}
// rename columns
foreach (var renameColumn in command.TableCommands.OfType<RenameColumnCommand>())
{
var builder = new StringBuilder();
Run(builder, renameColumn);
yield return builder.ToString();
}
// add index
foreach (var addIndex in command.TableCommands.OfType<AddIndexCommand>())
{
var builder = new StringBuilder();
Run(builder, addIndex);
yield return builder.ToString();
}
// drop index
foreach (var dropIndex in command.TableCommands.OfType<DropIndexCommand>())
{
var builder = new StringBuilder();
Run(builder, dropIndex);
yield return builder.ToString();
}
}
public virtual void Run(StringBuilder builder, IAddColumnCommand command)
{
builder.AppendFormat("alter table {0} add ",
_dialect.QuoteForTableName(command.Name, _configuration.Schema)
);
Run(builder, (CreateColumnCommand)command);
}
public virtual void Run(StringBuilder builder, IDropColumnCommand command)
{
builder.AppendFormat("alter table {0} drop column {1}",
_dialect.QuoteForTableName(command.Name, _configuration.Schema),
_dialect.QuoteForColumnName(command.ColumnName)
);
}
public virtual void Run(StringBuilder builder, IAlterColumnCommand command)
{
builder.AppendFormat("alter table {0} alter column {1} ",
_dialect.QuoteForTableName(command.Name, _configuration.Schema),
_dialect.QuoteForColumnName(command.ColumnName)
);
var dbType = _dialect.ToDbType(command.DbType);
// type
if (dbType != DbType.Object)
{
builder.Append(_dialect.GetTypeName(dbType, command.Length, command.Precision, command.Scale));
}
else
{
if (command.Length > 0 || command.Precision > 0 || command.Scale > 0)
{
throw new Exception("Error while executing data migration: you need to specify the field's type in order to change its properties");
}
}
// [default value]
if (command.Default != null)
{
builder.Append(" set default ").Append(_dialect.GetSqlValue(command.Default)).Append(Space);
}
}
public virtual void Run(StringBuilder builder, IRenameColumnCommand command)
{
builder.AppendFormat("alter table {0} rename column {1} to {2}",
_dialect.QuoteForTableName(command.Name, _configuration.Schema),
_dialect.QuoteForColumnName(command.ColumnName),
_dialect.QuoteForColumnName(command.NewColumnName)
);
}
public virtual void Run(StringBuilder builder, IAddIndexCommand command)
{
builder.AppendFormat("create index {1} on {0} ({2}) ",
_dialect.QuoteForTableName(command.Name, _configuration.Schema),
_dialect.QuoteForColumnName(command.IndexName),
String.Join(", ", command.ColumnNames.Select(x => _dialect.QuoteForColumnName(x)).ToArray())
);
}
public virtual void Run(StringBuilder builder, IDropIndexCommand command)
{
builder.Append(_dialect.GetDropIndexString(command.IndexName, command.Name, _configuration.Schema));
}
public virtual IEnumerable<string> Run(ISqlStatementCommand command)
{
if (command.Providers.Count != 0)
{
yield break;
}
yield return command.Sql;
}
public virtual IEnumerable<string> Run(ICreateForeignKeyCommand command)
{
var builder = new StringBuilder();
builder.Append("alter table ")
.Append(_dialect.QuoteForTableName(command.SrcTable, _configuration.Schema));
builder.Append(_dialect.GetAddForeignKeyConstraintString(command.Name,
command.SrcColumns.Select(x => _dialect.QuoteForColumnName(x)).ToArray(),
_dialect.QuoteForTableName(command.DestTable, _configuration.Schema),
command.DestColumns.Select(x => _dialect.QuoteForColumnName(x)).ToArray(),
false));
yield return builder.ToString();
}
public virtual IEnumerable<string> Run(IDropForeignKeyCommand command)
{
var builder = new StringBuilder();
builder.Append("alter table ")
.Append(_dialect.QuoteForTableName(command.SrcTable, _configuration.Schema))
.Append(_dialect.GetDropForeignKeyConstraintString(command.Name));
yield return builder.ToString();
}
private void Run(StringBuilder builder, ICreateColumnCommand command)
{
// name
builder.Append(_dialect.QuoteForColumnName(command.ColumnName)).Append(Space);
if (!command.IsIdentity || _dialect.HasDataTypeInIdentityColumn)
{
var dbType = _dialect.ToDbType(command.DbType);
builder.Append(_dialect.GetTypeName(dbType, command.Length, command.Precision, command.Scale));
}
// append identity if handled
if (command.IsIdentity && _dialect.SupportsIdentityColumns)
{
builder.Append(Space).Append(_configuration.IdentityColumnSize == IdentityColumnSize.Int32 ? _dialect.LegacyIdentityColumnString : _dialect.IdentityColumnString);
}
// [default value]
if (command.Default != null)
{
builder.Append(" default ").Append(_dialect.GetSqlValue(command.Default)).Append(Space);
}
// nullable
builder.Append(command.IsNotNull
? " not null"
: !command.IsPrimaryKey && !command.IsUnique
? _dialect.NullColumnString
: string.Empty);
// append unique if handled, otherwise at the end of the satement
if (command.IsUnique && _dialect.SupportsUnique)
{
builder.Append(" unique");
}
}
}
}