tparsesql.nim 6.3 KB

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