Skip to content
Permalink
Browse files Browse the repository at this point in the history
SqlGen - EscapeNames property to disable escaping
  • Loading branch information
Martin Willey committed Sep 3, 2020
1 parent 95e9446 commit 4c0ab7b
Show file tree
Hide file tree
Showing 32 changed files with 468 additions and 130 deletions.
29 changes: 27 additions & 2 deletions DatabaseSchemaReader.sln
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26228.4
# Visual Studio Version 16
VisualStudioVersion = 16.0.30225.117
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DatabaseSchemaReader", "DatabaseSchemaReader\DatabaseSchemaReader.csproj", "{8280AFB4-9066-4656-A80E-23B859E5299C}"
EndProject
Expand All @@ -22,6 +22,8 @@ Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x86 = Debug|x86
net35|Any CPU = net35|Any CPU
net35|x86 = net35|x86
Net4|Any CPU = Net4|Any CPU
Net4|x86 = Net4|x86
net45|Any CPU = net45|Any CPU
Expand All @@ -34,6 +36,10 @@ Global
{8280AFB4-9066-4656-A80E-23B859E5299C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8280AFB4-9066-4656-A80E-23B859E5299C}.Debug|x86.ActiveCfg = Debug|Any CPU
{8280AFB4-9066-4656-A80E-23B859E5299C}.Debug|x86.Build.0 = Debug|Any CPU
{8280AFB4-9066-4656-A80E-23B859E5299C}.net35|Any CPU.ActiveCfg = Release|Any CPU
{8280AFB4-9066-4656-A80E-23B859E5299C}.net35|Any CPU.Build.0 = Release|Any CPU
{8280AFB4-9066-4656-A80E-23B859E5299C}.net35|x86.ActiveCfg = Release|Any CPU
{8280AFB4-9066-4656-A80E-23B859E5299C}.net35|x86.Build.0 = Release|Any CPU
{8280AFB4-9066-4656-A80E-23B859E5299C}.Net4|Any CPU.ActiveCfg = Release|Any CPU
{8280AFB4-9066-4656-A80E-23B859E5299C}.Net4|Any CPU.Build.0 = Release|Any CPU
{8280AFB4-9066-4656-A80E-23B859E5299C}.Net4|x86.ActiveCfg = Release|Any CPU
Expand All @@ -50,6 +56,10 @@ Global
{2881C1CA-88FC-4635-8B48-C0F1170C39B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2881C1CA-88FC-4635-8B48-C0F1170C39B7}.Debug|x86.ActiveCfg = Debug|Any CPU
{2881C1CA-88FC-4635-8B48-C0F1170C39B7}.Debug|x86.Build.0 = Debug|Any CPU
{2881C1CA-88FC-4635-8B48-C0F1170C39B7}.net35|Any CPU.ActiveCfg = Release|Any CPU
{2881C1CA-88FC-4635-8B48-C0F1170C39B7}.net35|Any CPU.Build.0 = Release|Any CPU
{2881C1CA-88FC-4635-8B48-C0F1170C39B7}.net35|x86.ActiveCfg = Release|Any CPU
{2881C1CA-88FC-4635-8B48-C0F1170C39B7}.net35|x86.Build.0 = Release|Any CPU
{2881C1CA-88FC-4635-8B48-C0F1170C39B7}.Net4|Any CPU.ActiveCfg = Release|Any CPU
{2881C1CA-88FC-4635-8B48-C0F1170C39B7}.Net4|Any CPU.Build.0 = Release|Any CPU
{2881C1CA-88FC-4635-8B48-C0F1170C39B7}.Net4|x86.ActiveCfg = Release|Any CPU
Expand All @@ -66,6 +76,10 @@ Global
{7EF4F6B5-100C-432A-9858-C7F3FEC14F5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7EF4F6B5-100C-432A-9858-C7F3FEC14F5C}.Debug|x86.ActiveCfg = Debug|Any CPU
{7EF4F6B5-100C-432A-9858-C7F3FEC14F5C}.Debug|x86.Build.0 = Debug|Any CPU
{7EF4F6B5-100C-432A-9858-C7F3FEC14F5C}.net35|Any CPU.ActiveCfg = net45|Any CPU
{7EF4F6B5-100C-432A-9858-C7F3FEC14F5C}.net35|Any CPU.Build.0 = net45|Any CPU
{7EF4F6B5-100C-432A-9858-C7F3FEC14F5C}.net35|x86.ActiveCfg = net45|Any CPU
{7EF4F6B5-100C-432A-9858-C7F3FEC14F5C}.net35|x86.Build.0 = net45|Any CPU
{7EF4F6B5-100C-432A-9858-C7F3FEC14F5C}.Net4|Any CPU.ActiveCfg = net45|Any CPU
{7EF4F6B5-100C-432A-9858-C7F3FEC14F5C}.Net4|Any CPU.Build.0 = net45|Any CPU
{7EF4F6B5-100C-432A-9858-C7F3FEC14F5C}.Net4|x86.ActiveCfg = net45|Any CPU
Expand All @@ -82,6 +96,10 @@ Global
{7BE356E2-11FD-4570-8523-EF512D476797}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7BE356E2-11FD-4570-8523-EF512D476797}.Debug|x86.ActiveCfg = Debug|Any CPU
{7BE356E2-11FD-4570-8523-EF512D476797}.Debug|x86.Build.0 = Debug|Any CPU
{7BE356E2-11FD-4570-8523-EF512D476797}.net35|Any CPU.ActiveCfg = net35|Any CPU
{7BE356E2-11FD-4570-8523-EF512D476797}.net35|Any CPU.Build.0 = net35|Any CPU
{7BE356E2-11FD-4570-8523-EF512D476797}.net35|x86.ActiveCfg = net35|Any CPU
{7BE356E2-11FD-4570-8523-EF512D476797}.net35|x86.Build.0 = net35|Any CPU
{7BE356E2-11FD-4570-8523-EF512D476797}.Net4|Any CPU.ActiveCfg = Net4|Any CPU
{7BE356E2-11FD-4570-8523-EF512D476797}.Net4|Any CPU.Build.0 = Net4|Any CPU
{7BE356E2-11FD-4570-8523-EF512D476797}.Net4|x86.ActiveCfg = Net4|Any CPU
Expand All @@ -97,6 +115,10 @@ Global
{CE4A50D0-62F7-49A4-B216-7DEB7EA57147}.Debug|Any CPU.ActiveCfg = Debug|x86
{CE4A50D0-62F7-49A4-B216-7DEB7EA57147}.Debug|x86.ActiveCfg = Debug|x86
{CE4A50D0-62F7-49A4-B216-7DEB7EA57147}.Debug|x86.Build.0 = Debug|x86
{CE4A50D0-62F7-49A4-B216-7DEB7EA57147}.net35|Any CPU.ActiveCfg = Release|x86
{CE4A50D0-62F7-49A4-B216-7DEB7EA57147}.net35|Any CPU.Build.0 = Release|x86
{CE4A50D0-62F7-49A4-B216-7DEB7EA57147}.net35|x86.ActiveCfg = net45|x86
{CE4A50D0-62F7-49A4-B216-7DEB7EA57147}.net35|x86.Build.0 = net45|x86
{CE4A50D0-62F7-49A4-B216-7DEB7EA57147}.Net4|Any CPU.ActiveCfg = net45|x86
{CE4A50D0-62F7-49A4-B216-7DEB7EA57147}.Net4|Any CPU.Build.0 = net45|x86
{CE4A50D0-62F7-49A4-B216-7DEB7EA57147}.Net4|x86.ActiveCfg = net45|x86
Expand All @@ -111,4 +133,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9D3E963B-CAE9-4937-86C7-E49C5DC2CDED}
EndGlobalSection
EndGlobal
11 changes: 9 additions & 2 deletions DatabaseSchemaReader/Data/InsertWriter.cs
Expand Up @@ -134,6 +134,11 @@ private void PrepareTypesDataTable()
/// <value><c>true</c> if include blobs; otherwise, <c>false</c>.</value>
public bool IncludeBlobs { get; set; }

