From 7fe3cdc92e1946c0a8405fa33a451f6f841e9695 Mon Sep 17 00:00:00 2001 From: Daniel Cohen Gindi Date: Sat, 4 Feb 2017 19:53:03 +0200 Subject: [PATCH] Added CASE phrase --- .../source.extension.vsixmanifest | 2 +- dg.Sql/Sql/Phrases/FlowControl/Case.cs | 170 ++++++++++++++++++ .../{General => FlowControl}/IfNull.cs | 0 dg.Sql/Sql/Phrases/PhraseHelper.cs | 38 +++- dg.Sql/dg.Sql.csproj | 3 +- 5 files changed, 209 insertions(+), 4 deletions(-) create mode 100644 dg.Sql/Sql/Phrases/FlowControl/Case.cs rename dg.Sql/Sql/Phrases/{General => FlowControl}/IfNull.cs (100%) diff --git a/dg.Sql.SchemeGenerator.VSIX/dg.Sql.SchemeGenerator.VSIX/source.extension.vsixmanifest b/dg.Sql.SchemeGenerator.VSIX/dg.Sql.SchemeGenerator.VSIX/source.extension.vsixmanifest index 5fca71c5..780fcaf7 100644 --- a/dg.Sql.SchemeGenerator.VSIX/dg.Sql.SchemeGenerator.VSIX/source.extension.vsixmanifest +++ b/dg.Sql.SchemeGenerator.VSIX/dg.Sql.SchemeGenerator.VSIX/source.extension.vsixmanifest @@ -3,7 +3,7 @@ dg.Sql Scheme Generator Daniel Cohen Gindi - 1.3.5.0 + 1.3.6.0 A DAL code generator for dg.Sql. Takes a pseudo-script, and outputs a full C# code. 1033 https://github.com/danielgindi/dg.Sql diff --git a/dg.Sql/Sql/Phrases/FlowControl/Case.cs b/dg.Sql/Sql/Phrases/FlowControl/Case.cs new file mode 100644 index 00000000..f9a6ae64 --- /dev/null +++ b/dg.Sql/Sql/Phrases/FlowControl/Case.cs @@ -0,0 +1,170 @@ +using System; +using System.Collections.Generic; +using System.Text; +using dg.Sql.Connector; + +namespace dg.Sql.Phrases +{ + public class Case : IPhrase + { + public class WhenClause + { + public ValueWrapper When; + public ValueWrapper Then; + } + + public ValueWrapper Value; + public List Conditions = new List(); + public ValueWrapper ElseValue; + + private WhenClause _CurrentWhen; + + #region Constructors + + public Case() + { + } + + public Case(ValueWrapper value) + { + this.Value = value; + } + + public Case(Where condition) + { + this.Value = new ValueWrapper(condition, ValueObjectType.Value); + } + + public Case(WhereList condition) + { + this.Value = new ValueWrapper(condition, ValueObjectType.Value); + } + + public Case(string tableName, string columnName) + { + this.Value = new ValueWrapper(tableName, columnName); + } + + public Case(object value, ValueObjectType valueType) + { + this.Value = new ValueWrapper(value, valueType); + } + + #endregion + + #region Builder + + public Case When(ValueWrapper value) + { + this._CurrentWhen = new WhenClause + { + When = value, + Then = null + }; + return this; + } + + public Case When(Where condition) + { + return When(new ValueWrapper(condition, ValueObjectType.Value)); + } + + public Case When(WhereList condition) + { + return When(new ValueWrapper(condition, ValueObjectType.Value)); + } + + public Case When(string tableName, string columnName) + { + return When(new ValueWrapper(tableName, columnName)); + } + + public Case When(object value, ValueObjectType valueType) + { + return When(new ValueWrapper(value, valueType)); + } + + public Case When(IPhrase value) + { + return When(new ValueWrapper(value)); + } + + public Case Then(ValueWrapper value) + { + if (this._CurrentWhen != null) + { + this._CurrentWhen.Then = value; + Conditions.Add(this._CurrentWhen); + this._CurrentWhen = null; + } + else if (Conditions.Count > 0) + { + Conditions[Conditions.Count - 1].Then = value; + } + return this; + } + + public Case Then(string tableName, string columnName) + { + return Then(new ValueWrapper(tableName, columnName)); + } + + public Case Then(object value, ValueObjectType valueType) + { + return Then(new ValueWrapper(value, valueType)); + } + + public Case Then(IPhrase value) + { + return Then(new ValueWrapper(value)); + } + + public Case Else(ValueWrapper value) + { + this.ElseValue = value; + return this; + } + + public Case Else(string tableName, string columnName) + { + return Else(new ValueWrapper(tableName, columnName)); + } + + public Case Else(object value, ValueObjectType valueType) + { + return Else(new ValueWrapper(value, valueType)); + } + + public Case Else(IPhrase value) + { + return Else(new ValueWrapper(value)); + } + + #endregion + + public string BuildPhrase(ConnectorBase conn, Query relatedQuery = null) + { + string ret = "CASE"; + + if (Value != null) + { + ret += " " + Value.Build(conn, relatedQuery); + } + + foreach (var when in Conditions) + { + ret += " WHEN " + (when.When == null ? "NULL" : when.When.Build(conn, relatedQuery)); + ret += " THEN " + (when.Then == null ? "NULL" : when.Then.Build(conn, relatedQuery)); + } + + if (ElseValue != null) + { + ret += " ELSE " + ElseValue.Build(conn, relatedQuery); + } + + ret += " END"; + + return ret; + } + } +} diff --git a/dg.Sql/Sql/Phrases/General/IfNull.cs b/dg.Sql/Sql/Phrases/FlowControl/IfNull.cs similarity index 100% rename from dg.Sql/Sql/Phrases/General/IfNull.cs rename to dg.Sql/Sql/Phrases/FlowControl/IfNull.cs diff --git a/dg.Sql/Sql/Phrases/PhraseHelper.cs b/dg.Sql/Sql/Phrases/PhraseHelper.cs index de4a9d18..7135981f 100644 --- a/dg.Sql/Sql/Phrases/PhraseHelper.cs +++ b/dg.Sql/Sql/Phrases/PhraseHelper.cs @@ -260,7 +260,37 @@ public static JsonExtract JsonExtract(Where where, string path = "$") #endregion - #region General + #region Flow Control + + public static Case Case() + { + return new Case(); + } + + public static Case Case(ValueWrapper value) + { + return new Case(value); + } + + public static Case Case(Where condition) + { + return new Case(condition); + } + + public static Case Case(WhereList condition) + { + return new Case(condition); + } + + public static Case Case(string tableName, string columnName) + { + return new Case(tableName, columnName); + } + + public static Case Case(object value, ValueObjectType valueType) + { + return new Case(value, valueType); + } public static IfNull IfNull( string firstTableName, string firstColumnName, @@ -268,7 +298,7 @@ public static IfNull IfNull( { return new IfNull( firstTableName, firstColumnName, - secondTableName, secondColumnName); + secondTableName, secondColumnName); } public static IfNull IfNull( @@ -298,6 +328,10 @@ public static IfNull IfNull( secondTableName, secondColumnName); } + #endregion + + #region General + public static RandWeight RandWeight(object value, ValueObjectType valueType) { return new RandWeight(value, valueType); diff --git a/dg.Sql/dg.Sql.csproj b/dg.Sql/dg.Sql.csproj index 185ed1a0..ca7a282b 100644 --- a/dg.Sql/dg.Sql.csproj +++ b/dg.Sql/dg.Sql.csproj @@ -76,6 +76,7 @@ + @@ -147,7 +148,7 @@ - +