primer cambio

This commit is contained in:
beseira13
2026-01-19 12:12:38 -03:00
parent 5f59dba52d
commit 44990f015a
4759 changed files with 588702 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
"use strict";
const fs = require("fs");
const path = require("path");
const AbstractConnectionManager = require("../abstract/connection-manager");
const { logger } = require("../../utils/logger");
const debug = logger.debugContext("connection:sqlite");
const dataTypes = require("../../data-types").sqlite;
const sequelizeErrors = require("../../errors");
const parserStore = require("../parserStore")("sqlite");
const { promisify } = require("util");
class ConnectionManager extends AbstractConnectionManager {
constructor(dialect, sequelize) {
super(dialect, sequelize);
if (this.sequelize.options.host === "localhost") {
delete this.sequelize.options.host;
}
this.connections = {};
this.lib = this._loadDialectModule("sqlite3");
this.refreshTypeParser(dataTypes);
}
async _onProcessExit() {
await Promise.all(Object.getOwnPropertyNames(this.connections).map((connection) => promisify((callback) => this.connections[connection].close(callback))()));
return super._onProcessExit.call(this);
}
_refreshTypeParser(dataType) {
parserStore.refresh(dataType);
}
_clearTypeParser() {
parserStore.clear();
}
async getConnection(options) {
options = options || {};
options.uuid = options.uuid || "default";
if (!!this.sequelize.options.storage !== null && this.sequelize.options.storage !== void 0) {
options.storage = this.sequelize.options.storage;
} else {
options.storage = this.sequelize.options.host || ":memory:";
}
options.inMemory = options.storage === ":memory:" ? 1 : 0;
const dialectOptions = this.sequelize.options.dialectOptions;
const defaultReadWriteMode = this.lib.OPEN_READWRITE | this.lib.OPEN_CREATE;
options.readWriteMode = dialectOptions && dialectOptions.mode || defaultReadWriteMode;
if (this.connections[options.inMemory || options.uuid]) {
return this.connections[options.inMemory || options.uuid];
}
if (!options.inMemory && (options.readWriteMode & this.lib.OPEN_CREATE) !== 0) {
fs.mkdirSync(path.dirname(options.storage), { recursive: true });
}
const connection = await new Promise((resolve, reject) => {
this.connections[options.inMemory || options.uuid] = new this.lib.Database(options.storage, options.readWriteMode, (err) => {
if (err)
return reject(new sequelizeErrors.ConnectionError(err));
debug(`connection acquired ${options.uuid}`);
resolve(this.connections[options.inMemory || options.uuid]);
});
});
if (this.sequelize.config.password) {
connection.run(`PRAGMA KEY=${this.sequelize.escape(this.sequelize.config.password)}`);
}
if (this.sequelize.options.foreignKeys !== false) {
connection.run("PRAGMA FOREIGN_KEYS=ON");
}
return connection;
}
releaseConnection(connection, force) {
if (connection.filename === ":memory:" && force !== true)
return;
if (connection.uuid) {
connection.close();
debug(`connection released ${connection.uuid}`);
delete this.connections[connection.uuid];
}
}
}
module.exports = ConnectionManager;
module.exports.ConnectionManager = ConnectionManager;
module.exports.default = ConnectionManager;
//# sourceMappingURL=connection-manager.js.map

View File