/// <summary>
/// Escape table and column names (default true)
/// </summary>
public bool EscapeNames { get; set; } = true;

/// <summary>
/// Writes the INSERTs in the specified SQL dialect
/// </summary>
Expand Down Expand Up @@ -267,7 +272,7 @@ private void PrepareTemplate()
{
var cols = GetAllColumns();

_template = "INSERT INTO " + _sqlWriter.EscapedTableName + @" (
_template = "INSERT INTO " + (EscapeNames ? _sqlWriter.EscapedTableName : _databaseTable.Name) + @" (
" + FormattedColumns(cols) + @") VALUES (
{0}
);
Expand All @@ -288,7 +293,9 @@ private string[] GetAllColumns()
{
if (IsNotWriteableType(databaseColumn)) continue;
if (!IncludeIdentity && databaseColumn.IsAutoNumber) continue;
cols.Add(_sqlWriter.EscapedColumnName(databaseColumn.Name));
var name = databaseColumn.Name;
if (EscapeNames) name = _sqlWriter.EscapedColumnName(databaseColumn.Name);
cols.Add(name);
}

return cols.ToArray();
Expand Down
7 changes: 7 additions & 0 deletions DatabaseSchemaReader/Data/ScriptWriter.cs
Expand Up @@ -14,6 +14,11 @@ public class ScriptWriter
{
private int _pageSize = 1000;

/// <summary>
/// Escape table and column names (default true)
/// </summary>
public bool EscapeNames { get; set; } = true;

/// <summary>
/// Gets or sets the maximum number of records returned. Default is 1000.
/// </summary>
Expand Down Expand Up @@ -85,6 +90,7 @@ public string ReadTable(DatabaseTable databaseTable, DbConnection connection)
var w = new InsertWriter(databaseTable, dt);
w.IncludeIdentity = IncludeIdentity;
w.IncludeBlobs = IncludeBlobs;
w.EscapeNames = EscapeNames;
var providerName = connection.GetType().Namespace;
return w.Write(FindSqlType(providerName));
}
Expand Down Expand Up @@ -129,6 +135,7 @@ public string ReadTable(DatabaseTable databaseTable, string connectionString, st
var w = new InsertWriter(databaseTable, dt);
w.IncludeIdentity = IncludeIdentity;
w.IncludeBlobs = IncludeBlobs;
w.EscapeNames = EscapeNames;
return w.Write(FindSqlType(providerName));
}

