|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Data; |
| 4 | +using System.Text; |
| 5 | +using FluentMigrator.Builders.Insert; |
| 6 | +using FluentMigrator.Expressions; |
| 7 | +using FluentMigrator.Model; |
| 8 | + |
| 9 | +namespace FluentMigrator.Runner.Generators |
| 10 | +{ |
| 11 | + public class MySqlGenerator : GeneratorBase |
| 12 | + { |
| 13 | + public const int AnsiTinyStringCapacity = 127; |
| 14 | + public const int StringCapacity = 255; |
| 15 | + public const int TextCapacity = 65535; |
| 16 | + public const int MediumTextCapacity = 16777215; |
| 17 | + public const int DecimalCapacity = 19; |
| 18 | + |
| 19 | + protected override void SetupTypeMaps() |
| 20 | + { |
| 21 | + SetTypeMap(DbType.AnsiStringFixedLength, "CHAR(255)"); |
| 22 | + SetTypeMap(DbType.AnsiStringFixedLength, "CHAR($size)", StringCapacity); |
| 23 | + SetTypeMap(DbType.AnsiStringFixedLength, "TEXT", TextCapacity); |
| 24 | + SetTypeMap(DbType.AnsiStringFixedLength, "MEDIUMTEXT", MediumTextCapacity); |
| 25 | + SetTypeMap(DbType.AnsiString, "VARCHAR(255)"); |
| 26 | + SetTypeMap(DbType.AnsiString, "VARCHAR($size)", StringCapacity); |
| 27 | + SetTypeMap(DbType.AnsiString, "TEXT", TextCapacity); |
| 28 | + SetTypeMap(DbType.AnsiString, "MEDIUMTEXT", MediumTextCapacity); |
| 29 | + SetTypeMap(DbType.Binary, "LONGBLOB"); |
| 30 | + SetTypeMap(DbType.Binary, "TINYBLOB", AnsiTinyStringCapacity); |
| 31 | + SetTypeMap(DbType.Binary, "BLOB", TextCapacity); |
| 32 | + SetTypeMap(DbType.Binary, "MEDIUMBLOB", MediumTextCapacity); |
| 33 | + SetTypeMap(DbType.Boolean, "TINYINT(1)"); |
| 34 | + SetTypeMap(DbType.Byte, "TINYINT UNSIGNED"); |
| 35 | + SetTypeMap(DbType.Currency, "MONEY"); |
| 36 | + SetTypeMap(DbType.Date, "DATE"); |
| 37 | + SetTypeMap(DbType.DateTime, "DATETIME"); |
| 38 | + SetTypeMap(DbType.Decimal, "DECIMAL(19,5)"); |
| 39 | + SetTypeMap(DbType.Decimal, "DECIMAL($size,$precision)", DecimalCapacity); |
| 40 | + SetTypeMap(DbType.Double, "DOUBLE"); |
| 41 | + SetTypeMap(DbType.Guid, "VARCHAR(40)"); |
| 42 | + SetTypeMap(DbType.Int16, "SMALLINT"); |
| 43 | + SetTypeMap(DbType.Int32, "INTEGER"); |
| 44 | + SetTypeMap(DbType.Int64, "BIGINT"); |
| 45 | + SetTypeMap(DbType.Single, "FLOAT"); |
| 46 | + SetTypeMap(DbType.StringFixedLength, "CHAR(255)"); |
| 47 | + SetTypeMap(DbType.StringFixedLength, "CHAR($size)", StringCapacity); |
| 48 | + SetTypeMap(DbType.StringFixedLength, "TEXT", TextCapacity); |
| 49 | + SetTypeMap(DbType.StringFixedLength, "MEDIUMTEXT", MediumTextCapacity); |
| 50 | + SetTypeMap(DbType.String, "VARCHAR(255)"); |
| 51 | + SetTypeMap(DbType.String, "VARCHAR($size)", StringCapacity); |
| 52 | + SetTypeMap(DbType.String, "TEXT", TextCapacity); |
| 53 | + SetTypeMap(DbType.String, "MEDIUMTEXT", MediumTextCapacity); |
| 54 | + SetTypeMap(DbType.Time, "DATETIME"); |
| 55 | + } |
| 56 | + |
| 57 | + public override string Generate(CreateTableExpression expression) |
| 58 | + { |
| 59 | + return FormatExpression("CREATE TABLE {0} ({1}) ENGINE = INNODB", expression.TableName, GetColumnDDL(expression.Columns)); |
| 60 | + } |
| 61 | + |
| 62 | + public override string Generate(CreateColumnExpression expression) |
| 63 | + { |
| 64 | + |
| 65 | + return FormatExpression("ALTER TABLE {0} ADD {1}", expression.TableName, GenerateDDLForColumn(expression.Column)); |
| 66 | + } |
| 67 | + |
| 68 | + public override string Generate(DeleteTableExpression expression) |
| 69 | + { |
| 70 | + return FormatExpression("DROP TABLE {0}", expression.TableName); |
| 71 | + } |
| 72 | + |
| 73 | + public override string Generate(DeleteColumnExpression expression) |
| 74 | + { |
| 75 | + return FormatExpression("ALTER TABLE {0} DROP COLUMN {1}", expression.TableName, expression.ColumnName); |
| 76 | + } |
| 77 | + |
| 78 | + public override string Generate(CreateForeignKeyExpression expression) |
| 79 | + { |
| 80 | + string primaryColumns = GetColumnList(expression.ForeignKey.PrimaryColumns); |
| 81 | + string foreignColumns = GetColumnList(expression.ForeignKey.ForeignColumns); |
| 82 | + |
| 83 | + string sql = "ALTER TABLE {0} ADD CONSTRAINT {1} FOREIGN KEY ({2}) REFERENCES {3} ({4})"; |
| 84 | + |
| 85 | + return String.Format(sql, |
| 86 | + expression.ForeignKey.ForeignTable, |
| 87 | + expression.ForeignKey.Name, |
| 88 | + foreignColumns, |
| 89 | + expression.ForeignKey.PrimaryTable, |
| 90 | + primaryColumns |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + public override string Generate(DeleteForeignKeyExpression expression) |
| 95 | + { |
| 96 | + string sql = "ALTER TABLE {0} DROP FOREIGN KEY `{1}`"; |
| 97 | + return String.Format(sql, expression.ForeignKey.PrimaryTable, expression.ForeignKey.Name); |
| 98 | + } |
| 99 | + |
| 100 | + public override string Generate(CreateIndexExpression expression) |
| 101 | + { |
| 102 | + var result = new StringBuilder("CREATE"); |
| 103 | + if (expression.Index.IsUnique) |
| 104 | + result.Append(" UNIQUE"); |
| 105 | + |
| 106 | + result.Append(" INDEX {0} ON {1} ("); |
| 107 | + |
| 108 | + bool first = true; |
| 109 | + foreach (IndexColumnDefinition column in expression.Index.Columns) |
| 110 | + { |
| 111 | + if (first) |
| 112 | + first = false; |
| 113 | + else |
| 114 | + result.Append(","); |
| 115 | + |
| 116 | + result.Append(column.Name); |
| 117 | + if (column.Direction == Direction.Ascending) |
| 118 | + { |
| 119 | + result.Append(" ASC"); |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + result.Append(" DESC"); |
| 124 | + } |
| 125 | + } |
| 126 | + result.Append(")"); |
| 127 | + |
| 128 | + return FormatExpression(result.ToString(), expression.Index.Name, expression.Index.TableName); |
| 129 | + } |
| 130 | + |
| 131 | + public override string Generate(DeleteIndexExpression expression) |
| 132 | + { |
| 133 | + return FormatExpression("DROP INDEX {0}", expression.Index.Name, expression.Index.TableName); |
| 134 | + } |
| 135 | + |
| 136 | + public override string Generate(RenameTableExpression expression) |
| 137 | + { |
| 138 | + return FormatExpression("RENAME TABLE {0} TO {1}", expression.OldName, expression.NewName); |
| 139 | + } |
| 140 | + |
| 141 | + public override string Generate(RenameColumnExpression expression) |
| 142 | + { |
| 143 | + return FormatExpression("ALTER TABLE {0} CHANGE COLUMN {1} {2} definition", expression.TableName, expression.OldName, expression.NewName); |
| 144 | + } |
| 145 | + |
| 146 | + public override string Generate(InsertDataExpression expression) |
| 147 | + { |
| 148 | + var result = new StringBuilder(); |
| 149 | + foreach (InsertionData row in expression.Rows) |
| 150 | + { |
| 151 | + List<string> columnNames = new List<string>(); |
| 152 | + List<object> columnData = new List<object>(); |
| 153 | + foreach (KeyValuePair<string, object> item in row) |
| 154 | + { |
| 155 | + columnNames.Add(item.Key); |
| 156 | + columnData.Add(item.Value); |
| 157 | + } |
| 158 | + |
| 159 | + string columns = GetColumnList(columnNames); |
| 160 | + string data = GetDataList(columnData); |
| 161 | + result.Append(FormatExpression("INSERT INTO [{0}] ({1}) VALUES ({2});", expression.TableName, columns, data)); |
| 162 | + } |
| 163 | + return result.ToString(); |
| 164 | + } |
| 165 | + |
| 166 | + public string FormatExpression(string template, params object[] args) |
| 167 | + { |
| 168 | + return String.Format(template, args); |
| 169 | + } |
| 170 | + |
| 171 | + private string GetColumnList(IEnumerable<string> columns) |
| 172 | + { |
| 173 | + string result = ""; |
| 174 | + foreach (string column in columns) |
| 175 | + { |
| 176 | + result += column + ","; |
| 177 | + } |
| 178 | + return result.TrimEnd(','); |
| 179 | + } |
| 180 | + |
| 181 | + private string GetDataList(List<object> data) |
| 182 | + { |
| 183 | + string result = ""; |
| 184 | + foreach (object column in data) |
| 185 | + { |
| 186 | + result += GetConstantValue(column) + ","; |
| 187 | + } |
| 188 | + return result.TrimEnd(','); |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments