tparsesql.nim 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. discard """
  2. targets: "c js"
  3. """
  4. import parsesql
  5. import std/assertions
  6. doAssert treeRepr(parseSql("INSERT INTO STATS VALUES (10, 5.5); ")
  7. ) == """
  8. nkStmtList
  9. nkInsert
  10. nkIdent STATS
  11. nkNone
  12. nkValueList
  13. nkIntegerLit 10
  14. nkNumericLit 5.5"""
  15. doAssert $parseSql("SELECT foo FROM table;") == "select foo from table;"
  16. doAssert $parseSql("""
  17. SELECT
  18. CustomerName,
  19. ContactName,
  20. Address,
  21. City,
  22. PostalCode,
  23. Country,
  24. CustomerName,
  25. ContactName,
  26. Address,
  27. City,
  28. PostalCode,
  29. Country
  30. FROM table;""") == "select CustomerName, ContactName, Address, City, PostalCode, Country, CustomerName, ContactName, Address, City, PostalCode, Country from table;"
  31. doAssert $parseSql("SELECT foo FROM table limit 10") == "select foo from table limit 10;"
  32. doAssert $parseSql("SELECT foo, bar, baz FROM table limit 10") == "select foo, bar, baz from table limit 10;"
  33. doAssert $parseSql("SELECT foo AS bar FROM table") == "select foo as bar from table;"
  34. doAssert $parseSql("SELECT foo AS foo_prime, bar AS bar_prime, baz AS baz_prime FROM table") == "select foo as foo_prime, bar as bar_prime, baz as baz_prime from table;"
  35. doAssert $parseSql("SELECT * FROM table") == "select * from table;"
  36. doAssert $parseSql("SELECT count(*) FROM table") == "select count(*) from table;"
  37. doAssert $parseSql("SELECT count(*) as 'Total' FROM table") == "select count(*) as 'Total' from table;"
  38. doAssert $parseSql("SELECT count(*) as 'Total', sum(a) as 'Aggr' FROM table") == "select count(*) as 'Total', sum(a) as 'Aggr' from table;"
  39. doAssert $parseSql("""
  40. SELECT * FROM table
  41. WHERE a = b and c = d
  42. """) == "select * from table where a = b and c = d;"
  43. doAssert $parseSql("""
  44. SELECT * FROM table
  45. WHERE not b
  46. """) == "select * from table where not b;"
  47. doAssert $parseSql("""
  48. SELECT
  49. *
  50. FROM
  51. table
  52. WHERE
  53. a and not b
  54. """) == "select * from table where a and not b;"
  55. doAssert $parseSql("""
  56. SELECT * FROM table
  57. ORDER BY 1
  58. """) == "select * from table order by 1;"
  59. doAssert $parseSql("""
  60. SELECT * FROM table
  61. GROUP BY 1
  62. ORDER BY 1
  63. """) == "select * from table group by 1 order by 1;"
  64. doAssert $parseSql("""
  65. SELECT * FROM table
  66. ORDER BY 1
  67. LIMIT 100
  68. """) == "select * from table order by 1 limit 100;"
  69. doAssert $parseSql("""
  70. SELECT * FROM table
  71. WHERE a = b and c = d or n is null and not b + 1 = 3
  72. """) == "select * from table where a = b and c = d or n is null and not b + 1 = 3;"
  73. doAssert $parseSql("""
  74. SELECT * FROM table
  75. WHERE (a = b and c = d) or (n is null and not b + 1 = 3)
  76. """) == "select * from table where(a = b and c = d) or (n is null and not b + 1 = 3);"
  77. doAssert $parseSql("""
  78. SELECT * FROM table
  79. HAVING a = b and c = d
  80. """) == "select * from table having a = b and c = d;"
  81. doAssert $parseSql("""
  82. SELECT a, b FROM table
  83. GROUP BY a
  84. """) == "select a, b from table group by a;"
  85. doAssert $parseSql("""
  86. SELECT a, b FROM table
  87. GROUP BY 1, 2
  88. """) == "select a, b from table group by 1, 2;"
  89. doAssert $parseSql("SELECT t.a FROM t as t") == "select t.a from t as t;"
  90. doAssert $parseSql("""
  91. SELECT a, b FROM (
  92. SELECT * FROM t
  93. )
  94. """) == "select a, b from(select * from t);"
  95. doAssert $parseSql("""
  96. SELECT a, b FROM (
  97. SELECT * FROM t
  98. ) as foo
  99. """) == "select a, b from(select * from t) as foo;"
  100. doAssert $parseSql("""
  101. SELECT a, b FROM (
  102. SELECT * FROM (
  103. SELECT * FROM (
  104. SELECT * FROM (
  105. SELECT * FROM innerTable as inner1
  106. ) as inner2
  107. ) as inner3
  108. ) as inner4
  109. ) as inner5
  110. """) == "select a, b from(select * from(select * from(select * from(select * from innerTable as inner1) as inner2) as inner3) as inner4) as inner5;"
  111. doAssert $parseSql("""
  112. SELECT a, b FROM
  113. (SELECT * FROM a),
  114. (SELECT * FROM b),
  115. (SELECT * FROM c)
  116. """) == "select a, b from(select * from a),(select * from b),(select * from c);"
  117. doAssert $parseSql("""
  118. SELECT * FROM Products
  119. WHERE Price BETWEEN 10 AND 20;
  120. """) == "select * from Products where Price between 10 and 20;"
  121. doAssert $parseSql("""
  122. SELECT id FROM a
  123. JOIN b
  124. ON a.id == b.id
  125. """) == "select id from a join b on a.id == b.id;"
  126. doAssert $parseSql("""
  127. SELECT id FROM a
  128. JOIN (SELECT id from c) as b
  129. ON a.id == b.id
  130. """) == "select id from a join(select id from c) as b on a.id == b.id;"
  131. doAssert $parseSql("""
  132. SELECT id FROM a
  133. INNER JOIN b
  134. ON a.id == b.id
  135. """) == "select id from a inner join b on a.id == b.id;"
  136. doAssert $parseSql("""
  137. SELECT id FROM a
  138. OUTER JOIN b
  139. ON a.id == b.id
  140. """) == "select id from a outer join b on a.id == b.id;"
  141. doAssert $parseSql("""
  142. SELECT id FROM a
  143. CROSS JOIN b
  144. ON a.id == b.id
  145. """) == "select id from a cross join b on a.id == b.id;"
  146. doAssert $parseSql("""
  147. CREATE TYPE happiness AS ENUM ('happy', 'very happy', 'ecstatic');
  148. CREATE TABLE holidays (
  149. num_weeks int,
  150. happiness happiness
  151. );
  152. CREATE INDEX table1_attr1 ON table1(attr1);
  153. SELECT * FROM myTab WHERE col1 = 'happy';
  154. """) == "create type happiness as enum ('happy' , 'very happy' , 'ecstatic' ); create table holidays(num_weeks int , happiness happiness );; create index table1_attr1 on table1(attr1 );; select * from myTab where col1 = 'happy';"
  155. doAssert $parseSql("""
  156. INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
  157. VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
  158. """) == "insert into Customers (CustomerName , ContactName , Address , City , PostalCode , Country ) values ('Cardinal' , 'Tom B. Erichsen' , 'Skagen 21' , 'Stavanger' , '4006' , 'Norway' );"
  159. doAssert $parseSql("""
  160. INSERT INTO TableName DEFAULT VALUES
  161. """) == "insert into TableName default values;"
  162. doAssert $parseSql("""
  163. UPDATE Customers
  164. SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
  165. WHERE CustomerID = 1;
  166. """) == "update Customers set ContactName = 'Alfred Schmidt' , City = 'Frankfurt' where CustomerID = 1;"
  167. doAssert treeRepr(parseSql("""UPDATE Customers
  168. SET ContactName = 'Alice', City= 'Frankfurt';""")
  169. ) == """
  170. nkStmtList
  171. nkUpdate
  172. nkIdent Customers
  173. nkAsgn
  174. nkIdent ContactName
  175. nkStringLit Alice
  176. nkAsgn
  177. nkIdent City
  178. nkStringLit Frankfurt
  179. nkNone"""
  180. doAssert $parseSql("DELETE FROM table_name;") == "delete from table_name;"
  181. doAssert treeRepr(parseSql("DELETE FROM table_name;")
  182. ) == """
  183. nkStmtList
  184. nkDelete
  185. nkIdent table_name
  186. nkNone"""
  187. doAssert $parseSql("DELETE * FROM table_name;") == "delete from table_name;"
  188. doAssert $parseSql("""
  189. --Select all:
  190. SELECT * FROM Customers;
  191. """) == "select * from Customers;"
  192. doAssert $parseSql("""
  193. SELECT * FROM Customers WHERE (CustomerName LIKE 'L%'
  194. OR CustomerName LIKE 'R%' /*OR CustomerName LIKE 'S%'
  195. OR CustomerName LIKE 'T%'*/ OR CustomerName LIKE 'W%')
  196. AND Country='USA'
  197. ORDER BY CustomerName;
  198. """) == "select * from Customers where(CustomerName like 'L%' or CustomerName like 'R%' or CustomerName like 'W%') and Country = 'USA' order by CustomerName;"
  199. # parse quoted keywords as identifires
  200. doAssert $parseSql("""
  201. SELECT `SELECT`, `FROM` as `GROUP` FROM `WHERE`;
  202. """) == """select "SELECT", "FROM" as "GROUP" from "WHERE";"""
  203. doAssert $parseSql("""
  204. SELECT "SELECT", "FROM" as "GROUP" FROM "WHERE";
  205. """) == """select "SELECT", "FROM" as "GROUP" from "WHERE";"""