UNPKG

@sqlsmith/core

Version:

Core SQL schema merging engine with dependency resolution

63 lines 2.28 kB
export class CreateViewProcessor { getHandledTypes() { return ['view']; } canProcess(statement) { return statement?.type === 'create' && statement?.keyword === 'view'; } /** * Extracts view statements from the given AST. */ extractStatements(ast, filePath, dialect) { const statements = []; const astArray = Array.isArray(ast) ? ast : [ast]; for (const statement of astArray) { if (this.canProcess(statement)) { // TODO: The Create type from node-sql-parser doesn't have a 'view' // property. Using 'any' for now. const viewName = statement.view; if (viewName) { const dependencies = this.#extractViewDependencies(statement); statements.push({ type: 'view', name: viewName, dependsOn: dependencies, filePath, content: '', // Will be filled by the file parser ast: statement, }); } } } return statements; } #extractViewDependencies(statement) { const dependencies = []; // TODO: The Create type from node-sql-parser doesn't have a 'definition' // property for views. Using 'any' for now. const definition = statement.definition; if (definition) { const selectStatement = definition; const tables = this.#extractTableReferencesFromSelect(selectStatement); for (const table of tables) { dependencies.push({ name: table, type: 'table', }); } } return dependencies; } #extractTableReferencesFromSelect(selectStatement) { const tables = []; if (selectStatement.from && Array.isArray(selectStatement.from)) { for (const from of selectStatement.from) { if ('table' in from && from.table) { tables.push(from.table); } } } return tables; } } //# sourceMappingURL=create-view-processor.js.map