Initial commit

This commit is contained in:
2025-12-07 14:32:46 +00:00
commit 0a0969b8af
4726 changed files with 536089 additions and 0 deletions

View File

@@ -0,0 +1,327 @@
function compileCreateTable(ast, wrap = (v) => v) {
return createTable(ast, wrap);
}
function compileCreateIndex(ast, wrap = (v) => v) {
return createIndex(ast, wrap);
}
function createTable(ast, wrap) {
return `CREATE${temporary(ast, wrap)} TABLE${exists(ast, wrap)} ${schema(
ast,
wrap
)}${table(ast, wrap)} (${columnDefinitionList(
ast,
wrap
)}${tableConstraintList(ast, wrap)})${rowid(ast, wrap)}`;
}
function temporary(ast, wrap) {
return ast.temporary ? ' TEMP' : '';
}
function rowid(ast, wrap) {
return ast.rowid ? ' WITHOUT ROWID' : '';
}
function columnDefinitionList(ast, wrap) {
return ast.columns.map((column) => columnDefinition(column, wrap)).join(', ');
}
function columnDefinition(ast, wrap) {
return `${identifier(ast.name, wrap)}${typeName(
ast,
wrap
)}${columnConstraintList(ast.constraints, wrap)}`;
}
function typeName(ast, wrap) {
return ast.type !== null ? ` ${ast.type}` : '';
}
function columnConstraintList(ast, wrap) {
return `${primaryColumnConstraint(ast, wrap)}${notnullColumnConstraint(
ast,
wrap
)}${nullColumnConstraint(ast, wrap)}${uniqueColumnConstraint(
ast,
wrap
)}${checkColumnConstraint(ast, wrap)}${defaultColumnConstraint(
ast,
wrap
)}${collateColumnConstraint(ast, wrap)}${referencesColumnConstraint(
ast,
wrap
)}${asColumnConstraint(ast, wrap)}`;
}
function primaryColumnConstraint(ast, wrap) {
return ast.primary !== null
? ` ${constraintName(ast.primary, wrap)}PRIMARY KEY${order(
ast.primary,
wrap
)}${conflictClause(ast.primary, wrap)}${autoincrement(ast.primary, wrap)}`
: '';
}
function autoincrement(ast, wrap) {
return ast.autoincrement ? ' AUTOINCREMENT' : '';
}
function notnullColumnConstraint(ast, wrap) {
return ast.notnull !== null
? ` ${constraintName(ast.notnull, wrap)}NOT NULL${conflictClause(
ast.notnull,
wrap
)}`
: '';
}
function nullColumnConstraint(ast, wrap) {
return ast.null !== null
? ` ${constraintName(ast.null, wrap)}NULL${conflictClause(ast.null, wrap)}`
: '';
}
function uniqueColumnConstraint(ast, wrap) {
return ast.unique !== null
? ` ${constraintName(ast.unique, wrap)}UNIQUE${conflictClause(
ast.unique,
wrap
)}`
: '';
}
function checkColumnConstraint(ast, wrap) {
return ast.check !== null
? ` ${constraintName(ast.check, wrap)}CHECK (${expression(
ast.check.expression,
wrap
)})`
: '';
}
function defaultColumnConstraint(ast, wrap) {
return ast.default !== null
? ` ${constraintName(ast.default, wrap)}DEFAULT ${
!ast.default.expression
? ast.default.value
: `(${expression(ast.default.value, wrap)})`
}`
: '';
}
function collateColumnConstraint(ast, wrap) {
return ast.collate !== null
? ` ${constraintName(ast.collate, wrap)}COLLATE ${ast.collate.collation}`
: '';
}
function referencesColumnConstraint(ast, wrap) {
return ast.references !== null
? ` ${constraintName(ast.references, wrap)}${foreignKeyClause(
ast.references,
wrap
)}`
: '';
}
function asColumnConstraint(ast, wrap) {
return ast.as !== null
? ` ${constraintName(ast.as, wrap)}${
ast.as.generated ? 'GENERATED ALWAYS ' : ''
}AS (${expression(ast.as.expression, wrap)})${
ast.as.mode !== null ? ` ${ast.as.mode}` : ''
}`
: '';
}
function tableConstraintList(ast, wrap) {
return ast.constraints.reduce(
(constraintList, constraint) =>
`${constraintList}, ${tableConstraint(constraint, wrap)}`,
''
);
}
function tableConstraint(ast, wrap) {
switch (ast.type) {
case 'PRIMARY KEY':
return primaryTableConstraint(ast, wrap);
case 'UNIQUE':
return uniqueTableConstraint(ast, wrap);
case 'CHECK':
return checkTableConstraint(ast, wrap);
case 'FOREIGN KEY':
return foreignTableConstraint(ast, wrap);
}
}
function primaryTableConstraint(ast, wrap) {
return `${constraintName(ast, wrap)}PRIMARY KEY (${indexedColumnList(
ast,
wrap
)})${conflictClause(ast, wrap)}`;
}
function uniqueTableConstraint(ast, wrap) {
return `${constraintName(ast, wrap)}UNIQUE (${indexedColumnList(
ast,
wrap
)})${conflictClause(ast, wrap)}`;
}
function conflictClause(ast, wrap) {
return ast.conflict !== null ? ` ON CONFLICT ${ast.conflict}` : '';
}
function checkTableConstraint(ast, wrap) {
return `${constraintName(ast, wrap)}CHECK (${expression(
ast.expression,
wrap
)})`;
}
function foreignTableConstraint(ast, wrap) {
return `${constraintName(ast, wrap)}FOREIGN KEY (${columnNameList(
ast,
wrap
)}) ${foreignKeyClause(ast.references, wrap)}`;
}
function foreignKeyClause(ast, wrap) {
return `REFERENCES ${table(ast, wrap)}${columnNameListOptional(
ast,
wrap
)}${deleteUpdateMatchList(ast, wrap)}${deferrable(ast.deferrable, wrap)}`;
}
function columnNameListOptional(ast, wrap) {
return ast.columns.length > 0 ? ` (${columnNameList(ast, wrap)})` : '';
}
function columnNameList(ast, wrap) {
return ast.columns.map((column) => identifier(column, wrap)).join(', ');
}
function deleteUpdateMatchList(ast, wrap) {
return `${deleteReference(ast, wrap)}${updateReference(
ast,
wrap
)}${matchReference(ast, wrap)}`;
}
function deleteReference(ast, wrap) {
return ast.delete !== null ? ` ON DELETE ${ast.delete}` : '';
}
function updateReference(ast, wrap) {
return ast.update !== null ? ` ON UPDATE ${ast.update}` : '';
}
function matchReference(ast, wrap) {
return ast.match !== null ? ` MATCH ${ast.match}` : '';
}
function deferrable(ast, wrap) {
return ast !== null
? ` ${ast.not ? 'NOT ' : ''}DEFERRABLE${
ast.initially !== null ? ` INITIALLY ${ast.initially}` : ''
}`
: '';
}
function constraintName(ast, wrap) {
return ast.name !== null ? `CONSTRAINT ${identifier(ast.name, wrap)} ` : '';
}
function createIndex(ast, wrap) {
return `CREATE${unique(ast, wrap)} INDEX${exists(ast, wrap)} ${schema(
ast,
wrap
)}${index(ast, wrap)} on ${table(ast, wrap)} (${indexedColumnList(
ast,
wrap
)})${where(ast, wrap)}`;
}
function unique(ast, wrap) {
return ast.unique ? ' UNIQUE' : '';
}
function exists(ast, wrap) {
return ast.exists ? ' IF NOT EXISTS' : '';
}
function schema(ast, wrap) {
return ast.schema !== null ? `${identifier(ast.schema, wrap)}.` : '';
}
function index(ast, wrap) {
return identifier(ast.index, wrap);
}
function table(ast, wrap) {
return identifier(ast.table, wrap);
}
function where(ast, wrap) {
return ast.where !== null ? ` where ${expression(ast.where)}` : '';
}
function indexedColumnList(ast, wrap) {
return ast.columns
.map((column) =>
!column.expression
? indexedColumn(column, wrap)
: indexedColumnExpression(column, wrap)
)
.join(', ');
}
function indexedColumn(ast, wrap) {
return `${identifier(ast.name, wrap)}${collation(ast, wrap)}${order(
ast,
wrap
)}`;
}
function indexedColumnExpression(ast, wrap) {
return `${indexedExpression(ast.name, wrap)}${collation(ast, wrap)}${order(
ast,
wrap
)}`;
}
function collation(ast, wrap) {
return ast.collation !== null ? ` COLLATE ${ast.collation}` : '';
}
function order(ast, wrap) {
return ast.order !== null ? ` ${ast.order}` : '';
}
function indexedExpression(ast, wrap) {
return expression(ast, wrap);
}
function expression(ast, wrap) {
return ast.reduce(
(expr, e) =>
Array.isArray(e)
? `${expr}(${expression(e)})`
: !expr
? e
: `${expr} ${e}`,
''
);
}
function identifier(ast, wrap) {
return wrap(ast);
}
module.exports = {
compileCreateTable,
compileCreateIndex,
};

