Columns and Lists Nodes
Column and List nodes represent collections of data elements in SQL statements, including SELECT column lists, INSERT value lists, and parameter collections. They handle both individual items and their container structures.
Overview
These nodes manage ordered collections of SQL elements. They provide indexing, iteration, and manipulation capabilities for lists of columns, values, parameters, and other SQL constructs.
Column and List Categories
Result and Column Lists
Value Collections
Specialized Lists
List Structure Patterns
Generic List Pattern
| TListType
├── size() -> int
├── getItem(index) -> TItemType
├── addItem(item) -> void
└── iterator() -> Iterator<TItemType>
|
Result Column List Example
| TResultColumnList
├── size() -> int
├── getResultColumn(index) -> TResultColumn
└── TResultColumn
├── expr -> TExpression
├── aliasClause -> TObjectName
└── fieldAttr -> field attributes
|
Common Usage Patterns
Iterating Column Lists
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | TSelectSqlStatement select = // ... parsed statement
TResultColumnList columns = select.getResultColumnList();
for (int i = 0; i < columns.size(); i++) {
TResultColumn column = columns.getResultColumn(i);
// Get column expression
TExpression expr = column.getExpr();
// Get column alias (if any)
TObjectName alias = column.getAliasClause();
String aliasName = alias != null ? alias.toString() : null;
// Process column
processColumn(expr, aliasName);
}
|
Analyzing Value Lists
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | TInsertSqlStatement insert = // ... parsed statement
TMultiTarget multiTarget = insert.getValues();
if (multiTarget != null) {
for (int i = 0; i < multiTarget.size(); i++) {
TValuesList valuesList = multiTarget.getMultiTarget(i);
for (int j = 0; j < valuesList.size(); j++) {
TExpression value = valuesList.getExpression(j);
// Process each value
processValue(value);
}
}
}
|
Building Column Mappings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 | public Map<String, TExpression> buildColumnMappings(TResultColumnList columns) {
Map<String, TExpression> mappings = new HashMap<>();
for (int i = 0; i < columns.size(); i++) {
TResultColumn column = columns.getResultColumn(i);
TExpression expr = column.getExpr();
// Use alias if available, otherwise derive from expression
String columnName;
if (column.getAliasClause() != null) {
columnName = column.getAliasClause().toString();
} else {
columnName = deriveColumnName(expr);
}
mappings.put(columnName, expr);
}
return mappings;
}
|
List Types and Operations
Result Column Operations
| // Access specific column
TResultColumn firstColumn = resultList.getResultColumn(0);
// Check for SELECT *
boolean hasSelectAll = resultList.isSelectAll();
// Get column count
int columnCount = resultList.size();
|
Value List Operations
| // Multi-row INSERT values
TMultiTarget multiTarget = insert.getValues();
int rowCount = multiTarget.size();
// Single row values
TValuesList singleRow = multiTarget.getMultiTarget(0);
int valueCount = singleRow.size();
|
Parameter List Operations
| // Function parameters
TFunctionCall func = expr.getFunctionCall();
TParameterList params = func.getArgs();
if (params != null) {
for (int i = 0; i < params.size(); i++) {
TExpression param = params.getExpression(i);
processParameter(param);
}
}
|
Column Alias Handling
Explicit Aliases
| SELECT customer_id AS id, customer_name AS name FROM customers;
|
| TResultColumn column = columns.getResultColumn(0);
TObjectName alias = column.getAliasClause(); // "id"
TExpression expr = column.getExpr(); // "customer_id"
|
Implicit Column Names
| SELECT customer_id, COUNT(*) FROM customers;
|
| // First column: uses original column name
// Second column: derived name like "COUNT(*)" or system-generated
|
Database-Specific Considerations
SQL Server
OUTPUT
clause result lists
- Table-valued parameter lists
MERGE
statement target lists
PostgreSQL
RETURNING
clause result lists
- Array constructor syntax
- Row constructor expressions
Oracle
- Collection types in parameter lists
- Bulk collect operations
- Object type constructors
MySQL
ON DUPLICATE KEY UPDATE
value lists
- Multi-table UPDATE target lists
- Prepared statement parameter markers
- List Size: Check list size before iteration to avoid empty collections
- Index Access: Use direct index access when possible rather than iteration
- Null Checks: Always verify list existence before accessing items
- Memory: Be aware of large lists in complex queries
See Also