tparsesql.nim 6.3 KB

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