private string[] CreateTableMembers() { List <string> results = new List <string>(); ClusterAttribute clusterAttribute = GetClusterAttribute(); results.AddRange(CreateTableColumns()); results.Add(CreateTablePrimaryKey(clusterAttribute)); results.AddRange(CreateTableUniqueConstraints(clusterAttribute)); return(results.ToArray()); }
public override IEnumerable <string> SqlCommands(IDbConnection connection) { foreach (var cmd in base.SqlCommands(connection)) { yield return(cmd); } var modelType = _object.ModelType; var ct = new CreateTable(modelType); ClusterAttribute clusterAttribute = modelType.GetCustomAttribute <ClusterAttribute>() ?? new ClusterAttribute(ClusterOption.PrimaryKey); yield return($"ALTER TABLE [{_object.Schema}].[{_object.Name}] ADD {ct.CreateTablePrimaryKey(clusterAttribute)}"); }
public override string[] CreateTableMembers(Type type, IEnumerable <string> addedColumns, IEnumerable <string> modifiedColumns, IEnumerable <string> deletedColumns, bool withForeignKeys = false) { List <string> results = new List <string>(); ClusterAttribute clusterAttribute = GetClusterAttribute(type); results.AddRange(CreateTableColumns(type, addedColumns, modifiedColumns, deletedColumns)); results.Add(CreateTablePrimaryKey(type, clusterAttribute)); results.AddRange(CreateTableUniqueConstraints(type, clusterAttribute)); if (withForeignKeys) { var foreignKeys = type.GetForeignKeys(); results.AddRange(foreignKeys.Select(pi => ForeignKeyConstraintSyntax(pi).RemoveAll("\r\n", "\t"))); } return(results.ToArray()); }
private IEnumerable <string> CreateTableUniqueConstraints(Type type, ClusterAttribute clusterAttribute) { List <string> results = new List <string>(); if (PrimaryKeyColumns(type, markedOnly: true).Any()) { results.Add($"CONSTRAINT [U_{GetConstraintBaseName(type)}_Id] UNIQUE {clusterAttribute.Syntax(ClusterOption.Identity)}([Id])"); } results.AddRange(type.GetProperties().Where(pi => pi.HasAttribute <UniqueKeyAttribute>()).Select(pi => { UniqueKeyAttribute attr = pi.GetCustomAttribute <UniqueKeyAttribute>(); return($"CONSTRAINT [U_{GetConstraintBaseName(type)}_{pi.SqlColumnName()}] UNIQUE {attr.GetClusteredSyntax()}([{pi.SqlColumnName()}])"); })); results.AddRange(type.GetCustomAttributes <UniqueKeyAttribute>().Select((u, i) => { string constrainName = (string.IsNullOrEmpty(u.ConstraintName)) ? $"U_{GetConstraintBaseName(type)}_{i}" : u.ConstraintName; return($"CONSTRAINT [{constrainName}] UNIQUE {u.GetClusteredSyntax()}({string.Join(", ", u.ColumnNames.Select(col => $"[{col}]"))})"); })); return(results); }
private string CreateTablePrimaryKey(Type type, ClusterAttribute clusterAttribute) { return($"CONSTRAINT [PK_{GetConstraintBaseName(type)}] PRIMARY KEY {clusterAttribute.Syntax(ClusterOption.PrimaryKey)}({string.Join(", ", PrimaryKeyColumns(type).Select(col => $"[{col}]"))})"); }
internal string CreateTablePrimaryKey(ClusterAttribute clusterAttribute) { return($"CONSTRAINT [PK_{DbObject.ConstraintName(_modelType)}] PRIMARY KEY {clusterAttribute.Syntax(ClusterOption.PrimaryKey)}({string.Join(", ", PrimaryKeyColumns().Select(col => $"[{col}]"))})"); }
public override string PrimaryKeyAddStatement(TableInfo affectedTable) { ClusterAttribute clusterAttribute = GetClusterAttribute(affectedTable.ModelType); return($"ALTER TABLE [{affectedTable.Schema}].[{affectedTable.Name}] ADD CONSTRAINT {CreateTablePrimaryKey(affectedTable.ModelType, clusterAttribute)}"); }