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
|
<#
//---------------------------------------------------------------------
// <copyright file="SsdlToMySql.tt" company="Oracle">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//---------------------------------------------------------------------
// This T4 template generates MySQL from an instance of
// System.Data.Metadata.Edm.StoreItemCollection, an object representation
// of the SSDL. This MySQL is compatible with MySQL 4.1 and higher.
//---------------------------------------------------------------------
// Note: We will resolve all paths in assembly directives at runtime, taking
// macros into account (e.g. $(DevEnvDir), $(ProjectDir), etc.)
#>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Data.Entity" #>
<#@ assembly name="System.Data.Entity.Design" #>
<#@ assembly name="$(DevEnvDir)Microsoft.Data.Entity.Design.DatabaseGeneration.dll"#>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Data.Entity.Design" #>
<#@ import namespace="System.Data.Metadata.Edm" #>
<#@ import namespace="Microsoft.Data.Entity.Design.DatabaseGeneration" #>
<#@ import namespace="System.Runtime.Remoting.Messaging" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ template language="C#" debug="true" hostspecific="true" #>
<#@ include file="GenerateTSQL.Utility.ttinclude"#>
<#@ include file="GenerateMySQL.Utility.ttinclude"#>
<#@ output extension = ".mysql" #>
<#
// +++++++++++++++++++++++++++++++++++++++++++++++++
// Setup for the template (initializing variables, etc.)
// +++++++++++++++++++++++++++++++++++++++++++++++++
string databaseName = this.GetInput<string>(EdmParameterBag.ParameterName.DatabaseName.ToString());
string edmxPath = this.GetInput<string>(EdmParameterBag.ParameterName.EdmxPath.ToString());
Version targetVersion = this.GetInput<Version>(EdmParameterBag.ParameterName.TargetVersion.ToString());
if (false == InitializeAndValidateExistingStore())
{
#>
-- Warning: There were errors validating the existing SSDL. Drop statements
-- will not be generated.
<#
}
#>
-- -----------------------------------------------------------
-- Entity Designer DDL Script for MySQL Server 4.1 and higher
-- -----------------------------------------------------------
-- Date Created: <#=DateTime.Now#>
<#
if (!String.IsNullOrEmpty(edmxPath))
{
#>
-- Generated from EDMX file: <#=MyId(edmxPath)#>
-- Target version: <#=MyId(targetVersion.ToString())#>
<#
}
#>
-- --------------------------------------------------
<# if (!String.IsNullOrEmpty(databaseName))
{
#>
DROP DATABASE IF EXISTS `<#=MyId(databaseName)#>`;
CREATE DATABASE `<#=MyId(databaseName)#>`;
USE `<#=MyId(databaseName)#>`;
<#
}
#>
-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- NOTE: if the constraint does not exist, an ignorable error will be reported.
-- --------------------------------------------------
<#
foreach (AssociationSet associationSet in ExistingStore.GetAllAssociationSets())
{
ReferentialConstraint constraint = associationSet.ElementType.ReferentialConstraints.Single();
string constraintName = MyId(WriteFKConstraintName(constraint));
AssociationSetEnd dependentSetEnd = associationSet.AssociationSetEnds.Where(ase => ase.CorrespondingAssociationEndMember == constraint.ToRole).Single();
string dependentTableName = MyId(dependentSetEnd.EntitySet.GetTableName());
#>
-- ALTER TABLE `<#=dependentTableName#>` DROP CONSTRAINT `<#=constraintName#>`;
<#
}
#>
-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------
SET foreign_key_checks = 0;
<#
foreach (EntitySet entitySet in ExistingStore.GetAllEntitySets())
{
string tableName = MyId(entitySet.GetTableName());
#>
DROP TABLE IF EXISTS `<#=tableName#>`;
<#
}
#>
SET foreign_key_checks = 1;
-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------
<#
foreach (EntitySet entitySet in Store.GetAllEntitySets())
{
string tableName = MyId(entitySet.GetTableName());
#>
-- Creating table '<#=tableName#>'
CREATE TABLE `<#=tableName#>` (
<#
for (int p = 0; p < entitySet.ElementType.Properties.Count; p++)
{
EdmProperty prop = entitySet.ElementType.Properties[p];
#>
`<#=MyId(prop.Name)#>` <#=WriteMySqlType(prop)#> <#=WriteMySqlIdentity(prop, targetVersion)#> <#=WriteNullable(prop.Nullable)#><#=(p < entitySet.ElementType.Properties.Count - 1) ? "," : ""#>
<#
}
#>
);
<#
}
#>
-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------
<#
foreach (EntitySet entitySet in Store.GetAllEntitySets())
{
string tableName = MyId(entitySet.GetTableName());
bool hasAutoColumn = false;
foreach (EdmProperty keyProperty in entitySet.ElementType.GetKeyProperties())
{
if (IsIdentity(keyProperty, targetVersion))
{
hasAutoColumn = true;
break;
}
}
if (hasAutoColumn) continue;
#>
-- Creating primary key on <#=WriteMySqlColumns(entitySet.ElementType.GetKeyProperties(), ',')#> in table '<#=tableName#>'
ALTER TABLE `<#=tableName#>`
ADD CONSTRAINT `PK_<#=tableName#>`
PRIMARY KEY (<#=WriteMySqlColumns(entitySet.ElementType.GetKeyProperties(), ',')#> );
<#
}
#>
-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------
<#
foreach (AssociationSet associationSet in Store.GetAllAssociationSets())
{
ReferentialConstraint constraint = associationSet.ElementType.ReferentialConstraints.Single();
AssociationSetEnd dependentSetEnd = associationSet.AssociationSetEnds.Where(ase => ase.CorrespondingAssociationEndMember == constraint.ToRole).Single();
AssociationSetEnd principalSetEnd = associationSet.AssociationSetEnds.Where(ase => ase.CorrespondingAssociationEndMember == constraint.FromRole).Single();
string dependentTableName = MyId(dependentSetEnd.EntitySet.GetTableName());
string principalTableName = MyId(principalSetEnd.EntitySet.GetTableName());
#>
-- Creating foreign key on <#=WriteMySqlColumns(constraint.ToProperties, ',')#> in table '<#=dependentTableName#>'
ALTER TABLE `<#=dependentTableName#>`
ADD CONSTRAINT `<#=WriteFKConstraintName(constraint)#>`
FOREIGN KEY (<#=WriteMySqlColumns(constraint.ToProperties, ',')#>)
REFERENCES `<#=principalTableName#>`
(<#=WriteMySqlColumns(constraint.FromProperties, ',')#>)
ON DELETE <#=GetDeleteAction(constraint)#> ON UPDATE NO ACTION;
<#
// if the foreign keys are part of the primary key on the dependent end, then we should not add a constraint.
if (!dependentSetEnd.EntitySet.ElementType.GetKeyProperties().Take(constraint.ToProperties.Count()).OrderBy(r => r.Name).SequenceEqual(constraint.ToProperties.OrderBy(r => r.Name)))
{
#>
-- Creating non-clustered index for FOREIGN KEY '<#=WriteFKConstraintName(constraint)#>'
CREATE INDEX `IX_<#=WriteFKConstraintName(constraint)#>`
ON `<#=dependentTableName#>`
(<#=WriteMySqlColumns(constraint.ToProperties, ',')#>);
<#
}
#>
<#
}
#>
-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------
|