TdEngineMapper.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineMapper">
  4. <update id="createDatabase" parameterType="String">
  5. create database if not exists #{dataBaseName}
  6. </update>
  7. <update id="createSuperTable">
  8. create table if not exists #{dataBaseName}.#{superTableName}
  9. <foreach item="item" collection="schemaFields" separator=","
  10. open="(" close=")" index="">
  11. <if test="item.fieldName != null || item.fieldName != ''">
  12. ${item.fieldName}
  13. </if>
  14. <if test="item.dataType != null || item.dataType != ''">
  15. <choose>
  16. <when test="item.dataType == 'timestamp'">
  17. timestamp
  18. </when>
  19. <when test="item.dataType == 'tinyint'">
  20. tinyint
  21. </when>
  22. <when test="item.dataType == 'smallint'">
  23. smallint
  24. </when>
  25. <when test="item.dataType == 'int'">
  26. int
  27. </when>
  28. <when test="item.dataType == 'bigint'">
  29. bigint
  30. </when>
  31. <when test="item.dataType == 'float'">
  32. float
  33. </when>
  34. <when test="item.dataType == 'double'">
  35. double
  36. </when>
  37. <when test="item.dataType == 'binary'">
  38. binary
  39. </when>
  40. <when test="item.dataType == 'nchar'">
  41. nchar
  42. </when>
  43. <when test="item.dataType == 'bool'">
  44. bool
  45. </when>
  46. <when test="item.dataType == 'json'">
  47. json
  48. </when>
  49. </choose>
  50. </if>
  51. <if test="item.size != null">
  52. (#{item.size})
  53. </if>
  54. </foreach>
  55. tags
  56. <!--tdEngine不支持动态tags里的数据类型,只能使用choose标签比对-->
  57. <foreach item="item" collection="tagsFields" separator=","
  58. open="(" close=")" index="">
  59. <if test="item.fieldName != null || item.fieldName != ''">
  60. ${item.fieldName}
  61. </if>
  62. <if test="item.dataType != null || item.dataType != ''">
  63. <choose>
  64. <when test="item.dataType == 'timestamp'">
  65. timestamp
  66. </when>
  67. <when test="item.dataType == 'tinyint'">
  68. tinyint
  69. </when>
  70. <when test="item.dataType == 'smallint'">
  71. smallint
  72. </when>
  73. <when test="item.dataType == 'int'">
  74. int
  75. </when>
  76. <when test="item.dataType == 'bigint'">
  77. bigint
  78. </when>
  79. <when test="item.dataType == 'float'">
  80. float
  81. </when>
  82. <when test="item.dataType == 'double'">
  83. double
  84. </when>
  85. <when test="item.dataType == 'binary'">
  86. binary
  87. </when>
  88. <when test="item.dataType == 'nchar'">
  89. nchar
  90. </when>
  91. <when test="item.dataType == 'bool'">
  92. bool
  93. </when>
  94. <when test="item.dataType == 'json'">
  95. json
  96. </when>
  97. </choose>
  98. </if>
  99. <if test="item.size != null">
  100. (#{item.size})
  101. </if>
  102. </foreach>
  103. </update>
  104. <update id="createTable">
  105. create table
  106. if not exists #{dataBaseName}.#{tableName}
  107. using #{dataBaseName}.#{superTableName}
  108. tags
  109. <foreach item="item" collection="tagsFieldValues" separator=","
  110. open="(" close=")" index="">
  111. #{item.fieldValue}
  112. </foreach>
  113. </update>
  114. <insert id="insertData">
  115. insert into #{dataBaseName}.#{tableName}
  116. <foreach item="item" collection="schemaFieldValues" separator=","
  117. open="(" close=")" index="">
  118. #{item.fieldName}
  119. </foreach>
  120. using #{dataBaseName}.#{superTableName}
  121. tags
  122. <foreach item="item" collection="tagsFieldValues" separator=","
  123. open="(" close=")" index="">
  124. #{item.fieldValue}
  125. </foreach>
  126. values
  127. <foreach item="item" collection="schemaFieldValues" separator=","
  128. open="(" close=")" index="">
  129. #{item.fieldValue}
  130. </foreach>
  131. </insert>
  132. <select id="selectByTimestamp" parameterType="cn.iocoder.yudao.module.iot.domain.SelectDto"
  133. resultType="Map">
  134. select * from #{dataBaseName}.#{tableName}
  135. <!--查询这里不能使用#{}占位符的方式,使用这种方式,tdEngine不识别为列名,只能使用${}占位的方式-->
  136. <!--因为tdEngine引擎一次只执行一条sql,所以有效预防了sql的注入,且该服务该接口为内部调用,所以可以使用${}-->
  137. where ${fieldName}
  138. between #{startTime} and #{endTime}
  139. </select>
  140. <update id="addColumnForSuperTable">
  141. ALTER
  142. STABLE
  143. #{superTableName}
  144. ADD
  145. COLUMN
  146. <if test="fieldsVo.fieldName != null || fieldsVo.fieldName != ''">
  147. #{fieldsVo.fieldName}
  148. </if>
  149. <if test="fieldsVo.dataType != null || fieldsVo.dataType != ''">
  150. <choose>
  151. <when test="fieldsVo.dataType == 'timestamp'">
  152. timestamp
  153. </when>
  154. <when test="fieldsVo.dataType == 'tinyint'">
  155. tinyint
  156. </when>
  157. <when test="fieldsVo.dataType == 'smallint'">
  158. smallint
  159. </when>
  160. <when test="fieldsVo.dataType == 'int'">
  161. int
  162. </when>
  163. <when test="fieldsVo.dataType == 'bigint'">
  164. bigint
  165. </when>
  166. <when test="fieldsVo.dataType == 'float'">
  167. float
  168. </when>
  169. <when test="fieldsVo.dataType == 'double'">
  170. double
  171. </when>
  172. <when test="fieldsVo.dataType == 'binary'">
  173. binary
  174. </when>
  175. <when test="fieldsVo.dataType == 'nchar'">
  176. nchar
  177. </when>
  178. <when test="fieldsVo.dataType == 'bool'">
  179. bool
  180. </when>
  181. <when test="fieldsVo.dataType == 'json'">
  182. json
  183. </when>
  184. </choose>
  185. </if>
  186. <if test="fieldsVo.size != null">
  187. (
  188. #{fieldsVo.size}
  189. )
  190. </if>
  191. </update>
  192. <update id="dropColumnForSuperTable">
  193. ALTER
  194. STABLE
  195. #{superTableName}
  196. DROP
  197. COLUMN
  198. <if test="fieldsVo.fieldName != null || fieldsVo.fieldName != ''">
  199. #{fieldsVo.fieldName}
  200. </if>
  201. </update>
  202. <update id="addTagForSuperTable">
  203. ALTER
  204. STABLE
  205. #{superTableName}
  206. ADD
  207. TAG
  208. <if test="fieldsVo.fieldName != null || fieldsVo.fieldName != ''">
  209. #{fieldsVo.fieldName}
  210. </if>
  211. <if test="fieldsVo.dataType != null || fieldsVo.dataType != ''">
  212. <choose>
  213. <when test="fieldsVo.dataType == 'timestamp'">
  214. timestamp
  215. </when>
  216. <when test="fieldsVo.dataType == 'tinyint'">
  217. tinyint
  218. </when>
  219. <when test="fieldsVo.dataType == 'smallint'">
  220. smallint
  221. </when>
  222. <when test="fieldsVo.dataType == 'int'">
  223. int
  224. </when>
  225. <when test="fieldsVo.dataType == 'bigint'">
  226. bigint
  227. </when>
  228. <when test="fieldsVo.dataType == 'float'">
  229. float
  230. </when>
  231. <when test="fieldsVo.dataType == 'double'">
  232. double
  233. </when>
  234. <when test="fieldsVo.dataType == 'binary'">
  235. binary
  236. </when>
  237. <when test="fieldsVo.dataType == 'nchar'">
  238. nchar
  239. </when>
  240. <when test="fieldsVo.dataType == 'bool'">
  241. bool
  242. </when>
  243. <when test="fieldsVo.dataType == 'json'">
  244. json
  245. </when>
  246. </choose>
  247. </if>
  248. <if test="fieldsVo.size != null">
  249. (
  250. #{fieldsVo.size}
  251. )
  252. </if>
  253. </update>
  254. <update id="dropTagForSuperTable">
  255. ALTER
  256. STABLE
  257. #{superTableName}
  258. DROP
  259. TAG
  260. <if test="fieldsVo.fieldName != null || fieldsVo.fieldName != ''">
  261. #{fieldsVo.fieldName}
  262. </if>
  263. </update>
  264. <select id="getCountByTimestamp" parameterType="cn.iocoder.yudao.module.iot.domain.SelectDto"
  265. resultType="java.util.Map">
  266. SELECT count(0) AS count
  267. FROM #{dataBaseName}.#{tableName} WHERE ${fieldName} BETWEEN #{startTime} AND #{endTime}
  268. </select>
  269. <select id="checkTableExists" resultType="java.lang.Integer">
  270. SELECT COUNT(0)
  271. FROM information_schema.ins_tables
  272. WHERE db_name = #{dataBaseName}
  273. AND table_name = #{tableName}
  274. </select>
  275. <select id="getLastData" resultType="java.util.Map">
  276. select last(time), *
  277. from #{tableName}
  278. where device_id = #{deviceId}
  279. </select>
  280. <select id="getLastDataByTags" parameterType="cn.iocoder.yudao.module.iot.domain.TagsSelectDao"
  281. resultType="Map">
  282. select last(*)
  283. from #{dataBaseName}.#{stableName} group by ${tagsName}
  284. </select>
  285. <select id="getHistoryData" resultType="java.util.Map"
  286. parameterType="cn.iocoder.yudao.module.iot.domain.visual.SelectVisualDto">
  287. SELECT #{fieldName}, ts
  288. FROM #{dataBaseName}.#{tableName} WHERE ts BETWEEN #{startTime} AND #{endTime}
  289. LIMIT #{num}
  290. </select>
  291. <select id="getRealtimeData" resultType="java.util.Map"
  292. parameterType="cn.iocoder.yudao.module.iot.domain.visual.SelectVisualDto">
  293. SELECT #{fieldName}, ts
  294. FROM #{dataBaseName}.#{tableName} LIMIT #{num}
  295. </select>
  296. <select id="getAggregateData" resultType="java.util.Map"
  297. parameterType="cn.iocoder.yudao.module.iot.domain.visual.SelectVisualDto">
  298. SELECT #{aggregate}(${fieldName})
  299. FROM #{dataBaseName}.#{tableName} WHERE ts BETWEEN #{startTime} AND #{endTime} interval (${interval})
  300. LIMIT #{num}
  301. </select>
  302. <insert id="createSuperTableDevice">
  303. ${sql}
  304. </insert>
  305. </mapper>