Skip to content

Commit

Permalink
add support Dynamic & Set & Where & Include Required check.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang committed Mar 20, 2019
1 parent 3efed8a commit 15121ee
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 34 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-rc4</VersionPatch>
<VersionPatch>0-rc5</VersionPatch>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
</PropertyGroup>
</Project>
4 changes: 4 additions & 0 deletions doc/SmartSqlMap-v4.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
<xs:complexType mixed="true">
<xs:attribute name="RefId" type="xs:string" use="required" />
<xs:attribute name="Prepend" type="xs:string" use="optional" />
<xs:attribute name="Required" type="xs:boolean" use="optional" />
</xs:complexType>
</xs:element>
<xs:element name="Case">
Expand Down Expand Up @@ -681,6 +682,7 @@
<xs:element ref="Env" />
</xs:choice>
<xs:attribute name="Prepend" type="xs:string" use="optional" />
<xs:attribute name="Required" type="xs:boolean" use="optional" />
</xs:complexType>
</xs:element>
<xs:element name="Where">
Expand Down Expand Up @@ -708,6 +710,7 @@
<xs:element ref="For" />
<xs:element ref="Env" />
</xs:choice>
<xs:attribute name="Required" type="xs:boolean" use="optional" />
</xs:complexType>
</xs:element>
<xs:element name="Set">
Expand All @@ -733,6 +736,7 @@
<xs:element ref="For" />
<xs:element ref="Env" />
</xs:choice>
<xs:attribute name="Required" type="xs:boolean" use="optional" />
</xs:complexType>
</xs:element>
<xs:element name="For">
Expand Down
37 changes: 36 additions & 1 deletion src/SmartSql.Test.Unit/DbSessions/DbSessionTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SmartSql.Reflection;
using SmartSql.Configuration.Tags;
using SmartSql.Reflection;
using SmartSql.Test.Entities;
using System;
using System.Threading.Tasks;
Expand Down Expand Up @@ -182,6 +183,40 @@ public void Delete()
}
});
}
[Fact]
public void DeleteCheckIncludeRequired()
{
try
{
var id = DbSession.ExecuteScalar<int>(new RequestContext
{
Scope = nameof(AllPrimitive),
SqlId = "DeleteCheckIncludeRequired",
Request = new { }
});
}
catch (TagRequiredFailException ex)
{
Assert.True(true);
}
}
[Fact]
public void DeleteCheckIsNotEmptyRequired()
{
try
{
var id = DbSession.ExecuteScalar<int>(new RequestContext
{
Scope = nameof(AllPrimitive),
SqlId = "DeleteCheckIsNotEmptyRequired",
Request = new { }
});
}
catch (TagRequiredFailException ex)
{
Assert.True(true);
}
}

}
}
24 changes: 22 additions & 2 deletions src/SmartSql.Test.Unit/Maps/AllPrimitive.xml
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,27 @@
SELECT Top 6 T.* From T_AllPrimitive T;
SELECT Top 8 T.* From T_AllPrimitive T;
</Statement>


