|
| 1 | +import { CompletionContext } from '@codemirror/autocomplete'; |
| 2 | +import { SyntaxNode } from '@lezer/common'; |
| 3 | +import { |
| 4 | + DatabaseSchemas, |
| 5 | + TableColumnSchema, |
| 6 | + TableSchema, |
| 7 | +} from 'types/SqlSchema'; |
| 8 | + |
| 9 | +function getNodeString(context: CompletionContext, node: SyntaxNode) { |
| 10 | + return context.state.doc.sliceString(node.from, node.to); |
| 11 | +} |
| 12 | + |
| 13 | +export default class SqlCompletionHelper { |
| 14 | + /** |
| 15 | + * |
| 16 | + */ |
| 17 | + static trimIdentifier(id: string) { |
| 18 | + return id.replaceAll('`', ''); |
| 19 | + } |
| 20 | + |
| 21 | + static getColumnFromIdentifier( |
| 22 | + schema: DatabaseSchemas, |
| 23 | + currentDatabase: string | undefined, |
| 24 | + exposedTables: TableSchema[], |
| 25 | + identifier: string |
| 26 | + ): TableColumnSchema | undefined { |
| 27 | + return this.getColumnFromIdentifierPath( |
| 28 | + schema, |
| 29 | + currentDatabase, |
| 30 | + exposedTables, |
| 31 | + identifier.split('.').map(this.trimIdentifier) |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + static getColumnFromIdentifierPath( |
| 36 | + schema: DatabaseSchemas, |
| 37 | + currentDatabase: string | undefined, |
| 38 | + exposedTables: TableSchema[], |
| 39 | + path: string[] |
| 40 | + ): TableColumnSchema | undefined { |
| 41 | + if (path.length === 1) { |
| 42 | + return exposedTables |
| 43 | + .map((table) => Object.values(table.columns)) |
| 44 | + .flat() |
| 45 | + .find((column) => column.name === path[0]); |
| 46 | + } else if ( |
| 47 | + path.length === 2 && |
| 48 | + currentDatabase && |
| 49 | + schema[currentDatabase] |
| 50 | + ) { |
| 51 | + // This is tableName.columnName |
| 52 | + const [tableName, columnName] = path; |
| 53 | + return schema[currentDatabase]?.tables[tableName]?.columns[columnName]; |
| 54 | + } else if (path.length === 3) { |
| 55 | + // This is databaseName.tableName.columnName |
| 56 | + const [databaseName, tableName, columnName] = path; |
| 57 | + return schema[databaseName]?.tables[tableName]?.columns[columnName]; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Giving the identifier and schema, we will get the table schema back. |
| 63 | + * For example: databaseA.tableA will search for tableA in databaseA. |
| 64 | + * |
| 65 | + * @param schema |
| 66 | + * @param identifer |
| 67 | + */ |
| 68 | + static getTableFromIdentifier( |
| 69 | + schema: DatabaseSchemas, |
| 70 | + currentDatabase: string | undefined, |
| 71 | + identifier: string |
| 72 | + ): TableSchema | null { |
| 73 | + return this.getTableFromIdentifierPath( |
| 74 | + schema, |
| 75 | + currentDatabase, |
| 76 | + identifier.split('.').map(this.trimIdentifier) |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + static getTableFromIdentifierPath( |
| 81 | + schema: DatabaseSchemas, |
| 82 | + currentDatabase: string | undefined, |
| 83 | + path: string[] |
| 84 | + ): TableSchema | null { |
| 85 | + if (path.length === 0) return null; |
| 86 | + if (path.length > 2) return null; |
| 87 | + |
| 88 | + if (path.length === 1 && currentDatabase && schema[currentDatabase]) |
| 89 | + return schema[currentDatabase].tables[path[0]] || null; |
| 90 | + |
| 91 | + if (path.length === 2 && schema[path[0]]) { |
| 92 | + return schema[path[0]].tables[path[1]] || null; |
| 93 | + } |
| 94 | + |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Detect if we are inside the table section. For example |
| 100 | + * SELECT * FROM tableA | <-- this is inside FROM section |
| 101 | + * SELECT * FROM tableA WHERE id = | <-- this is not inside |
| 102 | + * |
| 103 | + * @param context |
| 104 | + * @param node |
| 105 | + * @returns |
| 106 | + */ |
| 107 | + static isInsideFrom(context: CompletionContext, node: SyntaxNode): boolean { |
| 108 | + let ptr: SyntaxNode | null = node; |
| 109 | + while (ptr) { |
| 110 | + const currentString = getNodeString(context, ptr); |
| 111 | + |
| 112 | + if ( |
| 113 | + ptr.type.name === 'Keyword' && |
| 114 | + ['WHERE', 'GROUP', 'HAVING', 'ORDER', 'LIMIT', 'FOR'].includes( |
| 115 | + currentString.toUpperCase() |
| 116 | + ) |
| 117 | + ) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + if ( |
| 122 | + ptr.type.name === 'Keyword' && |
| 123 | + currentString.toUpperCase() === 'FROM' |
| 124 | + ) { |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + ptr = ptr.prevSibling; |
| 129 | + } |
| 130 | + |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Find the inner node of the tree from given position |
| 136 | + */ |
| 137 | + static resolveInner(tree: SyntaxNode, pos: number): SyntaxNode | null { |
| 138 | + if (tree.from > pos || tree.to < pos) return null; |
| 139 | + |
| 140 | + let ptr = tree.firstChild; |
| 141 | + while (ptr) { |
| 142 | + const resolvedChild = SqlCompletionHelper.resolveInner(ptr, pos); |
| 143 | + if (resolvedChild) return resolvedChild; |
| 144 | + ptr = ptr.nextSibling; |
| 145 | + } |
| 146 | + |
| 147 | + return tree; |
| 148 | + } |
| 149 | + |
| 150 | + static fromTables(context: CompletionContext, node: SyntaxNode): string[] { |
| 151 | + const parent = node.type.name === 'Statement' ? node : node.parent; |
| 152 | + if (!parent) return []; |
| 153 | + if (!parent.firstChild) return []; |
| 154 | + |
| 155 | + const statementKeyword = getNodeString( |
| 156 | + context, |
| 157 | + parent.firstChild |
| 158 | + ).toUpperCase(); |
| 159 | + |
| 160 | + // Check if it is SELECT statement. |
| 161 | + // There is no FROM if it is not SELECT statement |
| 162 | + if (statementKeyword !== 'SELECT') return []; |
| 163 | + |
| 164 | + let ptr: SyntaxNode | null = parent.firstChild; |
| 165 | + |
| 166 | + // Loop until we find FROM keyword |
| 167 | + while (ptr) { |
| 168 | + const currentString = getNodeString(context, ptr); |
| 169 | + if ( |
| 170 | + ptr.type.name === 'Keyword' && |
| 171 | + currentString.toUpperCase() === 'FROM' |
| 172 | + ) { |
| 173 | + break; |
| 174 | + } |
| 175 | + ptr = ptr.nextSibling; |
| 176 | + } |
| 177 | + |
| 178 | + // FROM is not found. |
| 179 | + if (!ptr) return []; |
| 180 | + |
| 181 | + // Collecting the table name until WHERE |
| 182 | + const tables: string[] = []; |
| 183 | + |
| 184 | + ptr = ptr.nextSibling; |
| 185 | + while (ptr) { |
| 186 | + const currentString = getNodeString(context, ptr); |
| 187 | + |
| 188 | + if ( |
| 189 | + ptr.type.name === 'Keyword' && |
| 190 | + ['WHERE', 'GROUP', 'HAVING', 'ORDER', 'LIMIT', 'FOR'].includes( |
| 191 | + currentString.toUpperCase() |
| 192 | + ) |
| 193 | + ) { |
| 194 | + break; |
| 195 | + } else if (['QuotedIdentifier', 'Identifier'].includes(ptr.type.name)) { |
| 196 | + tables.push(currentString.replaceAll('`', '')); |
| 197 | + } |
| 198 | + |
| 199 | + ptr = ptr.nextSibling; |
| 200 | + } |
| 201 | + |
| 202 | + return tables; |
| 203 | + } |
| 204 | +} |
0 commit comments