Expand Down
6 changes: 5 additions & 1 deletion DatabaseSchemaReader/SqlGen/ConstraintWriterBase.cs
Expand Up @@ -14,13 +14,16 @@ abstract class ConstraintWriterBase
protected ConstraintWriterBase(DatabaseTable table)
{
Table = table;
EscapeNames = true;
}

protected abstract ISqlFormatProvider SqlFormatProvider();

public bool EscapeNames { get; set; }

protected string EscapeName(string name)
{
return SqlFormatProvider().Escape(name);
return EscapeNames? SqlFormatProvider().Escape(name) : name;
}

public bool IncludeSchema { get; set; }
Expand Down Expand Up @@ -74,6 +77,7 @@ protected virtual string AddUniqueConstraintFormat
{
get { return "ALTER TABLE {0} ADD CONSTRAINT {1} UNIQUE ({2})"; }
}

private string WriteUniqueKey(DatabaseConstraint uniqueKey)
{
var columnList = GetColumnList(uniqueKey.Columns);
Expand Down
2 changes: 1 addition & 1 deletion DatabaseSchemaReader/SqlGen/Db2/TablesGenerator.cs
Expand Up @@ -50,7 +50,7 @@ protected override ISqlFormatProvider SqlFormatProvider()

protected override void WriteDrops(StringBuilder sb)
{
sb.AppendLine(DropTables.Write(Schema, SqlFormatProvider()));
sb.AppendLine(DropTables.Write(Schema, SqlFormatProvider(), EscapeNames));
}
}
}
8 changes: 4 additions & 4 deletions DatabaseSchemaReader/SqlGen/DropTables.cs
Expand Up @@ -6,7 +6,7 @@ namespace DatabaseSchemaReader.SqlGen
{
class DropTables
{
public static string Write(DatabaseSchema schema, ISqlFormatProvider formatter)
public static string Write(DatabaseSchema schema, ISqlFormatProvider formatter, bool escapeNames)
{
var sb = new StringBuilder();
var lineEnding = formatter.LineEnding();
Expand All @@ -19,13 +19,13 @@ public static string Write(DatabaseSchema schema, ISqlFormatProvider formatter)
{
foreach (var foreignKey in table.ForeignKeys)
{
sb.AppendLine("-- ALTER TABLE " + formatter.Escape(table.Name) + " DROP CONSTRAINT " + foreignKey.Name + lineEnding);

sb.AppendLine("-- ALTER TABLE " + (escapeNames ? formatter.Escape(table.Name) : table.Name) +
" DROP CONSTRAINT " + foreignKey.Name + lineEnding);
}
}
foreach (var table in schema.Tables)
{
sb.AppendLine("-- DROP TABLE " + formatter.Escape(table.Name) + lineEnding);
sb.AppendLine("-- DROP TABLE " + (escapeNames ? formatter.Escape(table.Name) : table.Name) + lineEnding);
}
return sb.ToString();
}
Expand Down
6 changes: 5 additions & 1 deletion DatabaseSchemaReader/SqlGen/IMigrationGenerator.cs
Expand Up @@ -12,7 +12,11 @@ public interface IMigrationGenerator
/// </summary>
bool IncludeSchema { get; set; }
/// <summary>
/// Adds the table. If any primary key, unqiue or check constraints are attached, they are written too (don't write them individually). Foreign keys must be added separately (use <see cref="AddConstraint"/>)
/// Escape the names (default true)
/// </summary>
bool EscapeNames { get; set; }
/// <summary>
/// Adds the table. If any primary key, unique or check constraints are attached, they are written too (don't write them individually). Foreign keys must be added separately (use <see cref="AddConstraint"/>)
/// </summary>
/// <param name="databaseTable">The database table.</param>
/// <returns></returns>
Expand Down
6 changes: 4 additions & 2 deletions DatabaseSchemaReader/SqlGen/ITableGenerator.cs
Expand Up @@ -12,7 +12,10 @@ public interface ITableGenerator
/// </summary>
/// <value><c>true</c> if schema is written; otherwise, <c>false</c>.</value>
bool IncludeSchema { get; set; }

/// <summary>
/// Escape the names (default true)
/// </summary>
bool EscapeNames { get; set; }
/// <summary>
/// Gets or sets a value indicating whether to include default values while writing column definitions
/// </summary>
Expand All @@ -21,7 +24,6 @@ public interface ITableGenerator
/// </value>
bool IncludeDefaultValues { get; set; }


/// <summary>
/// Writes the DDL.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions DatabaseSchemaReader/SqlGen/ITablesGenerator.cs
Expand Up @@ -11,6 +11,11 @@ public interface ITablesGenerator
/// <value><c>true</c> if schema is written; otherwise, <c>false</c>.</value>
bool IncludeSchema { get; set; }

/// <summary>
/// Escape the table and column names
/// </summary>
bool EscapeNames { get; set; }

/// <summary>
/// Writes this ddl script.
/// </summary>
Expand Down
14 changes: 11 additions & 3 deletions DatabaseSchemaReader/SqlGen/MigrationGenerator.cs
Expand Up @@ -4,7 +4,6 @@
using System.Linq;
using System.Text;
using DatabaseSchemaReader.DataSchema;
using DatabaseSchemaReader.SqlGen.SqLite;
using DatabaseSchemaReader.Utilities;

namespace DatabaseSchemaReader.SqlGen
Expand All @@ -19,16 +18,24 @@ public MigrationGenerator(SqlType sqlType)
_sqlFormatProvider = SqlFormatFactory.Provider(sqlType);
_ddlFactory = new DdlGeneratorFactory(sqlType);
IncludeSchema = (sqlType != SqlType.SqlServerCe && sqlType != SqlType.SQLite);
EscapeNames = true;
}

/// <summary>
/// Include the schema when writing table. Must not be set for SQLite as there is no schema.
/// </summary>
public bool IncludeSchema { get; set; }

/// <summary>
/// Escape any names
/// </summary>
public bool EscapeNames { get; set; }

protected virtual ITableGenerator CreateTableGenerator(DatabaseTable databaseTable)
{
return _ddlFactory.TableGenerator(databaseTable);
var tableGenerator = _ddlFactory.TableGenerator(databaseTable);
if (!EscapeNames) tableGenerator.EscapeNames = false;
return tableGenerator;
}
protected virtual ISqlFormatProvider SqlFormatProvider()
{
Expand All @@ -37,7 +44,7 @@ protected virtual ISqlFormatProvider SqlFormatProvider()

public string Escape(string name)
{
return SqlFormatProvider().Escape(name);
return EscapeNames ? SqlFormatProvider().Escape(name) : name;
}
protected virtual string LineEnding()
{
Expand Down Expand Up @@ -214,6 +221,7 @@ public virtual string AddConstraint(DatabaseTable databaseTable, DatabaseConstra
var constraintWriter = _ddlFactory.ConstraintWriter(databaseTable);
if (constraintWriter == null) return null;
constraintWriter.IncludeSchema = IncludeSchema; //cascade setting
constraintWriter.EscapeNames = EscapeNames;
return constraintWriter.WriteConstraint(constraint);
}

Expand Down
2 changes: 1 addition & 1 deletion DatabaseSchemaReader/SqlGen/MySql/TablesGenerator.cs
Expand Up @@ -29,7 +29,7 @@ protected override ISqlFormatProvider SqlFormatProvider()

protected override void WriteDrops(StringBuilder sb)
{
sb.AppendLine(DropTables.Write(Schema, SqlFormatProvider()));
sb.AppendLine(DropTables.Write(Schema, SqlFormatProvider(), EscapeNames));
}
}
}
2 changes: 1 addition & 1 deletion DatabaseSchemaReader/SqlGen/SqlServer/TableGenerator.cs
Expand Up @@ -97,7 +97,7 @@ private static string TranslateCheckExpression(string expression)
}
protected virtual ConstraintWriterBase CreateConstraintWriter()
{
return new ConstraintWriter(Table) { IncludeSchema = IncludeSchema };
return new ConstraintWriter(Table) { IncludeSchema = IncludeSchema, EscapeNames = EscapeNames};
}
protected virtual IMigrationGenerator CreateMigrationGenerator()
{
Expand Down
2 changes: 1 addition & 1 deletion DatabaseSchemaReader/SqlGen/SqlServer/TablesGenerator.cs
Expand Up @@ -59,7 +59,7 @@ protected override ISqlFormatProvider SqlFormatProvider()

protected override void WriteDrops(StringBuilder sb)
{
sb.AppendLine(DropTables.Write(Schema, SqlFormatProvider()));
sb.AppendLine(DropTables.Write(Schema, SqlFormatProvider(), EscapeNames));
}
}
}
8 changes: 7 additions & 1 deletion DatabaseSchemaReader/SqlGen/TableGeneratorBase.cs
Expand Up @@ -16,13 +16,19 @@ protected TableGeneratorBase(DatabaseTable table)
TableName = table.Name;
IncludeSchema = true;
IncludeDefaultValues = true;
EscapeNames = true;
}

