mysqldump-ddl-parser
Version:
Parse DDL, output JS object.
49 lines (48 loc) • 1.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
class Column {
sql;
name = '';
type = '';
notNull = false;
autoIncrement = false;
default = null;
constructor(sql, dialect) {
this.sql = sql;
if (dialect == 'oracle') {
this.oracle(sql);
}
else {
this.mysql(sql);
}
}
oracle(sql) {
let parsed = sql.match(/"([^"]+)" ([^ ]+)/);
this.name = (parsed?.at(1) ?? '').toLowerCase();
this.type = parsed?.at(2) ?? '';
if (/NOT NULL/.test(sql)) {
this.notNull = true;
}
// TODO fuzzy
parsed = sql.match(/ DEFAULT ([^ ]*),?/);
if (parsed) {
this.default = parsed.at(1) ?? '';
}
}
mysql(sql) {
let parsed = sql.match(/`([^`]+)` ([^ ]+)/);
this.name = (parsed?.at(1) ?? '').toLowerCase();
this.type = parsed?.at(2) ?? '';
if (/NOT NULL/.test(sql)) {
this.notNull = true;
}
if (/AUTO_INCREMENT/.test(sql)) {
this.autoIncrement = true;
}
parsed = sql.match(/ DEFAULT (.*),?/);
if (parsed) {
this.default = parsed.at(1) ?? '';
}
}
}
exports.default = Column;