private static void BuildPrimaryKey(MetaTable table, StringBuilder sb) { foreach (MetaDataMember mm in table.RowType.IdentityMembers) { if (sb.Length > 0) { sb.Append(", "); } sb.Append(SqlIdentifier.QuoteCompoundIdentifier(mm.MappedName)); } }
protected override void WriteName(string name) { if (SqlIdentifier.NeedToQuote(name)) { sb.Append(SqlIdentifier.QuoteCompoundIdentifier(name)); } else { sb.Append(name); } }
protected override void WriteName(string name) { var index = name.LastIndexOf('.'); var columnName = index > 0 ? name.Substring(index + 1) : name; if (SqlIdentifier.NeedToQuote(columnName)) { sb.Append(SqlIdentifier.QuoteCompoundIdentifier(columnName)); } else { sb.Append(name); } }
private static string BuildKey(IEnumerable <MetaDataMember> members) { StringBuilder sb = new StringBuilder(); foreach (MetaDataMember mm in members) { if (sb.Length > 0) { sb.Append(", "); } sb.Append(SqlIdentifier.QuoteCompoundIdentifier(mm.MappedName)); } return(sb.ToString()); }
private int BuildFieldDeclarations(MetaType type, IDictionary <object, string> memberNameToMappedName, StringBuilder sb) { int num = 0; foreach (MetaDataMember member in type.DataMembers) { string str; if ((!member.IsDeclaredBy(type) || member.IsAssociation) || !member.IsPersistent) { continue; } object key = InheritanceRules.DistinguishedMemberName(member.Member); if (memberNameToMappedName.TryGetValue(key, out str)) { if (str != member.MappedName) { goto Label_0075; } continue; } memberNameToMappedName.Add(key, member.MappedName); Label_0075: if (sb.Length > 0) { sb.Append(", "); } sb.AppendLine(); sb.Append(string.Format(CultureInfo.InvariantCulture, " {0} ", new object[] { SqlIdentifier.QuoteCompoundIdentifier(member.MappedName) })); if (!string.IsNullOrEmpty(member.Expression)) { sb.Append("AS " + member.Expression); } else { sb.Append(GetDbType(member)); } num++; } return(num); }
private static IEnumerable <String> GetCreateForeignKeyCommands(MetaType type) { string tableName = type.Table.TableName; foreach (MetaDataMember mm in type.DataMembers) { if (mm.IsDeclaredBy(type) && mm.IsAssociation) { MetaAssociation assoc = mm.Association; if (assoc.IsForeignKey) { StringBuilder sb = new StringBuilder(); string thisKey = BuildKey(assoc.ThisKey); string otherKey = BuildKey(assoc.OtherKey); string otherTable = assoc.OtherType.Table.TableName; string name; name = mm.MappedName; if (name == mm.Name) { name = String.Format(Globalization.CultureInfo.InvariantCulture, "FK_{0}_{1}", tableName, mm.Name); } string cmd = "ALTER TABLE {0}" + Environment.NewLine + " ADD CONSTRAINT {1} FOREIGN KEY ({2}) REFERENCES {3}({4})"; //In DLinq we put the constraint on the child object (which triggers the behavior when deleted), //but in SQL it is part of the parent constraint (the parent row gets changed / deleted to satisfy the constraint) MetaDataMember otherMember = mm.Association.OtherMember; if (otherMember != null) { string delConstr = otherMember.Association.DeleteRule; if (delConstr != null) { cmd += Environment.NewLine + " ON DELETE " + delConstr; } } sb.AppendFormat(cmd, SqlIdentifier.QuoteCompoundIdentifier(tableName), SqlIdentifier.QuoteIdentifier(name), SqlIdentifier.QuoteCompoundIdentifier(thisKey), SqlIdentifier.QuoteCompoundIdentifier(otherTable), SqlIdentifier.QuoteCompoundIdentifier(otherKey)); yield return(sb.ToString()); } } } }
internal static string GetCreateTableCommand(MetaTable table) { StringBuilder sb = new StringBuilder(); StringBuilder decl = new StringBuilder(); BuildFieldDeclarations(table, decl); sb.AppendFormat("CREATE TABLE {0}", SqlIdentifier.QuoteCompoundIdentifier(table.TableName)); sb.Append("("); sb.Append(decl.ToString()); decl = new StringBuilder(); BuildPrimaryKey(table, decl); if (decl.Length > 0) { string name = String.Format(Globalization.CultureInfo.InvariantCulture, "PK_{0}", table.TableName); sb.Append(", "); sb.AppendLine(); sb.AppendFormat(" CONSTRAINT {0} PRIMARY KEY ({1})", SqlIdentifier.QuoteIdentifier(name), decl.ToString()); } sb.AppendLine(); sb.Append(" )"); return(sb.ToString()); }
internal override SqlSelect VisitSelect(SqlSelect ss) { if (!ss.DoNotOutput) { string str = null; if (ss.From != null) { StringBuilder tmp = this.sb; this.sb = new StringBuilder(); if (IsSimpleCrossJoinList(ss.From)) { VisitCrossJoinList(ss.From); } else { Visit(ss.From); } str = this.sb.ToString(); this.sb = tmp; } else { str = "RDB$DATABASE"; } this.sb.Append("SELECT "); if (ss.IsDistinct) { sb.Append("DISTINCT "); } if (ss.Top != null) { if (ss.Top is SqlFunctionCall == false) { sb.Append("FIRST "); } Visit(ss.Top); sb.Append(" "); } if (ss.Row.Columns.Count > 0) { this.VisitRow(ss.Row); } else if (this.isDebugMode) { this.Visit(ss.Selection); } else { //this.sb.Append("NULL AS [EMPTY]"); sb.Append("NULL AS "); sb.Append(SqlIdentifier.QuoteCompoundIdentifier("EMPTY")); } if (str != null) { this.NewLine(); this.sb.Append("FROM "); this.sb.Append(str); } if (ss.Where != null) { this.NewLine(); this.sb.Append("WHERE "); this.Visit(ss.Where); } if (ss.GroupBy.Count > 0) { this.NewLine(); this.sb.Append("GROUP BY "); int num = 0; int count = ss.GroupBy.Count; while (num < count) { SqlExpression node = ss.GroupBy[num]; if (num > 0) { this.sb.Append(", "); } this.Visit(node); num++; } if (ss.Having != null) { this.NewLine(); this.sb.Append("HAVING "); this.Visit(ss.Having); } } if ((ss.OrderBy.Count > 0) && (ss.OrderingType != SqlOrderingType.Never)) { this.NewLine(); this.sb.Append("ORDER BY "); int num3 = 0; int num4 = ss.OrderBy.Count; while (num3 < num4) { SqlOrderExpression expression2 = ss.OrderBy[num3]; if (num3 > 0) { this.sb.Append(", "); } this.Visit(expression2.Expression); if (expression2.OrderType == SqlOrderType.Descending) { this.sb.Append(" DESC"); } num3++; } } } return(ss); }
private static int BuildFieldDeclarations(MetaType type, Dictionary <object, string> memberNameToMappedName, StringBuilder sb) { int n = 0; foreach (MetaDataMember mm in type.DataMembers) { // Only generate declarations for the current type. if (mm.IsDeclaredBy(type)) { if (!mm.IsAssociation) { if (mm.IsPersistent) { object dn = InheritanceRules.DistinguishedMemberName(mm.Member); string mappedName; if (memberNameToMappedName.TryGetValue(dn, out mappedName)) { if (mappedName == mm.MappedName) { continue; } } else { memberNameToMappedName.Add(dn, mm.MappedName); } if (sb.Length > 0) { sb.Append(", "); } sb.AppendLine(); sb.Append(string.Format(Globalization.CultureInfo.InvariantCulture, " {0} ", SqlIdentifier.QuoteCompoundIdentifier(mm.MappedName))); if (!string.IsNullOrEmpty(mm.Expression)) { // Generate "AS <expression>" for computed columns sb.Append("AS " + mm.Expression); } else { sb.Append(GetDbType(mm)); } n++; } } } } return(n); }