Skip to content

Commit

Permalink
Support column charset/collation
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Nov 13, 2014
1 parent 9e29da1 commit 205d346
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 22 deletions.
2 changes: 2 additions & 0 deletions dg.Sql.SchemaGeneratorAddIn/Macro Structure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Keywords:
NOPROPERTY - will remove the property code for this specific column. Can be used to write a custom property
ACTUALDEFAULT - define a different code-based default value for the class's instance member variable
COLUMNNAME - define the actual column name in the db, if it is different than the variable's name
CHARSET - the name of a character set for this column
COLLATE - the name of a collation for this column
TODB - Define a format to be used in Insert(...) and Update(...). Can use the {0} specifier for column value in format
FROMDB - Define a format to be used in Read(...). Can use the {0} specifier for column value in format
VIRTUAL - Defines the property as virtual
Expand Down
41 changes: 31 additions & 10 deletions dg.Sql.SchemaGeneratorAddIn/SchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,15 @@ public static void GenerateDalClass(DTE2 application)
dalForeignKey1.ForeignColumns.Add(str30.Substring(str30.IndexOf(".") + 1));
dalForeignKey1.Columns.Add(dalColumn.Name);
dalForeignKeys.Add(dalForeignKey1);
}
}
else if (columnKeywordUpper.StartsWith("CHARSET ", StringComparison.Ordinal))
{
dalColumn.Charset = columnKeyword.Substring(8);
}
else if (columnKeywordUpper.StartsWith("COLLATE ", StringComparison.Ordinal))
{
dalColumn.Collate = columnKeyword.Substring(8);
}
}
if (dalColumn.IsPrimaryKey & dalColumn.Type == DalColumnType.TInt)
{
Expand Down Expand Up @@ -677,7 +685,6 @@ public static void GenerateDalClass(DTE2 application)
foreach (DalColumn enumTypeName in dalColumns)
{
string actualType = enumTypeName.ActualType;
stringBuilder.AppendFormat("schema.AddColumn(Columns.{1}, ", "\r\n", enumTypeName.Name);
if (!string.IsNullOrEmpty(enumTypeName.EnumTypeName))
{
enumTypeName.ActualType = enumTypeName.EnumTypeName;
Expand Down Expand Up @@ -857,8 +864,10 @@ public static void GenerateDalClass(DTE2 application)
else if (enumTypeName.Type == DalColumnType.TGeographicMultiSurface)
{
enumTypeName.ActualType = "Geometry.GeometryCollection";
}
stringBuilder.AppendFormat("typeof({0})", enumTypeName.ActualType);
}

stringBuilder.AppendFormat("schema.AddColumn(Columns.{0}, typeof({1})", enumTypeName.Name, enumTypeName.ActualType);

if (enumTypeName.Type == DalColumnType.TText)
{
stringBuilder.Append(", DataType.Text");
Expand Down Expand Up @@ -1031,12 +1040,24 @@ public static void GenerateDalClass(DTE2 application)
enumTypeName.ActualType += "?";
}

object[] dataTypeFormatArgs = new object[] { "\r\n", enumTypeName.MaxLength, enumTypeName.Precision, enumTypeName.Scale, null, null, null, null };
dataTypeFormatArgs[4] = (enumTypeName.AutoIncrement ? "true" : "false");
dataTypeFormatArgs[5] = (enumTypeName.IsPrimaryKey ? "true" : "false");
dataTypeFormatArgs[6] = (enumTypeName.IsNullable ? "true" : "false");
dataTypeFormatArgs[7] = enumTypeName.DefaultValue;
stringBuilder.AppendFormat(", {1}, {2}, {3}, {4}, {5}, {6}, {7});{0}", dataTypeFormatArgs);
stringBuilder.AppendFormat(", {0}, {1}, {2}, {3}, {4}, {5}, {6}",
enumTypeName.MaxLength,
enumTypeName.Precision,
enumTypeName.Scale,
enumTypeName.AutoIncrement ? "true" : "false",
enumTypeName.IsPrimaryKey ? "true" : "false",
enumTypeName.IsNullable ? "true" : "false",
enumTypeName.DefaultValue);

if (!string.IsNullOrEmpty(enumTypeName.Charset) || !string.IsNullOrEmpty(enumTypeName.Collate))
{
stringBuilder.AppendFormat(@", {0}, {1}",
string.IsNullOrEmpty(enumTypeName.Charset) ? "null" : (@"""" + enumTypeName.Charset + @""""),
string.IsNullOrEmpty(enumTypeName.Collate) ? "null" : (@"""" + enumTypeName.Collate + @""""));
}

stringBuilder.AppendFormat(");{0}", "\r\n");

if (string.IsNullOrEmpty(actualType))
{
continue;
Expand Down
4 changes: 3 additions & 1 deletion dg.Sql.SchemaGeneratorAddIn/Types/DalColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public class DalColumn
public string Comment;
public string ActualType;
public string ToDb;
public string FromDb;
public string FromDb;
public string Charset;
public string Collate;
public bool Virtual;
}
}
36 changes: 26 additions & 10 deletions dg.Sql/Sql/Query/Builders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ public void BuildColumnPropertiesDataType(StringBuilder sb, ConnectorBase connec
}
sb.Append(' ');
}

if (column.AutoIncrement)
{
if (dataType == DataType.BigInt || dataType == DataType.UnsignedBigInt)
Expand All @@ -801,6 +802,20 @@ public void BuildColumnPropertiesDataType(StringBuilder sb, ConnectorBase connec
}
sb.Append(' ');
}

if (connection.TYPE != ConnectorBase.SqlServiceType.POSTGRESQL && !string.IsNullOrEmpty(column.Charset))
{
sb.Append(@"COLLATE ");
sb.Append(column.Collate);
sb.Append(' ');
}

if (!string.IsNullOrEmpty(column.Charset))
{
sb.Append(@"CHARACTER SET ");
sb.Append(column.Charset);
sb.Append(' ');
}
}

public string BuildCommand()
Expand Down Expand Up @@ -1464,26 +1479,27 @@ public string BuildCommand(ConnectorBase connection)
{
// Very limited syntax, will have to do this with several statements

string alterTableStatement = @"ALTER TABLE ";
sb.Append(@"ALTER TABLE ");
if (Schema.DatabaseOwner.Length > 0)
{
alterTableStatement += connection.EncloseFieldName(Schema.DatabaseOwner);
alterTableStatement += '.';
sb.Append(connection.EncloseFieldName(Schema.DatabaseOwner));
sb.Append('.');
}
alterTableStatement += connection.EncloseFieldName(_SchemaName);
alterTableStatement += @" ALTER COLUMN ";
alterTableStatement += connection.EncloseFieldName(_AlterColumn.Name);
sb.Append(connection.EncloseFieldName(_SchemaName));

string alterColumnStatement = @" ALTER COLUMN ";
alterColumnStatement += connection.EncloseFieldName(_AlterColumn.Name);

sb.Append(alterTableStatement);
sb.Append(alterColumnStatement);
sb.Append(@" TYPE ");
bool isTextField; // UNUSED HERE
BuildColumnPropertiesDataType(sb, connection, _AlterColumn, out isTextField);
sb.Append(';');
sb.Append(',');

sb.Append(alterTableStatement);
sb.Append(alterColumnStatement);
sb.Append(_AlterColumn.Nullable ? @" DROP NOT NULL;" : @" SET NOT NULL;");

sb.Append(alterTableStatement);
sb.Append(alterColumnStatement);
sb.Append(@" SET DEFAULT ");
PrepareColumnValue(_AlterColumn, _AlterColumn.Default, sb, connection);
sb.Append(';');
Expand Down
37 changes: 37 additions & 0 deletions dg.Sql/Sql/Schema/TableSchema.Column.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ public class Column
private int _MaxLength;
public int NumberPrecision;
public int NumberScale;
public string Charset;
public string Collate;

public Column() { }

public Column(string Name, System.Type Type, int MaxLength, int NumberPrecision, int NumberScale, bool AutoIncrement, bool IsPrimaryKey, bool Nullable, object Default)
{
this.Name = Name;
Expand All @@ -46,6 +49,23 @@ public Column(string Name, System.Type Type, int MaxLength, int NumberPrecision,
this.Default = Default;
this._ActualDataType = GetDataType();
}

public Column(string Name, System.Type Type, int MaxLength, int NumberPrecision, int NumberScale, bool AutoIncrement, bool IsPrimaryKey, bool Nullable, object Default, string Charset, string Collate)
{
this.Name = Name;
this.Type = Type;
this._MaxLength = MaxLength;
this.NumberPrecision = NumberPrecision;
this.NumberScale = NumberScale;
this.AutoIncrement = AutoIncrement;
this.IsPrimaryKey = IsPrimaryKey;
this.Nullable = Nullable;
this.Default = Default;
this._ActualDataType = GetDataType();
this.Charset = Charset;
this.Collate = Collate;
}

public Column(string Name, System.Type Type, DataType DataType, int MaxLength, int NumberPrecision, int NumberScale, bool AutoIncrement, bool IsPrimaryKey, bool Nullable, object Default)
{
this.Name = Name;
Expand All @@ -61,6 +81,23 @@ public Column(string Name, System.Type Type, DataType DataType, int MaxLength, i
this._ActualDataType = GetDataType();
}

public Column(string Name, System.Type Type, DataType DataType, int MaxLength, int NumberPrecision, int NumberScale, bool AutoIncrement, bool IsPrimaryKey, bool Nullable, object Default, string Charset, string Collate)
{
this.Name = Name;
this.Type = Type;
this._DataType = DataType;
this._MaxLength = MaxLength;
this.NumberPrecision = NumberPrecision;
this.NumberScale = NumberScale;
this.AutoIncrement = AutoIncrement;
this.IsPrimaryKey = IsPrimaryKey;
this.Nullable = Nullable;
this.Default = Default;
this._ActualDataType = GetDataType();
this.Charset = Charset;
this.Collate = Collate;
}

public System.Type Type
{
get { return this._Type; }
Expand Down
12 changes: 11 additions & 1 deletion dg.Sql/Sql/Schema/TableSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,21 @@ public void AddColumn(string Name, System.Type Type, int MaxLength, int NumberPr
Column column = new Column(Name, Type, MaxLength, NumberPrecision, NumberScale, AutoIncrement, IsPrimaryKey, Nullable, Default);
AddColumn(column);
}
public void AddColumn(string Name, System.Type Type, DataType DataType,int MaxLength, int NumberPrecision, int NumberScale, bool AutoIncrement, bool IsPrimaryKey, bool Nullable, object Default)
public void AddColumn(string Name, System.Type Type, DataType DataType, int MaxLength, int NumberPrecision, int NumberScale, bool AutoIncrement, bool IsPrimaryKey, bool Nullable, object Default)
{
Column column = new Column(Name, Type, DataType, MaxLength, NumberPrecision, NumberScale, AutoIncrement, IsPrimaryKey, Nullable, Default);
AddColumn(column);
}
public void AddColumn(string Name, System.Type Type, int MaxLength, int NumberPrecision, int NumberScale, bool AutoIncrement, bool IsPrimaryKey, bool Nullable, object Default, string Charset, string Collate)
{
Column column = new Column(Name, Type, MaxLength, NumberPrecision, NumberScale, AutoIncrement, IsPrimaryKey, Nullable, Default, Charset, Collate);
AddColumn(column);
}
public void AddColumn(string Name, System.Type Type, DataType DataType, int MaxLength, int NumberPrecision, int NumberScale, bool AutoIncrement, bool IsPrimaryKey, bool Nullable, object Default, string Charset, string Collate)
{
Column column = new Column(Name, Type, DataType, MaxLength, NumberPrecision, NumberScale, AutoIncrement, IsPrimaryKey, Nullable, Default, Charset, Collate);
AddColumn(column);
}
public void AddIndex(Index index)
{
if (Indexes == null) Indexes = new IndexList();
Expand Down

0 comments on commit 205d346

Please sign in to comment.