emqx_utils_sql_SUITE.erl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2022-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_utils_sql_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("eunit/include/eunit.hrl").
  20. -import(emqx_utils_sql, [get_statement_type/1, parse_insert/1]).
  21. all() ->
  22. emqx_common_test_helpers:all(?MODULE).
  23. t_get_statement_type(_) ->
  24. ?assertEqual(select, get_statement_type("SELECT * FROM abc")),
  25. ?assertEqual(insert, get_statement_type("INSERT INTO abc (c1, c2, c3)VALUES(1, 2, 3)")),
  26. ?assertEqual(update, get_statement_type("UPDATE abc SET c1 = 1, c2 = 2, c3 = 3")),
  27. ?assertEqual(delete, get_statement_type("DELETE FROM abc WHERE c1 = 1")),
  28. ?assertEqual({error, unknown}, get_statement_type("drop table books")).
  29. t_parse_insert(_) ->
  30. %% `values` in table name
  31. run_pi(
  32. <<"insert into tag_VALUES(tag_values,Timestamp) values (${tagvalues},${date})"/utf8>>,
  33. {<<"insert into tag_VALUES(tag_values,Timestamp)"/utf8>>, <<"(${tagvalues},${date})"/utf8>>}
  34. ),
  35. run_pi(
  36. <<"INSERT INTO Values_таблица (идентификатор, имя, возраст) VALUES \t (${id}, 'Иван', 25) "/utf8>>,
  37. {<<"INSERT INTO Values_таблица (идентификатор, имя, возраст)"/utf8>>,
  38. <<"(${id}, 'Иван', 25)"/utf8>>}
  39. ),
  40. %% `values` in column name
  41. run_pi(
  42. <<"insert into PI.dbo.tags(tag_values,Timestamp) values (${tagvalues},${date} )"/utf8>>,
  43. {<<"insert into PI.dbo.tags(tag_values,Timestamp)"/utf8>>,
  44. <<"(${tagvalues},${date} )"/utf8>>}
  45. ),
  46. run_pi(
  47. <<"INSERT INTO mqtt_test(payload, arrived) VALUES (${payload}, FROM_UNIXTIME((${timestamp}/1000)))"/utf8>>,
  48. {<<"INSERT INTO mqtt_test(payload, arrived)"/utf8>>,
  49. <<"(${payload}, FROM_UNIXTIME((${timestamp}/1000)))">>}
  50. ),
  51. run_pi(
  52. <<"insert into таблица (идентификатор,имя,возраст) VALUES(${id},'Алексей',30)"/utf8>>,
  53. {<<"insert into таблица (идентификатор,имя,возраст)"/utf8>>,
  54. <<"(${id},'Алексей',30)"/utf8>>}
  55. ),
  56. run_pi(
  57. <<"INSERT into 表格 (标识, 名字, 年龄) VALUES (${id}, '张三', 22)"/utf8>>,
  58. {<<"INSERT into 表格 (标识, 名字, 年龄)"/utf8>>, <<"(${id}, '张三', 22)"/utf8>>}
  59. ),
  60. run_pi(
  61. <<" inSErt into 表格(标识,名字,年龄)values(${id},'李四', 35)"/utf8>>,
  62. {<<"inSErt into 表格(标识,名字,年龄)"/utf8>>, <<"(${id},'李四', 35)"/utf8>>}
  63. ),
  64. run_pi(
  65. <<"insert into PI.dbo.tags( tag_value,Timestamp) VALUES\t\t( ${tagvalues}, ${date} )"/utf8>>,
  66. {<<"insert into PI.dbo.tags( tag_value,Timestamp)"/utf8>>,
  67. <<"( ${tagvalues}, ${date} )"/utf8>>}
  68. ),
  69. run_pi(
  70. <<"insert into PI.dbo.tags(tag_value , Timestamp )vALues(${tagvalues},${date})"/utf8>>,
  71. {<<"insert into PI.dbo.tags(tag_value , Timestamp )"/utf8>>,
  72. <<"(${tagvalues},${date})"/utf8>>}
  73. ),
  74. run_pi(
  75. <<"inSErt INTO table75 (column1, column2, column3) values (${one}, ${two},${three})"/utf8>>,
  76. {<<"inSErt INTO table75 (column1, column2, column3)"/utf8>>,
  77. <<"(${one}, ${two},${three})"/utf8>>}
  78. ),
  79. run_pi(
  80. <<"INSERT Into some_table values\t(${tag1}, ${tag2} )">>,
  81. {<<"INSERT Into some_table "/utf8>>, <<"(${tag1}, ${tag2} )">>}
  82. ).
  83. t_parse_insert_nested_brackets(_) ->
  84. InsertPart = <<"INSERT INTO test_tab (val1, val2)">>,
  85. ValueLs = [
  86. <<"(ABS(POWER((2 * POWER(ABS( (-3 + 1) * 4), (2 * (1 + ABS( (-3 + 1) * 4))))), (3 - POWER(4, 2)))), ",
  87. "POWER(ABS( (-3 + 1) * 4), (2 * (1 + ABS( (-3 + 1) * 4)))))">>,
  88. <<"(GREATEST(LEAST(5, 10), ABS(-7)), LEAST(GREATEST(3, 2), 9))">>,
  89. <<"(ABS(POWER((2 * 2), (3 - 1))), POWER(ABS(-3), (2 * (1 + 1))))">>,
  90. <<"(SQRT(POWER(4, 2)), MOD((10 + 3), (2 * 5)))">>,
  91. <<"(ROUND(CEIL(3.14159 * 2), 2), CEIL(ROUND(7.5, 1)))">>,
  92. <<"(FLOOR(SQRT(ABS(-8.99))), SIGN(POWER(-2, 3)))">>,
  93. <<"(TRUNCATE(RAND() * 100, 2), ROUND(RAND() * 10, 1))">>,
  94. <<"(EXP(LOG(POWER(2, 3))), LOG(EXP(5)))">>,
  95. <<"(COS(PI() / (3 - 1)), PI() / COS(PI() / 4))">>,
  96. <<"(SIN(TAN(PI() / 4)), TAN(SIN(PI() / 6)))">>
  97. ],
  98. [
  99. run_pi(<<InsertPart/binary, " VALUES ", ValueL/binary>>, {InsertPart, ValueL})
  100. || ValueL <- ValueLs
  101. ].
  102. t_parse_insert_failed(_) ->
  103. run_pi("drop table books"),
  104. run_pi("SELECT * FROM abc"),
  105. run_pi("UPDATE abc SET c1 = 1, c2 = 2, c3 = 3"),
  106. run_pi("DELETE FROM abc WHERE c1 = 1"),
  107. run_pi("insert intotable(a,b)values(1,2)"),
  108. run_pi("insert into (a,val)values(1,'val')").
  109. run_pi(SQL) ->
  110. ?assertEqual({error, not_insert_sql}, parse_insert(SQL)),
  111. ct:pal("SQL:~n~ts~n", [SQL]).
  112. run_pi(SQL, {InsertPart, Values}) ->
  113. {ok, {InsertPart0, Values0}} = parse_insert(SQL),
  114. ?assertEqual(InsertPart, InsertPart0),
  115. ?assertEqual(Values, Values0),
  116. ct:pal("SQL:~n~ts~n", [SQL]).