You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
7.7 KiB

7 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/LICENSE
  3. (function() {
  4. var Pos = CodeMirror.Pos;
  5. var simpleTables = {
  6. "users": ["name", "score", "birthDate"],
  7. "xcountries": ["name", "population", "size"]
  8. };
  9. var schemaTables = {
  10. "schema.users": ["name", "score", "birthDate"],
  11. "schema.countries": ["name", "population", "size"]
  12. };
  13. var displayTextTables = [{
  14. text: "mytable",
  15. displayText: "mytable | The main table",
  16. columns: [{text: "id", displayText: "id | Unique ID"},
  17. {text: "name", displayText: "name | The name"}]
  18. }];
  19. var displayTextTablesWithDefault = [
  20. {
  21. text: "Api__TokenAliases",
  22. columns: [
  23. {
  24. text: "token",
  25. displayText: "token | varchar(255) | Primary",
  26. columnName: "token",
  27. columnHint: "varchar(255) | Primary"
  28. },
  29. {
  30. text: "alias",
  31. displayText: "alias | varchar(255) | Primary",
  32. columnName: "alias",
  33. columnHint: "varchar(255) | Primary"
  34. }
  35. ]
  36. },
  37. {
  38. text: "mytable",
  39. columns: [
  40. { text: "id", displayText: "id | Unique ID" },
  41. { text: "name", displayText: "name | The name" }
  42. ]
  43. }
  44. ];
  45. namespace = "sql-hint_";
  46. function test(name, spec) {
  47. testCM(name, function(cm) {
  48. cm.setValue(spec.value);
  49. cm.setCursor(spec.cursor);
  50. var completion = CodeMirror.hint.sql(cm, {
  51. tables: spec.tables,
  52. defaultTable: spec.defaultTable,
  53. disableKeywords: spec.disableKeywords
  54. });
  55. if (!deepCompare(completion.list, spec.list))
  56. throw new Failure("Wrong completion results " + JSON.stringify(completion.list) + " vs " + JSON.stringify(spec.list));
  57. eqCharPos(completion.from, spec.from);
  58. eqCharPos(completion.to, spec.to);
  59. }, {
  60. value: spec.value,
  61. mode: spec.mode || "text/x-mysql"
  62. });
  63. }
  64. test("keywords", {
  65. value: "SEL",
  66. cursor: Pos(0, 3),
  67. list: [{"text":"SELECT","className":"CodeMirror-hint-keyword"}],
  68. from: Pos(0, 0),
  69. to: Pos(0, 3)
  70. });
  71. test("keywords_disabled", {
  72. value: "SEL",
  73. cursor: Pos(0, 3),
  74. disableKeywords: true,
  75. list: [],
  76. from: Pos(0, 0),
  77. to: Pos(0, 3)
  78. });
  79. test("from", {
  80. value: "SELECT * fr",
  81. cursor: Pos(0, 11),
  82. list: [{"text":"FROM","className":"CodeMirror-hint-keyword"}],
  83. from: Pos(0, 9),
  84. to: Pos(0, 11)
  85. });
  86. test("table", {
  87. value: "SELECT xc",
  88. cursor: Pos(0, 9),
  89. tables: simpleTables,
  90. list: [{"text":"xcountries","className":"CodeMirror-hint-table"}],
  91. from: Pos(0, 7),
  92. to: Pos(0, 9)
  93. });
  94. test("columns", {
  95. value: "SELECT users.",
  96. cursor: Pos(0, 13),
  97. tables: simpleTables,
  98. list: ["users.name", "users.score", "users.birthDate"],
  99. from: Pos(0, 7),
  100. to: Pos(0, 13)
  101. });
  102. test("singlecolumn", {
  103. value: "SELECT users.na",
  104. cursor: Pos(0, 15),
  105. tables: simpleTables,
  106. list: ["users.name"],
  107. from: Pos(0, 7),
  108. to: Pos(0, 15)
  109. });
  110. test("quoted", {
  111. value: "SELECT `users`.`na",
  112. cursor: Pos(0, 18),
  113. tables: simpleTables,
  114. list: ["`users`.`name`"],
  115. from: Pos(0, 7),
  116. to: Pos(0, 18)
  117. });
  118. test("doublequoted", {
  119. value: "SELECT \"users\".\"na",
  120. cursor: Pos(0, 18),
  121. tables: simpleTables,
  122. list: ["\"users\".\"name\""],
  123. from: Pos(0, 7),
  124. to: Pos(0, 18),
  125. mode: "text/x-sqlite"
  126. });
  127. test("quotedcolumn", {
  128. value: "SELECT users.`na",
  129. cursor: Pos(0, 16),
  130. tables: simpleTables,
  131. list: ["`users`.`name`"],
  132. from: Pos(0, 7),
  133. to: Pos(0, 16)
  134. });
  135. test("doublequotedcolumn", {
  136. value: "SELECT users.\"na",
  137. cursor: Pos(0, 16),
  138. tables: simpleTables,
  139. list: ["\"users\".\"name\""],
  140. from: Pos(0, 7),
  141. to: Pos(0, 16),
  142. mode: "text/x-sqlite"
  143. });
  144. test("schema", {
  145. value: "SELECT schem",
  146. cursor: Pos(0, 12),
  147. tables: schemaTables,
  148. list: [{"text":"schema.users","className":"CodeMirror-hint-table"},
  149. {"text":"schema.countries","className":"CodeMirror-hint-table"},
  150. {"text":"SCHEMA","className":"CodeMirror-hint-keyword"},
  151. {"text":"SCHEMA_NAME","className":"CodeMirror-hint-keyword"},
  152. {"text":"SCHEMAS","className":"CodeMirror-hint-keyword"}],
  153. from: Pos(0, 7),
  154. to: Pos(0, 12)
  155. });
  156. test("schemaquoted", {
  157. value: "SELECT `sch",
  158. cursor: Pos(0, 11),
  159. tables: schemaTables,
  160. list: ["`schema`.`users`", "`schema`.`countries`"],
  161. from: Pos(0, 7),
  162. to: Pos(0, 11)
  163. });
  164. test("schemadoublequoted", {
  165. value: "SELECT \"sch",
  166. cursor: Pos(0, 11),
  167. tables: schemaTables,
  168. list: ["\"schema\".\"users\"", "\"schema\".\"countries\""],
  169. from: Pos(0, 7),
  170. to: Pos(0, 11),
  171. mode: "text/x-sqlite"
  172. });
  173. test("schemacolumn", {
  174. value: "SELECT schema.users.",
  175. cursor: Pos(0, 20),
  176. tables: schemaTables,
  177. list: ["schema.users.name",
  178. "schema.users.score",
  179. "schema.users.birthDate"],
  180. from: Pos(0, 7),
  181. to: Pos(0, 20)
  182. });
  183. test("schemacolumnquoted", {
  184. value: "SELECT `schema`.`users`.",
  185. cursor: Pos(0, 24),
  186. tables: schemaTables,
  187. list: ["`schema`.`users`.`name`",
  188. "`schema`.`users`.`score`",
  189. "`schema`.`users`.`birthDate`"],
  190. from: Pos(0, 7),
  191. to: Pos(0, 24)
  192. });
  193. test("schemacolumndoublequoted", {
  194. value: "SELECT \"schema\".\"users\".",
  195. cursor: Pos(0, 24),
  196. tables: schemaTables,
  197. list: ["\"schema\".\"users\".\"name\"",
  198. "\"schema\".\"users\".\"score\"",
  199. "\"schema\".\"users\".\"birthDate\""],
  200. from: Pos(0, 7),
  201. to: Pos(0, 24),
  202. mode: "text/x-sqlite"
  203. });
  204. test("displayText_default_table", {
  205. value: "SELECT a",
  206. cursor: Pos(0, 8),
  207. disableKeywords: true,
  208. defaultTable: "Api__TokenAliases",
  209. tables: displayTextTablesWithDefault,
  210. list: [
  211. {
  212. text: "alias",
  213. displayText: "alias | varchar(255) | Primary",
  214. columnName: "alias",
  215. columnHint: "varchar(255) | Primary",
  216. className: "CodeMirror-hint-table CodeMirror-hint-default-table"
  217. },
  218. { text: "Api__TokenAliases", className: "CodeMirror-hint-table" }
  219. ],
  220. from: Pos(0, 7),
  221. to: Pos(0, 8)
  222. });
  223. test("displayText_table", {
  224. value: "SELECT myt",
  225. cursor: Pos(0, 10),
  226. tables: displayTextTables,
  227. list: [{text: "mytable", displayText: "mytable | The main table", "className":"CodeMirror-hint-table"}],
  228. from: Pos(0, 7),
  229. to: Pos(0, 10)
  230. });
  231. test("displayText_column", {
  232. value: "SELECT mytable.",
  233. cursor: Pos(0, 15),
  234. tables: displayTextTables,
  235. list: [{text: "mytable.id", displayText: "id | Unique ID"},
  236. {text: "mytable.name", displayText: "name | The name"}],
  237. from: Pos(0, 7),
  238. to: Pos(0, 15)
  239. });
  240. test("alias_complete", {
  241. value: "SELECT t. FROM users t",
  242. cursor: Pos(0, 9),
  243. tables: simpleTables,
  244. list: ["t.name", "t.score", "t.birthDate"],
  245. from: Pos(0, 7),
  246. to: Pos(0, 9)
  247. });
  248. test("alias_complete_with_displayText", {
  249. value: "SELECT t. FROM mytable t",
  250. cursor: Pos(0, 9),
  251. tables: displayTextTables,
  252. list: [{text: "t.id", displayText: "id | Unique ID"},
  253. {text: "t.name", displayText: "name | The name"}],
  254. from: Pos(0, 7),
  255. to: Pos(0, 9)
  256. })
  257. function deepCompare(a, b) {
  258. if (a === b) return true
  259. if (!(a && typeof a == "object") ||
  260. !(b && typeof b == "object")) return false
  261. var array = Array.isArray(a)
  262. if (Array.isArray(b) != array) return false
  263. if (array) {
  264. if (a.length != b.length) return false
  265. for (var i = 0; i < a.length; i++) if (!deepCompare(a[i], b[i])) return false
  266. } else {
  267. for (var p in a) if (!(p in b) || !deepCompare(a[p], b[p])) return false
  268. for (var p in b) if (!(p in a)) return false
  269. }
  270. return true
  271. }
  272. })();