TdEngineMapper.xml
11.3 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineMapper">
<update id="createDatabase" parameterType="String">
create database if not exists #{dataBaseName}
</update>
<update id="createSuperTable">
CREATE STABLE IF NOT EXISTS ${dataBaseName}.${superTableName}
<foreach item="item" collection="schemaFields" separator=","
open="(" close=")" index="">
<if test="item.fieldName != null || item.fieldName != ''">
${item.fieldName}
</if>
<if test="item.dataType != null || item.dataType != ''">
<choose>
<when test="item.dataType == 'TIMESTAMP'">
TIMESTAMP
</when>
<when test="item.dataType == 'TINYINT'">
TINYINT
</when>
<when test="item.dataType == 'SMALLINT'">
SMALLINT
</when>
<when test="item.dataType == 'INT'">
INT
</when>
<when test="item.dataType == 'BIGINT'">
BIGINT
</when>
<when test="item.dataType == 'FLOAT'">
FLOAT
</when>
<when test="item.dataType == 'DOUBLE'">
DOUBLE
</when>
<when test="item.dataType == 'BINARY'">
BINARY
</when>
<when test="item.dataType == 'NCHAR'">
NCHAR
</when>
<when test="item.dataType == 'BOOL'">
BOOL
</when>
<when test="item.dataType == 'JSON'">
JSON
</when>
</choose>
</if>
<if test="item.dataLength > 0">
(
${item.dataLength}
)
</if>
</foreach>
TAGS
<!--tdEngine不支持动态tags里的数据类型,只能使用choose标签比对-->
<foreach item="item" collection="tagsFields" separator=","
open="(" close=")" index="">
<if test="item.fieldName != null || item.fieldName != ''">
${item.fieldName}
</if>
<if test="item.dataType != null || item.dataType != ''">
<choose>
<when test="item.dataType == 'TIMESTAMP'">
TIMESTAMP
</when>
<when test="item.dataType == 'TINYINT'">
TINYINT
</when>
<when test="item.dataType == 'SMALLINT'">
SMALLINT
</when>
<when test="item.dataType == 'INT'">
INT
</when>
<when test="item.dataType == 'BIGINT'">
BIGINT
</when>
<when test="item.dataType == 'FLOAT'">
FLOAT
</when>
<when test="item.dataType == 'DOUBLE'">
DOUBLE
</when>
<when test="item.dataType == 'BINARY'">
BINARY
</when>
<when test="item.dataType == 'NCHAR'">
NCHAR
</when>
<when test="item.dataType == 'BOOL'">
BOOL
</when>
<when test="item.dataType == 'JSON'">
JSON
</when>
</choose>
</if>
<if test="item.dataLength > 0">
(
${item.dataLength}
)
</if>
</foreach>
</update>
<update id="createTable">
create table
if not exists #{dataBaseName}.#{tableName}
using #{dataBaseName}.#{superTableName}
tags
<foreach item="item" collection="tagsFieldValues" separator=","
open="(" close=")" index="">
#{item.fieldValue}
</foreach>
</update>
<insert id="insertData">
insert into #{dataBaseName}.#{tableName}
<foreach item="item" collection="schemaFieldValues" separator=","
open="(" close=")" index="">
#{item.fieldName}
</foreach>
using #{dataBaseName}.#{superTableName}
tags
<foreach item="item" collection="tagsFieldValues" separator=","
open="(" close=")" index="">
#{item.fieldValue}
</foreach>
values
<foreach item="item" collection="schemaFieldValues" separator=","
open="(" close=")" index="">
#{item.fieldValue}
</foreach>
</insert>
<select id="selectByTimestamp" parameterType="cn.iocoder.yudao.module.iot.domain.SelectDto"
resultType="Map">
select * from #{dataBaseName}.#{tableName}
<!--查询这里不能使用#{}占位符的方式,使用这种方式,tdEngine不识别为列名,只能使用${}占位的方式-->
<!--因为tdEngine引擎一次只执行一条sql,所以有效预防了sql的注入,且该服务该接口为内部调用,所以可以使用${}-->
where ${fieldName}
between #{startTime} and #{endTime}
</select>
<update id="addColumnForSuperTable">
ALTER STABLE ${dataBaseName}.${superTableName} ADD COLUMN
<if test="field.fieldName != null || field.fieldName != ''">
#{field.fieldName}
</if>
<if test="field.dataType != null || field.dataType != ''">
<choose>
<when test="field.dataType == 'TIMESTAMP'">
TIMESTAMP
</when>
<when test="field.dataType == 'TINYINT'">
TINYINT
</when>
<when test="field.dataType == 'SMALLINT'">
SMALLINT
</when>
<when test="field.dataType == 'INT'">
INT
</when>
<when test="field.dataType == 'BIGINT'">
BIGINT
</when>
<when test="field.dataType == 'FLOAT'">
FLOAT
</when>
<when test="field.dataType == 'DOUBLE'">
DOUBLE
</when>
<when test="field.dataType == 'BINARY'">
BINARY
</when>
<when test="field.dataType == 'NCHAR'">
NCHAR
</when>
<when test="field.dataType == 'BOOL'">
BOOL
</when>
<when test="field.dataType == 'JSON'">
JSON
</when>
</choose>
</if>
<if test="field.dataLength > 0">
(
#{field.dataLength}
)
</if>
</update>
<update id="dropColumnForSuperTable">
ALTER STABLE ${dataBaseName}.${superTableName} DROP COLUMN
<if test="field.fieldName != null || field.fieldName != ''">
#{field.fieldName}
</if>
</update>
<update id="addTagForSuperTable">
ALTER
STABLE
#{superTableName}
ADD
TAG
<if test="field.fieldName != null || fieldDO.fieldName != ''">
#{fieldDO.fieldName}
</if>
<if test="fieldDO.dataType != null || fieldDO.dataType != ''">
<choose>
<when test="fieldDO.dataType == 'timestamp'">
timestamp
</when>
<when test="fieldDO.dataType == 'tinyint'">
tinyint
</when>
<when test="fieldDO.dataType == 'smallint'">
smallint
</when>
<when test="fieldDO.dataType == 'int'">
int
</when>
<when test="fieldDO.dataType == 'bigint'">
bigint
</when>
<when test="fieldDO.dataType == 'float'">
float
</when>
<when test="fieldDO.dataType == 'double'">
double
</when>
<when test="fieldDO.dataType == 'binary'">
binary
</when>
<when test="fieldDO.dataType == 'nchar'">
nchar
</when>
<when test="fieldDO.dataType == 'bool'">
bool
</when>
<when test="fieldDO.dataType == 'json'">
json
</when>
</choose>
</if>
<if test="fieldDO.dataLength > 0">
(
#{fieldDO.dataLength}
)
</if>
</update>
<update id="dropTagForSuperTable">
ALTER
STABLE
#{superTableName}
DROP
TAG
<if test="fieldsVo.fieldName != null || fieldsVo.fieldName != ''">
#{fieldsVo.fieldName}
</if>
</update>
<select id="getCountByTimestamp" parameterType="cn.iocoder.yudao.module.iot.domain.SelectDto"
resultType="java.util.Map">
SELECT count(0) AS count
FROM #{dataBaseName}.#{tableName} WHERE ${fieldName} BETWEEN #{startTime} AND #{endTime}
</select>
<select id="showSuperTables" resultType="java.util.Map">
SHOW ${dataBaseName}.STABLES LIKE '${superTableName}'
</select>
<select id="getLastData" resultType="java.util.Map">
select last(time), *
from #{tableName}
where device_id = #{deviceId}
</select>
<select id="getLastDataByTags" parameterType="cn.iocoder.yudao.module.iot.domain.TagsSelectDao"
resultType="Map">
select last(*)
from #{dataBaseName}.#{stableName} group by ${tagsName}
</select>
<select id="getHistoryData" resultType="java.util.Map"
parameterType="cn.iocoder.yudao.module.iot.domain.visual.SelectVisualDto">
SELECT #{fieldName}, ts
FROM #{dataBaseName}.#{tableName} WHERE ts BETWEEN #{startTime} AND #{endTime}
LIMIT #{num}
</select>
<select id="getRealtimeData" resultType="java.util.Map"
parameterType="cn.iocoder.yudao.module.iot.domain.visual.SelectVisualDto">
SELECT #{fieldName}, ts
FROM #{dataBaseName}.#{tableName} LIMIT #{num}
</select>
<select id="getAggregateData" resultType="java.util.Map"
parameterType="cn.iocoder.yudao.module.iot.domain.visual.SelectVisualDto">
SELECT #{aggregate}(${fieldName})
FROM #{dataBaseName}.#{tableName} WHERE ts BETWEEN #{startTime} AND #{endTime} interval (${interval})
LIMIT #{num}
</select>
<select id="describeSuperTable" resultType="java.util.Map">
DESCRIBE ${dataBaseName}.${superTableName}
</select>
</mapper>