Socket
Socket
Sign inDemoInstall

mysql-emulator

Package Overview
Dependencies
511
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.17 to 0.0.18

dist/processor/transaction.processor.d.ts

1

dist/parser/create-table-query.d.ts

@@ -13,2 +13,3 @@ import { Create } from 'node-sql-parser';

autoIncrement: boolean | null;
onUpdateCurrentTimestamp: boolean | null;
};

@@ -15,0 +16,0 @@ export type CreateConstraint = {

@@ -19,3 +19,3 @@ "use strict";

static fromAst(ast) {
var _a, _b;
var _a, _b, _c, _d, _e, _f, _g;
const [{ db, table }] = ast.table;

@@ -35,2 +35,6 @@ const columns = [];

autoIncrement: c.auto_increment ? true : null,
onUpdateCurrentTimestamp: ((_e = (_d = (_c = c.default_val) === null || _c === void 0 ? void 0 : _c.value) === null || _d === void 0 ? void 0 : _d.over) === null || _e === void 0 ? void 0 : _e.type) === 'on update' ||
((_g = (_f = c.reference_definition) === null || _f === void 0 ? void 0 : _f.on_action[0]) === null || _g === void 0 ? void 0 : _g.type) === 'on update'
? true
: null,
});

@@ -37,0 +41,0 @@ }

@@ -17,4 +17,10 @@ import { SelectQuery } from './select-query';

distinct?: boolean;
separator?: string;
};
};
export type Interval = {
type: 'interval';
unit: 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year';
value: number;
};
export type NumberType = {

@@ -67,3 +73,3 @@ type: 'number';

isArray: boolean;
}) | CaseType | UnaryExpression | BinaryExpression | ColumnRef | FunctionType | NumberType | StringType | BooleanType | ArrayType | NullType | DefaultType | Star;
}) | CaseType | UnaryExpression | BinaryExpression | ColumnRef | FunctionType | Interval | NumberType | StringType | BooleanType | ArrayType | NullType | DefaultType | Star;
export declare const buildExpression: (ast: any) => Expression;

17

dist/parser/expression.js

@@ -7,3 +7,3 @@ "use strict";