View File

@@ -0,0 +1,161 @@
// Sequence parser combinator
function s(sequence, post = (v) => v) {
return function ({ index = 0, input }) {
let position = index;
const ast = [];
for (const parser of sequence) {
const result = parser({ index: position, input });
if (result.success) {
position = result.index;
ast.push(result.ast);
} else {
return result;
}
}
return { success: true, ast: post(ast), index: position, input };
};
}
// Alternative parser combinator
function a(alternative, post = (v) => v) {
return function ({ index = 0, input }) {
for (const parser of alternative) {
const result = parser({ index, input });
if (result.success) {
return {
success: true,
ast: post(result.ast),
index: result.index,
input,
};
}
}
return { success: false, ast: null, index, input };
};
}
// Many parser combinator
function m(many, post = (v) => v) {
return function ({ index = 0, input }) {
let result = {};
let position = index;
const ast = [];
do {
result = many({ index: position, input });
if (result.success) {
position = result.index;
ast.push(result.ast);
}
} while (result.success);
if (ast.length > 0) {
return { success: true, ast: post(ast), index: position, input };
} else {
return { success: false, ast: null, index: position, input };
}
};
}
// Optional parser combinator
function o(optional, post = (v) => v) {
return function ({ index = 0, input }) {
const result = optional({ index, input });
if (result.success) {
return {
success: true,
ast: post(result.ast),
index: result.index,
input,
};
} else {
return { success: true, ast: post(null), index, input };
}
};
}
// Lookahead parser combinator
function l(lookahead, post = (v) => v) {
return function ({ index = 0, input }) {
const result = lookahead.do({ index, input });
if (result.success) {
const resultNext = lookahead.next({ index: result.index, input });
if (resultNext.success) {
return {
success: true,
ast: post(result.ast),
index: result.index,
input,
};
}
}
return { success: false, ast: null, index, input };
};
}
// Negative parser combinator
function n(negative, post = (v) => v) {
return function ({ index = 0, input }) {
const result = negative.do({ index, input });
if (result.success) {
const resultNot = negative.not({ index, input });
if (!resultNot.success) {
return {
success: true,
ast: post(result.ast),
index: result.index,
input,
};
}
}
return { success: false, ast: null, index, input };
};
}
// Token parser combinator
function t(token, post = (v) => v.text) {
return function ({ index = 0, input }) {
const result = input[index];
if (
result !== undefined &&
(token.type === undefined || token.type === result.type) &&
(token.text === undefined ||
token.text.toUpperCase() === result.text.toUpperCase())
) {
return {
success: true,
ast: post(result),
index: index + 1,
input,
};
} else {
return { success: false, ast: null, index, input };
}
};
}
// Empty parser constant
const e = function ({ index = 0, input }) {
return { success: true, ast: null, index, input };
};
// Finish parser constant
const f = function ({ index = 0, input }) {
return { success: index === input.length, ast: null, index, input };
};
module.exports = { s, a, m, o, l, n, t, e, f };

View File

@@ -0,0 +1,638 @@
const { tokenize } = require('./tokenizer');
const { s, a, m, o, l, n, t, e, f } = require('./parser-combinator');
const TOKENS = {
keyword:
/(?:ABORT|ACTION|ADD|AFTER|ALL|ALTER|ALWAYS|ANALYZE|AND|AS|ASC|ATTACH|AUTOINCREMENT|BEFORE|BEGIN|BETWEEN|BY|CASCADE|CASE|CAST|CHECK|COLLATE|COLUMN|COMMIT|CONFLICT|CONSTRAINT|CREATE|CROSS|CURRENT|CURRENT_DATE|CURRENT_TIME|CURRENT_TIMESTAMP|DATABASE|DEFAULT|DEFERRED|DEFERRABLE|DELETE|DESC|DETACH|DISTINCT|DO|DROP|END|EACH|ELSE|ESCAPE|EXCEPT|EXCLUSIVE|EXCLUDE|EXISTS|EXPLAIN|FAIL|FILTER|FIRST|FOLLOWING|FOR|FOREIGN|FROM|FULL|GENERATED|GLOB|GROUP|GROUPS|HAVING|IF|IGNORE|IMMEDIATE|IN|INDEX|INDEXED|INITIALLY|INNER|INSERT|INSTEAD|INTERSECT|INTO|IS|ISNULL|JOIN|KEY|LAST|LEFT|LIKE|LIMIT|MATCH|MATERIALIZED|NATURAL|NO|NOT|NOTHING|NOTNULL|NULL|NULLS|OF|OFFSET|ON|OR|ORDER|OTHERS|OUTER|OVER|PARTITION|PLAN|PRAGMA|PRECEDING|PRIMARY|QUERY|RAISE|RANGE|RECURSIVE|REFERENCES|REGEXP|REINDEX|RELEASE|RENAME|REPLACE|RESTRICT|RETURNING|RIGHT|ROLLBACK|ROW|ROWS|SAVEPOINT|SELECT|SET|TABLE|TEMP|TEMPORARY|THEN|TIES|TO|TRANSACTION|TRIGGER|UNBOUNDED|UNION|UNIQUE|UPDATE|USING|VACUUM|VALUES|VIEW|VIRTUAL|WHEN|WHERE|WINDOW|WITH|WITHOUT)(?=\s+|-|\(|\)|;|\+|\*|\/|%|==|=|<=|<>|<<|<|>=|>>|>|!=|,|&|~|\|\||\||\.)/,
id: /"[^"]*(?:""[^"]*)*"|`[^`]*(?:``[^`]*)*`|\[[^[\]]*\]|[a-z_][a-z0-9_$]*/,
string: /'[^']*(?:''[^']*)*'/,
blob: /x'(?:[0-9a-f][0-9a-f])+'/,
numeric: /(?:\d+(?:\.\d*)?|\.\d+)(?:e(?:\+|-)?\d+)?|0x[0-9a-f]+/,
variable: /\?\d*|[@$:][a-z0-9_$]+/,
operator: /-|\(|\)|;|\+|\*|\/|%|==|=|<=|<>|<<|<|>=|>>|>|!=|,|&|~|\|\||\||\./,
_ws: /\s+/,
};
function parseCreateTable(sql) {
const result = createTable({ input: tokenize(sql, TOKENS) });
if (!result.success) {
throw new Error(
`Parsing CREATE TABLE failed at [${result.input
.slice(result.index)
.map((t) => t.text)
.join(' ')}] of "${sql}"`
);
}
return result.ast;
}
function parseCreateIndex(sql) {
const result = createIndex({ input: tokenize(sql, TOKENS) });
if (!result.success) {
throw new Error(
`Parsing CREATE INDEX failed at [${result.input
.slice(result.index)
.map((t) => t.text)
.join(' ')}] of "${sql}"`
);
}
return result.ast;
}
function createTable(ctx) {
return s(
[
t({ text: 'CREATE' }, (v) => null),
temporary,
t({ text: 'TABLE' }, (v) => null),
exists,
schema,
table,
t({ text: '(' }, (v) => null),
columnDefinitionList,
tableConstraintList,
t({ text: ')' }, (v) => null),
rowid,
f,
],
(v) => Object.assign({}, ...v.filter((x) => x !== null))
)(ctx);
}
function temporary(ctx) {
return a([t({ text: 'TEMP' }), t({ text: 'TEMPORARY' }), e], (v) => ({
temporary: v !== null,
}))(ctx);
}
function rowid(ctx) {
return o(s([t({ text: 'WITHOUT' }), t({ text: 'ROWID' })]), (v) => ({
rowid: v !== null,
}))(ctx);
}
function columnDefinitionList(ctx) {
return a([
s([columnDefinition, t({ text: ',' }), columnDefinitionList], (v) => ({
columns: [v[0]].concat(v[2].columns),
})),
s([columnDefinition], (v) => ({ columns: [v[0]] })),
])(ctx);
}
function columnDefinition(ctx) {
return s(
[s([identifier], (v) => ({ name: v[0] })), typeName, columnConstraintList],
(v) => Object.assign({}, ...v)
)(ctx);
}
function typeName(ctx) {
return o(
s(
[
m(t({ type: 'id' })),
a([
s(
[
t({ text: '(' }),
signedNumber,
t({ text: ',' }),
signedNumber,
t({ text: ')' }),
],
(v) => `(${v[1]}, ${v[3]})`
),
s(
[t({ text: '(' }), signedNumber, t({ text: ')' })],
(v) => `(${v[1]})`
),
e,
]),
],
(v) => `${v[0].join(' ')}${v[1] || ''}`
),
(v) => ({ type: v })
)(ctx);
}
function columnConstraintList(ctx) {
return o(m(columnConstraint), (v) => ({
constraints: Object.assign(
{
primary: null,
notnull: null,
null: null,
unique: null,
check: null,
default: null,
collate: null,
references: null,
as: null,
},
...(v || [])
),
}))(ctx);
}
function columnConstraint(ctx) {
return a([
primaryColumnConstraint,
notnullColumnConstraint,
nullColumnConstraint,
uniqueColumnConstraint,
checkColumnConstraint,
defaultColumnConstraint,
collateColumnConstraint,
referencesColumnConstraint,
asColumnConstraint,
])(ctx);
}
function primaryColumnConstraint(ctx) {
return s(
[
constraintName,
t({ text: 'PRIMARY' }, (v) => null),
t({ text: 'KEY' }, (v) => null),
order,
conflictClause,
autoincrement,
],
(v) => ({ primary: Object.assign({}, ...v.filter((x) => x !== null)) })
)(ctx);
}
function autoincrement(ctx) {
return o(t({ text: 'AUTOINCREMENT' }), (v) => ({
autoincrement: v !== null,
}))(ctx);
}
function notnullColumnConstraint(ctx) {
return s(
[
constraintName,
t({ text: 'NOT' }, (v) => null),
t({ text: 'NULL' }, (v) => null),
conflictClause,
],
(v) => ({ notnull: Object.assign({}, ...v.filter((x) => x !== null)) })
)(ctx);
}
function nullColumnConstraint(ctx) {
return s(
[constraintName, t({ text: 'NULL' }, (v) => null), conflictClause],
(v) => ({ null: Object.assign({}, ...v.filter((x) => x !== null)) })
)(ctx);
}
function uniqueColumnConstraint(ctx) {
return s(
[constraintName, t({ text: 'UNIQUE' }, (v) => null), conflictClause],
(v) => ({ unique: Object.assign({}, ...v.filter((x) => x !== null)) })
)(ctx);
}
function checkColumnConstraint(ctx) {
return s(
[
constraintName,
t({ text: 'CHECK' }, (v) => null),
t({ text: '(' }, (v) => null),
s([expression], (v) => ({ expression: v[0] })),
t({ text: ')' }, (v) => null),
],
(v) => ({ check: Object.assign({}, ...v.filter((x) => x !== null)) })
)(ctx);
}
function defaultColumnConstraint(ctx) {
return s(
[
constraintName,
t({ text: 'DEFAULT' }, (v) => null),
a([
s([t({ text: '(' }), expression, t({ text: ')' })], (v) => ({
value: v[1],
expression: true,
})),
s([literalValue], (v) => ({ value: v[0], expression: false })),
s([signedNumber], (v) => ({ value: v[0], expression: false })),
]),
],
(v) => ({ default: Object.assign({}, ...v.filter((x) => x !== null)) })
)(ctx);
}
function collateColumnConstraint(ctx) {
return s(
[
constraintName,
t({ text: 'COLLATE' }, (v) => null),
t({ type: 'id' }, (v) => ({ collation: v.text })),
],
(v) => ({ collate: Object.assign({}, ...v.filter((x) => x !== null)) })
)(ctx);
}
function referencesColumnConstraint(ctx) {
return s(
[constraintName, s([foreignKeyClause], (v) => v[0].references)],
(v) => ({
references: Object.assign({}, ...v.filter((x) => x !== null)),
})
)(ctx);
}
function asColumnConstraint(ctx) {
return s(
[
constraintName,
o(s([t({ text: 'GENERATED' }), t({ text: 'ALWAYS' })]), (v) => ({
generated: v !== null,
})),
t({ text: 'AS' }, (v) => null),
t({ text: '(' }, (v) => null),
s([expression], (v) => ({ expression: v[0] })),
t({ text: ')' }, (v) => null),
a([t({ text: 'STORED' }), t({ text: 'VIRTUAL' }), e], (v) => ({
mode: v ? v.toUpperCase() : null,
})),
],
(v) => ({ as: Object.assign({}, ...v.filter((x) => x !== null)) })
)(ctx);
}
function tableConstraintList(ctx) {
return o(m(s([t({ text: ',' }), tableConstraint], (v) => v[1])), (v) => ({
constraints: v || [],
}))(ctx);
}
function tableConstraint(ctx) {
return a([
primaryTableConstraint,
uniqueTableConstraint,
checkTableConstraint,
foreignTableConstraint,
])(ctx);
}
function primaryTableConstraint(ctx) {
return s(
[
constraintName,
t({ text: 'PRIMARY' }, (v) => null),
t({ text: 'KEY' }, (v) => null),
t({ text: '(' }, (v) => null),
indexedColumnList,
t({ text: ')' }, (v) => null),
conflictClause,
],
(v) =>
Object.assign({ type: 'PRIMARY KEY' }, ...v.filter((x) => x !== null))
)(ctx);
}
function uniqueTableConstraint(ctx) {
return s(
[
constraintName,
t({ text: 'UNIQUE' }, (v) => null),
t({ text: '(' }, (v) => null),
indexedColumnList,
t({ text: ')' }, (v) => null),
conflictClause,
],
(v) => Object.assign({ type: 'UNIQUE' }, ...v.filter((x) => x !== null))
)(ctx);
}
function conflictClause(ctx) {
return o(
s(
[
t({ text: 'ON' }),
t({ text: 'CONFLICT' }),
a([
t({ text: 'ROLLBACK' }),
t({ text: 'ABORT' }),
t({ text: 'FAIL' }),
t({ text: 'IGNORE' }),
t({ text: 'REPLACE' }),
]),
],
(v) => v[2]
),
(v) => ({ conflict: v ? v.toUpperCase() : null })
)(ctx);
}
function checkTableConstraint(ctx) {
return s(
[
constraintName,
t({ text: 'CHECK' }, (v) => null),
t({ text: '(' }, (v) => null),
s([expression], (v) => ({ expression: v[0] })),
t({ text: ')' }, (v) => null),
],
(v) => Object.assign({ type: 'CHECK' }, ...v.filter((x) => x !== null))
)(ctx);
}
function foreignTableConstraint(ctx) {
return s(
[
constraintName,
t({ text: 'FOREIGN' }, (v) => null),
t({ text: 'KEY' }, (v) => null),
t({ text: '(' }, (v) => null),
columnNameList,
t({ text: ')' }, (v) => null),
foreignKeyClause,
],
(v) =>
Object.assign({ type: 'FOREIGN KEY' }, ...v.filter((x) => x !== null))
)(ctx);
}
function foreignKeyClause(ctx) {
return s(
[
t({ text: 'REFERENCES' }, (v) => null),
table,
columnNameListOptional,
o(m(a([deleteReference, updateReference, matchReference])), (v) =>
Object.assign({ delete: null, update: null, match: null }, ...(v || []))
),
deferrable,
],
(v) => ({ references: Object.assign({}, ...v.filter((x) => x !== null)) })
)(ctx);
}
function columnNameListOptional(ctx) {
return o(
s([t({ text: '(' }), columnNameList, t({ text: ')' })], (v) => v[1]),
(v) => ({ columns: v ? v.columns : [] })
)(ctx);
}
function columnNameList(ctx) {
return s(
[
o(m(s([identifier, t({ text: ',' })], (v) => v[0])), (v) =>
v !== null ? v : []
),
identifier,
],
(v) => ({ columns: v[0].concat([v[1]]) })
)(ctx);
}
function deleteReference(ctx) {
return s([t({ text: 'ON' }), t({ text: 'DELETE' }), onAction], (v) => ({
delete: v[2],
}))(ctx);
}
function updateReference(ctx) {
return s([t({ text: 'ON' }), t({ text: 'UPDATE' }), onAction], (v) => ({
update: v[2],
}))(ctx);
}
function matchReference(ctx) {
return s(
[t({ text: 'MATCH' }), a([t({ type: 'keyword' }), t({ type: 'id' })])],
(v) => ({ match: v[1] })
)(ctx);
}
function deferrable(ctx) {
return o(
s([
o(t({ text: 'NOT' })),
t({ text: 'DEFERRABLE' }),
o(
s(
[
t({ text: 'INITIALLY' }),
a([t({ text: 'DEFERRED' }), t({ text: 'IMMEDIATE' })]),
],
(v) => v[1].toUpperCase()
)
),
]),
(v) => ({ deferrable: v ? { not: v[0] !== null, initially: v[2] } : null })
)(ctx);
}
function constraintName(ctx) {
return o(
s([t({ text: 'CONSTRAINT' }), identifier], (v) => v[1]),
(v) => ({ name: v })
)(ctx);
}
function createIndex(ctx) {
return s(
[
t({ text: 'CREATE' }, (v) => null),
unique,
t({ text: 'INDEX' }, (v) => null),
exists,
schema,
index,
t({ text: 'ON' }, (v) => null),
table,
t({ text: '(' }, (v) => null),
indexedColumnList,
t({ text: ')' }, (v) => null),
where,
f,
],
(v) => Object.assign({}, ...v.filter((x) => x !== null))
)(ctx);
}
function unique(ctx) {
return o(t({ text: 'UNIQUE' }), (v) => ({ unique: v !== null }))(ctx);
}
function exists(ctx) {
return o(
s([t({ text: 'IF' }), t({ text: 'NOT' }), t({ text: 'EXISTS' })]),
(v) => ({ exists: v !== null })
)(ctx);
}
function schema(ctx) {
return o(
s([identifier, t({ text: '.' })], (v) => v[0]),
(v) => ({ schema: v })
)(ctx);
}
function index(ctx) {
return s([identifier], (v) => ({ index: v[0] }))(ctx);
}
function table(ctx) {
return s([identifier], (v) => ({ table: v[0] }))(ctx);
}
function where(ctx) {
return o(
s([t({ text: 'WHERE' }), expression], (v) => v[1]),
(v) => ({ where: v })
)(ctx);
}
function indexedColumnList(ctx) {
return a([
s([indexedColumn, t({ text: ',' }), indexedColumnList], (v) => ({
columns: [v[0]].concat(v[2].columns),
})),
s([indexedColumnExpression, t({ text: ',' }), indexedColumnList], (v) => ({
columns: [v[0]].concat(v[2].columns),
})),
l({ do: indexedColumn, next: t({ text: ')' }) }, (v) => ({
columns: [v],
})),
l({ do: indexedColumnExpression, next: t({ text: ')' }) }, (v) => ({
columns: [v],
})),
])(ctx);
}
function indexedColumn(ctx) {
return s(
[
s([identifier], (v) => ({ name: v[0], expression: false })),
collation,
order,
],
(v) => Object.assign({}, ...v.filter((x) => x !== null))
)(ctx);
}
function indexedColumnExpression(ctx) {
return s(
[
s([indexedExpression], (v) => ({ name: v[0], expression: true })),
collation,
order,
],
(v) => Object.assign({}, ...v.filter((x) => x !== null))
)(ctx);
}
function collation(ctx) {
return o(
s([t({ text: 'COLLATE' }), t({ type: 'id' })], (v) => v[1]),
(v) => ({ collation: v })
)(ctx);
}
function order(ctx) {
return a([t({ text: 'ASC' }), t({ text: 'DESC' }), e], (v) => ({
order: v ? v.toUpperCase() : null,
}))(ctx);
}
function indexedExpression(ctx) {
return m(
a([
n({
do: t({ type: 'keyword' }),
not: a([
t({ text: 'COLLATE' }),
t({ text: 'ASC' }),
t({ text: 'DESC' }),
]),
}),
t({ type: 'id' }),
t({ type: 'string' }),
t({ type: 'blob' }),
t({ type: 'numeric' }),
t({ type: 'variable' }),
n({
do: t({ type: 'operator' }),
not: a([t({ text: '(' }), t({ text: ')' }), t({ text: ',' })]),
}),
s([t({ text: '(' }), o(expression), t({ text: ')' })], (v) => v[1] || []),
])
)(ctx);
}
function expression(ctx) {
return m(
a([
t({ type: 'keyword' }),
t({ type: 'id' }),
t({ type: 'string' }),
t({ type: 'blob' }),
t({ type: 'numeric' }),
t({ type: 'variable' }),
n({
do: t({ type: 'operator' }),
not: a([t({ text: '(' }), t({ text: ')' })]),
}),
s([t({ text: '(' }), o(expression), t({ text: ')' })], (v) => v[1] || []),
])
)(ctx);
}
function identifier(ctx) {
return a([t({ type: 'id' }), t({ type: 'string' })], (v) =>
/^["`['][^]*["`\]']$/.test(v) ? v.substring(1, v.length - 1) : v
)(ctx);
}
function onAction(ctx) {
return a(
[
s([t({ text: 'SET' }), t({ text: 'NULL' })], (v) => `${v[0]} ${v[1]}`),
s([t({ text: 'SET' }), t({ text: 'DEFAULT' })], (v) => `${v[0]} ${v[1]}`),
t({ text: 'CASCADE' }),
t({ text: 'RESTRICT' }),
s([t({ text: 'NO' }), t({ text: 'ACTION' })], (v) => `${v[0]} ${v[1]}`),
],
(v) => v.toUpperCase()
)(ctx);
}
function literalValue(ctx) {
return a([
t({ type: 'numeric' }),
t({ type: 'string' }),
t({ type: 'id' }),
t({ type: 'blob' }),
t({ text: 'NULL' }),
t({ text: 'TRUE' }),
t({ text: 'FALSE' }),
t({ text: 'CURRENT_TIME' }),
t({ text: 'CURRENT_DATE' }),
t({ text: 'CURRENT_TIMESTAMP' }),
])(ctx);
}
function signedNumber(ctx) {
return s(
[a([t({ text: '+' }), t({ text: '-' }), e]), t({ type: 'numeric' })],
(v) => `${v[0] || ''}${v[1]}`
)(ctx);
}
module.exports = {
parseCreateTable,
parseCreateIndex,
};

View File

@@ -0,0 +1,41 @@
function copyData(sourceTable, targetTable, columns) {
return `INSERT INTO "${targetTable}" SELECT ${
columns === undefined
? '*'
: columns.map((column) => `"${column}"`).join(', ')
} FROM "${sourceTable}";`;
}
function dropOriginal(tableName) {
return `DROP TABLE "${tableName}"`;
}
function renameTable(tableName, alteredName) {
return `ALTER TABLE "${tableName}" RENAME TO "${alteredName}"`;
}
function getTableSql(tableName) {
return `SELECT type, sql FROM sqlite_master WHERE (type='table' OR (type='index' AND sql IS NOT NULL)) AND lower(tbl_name)='${tableName.toLowerCase()}'`;
}
function isForeignCheckEnabled() {
return `PRAGMA foreign_keys`;
}
function setForeignCheck(enable) {
return `PRAGMA foreign_keys = ${enable ? 'ON' : 'OFF'}`;
}
function executeForeignCheck() {
return `PRAGMA foreign_key_check`;
}
module.exports = {
copyData,
dropOriginal,
renameTable,
getTableSql,
isForeignCheckEnabled,
setForeignCheck,
executeForeignCheck,
};

View File

@@ -0,0 +1,38 @@
function tokenize(text, tokens) {
const compiledRegex = new RegExp(
Object.entries(tokens)
.map(([type, regex]) => `(?<${type}>${regex.source})`)
.join('|'),
'yi'
);
let index = 0;
const ast = [];
while (index < text.length) {
compiledRegex.lastIndex = index;
const result = text.match(compiledRegex);
if (result !== null) {
const [type, text] = Object.entries(result.groups).find(
([name, group]) => group !== undefined
);
index += text.length;
if (!type.startsWith('_')) {
ast.push({ type, text });
}
} else {
throw new Error(
`No matching tokenizer rule found at: [${text.substring(index)}]`
);
}
}
return ast;
}
module.exports = {
tokenize,
};

View File

@@ -0,0 +1,12 @@
function isEqualId(first, second) {
return first.toLowerCase() === second.toLowerCase();
}
function includesId(list, id) {
return list.some((item) => isEqualId(item, id));
}
module.exports = {
isEqualId,
includesId,
};