rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
84 lines • 3.31 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.JoinHandler = void 0;
/**
* Handles the processing of JOIN operations
*/
class JoinHandler {
constructor(graph, dataSourceHandler) {
this.graph = graph;
this.dataSourceHandler = dataSourceHandler;
this.joinIdCounter = 0;
}
/**
* Resets the join ID counter for deterministic IDs
*/
resetJoinCounter() {
this.joinIdCounter = 0;
}
/**
* Gets the next join ID
*/
getNextJoinId() {
return String(++this.joinIdCounter);
}
/**
* Processes a FROM clause with JOINs and returns the final node ID
*/
processFromClause(fromClause, cteNames, queryProcessor) {
// Process main source
const mainSourceId = this.dataSourceHandler.processSource(fromClause.source, cteNames, queryProcessor);
// Process JOINs if they exist
if (fromClause.joins && fromClause.joins.length > 0) {
return this.processJoins(fromClause.joins, mainSourceId, cteNames, queryProcessor);
}
return mainSourceId;
}
/**
* Processes a series of JOINs sequentially
*/
processJoins(joins, currentNodeId, cteNames, queryProcessor) {
let resultNodeId = currentNodeId;
for (const join of joins) {
const joinNodeId = this.dataSourceHandler.processSource(join.source, cteNames, queryProcessor);
// Create join operation node
const joinOpId = this.getNextJoinId();
const joinNode = this.graph.createJoinNode(joinOpId, join.joinType.value);
// Get nullability labels for the JOIN
const { leftLabel, rightLabel } = this.getJoinNullabilityLabels(join.joinType.value);
// Connect current source and join source to join operation with nullability labels
if (resultNodeId && !this.graph.hasConnection(resultNodeId, joinNode.id)) {
this.graph.addConnection(resultNodeId, joinNode.id, leftLabel);
}
if (joinNodeId && !this.graph.hasConnection(joinNodeId, joinNode.id)) {
this.graph.addConnection(joinNodeId, joinNode.id, rightLabel);
}
resultNodeId = joinNode.id;
}
return resultNodeId;
}
/**
* Gets nullability labels for JOIN edges based on JOIN type
*/
getJoinNullabilityLabels(joinType) {
switch (joinType.toLowerCase()) {
case 'left join':
return { leftLabel: 'NOT NULL', rightLabel: 'NULLABLE' };
case 'right join':
return { leftLabel: 'NULLABLE', rightLabel: 'NOT NULL' };
case 'inner join':
case 'join':
return { leftLabel: 'NOT NULL', rightLabel: 'NOT NULL' };
case 'full join':
case 'full outer join':
return { leftLabel: 'NULLABLE', rightLabel: 'NULLABLE' };
case 'cross join':
return { leftLabel: 'NOT NULL', rightLabel: 'NOT NULL' };
default:
// Default to no nullability info for unknown join types
return { leftLabel: '', rightLabel: '' };
}
}
}
exports.JoinHandler = JoinHandler;
//# sourceMappingURL=JoinHandler.js.map