Skip to content

Commit 5f7f784

Browse files
committed
Integrate caseInsensitiveIdentifiers
FEATURE: Dialects can now disable quoting of identifiers containing upper-case characters with the `caseInsensitiveIdentifiers` option. Closes #16
1 parent f53202a commit 5f7f784

File tree

5 files changed

+36
-32
lines changed

5 files changed

+36
-32
lines changed

README.md

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,10 @@ extensions.</p>
4242
<dd><p>The <a href="#user-content-sqldialect">dialect</a> to use. Defaults to
4343
<a href="#user-content-standardsql"><code>StandardSQL</code></a>.</p>
4444
</dd><dt id="user-content-sqlconfig.schema">
45-
<code><strong><a href="#user-content-sqlconfig.schema">schema</a></strong>&#8288;?: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>&lt;readonly (<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a> | <a href="https://codemirror.net/docs/ref#autocomplete.Completion">Completion</a>)[]&gt;</code></dt>
46-
47-
<dd><p>An object that maps table names, optionally prefixed with a
48-
schema name (<code>&quot;schema.table&quot;</code>) to options (columns) that can be
49-
completed for that table. Use lower-case names here.</p>
50-
</dd><dt id="user-content-sqlconfig.tables">
51-
<code><strong><a href="#user-content-sqlconfig.tables">tables</a></strong>&#8288;?: readonly <a href="https://codemirror.net/docs/ref#autocomplete.Completion">Completion</a>[]</code></dt>
52-
53-
<dd><p>By default, the completions for the table names will be
54-
generated from the <code>schema</code> object. But if you want to
55-
customize them, you can pass an array of completions through
56-
this option.</p>
57-
</dd><dt id="user-content-sqlconfig.schemas">
58-
<code><strong><a href="#user-content-sqlconfig.schemas">schemas</a></strong>&#8288;?: readonly <a href="https://codemirror.net/docs/ref#autocomplete.Completion">Completion</a>[]</code></dt>
59-
60-
<dd><p>Similar to <code>tables</code>, if you want to provide completion objects
61-
for your schemas rather than using the generated ones, pass them
62-
here.</p>
45+
<code><strong><a href="#user-content-sqlconfig.schema">schema</a></strong>&#8288;?: <a href="#user-content-sqlnamespace">SQLNamespace</a></code></dt>
46+
47+
<dd><p>You can use this to define the schemas, tables, and their fields
48+
for autocompletion.</p>
6349
</dd><dt id="user-content-sqlconfig.defaulttable">
6450
<code><strong><a href="#user-content-sqlconfig.defaulttable">defaultTable</a></strong>&#8288;?: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a></code></dt>
6551

@@ -76,6 +62,19 @@ completed directly at the top level.</p>
7662
<dd><p>When set to true, keyword completions will be upper-case.</p>
7763
</dd></dl>
7864

65+
</dd>
66+
<dt id="user-content-sqlnamespace">
67+
<code>
68+
type
69+
<strong><a href="#user-content-sqlnamespace">SQLNamespace</a></strong> = <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>&lt;<a href="#user-content-sqlnamespace">SQLNamespace</a>&gt; | {self: <a href="https://codemirror.net/docs/ref#autocomplete.Completion">Completion</a>, children: <a href="#user-content-sqlnamespace">SQLNamespace</a>} | readonly (<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a> | <a href="https://codemirror.net/docs/ref#autocomplete.Completion">Completion</a>)[]</code>
70+
</dt>
71+
72+
<dd><p>The type used to describe a level of the schema for
73+
<a href="#user-content-sqlconfig.schema">completion</a>. Can be an array of
74+
options (columns), an object mapping table or schema names to
75+
deeper levels, or a <code>{self, children}</code> object that assigns a
76+
completion option to use for its parent property, when the default option
77+
(its name as label and type <code>&quot;type&quot;</code>) isn't suitable.</p>
7978
</dd>
8079
<dt id="user-content-sqldialect">
8180
<h4>
@@ -153,6 +152,11 @@ strings, rather than identifiers.</p>
153152
<code><strong><a href="#user-content-sqldialectspec.charsetcasts">charSetCasts</a></strong>&#8288;?: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a></code></dt>
154153

155154
<dd><p>Enables strings like <code>_utf8'str'</code> or <code>N'str'</code>.</p>
155+
</dd><dt id="user-content-sqldialectspec.plsqlquotingmechanism">
156+
<code><strong><a href="#user-content-sqldialectspec.plsqlquotingmechanism">plsqlQuotingMechanism</a></strong>&#8288;?: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a></code></dt>
157+
158+
<dd><p>Enables string quoting syntax like <code>q'[str]'</code>, as used in
159+
PL/SQL.</p>
156160
</dd><dt id="user-content-sqldialectspec.operatorchars">
157161
<code><strong><a href="#user-content-sqldialectspec.operatorchars">operatorChars</a></strong>&#8288;?: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a></code></dt>
158162

@@ -168,11 +172,12 @@ Defaults to <code>&quot;?&quot;</code>.</p>
168172

