Skip to content

Commit 2850b33

Browse files
committed
Add support for conditionals inside <iterate>
1 parent bc5f382 commit 2850b33

File tree

3 files changed

+137
-115
lines changed

3 files changed

+137
-115
lines changed

src/IBatisNet.DataMapper/Configuration/Sql/Dynamic/Handlers/ConditionalTagHandler.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,10 @@ protected long Compare(SqlTagContext ctx, SqlTag sqlTag, object parameterObject)
124124
Type type = null;
125125
if (propertyName != null && propertyName.Length > 0)
126126
{
127-
// check to see if we are a list
128-
//if (fpropertyName.StartsWith("["))
129-
//{
127+
IterateContext iterate = ctx.PeekIterateContext();
130128

131-
//}
129+
if (iterate != null)
130+
propertyName = propertyName.Replace("[]", "[" + (iterate.Index + 1) + "]");
132131

133132
value1 = ObjectProbe.GetMemberValue(parameterObject, propertyName, AccessorFactory);
134133
type = value1.GetType();

src/IBatisNet.DataMapper/Configuration/Sql/Dynamic/Handlers/IterateTagHandler.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public override int DoStartFragment(SqlTagContext ctx, SqlTag tag, object parame
7474
}
7575
iterate = new IterateContext(collection);
7676
ctx.AddAttribute(tag, iterate);
77+
78+
ctx.PushIterateContext(iterate);
7779
}
7880
if (iterate != null && iterate.HasNext)
7981
{
@@ -158,6 +160,8 @@ public override int DoEndFragment(SqlTagContext ctx, SqlTag tag,
158160
}
159161
else
160162
{
163+
ctx.PopIterateContext();
164+
161165
return BaseTagHandler.INCLUDE_BODY;
162166
}
163167
}

src/IBatisNet.DataMapper/Configuration/Sql/Dynamic/Handlers/SqlTagContext.cs

Lines changed: 130 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -30,121 +30,140 @@
3030
using System.Text;
3131
using IBatisNet.DataMapper.Configuration.ParameterMapping;
3232
using IBatisNet.DataMapper.Configuration.Sql.Dynamic.Elements;
33+
using System.Collections.Generic;
3334

3435
#endregion
3536

3637

