This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+5
View File
@@ -0,0 +1,5 @@
/**
* Copy of `acquireRawConnection` from knex codebase, but instead relies
* on `getRuntimeConnectionSettings` vs `connectionSettings`
*/
export declare function acquireRawConnection(): Promise<any>;
+115
View File
@@ -0,0 +1,115 @@
"use strict";
/*
* knex-dynamic-connection
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.acquireRawConnection = void 0;
const debug = require('debug')('knex:patched:mssql');
const security_js_1 = require("knex/lib/util/security.js");
function isNil(value) {
return value === undefined || value === null;
}
/**
* Copy of https://github.com/knex/knex/blob/3718d647e452e3a7659ad3310a56e862f64c17be/lib/dialects/mssql/index.js#L32, but accepts the settings as an argument. I wish, they did the same
* thing too.
*
* If you ever see the KNEX version using a `this` property inside this method, then
* please report it to us. Right now, this method is pure.
* Also, ignore "this.connectionSettings"
*/
function generateConnection(settings) {
settings.options = settings.options || {};
/** @type {import('tedious').ConnectionConfig} */
const cfg = {
authentication: {
type: settings.type || 'default',
options: {
userName: settings.userName || settings.user,
password: settings.password,
domain: settings.domain,
token: settings.token,
clientId: settings.clientId,
clientSecret: settings.clientSecret,
tenantId: settings.tenantId,
msiEndpoint: settings.msiEndpoint,
},
},
server: settings.server || settings.host,
options: {
database: settings.database,
encrypt: settings.encrypt || false,
port: settings.port || 1433,
connectTimeout: settings.connectionTimeout || settings.timeout || 15000,
requestTimeout: !isNil(settings.requestTimeout) ? settings.requestTimeout : 15000,
rowCollectionOnDone: false,
rowCollectionOnRequestCompletion: false,
useColumnNames: false,
tdsVersion: settings.options.tdsVersion || '7_4',
appName: settings.options.appName || 'knex',
trustServerCertificate: false,
...settings.options,
},
};
if (cfg.authentication.options.password) {
(0, security_js_1.setHiddenProperty)(cfg.authentication.options);
}
// tedious always connect via tcp when port is specified
if (cfg.options.instanceName) {
delete cfg.options.port;
}
if (isNaN(cfg.options.requestTimeout)) {
cfg.options.requestTimeout = 15000;
}
if (cfg.options.requestTimeout === Infinity) {
cfg.options.requestTimeout = 0;
}
if (cfg.options.requestTimeout < 0) {
cfg.options.requestTimeout = 0;
}
if (settings.debug) {
cfg.options.debug = {
packet: true,
token: true,
data: true,
payload: true,
};
}
return cfg;
}
/**
* Copy of `acquireRawConnection` from knex codebase, but instead relies
* on `getRuntimeConnectionSettings` vs `connectionSettings`
*/
function acquireRawConnection() {
return new Promise((resolver, rejecter) => {
debug('connection::connection new connection requested');
const Driver = this._driver();
const settings = Object.assign({}, generateConnection(this.getRuntimeConnectionSettings()));
const connection = new Driver.Connection(settings);
connection.connect((err) => {
if (err) {
debug('connection::connect error: %s', err.message);
return rejecter(err);
}
debug('connection::connect connected to server');
connection.connected = true;
connection.on('error', (e) => {
debug('connection::error message=%s', e.message);
connection.__knex__disposed = e;
connection.connected = false;
});
connection.once('end', () => {
connection.connected = false;
connection.__knex__disposed = 'Connection to server was terminated.';
debug('connection::end connection ended.');
});
return resolver(connection);
});
});
}
exports.acquireRawConnection = acquireRawConnection;
+5
View File
@@ -0,0 +1,5 @@
/**
* Copy of `acquireRawConnection` from knex codebase, but instead relies
* on `getRuntimeConnectionSettings` vs `connectionSettings`
*/
export declare function acquireRawConnection(): Promise<any>;
+32
View File
@@ -0,0 +1,32 @@
"use strict";
/*
* knex-dynamic-connection
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.acquireRawConnection = void 0;
/**
* Copy of `acquireRawConnection` from knex codebase, but instead relies
* on `getRuntimeConnectionSettings` vs `connectionSettings`
*/
function acquireRawConnection() {
return new Promise((resolver, rejecter) => {
const connection = this.driver.createConnection(this.getRuntimeConnectionSettings());
connection.on('error', (err) => {
connection.__knex__disposed = err;
});
connection.connect((err) => {
if (err) {
// if connection is rejected, remove listener that was registered above...
connection.removeAllListeners();
return rejecter(err);
}
resolver(connection);
});
});
}
exports.acquireRawConnection = acquireRawConnection;
@@ -0,0 +1,5 @@
/**
* Copy of `acquireRawConnection` from knex codebase, but instead relies
* on `getRuntimeConnectionSettings` vs `connectionSettings`
*/
export declare function acquireRawConnection(): Promise<any>;
+242
View File
@@ -0,0 +1,242 @@
"use strict";
/* eslint-disable @typescript-eslint/no-shadow */
/*
* knex-dynamic-connection
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.acquireRawConnection = void 0;
const stream_1 = __importDefault(require("stream"));
const util_1 = require("util");
const utils_1 = require("knex/lib/dialects/oracledb/utils");
/**
* Copy/pasted as it is from
* https://github.com/knex/knex/blob/master/lib/dialects/oracledb/index.js#L466
*/
const lobProcessing = function (stream) {
const oracledb = require('oracledb');
/**
* @type 'string' | 'buffer'
*/
let type;
if (stream.type) {
// v1.2-v4
if (stream.type === oracledb.BLOB) {
type = 'buffer';
}
else if (stream.type === oracledb.CLOB) {
type = 'string';
}
}
else if (stream.iLob) {
// v1
if (stream.iLob.type === oracledb.CLOB) {
type = 'string';
}
else if (stream.iLob.type === oracledb.BLOB) {
type = 'buffer';
}
}
else {
throw new Error('Unrecognized oracledb lob stream type');
}
if (type === 'string') {
stream.setEncoding('utf-8');
}
return readStream(stream, type);
};
/**
* Copy/pasted as it is from
* https://github.com/knex/knex/blob/master/lib/dialects/oracledb/index.js#L424
*/
function resolveConnectString(connectionSettings) {
if (connectionSettings.connectString) {
return connectionSettings.connectString;
}
if (!connectionSettings.port) {
return connectionSettings.host + '/' + connectionSettings.database;
}
return connectionSettings.host + ':' + connectionSettings.port + '/' + connectionSettings.database;
}
/**
* Copy/pasted as it is from
* https://github.com/knex/knex/blob/master/lib/dialects/oracledb/index.js#L442
*/
function readStream(stream, type) {
return new Promise((resolve, reject) => {
let data = type === 'string' ? '' : Buffer.alloc(0);
stream.on('error', function (err) {
reject(err);
});
stream.on('data', function (chunk) {
if (type === 'string') {
data += chunk;
}
else {
data = Buffer.concat([data, chunk]);
}
});
stream.on('end', function () {
resolve(data);
});
});
}
/**
* Copy of `acquireRawConnection` from knex codebase, but instead relies
* on `getRuntimeConnectionSettings` vs `connectionSettings`
*/
function acquireRawConnection() {
const client = this;
const asyncConnection = new Promise(function (resolver, rejecter) {
const settings = client.getRuntimeConnectionSettings();
// If external authentication don't have to worry about username/password and
// if not need to set the username and password
const oracleDbConfig = settings.externalAuth
? { externalAuth: settings.externalAuth }
: {
user: settings.user,
password: settings.password,
};
// In the case of external authentication connection string will be given
oracleDbConfig['connectString'] = resolveConnectString(settings);
if (settings.prefetchRowCount) {
oracleDbConfig['prefetchRows'] = settings.prefetchRowCount;
}
if (settings.stmtCacheSize !== undefined) {
oracleDbConfig['stmtCacheSize'] = settings.stmtCacheSize;
}
client.driver.fetchAsString = client.fetchAsString;
client.driver.getConnection(oracleDbConfig, function (err, connection) {
if (err) {
return rejecter(err);
}
connection.commitAsync = function () {
return new Promise((commitResolve, commitReject) => {
this.commit(function (err) {
if (err) {
return commitReject(err);
}
commitResolve();
});
});
};
connection.rollbackAsync = function () {
return new Promise((rollbackResolve, rollbackReject) => {
this.rollback(function (err) {
if (err) {
return rollbackReject(err);
}
rollbackResolve();
});
});
};
const fetchAsync = (0, util_1.promisify)(function (sql, bindParams, options, cb) {
options = options || {};
options.outFormat = client.driver.OUT_FORMAT_OBJECT || client.driver.OBJECT;
if (!options.outFormat) {
throw new Error('not found oracledb.outFormat constants');
}
if (options.resultSet) {
connection.execute(sql, bindParams || [], options, function (err, result) {
if (err) {
if ((0, utils_1.isConnectionError)(err)) {
connection.close().catch(function () { });
connection.__knex__disposed = err;
}
return cb(err);
}
const fetchResult = { rows: [], resultSet: result.resultSet };
const numRows = 100;
const fetchRowsFromRS = function (connection, resultSet, numRows) {
resultSet.getRows(numRows, function (err, rows) {
if (err) {
if ((0, utils_1.isConnectionError)(err)) {
connection.close().catch(function () { });
connection.__knex__disposed = err;
}
resultSet.close(function () {
return cb(err);
});
}
else if (rows.length === 0) {
return cb(null, fetchResult);
}
else if (rows.length > 0) {
if (rows.length === numRows) {
fetchResult.rows = fetchResult.rows.concat(rows);
fetchRowsFromRS(connection, resultSet, numRows);
}
else {
fetchResult.rows = fetchResult.rows.concat(rows);
return cb(null, fetchResult);
}
}
});
};
fetchRowsFromRS(connection, result.resultSet, numRows);
});
}
else {
connection.execute(sql, bindParams || [], options, function (err, result) {
if (err) {
// dispose the connection on connection error
if ((0, utils_1.isConnectionError)(err)) {
connection.close().catch(function () { });
connection.__knex__disposed = err;
}
return cb(err);
}
return cb(null, result);
});
}
});
connection.executeAsync = function (sql, bindParams, options) {
// Read all lob
return fetchAsync(sql, bindParams, options).then(async (results) => {
const closeResultSet = () => {
return results.resultSet
? (0, util_1.promisify)(results.resultSet.close).call(results.resultSet)
: Promise.resolve();
};
// Collect LOBs to read
const lobs = [];
if (results.rows) {
if (Array.isArray(results.rows)) {
for (let i = 0; i < results.rows.length; i++) {
// Iterate through the rows
const row = results.rows[i];
for (const column in row) {
if (row[column] instanceof stream_1.default.Readable) {
lobs.push({ index: i, key: column, stream: row[column] });
}
}
}
}
}
try {
for (const lob of lobs) {
// todo should be fetchAsString/fetchAsBuffer polyfill only
results.rows[lob.index][lob.key] = await lobProcessing(lob.stream);
}
}
catch (e) {
await closeResultSet().catch(() => { });
throw e;
}
await closeResultSet();
return results;
});
};
resolver(connection);
});
});
return asyncConnection;
}
exports.acquireRawConnection = acquireRawConnection;
+5
View File
@@ -0,0 +1,5 @@
/**
* Copy of `acquireRawConnection` from knex codebase, but instead relies
* on `getRuntimeConnectionSettings` vs `connectionSettings`
*/
export declare function acquireRawConnection(): Promise<any>;
+43
View File
@@ -0,0 +1,43 @@
"use strict";
/* eslint-disable @typescript-eslint/no-shadow */
/*
* knex-dynamic-connection
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.acquireRawConnection = void 0;
/**
* Copy of `acquireRawConnection` from knex codebase, but instead relies
* on `getRuntimeConnectionSettings` vs `connectionSettings`
*/
/* eslint no-shadow: "off" */
function acquireRawConnection() {
const client = this;
const connection = new client.driver.Client(client.getRuntimeConnectionSettings());
connection.on('error', (err) => {
connection.__knex__disposed = err;
});
connection.on('end', (err) => {
connection.__knex__disposed = err || 'Connection ended unexpectedly';
});
return connection
.connect()
.then(() => {
if (!client.version) {
return client.checkVersion(connection).then(function (version) {
client.version = version;
return connection;
});
}
return connection;
})
.then(function setSearchPath(connection) {
client.setSchemaSearchPath(connection);
return connection;
});
}
exports.acquireRawConnection = acquireRawConnection;