<Statement Id="DeleteQueryParams">
<Where>
<IsNotEmpty Prepend="And" Property="Id">
Id=@Id
</IsNotEmpty>
<IsNotEmpty Prepend="And" Property="Name">
Name=@Name
</IsNotEmpty>
</Where>
</Statement>
<Statement Id="DeleteCheckIncludeRequired">
Delete From T_AllPrimitive
<Include RefId="DeleteQueryParams" Required="true"/>
</Statement>
<Statement Id="DeleteCheckIsNotEmptyRequired">
Delete From T_AllPrimitive
<Where>
<IsNotEmpty Prepend="And" Property="Id" Required="true">
Id=@Id
</IsNotEmpty>
</Where>
</Statement>
</Statements>
</SmartSqlMap>
2 changes: 1 addition & 1 deletion src/SmartSql.Test.Unit/Maps/T_Entity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@
(10)
</IsNull>-->
T.* From T_Entity T With(NoLock)
<Include RefId="QueryParams"/>
<Include RefId="QueryParams" />
<Switch Prepend="Order By" Property="OrderBy">
<Default>
T.FLong Desc
Expand Down
2 changes: 1 addition & 1 deletion src/SmartSql.Test.Unit/Utils/ResourceUtilTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ResourceUtilTest
public void LoadUriAsXml()
{
var smartSqlConfig_github =
"https://raw.githubusercontent.com/Smart-Kit/SmartSql/master/doc/Xml.Template/SmartSqlMapConfig.xml";
"https://raw.githubusercontent.com/Smart-Kit/SmartSql/master/src/SmartSql.Test.Unit/SmartSqlMapConfig.xml";
Uri uri=new Uri(smartSqlConfig_github);
var xml = ResourceUtil.LoadUriAsXml(uri);
Assert.NotNull(xml);
Expand Down
50 changes: 27 additions & 23 deletions src/SmartSql/Configuration/Tags/Dynamic.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using SmartSql.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartSql.Configuration.Tags
Expand All @@ -8,7 +10,12 @@ public class Dynamic : Tag
{
public override bool IsCondition(RequestContext context)
{
return true;
var passed = ChildTags.Any(childTag => childTag.IsCondition(context));
if (Required && !passed)
{
throw new TagRequiredFailException(this);
}
return passed;
}
public override void BuildSql(RequestContext context)
{
Expand All @@ -17,31 +24,28 @@ public override void BuildSql(RequestContext context)

public override void BuildChildSql(RequestContext context)
{
if (ChildTags != null && ChildTags.Count > 0)
bool isFirstChild = true;
foreach (var childTag in ChildTags)
{
bool isFirstChild = true;
foreach (var childTag in ChildTags)
if (!childTag.IsCondition(context))
{
if (!childTag.IsCondition(context))
{
continue;
}
if (isFirstChild)
{
isFirstChild = false;
context.SqlBuilder.Append(" ");
context.SqlBuilder.Append(Prepend);
context.SqlBuilder.Append(" ");
if (!(childTag is SqlText))
{
context.IgnorePrepend = true;
}
childTag.BuildSql(context);
}
else
continue;
}
if (isFirstChild)
{
isFirstChild = false;
context.SqlBuilder.Append(" ");
context.SqlBuilder.Append(Prepend);
context.SqlBuilder.Append(" ");
if (!(childTag is SqlText))
{
childTag.BuildSql(context);
context.IgnorePrepend = true;
}
childTag.BuildSql(context);
}
else
{
childTag.BuildSql(context);
}
}
}
Expand Down
29 changes: 26 additions & 3 deletions src/SmartSql/Configuration/Tags/Include.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;
using System.Text;

Expand All @@ -10,11 +11,33 @@ public class Include : Tag
public Statement Ref { get; set; }
public override void BuildSql(RequestContext context)
{
context.SqlBuilder.Append(Prepend);
Ref.BuildSql(context);
if (IsCondition(context))
{
context.SqlBuilder.Append(Prepend);
Ref.BuildSql(context);
}
}
public override bool IsCondition(RequestContext context)
{
if (!Required)
{
return true;
}

bool passed = false;

foreach (var childTag in Ref.SqlTags)
{
if (childTag.IsCondition(context))
{
passed = true;
break;
}
}
if (!passed)
{
throw new TagRequiredFailException(this);
}
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/SmartSql/Configuration/Tags/Tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected virtual Object EnsurePropertyValue(RequestContext context)
var existProperty = context.Parameters.TryGetParameterValue(Property, out object paramVal);
if (Required && !existProperty)
{
throw new SmartSqlException($"Statement:{Statement.FullSqlId} Tag:{Property} Required fail.");
throw new TagRequiredFailException(this);
}
return paramVal;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public override ITag Build(XmlNode xmlNode, Statement statement)
return new Dynamic
{
Prepend = GetPrepend(xmlNode),
Required = GetRequired(xmlNode),
Statement = statement,
ChildTags = new List<ITag>()
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public override ITag Build(XmlNode xmlNode, Statement statement)
{
RefId = refId,
Prepend = GetPrepend(xmlNode),
Required = GetRequired(xmlNode),
Statement = statement
};
statement.IncludeDependencies.Add(includeTag);
Expand Down
1 change: 1 addition & 0 deletions src/SmartSql/Configuration/Tags/TagBuilders/SetBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public override ITag Build(XmlNode xmlNode, Statement statement)
return new Set
{
ChildTags = new List<ITag>(),
Required = GetRequired(xmlNode),
Statement = statement
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public override ITag Build(XmlNode xmlNode, Statement statement)
{
Property = GetProperty(switchNode),
Prepend = GetPrepend(switchNode),
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 @@ -12,6 +12,7 @@ public override ITag Build(XmlNode xmlNode, Statement statement)
return new Where
{
ChildTags = new List<ITag>(),
Required = GetRequired(xmlNode),
Statement = statement
};
}
Expand Down
17 changes: 17 additions & 0 deletions src/SmartSql/Configuration/Tags/TagRequiredFailException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using SmartSql.Exceptions;
using System;
using System.Collections.Generic;
using System.Text;

namespace SmartSql.Configuration.Tags
{
public class TagRequiredFailException : SmartSqlException
{
public TagRequiredFailException(Tag tag)
: base($"Statement:{tag.Statement.FullSqlId} Tag:{tag.GetType().Name} Required fail.")
{

}

}
}

0 comments on commit 15121ee

Please sign in to comment.