123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- // CodeMirror, copyright (c) by Marijn Haverbeke and others
- // Distributed under an MIT license: https://codemirror.net/LICENSE
- (function() {
- var Pos = CodeMirror.Pos;
- var simpleTables = {
- "users": ["name", "score", "birthDate"],
- "xcountries": ["name", "population", "size"]
- };
- var schemaTables = {
- "schema.users": ["name", "score", "birthDate"],
- "schema.countries": ["name", "population", "size"]
- };
- var displayTextTables = [{
- text: "mytable",
- displayText: "mytable | The main table",
- columns: [{text: "id", displayText: "id | Unique ID"},
- {text: "name", displayText: "name | The name"}]
- }];
- var displayTextTablesWithDefault = [
- {
- text: "Api__TokenAliases",
- columns: [
- {
- text: "token",
- displayText: "token | varchar(255) | Primary",
- columnName: "token",
- columnHint: "varchar(255) | Primary"
- },
- {
- text: "alias",
- displayText: "alias | varchar(255) | Primary",
- columnName: "alias",
- columnHint: "varchar(255) | Primary"
- }
- ]
- },
- {
- text: "mytable",
- columns: [
- { text: "id", displayText: "id | Unique ID" },
- { text: "name", displayText: "name | The name" }
- ]
- }
- ];
- namespace = "sql-hint_";
- function test(name, spec) {
- testCM(name, function(cm) {
- cm.setValue(spec.value);
- cm.setCursor(spec.cursor);
- var completion = CodeMirror.hint.sql(cm, {
- tables: spec.tables,
- defaultTable: spec.defaultTable,
- disableKeywords: spec.disableKeywords
- });
- if (!deepCompare(completion.list, spec.list))
- throw new Failure("Wrong completion results " + JSON.stringify(completion.list) + " vs " + JSON.stringify(spec.list));
- eqCharPos(completion.from, spec.from);
- eqCharPos(completion.to, spec.to);
- }, {
- value: spec.value,
- mode: spec.mode || "text/x-mysql"
- });
- }
- test("keywords", {
- value: "SEL",
- cursor: Pos(0, 3),
- list: [{"text":"SELECT","className":"CodeMirror-hint-keyword"}],
- from: Pos(0, 0),
- to: Pos(0, 3)
- });
- test("keywords_disabled", {
- value: "SEL",
- cursor: Pos(0, 3),
- disableKeywords: true,
- list: [],
- from: Pos(0, 0),
- to: Pos(0, 3)
- });
- test("from", {
- value: "SELECT * fr",
- cursor: Pos(0, 11),
- list: [{"text":"FROM","className":"CodeMirror-hint-keyword"}],
- from: Pos(0, 9),
- to: Pos(0, 11)
- });
- test("table", {
- value: "SELECT xc",
- cursor: Pos(0, 9),
- tables: simpleTables,
- list: [{"text":"xcountries","className":"CodeMirror-hint-table"}],
- from: Pos(0, 7),
- to: Pos(0, 9)
- });
- test("columns", {
- value: "SELECT users.",
- cursor: Pos(0, 13),
- tables: simpleTables,
- list: ["users.name", "users.score", "users.birthDate"],
- from: Pos(0, 7),
- to: Pos(0, 13)
- });
- test("singlecolumn", {
- value: "SELECT users.na",
- cursor: Pos(0, 15),
- tables: simpleTables,
- list: ["users.name"],
- from: Pos(0, 7),
- to: Pos(0, 15)
- });
- test("quoted", {
- value: "SELECT `users`.`na",
- cursor: Pos(0, 18),
- tables: simpleTables,
- list: ["`users`.`name`"],
- from: Pos(0, 7),
- to: Pos(0, 18)
- });
- test("doublequoted", {
- value: "SELECT \"users\".\"na",
- cursor: Pos(0, 18),
- tables: simpleTables,
- list: ["\"users\".\"name\""],
- from: Pos(0, 7),
- to: Pos(0, 18),
- mode: "text/x-sqlite"
- });
- test("quotedcolumn", {
- value: "SELECT users.`na",
- cursor: Pos(0, 16),
- tables: simpleTables,
- list: ["`users`.`name`"],
- from: Pos(0, 7),
- to: Pos(0, 16)
- });
- test("doublequotedcolumn", {
- value: "SELECT users.\"na",
- cursor: Pos(0, 16),
- tables: simpleTables,
- list: ["\"users\".\"name\""],
- from: Pos(0, 7),
- to: Pos(0, 16),
- mode: "text/x-sqlite"
- });
- test("schema", {
- value: "SELECT schem",
- cursor: Pos(0, 12),
- tables: schemaTables,
- list: [{"text":"schema.users","className":"CodeMirror-hint-table"},
- {"text":"schema.countries","className":"CodeMirror-hint-table"},
- {"text":"SCHEMA","className":"CodeMirror-hint-keyword"},
- {"text":"SCHEMA_NAME","className":"CodeMirror-hint-keyword"},
- {"text":"SCHEMAS","className":"CodeMirror-hint-keyword"}],
- from: Pos(0, 7),
- to: Pos(0, 12)
- });
- test("schemaquoted", {
- value: "SELECT `sch",
- cursor: Pos(0, 11),
- tables: schemaTables,
- list: ["`schema`.`users`", "`schema`.`countries`"],
- from: Pos(0, 7),
- to: Pos(0, 11)
- });
- test("schemadoublequoted", {
- value: "SELECT \"sch",
- cursor: Pos(0, 11),
- tables: schemaTables,
- list: ["\"schema\".\"users\"", "\"schema\".\"countries\""],
- from: Pos(0, 7),
- to: Pos(0, 11),
- mode: "text/x-sqlite"
- });
- test("schemacolumn", {
- value: "SELECT schema.users.",
- cursor: Pos(0, 20),
- tables: schemaTables,
- list: ["schema.users.name",
- "schema.users.score",
- "schema.users.birthDate"],
- from: Pos(0, 7),
- to: Pos(0, 20)
- });
- test("schemacolumnquoted", {
- value: "SELECT `schema`.`users`.",
- cursor: Pos(0, 24),
- tables: schemaTables,
- list: ["`schema`.`users`.`name`",
- "`schema`.`users`.`score`",
- "`schema`.`users`.`birthDate`"],
- from: Pos(0, 7),
- to: Pos(0, 24)
- });
- test("schemacolumndoublequoted", {
- value: "SELECT \"schema\".\"users\".",
- cursor: Pos(0, 24),
- tables: schemaTables,
- list: ["\"schema\".\"users\".\"name\"",
- "\"schema\".\"users\".\"score\"",
- "\"schema\".\"users\".\"birthDate\""],
- from: Pos(0, 7),
- to: Pos(0, 24),
- mode: "text/x-sqlite"
- });
- test("displayText_default_table", {
- value: "SELECT a",
- cursor: Pos(0, 8),
- disableKeywords: true,
- defaultTable: "Api__TokenAliases",
- tables: displayTextTablesWithDefault,
- list: [
- {
- text: "alias",
- displayText: "alias | varchar(255) | Primary",
- columnName: "alias",
- columnHint: "varchar(255) | Primary",
- className: "CodeMirror-hint-table CodeMirror-hint-default-table"
- },
- { text: "Api__TokenAliases", className: "CodeMirror-hint-table" }
- ],
- from: Pos(0, 7),
- to: Pos(0, 8)
- });
- test("displayText_table", {
- value: "SELECT myt",
- cursor: Pos(0, 10),
- tables: displayTextTables,
- list: [{text: "mytable", displayText: "mytable | The main table", "className":"CodeMirror-hint-table"}],
- from: Pos(0, 7),
- to: Pos(0, 10)
- });
- test("displayText_column", {
- value: "SELECT mytable.",
- cursor: Pos(0, 15),
- tables: displayTextTables,
- list: [{text: "mytable.id", displayText: "id | Unique ID"},
- {text: "mytable.name", displayText: "name | The name"}],
- from: Pos(0, 7),
- to: Pos(0, 15)
- });
- test("alias_complete", {
- value: "SELECT t. FROM users t",
- cursor: Pos(0, 9),
- tables: simpleTables,
- list: ["t.name", "t.score", "t.birthDate"],
- from: Pos(0, 7),
- to: Pos(0, 9)
- });
- test("alias_complete_with_displayText", {
- value: "SELECT t. FROM mytable t",
- cursor: Pos(0, 9),
- tables: displayTextTables,
- list: [{text: "t.id", displayText: "id | Unique ID"},
- {text: "t.name", displayText: "name | The name"}],
- from: Pos(0, 7),
- to: Pos(0, 9)
- })
- function deepCompare(a, b) {
- if (a === b) return true
- if (!(a && typeof a == "object") ||
- !(b && typeof b == "object")) return false
- var array = Array.isArray(a)
- if (Array.isArray(b) != array) return false
- if (array) {
- if (a.length != b.length) return false
- for (var i = 0; i < a.length; i++) if (!deepCompare(a[i], b[i])) return false
- } else {
- for (var p in a) if (!(p in b) || !deepCompare(a[p], b[p])) return false
- for (var p in b) if (!(p in a)) return false
- }
- return true
- }
- })();
|