Skip to content

Commit

Permalink
Merge branch 'master' into feature/persisted-computed-column
Browse files Browse the repository at this point in the history
  • Loading branch information
sethreno authored Mar 13, 2017
2 parents 4a96dfb + 3b10457 commit 9e7d5b1
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 21 deletions.
6 changes: 3 additions & 3 deletions model/DBHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ public static void CreateDb(string connection, string databaseFilesPath = null)
if (databaseFilesPath != null) {
Directory.CreateDirectory(databaseFilesPath);
files = $@"ON
(NAME = {dbName},
(NAME = '{dbName}',
FILENAME = '{databaseFilesPath}\{dbName + Guid.NewGuid()}.mdf')
LOG ON
(NAME = {dbName}_log,
(NAME = '{dbName}_log',
FILENAME = '{databaseFilesPath}\{dbName + Guid.NewGuid()}.ldf')";
}
ExecSql(cnBuilder.ToString(), "CREATE DATABASE [" + dbName + "]" + files);
ExecSql(cnBuilder.ToString(), "CREATE DATABASE [" + dbName + "] " + files);
}

public static bool DbExists(string conn) {
Expand Down
7 changes: 4 additions & 3 deletions model/Models/Column.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ public string IdentityText {
}
}

public string RowGuidColText => IsRowGuidCol ? "ROWGUIDCOL" : string.Empty;
public bool Persisted { get; set; }
public string RowGuidColText => IsRowGuidCol ? " ROWGUIDCOL " : string.Empty;

public bool Persisted { get; set; }

public ColumnDiff Compare(Column c) {
return new ColumnDiff(this, c);
Expand Down Expand Up @@ -101,7 +102,7 @@ private string ScriptBase(bool includeDefaultConstraint) {
val.Append($" {IsNullableText}");
if (includeDefaultConstraint) val.Append(DefaultText);
if (Identity != null) val.Append(IdentityText);
if (IsRowGuidCol) val.Append(@" {RowGuidColText}");
if (IsRowGuidCol) val.Append(RowGuidColText);
return val.ToString();

case "binary":
Expand Down
61 changes: 61 additions & 0 deletions model/Models/Comparers/ForeignKeyComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;

namespace SchemaZen.Library.Models.Comparers
{
public class ForeignKeyComparer : IComparer<ForeignKey>
{
public static ForeignKeyComparer Instance { get; } = new ForeignKeyComparer();

public int Compare(ForeignKey x, ForeignKey y)
{
var result = string.Compare(x.Table.Owner, y.Table.Owner, StringComparison.Ordinal);

if (result != 0)
{
return result;
}

result = string.Compare(x.Table.Name, y.Table.Name, StringComparison.Ordinal);

if (result != 0)
{
return result;
}

result = x.Columns.Count.CompareTo(y.Columns.Count);

if (result != 0)
{
return result;
}

for (var i = 0; i < x.Columns.Count; i++)
{
result = string.Compare(x.Columns[i], y.Columns[i], StringComparison.Ordinal);
if (result != 0)
{
return result;
}
}

result = x.RefColumns.Count.CompareTo(y.RefColumns.Count);

if (result != 0)
{
return result;
}

for (var i = 0; i < x.RefColumns.Count; i++)
{
result = string.Compare(x.RefColumns[i], y.RefColumns[i], StringComparison.Ordinal);
if (result != 0)
{
return result;
}
}

return string.Compare(x.Name, y.Name, StringComparison.Ordinal);
}
}
}
5 changes: 3 additions & 2 deletions model/Models/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using SchemaZen.Library.Models.Comparers;

namespace SchemaZen.Library.Models {
public class Database {
Expand Down Expand Up @@ -1165,7 +1166,7 @@ public string ScriptCreate() {
text.AppendLine();
text.AppendLine("GO");

foreach (var fk in ForeignKeys) {
foreach (var fk in ForeignKeys.OrderBy(f => f, ForeignKeyComparer.Instance)) {
text.AppendLine(fk.ScriptCreate());
}
text.AppendLine();
Expand Down Expand Up @@ -1228,7 +1229,7 @@ public void ScriptToDir(string tableHint = null, Action<TraceLevel, string> log
WriteScriptDir("tables", Tables.ToArray(), log);
WriteScriptDir("table_types", TableTypes.ToArray(), log);
WriteScriptDir("user_defined_types", UserDefinedTypes.ToArray(), log);
WriteScriptDir("foreign_keys", ForeignKeys.OrderBy(x => x.Name).ToArray(), log);
WriteScriptDir("foreign_keys", ForeignKeys.OrderBy(x => x, ForeignKeyComparer.Instance).ToArray(), log);
foreach (var routineType in Routines.GroupBy(x => x.RoutineType)) {
var dir = routineType.Key.ToString().ToLower() + "s";
WriteScriptDir(dir, routineType.ToArray(), log);
Expand Down
12 changes: 9 additions & 3 deletions model/Models/Routine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,19 @@ private string ScriptBase(Database db, string definition) {
after +=
$"{Environment.NewLine}{(Disabled ? "DISABLE" : "ENABLE")} TRIGGER [{Owner}].[{Name}] ON [{RelatedTableSchema}].[{RelatedTableName}]{Environment.NewLine}GO{Environment.NewLine}";

if (string.IsNullOrEmpty(definition))
definition = $"/* missing definition for {RoutineType} [{Owner}].[{Name}] */";
if (string.IsNullOrEmpty(definition))
definition = $"/* missing definition for {RoutineType} [{Owner}].[{Name}] */";
else
definition = RemoveExtraNewLines(definition);

return before + definition + after;
}

public string ScriptCreate() {
private static string RemoveExtraNewLines(string definition) {
return definition.Trim('\r', '\n');
}

public string ScriptCreate() {
return ScriptBase(Db, Text);
}

Expand Down
24 changes: 14 additions & 10 deletions model/Models/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ exec sp_executesql N'create schema [{Name}] authorization [{Owner}]'
}

public class Table : INameable, IHasOwner, IScriptable {
private const string _fieldSeparator = "\t";
private const string _escapeFieldSeparator = "--SchemaZenFieldSeparator--";
private const string _rowSeparator = "\r\n";
private const string _escapeRowSeparator = "--SchemaZenRowSeparator--";
private const string _nullValue = "--SchemaZenNull--";
private const string _rowSeparator = "\r\n";
private const string _tab = "\t";
private const string _escapeTab = "--SchemaZenTAB--";
private const string _carriageReturn = "\r";
private const string _escapeCarriageReturn = "--SchemaZenCR--";
private const string _lineFeed = "\n";
private const string _escapeLineFeed = "--SchemaZenLF--";
private const string _nullValue = "--SchemaZenNull--";
private const string _dateTimeFormat = "yyyy-MM-dd HH:mm:ss.FFFFFFF";

public const int RowsInBatch = 15000;
Expand Down Expand Up @@ -184,10 +187,11 @@ public void ExportData(string conn, TextWriter data, string tableHint = null) {
data.Write(((DateTime) dr[c.Name]).ToString(_dateTimeFormat, CultureInfo.InvariantCulture));
else
data.Write(dr[c.Name].ToString()
.Replace(_fieldSeparator, _escapeFieldSeparator)
.Replace(_rowSeparator, _escapeRowSeparator));
.Replace(_tab, _escapeTab)
.Replace(_lineFeed, _escapeLineFeed)
.Replace(_carriageReturn, _escapeCarriageReturn));
if (c != cols.Last())
data.Write(_fieldSeparator);
data.Write(_tab);
}
data.WriteLine();
}
Expand Down Expand Up @@ -243,14 +247,14 @@ public void ImportData(string conn, string filename) {
batch_rows++;

var row = dt.NewRow();
var fields = (new String(line.ToArray())).Split(new[] { _fieldSeparator }, StringSplitOptions.None);
var fields = (new String(line.ToArray())).Split(new[] { _tab }, StringSplitOptions.None);
if (fields.Length != dt.Columns.Count) {
throw new DataFileException("Incorrect number of columns", filename, linenumber);
}
for (var j = 0; j < fields.Length; j++) {
try {
row[j] = ConvertType(cols[j].Type,
fields[j].Replace(_escapeRowSeparator, _rowSeparator).Replace(_escapeFieldSeparator, _fieldSeparator));
fields[j].Replace(_escapeLineFeed, _lineFeed).Replace(_escapeCarriageReturn, _carriageReturn).Replace(_escapeTab, _tab));
} catch (FormatException ex) {
throw new DataFileException($"{ex.Message} at column {j + 1}", filename, linenumber);
}
Expand Down
1 change: 1 addition & 0 deletions model/Schemazen.Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<Compile Include="Models\Column.cs" />
<Compile Include="Models\ColumnList.cs" />
<Compile Include="ConfigHelper.cs" />
<Compile Include="Models\Comparers\ForeignKeyComparer.cs" />
<Compile Include="Models\Constraint.cs" />
<Compile Include="Models\ConstraintColumn.cs" />
<Compile Include="Models\Database.cs" />
Expand Down

0 comments on commit 9e7d5b1

Please sign in to comment.