@@ -8,6 +8,7 @@ import { getTableContextFromMap } from '../../model/helper';
88import { Query , QueryCursorContext } from '../../model/query' ;
99import { createFromASTTree } from '../../model/dialect/obmysql' ;
1010import { keywords } from '../keywords' ;
11+ import { ISelectColumn } from '../../model' ;
1112
1213const keywordsSet = new Set ( keywords )
1314
@@ -332,5 +333,82 @@ export default {
332333
333334 return sqlCompletion ( statement , offset - statement . start )
334335
336+ } ,
337+
338+ getOffsetType ( text : string , delimiter : string , offset : number ) : {
339+ type : 'table' ;
340+ name : string ;
341+ schema ?: string ;
342+ } | null {
343+ if ( ! text ) {
344+ return null ;
345+ }
346+ const sqlDocuments = getSQLDocument ( text , delimiter ) ;
347+ let statement = sqlDocuments . statements . find ( s => s . start <= ( offset - 1 ) && s . stop >= ( offset - 1 ) ) ;
348+ if ( ! statement ) {
349+ return null ;
350+ }
351+ const result = statement . parse ( offset , ( ) => { } ) ;
352+ if ( ! result ) {
353+ return null ;
354+ }
355+ const queryMap = createFromASTTree ( result . result ) ;
356+ let query : Query | undefined ;
357+ queryMap . forEach ( ( value , key ) => {
358+ const location = value . location ;
359+ if ( ! location ) {
360+ return ;
361+ }
362+ if ( location . range [ 0 ] <= offset && location . range [ 1 ] >= offset ) {
363+ if ( ! query ) {
364+ query = value ;
365+ return ;
366+ }
367+ if ( query . location && query . location . range [ 0 ] < location . range [ 0 ] ) {
368+ query = value ;
369+ return ;
370+ }
371+ }
372+ } )
373+ if ( ! query ) {
374+ return null ;
375+ }
376+ const fromTable = query . fromTables . find ( fromTable => {
377+ const { location } = fromTable ;
378+ return location . range [ 0 ] <= offset && location . range [ 1 ] >= offset ;
379+ } )
380+ if ( fromTable ) {
381+ if ( fromTable . query ) {
382+ return null ;
383+ }
384+ return {
385+ type : 'table' ,
386+ name : fromTable . tableName || '' ,
387+ schema : fromTable . schemaName
388+ }
389+ }
390+ /**
391+ * select columns
392+ */
393+ const selectColumn = query . selectColumns . find ( selectColumn => {
394+ const { location } = selectColumn ;
395+ return location . range [ 0 ] <= offset && location . range [ 1 ] >= offset ;
396+ } )
397+ if ( selectColumn ) {
398+ if ( ! selectColumn . columnName || ! ( selectColumn . columnName instanceof Array ) || selectColumn . columnName ?. length < 2 ) {
399+ return null ;
400+ }
401+ const tableAlias = selectColumn . columnName [ 0 ] ;
402+ const from = query . fromTables . find ( from => from . alias === tableAlias || from . tableName === tableAlias ) ;
403+ if ( ! from ) {
404+ return null ;
405+ }
406+ return {
407+ type : 'table' ,
408+ name : from . tableName || '' ,
409+ schema : from . schemaName
410+ }
411+ }
412+ return null ;
335413 }
336414}
0 commit comments