@sequeljs/ast
Version:
A SQL AST manager for JavaScript
101 lines • 3.57 kB
JavaScript
import Limit from '../nodes/Limit';
import SQLLiteral from '../nodes/SQLLiteral';
import Union from '../nodes/Union';
import ToSQL from './ToSQL';
export default class MySQL extends ToSQL {
visitBin(thing, col) {
let collector = col;
collector.append('BINARY ');
collector = this.visit(thing.expr, collector);
return collector;
}
visitConcat(thing, col) {
let collector = col;
collector.append(' CONCAT(');
collector = this.visit(thing.left, collector);
collector.append(', ');
collector = this.visit(thing.right, collector);
collector.append(') ');
return collector;
}
visitIsDistinctFrom(thing, col) {
let collector = col;
collector.append('NOT ');
collector = this.visitIsNotDistinctFrom(thing, collector);
return collector;
}
visitIsNotDistinctFrom(thing, col) {
let collector = col;
collector = this.visit(thing.left, collector);
collector.append(' <=> ');
collector = this.visit(thing.right, collector);
return collector;
}
visitSelectCore(thing, col) {
if (!thing.from) {
// eslint-disable-next-line no-param-reassign
thing.from = new SQLLiteral('DUAL');
}
return super.visitSelectCore(thing, col);
}
visitSelectStatement(thing, col) {
if (thing.offset && !thing.limit) {
/**
* https://dev.mysql.com/doc/refman/8.0/en/select.html
* To retrieve all rows from a certain offset up to the end of the result
* set, you can use some large number for the second parameter.
*/
// eslint-disable-next-line no-param-reassign
thing.limit = new Limit(new SQLLiteral('18446744073709551615'));
}
return super.visitSelectStatement(thing, col);
}
visitUnion(thing, col, suppressParens = false) {
let collector = col;
if (!suppressParens) {
collector.append('( ');
}
if (thing.left instanceof Union) {
collector = this.visitUnion(thing.left, collector, true);
}
else {
collector = this.visit(thing.left, collector);
}
collector.append(' UNION ');
if (thing.right instanceof Union) {
collector = this.visitUnion(thing.right, collector, true);
}
else {
collector = this.visit(thing.right, collector);
}
if (!suppressParens) {
collector.append(' )');
}
return collector;
}
visitUnqualifiedColumn(thing, col) {
let collector = col;
collector = this.visit(thing.expr, collector);
return collector;
}
visitUpdateStatement(thing, col) {
let collector = col;
collector.append('UPDATE ');
collector = this.visit(thing.relation, collector);
if (thing.values.length > 0) {
collector.append(' SET ');
collector = this.injectJoin(thing.values, collector, ', ');
}
if (thing.wheres.length > 0) {
collector.append(' WHERE ');
collector = this.injectJoin(thing.wheres, collector, ' AND ');
}
if (thing.orders.length > 0) {
collector.append(' ORDER BY ');
collector = this.injectJoin(thing.orders, collector, ', ');
}
collector = this.maybeVisit(thing.limit, collector);
return collector;
}
}
//# sourceMappingURL=MySQL.js.map