private string GetCreateTableSQL(MTableDefine tb) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("/*建立数表{0}*/", tb.TableName).AppendLine(); sb.AppendFormat("CREATE TABLE {0}", tb.TableName).AppendLine(); sb.Append("("); // 字段 foreach (var field in tb.FieldList) { if (field.FieldName.ToUpper() == "MODIFYTIME" && field.DefaultValue == "CURRENT_TIMESTAMP") { sb.AppendFormat("{0} {1} NOT NULL DEFAULT {2} ON UPDATE {2} COMMENT '{3}',", field.FieldName, field.DataType, field.DefaultValue, field.FieldNameCH + ";" + field.ValueConstraint).AppendLine(); } else if (field.DataType.ToUpper() == "DATETIME") { sb.AppendFormat("{0} {1} NOT NULL DEFAULT {2} COMMENT '{3}',", field.FieldName, field.DataType, field.DefaultValue, field.FieldNameCH + ";" + field.ValueConstraint).AppendLine(); } else { if (field.Length == 0) { // 没有长度 sb.AppendFormat("{0} {1} NOT NULL DEFAULT '{2}' COMMENT '{3}',", field.FieldName, field.DataType, field.DefaultValue, field.FieldNameCH + ";" + field.ValueConstraint).AppendLine(); } else { sb.AppendFormat("{0} {1}({2}) NOT NULL DEFAULT '{3}' COMMENT '{4}',", field.FieldName, field.DataType, field.Length, field.DefaultValue, field.FieldNameCH + ";" + field.ValueConstraint).AppendLine(); } } } var primarkeys = (from p in tb.FieldList where p.PrimaryKeyIndex > 0 orderby p.PrimaryKeyIndex ascending select p.FieldName); string primaryKeyStr = string.Join(",", primarkeys.ToArray()); // 主键 sb.AppendFormat("PRIMARY KEY({0}),", primaryKeyStr).AppendLine(); // 唯一索引 if (tb.FieldList.Exists(sa => sa.IsUniqueIndex == true)) { var uniqueIndex = (from sa in tb.FieldList where sa.IsUniqueIndex select sa.FieldName); sb.AppendFormat("UNIQUE INDEX {0}_{1} ({1}),", tb.TableName, uniqueIndex.ToArray()[0]).AppendLine(); } // 其他索引列,如果有的话 if (tb.FieldList.Exists(sa => sa.IndexNo > 0)) { var indexNoList = (from sa in tb.FieldList where sa.IndexNo > 0 orderby sa.IndexNo ascending select sa.FieldName); string indexNoStr = string.Join(",", indexNoList.ToArray()); string indexNoListName = string.Join("_", indexNoList.ToArray()); sb.AppendFormat("INDEX {0}_{1} ({2}),", tb.TableName, indexNoListName, indexNoStr).AppendLine(); } // 去掉最后一个逗号 sb = new StringBuilder(sb.ToString().Trim()); sb = sb.Remove(sb.Length - 1, 1); sb.AppendFormat(")ENGINE=InnoDB default charset=utf8 COMMENT='{0}';", tb.TableNameCH); return sb.ToString(); }
public string GeneratorModel(MTableDefine table, string namespaceStr) { StringBuilder sb = new StringBuilder(); return sb.ToString(); }