Skip to content

Commit

Permalink
Merge pull request #127 from mobilebilly/feature/persisted-computed-c…
Browse files Browse the repository at this point in the history
…olumn

Support persisted computed column
  • Loading branch information
sethreno authored Mar 13, 2017
2 parents 3b10457 + 9e7d5b1 commit 0e0ad52
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
18 changes: 11 additions & 7 deletions model/Models/Column.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,21 @@ public string IdentityText {
}
}

public string RowGuidColText => IsRowGuidCol ? " ROWGUIDCOL " : string.Empty;
public string RowGuidColText => IsRowGuidCol ? " ROWGUIDCOL " : string.Empty;

public bool Persisted { get; set; }

public ColumnDiff Compare(Column c) {
public ColumnDiff Compare(Column c) {
return new ColumnDiff(this, c);
}

private string ScriptBase(bool includeDefaultConstraint) {
if (!string.IsNullOrEmpty(ComputedDefinition))
return $"[{Name}] AS {ComputedDefinition}";
if (!string.IsNullOrEmpty(ComputedDefinition)) {
var persistedSetting = Persisted ? " PERSISTED" : string.Empty;
return $"[{Name}] AS {ComputedDefinition}{persistedSetting}";
}

var val = new StringBuilder($"[{Name}] [{Type}]");
var val = new StringBuilder($"[{Name}] [{Type}]");

switch (Type) {
case "bigint":
Expand Down Expand Up @@ -171,9 +175,9 @@ public ColumnDiff(Column target, Column source) {

private bool IsDiffBase => Source.IsNullable != Target.IsNullable || Source.Length != Target.Length ||
Source.Position != Target.Position || Source.Type != Target.Type || Source.Precision != Target.Precision ||
Source.Scale != Target.Scale || Source.ComputedDefinition != Target.ComputedDefinition;
Source.Scale != Target.Scale || Source.ComputedDefinition != Target.ComputedDefinition || Source.Persisted != Target.Persisted;

public bool DefaultIsDiff => Source.DefaultText != Target.DefaultText;
public bool DefaultIsDiff => Source.DefaultText != Target.DefaultText;

public bool OnlyDefaultIsDiff => DefaultIsDiff && !IsDiffBase;

Expand Down
9 changes: 6 additions & 3 deletions model/Models/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -669,14 +669,17 @@ private void LoadColumnComputes(SqlCommand cm) {
object_schema_name(object_id) as TABLE_SCHEMA,
object_name(object_id) as TABLE_NAME,
name as COLUMN_NAME,
definition as DEFINITION
definition as DEFINITION,
is_persisted as PERSISTED
from sys.computed_columns cc
";
using (var dr = cm.ExecuteReader()) {
while (dr.Read()) {
var t = FindTable((string)dr["TABLE_NAME"], (string)dr["TABLE_SCHEMA"]);
t.Columns.Find((string)dr["COLUMN_NAME"]).ComputedDefinition = (string)dr["DEFINITION"];
}
var column = t.Columns.Find((string) dr["COLUMN_NAME"]);
column.ComputedDefinition = (string)dr["DEFINITION"];
column.Persisted = (bool)dr["PERSISTED"];
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions test/ColumnTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ public void no_trailing_space_with_no_name_default()
Assert.AreEqual(" DEFAULT 0", lines[1]);
}



[Test]
public void computed_column()
{
var c = new Column("test", "int", false, null) {ComputedDefinition = "(A + B)"};

Assert.AreEqual("[test] AS (A + B)", c.ScriptCreate());
}

[Test]
public void computed_column_persisted()
{
var c = new Column("test", "int", false, null) {ComputedDefinition = "(A + B)", Persisted = true};

Assert.AreEqual("[test] AS (A + B) PERSISTED", c.ScriptCreate());
}
}
}
}

0 comments on commit 0e0ad52

Please sign in to comment.