forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSDLToPgSQL.tt
More file actions
170 lines (159 loc) · 5.1 KB
/
SSDLToPgSQL.tt
File metadata and controls
170 lines (159 loc) · 5.1 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
<#
//---------------------------------------------------------------------
// This T4 template generates PostgreSQL queries from an instance of
// System.Data.Metadata.Edm.StoreItemCollection, an object representation
// of the SSDL. This query set is compatible with PostgreSQL x.x and higher.
//---------------------------------------------------------------------
// Inherited from:
// SSDLToFB.tt at http://sourceforge.net/p/firebird/NETProvider/ci/cb43606e32048b8c5175013ad4f296fc598c1844/tree/NETProvider/source/FirebirdSql/Data/Entity/SSDLToFB.tt
// History:
// SSDLToPgSQL.tt v0.1 2015/01/26 created by kenji uno
#>
<#@ template language="C#" #>
<#@ output extension = ".sql" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Data.Entity" #>
<#@ assembly name="System.Xml" #>
<#@ import namespace="System.Data.Metadata.Edm" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Runtime.Remoting.Messaging" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Collections.Generic" #>
-- Created: <#=DateTime.Now.ToString()#>
<#
if (StoreItems == null)
{
#>
-- No input.
<#
}
else
{
#>
-- Tables
<#
IDictionary<string, string> additionalColumnComments = new Dictionary<string, string>();
foreach (EntitySet entitySet in StoreItems.GetItems<EntityContainer>()[0].BaseEntitySets.OfType<EntitySet>())
{
additionalColumnComments.Clear();
#>
CREATE TABLE <#=Quote(SchemaName(entitySet))#>.<#=Quote(TableName(entitySet))#> (
<#
PushIndent("\t");
foreach (EdmProperty property in entitySet.ElementType.Properties)
{
#>
<#=GenerateColumn(property, ref additionalColumnComments)#>,
<#
}
PopIndent();
#>
CONSTRAINT <#=Quote("PK_" + TableName(entitySet))#> PRIMARY KEY (<#=string.Join(", ", entitySet.ElementType.KeyMembers.Select(pk => Quote(ColumnName(pk))).ToArray())#>)
);
<#
}
#>
-- Foreign Key Constraints
<#
foreach (AssociationSet associationSet in StoreItems.GetItems<EntityContainer>()[0].BaseEntitySets.OfType<AssociationSet>())
{
ReferentialConstraint constraint = associationSet.ElementType.ReferentialConstraints.Single<ReferentialConstraint>();
AssociationSetEnd end = associationSet.AssociationSetEnds[constraint.FromRole.Name];
AssociationSetEnd end2 = associationSet.AssociationSetEnds[constraint.ToRole.Name];
#>
ALTER TABLE <#=Quote(SchemaName(end2.EntitySet))#>.<#=Quote(TableName(end2.EntitySet))#> ADD CONSTRAINT <#=Quote("FK_" + AssociationSetName(associationSet))#> FOREIGN KEY (<#=string.Join(", ", constraint.ToProperties.Select(fk => Quote(ColumnName(fk))).ToArray())#>)
REFERENCES <#=Quote(SchemaName(end.EntitySet))#>.<#=Quote(TableName(end.EntitySet))#>(<#=string.Join(", ", constraint.FromProperties.Select(pk => Quote(ColumnName(pk))).ToArray())#>)
ON DELETE <#=(end.CorrespondingAssociationEndMember.DeleteBehavior == OperationAction.Cascade ? "CASCADE" : "NO ACTION")#>
;
<#
}
#>
<#
}
#>
-- EOF
<#+
public StoreItemCollection StoreItemCollection { private get; set; }
private StoreItemCollection _storeItems;
private StoreItemCollection StoreItems
{
get
{
if (_storeItems == null)
{
var ssdl = this.GetInput<string>("Ssdl");
if (StoreItemCollection != null)
{
_storeItems = StoreItemCollection;
}
else if (ssdl != null)
{
using (TextReader text = new StringReader(ssdl))
{
using (XmlReader xml = XmlReader.Create(text))
{
_storeItems = new StoreItemCollection(new[] { xml });
}
}
}
}
return _storeItems;
}
}
private string Quote(string s)
{
return "\"" + s + "\"";
}
private string GenerateColumn(EdmProperty property, ref IDictionary<string, string> columnComments)
{
StringBuilder result = new StringBuilder();
result.Append(Quote(ColumnName(property)));
result.Append(" ");
switch (property.TypeUsage.EdmType.Name)
{
case "varchar":
case "char":
result.Append(property.TypeUsage.EdmType.Name.ToUpperInvariant());
result.AppendFormat("({0})", property.TypeUsage.Facets["MaxLength"].Value);
break;
case "decimal":
case "numeric":
result.Append(property.TypeUsage.EdmType.Name.ToUpperInvariant());
result.AppendFormat("({0},{1})", property.TypeUsage.Facets["Precision"].Value, property.TypeUsage.Facets["Scale"].Value);
break;
case "double":
result.Append("DOUBLE PRECISION");
break;
default:
result.Append(property.TypeUsage.EdmType.Name.ToUpperInvariant());
break;
}
if (!property.Nullable)
{
result.Append(" NOT NULL");
}
return result.ToString();
}
private string ColumnName(EdmMember member)
{
return (string)member.MetadataProperties["Name"].Value;
}
private string TableName(EntitySet entitySet)
{
return (string)entitySet.MetadataProperties["Table"].Value ?? (string)entitySet.MetadataProperties["Name"].Value;
}
private string SchemaName(EntitySet entitySet)
{
return (string)entitySet.MetadataProperties["Schema"].Value;
}
private string AssociationSetName(AssociationSet associationSet)
{
return (string)associationSet.MetadataProperties["Name"].Value;
}
private T GetInput<T>(string identity) where T : class
{
return CallContext.GetData(identity) as T;
}
#>