@@ -0,0 +1,7 @@
{
"version": 3,
"sources": ["../../../src/dialects/sqlite/connection-manager.js"],
"sourcesContent": ["'use strict';\n\nconst fs = require('fs');\nconst path = require('path');\nconst AbstractConnectionManager = require('../abstract/connection-manager');\nconst { logger } = require('../../utils/logger');\nconst debug = logger.debugContext('connection:sqlite');\nconst dataTypes = require('../../data-types').sqlite;\nconst sequelizeErrors = require('../../errors');\nconst parserStore = require('../parserStore')('sqlite');\nconst { promisify } = require('util');\n\nclass ConnectionManager extends AbstractConnectionManager {\n constructor(dialect, sequelize) {\n super(dialect, sequelize);\n\n // We attempt to parse file location from a connection uri\n // but we shouldn't match sequelize default host.\n if (this.sequelize.options.host === 'localhost') {\n delete this.sequelize.options.host;\n }\n\n this.connections = {};\n this.lib = this._loadDialectModule('sqlite3');\n this.refreshTypeParser(dataTypes);\n }\n\n async _onProcessExit() {\n await Promise.all(\n Object.getOwnPropertyNames(this.connections)\n .map(connection => promisify(callback => this.connections[connection].close(callback))())\n );\n return super._onProcessExit.call(this);\n }\n\n // Expose this as a method so that the parsing may be updated when the user has added additional, custom types\n _refreshTypeParser(dataType) {\n parserStore.refresh(dataType);\n }\n\n _clearTypeParser() {\n parserStore.clear();\n }\n\n async getConnection(options) {\n options = options || {};\n options.uuid = options.uuid || 'default';\n\n if (!!this.sequelize.options.storage !== null && this.sequelize.options.storage !== undefined) {\n // Check explicitely for the storage option to not be set since an empty string signals\n // SQLite will create a temporary disk-based database in that case.\n options.storage = this.sequelize.options.storage;\n } else {\n options.storage = this.sequelize.options.host || ':memory:';\n }\n\n options.inMemory = options.storage === ':memory:' ? 1 : 0;\n\n const dialectOptions = this.sequelize.options.dialectOptions;\n const defaultReadWriteMode = this.lib.OPEN_READWRITE | this.lib.OPEN_CREATE;\n\n options.readWriteMode = dialectOptions && dialectOptions.mode || defaultReadWriteMode;\n\n if (this.connections[options.inMemory || options.uuid]) {\n return this.connections[options.inMemory || options.uuid];\n }\n\n if (!options.inMemory && (options.readWriteMode & this.lib.OPEN_CREATE) !== 0) {\n // automatic path provision for `options.storage`\n fs.mkdirSync(path.dirname(options.storage), { recursive: true });\n }\n\n const connection = await new Promise((resolve, reject) => {\n this.connections[options.inMemory || options.uuid] = new this.lib.Database(\n options.storage,\n options.readWriteMode,\n err => {\n if (err) return reject(new sequelizeErrors.ConnectionError(err));\n debug(`connection acquired ${options.uuid}`);\n resolve(this.connections[options.inMemory || options.uuid]);\n }\n );\n });\n\n if (this.sequelize.config.password) {\n // Make it possible to define and use password for sqlite encryption plugin like sqlcipher\n connection.run(`PRAGMA KEY=${this.sequelize.escape(this.sequelize.config.password)}`);\n }\n if (this.sequelize.options.foreignKeys !== false) {\n // Make it possible to define and use foreign key constraints unless\n // explicitly disallowed. It's still opt-in per relation\n connection.run('PRAGMA FOREIGN_KEYS=ON');\n }\n\n return connection;\n }\n\n releaseConnection(connection, force) {\n if (connection.filename === ':memory:' && force !== true) return;\n\n if (connection.uuid) {\n connection.close();\n debug(`connection released ${connection.uuid}`);\n delete this.connections[connection.uuid];\n }\n }\n}\n\nmodule.exports = ConnectionManager;\nmodule.exports.ConnectionManager = ConnectionManager;\nmodule.exports.default = ConnectionManager;\n"],
"mappings": ";AAEA,MAAM,KAAK,QAAQ;AACnB,MAAM,OAAO,QAAQ;AACrB,MAAM,4BAA4B,QAAQ;AAC1C,MAAM,EAAE,WAAW,QAAQ;AAC3B,MAAM,QAAQ,OAAO,aAAa;AAClC,MAAM,YAAY,QAAQ,oBAAoB;AAC9C,MAAM,kBAAkB,QAAQ;AAChC,MAAM,cAAc,QAAQ,kBAAkB;AAC9C,MAAM,EAAE,cAAc,QAAQ;AAE9B,gCAAgC,0BAA0B;AAAA,EACxD,YAAY,SAAS,WAAW;AAC9B,UAAM,SAAS;AAIf,QAAI,KAAK,UAAU,QAAQ,SAAS,aAAa;AAC/C,aAAO,KAAK,UAAU,QAAQ;AAAA;AAGhC,SAAK,cAAc;AACnB,SAAK,MAAM,KAAK,mBAAmB;AACnC,SAAK,kBAAkB;AAAA;AAAA,QAGnB,iBAAiB;AACrB,UAAM,QAAQ,IACZ,OAAO,oBAAoB,KAAK,aAC7B,IAAI,gBAAc,UAAU,cAAY,KAAK,YAAY,YAAY,MAAM;AAEhF,WAAO,MAAM,eAAe,KAAK;AAAA;AAAA,EAInC,mBAAmB,UAAU;AAC3B,gBAAY,QAAQ;AAAA;AAAA,EAGtB,mBAAmB;AACjB,gBAAY;AAAA;AAAA,QAGR,cAAc,SAAS;AAC3B,cAAU,WAAW;AACrB,YAAQ,OAAO,QAAQ,QAAQ;AAE/B,QAAI,CAAC,CAAC,KAAK,UAAU,QAAQ,YAAY,QAAQ,KAAK,UAAU,QAAQ,YAAY,QAAW;AAG7F,cAAQ,UAAU,KAAK,UAAU,QAAQ;AAAA,WACpC;AACL,cAAQ,UAAU,KAAK,UAAU,QAAQ,QAAQ;AAAA;AAGnD,YAAQ,WAAW,QAAQ,YAAY,aAAa,IAAI;AAExD,UAAM,iBAAiB,KAAK,UAAU,QAAQ;AAC9C,UAAM,uBAAuB,KAAK,IAAI,iBAAiB,KAAK,IAAI;AAEhE,YAAQ,gBAAgB,kBAAkB,eAAe,QAAQ;AAEjE,QAAI,KAAK,YAAY,QAAQ,YAAY,QAAQ,OAAO;AACtD,aAAO,KAAK,YAAY,QAAQ,YAAY,QAAQ;AAAA;AAGtD,QAAI,CAAC,QAAQ,YAAa,SAAQ,gBAAgB,KAAK,IAAI,iBAAiB,GAAG;AAE7E,SAAG,UAAU,KAAK,QAAQ,QAAQ,UAAU,EAAE,WAAW;AAAA;AAG3D,UAAM,aAAa,MAAM,IAAI,QAAQ,CAAC,SAAS,WAAW;AACxD,WAAK,YAAY,QAAQ,YAAY,QAAQ,QAAQ,IAAI,KAAK,IAAI,SAChE,QAAQ,SACR,QAAQ,eACR,SAAO;AACL,YAAI;AAAK,iBAAO,OAAO,IAAI,gBAAgB,gBAAgB;AAC3D,cAAM,uBAAuB,QAAQ;AACrC,gBAAQ,KAAK,YAAY,QAAQ,YAAY,QAAQ;AAAA;AAAA;AAK3D,QAAI,KAAK,UAAU,OAAO,UAAU;AAElC,iBAAW,IAAI,cAAc,KAAK,UAAU,OAAO,KAAK,UAAU,OAAO;AAAA;AAE3E,QAAI,KAAK,UAAU,QAAQ,gBAAgB,OAAO;AAGhD,iBAAW,IAAI;AAAA;AAGjB,WAAO;AAAA;AAAA,EAGT,kBAAkB,YAAY,OAAO;AACnC,QAAI,WAAW,aAAa,cAAc,UAAU;AAAM;AAE1D,QAAI,WAAW,MAAM;AACnB,iBAAW;AACX,YAAM,uBAAuB,WAAW;AACxC,aAAO,KAAK,YAAY,WAAW;AAAA;AAAA;AAAA;AAKzC,OAAO,UAAU;AACjB,OAAO,QAAQ,oBAAoB;AACnC,OAAO,QAAQ,UAAU;",
"names": []
}

View File

@@ -0,0 +1,180 @@
"use strict";
module.exports = (BaseTypes) => {
const warn = BaseTypes.ABSTRACT.warn.bind(void 0, "https://www.sqlite.org/datatype3.html");
function removeUnsupportedIntegerOptions(dataType) {
if (dataType._zerofill || dataType._unsigned) {
warn(`SQLite does not support '${dataType.key}' with UNSIGNED or ZEROFILL. Plain '${dataType.key}' will be used instead.`);
dataType._unsigned = void 0;
dataType._zerofill = void 0;
}
}
BaseTypes.DATE.types.sqlite = ["DATETIME"];
BaseTypes.STRING.types.sqlite = ["VARCHAR", "VARCHAR BINARY"];
BaseTypes.CHAR.types.sqlite = ["CHAR", "CHAR BINARY"];
BaseTypes.TEXT.types.sqlite = ["TEXT"];
BaseTypes.TINYINT.types.sqlite = ["TINYINT"];
BaseTypes.SMALLINT.types.sqlite = ["SMALLINT"];
BaseTypes.MEDIUMINT.types.sqlite = ["MEDIUMINT"];
BaseTypes.INTEGER.types.sqlite = ["INTEGER"];
BaseTypes.BIGINT.types.sqlite = ["BIGINT"];
BaseTypes.FLOAT.types.sqlite = ["FLOAT"];
BaseTypes.TIME.types.sqlite = ["TIME"];
BaseTypes.DATEONLY.types.sqlite = ["DATE"];
BaseTypes.BOOLEAN.types.sqlite = ["TINYINT"];
BaseTypes.BLOB.types.sqlite = ["TINYBLOB", "BLOB", "LONGBLOB"];
BaseTypes.DECIMAL.types.sqlite = ["DECIMAL"];
BaseTypes.UUID.types.sqlite = ["UUID"];
BaseTypes.ENUM.types.sqlite = false;
BaseTypes.REAL.types.sqlite = ["REAL"];
BaseTypes.DOUBLE.types.sqlite = ["DOUBLE PRECISION"];
BaseTypes.GEOMETRY.types.sqlite = false;
BaseTypes.JSON.types.sqlite = ["JSON", "JSONB"];
class JSONTYPE extends BaseTypes.JSON {
static parse(data) {
return JSON.parse(data);
}
}
class DATE extends BaseTypes.DATE {
static parse(date, options) {
if (!date.includes("+")) {
return new Date(date + options.timezone);
}
return new Date(date);
}
}
class DATEONLY extends BaseTypes.DATEONLY {
static parse(date) {
return date;
}
}
class STRING extends BaseTypes.STRING {
toSql() {
if (this._binary) {
return `VARCHAR BINARY(${this._length})`;
}
return super.toSql(this);
}
}
class TEXT extends BaseTypes.TEXT {
toSql() {
if (this._length) {
warn("SQLite does not support TEXT with options. Plain `TEXT` will be used instead.");
this._length = void 0;
}
return "TEXT";
}
}
class CITEXT extends BaseTypes.CITEXT {
toSql() {
return "TEXT COLLATE NOCASE";
}
}
class CHAR extends BaseTypes.CHAR {
toSql() {
if (this._binary) {
return `CHAR BINARY(${this._length})`;
}
return super.toSql();
}
}
class NUMBER extends BaseTypes.NUMBER {
toSql() {
let result = this.key;
if (this._unsigned) {
result += " UNSIGNED";
}
if (this._zerofill) {
result += " ZEROFILL";
}
if (this._length) {
result += `(${this._length}`;
if (typeof this._decimals === "number") {
result += `,${this._decimals}`;
}
result += ")";
}
return result;
}
}
class TINYINT extends BaseTypes.TINYINT {
constructor(length) {
super(length);
removeUnsupportedIntegerOptions(this);
}
}
class SMALLINT extends BaseTypes.SMALLINT {
constructor(length) {
super(length);
removeUnsupportedIntegerOptions(this);
}
}
class MEDIUMINT extends BaseTypes.MEDIUMINT {
constructor(length) {
super(length);
removeUnsupportedIntegerOptions(this);
}
}
class INTEGER extends BaseTypes.INTEGER {
constructor(length) {
super(length);
removeUnsupportedIntegerOptions(this);
}
}
class BIGINT extends BaseTypes.BIGINT {
constructor(length) {
super(length);
removeUnsupportedIntegerOptions(this);
}
}
class FLOAT extends BaseTypes.FLOAT {
}
class DOUBLE extends BaseTypes.DOUBLE {
}
class REAL extends BaseTypes.REAL {
}
function parseFloating(value) {
if (typeof value !== "string") {
return value;
}
if (value === "NaN") {
return NaN;
}
if (value === "Infinity") {
return Infinity;
}
if (value === "-Infinity") {
return -Infinity;
}
}
for (const floating of [FLOAT, DOUBLE, REAL]) {
floating.parse = parseFloating;
}
for (const num of [FLOAT, DOUBLE, REAL, TINYINT, SMALLINT, MEDIUMINT, INTEGER, BIGINT]) {
num.prototype.toSql = NUMBER.prototype.toSql;
}
class ENUM extends BaseTypes.ENUM {
toSql() {
return "TEXT";
}
}
return {
DATE,
DATEONLY,
STRING,
CHAR,
NUMBER,
FLOAT,
REAL,
"DOUBLE PRECISION": DOUBLE,
TINYINT,
SMALLINT,
MEDIUMINT,
INTEGER,
BIGINT,
TEXT,
ENUM,
JSON: JSONTYPE,
CITEXT
};
};
//# sourceMappingURL=data-types.js.map

