Skip to content

Commit 592f164

Browse files
committed
docs: update README to include additional entity information
1 parent 461236e commit 592f164

File tree

3 files changed

+311
-7
lines changed

3 files changed

+311
-7
lines changed

README-zh_CN.md

Lines changed: 171 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ console.log(sqlSlices)
330330
{
331331
entityContextType: 'table',
332332
text: 'tb',
333+
declareType: 0,
334+
isAccessible: true,
333335
position: {
334336
line: 1,
335337
startIndex: 14,
@@ -346,16 +348,181 @@ console.log(sqlSlices)
346348
},
347349
relatedEntities: null,
348350
columns: null,
349-
isAlias: false,
350-
origin: null,
351-
alias: null
352-
}
351+
_alias: null,
352+
_comment: null
353+
},
354+
{
355+
entityContextType: 'queryResult',
356+
text: '*',
357+
declareType: undefined,
358+
isAccessible: null,
359+
position: {
360+
line: 1,
361+
startIndex: 7,
362+
endIndex: 7,
363+
startColumn: 8,
364+
endColumn: 9
365+
},
366+
belongStmt: {
367+
stmtContextType: 'selectStmt',
368+
position: [Object],
369+
rootStmt: [Object],
370+
parentStmt: [Object],
371+
isContainCaret: true
372+
},
373+
relatedEntities: [
374+
// relate to table entity
375+
],
376+
columns: [
377+
// relate to `*` column entity
378+
],
379+
_alias: null,
380+
_comment: null,
381+
},
353382
]
354383
*/
355384
```
356385

357386
行列号信息不是必传的,如果传了行列号信息,那么收集到的实体中,如果实体位于对应行列号所在的语句下,那么实体的所属的语句对象上会带有 `isContainCaret` 标识,这在与自动补全功能结合时,可以帮助你快速筛选出需要的实体信息。
358387

388+
在子查询嵌套的情况下,`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`, 无法更细节的区分出可用的表实体。
389+
390+
所以, 针对`entityContextType``table`的实体类型, 收集到的实体上会带有`isAccessible`标识, 用于表示该实体是否可访问。`isAccessible`内部利用作用域深度来判断, 当实体的语句作用域深度与光标所在语句的作用域深度相同且`isContainCaret``true`时, 则认为该实体可访问(当然这种判断方法并非绝对,但能排除大多数无关实体)。
391+
392+
#### 实体额外信息说明
393+
394+
**别名(Alias)信息**
395+
396+
当实体具有别名时,会在实体对象中包含 `_alias` 字段:
397+
- `_alias`: 别名的详细信息,包含文本内容和位置信息
398+
399+
```typescript
400+
// 示例:SELECT u.name FROM users AS u
401+
{
402+
entityContextType: 'table',
403+
text: 'users',
404+
_alias: { // 表的别名信息
405+
text: 'u',
406+
startIndex: 29,
407+
endIndex: 29,
408+
startColumn: 30,
409+
endColumn: 31,
410+
line: 1
411+
}
412+
}
413+
414+
// 示例:SELECT name AS username FROM users
415+
{
416+
entityContextType: 'column',
417+
text: 'name',
418+
_alias: { // 列的别名信息
419+
text: 'username',
420+
startIndex: 15,
421+
endIndex: 22,
422+
startColumn: 16,
423+
endColumn: 24,
424+
line: 1
425+
}
426+
}
427+
```
428+
429+
**声明类型(DeclareType**
430+
431+
`declareType` 字段用于标识实体的声明方式,不同类型的实体有不同的声明类型:
432+
433+
**表实体的声明类型(TableDeclareType):**
434+
- `LITERAL`:字面量表名,如 `SELECT * FROM users`
435+
- `EXPRESSION`:表达式定义的表,如子查询 `SELECT * FROM (SELECT * FROM users) AS t`
436+
437+
**列实体的声明类型(ColumnDeclareType):**
438+
- `LITERAL`:字面量列名,如 `SELECT id, name FROM users`
439+
- `ALL`:通配符语法,如 `SELECT users.* FROM users`
440+
- `EXPRESSION`:复杂表达式,如子查询、CASE语句、函数调用等
441+
442+
```typescript
443+
// 示例:不同 declareType 的示例
444+
// 1. 字面量列
445+
{
446+
entityContextType: 'column',
447+
text: 'name',
448+
declareType: ColumnDeclareType.LITERAL,
449+
}
450+
451+
// 2. 通配符列
452+
{
453+
entityContextType: 'column',
454+
text: 'users.*',
455+
declareType: ColumnDeclareType.ALL,
456+
}
457+
458+
// 3. 表达式列
459+
{
460+
entityContextType: 'column',
461+
text: 'CASE WHEN age > 18 THEN "adult" ELSE "minor" END',
462+
declareType: ColumnDeclareType.EXPRESSION,
463+
}
464+
```
465+
466+
467+
**其他元信息字段**
468+
469+
**注释信息(Comment**
470+
- `_comment`:实体的注释信息,主要用于 CREATE 语句中的列注释或表注释
471+
472+
```typescript
473+
// 示例:CREATE TABLE users (id INT COMMENT 'USERID', name VARCHAR(50) COMMENT 'USERNAME')
474+
{
475+
entityContextType: 'column',
476+
text: 'id',
477+
_comment: {
478+
text: "'USERID'",
479+
startIndex: 35,
480+
endIndex: 42,
481+
startColumn: 36,
482+
endColumn: 44,
483+
line: 1
484+
},
485+
_colType: {
486+
text: 'INT',
487+
startIndex: 23,
488+
endIndex: 42,
489+
startColumn: 24,
490+
endColumn: 44,
491+
line: 1
492+
}
493+
}
494+
```
495+
496+
**列类型信息(Column Type**
497+
- `_colType`:列的数据类型信息,仅用于建表语句中的列实体,包含类型名称和位置信息
498+
499+
```typescript
500+
// 示例:CREATE TABLE users (name VARCHAR(50) NOT NULL)
501+
{
502+
entityContextType: 'columnCreate',
503+
text: 'name',
504+
_colType: {
505+
text: 'VARCHAR(50)',
506+
startIndex: 25,
507+
endIndex: 35,
508+
startColumn: 26,
509+
endColumn: 37,
510+
line: 1
511+
}
512+
}
513+
```
514+
515+
**关联信息字段**
516+
- `relatedEntities`:与当前实体相关的其他实体列表,例如查询结果实体关联的表实体
517+
- `columns`:包含的字段列表
518+
519+
一个简单的实体关联实例:
520+
521+
```sql
522+
CREATE TABLE tb1 AS SELECT id FROM tb2;
523+
```
524+
525+
![relation-image](./docs/images/relation.png)
359526

360527
### 获取语义上下文信息
361528
调用 SQL 实例上的 `getSemanticContextAtCaretPosition` 方法,传入 sql 文本和指定位置的行列号, 例如:

README.md

Lines changed: 140 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,16 +347,153 @@ Call the `getAllEntities` method on the SQL instance, and pass in the sql text a
347347
},
348348
relatedEntities: null,
349349
columns: null,
350-
isAlias: false,
351-
origin: null,
352-
alias: null
350+
_alias: null,
351+
_comment: null
353352
}
354353
]
355354
*/
356355
```
357356
358357
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.
359358
359+
In nested subquery scenarios, `isContainCaret` may not be sufficient to filter out the required entities. For example, for SQL: `SELECT id FROM t1 LEFT JOIN (SELECT id, name FROM t2) AS t3 ON t1.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:
403+
404+
**Table Entity Declaration Types (TableDeclareType):**
405+
- `LITERAL`: Literal table name, e.g., `SELECT * FROM users`
406+
- `EXPRESSION`: Table defined by expression, e.g., subquery `SELECT * FROM (SELECT * FROM users) AS t`
407+
408+
**Column Entity Declaration Types (ColumnDeclareType):**
409+
- `LITERAL`: Literal column name, e.g., `SELECT id, name FROM users`
410+
- `ALL`: Wildcard syntax, e.g., `SELECT users.* FROM users`
411+
- `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+
CREATE TABLE tb1 AS SELECT id FROM tb2;
493+
```
494+
495+
![relation-image](./docs/images/relation.png)
496+
360497
### Get semantic context information
361498
362499
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:

docs/images/relation.png

654 KB
Loading

0 commit comments

Comments
 (0)