/
DefaultFbMigrationSqlGeneratorBehavior.cs
124 lines (118 loc) · 3.93 KB
/
DefaultFbMigrationSqlGeneratorBehavior.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
/*
* Firebird ADO.NET Data provider for .NET and Mono
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.firebirdsql.org/index.php?op=doc&id=idpl
*
* Software distributed under the License is distributed on
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* Copyright (c) 2014,2017 Jiri Cincura (jiri@cincura.net)
* All Rights Reserved.
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FirebirdSql.Data.EntityFramework6.SqlGen;
namespace FirebirdSql.Data.EntityFramework6
{
public class DefaultFbMigrationSqlGeneratorBehavior : IFbMigrationSqlGeneratorBehavior
{
public virtual IEnumerable<string> CreateIdentityForColumn(string columnName, string tableName)
{
var identitySequenceName = CreateIdentitySequenceName(columnName, tableName);
using (var writer = FbMigrationSqlGenerator.SqlWriter())
{
writer.WriteLine("EXECUTE BLOCK");
writer.WriteLine("AS");
writer.WriteLine("BEGIN");
writer.Indent++;
writer.Write("if (not exists(select 1 from rdb$generators where rdb$generator_name = '");
writer.Write(identitySequenceName);
writer.Write("')) then");
writer.WriteLine();
writer.WriteLine("begin");
writer.Indent++;
writer.Write("execute statement 'create sequence ");
writer.Write(identitySequenceName);
writer.Write("';");
writer.WriteLine();
writer.Indent--;
writer.WriteLine("end");
writer.Indent--;
writer.Write("END");
yield return writer.ToString();
}
using (var writer = FbMigrationSqlGenerator.SqlWriter())
{
writer.Write("CREATE OR ALTER TRIGGER ");
writer.Write(FbMigrationSqlGenerator.Quote(CreateTriggerName(columnName, tableName)));
writer.Write(" ACTIVE BEFORE INSERT ON ");
writer.Write(FbMigrationSqlGenerator.Quote(tableName));
writer.WriteLine();
writer.WriteLine("AS");
writer.WriteLine("BEGIN");
writer.Indent++;
writer.Write("if (new.");
writer.Write(FbMigrationSqlGenerator.Quote(columnName));
writer.Write(" is null) then");
writer.WriteLine();
writer.WriteLine("begin");
writer.Indent++;
writer.Write("new.");
writer.Write(FbMigrationSqlGenerator.Quote(columnName));
writer.Write(" = next value for ");
writer.Write(identitySequenceName);
writer.Write(";");
writer.WriteLine();
writer.Indent--;
writer.WriteLine("end");
writer.Indent--;
writer.Write("END");
yield return writer.ToString();
}
}
public virtual IEnumerable<string> DropIdentityForColumn(string columnName, string tableName)
{
var triggerName = CreateTriggerName(columnName, tableName);
using (var writer = FbMigrationSqlGenerator.SqlWriter())
{
writer.WriteLine("EXECUTE BLOCK");
writer.WriteLine("AS");
writer.WriteLine("BEGIN");
writer.Indent++;
writer.Write("if (exists(select 1 from rdb$triggers where rdb$trigger_name = '");
writer.Write(triggerName);
writer.Write("')) then");
writer.WriteLine();
writer.WriteLine("begin");
writer.Indent++;
writer.Write("execute statement 'drop trigger ");
writer.Write(FbMigrationSqlGenerator.Quote(triggerName));
writer.Write("';");
writer.WriteLine();
writer.Indent--;
writer.WriteLine("end");
writer.Indent--;
writer.Write("END");
yield return writer.ToString();
}
}
protected virtual string CreateTriggerName(string columnName, string tableName)
{
return string.Format("ID_{0}_{1}", tableName, columnName);
}
protected virtual string CreateIdentitySequenceName(string columnName, string tableName)
{
return "GEN_IDENTITY";
}
}
}