protected abstract ISqlFormatProvider SqlFormatProvider();

/// <summary>
/// Escape any names
/// </summary>
public bool EscapeNames { get; set; }

private string EscapeName(string name)
{
return SqlFormatProvider().Escape(name);
return EscapeNames? SqlFormatProvider().Escape(name) : name;
}
protected abstract string WriteDataType(DatabaseColumn column);
protected abstract string NonNativeAutoIncrementWriter();
Expand Down
5 changes: 5 additions & 0 deletions DatabaseSchemaReader/SqlGen/TablesGeneratorBase.cs
Expand Up @@ -11,10 +11,13 @@ protected TablesGeneratorBase(DatabaseSchema schema)
{
Schema = schema;
IncludeSchema = true;
EscapeNames = true;
}

public bool IncludeSchema { get; set; }

public bool EscapeNames { get; set; }

public string Write()
{
var sb = new StringBuilder();
Expand All @@ -24,6 +27,7 @@ public string Write()
{
var tableGenerator = LoadTableGenerator(table);
tableGenerator.IncludeSchema = IncludeSchema;
tableGenerator.EscapeNames = EscapeNames;
sb.AppendLine(tableGenerator.Write());
}
sb.AppendLine();
Expand All @@ -35,6 +39,7 @@ public string Write()
//SQLite has no ALTER TABLE support, so this is not supported
if (constraintWriter == null) continue;
constraintWriter.IncludeSchema = IncludeSchema;
constraintWriter.EscapeNames = EscapeNames;
sb.AppendLine(constraintWriter.WriteForeignKeys());
}
sb.AppendLine();
Expand Down

0 comments on commit 4c0ab7b

Please sign in to comment.