File diff suppressed because one or more lines are too long

57
node_modules/sequelize/lib/dialects/sqlite/index.js generated vendored Normal file
View File

@@ -0,0 +1,57 @@
"use strict";
const _ = require("lodash");
const AbstractDialect = require("../abstract");
const ConnectionManager = require("./connection-manager");
const Query = require("./query");
const QueryGenerator = require("./query-generator");
const DataTypes = require("../../data-types").sqlite;
const { SQLiteQueryInterface } = require("./query-interface");
class SqliteDialect extends AbstractDialect {
constructor(sequelize) {
super();
this.sequelize = sequelize;
this.connectionManager = new ConnectionManager(this, sequelize);
this.queryGenerator = new QueryGenerator({
_dialect: this,
sequelize
});
this.queryInterface = new SQLiteQueryInterface(sequelize, this.queryGenerator);
}
}
SqliteDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototype.supports), {
DEFAULT: false,
"DEFAULT VALUES": true,
"UNION ALL": false,
"RIGHT JOIN": false,
inserts: {
ignoreDuplicates: " OR IGNORE",
updateOnDuplicate: " ON CONFLICT DO UPDATE SET",
conflictFields: true,
onConflictWhere: true
},
index: {
using: false,
where: true,
functionBased: true
},
transactionOptions: {
type: true
},
constraints: {
addConstraint: false,
dropConstraint: false
},
groupedLimit: false,
JSON: true
});
SqliteDialect.prototype.defaultVersion = "3.8.0";
SqliteDialect.prototype.Query = Query;
SqliteDialect.prototype.DataTypes = DataTypes;
SqliteDialect.prototype.name = "sqlite";
SqliteDialect.prototype.TICK_CHAR = "`";
SqliteDialect.prototype.TICK_CHAR_LEFT = SqliteDialect.prototype.TICK_CHAR;
SqliteDialect.prototype.TICK_CHAR_RIGHT = SqliteDialect.prototype.TICK_CHAR;
module.exports = SqliteDialect;
module.exports.SqliteDialect = SqliteDialect;
module.exports.default = SqliteDialect;
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1,7 @@
{
"version": 3,
"sources": ["../../../src/dialects/sqlite/index.js"],
"sourcesContent": ["'use strict';\n\nconst _ = require('lodash');\nconst AbstractDialect = require('../abstract');\nconst ConnectionManager = require('./connection-manager');\nconst Query = require('./query');\nconst QueryGenerator = require('./query-generator');\nconst DataTypes = require('../../data-types').sqlite;\nconst { SQLiteQueryInterface } = require('./query-interface');\n\nclass SqliteDialect extends AbstractDialect {\n constructor(sequelize) {\n super();\n this.sequelize = sequelize;\n this.connectionManager = new ConnectionManager(this, sequelize);\n this.queryGenerator = new QueryGenerator({\n _dialect: this,\n sequelize\n });\n\n this.queryInterface = new SQLiteQueryInterface(\n sequelize,\n this.queryGenerator\n );\n }\n}\n\nSqliteDialect.prototype.supports = _.merge(\n _.cloneDeep(AbstractDialect.prototype.supports),\n {\n DEFAULT: false,\n 'DEFAULT VALUES': true,\n 'UNION ALL': false,\n 'RIGHT JOIN': false,\n inserts: {\n ignoreDuplicates: ' OR IGNORE',\n updateOnDuplicate: ' ON CONFLICT DO UPDATE SET',\n conflictFields: true,\n onConflictWhere: true\n },\n index: {\n using: false,\n where: true,\n functionBased: true\n },\n transactionOptions: {\n type: true\n },\n constraints: {\n addConstraint: false,\n dropConstraint: false\n },\n groupedLimit: false,\n JSON: true\n }\n);\n\nSqliteDialect.prototype.defaultVersion = '3.8.0'; // minimum supported version\nSqliteDialect.prototype.Query = Query;\nSqliteDialect.prototype.DataTypes = DataTypes;\nSqliteDialect.prototype.name = 'sqlite';\nSqliteDialect.prototype.TICK_CHAR = '`';\nSqliteDialect.prototype.TICK_CHAR_LEFT = SqliteDialect.prototype.TICK_CHAR;\nSqliteDialect.prototype.TICK_CHAR_RIGHT = SqliteDialect.prototype.TICK_CHAR;\n\nmodule.exports = SqliteDialect;\nmodule.exports.SqliteDialect = SqliteDialect;\nmodule.exports.default = SqliteDialect;\n"],
"mappings": ";AAEA,MAAM,IAAI,QAAQ;AAClB,MAAM,kBAAkB,QAAQ;AAChC,MAAM,oBAAoB,QAAQ;AAClC,MAAM,QAAQ,QAAQ;AACtB,MAAM,iBAAiB,QAAQ;AAC/B,MAAM,YAAY,QAAQ,oBAAoB;AAC9C,MAAM,EAAE,yBAAyB,QAAQ;AAEzC,4BAA4B,gBAAgB;AAAA,EAC1C,YAAY,WAAW;AACrB;AACA,SAAK,YAAY;AACjB,SAAK,oBAAoB,IAAI,kBAAkB,MAAM;AACrD,SAAK,iBAAiB,IAAI,eAAe;AAAA,MACvC,UAAU;AAAA,MACV;AAAA;AAGF,SAAK,iBAAiB,IAAI,qBACxB,WACA,KAAK;AAAA;AAAA;AAKX,cAAc,UAAU,WAAW,EAAE,MACnC,EAAE,UAAU,gBAAgB,UAAU,WACtC;AAAA,EACE,SAAS;AAAA,EACT,kBAAkB;AAAA,EAClB,aAAa;AAAA,EACb,cAAc;AAAA,EACd,SAAS;AAAA,IACP,kBAAkB;AAAA,IAClB,mBAAmB;AAAA,IACnB,gBAAgB;AAAA,IAChB,iBAAiB;AAAA;AAAA,EAEnB,OAAO;AAAA,IACL,OAAO;AAAA,IACP,OAAO;AAAA,IACP,eAAe;AAAA;AAAA,EAEjB,oBAAoB;AAAA,IAClB,MAAM;AAAA;AAAA,EAER,aAAa;AAAA,IACX,eAAe;AAAA,IACf,gBAAgB;AAAA;AAAA,EAElB,cAAc;AAAA,EACd,MAAM;AAAA;AAIV,cAAc,UAAU,iBAAiB;AACzC,cAAc,UAAU,QAAQ;AAChC,cAAc,UAAU,YAAY;AACpC,cAAc,UAAU,OAAO;AAC/B,cAAc,UAAU,YAAY;AACpC,cAAc,UAAU,iBAAiB,cAAc,UAAU;AACjE,cAAc,UAAU,kBAAkB,cAAc,UAAU;AAElE,OAAO,UAAU;AACjB,OAAO,QAAQ,gBAAgB;AAC/B,OAAO,QAAQ,UAAU;",
"names": []
}

