You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
在子查询嵌套的情况下,`isContainCaret` 可能不足以筛选出需要的实体,例如对于SQL: `SELECT id FROM t1 LEFT JOIN (SELECT id, name FROM t2) AS t3 ON t1.id = t3.id`, 当我们光标处在内部查询`t3`派生表内时, 期望提供`t2`表下的字段补全, 但由于`t1`与`t2`的`isContainCaret`都为`true`, 无法更细节的区分出可用的表实体。
Copy file name to clipboardExpand all lines: README.md
+140-3Lines changed: 140 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -347,16 +347,153 @@ Call the `getAllEntities` method on the SQL instance, and pass in the sql text a
347
347
},
348
348
relatedEntities: null,
349
349
columns: null,
350
-
isAlias: false,
351
-
origin: null,
352
-
alias: null
350
+
_alias: null,
351
+
_comment: null
353
352
}
354
353
]
355
354
*/
356
355
```
357
356
358
357
Position is not required, if the position is passed, then in the collected entities, if the entity is located under the statement where the corresponding position is located, then the statement object to which the entity belongs will be marked with `isContainCaret`, which can help you quickly filter out the required entities when combined with the code completion function.
359
358
359
+
In nested subquery scenarios, `isContainCaret` may not be sufficient to filter out the required entities. For example, for SQL: `SELECT id FROM t1 LEFTJOIN (SELECT id, name FROM t2) AS t3 ONt1.id=t3.id`, when our cursor is inside the inner query `t3` derived table, we expect to provide field completion for the `t2` table, but since both `t1` and `t2` have `isContainCaret` as `true`, we cannot distinguish available table entities in more detail.
360
+
361
+
Therefore, for entity types with `entityContextType` as `table`, collected entities will have an `isAccessible` flag to indicate whether the entity is accessible. `isAccessible` uses scope depth internally to determine accessibility. When the entity's statement scope depth equals the cursor's statement scope depth and `isContainCaret` is `true`, the entity is considered accessible (though this determination method is not absolute, it can exclude most irrelevant entities).
362
+
363
+
#### Additional Entity Information
364
+
365
+
**Alias Information**
366
+
367
+
When an entity has an alias, the entity object will contain the `_alias` field:
368
+
- `_alias`: Detailed alias information, including text content and position information
369
+
370
+
```typescript
371
+
// Example: SELECT u.name FROM users AS u
372
+
{
373
+
entityContextType:'table',
374
+
text:'users',
375
+
_alias: { // Table alias information
376
+
text:'u',
377
+
startIndex:29,
378
+
endIndex:29,
379
+
startColumn:30,
380
+
endColumn:31,
381
+
line:1
382
+
}
383
+
}
384
+
385
+
// Example: SELECT name AS username FROM users
386
+
{
387
+
entityContextType:'column',
388
+
text:'name',
389
+
_alias: { // Column alias information
390
+
text:'username',
391
+
startIndex:15,
392
+
endIndex:22,
393
+
startColumn:16,
394
+
endColumn:24,
395
+
line:1
396
+
}
397
+
}
398
+
```
399
+
400
+
**Declaration Type (DeclareType)**
401
+
402
+
The `declareType` field identifies how an entity is declared, with different entity types having different declaration types:
- `EXPRESSION`: Complex expressions like subqueries, CASE statements, function calls, etc.
412
+
413
+
```typescript
414
+
// Examples of different declareType values
415
+
// 1. Literal column
416
+
{
417
+
entityContextType:'column',
418
+
text:'name',
419
+
declareType:ColumnDeclareType.LITERAL,
420
+
}
421
+
422
+
// 2. Wildcard column
423
+
{
424
+
entityContextType:'column',
425
+
text:'users.*',
426
+
declareType:ColumnDeclareType.ALL,
427
+
}
428
+
429
+
// 3. Expression column
430
+
{
431
+
entityContextType:'column',
432
+
text:'CASE WHEN age > 18 THEN "adult" ELSE "minor" END',
433
+
declareType:ColumnDeclareType.EXPRESSION,
434
+
}
435
+
```
436
+
437
+
**Other Metadata Fields**
438
+
439
+
**Comment Information**
440
+
- `_comment`: Entity comment information, mainly used for column or table comments in CREATE statements
441
+
442
+
```typescript
443
+
// Example: CREATE TABLE users (id INT COMMENT 'USERID', name VARCHAR(50) COMMENT 'USERNAME')
444
+
{
445
+
entityContextType:'column',
446
+
text:'id',
447
+
_comment: {
448
+
text:"'USERID'",
449
+
startIndex:35,
450
+
endIndex:42,
451
+
startColumn:36,
452
+
endColumn:44,
453
+
line:1
454
+
},
455
+
_colType: {
456
+
text:'INT',
457
+
startIndex:23,
458
+
endIndex:42,
459
+
startColumn:24,
460
+
endColumn:44,
461
+
line:1
462
+
}
463
+
}
464
+
```
465
+
466
+
**Column Type Information**
467
+
- `_colType`: Column data type information, only used for column entities in CREATE TABLE statements, includes type name and position information
468
+
469
+
```typescript
470
+
// Example: CREATE TABLE users (name VARCHAR(50) NOT NULL)
471
+
{
472
+
entityContextType:'columnCreate',
473
+
text:'name',
474
+
_colType: {
475
+
text:'VARCHAR(50)',
476
+
startIndex:25,
477
+
endIndex:35,
478
+
startColumn:26,
479
+
endColumn:37,
480
+
line:1
481
+
}
482
+
}
483
+
```
484
+
485
+
**Relationship Fields**
486
+
- `relatedEntities`: List of other entities related to the current entity, e.g., query result entities related to table entities
487
+
- `columns`: List of contained columns
488
+
489
+
A simple entity relationship example:
490
+
491
+
```sql
492
+
CREATETABLE tb1 ASSELECT id FROM tb2;
493
+
```
494
+
495
+

496
+
360
497
### Get semantic context information
361
498
362
499
Call the `getSemanticContextAtCaretPosition` method on the SQL instance, passing in the sql text and the line and column numbers at the specified position, for example:
0 commit comments