-
Notifications
You must be signed in to change notification settings - Fork 228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: auto add quotes for table/columns #1109
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { getLanguageSetting } from './sql-setting'; | ||
import { getLanguageSetting, ILanguageSetting } from './sql-setting'; | ||
|
||
import CodeMirror from 'lib/codemirror'; | ||
import { ICodeAnalysis, TableToken } from 'lib/sql-helper/sql-lexer'; | ||
|
@@ -103,6 +103,9 @@ function findLast(arr: Array<[number, any]>, num: number) { | |
return arr[index]; | ||
} | ||
|
||
function checkNameNeedsEscape(name: string) { | ||
return !name.match(/^\w+$/); | ||
} | ||
interface IAutoCompleteResult { | ||
list: ICompletionRow[]; | ||
from: CodeMirror.Position; | ||
|
@@ -118,6 +121,7 @@ export class SqlAutoCompleter { | |
private codeAnalysis?: ICodeAnalysis; | ||
private metastoreId?: number; | ||
private language: string; | ||
private languageSetting: ILanguageSetting; | ||
private keywords?: string[]; | ||
private type: AutoCompleteType; | ||
|
||
|
@@ -129,12 +133,14 @@ export class SqlAutoCompleter { | |
) { | ||
this.codeMirrorInstance = codeMirrorInstance; | ||
this.metastoreId = metastoreId; | ||
this.language = language; | ||
this.type = type; | ||
|
||
this.Pos = this.codeMirrorInstance.Pos; | ||
this.codeAnalysis = null; | ||
|
||
this.language = language; | ||
this.languageSetting = getLanguageSetting(this.language); | ||
|
||
this.registerHelper(); | ||
} | ||
|
||
|
@@ -143,10 +149,28 @@ export class SqlAutoCompleter { | |
this.codeAnalysis = codeAnalysis; | ||
} | ||
|
||
@bind | ||
private flatQuotationFormatter(name: string) { | ||
if (!this.languageSetting.quoteChars) { | ||
return name; | ||
} | ||
|
||
if (!checkNameNeedsEscape(name)) { | ||
return name; | ||
} | ||
|
||
const [quoteStart, quoteEnd] = this.languageSetting.quoteChars; | ||
return quoteStart + name + quoteEnd; | ||
} | ||
|
||
@bind | ||
private hierarchicalQuotationFormatter(name: string) { | ||
return name.split('.').map(this.flatQuotationFormatter).join('.'); | ||
} | ||
|
||
public getKeywords() { | ||
if (!this.keywords) { | ||
const languageSetting = getLanguageSetting(this.language); | ||
this.keywords = [...languageSetting.keywords]; | ||
this.keywords = [...this.languageSetting.keywords]; | ||
} | ||
return this.keywords; | ||
} | ||
|
@@ -233,7 +257,7 @@ export class SqlAutoCompleter { | |
(id) => dataSources.dataColumnsById[id].name | ||
); | ||
const filteredColumnNames = columnNames.filter((name) => | ||
name.startsWith(prefix) | ||
name.toLowerCase().startsWith(prefix) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, in this line
|
||
); | ||
|
||
return filteredColumnNames; | ||
|
@@ -247,17 +271,19 @@ export class SqlAutoCompleter { | |
return new Promise(async (resolve) => { | ||
let results: ICompletionRow[] = []; | ||
if (lineAnalysis.context === 'table') { | ||
results = (await this.getTableNamesFromPrefix(searchStr)).map( | ||
formatter.bind(null, lineAnalysis.context) | ||
); | ||
results = (await this.getTableNamesFromPrefix(searchStr)) | ||
.map(this.hierarchicalQuotationFormatter) | ||
.map(formatter.bind(null, lineAnalysis.context)); | ||
} else if ( | ||
lineAnalysis.context === 'column' && | ||
lineAnalysis.reference | ||
) { | ||
results = this.getColumnsFromPrefix( | ||
searchStr, | ||
lineAnalysis.reference | ||
).map(formatter.bind(null, lineAnalysis.context)); | ||
) | ||
.map(this.flatQuotationFormatter) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about just move this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i moved all of the quotation formatting logic to the formatter level so it would not interfere with display |
||
.map(formatter.bind(null, lineAnalysis.context)); | ||
} | ||
resolve(results); | ||
}); | ||
|
@@ -287,10 +313,17 @@ export class SqlAutoCompleter { | |
const tableNames = await this.getTableNamesFromPrefix(prefix); | ||
|
||
for (const tableName of tableNames) { | ||
const match = tableName.match(/^(\w+)\.(\w+)$/); | ||
if (match) { | ||
const schemaTableNames = tableName.split('.'); | ||
|
||
if (schemaTableNames.length === 2) { | ||
Comment on lines
+339
to
+341
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like I saw this similar code piece multiple times, might be good to have tiny util function for it? can return an object with parsed schema name and table name, so that
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm it is just |
||
results.push( | ||
formatter(lineAnalysis.context, match[2], tableName) | ||
formatter( | ||
lineAnalysis.context, | ||
this.flatQuotationFormatter( | ||
schemaTableNames[1] | ||
), | ||
tableName | ||
) | ||
); | ||
} | ||
} | ||
|
@@ -316,9 +349,11 @@ export class SqlAutoCompleter { | |
} | ||
|
||
const prefix = context[context.length - 1]; | ||
results = this.getColumnsFromPrefix(prefix, tableNames).map( | ||
(column) => formatter(lineAnalysis.context, column, column) | ||
); | ||
results = this.getColumnsFromPrefix(prefix, tableNames) | ||
.map(this.flatQuotationFormatter) | ||
.map((column) => | ||
formatter(lineAnalysis.context, column, column) | ||
); | ||
} | ||
|
||
resolve(results); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why it's not needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually it is needed, otherwise query hint wouldn't work. I changed back the code and added more comments