View File

@@ -0,0 +1,362 @@
"use strict";
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
const Utils = require("../../utils");
const Transaction = require("../../transaction");
const _ = require("lodash");
const MySqlQueryGenerator = require("../mysql/query-generator");
const AbstractQueryGenerator = require("../abstract/query-generator");
class SQLiteQueryGenerator extends MySqlQueryGenerator {
createSchema() {
return "SELECT name FROM `sqlite_master` WHERE type='table' and name!='sqlite_sequence';";
}
showSchemasQuery() {
return "SELECT name FROM `sqlite_master` WHERE type='table' and name!='sqlite_sequence';";
}
versionQuery() {
return "SELECT sqlite_version() as `version`";
}
createTableQuery(tableName, attributes, options) {
options = options || {};
const primaryKeys = [];
const needsMultiplePrimaryKeys = Object.values(attributes).filter((definition) => definition.includes("PRIMARY KEY")).length > 1;
const attrArray = [];
for (const attr in attributes) {
if (Object.prototype.hasOwnProperty.call(attributes, attr)) {
const dataType = attributes[attr];
const containsAutoIncrement = dataType.includes("AUTOINCREMENT");
let dataTypeString = dataType;
if (dataType.includes("PRIMARY KEY")) {
if (dataType.includes("INT")) {
dataTypeString = containsAutoIncrement ? "INTEGER PRIMARY KEY AUTOINCREMENT" : "INTEGER PRIMARY KEY";
if (dataType.includes(" REFERENCES")) {
dataTypeString += dataType.substr(dataType.indexOf(" REFERENCES"));
}
}
if (needsMultiplePrimaryKeys) {
primaryKeys.push(attr);
if (dataType.includes("NOT NULL")) {
dataTypeString = dataType.replace(" PRIMARY KEY", "");
} else {
dataTypeString = dataType.replace("PRIMARY KEY", "NOT NULL");
}
}
}
attrArray.push(`${this.quoteIdentifier(attr)} ${dataTypeString}`);
}
}
const table = this.quoteTable(tableName);
let attrStr = attrArray.join(", ");
const pkString = primaryKeys.map((pk) => this.quoteIdentifier(pk)).join(", ");
if (options.uniqueKeys) {
_.each(options.uniqueKeys, (columns) => {
if (columns.customIndex) {
attrStr += `, UNIQUE (${columns.fields.map((field) => this.quoteIdentifier(field)).join(", ")})`;
}
});
}
if (pkString.length > 0) {
attrStr += `, PRIMARY KEY (${pkString})`;
}
const sql = `CREATE TABLE IF NOT EXISTS ${table} (${attrStr});`;
return this.replaceBooleanDefaults(sql);
}
booleanValue(value) {
return value ? 1 : 0;
}
_checkValidJsonStatement(stmt) {
if (typeof stmt !== "string") {
return false;
}
const jsonFunctionRegex = /^\s*(json(?:_[a-z]+){0,2})\([^)]*\)/i;
const tokenCaptureRegex = /^\s*((?:([`"'])(?:(?!\2).|\2{2})*\2)|[\w\d\s]+|[().,;+-])/i;
let currentIndex = 0;
let openingBrackets = 0;
let closingBrackets = 0;
let hasJsonFunction = false;
let hasInvalidToken = false;
while (currentIndex < stmt.length) {
const string = stmt.substr(currentIndex);
const functionMatches = jsonFunctionRegex.exec(string);
if (functionMatches) {
currentIndex += functionMatches[0].indexOf("(");
hasJsonFunction = true;
continue;
}
const tokenMatches = tokenCaptureRegex.exec(string);
if (tokenMatches) {
const capturedToken = tokenMatches[1];
if (capturedToken === "(") {
openingBrackets++;
} else if (capturedToken === ")") {
closingBrackets++;
} else if (capturedToken === ";") {
hasInvalidToken = true;
break;
}
currentIndex += tokenMatches[0].length;
continue;
}
break;
}
hasInvalidToken |= openingBrackets !== closingBrackets;
if (hasJsonFunction && hasInvalidToken) {
throw new Error(`Invalid json statement: ${stmt}`);
}
return hasJsonFunction;
}
_toJSONValue(value) {
if (value instanceof Date) {
return value.toISOString();
}
if (Array.isArray(value) && value[0] instanceof Date) {
return value.map((val) => val.toISOString());
}
return value;
}
handleSequelizeMethod(smth, tableName, factory, options, prepend) {
if (smth instanceof Utils.Json) {
return super.handleSequelizeMethod(smth, tableName, factory, options, prepend);
}
if (smth instanceof Utils.Cast) {
if (/timestamp/i.test(smth.type)) {
smth.type = "datetime";
}
}
return AbstractQueryGenerator.prototype.handleSequelizeMethod.call(this, smth, tableName, factory, options, prepend);
}
addColumnQuery(table, key, dataType) {
const attributes = {};
attributes[key] = dataType;
const fields = this.attributesToSQL(attributes, { context: "addColumn" });
const attribute = `${this.quoteIdentifier(key)} ${fields[key]}`;
const sql = `ALTER TABLE ${this.quoteTable(table)} ADD ${attribute};`;
return this.replaceBooleanDefaults(sql);
}
showTablesQuery() {
return "SELECT name FROM `sqlite_master` WHERE type='table' and name!='sqlite_sequence';";
}
updateQuery(tableName, attrValueHash, where, options, attributes) {
options = options || {};
_.defaults(options, this.options);
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, options.omitNull, options);
const modelAttributeMap = {};
const values = [];
const bind = [];
const bindParam = options.bindParam || this.bindParam(bind);
if (attributes) {
_.each(attributes, (attribute, key) => {
modelAttributeMap[key] = attribute;
if (attribute.field) {
modelAttributeMap[attribute.field] = attribute;
}
});
}
for (const key in attrValueHash) {
const value = attrValueHash[key];
if (value instanceof Utils.SequelizeMethod || options.bindParam === false) {
values.push(`${this.quoteIdentifier(key)}=${this.escape(value, modelAttributeMap && modelAttributeMap[key] || void 0, { context: "UPDATE" })}`);
} else {
values.push(`${this.quoteIdentifier(key)}=${this.format(value, modelAttributeMap && modelAttributeMap[key] || void 0, { context: "UPDATE" }, bindParam)}`);
}
}
let query;
const whereOptions = __spreadProps(__spreadValues({}, options), { bindParam });
if (options.limit) {
query = `UPDATE ${this.quoteTable(tableName)} SET ${values.join(",")} WHERE rowid IN (SELECT rowid FROM ${this.quoteTable(tableName)} ${this.whereQuery(where, whereOptions)} LIMIT ${this.escape(options.limit)})`;
} else {
query = `UPDATE ${this.quoteTable(tableName)} SET ${values.join(",")} ${this.whereQuery(where, whereOptions)}`;
}
return { query, bind };
}
truncateTableQuery(tableName, options = {}) {
return [
`DELETE FROM ${this.quoteTable(tableName)}`,
options.restartIdentity ? `; DELETE FROM ${this.quoteTable("sqlite_sequence")} WHERE ${this.quoteIdentifier("name")} = ${Utils.addTicks(Utils.removeTicks(this.quoteTable(tableName), "`"), "'")};` : ""
].join("");
}
deleteQuery(tableName, where, options = {}, model) {
_.defaults(options, this.options);
let whereClause = this.getWhereConditions(where, null, model, options);
if (whereClause) {
whereClause = `WHERE ${whereClause}`;
}
if (options.limit) {
whereClause = `WHERE rowid IN (SELECT rowid FROM ${this.quoteTable(tableName)} ${whereClause} LIMIT ${this.escape(options.limit)})`;
}
return `DELETE FROM ${this.quoteTable(tableName)} ${whereClause}`;
}
attributesToSQL(attributes) {
const result = {};
for (const name in attributes) {
const dataType = attributes[name];
const fieldName = dataType.field || name;
if (_.isObject(dataType)) {
let sql = dataType.type.toString();
if (Object.prototype.hasOwnProperty.call(dataType, "allowNull") && !dataType.allowNull) {
sql += " NOT NULL";
}
if (Utils.defaultValueSchemable(dataType.defaultValue)) {
sql += ` DEFAULT ${this.escape(dataType.defaultValue, dataType)}`;
}
if (dataType.unique === true) {
sql += " UNIQUE";
}
if (dataType.primaryKey) {
sql += " PRIMARY KEY";
if (dataType.autoIncrement) {
sql += " AUTOINCREMENT";
}
}
if (dataType.references) {
const referencesTable = this.quoteTable(dataType.references.model);
let referencesKey;
if (dataType.references.key) {
referencesKey = this.quoteIdentifier(dataType.references.key);
} else {
referencesKey = this.quoteIdentifier("id");
}
sql += ` REFERENCES ${referencesTable} (${referencesKey})`;
if (dataType.onDelete) {
sql += ` ON DELETE ${dataType.onDelete.toUpperCase()}`;
}
if (dataType.onUpdate) {
sql += ` ON UPDATE ${dataType.onUpdate.toUpperCase()}`;
}
}
result[fieldName] = sql;
} else {
result[fieldName] = dataType;
}
}
return result;
}
showIndexesQuery(tableName) {
return `PRAGMA INDEX_LIST(${this.quoteTable(tableName)})`;
}
showConstraintsQuery(tableName, constraintName) {
let sql = `SELECT sql FROM sqlite_master WHERE tbl_name='${tableName}'`;
if (constraintName) {
sql += ` AND sql LIKE '%${constraintName}%'`;
}
return `${sql};`;
}
removeIndexQuery(tableName, indexNameOrAttributes) {
let indexName = indexNameOrAttributes;
if (typeof indexName !== "string") {
indexName = Utils.underscore(`${tableName}_${indexNameOrAttributes.join("_")}`);
}
return `DROP INDEX IF EXISTS ${this.quoteIdentifier(indexName)}`;
}
describeTableQuery(tableName, schema, schemaDelimiter) {
const table = {
_schema: schema,
_schemaDelimiter: schemaDelimiter,
tableName
};
return `PRAGMA TABLE_INFO(${this.quoteTable(this.addSchema(table))});`;
}
describeCreateTableQuery(tableName) {
return `SELECT sql FROM sqlite_master WHERE tbl_name='${tableName}';`;
}
removeColumnQuery(tableName, attributes) {
attributes = this.attributesToSQL(attributes);
let backupTableName;
if (typeof tableName === "object") {
backupTableName = {
tableName: `${tableName.tableName}_backup`,
schema: tableName.schema
};
} else {
backupTableName = `${tableName}_backup`;
}
const quotedTableName = this.quoteTable(tableName);
const quotedBackupTableName = this.quoteTable(backupTableName);
const attributeNames = Object.keys(attributes).map((attr) => this.quoteIdentifier(attr)).join(", ");
return `${this.createTableQuery(backupTableName, attributes)}INSERT INTO ${quotedBackupTableName} SELECT ${attributeNames} FROM ${quotedTableName};DROP TABLE ${quotedTableName};${this.createTableQuery(tableName, attributes)}INSERT INTO ${quotedTableName} SELECT ${attributeNames} FROM ${quotedBackupTableName};DROP TABLE ${quotedBackupTableName};`;
}
_alterConstraintQuery(tableName, attributes, createTableSql) {
let backupTableName;
attributes = this.attributesToSQL(attributes);
if (typeof tableName === "object") {
backupTableName = {
tableName: `${tableName.tableName}_backup`,
schema: tableName.schema
};
} else {
backupTableName = `${tableName}_backup`;
}
const quotedTableName = this.quoteTable(tableName);
const quotedBackupTableName = this.quoteTable(backupTableName);
const attributeNames = Object.keys(attributes).map((attr) => this.quoteIdentifier(attr)).join(", ");
return `${createTableSql.replace(`CREATE TABLE ${quotedTableName}`, `CREATE TABLE ${quotedBackupTableName}`).replace(`CREATE TABLE ${quotedTableName.replace(/`/g, '"')}`, `CREATE TABLE ${quotedBackupTableName}`)}INSERT INTO ${quotedBackupTableName} SELECT ${attributeNames} FROM ${quotedTableName};DROP TABLE ${quotedTableName};ALTER TABLE ${quotedBackupTableName} RENAME TO ${quotedTableName};`;
}
renameColumnQuery(tableName, attrNameBefore, attrNameAfter, attributes) {
let backupTableName;
attributes = this.attributesToSQL(attributes);
if (typeof tableName === "object") {
backupTableName = {
tableName: `${tableName.tableName}_backup`,
schema: tableName.schema
};
} else {
backupTableName = `${tableName}_backup`;
}
const quotedTableName = this.quoteTable(tableName);
const quotedBackupTableName = this.quoteTable(backupTableName);
const attributeNamesImport = Object.keys(attributes).map((attr) => attrNameAfter === attr ? `${this.quoteIdentifier(attrNameBefore)} AS ${this.quoteIdentifier(attr)}` : this.quoteIdentifier(attr)).join(", ");
const attributeNamesExport = Object.keys(attributes).map((attr) => this.quoteIdentifier(attr)).join(", ");
return `${this.createTableQuery(backupTableName, attributes)}INSERT INTO ${quotedBackupTableName} SELECT ${attributeNamesImport} FROM ${quotedTableName};DROP TABLE ${quotedTableName};${this.createTableQuery(tableName, attributes)}INSERT INTO ${quotedTableName} SELECT ${attributeNamesExport} FROM ${quotedBackupTableName};DROP TABLE ${quotedBackupTableName};`;
}
startTransactionQuery(transaction) {
if (transaction.parent) {
return `SAVEPOINT ${this.quoteIdentifier(transaction.name)};`;
}
return `BEGIN ${transaction.options.type} TRANSACTION;`;
}
setIsolationLevelQuery(value) {
switch (value) {
case Transaction.ISOLATION_LEVELS.REPEATABLE_READ:
return "-- SQLite is not able to choose the isolation level REPEATABLE READ.";
case Transaction.ISOLATION_LEVELS.READ_UNCOMMITTED:
return "PRAGMA read_uncommitted = ON;";
case Transaction.ISOLATION_LEVELS.READ_COMMITTED:
return "PRAGMA read_uncommitted = OFF;";
case Transaction.ISOLATION_LEVELS.SERIALIZABLE:
return "-- SQLite's default isolation level is SERIALIZABLE. Nothing to do.";
default:
throw new Error(`Unknown isolation level: ${value}`);
}
}
replaceBooleanDefaults(sql) {
return sql.replace(/DEFAULT '?false'?/g, "DEFAULT 0").replace(/DEFAULT '?true'?/g, "DEFAULT 1");
}
getForeignKeysQuery(tableName) {
return `PRAGMA foreign_key_list(${this.quoteTable(this.addSchema(tableName))})`;
}
tableExistsQuery(tableName) {
return `SELECT name FROM sqlite_master WHERE type='table' AND name=${this.escape(this.addSchema(tableName))};`;
}
quoteIdentifier(identifier, force) {
return Utils.addTicks(Utils.removeTicks(identifier, "`"), "`");
}
}
module.exports = SQLiteQueryGenerator;
//# sourceMappingURL=query-generator.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,175 @@
"use strict";
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
const sequelizeErrors = require("../../errors");
const QueryTypes = require("../../query-types");
const { QueryInterface } = require("../abstract/query-interface");
const { cloneDeep } = require("../../utils");
const _ = require("lodash");
class SQLiteQueryInterface extends QueryInterface {
async removeColumn(tableName, attributeName, options) {
options = options || {};
const fields = await this.describeTable(tableName, options);
delete fields[attributeName];
const sql = this.queryGenerator.removeColumnQuery(tableName, fields);
const subQueries = sql.split(";").filter((q) => q !== "");
for (const subQuery of subQueries)
await this.sequelize.query(`${subQuery};`, __spreadValues({ raw: true }, options));
}
async changeColumn(tableName, attributeName, dataTypeOrOptions, options) {
options = options || {};
const fields = await this.describeTable(tableName, options);
Object.assign(fields[attributeName], this.normalizeAttribute(dataTypeOrOptions));
const sql = this.queryGenerator.removeColumnQuery(tableName, fields);
const subQueries = sql.split(";").filter((q) => q !== "");
for (const subQuery of subQueries)
await this.sequelize.query(`${subQuery};`, __spreadValues({ raw: true }, options));
}
async renameColumn(tableName, attrNameBefore, attrNameAfter, options) {
options = options || {};
const fields = await this.assertTableHasColumn(tableName, attrNameBefore, options);
fields[attrNameAfter] = __spreadValues({}, fields[attrNameBefore]);
delete fields[attrNameBefore];
const sql = this.queryGenerator.renameColumnQuery(tableName, attrNameBefore, attrNameAfter, fields);
const subQueries = sql.split(";").filter((q) => q !== "");
for (const subQuery of subQueries)
await this.sequelize.query(`${subQuery};`, __spreadValues({ raw: true }, options));
}
async removeConstraint(tableName, constraintName, options) {
let createTableSql;
const constraints = await this.showConstraint(tableName, constraintName);
const constraint = constraints.find((constaint) => constaint.constraintName === constraintName);
if (!constraint) {
throw new sequelizeErrors.UnknownConstraintError({
message: `Constraint ${constraintName} on table ${tableName} does not exist`,
constraint: constraintName,
table: tableName
});
}
createTableSql = constraint.sql;
constraint.constraintName = this.queryGenerator.quoteIdentifier(constraint.constraintName);
let constraintSnippet = `, CONSTRAINT ${constraint.constraintName} ${constraint.constraintType} ${constraint.constraintCondition}`;
if (constraint.constraintType === "FOREIGN KEY") {
const referenceTableName = this.queryGenerator.quoteTable(constraint.referenceTableName);
constraint.referenceTableKeys = constraint.referenceTableKeys.map((columnName) => this.queryGenerator.quoteIdentifier(columnName));
const referenceTableKeys = constraint.referenceTableKeys.join(", ");
constraintSnippet += ` REFERENCES ${referenceTableName} (${referenceTableKeys})`;
constraintSnippet += ` ON UPDATE ${constraint.updateAction}`;
constraintSnippet += ` ON DELETE ${constraint.deleteAction}`;
}
createTableSql = createTableSql.replace(constraintSnippet, "");
createTableSql += ";";
const fields = await this.describeTable(tableName, options);
const sql = this.queryGenerator._alterConstraintQuery(tableName, fields, createTableSql);
const subQueries = sql.split(";").filter((q) => q !== "");
for (const subQuery of subQueries)
await this.sequelize.query(`${subQuery};`, __spreadValues({ raw: true }, options));
}
async addConstraint(tableName, options) {
if (!options.fields) {
throw new Error("Fields must be specified through options.fields");
}
if (!options.type) {
throw new Error("Constraint type must be specified through options.type");
}
options = cloneDeep(options);
const constraintSnippet = this.queryGenerator.getConstraintSnippet(tableName, options);
const describeCreateTableSql = this.queryGenerator.describeCreateTableQuery(tableName);
const constraints = await this.sequelize.query(describeCreateTableSql, __spreadProps(__spreadValues({}, options), { type: QueryTypes.SELECT, raw: true }));
let sql = constraints[0].sql;
const index = sql.length - 1;
const createTableSql = `${sql.substr(0, index)}, ${constraintSnippet})${sql.substr(index + 1)};`;
const fields = await this.describeTable(tableName, options);
sql = this.queryGenerator._alterConstraintQuery(tableName, fields, createTableSql);
const subQueries = sql.split(";").filter((q) => q !== "");
for (const subQuery of subQueries)
await this.sequelize.query(`${subQuery};`, __spreadValues({ raw: true }, options));
}
async getForeignKeyReferencesForTable(tableName, options) {
const database = this.sequelize.config.database;
const query = this.queryGenerator.getForeignKeysQuery(tableName, database);
const result = await this.sequelize.query(query, options);
return result.map((row) => ({
tableName,
columnName: row.from,
referencedTableName: row.table,
referencedColumnName: row.to,
tableCatalog: database,
referencedTableCatalog: database
}));
}
async dropAllTables(options) {
options = options || {};
const skip = options.skip || [];
const tableNames = await this.showAllTables(options);
await this.sequelize.query("PRAGMA foreign_keys = OFF", options);
await this._dropAllTables(tableNames, skip, options);
await this.sequelize.query("PRAGMA foreign_keys = ON", options);
}
async describeTable(tableName, options) {
let schema = null;
let schemaDelimiter = null;
if (typeof options === "string") {
schema = options;
} else if (typeof options === "object" && options !== null) {
schema = options.schema || null;
schemaDelimiter = options.schemaDelimiter || null;
}
if (typeof tableName === "object" && tableName !== null) {
schema = tableName.schema;
tableName = tableName.tableName;
}
const sql = this.queryGenerator.describeTableQuery(tableName, schema, schemaDelimiter);
options = __spreadProps(__spreadValues({}, options), { type: QueryTypes.DESCRIBE });
const sqlIndexes = this.queryGenerator.showIndexesQuery(tableName);
try {
const data = await this.sequelize.query(sql, options);
if (_.isEmpty(data)) {
throw new Error(`No description found for "${tableName}" table. Check the table name and schema; remember, they _are_ case sensitive.`);
}
const indexes = await this.sequelize.query(sqlIndexes, options);
for (const prop in data) {
data[prop].unique = false;
}
for (const index of indexes) {
for (const field of index.fields) {
if (index.unique !== void 0) {
data[field.attribute].unique = index.unique;
}
}
}
const foreignKeys = await this.getForeignKeyReferencesForTable(tableName, options);
for (const foreignKey of foreignKeys) {
data[foreignKey.columnName].references = {
model: foreignKey.referencedTableName,
key: foreignKey.referencedColumnName
};
}
return data;
} catch (e) {
if (e.original && e.original.code === "ER_NO_SUCH_TABLE") {
throw new Error(`No description found for "${tableName}" table. Check the table name and schema; remember, they _are_ case sensitive.`);
}
throw e;
}
}
}
exports.SQLiteQueryInterface = SQLiteQueryInterface;
//# sourceMappingURL=query-interface.js.map

File diff suppressed because one or more lines are too long

364
node_modules/sequelize/lib/dialects/sqlite/query.js generated vendored Normal file
View File

@@ -0,0 +1,364 @@
"use strict";
const _ = require("lodash");
const Utils = require("../../utils");
const AbstractQuery = require("../abstract/query");
const QueryTypes = require("../../query-types");
const sequelizeErrors = require("../../errors");
const parserStore = require("../parserStore")("sqlite");
const { logger } = require("../../utils/logger");
const debug = logger.debugContext("sql:sqlite");
function stringifyIfBigint(value) {
if (typeof value === "bigint") {
return value.toString();
}
return value;
}
class Query extends AbstractQuery {
getInsertIdField() {
return "lastID";
}
static formatBindParameters(sql, values, dialect) {
let bindParam;
if (Array.isArray(values)) {
bindParam = {};
values.forEach((v, i) => {
bindParam[`$${i + 1}`] = v;
});
sql = AbstractQuery.formatBindParameters(sql, values, dialect, { skipValueReplace: true })[0];
} else {
bindParam = {};
if (typeof values === "object") {
for (const k of Object.keys(values)) {
bindParam[`$${k}`] = values[k];
}
}
sql = AbstractQuery.formatBindParameters(sql, values, dialect, { skipValueReplace: true })[0];
}
return [sql, bindParam];
}
_collectModels(include, prefix) {
const ret = {};
if (include) {
for (const _include of include) {
let key;
if (!prefix) {
key = _include.as;
} else {
key = `${prefix}.${_include.as}`;
}
ret[key] = _include.model;
if (_include.include) {
_.merge(ret, this._collectModels(_include.include, key));
}
}
}
return ret;
}
_handleQueryResponse(metaData, columnTypes, err, results, errStack) {
if (err) {
err.sql = this.sql;
throw this.formatError(err, errStack);
}
let result = this.instance;
if (this.isInsertQuery(results, metaData) || this.isUpsertQuery()) {
this.handleInsertQuery(results, metaData);
if (!this.instance) {
if (metaData.constructor.name === "Statement" && this.model && this.model.autoIncrementAttribute && this.model.autoIncrementAttribute === this.model.primaryKeyAttribute && this.model.rawAttributes[this.model.primaryKeyAttribute]) {
const startId = metaData[this.getInsertIdField()] - metaData.changes + 1;
result = [];
for (let i = startId; i < startId + metaData.changes; i++) {
result.push({ [this.model.rawAttributes[this.model.primaryKeyAttribute].field]: i });
}
} else {
result = metaData[this.getInsertIdField()];
}
}
}
if (this.isShowTablesQuery()) {
return results.map((row) => row.name);
}
if (this.isShowConstraintsQuery()) {
result = results;
if (results && results[0] && results[0].sql) {
result = this.parseConstraintsFromSql(results[0].sql);
}
return result;
}
if (this.isSelectQuery()) {
if (this.options.raw) {
return this.handleSelectQuery(results);
}
const prefixes = this._collectModels(this.options.include);
results = results.map((result2) => {
return _.mapValues(result2, (value, name) => {
let model;
if (name.includes(".")) {
const lastind = name.lastIndexOf(".");
model = prefixes[name.substr(0, lastind)];
name = name.substr(lastind + 1);
} else {
model = this.options.model;
}
const tableName = model.getTableName().toString().replace(/`/g, "");
const tableTypes = columnTypes[tableName] || {};
if (tableTypes && !(name in tableTypes)) {
_.forOwn(model.rawAttributes, (attribute, key) => {
if (name === key && attribute.field) {
name = attribute.field;
return false;
}
});
}
return Object.prototype.hasOwnProperty.call(tableTypes, name) ? this.applyParsers(tableTypes[name], value) : value;
});
});
return this.handleSelectQuery(results);
}
if (this.isShowOrDescribeQuery()) {
return results;
}
if (this.sql.includes("PRAGMA INDEX_LIST")) {
return this.handleShowIndexesQuery(results);
}
if (this.sql.includes("PRAGMA INDEX_INFO")) {
return results;
}
if (this.sql.includes("PRAGMA TABLE_INFO")) {
result = {};
let defaultValue;
for (const _result of results) {
if (_result.dflt_value === null) {
defaultValue = void 0;
} else if (_result.dflt_value === "NULL") {
defaultValue = null;
} else {
defaultValue = _result.dflt_value;
}
result[_result.name] = {
type: _result.type,
allowNull: _result.notnull === 0,
defaultValue,
primaryKey: _result.pk !== 0
};
if (result[_result.name].type === "TINYINT(1)") {
result[_result.name].defaultValue = { "0": false, "1": true }[result[_result.name].defaultValue];
}
if (typeof result[_result.name].defaultValue === "string") {
result[_result.name].defaultValue = result[_result.name].defaultValue.replace(/'/g, "");
}
}
return result;
}
if (this.sql.includes("PRAGMA foreign_keys;")) {
return results[0];
}
if (this.sql.includes("PRAGMA foreign_keys")) {
return results;
}
if (this.sql.includes("PRAGMA foreign_key_list")) {
return results;
}
if ([QueryTypes.BULKUPDATE, QueryTypes.BULKDELETE].includes(this.options.type)) {
return metaData.changes;
}
if (this.options.type === QueryTypes.VERSION) {
return results[0].version;
}
if (this.options.type === QueryTypes.RAW) {
return [results, metaData];
}
if (this.isUpsertQuery()) {
return [result, null];
}
if (this.isUpdateQuery() || this.isInsertQuery()) {
return [result, metaData.changes];
}
return result;
}
async run(sql, parameters) {
const conn = this.connection;
this.sql = sql;
const method = this.getDatabaseMethod();
const complete = this._logQuery(sql, debug, parameters);
return new Promise((resolve, reject) => conn.serialize(async () => {
const columnTypes = {};
const errForStack = new Error();
const executeSql = () => {
if (sql.startsWith("-- ")) {
return resolve();
}
const query = this;
function afterExecute(executionError, results) {
try {
complete();
resolve(query._handleQueryResponse(this, columnTypes, executionError, results, errForStack.stack));
return;
} catch (error) {
reject(error);
}
}
if (!parameters)
parameters = [];
if (_.isPlainObject(parameters)) {
const newParameters = Object.create(null);
for (const key of Object.keys(parameters)) {
newParameters[`${key}`] = stringifyIfBigint(parameters[key]);
}
parameters = newParameters;
} else {
parameters = parameters.map(stringifyIfBigint);
}
conn[method](sql, parameters, afterExecute);
return null;
};
if (this.getDatabaseMethod() === "all") {
let tableNames = [];
if (this.options && this.options.tableNames) {
tableNames = this.options.tableNames;
} else if (/FROM `(.*?)`/i.exec(this.sql)) {
tableNames.push(/FROM `(.*?)`/i.exec(this.sql)[1]);
}
tableNames = tableNames.filter((tableName) => !(tableName in columnTypes) && tableName !== "sqlite_master");
if (!tableNames.length) {
return executeSql();
}
await Promise.all(tableNames.map((tableName) => new Promise((resolve2) => {
tableName = tableName.replace(/`/g, "");
columnTypes[tableName] = {};
conn.all(`PRAGMA table_info(\`${tableName}\`)`, (err, results) => {
if (!err) {
for (const result of results) {
columnTypes[tableName][result.name] = result.type;
}
}
resolve2();
});
})));
}
return executeSql();
}));
}
parseConstraintsFromSql(sql) {
let constraints = sql.split("CONSTRAINT ");
let referenceTableName, referenceTableKeys, updateAction, deleteAction;
constraints.splice(0, 1);
constraints = constraints.map((constraintSql) => {
if (constraintSql.includes("REFERENCES")) {
updateAction = constraintSql.match(/ON UPDATE (CASCADE|SET NULL|RESTRICT|NO ACTION|SET DEFAULT){1}/);
deleteAction = constraintSql.match(/ON DELETE (CASCADE|SET NULL|RESTRICT|NO ACTION|SET DEFAULT){1}/);
if (updateAction) {
updateAction = updateAction[1];
}
if (deleteAction) {
deleteAction = deleteAction[1];
}
const referencesRegex = /REFERENCES.+\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)/;
const referenceConditions = constraintSql.match(referencesRegex)[0].split(" ");
referenceTableName = Utils.removeTicks(referenceConditions[1]);
let columnNames = referenceConditions[2];
columnNames = columnNames.replace(/\(|\)/g, "").split(", ");
referenceTableKeys = columnNames.map((column) => Utils.removeTicks(column));
}
const constraintCondition = constraintSql.match(/\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)/)[0];
constraintSql = constraintSql.replace(/\(.+\)/, "");
const constraint = constraintSql.split(" ");
if (["PRIMARY", "FOREIGN"].includes(constraint[1])) {
constraint[1] += " KEY";
}
return {
constraintName: Utils.removeTicks(constraint[0]),
constraintType: constraint[1],
updateAction,
deleteAction,
sql: sql.replace(/"/g, "`"),
constraintCondition,
referenceTableName,
referenceTableKeys
};
});
return constraints;
}
applyParsers(type, value) {
if (type.includes("(")) {
type = type.substr(0, type.indexOf("("));
}
type = type.replace("UNSIGNED", "").replace("ZEROFILL", "");
type = type.trim().toUpperCase();
const parse = parserStore.get(type);
if (value !== null && parse) {
return parse(value, { timezone: this.sequelize.options.timezone });
}
return value;
}
formatError(err, errStack) {
switch (err.code) {
case "SQLITE_CONSTRAINT_UNIQUE":
case "SQLITE_CONSTRAINT_PRIMARYKEY":
case "SQLITE_CONSTRAINT_TRIGGER":
case "SQLITE_CONSTRAINT_FOREIGNKEY":
case "SQLITE_CONSTRAINT": {
if (err.message.includes("FOREIGN KEY constraint failed")) {
return new sequelizeErrors.ForeignKeyConstraintError({
parent: err,
stack: errStack
});
}
let fields = [];
let match = err.message.match(/columns (.*?) are/);
if (match !== null && match.length >= 2) {
fields = match[1].split(", ");
} else {
match = err.message.match(/UNIQUE constraint failed: (.*)/);
if (match !== null && match.length >= 2) {
fields = match[1].split(", ").map((columnWithTable) => columnWithTable.split(".")[1]);
}
}
const errors = [];
let message = "Validation error";
for (const field of fields) {
errors.push(new sequelizeErrors.ValidationErrorItem(this.getUniqueConstraintErrorMessage(field), "unique violation", field, this.instance && this.instance[field], this.instance, "not_unique"));
}
if (this.model) {
_.forOwn(this.model.uniqueKeys, (constraint) => {
if (_.isEqual(constraint.fields, fields) && !!constraint.msg) {
message = constraint.msg;
return false;
}
});
}
return new sequelizeErrors.UniqueConstraintError({ message, errors, parent: err, fields, stack: errStack });
}
case "SQLITE_BUSY":
return new sequelizeErrors.TimeoutError(err, { stack: errStack });
default:
return new sequelizeErrors.DatabaseError(err, { stack: errStack });
}
}
async handleShowIndexesQuery(data) {
return Promise.all(data.reverse().map(async (item) => {
item.fields = [];
item.primary = false;
item.unique = !!item.unique;
item.constraintName = item.name;
const columns = await this.run(`PRAGMA INDEX_INFO(\`${item.name}\`)`);
for (const column of columns) {
item.fields[column.seqno] = {
attribute: column.name,
length: void 0,
order: void 0
};
}
return item;
}));
}
getDatabaseMethod() {
if (this.isInsertQuery() || this.isUpdateQuery() || this.isUpsertQuery() || this.isBulkUpdateQuery() || this.sql.toLowerCase().includes("CREATE TEMPORARY TABLE".toLowerCase()) || this.options.type === QueryTypes.BULKDELETE) {
return "run";
}
return "all";
}
}
module.exports = Query;
module.exports.Query = Query;
module.exports.default = Query;
//# sourceMappingURL=query.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,19 @@
var __defProp = Object.defineProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __export = (target, all) => {
__markAsModule(target);
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
__export(exports, {
withSqliteForeignKeysOff: () => withSqliteForeignKeysOff
});
async function withSqliteForeignKeysOff(sequelize, options, cb) {
try {
await sequelize.query("PRAGMA foreign_keys = OFF", options);
return await cb();
} finally {
await sequelize.query("PRAGMA foreign_keys = ON", options);
}
}
//# sourceMappingURL=sqlite-utils.js.map

View File

@@ -0,0 +1,7 @@
{
"version": 3,
"sources": ["../../../src/dialects/sqlite/sqlite-utils.ts"],
"sourcesContent": ["import type { Sequelize } from '../../sequelize.js';\nimport type { QueryOptions } from '../abstract/query-interface.js';\n\nexport async function withSqliteForeignKeysOff<T>(sequelize: Sequelize, options: QueryOptions, cb: () => Promise<T>): Promise<T> {\n try {\n await sequelize.query('PRAGMA foreign_keys = OFF', options);\n\n return await cb();\n } finally {\n await sequelize.query('PRAGMA foreign_keys = ON', options);\n }\n}\n"],
"mappings": ";;;;;;;AAAA;AAAA;AAAA;AAGA,wCAAkD,WAAsB,SAAuB,IAAkC;AAC/H,MAAI;AACF,UAAM,UAAU,MAAM,6BAA6B;AAEnD,WAAO,MAAM;AAAA,YACb;AACA,UAAM,UAAU,MAAM,4BAA4B;AAAA;AAAA;",
"names": []
}