169173
<dd><p>The characters that can be used to quote identifiers. Defaults
170174
to <code>&quot;\&quot;&quot;</code>.</p>
171-
</dd><dt id="user-content-sqldialectspec.identifiercaseinsensitive">
172-
<code><strong><a href="#user-content-sqldialectspec.identifiercaseinsensitive">identifiercaseinsensitive</a></strong>&#8288;?: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a></code></dt>
175+
</dd><dt id="user-content-sqldialectspec.caseinsensitiveidentifiers">
176+
<code><strong><a href="#user-content-sqldialectspec.caseinsensitiveidentifiers">caseInsensitiveIdentifiers</a></strong>&#8288;?: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a></code></dt>
173177

174-
<dd><p>Controls whether identifiers are case-insensitive. Identifiers with upper-case letters are quoted then set to false. Defaults
175-
to <code>false</code>.</p>
178+
<dd><p>Controls whether identifiers are case-insensitive. Identifiers
179+
with upper-case letters are quoted when set to false (which is
180+
the default).</p>
176181
</dd><dt id="user-content-sqldialectspec.unquotedbitliterals">
177182
<code><strong><a href="#user-content-sqldialectspec.unquotedbitliterals">unquotedBitLiterals</a></strong>&#8288;?: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a></code></dt>
178183

src/complete.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class CompletionLevel {
101101
list: Completion[] = []
102102
children: {[name: string]: CompletionLevel} | undefined = undefined
103103

104-
constructor(readonly idQuote: string, readonly idCaseInsensitive?: boolean) {}
104+
constructor(readonly idQuote: string, readonly idCaseInsensitive: boolean) {}
105105

106106
child(name: string) {
107107
let children = this.children || (this.children = Object.create(null))
@@ -155,8 +155,7 @@ class CompletionLevel {
155155
}
156156

157157
function nameCompletion(label: string, type: string, idQuote: string, idCaseInsensitive: boolean): Completion {
158-
const regex = new RegExp("^[a-z_][a-z_\\d]*$", idCaseInsensitive ? 'i' : undefined);
159-
if (regex.test(label)) return {label, type}
158+
if ((new RegExp("^[a-z_][a-z_\\d]*$", idCaseInsensitive ? "i" : "")).test(label)) return {label, type}
160159
return {label, type, apply: idQuote + label + idQuote}
161160
}
162161

@@ -169,7 +168,7 @@ export function completeFromSchema(schema: SQLNamespace,
169168
defaultTableName?: string, defaultSchemaName?: string,
170169
dialect?: SQLDialect): CompletionSource {
171170
let idQuote = dialect?.spec.identifierQuotes?.[0] || '"'
172-
let top = new CompletionLevel(idQuote, dialect?.spec.identifierCaseInsensitive)
171+
let top = new CompletionLevel(idQuote, !!dialect?.spec.caseInsensitiveIdentifiers)
173172
let defaultSchema = defaultSchemaName ? top.child(defaultSchemaName) : null
174173
top.addNamespace(schema)
175174
if (tables) (defaultSchema || top).addCompletions(tables)

src/sql.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ export type SQLDialectSpec = {
7777
/// to `"\""`.
7878
identifierQuotes?: string
7979
/// Controls whether identifiers are case-insensitive. Identifiers
80-
/// with upper-case letters are quoted then set to false. Defaults to
81-
/// false.
82-
identifierCaseInsensitive?: boolean,
80+
/// with upper-case letters are quoted when set to false (which is
81+
/// the default).
82+
caseInsensitiveIdentifiers?: boolean,
8383
/// Controls whether bit values can be defined as 0b1010. Defaults
8484
/// to false.
8585
unquotedBitLiterals?: boolean,

src/tokens.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export interface Dialect {
168168
operatorChars: string,
169169
specialVar: string,
170170
identifierQuotes: string,
171-
identifierCaseInsensitive: boolean,
171+
caseInsensitiveIdentifiers: boolean,
172172
words: {[name: string]: number}
173173
}
174174

@@ -189,7 +189,7 @@ const defaults: Dialect = {
189189
operatorChars: "*+\-%<>!=&|~^/",
190190
specialVar: "?",
191191
identifierQuotes: '"',
192-
identifierCaseInsensitive: false,
192+
caseInsensitiveIdentifiers: false,
193193
words: keywords(SQLKeywords, SQLTypes)
194194
}
195195

test/test-complete.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ describe("SQL completion", () => {
166166
ist(str(get("foo.c|", {schema: {foo: ["Column", "cell"]}, dialect: PostgreSQL})),
167167
'"Column", cell')
168168

169-
const customDialect = SQLDialect.define({...PostgreSQL.spec, identifierCaseInsensitive: true})
169+
const customDialect = SQLDialect.define({...PostgreSQL.spec, caseInsensitiveIdentifiers: true})
170170
ist(str(get("foo.c|", {schema: {foo: ["Column", "cell"]}, dialect: customDialect})),
171171
'Column, cell')
172172
})

0 commit comments

Comments
 (0)