@sqlsmith/core
Version:
Core SQL schema merging engine with dependency resolution
66 lines • 2.71 kB
JavaScript
export class CreateTableProcessor {
canProcess(statement) {
return statement?.type === 'create' && statement?.keyword === 'table';
}
getHandledTypes() {
return ['table'];
}
extractStatements(ast, filePath, dialect) {
const statements = [];
const astArray = Array.isArray(ast) ? ast : [ast];
for (const statement of astArray) {
if (this.canProcess(statement)) {
const dependencies = this.#extractTableDependencies(statement);
const tableName = statement.table?.[0]?.table;
if (tableName) {
statements.push({
type: 'table',
name: tableName,
dependsOn: dependencies,
filePath,
content: '', // Will be filled by the file parser
ast: statement,
});
}
}
}
return statements;
}
#extractTableDependencies(statement) {
const dependencies = [];
if (statement.create_definitions) {
for (const definition of statement.create_definitions) {
// Constraint-level FOREIGN KEY (e.g. `FOREIGN KEY (...) REFERENCES other(id)`)
if ('constraint_type' in definition &&
definition.constraint_type === 'FOREIGN KEY' &&
'reference_definition' in definition &&
definition.reference_definition &&
Array.isArray(definition.reference_definition.table)) {
for (const tbl of definition.reference_definition.table) {
if (tbl?.table) {
dependencies.push({
name: tbl.table,
type: 'table',
});
}
}
}
// Column-level `REFERENCES other(id)`
if ('reference_definition' in definition &&
definition.reference_definition &&
Array.isArray(definition.reference_definition.table)) {
for (const tbl of definition.reference_definition.table) {
if (tbl?.table) {
dependencies.push({
name: tbl.table,
type: 'table',
});
}
}
}
}
}
return dependencies;
}
}
//# sourceMappingURL=create-table-processor.js.map