public string GetDdl() { var colums = Colums.Select(x => { var sql = $"{x.ColumName} {x.ColumType}"; if (x.KeyKind == ConstraintsKind.Primary && Indexs.Count(idx => idx.FieldNames.Contains(x.ColumName)) < 1) { sql += " primary key"; } else if (x.KeyKind == ConstraintsKind.NotNull) { sql += " not null"; } return(sql.ToUpper()); }); var index = Indexs.GroupBy(x => x.IndexName) .Where(x => 0 < x.Count()) .Select(x => { var sql = "PRIMARY KEY ("; foreach (var col in x) { sql += col.FieldNames; } sql += ")"; return(sql); }); return($"CREATE TABLE {TableName.ToUpper()} ({ string.Join(", ", colums.Union(index).ToArray())})"); }
public string GetDdl(DbViewModel dbVm) { var colums = Colums.Select(x => { var sql = $"{x.ColumName} {x.ColumType}"; if (!x.NullFlag) { sql += " not null"; } return(sql.ToUpper()); }); var index = Indexs .Select(x => { var sql = x.IndexName.StartsWith("rdb", System.StringComparison.OrdinalIgnoreCase) ? "" : $"CONSTRAINT {x.IndexName} "; switch (x.Kind) { case ConstraintsKind.Primary: sql += $"PRIMARY KEY ({string.Join(", ", x.FieldNames.ToArray())})"; break; case ConstraintsKind.Foreign: var idx = dbVm.Indexes.Where(dbIdx => dbIdx.IndexName == x.IndexName).First(); sql += $"FOREIGN KEY({string.Join(", ", x.FieldNames.ToArray())}) REFERENCES {idx.TableName} ({string.Join(", ", idx.FieldNames.ToArray())})"; break; case ConstraintsKind.Unique: sql += $"UNIQUE ({string.Join(", ", x.FieldNames.ToArray())})"; break; default: return(""); } return(sql); }); var domain = Colums.Where(x => x.IsDomainType) .Select(x => new { x.ColumType, x.ColumDataType }) .Distinct() .Select(x => $"CREATE DOMAIN {x.ColumType} AS {x.ColumDataType};\r\n"); var domainStr = string.Join("", domain.ToArray()); return(domainStr + $"CREATE TABLE {TableName} ({Environment.NewLine} { string.Join($",{Environment.NewLine} ", colums.Union(index).ToArray()) + Environment.NewLine})"); }
public string GetDdl(DbViewModel dbVm) { return($"CREATE VIEW {TableName} ({string.Join(", ", Colums.Select(x => x.ColumName).ToArray())}) AS" + Environment.NewLine + Source); }