|
| 1 | +using Chloe.DDL; |
| 2 | +using Chloe.Descriptors; |
| 3 | +using Chloe.Reflection; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Xml.Linq; |
| 9 | + |
| 10 | +namespace Chloe.MySql.DDL |
| 11 | +{ |
| 12 | + public class MySqlTableGenerator : TableGenerator |
| 13 | + { |
| 14 | + public MySqlTableGenerator(IDbContext dbContext) : base(dbContext) |
| 15 | + { |
| 16 | + |
| 17 | + } |
| 18 | + |
| 19 | + public override List<string> GenCreateTableScript(TypeDescriptor typeDescriptor, TableCreateMode createMode = TableCreateMode.CreateIfNotExists) |
| 20 | + { |
| 21 | + string tableName = typeDescriptor.Table.Name; |
| 22 | + |
| 23 | + StringBuilder sb = new StringBuilder(); |
| 24 | + |
| 25 | + if (createMode == TableCreateMode.CreateIfNotExists) |
| 26 | + { |
| 27 | + sb.Append($"CREATE TABLE IF NOT EXISTS {Utils.QuoteName(tableName)}("); |
| 28 | + } |
| 29 | + else if (createMode == TableCreateMode.CreateNew) |
| 30 | + { |
| 31 | + sb.AppendLine($"DROP TABLE IF EXISTS {Utils.QuoteName(tableName)};"); |
| 32 | + sb.Append($"CREATE TABLE {Utils.QuoteName(tableName)}("); |
| 33 | + } |
| 34 | + else |
| 35 | + { |
| 36 | + sb.Append($"CREATE TABLE {Utils.QuoteName(tableName)}("); |
| 37 | + } |
| 38 | + |
| 39 | + XDocument commentDoc = GetAssemblyCommentDoc(typeDescriptor.Definition.Type.Assembly); |
| 40 | + |
| 41 | + string c = ""; |
| 42 | + foreach (var propertyDescriptor in typeDescriptor.PrimitivePropertyDescriptors.OrderBy(a => GetTypeInheritLayer(a.Property.DeclaringType))) |
| 43 | + { |
| 44 | + sb.AppendLine(c); |
| 45 | + sb.Append($" {BuildColumnPart(propertyDescriptor, commentDoc)}"); |
| 46 | + c = ","; |
| 47 | + } |
| 48 | + |
| 49 | + if (typeDescriptor.PrimaryKeys.Count > 0) |
| 50 | + { |
| 51 | + string key = typeDescriptor.PrimaryKeys.First().Column.Name; |
| 52 | + sb.AppendLine(c); |
| 53 | + sb.Append($" PRIMARY KEY ({Utils.QuoteName(key)}) USING BTREE"); |
| 54 | + } |
| 55 | + |
| 56 | + sb.AppendLine(); |
| 57 | + sb.Append(");"); |
| 58 | + |
| 59 | + return new List<string>() { sb.ToString() }; |
| 60 | + } |
| 61 | + |
| 62 | + string BuildColumnPart(PrimitivePropertyDescriptor propertyDescriptor, XDocument commentDoc) |
| 63 | + { |
| 64 | + string part = $"{Utils.QuoteName(propertyDescriptor.Column.Name)} { GetDataTypeName(propertyDescriptor)}"; |
| 65 | + |
| 66 | + if (propertyDescriptor.IsAutoIncrement) |
| 67 | + { |
| 68 | + part += " AUTO_INCREMENT"; |
| 69 | + } |
| 70 | + |
| 71 | + if (!propertyDescriptor.IsNullable) |
| 72 | + { |
| 73 | + part += " NOT NULL"; |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + part += " NULL"; |
| 78 | + } |
| 79 | + |
| 80 | + string comment = FindComment(propertyDescriptor, commentDoc); |
| 81 | + if (!string.IsNullOrEmpty(comment)) |
| 82 | + part += $" COMMENT '{comment}'"; |
| 83 | + |
| 84 | + return part; |
| 85 | + } |
| 86 | + static string GetDataTypeName(PrimitivePropertyDescriptor propertyDescriptor) |
| 87 | + { |
| 88 | + if (propertyDescriptor.TryGetAnnotation(typeof(DataTypeAttribute), out var annotation)) |
| 89 | + { |
| 90 | + return (annotation as DataTypeAttribute).Name; |
| 91 | + } |
| 92 | + |
| 93 | + Type type = propertyDescriptor.PropertyType.GetUnderlyingType(); |
| 94 | + if (type.IsEnum) |
| 95 | + { |
| 96 | + type = type.GetEnumUnderlyingType(); |
| 97 | + } |
| 98 | + |
| 99 | + if (type == typeof(string)) |
| 100 | + { |
| 101 | + int stringLength = propertyDescriptor.Column.Size ?? 4000; |
| 102 | + return $"varchar({stringLength})"; |
| 103 | + } |
| 104 | + |
| 105 | + if (type == typeof(int)) |
| 106 | + { |
| 107 | + return "int(11)"; |
| 108 | + } |
| 109 | + |
| 110 | + if (type == typeof(byte)) |
| 111 | + { |
| 112 | + return "int(11)"; |
| 113 | + } |
| 114 | + |
| 115 | + if (type == typeof(Int16)) |
| 116 | + { |
| 117 | + return "int(11)"; |
| 118 | + } |
| 119 | + |
| 120 | + if (type == typeof(long)) |
| 121 | + { |
| 122 | + return "bigint"; |
| 123 | + } |
| 124 | + |
| 125 | + if (type == typeof(float)) |
| 126 | + { |
| 127 | + return "float(10, 4)"; |
| 128 | + } |
| 129 | + |
| 130 | + if (type == typeof(double)) |
| 131 | + { |
| 132 | + return "double(10, 4)"; |
| 133 | + } |
| 134 | + |
| 135 | + if (type == typeof(decimal)) |
| 136 | + { |
| 137 | + return "decimal(10, 4)"; |
| 138 | + } |
| 139 | + |
| 140 | + if (type == typeof(bool)) |
| 141 | + { |
| 142 | + return "int(11)"; |
| 143 | + } |
| 144 | + |
| 145 | + if (type == typeof(DateTime)) |
| 146 | + { |
| 147 | + return "datetime(0)"; |
| 148 | + } |
| 149 | + |
| 150 | + if (type == typeof(Guid)) |
| 151 | + { |
| 152 | + return "varchar(50)"; |
| 153 | + } |
| 154 | + |
| 155 | + throw new NotSupportedException(type.FullName); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments