Skip to content

Commit

Permalink
add support Tag.Required
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang committed Mar 19, 2019
1 parent 4a5cc47 commit 3efed8a
Show file tree
Hide file tree
Showing 36 changed files with 70 additions and 25 deletions.
2 changes: 1 addition & 1 deletion build/version.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<VersionMajor>4</VersionMajor>
<VersionMinor>0</VersionMinor>
<VersionPatch>0-rc3</VersionPatch>
<VersionPatch>0-rc4</VersionPatch>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
</PropertyGroup>
</Project>
15 changes: 15 additions & 0 deletions doc/SmartSqlMap-v4.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
</xs:sequence>
<xs:attribute name="Prepend" type="xs:string" use="optional" />
<xs:attribute name="Property" type="xs:string" use="required" />
<xs:attribute name="Required" type="xs:boolean" use="optional" />
</xs:complexType>
</xs:element>
<xs:element name="IsEmpty">
Expand Down Expand Up @@ -250,6 +251,7 @@
</xs:choice>
<xs:attribute name="Prepend" type="xs:string" use="optional" />
<xs:attribute name="Property" type="xs:string" use="required" />
<xs:attribute name="Required" type="xs:boolean" use="optional" />
</xs:complexType>
</xs:element>
<xs:element name="IsEqual">
Expand Down Expand Up @@ -279,6 +281,7 @@
</xs:choice>
<xs:attribute name="Prepend" type="xs:string" use="optional" />
<xs:attribute name="Property" type="xs:string" use="required" />
<xs:attribute name="Required" type="xs:boolean" use="optional" />
<xs:attribute name="CompareValue" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
Expand Down Expand Up @@ -309,6 +312,7 @@
</xs:choice>
<xs:attribute name="Prepend" type="xs:string" use="optional" />
<xs:attribute name="Property" type="xs:string" use="required" />
<xs:attribute name="Required" type="xs:boolean" use="optional" />
<xs:attribute name="CompareValue" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
Expand Down Expand Up @@ -339,6 +343,7 @@
</xs:choice>
<xs:attribute name="Prepend" type="xs:string" use="optional" />
<xs:attribute name="Property" type="xs:string" use="required" />
<xs:attribute name="Required" type="xs:boolean" use="optional" />
<xs:attribute name="CompareValue" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
Expand Down Expand Up @@ -369,6 +374,7 @@
</xs:choice>
<xs:attribute name="Prepend" type="xs:string" use="optional" />
<xs:attribute name="Property" type="xs:string" use="required" />
<xs:attribute name="Required" type="xs:boolean" use="optional" />
<xs:attribute name="CompareValue" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
Expand Down Expand Up @@ -399,6 +405,7 @@
</xs:choice>
<xs:attribute name="Prepend" type="xs:string" use="optional" />
<xs:attribute name="Property" type="xs:string" use="required" />
<xs:attribute name="Required" type="xs:boolean" use="optional" />
<xs:attribute name="CompareValue" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
Expand Down Expand Up @@ -429,6 +436,7 @@
</xs:choice>
<xs:attribute name="Prepend" type="xs:string" use="optional" />
<xs:attribute name="Property" type="xs:string" use="required" />
<xs:attribute name="Required" type="xs:boolean" use="optional" />
</xs:complexType>
</xs:element>
<xs:element name="IsNotEqual">
Expand Down Expand Up @@ -458,6 +466,7 @@
</xs:choice>
<xs:attribute name="Prepend" type="xs:string" use="optional" />
<xs:attribute name="Property" type="xs:string" use="required" />
<xs:attribute name="Required" type="xs:boolean" use="optional" />
<xs:attribute name="CompareValue" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
Expand Down Expand Up @@ -488,6 +497,7 @@
</xs:choice>
<xs:attribute name="Prepend" type="xs:string" use="optional" />
<xs:attribute name="Property" type="xs:string" use="required" />
<xs:attribute name="Required" type="xs:boolean" use="optional" />
</xs:complexType>
</xs:element>
<xs:element name="IsNull">
Expand Down Expand Up @@ -517,6 +527,7 @@
</xs:choice>
<xs:attribute name="Prepend" type="xs:string" use="optional" />
<xs:attribute name="Property" type="xs:string" use="required" />
<xs:attribute name="Required" type="xs:boolean" use="optional" />
</xs:complexType>
</xs:element>
<xs:element name="IsTrue">
Expand Down Expand Up @@ -546,6 +557,7 @@
</xs:choice>
<xs:attribute name="Prepend" type="xs:string" use="optional" />
<xs:attribute name="Property" type="xs:string" use="required" />
<xs:attribute name="Required" type="xs:boolean" use="optional" />
</xs:complexType>
</xs:element>
<xs:element name="IsFalse">
Expand Down Expand Up @@ -575,6 +587,7 @@
</xs:choice>
<xs:attribute name="Prepend" type="xs:string" use="optional" />
<xs:attribute name="Property" type="xs:string" use="required" />
<xs:attribute name="Required" type="xs:boolean" use="optional" />
</xs:complexType>
</xs:element>
<xs:element name="IsProperty">
Expand Down Expand Up @@ -638,6 +651,7 @@
<xs:complexType mixed="true">
<xs:attribute name="Prepend" type="xs:string" use="optional" />
<xs:attribute name="Property" type="xs:string" use="required" />
<xs:attribute name="Required" type="xs:boolean" use="optional" />
</xs:complexType>
</xs:element>
<xs:element name="Dynamic">
Expand Down Expand Up @@ -748,6 +762,7 @@
</xs:choice>
<xs:attribute name="Prepend" type="xs:string" use="optional" />
<xs:attribute name="Property" type="xs:string" use="required" />
<xs:attribute name="Required" type="xs:boolean" use="optional" />
<xs:attribute name="Open" type="xs:string" use="required" />
<xs:attribute name="Separator" type="xs:string" use="required" />
<xs:attribute name="Close" type="xs:string" use="required" />
Expand Down
6 changes: 3 additions & 3 deletions src/SmartSql/Configuration/Tags/For.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class For : Tag

public override bool IsCondition(RequestContext context)
{
var reqVal = GetPropertyValue(context);
var reqVal = EnsurePropertyValue(context);
if (reqVal == null) { return false; }
if (reqVal is IEnumerable)
{
Expand All @@ -27,7 +27,7 @@ public override bool IsCondition(RequestContext context)
public override void BuildChildSql(RequestContext context)
{
context.SqlBuilder.Append(Open);
var reqVal = (GetPropertyValue(context) as IEnumerable).GetEnumerator();
var reqVal = (EnsurePropertyValue(context) as IEnumerable).GetEnumerator();
reqVal.MoveNext();
if (string.IsNullOrEmpty(Key))
{
Expand All @@ -47,7 +47,7 @@ public override void BuildChildSql(RequestContext context)
}
private void BuildItemSql(string itemSqlStr, RequestContext context)
{
var reqVal = (GetPropertyValue(context) as IEnumerable);
var reqVal = (EnsurePropertyValue(context) as IEnumerable);
int item_index = 0;
string dbPrefix = GetDbProviderPrefix(context);
foreach (var itemVal in reqVal)
Expand Down
2 changes: 1 addition & 1 deletion src/SmartSql/Configuration/Tags/IsEmpty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class IsEmpty : Tag
{
public override bool IsCondition(RequestContext context)
{
Object reqVal = GetPropertyValue(context);
Object reqVal = EnsurePropertyValue(context);
if (reqVal == null)
{
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/SmartSql/Configuration/Tags/IsEqual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class IsEqual : StringCompareTag
{
public override bool IsCondition(RequestContext context)
{
var reqVal = GetPropertyValue(context);
var reqVal = EnsurePropertyValue(context);
if (reqVal == null) { return false; }
string reqValStr = string.Empty;
if (reqVal is Enum)
Expand Down
2 changes: 1 addition & 1 deletion src/SmartSql/Configuration/Tags/IsFalse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class IsFalse : Tag
{
public override bool IsCondition(RequestContext context)
{
Object reqVal = GetPropertyValue(context);
Object reqVal = EnsurePropertyValue(context);
if (reqVal is Boolean)
{
return (bool)reqVal == false;
Expand Down
2 changes: 1 addition & 1 deletion src/SmartSql/Configuration/Tags/IsGreaterEqual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class IsGreaterEqual : NumericalCompareTag
{
public override bool IsCondition(RequestContext context)
{
Object reqVal = GetPropertyValue(context);
Object reqVal = EnsurePropertyValue(context);
if (reqVal == null) { return false; }
Decimal reqValNum = 0M;
if (reqVal is Enum)
Expand Down
2 changes: 1 addition & 1 deletion src/SmartSql/Configuration/Tags/IsGreaterThan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class IsGreaterThan : NumericalCompareTag
{
public override bool IsCondition(RequestContext context)
{
Object reqVal = GetPropertyValue(context);
Object reqVal = EnsurePropertyValue(context);
if (reqVal == null) { return false; }

Decimal reqValNum = 0M;
Expand Down
2 changes: 1 addition & 1 deletion src/SmartSql/Configuration/Tags/IsLessEqual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class IsLessEqual : NumericalCompareTag
{
public override bool IsCondition(RequestContext context)
{
Object reqVal = GetPropertyValue(context);
Object reqVal = EnsurePropertyValue(context);
if (reqVal == null) { return false; }
Decimal reqValNum = 0M;
if (reqVal is Enum)
Expand Down
2 changes: 1 addition & 1 deletion src/SmartSql/Configuration/Tags/IsLessThan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class IsLessThan : NumericalCompareTag
{
public override bool IsCondition(RequestContext context)
{
Object reqVal = GetPropertyValue(context);
Object reqVal = EnsurePropertyValue(context);
if (reqVal == null) { return false; }
Decimal reqValNum = 0M;
if (reqVal is Enum)
Expand Down
2 changes: 1 addition & 1 deletion src/SmartSql/Configuration/Tags/IsNotEmpty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class IsNotEmpty : Tag
{
public override bool IsCondition(RequestContext context)
{
Object reqVal = GetPropertyValue(context);
Object reqVal = EnsurePropertyValue(context);
if (reqVal == null)
{
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/SmartSql/Configuration/Tags/IsNotEqual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class IsNotEqual : StringCompareTag
{
public override bool IsCondition(RequestContext context)
{
Object reqVal = GetPropertyValue(context);
Object reqVal = EnsurePropertyValue(context);
if (reqVal == null) { return false; }
string reqValStr = string.Empty;
if (reqVal is Enum)
Expand Down
2 changes: 1 addition & 1 deletion src/SmartSql/Configuration/Tags/IsNotNull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class IsNotNull : Tag
{
public override bool IsCondition(RequestContext context)
{
Object reqVal = GetPropertyValue(context);
Object reqVal = EnsurePropertyValue(context);
return reqVal != null;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/SmartSql/Configuration/Tags/IsNull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class IsNull : Tag
{
public override bool IsCondition(RequestContext context)
{
Object reqVal = GetPropertyValue(context);
Object reqVal = EnsurePropertyValue(context);
return reqVal == null;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/SmartSql/Configuration/Tags/IsTrue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class IsTrue : Tag
{
public override bool IsCondition(RequestContext context)
{
Object reqVal = GetPropertyValue(context);
Object reqVal = EnsurePropertyValue(context);
if (reqVal is Boolean)
{
return (bool)reqVal == true;
Expand Down
2 changes: 1 addition & 1 deletion src/SmartSql/Configuration/Tags/Placeholder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public override void BuildSql(RequestContext context)
{
if (IsCondition(context))
{
Object reqVal = GetPropertyValue(context);
Object reqVal = EnsurePropertyValue(context);
context.SqlBuilder.Append($"{Prepend}{reqVal}");
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/SmartSql/Configuration/Tags/Switch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class Switch : Tag
{
public override bool IsCondition(RequestContext context)
{
EnsurePropertyValue(context);
return true;
}
public override void BuildSql(RequestContext context)
Expand Down Expand Up @@ -35,7 +36,7 @@ public class Case : StringCompareTag
{
public override bool IsCondition(RequestContext context)
{
var reqVal = GetPropertyValue(context);
var reqVal = EnsurePropertyValue(context);
if (reqVal == null) { return false; }
string reqValStr = string.Empty;
if (reqVal is Enum)
Expand Down
12 changes: 9 additions & 3 deletions src/SmartSql/Configuration/Tags/Tag.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using SmartSql.Exceptions;
using System;
using System.Collections.Generic;

namespace SmartSql.Configuration.Tags
Expand All @@ -7,6 +8,7 @@ public abstract class Tag : ITag
{
public virtual String Prepend { get; set; }
public String Property { get; set; }
public bool Required { get; set; }
public IList<ITag> ChildTags { get; set; }
public ITag Parent { get; set; }
public Statement Statement { get; set; }
Expand Down Expand Up @@ -45,9 +47,13 @@ protected virtual String GetDbProviderPrefix(RequestContext context)
return context.ExecutionContext.SmartSqlConfig.Settings.ParameterPrefix;
}

protected virtual Object GetPropertyValue(RequestContext context)
protected virtual Object EnsurePropertyValue(RequestContext context)
{
context.Parameters.TryGetParameterValue(Property, out object paramVal);
var existProperty = context.Parameters.TryGetParameterValue(Property, out object paramVal);
if (Required && !existProperty)
{
throw new SmartSqlException($"Statement:{Statement.FullSqlId} Tag:{Property} Required fail.");
}
return paramVal;
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/SmartSql/Configuration/Tags/TagBuilders/AbstractTagBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ namespace SmartSql.Configuration.Tags.TagBuilders
{
public abstract class AbstractTagBuilder : ITagBuilder
{
private const String PREPEND = "Prepend";
private const String PROPERTY = "Property";
private const String COMPARE_VALUE = "CompareValue";
private const String PREPEND = nameof(Tag.Prepend);
private const String PROPERTY = nameof(Tag.Property);
private const String REQUIRED = nameof(Tag.Required);
private const String COMPARE_VALUE = nameof(NumericalCompareTag.CompareValue);
public abstract ITag Build(XmlNode xmlNode, Statement statement);

public String GetXmlAttributeValue(XmlNode xmlNode, string attributeName)
Expand All @@ -22,7 +23,7 @@ public Decimal GetXmlAttributeValueAsDecimal(XmlNode xmlNode, string attributeNa
string strVal = GetXmlAttributeValue(xmlNode, attributeName);
if (!Decimal.TryParse(strVal, out decimal decimalVal))
{
throw new SmartSqlException();
throw new SmartSqlException($"can not convert {strVal} to decimal from xml-node:{xmlNode.Value}.");
}
return decimalVal;
}
Expand All @@ -35,6 +36,11 @@ public String GetProperty(XmlNode xmlNode)
{
return GetXmlAttributeValue(xmlNode, PROPERTY);
}
public bool GetRequired(XmlNode xmlNode)
{
xmlNode.Attributes.TryGetValueAsBoolean(REQUIRED, out var requiredVal);
return requiredVal;
}
public String GetCompareValue(XmlNode xmlNode)
{
return GetXmlAttributeValue(xmlNode, COMPARE_VALUE);
Expand Down
1 change: 1 addition & 0 deletions src/SmartSql/Configuration/Tags/TagBuilders/ForBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public override ITag Build(XmlNode xmlNode, Statement statement)
{
Property = GetProperty(xmlNode),
Prepend = GetPrepend(xmlNode),
Required = GetRequired(xmlNode),
Open = GetXmlAttributeValue(xmlNode, nameof(For.Open)),
Close = GetXmlAttributeValue(xmlNode, nameof(For.Close)),
Separator = GetXmlAttributeValue(xmlNode, nameof(For.Separator)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public override ITag Build(XmlNode xmlNode, Statement statement)
{
Property = GetProperty(xmlNode),
Prepend = GetPrepend(xmlNode),
Required = GetRequired(xmlNode),
ChildTags = new List<ITag>(),
Statement = statement
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public override ITag Build(XmlNode xmlNode, Statement statement)
{
Property = GetProperty(xmlNode),
Prepend = GetPrepend(xmlNode),
Required = GetRequired(xmlNode),
CompareValue = GetCompareValue(xmlNode),
ChildTags = new List<ITag>(),
Statement = statement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public override ITag Build(XmlNode xmlNode, Statement statement)
{
Property = GetProperty(xmlNode),
Prepend = GetPrepend(xmlNode),
Required = GetRequired(xmlNode),
ChildTags = new List<ITag>(),
Statement = statement
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public override ITag Build(XmlNode xmlNode, Statement statement)
{
Property = GetProperty(xmlNode),
Prepend = GetPrepend(xmlNode),
Required = GetRequired(xmlNode),
CompareValue = GetCompareValueAsDecimal(xmlNode),
ChildTags = new List<ITag>(),
Statement = statement
Expand Down
Loading

0 comments on commit 3efed8a

Please sign in to comment.