3738
namespace IBatisNet.DataMapper.Configuration.Sql.Dynamic.Handlers
3839
{
39-
/// <summary>
40-
/// Summary description for SqlTagContext.
41-
/// </summary>
42-
public sealed class SqlTagContext
43-
{
44-
#region Fields
45-
private Hashtable _attributes = new Hashtable();
46-
private bool _overridePrepend = false;
47-
private SqlTag _firstNonDynamicTagWithPrepend = null;
48-
private ArrayList _parameterMappings = new ArrayList();
49-
private StringBuilder buffer = new StringBuilder();
50-
#endregion
51-
52-
53-
/// <summary>
54-
///
55-
/// </summary>
56-
public SqlTagContext()
57-
{
58-
_overridePrepend = false;
59-
}
60-
61-
/// <summary>
62-
///
63-
/// </summary>
64-
/// <returns></returns>
65-
public StringBuilder GetWriter()
66-
{
67-
return buffer;
68-
}
69-
70-
/// <summary>
71-
///
72-
/// </summary>
73-
public string BodyText
74-
{
75-
get
76-
{
77-
return buffer.ToString().Trim();
78-
}
79-
}
80-
81-
/// <summary>
82-
///
83-
/// </summary>
84-
public bool IsOverridePrepend
85-
{
86-
set
87-
{
88-
_overridePrepend = value;
89-
}
90-
get
91-
{
92-
return _overridePrepend;
93-
}
94-
}
95-
96-
/// <summary>
97-
///
98-
/// </summary>
99-
public SqlTag FirstNonDynamicTagWithPrepend
100-
{
101-
get
102-
{
103-
return _firstNonDynamicTagWithPrepend;
104-
}
105-
set
106-
{
107-
_firstNonDynamicTagWithPrepend = value;
108-
}
109-
}
110-
111-
112-
/// <summary>
113-
///
114-
/// </summary>
115-
/// <param name="key"></param>
116-
/// <param name="value"></param>
117-
public void AddAttribute(object key, object value)
118-
{
119-
_attributes.Add(key, value);
120-
}
121-
122-
/// <summary>
123-
///
124-
/// </summary>
125-
/// <param name="key"></param>
126-
/// <returns></returns>
127-
public object GetAttribute(object key)
128-
{
129-
return _attributes[key];
130-
}
131-
132-
/// <summary>
133-
///
134-
/// </summary>
135-
/// <param name="mapping"></param>
136-
public void AddParameterMapping(ParameterProperty mapping)
137-
{
138-
_parameterMappings.Add(mapping);
139-
}
140-
141-
/// <summary>
142-
///
143-
/// </summary>
144-
/// <returns></returns>
145-
public IList GetParameterMappings()
146-
{
147-
return _parameterMappings;
148-
}
149-
}
40+
/// <summary>
41+
/// Summary description for SqlTagContext.
42+
/// </summary>
43+
public sealed class SqlTagContext
44+
{
45+
#region Fields
46+
private Hashtable _attributes = new Hashtable();
47+
private bool _overridePrepend = false;
48+
private SqlTag _firstNonDynamicTagWithPrepend = null;
49+
private ArrayList _parameterMappings = new ArrayList();
50+
private StringBuilder buffer = new StringBuilder();
51+
Stack<IterateContext> _iterates = new Stack<IterateContext>();
52+
#endregion
53+
54+
/// <summary>
55+
///
56+
/// </summary>
57+
public SqlTagContext()
58+
{
59+
_overridePrepend = false;
60+
}
61+
62+
/// <summary>
63+
///
64+
/// </summary>
65+
/// <returns></returns>
66+
public StringBuilder GetWriter()
67+
{
68+
return buffer;
69+
}
70+
71+
/// <summary>
72+
///
73+
/// </summary>
74+
public string BodyText
75+
{
76+
get
77+
{
78+
return buffer.ToString().Trim();
79+
}
80+
}
81+
82+
/// <summary>
83+
///
84+
/// </summary>
85+
public bool IsOverridePrepend
86+
{
87+
set
88+
{
89+
_overridePrepend = value;
90+
}
91+
get
92+
{
93+
return _overridePrepend;
94+
}
95+
}
96+
97+
/// <summary>
98+
///
99+
/// </summary>
100+
public SqlTag FirstNonDynamicTagWithPrepend
101+
{
102+
get
103+
{
104+
return _firstNonDynamicTagWithPrepend;
105+
}
106+
set
107+
{
108+
_firstNonDynamicTagWithPrepend = value;
109+
}
110+
}
111+
112+
113+
/// <summary>
114+
///
115+
/// </summary>
116+
/// <param name="key"></param>
117+
/// <param name="value"></param>
118+
public void AddAttribute(object key, object value)
119+
{
120+
_attributes.Add(key, value);
121+
}
122+
123+
/// <summary>
124+
///
125+
/// </summary>
126+
/// <param name="key"></param>
127+
/// <returns></returns>
128+
public object GetAttribute(object key)
129+
{
130+
return _attributes[key];
131+
}
132+
133+
/// <summary>
134+
///
135+
/// </summary>
136+
/// <param name="mapping"></param>
137+
public void AddParameterMapping(ParameterProperty mapping)
138+
{
139+
_parameterMappings.Add(mapping);
140+
}
141+
142+
/// <summary>
143+
///
144+
/// </summary>
145+
/// <returns></returns>
146+
public IList GetParameterMappings()
147+
{
148+
return _parameterMappings;
149+
}
150+
151+
public void PushIterateContext(IterateContext iterate)
152+
{
153+
_iterates.Push(iterate);
154+
}
155+
156+
public IterateContext PeekIterateContext()
157+
{
158+
if (_iterates.Count > 0)
159+
return _iterates.Peek();
160+
else
161+
return null;
162+
}
163+
164+
public IterateContext PopIterateContext()
165+
{
166+
return _iterates.Pop();
167+
}
168+
}
150169
}

0 commit comments

Comments
 (0)