const buildExpression = (ast) => {
var _a, _b, _c, _d;
var _a, _b, _c, _d, _e, _f;
if (ast.type === 'unary_expr') {

@@ -47,6 +47,6 @@ return {

if (ast.type === 'function' || ast.type === 'aggr_func') {
const options = ((_a = ast.args) === null || _a === void 0 ? void 0 : _a.distinct) ? { distinct: true } : {};
const args = ((_b = ast.args) === null || _b === void 0 ? void 0 : _b.type) === 'expr_list'
const options = Object.assign(Object.assign({}, (((_a = ast.args) === null || _a === void 0 ? void 0 : _a.distinct) && { distinct: true })), (((_b = ast.args) === null || _b === void 0 ? void 0 : _b.separator) && { separator: (_c = ast.args) === null || _c === void 0 ? void 0 : _c.separator.value.value }));
const args = ((_d = ast.args) === null || _d === void 0 ? void 0 : _d.type) === 'expr_list'
? ast.args.value.map(exports.buildExpression)
: ((_c = ast.args) === null || _c === void 0 ? void 0 : _c.expr)
: ((_e = ast.args) === null || _e === void 0 ? void 0 : _e.expr)
? [(0, exports.buildExpression)(ast.args.expr)]

@@ -61,2 +61,9 @@ : [];

}
if (ast.type === 'interval') {
return {
type: 'interval',
unit: ast.unit,
value: ast.expr.value,
};
}
if (ast.type === 'number') {

@@ -83,3 +90,3 @@ return {

}
if (ast.type === 'expr_list' && ((_d = ast.value[0]) === null || _d === void 0 ? void 0 : _d.ast)) {
if (ast.type === 'expr_list' && ((_f = ast.value[0]) === null || _f === void 0 ? void 0 : _f.ast)) {
return {

@@ -86,0 +93,0 @@ type: 'select',

import { Update } from 'node-sql-parser';
import { Expression } from './expression';
type Assignment = {
export type Assignment = {
table: string | null;

@@ -17,2 +17,1 @@ column: string;

}
export {};

@@ -78,3 +78,3 @@ "use strict";

case 'YEAR':
return new server_1.DateColumn(c.name, c.nullable, c.defaultValue);
return new server_1.DateColumn(c.name, c.nullable, c.defaultValue, Boolean(c.onUpdateCurrentTimestamp));
case 'ENUM':

@@ -81,0 +81,0 @@ if (c.defaultValue) {

@@ -13,7 +13,7 @@ import { BinaryExpression, CaseType, ColumnRef, Expression, FunctionType, Star, SubQuery, UnaryExpression } from '../parser';

}, row: object): any;
protected evaluateUnaryExpression(ue: UnaryExpression, row: object): any;
protected evaluateBinaryExpression(be: BinaryExpression, row: object): any;
protected evaluateCaseExpression(c: CaseType, row: object): any;
protected evaluateUnaryExpression(ue: UnaryExpression, row: object, group: object[]): any;
protected evaluateBinaryExpression(be: BinaryExpression, row: object, group: object[]): any;
protected evaluateCaseExpression(c: CaseType, row: object, group: object[]): any;
protected evaluateColumnReference(c: ColumnRef, row: object): any;
protected evaluateFunction(f: FunctionType, row: object, group: object[]): any;
}

@@ -24,7 +24,7 @@ "use strict";

case 'unary_expression':
return this.evaluateUnaryExpression(e, rowWithContext);
return this.evaluateUnaryExpression(e, rowWithContext, group);
case 'binary_expression':
return this.evaluateBinaryExpression(e, rowWithContext);
return this.evaluateBinaryExpression(e, rowWithContext, group);
case 'case':
return this.evaluateCaseExpression(e, rowWithContext);
return this.evaluateCaseExpression(e, rowWithContext, group);
case 'function':

@@ -74,4 +74,4 @@ return this.evaluateFunction(e, rowWithContext, group);

}
evaluateUnaryExpression(ue, row) {
const value = this.evaluateExpression(ue.expression, row);
evaluateUnaryExpression(ue, row, group) {
const value = this.evaluateExpression(ue.expression, row, group);
switch (ue.operator) {

@@ -83,3 +83,3 @@ case 'NOT':

}
evaluateBinaryExpression(be, row) {
evaluateBinaryExpression(be, row, group) {
const handler = binary_operators_1.binaryOperators[be.operator];

@@ -89,13 +89,13 @@ if (!handler) {

}
const left = this.evaluateExpression(be.left, row);
const right = this.evaluateExpression(be.right, row);
const left = this.evaluateExpression(be.left, row, group);
const right = this.evaluateExpression(be.right, row, group);
return handler(left, right);
}
evaluateCaseExpression(c, row) {
evaluateCaseExpression(c, row, group) {
for (const { condition, value } of c.when) {
if (this.evaluateExpression(condition, row)) {
return this.evaluateExpression(value, row);
if (this.evaluateExpression(condition, row, group)) {
return this.evaluateExpression(value, row, group);
}
}
return c.else ? this.evaluateExpression(c.else, row) : null;
return c.else ? this.evaluateExpression(c.else, row, group) : null;
}

@@ -102,0 +102,0 @@ evaluateColumnReference(c, row) {

@@ -15,2 +15,28 @@ "use strict";

};
const applyInterval = (d, i, k) => {
if (i.unit === 'second') {
d.setSeconds(d.getSeconds() + k * i.value);
}
else if (i.unit === 'minute') {
d.setMinutes(d.getMinutes() + k * i.value);
}
else if (i.unit === 'hour') {
d.setHours(d.getHours() + k * i.value);
}
else if (i.unit === 'day') {
d.setDate(d.getDate() + k * i.value);
}
else if (i.unit === 'week') {
d.setDate(d.getDate() + k * i.value * 7);
}
else if (i.unit === 'month') {
d.setMonth(d.getMonth() + k * i.value);
}
else if (i.unit === 'year') {
d.setFullYear(d.getFullYear() + k * i.value);
}
else {
throw new evaluator_exception_1.EvaluatorException(`Unknown interval unit '${i.unit}'`);
}
};
exports.functions = {

@@ -75,2 +101,9 @@ database: (e) => e.getServer().getDatabase(null).getName(),

},
group_concat: (e, f, row, group) => {
var _a;
if (group.length === 0) {
return null;
}
return group.map((row) => e.evaluateExpression(getArgument(f), row)).join((_a = f.options.separator) !== null && _a !== void 0 ? _a : ',');
},
concat: (e, f, row) => {

@@ -227,2 +260,18 @@ var _a;

},
unix_timestamp: (e, f, row) => {
var _a, _b;
if (((_a = f.args) === null || _a === void 0 ? void 0 : _a.length) !== 0 && ((_b = f.args) === null || _b === void 0 ? void 0 : _b.length) !== 1) {
throw new evaluator_exception_1.EvaluatorException(`Incorrect parameter count in the call to native function '${f.name}'`);
}
const d = f.args.length === 0 ? new Date() : (0, utils_1.toDate)(e.evaluateExpression(getArgument(f), row));
if (d === null) {
return null;
}
return ((d.getTime() / 1000) | 0) - d.getTimezoneOffset() * 60;
},
from_unixtime: (e, f, row) => {
const value = e.evaluateExpression(getArgument(f), row);
const d = new Date(value * 1000);
return new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds());
},
last_day: (e, f, row) => {

@@ -277,2 +326,34 @@ const value = e.evaluateExpression(getArgument(f), row);

},
date_add: (e, f, row) => {
var _a;
if (((_a = f.args) === null || _a === void 0 ? void 0 : _a.length) !== 2) {
throw new evaluator_exception_1.EvaluatorException(`Incorrect parameter count in the call to native function '${f.name}'`);
}
const [valueArg, interval] = f.args;
if (interval.type !== 'interval') {
throw new evaluator_exception_1.EvaluatorException(`Could not evaluate function '${f.name}'`);
}
const d = (0, utils_1.toDate)(e.evaluateExpression(valueArg, row));
if (d === null) {
return null;
}
applyInterval(d, interval, 1);
return (0, utils_1.toDateString)(d);
},
date_sub: (e, f, row) => {
var _a;
if (((_a = f.args) === null || _a === void 0 ? void 0 : _a.length) !== 2) {
throw new evaluator_exception_1.EvaluatorException(`Incorrect parameter count in the call to native function '${f.name}'`);
}
const [valueArg, interval] = f.args;
if (interval.type !== 'interval') {
throw new evaluator_exception_1.EvaluatorException(`Could not evaluate function '${f.name}'`);
}
const d = (0, utils_1.toDate)(e.evaluateExpression(valueArg, row));
if (d === null) {
return null;
}
applyInterval(d, interval, -1);
return (0, utils_1.toDateString)(d);
},
date_format: (e, f, row) => {

@@ -304,5 +385,8 @@ var _a;

m: month.toString().padStart(2, '0'),
D: [1, 21, 31].includes(date) ? `${date}st`
: [2, 22].includes(date) ? `${date}nd`
: [3, 23].includes(date) ? `${date}rd`
D: [1, 21, 31].includes(date)
? `${date}st`
: [2, 22].includes(date)
? `${date}nd`
: [3, 23].includes(date)
? `${date}rd`
: `${date}th`,

@@ -321,12 +405,10 @@ d: date.toString().padStart(2, '0'),

f: milliseconds.toString().padStart(3, '0') + '000',
r: [standardHours, minutes, seconds]
.map((n) => n.toString().padStart(2, '0'))
.join(':') + ' ' + meridiem,
T: [hours, minutes, seconds]
.map((n) => n.toString().padStart(2, '0'))
.join(':'),
r: [standardHours, minutes, seconds].map((n) => n.toString().padStart(2, '0')).join(':') + ' ' + meridiem,
T: [hours, minutes, seconds].map((n) => n.toString().padStart(2, '0')).join(':'),
w: d.getDay(),
W: d.toLocaleDateString('default', { weekday: 'long' }),
a: d.toLocaleDateString('default', { weekday: 'short' }),
j: Math.floor((d.getTime() - new Date(year, 0, 0).getTime()) / 864e5).toString().padStart(3, '0'),
j: Math.floor((d.getTime() - new Date(year, 0, 0).getTime()) / 864e5)
.toString()
.padStart(3, '0'),
}).reduce((res, [key, value]) => res.replace('%' + key, value), format);

@@ -333,0 +415,0 @@ },

@@ -12,2 +12,3 @@ "use strict";

const show_processor_1 = require("./show.processor");
const transaction_processor_1 = require("./transaction.processor");
class Processor {

@@ -21,3 +22,4 @@ constructor(server) {

if (query instanceof parser_1.TransactionQuery) {
return;
const p = new transaction_processor_1.TransactionProcessor(this.server);
return p.process(query);
}

@@ -24,0 +26,0 @@ if (query instanceof parser_1.ShowQuery) {

@@ -196,3 +196,3 @@ "use strict";

};
const checkIfKeep = (row) => {
const checkIfKeep = (row, group) => {
if (this.query.having === null) {

@@ -202,3 +202,3 @@ return true;

try {
return this.evaluator.evaluateExpression(this.query.having, row);
return this.evaluator.evaluateExpression(this.query.having, row, group);
}

@@ -217,3 +217,3 @@ catch (err) {

const [mappedRow, rawRowWithAliases] = mapRow(rawRow, []);
if (checkIfKeep(rawRowWithAliases)) {
if (checkIfKeep(rawRowWithAliases, [])) {
this.rows.push(mappedRow);

@@ -228,3 +228,3 @@ }

const [mappedRow, rawRowWithAliases] = mapRow(firstRawRow, group);
if (checkIfKeep(rawRowWithAliases)) {
if (checkIfKeep(rawRowWithAliases, group)) {
this.rows.push(mappedRow);

@@ -231,0 +231,0 @@ }

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UpdateProcessor = void 0;
const server_1 = require("../server");
const utils_1 = require("../utils");

@@ -24,2 +25,3 @@ const evaluator_1 = require("./evaluator");

};
const assignmentMap = new Map(query.assignments.map((a) => [a.column, a]));
let changedRows = 0;

@@ -48,6 +50,10 @@ let affectedRows = 0;

}, existingRow);
if (existingRow !== updatedRow) {
changedRows++;
if (existingRow === updatedRow) {
return existingRow;
}
return updatedRow;
changedRows++;
return columnDefinitions.reduce((row, c) => {
return c instanceof server_1.DateColumn && c.hasOnUpdateCurrentTimestamp() && !assignmentMap.has(c.getName())
? Object.assign(Object.assign({}, row), { [c.getName()]: new Date() }) : row;
}, updatedRow);
});

@@ -54,0 +60,0 @@ table.setRows(updatedRows);

import { Column } from '../column';
import { Expression } from '../../parser';
export declare class DateColumn extends Column {
constructor(name: string, nullable: boolean, defaultValue: Expression | null);
protected onUpdateCurrentTimestamp: boolean;
constructor(name: string, nullable: boolean, defaultValue: Expression | null, onUpdateCurrentTimestamp: boolean);
cast(value: any): Date | null;
hasOnUpdateCurrentTimestamp(): boolean;
}

@@ -5,8 +5,16 @@ "use strict";

const column_1 = require("../column");
const utils_1 = require("../../utils");
class DateColumn extends column_1.Column {
constructor(name, nullable, defaultValue) {
constructor(name, nullable, defaultValue, onUpdateCurrentTimestamp) {
super(name, nullable, defaultValue);
this.onUpdateCurrentTimestamp = onUpdateCurrentTimestamp;
}
cast(value) {
return (0, utils_1.toDate)(value);
}
hasOnUpdateCurrentTimestamp() {
return this.onUpdateCurrentTimestamp;
}
}
exports.DateColumn = DateColumn;
//# sourceMappingURL=date-column.js.map

@@ -5,2 +5,3 @@ import { Table } from './table';

protected tables: Map<string, Table>;
protected snapshots: Map<string, object[]>;
constructor(name: string);

@@ -12,2 +13,6 @@ createTable(name: string): Table;

getName(): string;
snapshot(): void;
hasSnapshot(): boolean;
restoreSnapshot(): void;
deleteSnapshot(): void;
}

@@ -10,2 +10,3 @@ "use strict";

this.tables = new Map();
this.snapshots = new Map();
}

@@ -52,4 +53,21 @@ createTable(name) {

}
snapshot() {
this.tables.forEach((t) => {
this.snapshots.set(t.getName(), [...t.getRows()]);
});
}
hasSnapshot() {
return this.snapshots.size > 0;
}
restoreSnapshot() {
this.snapshots.forEach((rows, tableName) => {
this.getTable(tableName).setRows(rows);
});
this.deleteSnapshot();
}
deleteSnapshot() {
this.snapshots = new Map();
}
}
exports.Database = Database;
//# sourceMappingURL=database.js.map

@@ -13,3 +13,2 @@ import { Column } from './column';

addConstraint(c: UniqueConstraint): void;
getConstraints(): UniqueConstraint[];
insertRow(row: object): void;

@@ -16,0 +15,0 @@ getRows(): object[];

@@ -23,5 +23,2 @@ "use strict";

}
getConstraints() {
return this.constraints;
}
insertRow(row) {

@@ -28,0 +25,0 @@ for (const constraint of this.constraints) {

@@ -13,2 +13,3 @@ export declare const mapKeys: (o: object, mapper: (key: string, value: any) => string, filter?: (key: string, value: any) => boolean) => object;

export declare const isString: (s: any) => s is string;
export declare const toDateString: (d: Date) => string;
export declare const toDate: (value: any) => Date | null;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toDate = exports.isString = exports.isObject = exports.toNumber = exports.hashCode = exports.sortBy = exports.extractTable = exports.extractColumn = exports.mapKeys = void 0;
exports.toDate = exports.toDateString = exports.isString = exports.isObject = exports.toNumber = exports.hashCode = exports.sortBy = exports.extractTable = exports.extractColumn = exports.mapKeys = void 0;
const mapKeys = (o, mapper, filter = () => true) => {

@@ -66,2 +66,16 @@ return Object.keys(o).reduce((res, key) => {

exports.isString = isString;
const toDateString = (d) => {
const date = [
d.getFullYear(),
(d.getMonth() + 1).toString().padStart(2, '0'),
d.getDate().toString().padStart(2, '0'),
].join('-');
const time = [
d.getHours().toString().padStart(2, '0'),
d.getMinutes().toString().padStart(2, '0'),
d.getSeconds().toString().padStart(2, '0'),
].join(':');
return `${date} ${time}`;
};
exports.toDateString = toDateString;
const toDate = (value) => {

@@ -68,0 +82,0 @@ if (!value) {

{
"name": "mysql-emulator",
"version": "0.0.17",
"version": "0.0.18",
"description": "MySQL emulator",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc