Package gudusoft.gsqlparser.util
Class TGetTableColumn
Object
gudusoft.gsqlparser.util.TGetTableColumn
用于从SQL语句中提取表和列信息的工具类。
表输出规则(基于 ETableSource 类型)
默认行为
默认情况下,此工具仅输出具有表名(getTableName() != null)且不在排除列表中的表。
以下 ETableSource 类型的表在默认情况下不会出现在输出的表列表中:
ETableSource.subquery- 子查询(始终排除)ETableSource.openquery- OPENQUERY 表(始终排除)ETableSource.function- 表值函数(始终排除)ETableSource.unnest- UNNEST 表(因为 getTableName() 为 null,使用别名)ETableSource.tableExpr- 表表达式(因为 getTableName() 为 null,使用别名)- CTE(公用表表达式)引用的表 - 默认排除,可通过
showCTE控制
以下 ETableSource 类型的表会出现在默认输出中(如果 getTableName() 不为 null):
ETableSource.objectname- 普通基础表(始终输出)ETableSource.pivoted_table- PIVOT 表ETableSource.lateralView- LATERAL VIEW 表
控制选项
1. showCTE 选项
控制是否输出 CTE(公用表表达式)相关的表:
showCTE = false(默认):CTE 表不会出现在输出中showCTE = true:CTE 表会出现在输出中
2. onlyPhysicalTables 选项
控制是否仅输出物理表(与 TSQLResolver2ResultFormatter 的 onlyPhysicalTables 选项兼容):
onlyPhysicalTables = false(默认):输出所有符合条件的表onlyPhysicalTables = true:仅输出ETableSource.objectname类型的物理表, 排除以下内容:ETableSource.unnest- UNNEST 表ETableSource.pivoted_table- PIVOT 表ETableSource.lateralView- LATERAL VIEW 表- CTE(公用表表达式)引用的表
如何输出所有类型的表
要在输出中包含CTE表,请设置 showCTE = true:
TGetTableColumn getTableColumn = new TGetTableColumn(EDbVendor.dbvoracle);
getTableColumn.showCTE = true; // 启用CTE表的输出
getTableColumn.runText(sql);
注意:即使设置了 showCTE = true,以下类型的表仍然不会出现在表列表中:
ETableSource.subquery- 子查询(不是真正的物理表)ETableSource.openquery- OPENQUERY 表ETableSource.function- 表值函数ETableSource.unnest- UNNEST 表(getTableName() 为 null)ETableSource.tableExpr- 表表达式(getTableName() 为 null)
ETableSource 类型与输出对照表
注意:此表显示的是默认配置下的行为。showUnnest、showPivotTable、showLateralView 默认都为 true, 所以即使 onlyPhysicalTables=true,这三种表类型默认也会被输出。
| ETableSource 类型 | 默认输出 | showCTE=true | onlyPhysicalTables=true (默认 show*=true) | 原因 |
|---|---|---|---|---|
| objectname | ✓ 输出 | ✓ 输出 | ✓ 输出 | 普通基础表 |
| subquery | ✗ 不输出 | ✗ 不输出 | ✗ 不输出 | 显式排除 |
| openquery | ✗ 不输出 | ✗ 不输出 | ✗ 不输出 | 显式排除 |
| function | ✗ 不输出 | ✗ 不输出 | ✗ 不输出 | 显式排除 |
| CTE 表 | ✗ 不输出 | ✓ 输出 | ✗ 不输出 | 由 showCTE 控制,onlyPhysicalTables 排除 |
| unnest | ✗ 不输出 | ✗ 不输出 | ✓ 输出 | 由 showUnnest 控制(默认 true) |
| tableExpr | ✗ 不输出 | ✗ 不输出 | ✗ 不输出 | getTableName() 为 null |
| pivoted_table | ✓ 输出 | ✓ 输出 | ✓ 输出 | 由 showPivotTable 控制(默认 true) |
| lateralView | ✓ 输出 | ✓ 输出 | ✓ 输出 | 由 showLateralView 控制(默认 true) |
其他相关选项
showCTE- 设置为 true 时,CTE表会包含在输出中onlyPhysicalTables- 设置为 true 时,仅输出 objectname 类型的物理表showUnnest- 设置为 true 时(默认),即使 onlyPhysicalTables=true,UNNEST 表也会被输出showPivotTable- 设置为 true 时(默认),即使 onlyPhysicalTables=true,PIVOT 表也会被输出showLateralView- 设置为 true 时(默认),即使 onlyPhysicalTables=true,LATERAL VIEW 表也会被输出showColumnsOfCTE- 设置为 true 时,显示CTE中的列showTableEffect- 设置为 true 时,显示表的操作类型(SELECT、INSERT、UPDATE、DELETE等)showDetail- 设置为 true 时,显示详细信息showSummary- 设置为 true 时(默认),显示汇总信息
示例
// 示例1:仅获取物理表(默认行为)
TGetTableColumn gtc = new TGetTableColumn(EDbVendor.dbvoracle);
gtc.runText("SELECT * FROM employees e, (SELECT * FROM departments) d");
// 输出仅包含: employees(子查询 departments 不会出现)
// 示例2:包含CTE表
TGetTableColumn gtc2 = new TGetTableColumn(EDbVendor.dbvoracle);
gtc2.showCTE = true;
gtc2.runText("WITH cte AS (SELECT * FROM t1) SELECT * FROM cte, t2");
// 输出包含: t1, t2, cte(因为 showCTE = true)
// 示例3:仅输出物理表,但保留 PIVOT 表(默认行为)
TGetTableColumn gtc3 = new TGetTableColumn(EDbVendor.dbvoracle);
gtc3.onlyPhysicalTables = true;
gtc3.runText("SELECT * FROM t1 PIVOT (SUM(amount) FOR month IN ('Jan', 'Feb'))");
// 输出包含: t1 和 PIVOT 表(因为 showPivotTable 默认为 true)
// 示例4:仅输出物理表,排除 PIVOT 表
TGetTableColumn gtc4 = new TGetTableColumn(EDbVendor.dbvoracle);
gtc4.onlyPhysicalTables = true;
gtc4.showPivotTable = false; // 排除 PIVOT 表
gtc4.runText("SELECT * FROM t1 PIVOT (SUM(amount) FOR month IN ('Jan', 'Feb'))");
// 输出仅包含: t1(PIVOT 表被排除)
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionbooleanbooleanbooleanboolean控制是否仅输出物理表(与TSQLResolver2ResultFormatter的 onlyPhysicalTables 选项兼容)。booleanbooleanbooleanbooleanbooleanbooleanbooleanbooleanboolean当设置为true(默认)时,即使onlyPhysicalTables=true,LATERAL VIEW 表也会被输出。 当设置为false时,如果onlyPhysicalTables=true,LATERAL VIEW 表会被排除。boolean当设置为true(默认)时,即使onlyPhysicalTables=true,PIVOT 表也会被输出。 当设置为false时,如果onlyPhysicalTables=true,PIVOT 表会被排除。booleanbooleanbooleanbooleanboolean当设置为true(默认)时,即使onlyPhysicalTables=true,UNNEST 表也会被输出。 当设置为false时,如果onlyPhysicalTables=true,UNNEST 表会被排除。 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidanalyzeStmt(TCustomSqlStatement stmt, int pNest) Get the current display name mode.getInfos()protected voidvoidvoidSet the display name mode for identifier formatting.voidsetMetaDatabase(IMetaDatabase metaDatabase) voidsetResolverType(EResolverType resolverType) void
-
Field Details
-
infos
-
outList
-
isConsole
-
listStarColumn
-
showTableEffect
-
showColumnLocation
-
showDatatype
-
showIndex
-
showColumnsOfCTE
-
showStarColumnOfCTE
-
linkOrphanColumnToFirstTable
-
showDetail
-
showSummary
-
showTreeStructure
-
showBySQLClause
-
showJoin
-
showCTE
-
onlyPhysicalTables
控制是否仅输出物理表(与TSQLResolver2ResultFormatter的 onlyPhysicalTables 选项兼容)。当设置为
true时,仅输出ETableSource.objectname类型的物理表, 排除以下类型:ETableSource.unnest- UNNEST 表ETableSource.pivoted_table- PIVOT 表ETableSource.lateralView- LATERAL VIEW 表- CTE(公用表表达式)引用的表
默认值:
false(输出所有符合条件的表,包括 unnest、pivoted_table、lateralView 等) -
showUnnest
当设置为true(默认)时,即使onlyPhysicalTables=true,UNNEST 表也会被输出。 当设置为false时,如果onlyPhysicalTables=true,UNNEST 表会被排除。 -
showPivotTable
当设置为true(默认)时,即使onlyPhysicalTables=true,PIVOT 表也会被输出。 当设置为false时,如果onlyPhysicalTables=true,PIVOT 表会被排除。 -
showLateralView
当设置为true(默认)时,即使onlyPhysicalTables=true,LATERAL VIEW 表也会被输出。 当设置为false时,如果onlyPhysicalTables=true,LATERAL VIEW 表会被排除。
-
-
Constructor Details
-
TGetTableColumn
-
-
Method Details
-
setResolverType
-
setMetaDatabase
-
setSqlEnv
-
getDisplayNameMode
Get the current display name mode.- Returns:
- the current DisplayNameMode
-
setDisplayNameMode
Set the display name mode for identifier formatting.This controls how identifiers (table names, column names) are formatted in output:
DisplayNameMode.DISPLAY- Strip delimiters, preserve original case (e.g.,[OrderID]→OrderID)DisplayNameMode.SQL_RENDER- Preserve delimiters for valid SQL regeneration (e.g.,[Order ID]→[Order ID])DisplayNameMode.CANONICAL- Apply vendor-specific case folding (e.g., Oracle:MyTable→MYTABLE)
This method can be called after instantiation to change the mode for each SQL text being processed.
- Parameters:
mode- the DisplayNameMode to use- Returns:
- this instance for method chaining
-
runText
-
runFile
-
getInfos
-
run
-
analyzeStmt
-