/// <summary> /// Returns the identifier in its delimited form. /// </summary> /// <param name="parts">The identifier parts to delimit. The default value is all parts.</param> /// <returns>The identifier name in its delimited form.</returns> public string Delimit(SqlIdentifierParts parts = SqlIdentifierParts.All) { var text = new StringBuilder(); if (parts.HasFlag(SqlIdentifierParts.SchemaName)) { text.Append('['); text.Append(SchemaName); text.Append(']'); } if (parts.HasFlag(SqlIdentifierParts.ObjectName)) { if (text.Length > 0) { text.Append('.'); } text.Append('['); text.Append(ObjectName); text.Append(']'); } return(text.ToString()); }
/// <summary> /// Returns the object name in a form suitable for an identifier name. /// </summary> /// <param name="parts">The identifier parts to delimit. The default value is all parts.</param> /// <returns>The object name as a safe, identifier name.</returns> /// <remarks>This method can be used to return the object name in a safe form /// that can be used in identifiers. For example, if an index is to be created /// for a table, the format "IX_{name}" might be used. If the qualified object /// name is [dbo].[My Table], this method would return "dbo_My_Table" that can /// subsequently used to build the index identifier "IX_dbo_My_Table".</remarks> public string AsIdentifierName(SqlIdentifierParts parts = SqlIdentifierParts.All) { var text = new StringBuilder(); if (parts.HasFlag(SqlIdentifierParts.SchemaName)) { foreach (var @char in SchemaName) { text.Append(IsLetterOrDigit(@char) ? @char : '_'); } } if (parts.HasFlag(SqlIdentifierParts.ObjectName)) { if (text.Length > 0) { text.Append('_'); } foreach (var @char in ObjectName) { text.Append(IsLetterOrDigit(@char) ? @char : '_'); } } return(text.ToString()); }