forked from actiontech/mybatis-mapper-2-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_ibatis_test.go
199 lines (168 loc) · 5.92 KB
/
parser_ibatis_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package parser
import "testing"
func TestIbatis(t *testing.T) {
testParser(t, `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Employee">
<select id="findByID" resultClass="Employee">
SELECT * FROM EMPLOYEE
<dynamic prepend="WHERE ">
<isNull property="id" prepend="AND ">
id IS NULL
</isNull>
<isNotNull property="id" prepend="OR ">
id = #id#
</isNotNull>
</dynamic>
</select>
</sqlMap>`,
"SELECT * FROM `EMPLOYEE` WHERE `id` IS NULL OR `id`=?;")
testParser(t, `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Employee">
<select id="findByID" resultClass="Employee">
SELECT * FROM EMPLOYEE
<dynamic prepend="WHERE ">
<iterate prepend="AND" property="UserNameList"
open="(" close=")" conjunction="OR">
username=#UserNameList[]#
</iterate>
<isNull property="id">
id IS NULL
</isNull>
<isNotNull property="id">
id = #id#
</isNotNull>
</dynamic>
</select>
</sqlMap>`,
"SELECT * FROM `EMPLOYEE` WHERE (`username`=? OR `username`=?) AND `id` IS NULL AND `id`=?;")
}
func TestParseIBatisInclude(t *testing.T) {
testParserQuery(t, false, `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Employee">
<sql id="selectItem_fragment">
FROM items
WHERE parentid = 6
</sql>
<select id="selectItemCount" resultClass="int">
SELECT COUNT(*) AS total
<include refid="selectItem_fragment"/>
</select>
<select id="selectItems" resultClass="Item">
SELECT id, name
<include refid="selectItem_fragment"/>
</select>
</sqlMap>`,
[]string{
"SELECT COUNT(1) AS `total` FROM `items` WHERE `parentid`=6",
"SELECT `id`,`name` FROM `items` WHERE `parentid`=6",
})
testParserQuery(t, false, `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Employee">
<sql id="selectItem_fragment">
FROM items
WHERE parentid = #value#
</sql>
<select id="selectItemCount" parameterClass="int" resultClass="int">
SELECT COUNT(*) AS total
<include refid="selectItem_fragment"/>
</select>
<select id="selectItems" parameterClass="int" resultClass="Item">
SELECT id, name
<include refid="selectItem_fragment"/>
</select>
</sqlMap>`,
[]string{
"SELECT COUNT(1) AS `total` FROM `items` WHERE `parentid`=?",
"SELECT `id`,`name` FROM `items` WHERE `parentid`=?",
})
}
func TestParseIBatisAll(t *testing.T) {
testParserQuery(t, true, `
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Employee">
<sql id="selectItem_fragment">
FROM Employee
WHERE id = 6
</sql>
<sql id="selectItem_fragment2">
FROM Employee
WHERE id = #value#
</sql>
<select id="selectItemCount1" resultClass="int">
SELECT COUNT(*) AS total
<include refid="selectItem_fragment"/>
</select>
<select id="selectItems1" resultClass="Item">
SELECT id, name
<include refid="selectItem_fragment"/>
</select>
<select id="selectItemCount2" parameterClass="int" resultClass="int">
SELECT COUNT(*) AS total
<include refid="selectItem_fragment2"/>
</select>
<select id="selectItems2" parameterClass="int" resultClass="Item">
SELECT id, name
<include refid="selectItem_fragment2"/>
</select>
<select id="findByID1" resultClass="Employee">
SELECT * FROM EMPLOYEE
<dynamic prepend="WHERE ">
<isNull property="id" prepend="AND ">
id IS NULL
</isNull>
<isNotNull property="id" prepend="OR ">
id = #id#
</isNotNull>
</dynamic>
</select>
<select id="findByID2" resultClass="Employee">
SELECT * FROM EMPLOYEE
<dynamic prepend="WHERE ">
<iterate prepend="AND" property="UserNameList" open="(" close=")" conjunction="OR">
username=#UserNameList[]#
</iterate>
<isNull property="id">
id IS NULL
</isNull>
<isNotNull property="id">
id = #id#
</isNotNull>
</dynamic>
</select>
<select id="dynamicGetAccountList" resultMap="account-result" >
select * from EMPLOYEE
<dynamic prepend="WHERE">
<isNotNull prepend="AND" property="firstName" open="(" close=")">
ACC_FIRST_NAME = #firstName#
<isNotNull prepend="OR" property="lastName">
ACC_LAST_NAME = #lastName#
</isNotNull>
</isNotNull>
<isNotNull prepend="AND" property="emailAddress">
ACC_EMAIL like #emailAddress#
</isNotNull>
<isGreaterThan prepend="AND" property="id" compareValue="0">
ACC_ID = #id#
</isGreaterThan>
</dynamic>
order by ACC_LAST_NAME
</select>
<select id="getProduct" resultMap="get-product-result">
select * from EMPLOYEE order by $preferredOrder$
</select>
</sqlMap>
`, []string{
"SELECT COUNT(1) AS `total` FROM `Employee` WHERE `id`=6",
"SELECT `id`,`name` FROM `Employee` WHERE `id`=6",
"SELECT COUNT(1) AS `total` FROM `Employee` WHERE `id`=?",
"SELECT `id`,`name` FROM `Employee` WHERE `id`=?",
"SELECT * FROM `EMPLOYEE` WHERE `id` IS NULL OR `id`=?",
"SELECT * FROM `EMPLOYEE` WHERE (`username`=? OR `username`=?) AND `id` IS NULL AND `id`=?",
"SELECT * FROM `EMPLOYEE` WHERE `ACC_FIRST_NAME`=? OR `ACC_LAST_NAME`=? AND `ACC_EMAIL` LIKE ? AND `ACC_ID`=? ORDER BY `ACC_LAST_NAME`",
"SELECT * FROM `EMPLOYEE` ORDER BY ?",
})
}