Skip to content

Commit

Permalink
Added CASE phrase
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Feb 4, 2017
1 parent 01f8c88 commit 7fe3cdc
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Identifier Id="0a283a43-25bf-454c-b5eb-3571c5c1864d">
<Name>dg.Sql Scheme Generator</Name>
<Author>Daniel Cohen Gindi</Author>
<Version>1.3.5.0</Version>
<Version>1.3.6.0</Version>
<Description xml:space="preserve">A DAL code generator for dg.Sql. Takes a pseudo-script, and outputs a full C# code.</Description>
<Locale>1033</Locale>
<MoreInfoUrl>https://github.com/danielgindi/dg.Sql</MoreInfoUrl>
Expand Down
170 changes: 170 additions & 0 deletions dg.Sql/Sql/Phrases/FlowControl/Case.cs
Original file line number Diff line number Diff line change
@@ -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<WhenClause> Conditions = new List<WhenClause>();
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;
}
}
}
File renamed without changes.
38 changes: 36 additions & 2 deletions dg.Sql/Sql/Phrases/PhraseHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,45 @@ 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,
string secondTableName, string secondColumnName)
{
return new IfNull(
firstTableName, firstColumnName,
secondTableName, secondColumnName);
secondTableName, secondColumnName);
}

public static IfNull IfNull(
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion dg.Sql/dg.Sql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<ItemGroup>
<Compile Include="Sql\Base Types\ValueWrapper.cs" />
<Compile Include="Sql\Phrases\Aggregate\GroupConcat.cs" />
<Compile Include="Sql\Phrases\FlowControl\Case.cs" />
<Compile Include="Sql\Phrases\Hash\SHA1.cs" />
<Compile Include="Sql\Phrases\Hash\MD5.cs" />
<Compile Include="Sql\Phrases\Json\JsonExtract.cs" />
Expand Down Expand Up @@ -147,7 +148,7 @@
<Compile Include="Sql\Phrases\Aggregate\Count.cs" />
<Compile Include="Sql\Phrases\General\RandWeight.cs" />
<Compile Include="Sql\QueryKeywords\GroupBy.cs" />
<Compile Include="Sql\Phrases\General\IfNull.cs" />
<Compile Include="Sql\Phrases\FlowControl\IfNull.cs" />
<Compile Include="Sql\Schema\TableSchema.ForeignKey.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Sql\RecordEntity\AbstractRecord.cs" />
Expand Down

0 comments on commit 7fe3cdc

Please sign in to comment.