mirror of
https://github.com/knex/knex.git
synced 2025-12-29 16:08:45 +00:00
Extract static method _num out of class. Use super-methods (#4212)
This commit is contained in:
parent
bf287be13a
commit
0b2a2ca4d1
@ -1,6 +1,7 @@
|
||||
// MSSQL Column Compiler
|
||||
// -------
|
||||
const ColumnCompiler = require('../../../schema/columncompiler');
|
||||
const { toNumber } = require('../../../util/helpers');
|
||||
|
||||
class ColumnCompiler_MSSQL extends ColumnCompiler {
|
||||
constructor() {
|
||||
@ -31,7 +32,7 @@ class ColumnCompiler_MSSQL extends ColumnCompiler {
|
||||
}
|
||||
|
||||
varchar(length) {
|
||||
return `nvarchar(${this._num(length, 255)})`;
|
||||
return `nvarchar(${toNumber(length, 255)})`;
|
||||
}
|
||||
|
||||
timestamp({ useTz = false } = {}) {
|
||||
@ -46,7 +47,7 @@ class ColumnCompiler_MSSQL extends ColumnCompiler {
|
||||
}
|
||||
|
||||
binary(length) {
|
||||
return length ? `varbinary(${this._num(length)})` : 'varbinary(max)';
|
||||
return length ? `varbinary(${toNumber(length)})` : 'varbinary(max)';
|
||||
}
|
||||
|
||||
// Modifiers
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
// -------
|
||||
const ColumnCompiler = require('../../../schema/columncompiler');
|
||||
const { isObject } = require('../../../util/is');
|
||||
const { toNumber } = require('../../../util/helpers');
|
||||
|
||||
const commentEscapeRegex = /(?<!\\)'/g;
|
||||
|
||||
@ -24,16 +25,16 @@ class ColumnCompiler_MySQL extends ColumnCompiler {
|
||||
|
||||
double(precision, scale) {
|
||||
if (!precision) return 'double';
|
||||
return `double(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
|
||||
return `double(${toNumber(precision, 8)}, ${toNumber(scale, 2)})`;
|
||||
}
|
||||
|
||||
integer(length) {
|
||||
length = length ? `(${this._num(length, 11)})` : '';
|
||||
length = length ? `(${toNumber(length, 11)})` : '';
|
||||
return `int${length}`;
|
||||
}
|
||||
|
||||
tinyint(length) {
|
||||
length = length ? `(${this._num(length, 1)})` : '';
|
||||
length = length ? `(${toNumber(length, 1)})` : '';
|
||||
return `tinyint${length}`;
|
||||
}
|
||||
|
||||
@ -91,11 +92,11 @@ class ColumnCompiler_MySQL extends ColumnCompiler {
|
||||
}
|
||||
|
||||
bit(length) {
|
||||
return length ? `bit(${this._num(length)})` : 'bit';
|
||||
return length ? `bit(${toNumber(length)})` : 'bit';
|
||||
}
|
||||
|
||||
binary(length) {
|
||||
return length ? `varbinary(${this._num(length)})` : 'blob';
|
||||
return length ? `varbinary(${toNumber(length)})` : 'blob';
|
||||
}
|
||||
|
||||
json() {
|
||||
|
||||
18
lib/dialects/oracle/schema/internal/incrementUtils.js
Normal file
18
lib/dialects/oracle/schema/internal/incrementUtils.js
Normal file
@ -0,0 +1,18 @@
|
||||
const Trigger = require('./trigger');
|
||||
|
||||
// helper function for pushAdditional in increments() and bigincrements()
|
||||
function createAutoIncrementTriggerAndSequence(columnCompiler) {
|
||||
// TODO Add warning that sequence etc is created
|
||||
columnCompiler.pushAdditional(function () {
|
||||
const tableName = this.tableCompiler.tableNameRaw;
|
||||
const createTriggerSQL = Trigger.createAutoIncrementTrigger(
|
||||
this.client.logger,
|
||||
tableName
|
||||
);
|
||||
this.pushQuery(createTriggerSQL);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createAutoIncrementTriggerAndSequence,
|
||||
};
|
||||
@ -1,4 +1,4 @@
|
||||
const utils = require('../utils');
|
||||
const utils = require('../../utils');
|
||||
|
||||
const trigger = {
|
||||
renameColumnTrigger: function (logger, tableName, columnName, to) {
|
||||
@ -1,7 +1,10 @@
|
||||
const uniq = require('lodash/uniq');
|
||||
const Raw = require('../../../raw');
|
||||
const ColumnCompiler = require('../../../schema/columncompiler');
|
||||
const Trigger = require('./trigger');
|
||||
const {
|
||||
createAutoIncrementTriggerAndSequence,
|
||||
} = require('./internal/incrementUtils');
|
||||
const { toNumber } = require('../../../util/helpers');
|
||||
|
||||
// Column Compiler
|
||||
// -------
|
||||
@ -12,46 +15,33 @@ class ColumnCompiler_Oracle extends ColumnCompiler {
|
||||
this.modifiers = ['defaultTo', 'checkIn', 'nullable', 'comment'];
|
||||
}
|
||||
|
||||
// helper function for pushAdditional in increments() and bigincrements()
|
||||
_createAutoIncrementTriggerAndSequence() {
|
||||
// TODO Add warning that sequence etc is created
|
||||
this.pushAdditional(function () {
|
||||
const tableName = this.tableCompiler.tableNameRaw;
|
||||
const createTriggerSQL = Trigger.createAutoIncrementTrigger(
|
||||
this.client.logger,
|
||||
tableName
|
||||
);
|
||||
this.pushQuery(createTriggerSQL);
|
||||
});
|
||||
}
|
||||
|
||||
increments() {
|
||||
this._createAutoIncrementTriggerAndSequence();
|
||||
createAutoIncrementTriggerAndSequence(this);
|
||||
return 'integer not null primary key';
|
||||
}
|
||||
|
||||
bigincrements() {
|
||||
this._createAutoIncrementTriggerAndSequence();
|
||||
createAutoIncrementTriggerAndSequence(this);
|
||||
return 'number(20, 0) not null primary key';
|
||||
}
|
||||
|
||||
floating(precision) {
|
||||
const parsedPrecision = this._num(precision, 0);
|
||||
const parsedPrecision = toNumber(precision, 0);
|
||||
return `float${parsedPrecision ? `(${parsedPrecision})` : ''}`;
|
||||
}
|
||||
|
||||
double(precision, scale) {
|
||||
// if (!precision) return 'number'; // TODO: Check If default is ok
|
||||
return `number(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
|
||||
return `number(${toNumber(precision, 8)}, ${toNumber(scale, 2)})`;
|
||||
}
|
||||
|
||||
decimal(precision, scale) {
|
||||
if (precision === null) return 'decimal';
|
||||
return `decimal(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
|
||||
return `decimal(${toNumber(precision, 8)}, ${toNumber(scale, 2)})`;
|
||||
}
|
||||
|
||||
integer(length) {
|
||||
return length ? `number(${this._num(length, 11)})` : 'integer';
|
||||
return length ? `number(${toNumber(length, 11)})` : 'integer';
|
||||
}
|
||||
|
||||
enu(allowed) {
|
||||
@ -82,7 +72,7 @@ class ColumnCompiler_Oracle extends ColumnCompiler {
|
||||
}
|
||||
|
||||
varchar(length) {
|
||||
return `varchar2(${this._num(length, 255)})`;
|
||||
return `varchar2(${toNumber(length, 255)})`;
|
||||
}
|
||||
|
||||
// Modifiers
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
// -------
|
||||
const SchemaCompiler = require('../../../schema/compiler');
|
||||
const utils = require('../utils');
|
||||
const Trigger = require('./trigger');
|
||||
const Trigger = require('./internal/trigger');
|
||||
|
||||
class SchemaCompiler_Oracle extends SchemaCompiler {
|
||||
constructor() {
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
const utils = require('../utils');
|
||||
const TableCompiler = require('../../../schema/tablecompiler');
|
||||
const helpers = require('../../../util/helpers');
|
||||
const Trigger = require('./trigger');
|
||||
const Trigger = require('./internal/trigger');
|
||||
|
||||
// Table Compiler
|
||||
// ------
|
||||
|
||||
@ -21,7 +21,7 @@ class QueryCompiler_PG extends QueryCompiler {
|
||||
// Compiles an `insert` query, allowing for multiple
|
||||
// inserts using a single query statement.
|
||||
insert() {
|
||||
let sql = QueryCompiler.prototype.insert.call(this);
|
||||
let sql = super.insert();
|
||||
if (sql === '') return sql;
|
||||
|
||||
const { returning, onConflict, ignore, merge, insert } = this.single;
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
|
||||
const ColumnCompiler = require('../../../schema/columncompiler');
|
||||
const { isObject } = require('../../../util/is');
|
||||
const { toNumber } = require('../../../util/helpers');
|
||||
const commentEscapeRegex = /(?<!')'(?!')/g;
|
||||
|
||||
class ColumnCompiler_PG extends ColumnCompiler {
|
||||
@ -51,7 +52,7 @@ class ColumnCompiler_PG extends ColumnCompiler {
|
||||
|
||||
decimal(precision, scale) {
|
||||
if (precision === null) return 'decimal';
|
||||
return `decimal(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
|
||||
return `decimal(${toNumber(precision, 8)}, ${toNumber(scale, 2)})`;
|
||||
}
|
||||
|
||||
json(jsonb) {
|
||||
|
||||
@ -5,7 +5,7 @@ const Client_PG = require('../postgres');
|
||||
const map = require('lodash/map');
|
||||
|
||||
const Transaction = require('./transaction');
|
||||
const QueryCompiler = require('./query/compiler');
|
||||
const QueryCompiler = require('./query/reshift-querycompiler');
|
||||
const ColumnBuilder = require('./schema/redshift-columnbuilder');
|
||||
const ColumnCompiler = require('./schema/redshift-columncompiler');
|
||||
const TableCompiler = require('./schema/redshift-tablecompiler');
|
||||
|
||||
@ -94,12 +94,7 @@ class TableCompiler_Redshift extends TableCompiler_PG {
|
||||
// Compiles column add. Redshift can only add one column per ALTER TABLE, so core addColumns doesn't work. #2545
|
||||
addColumns(columns, prefix, colCompilers) {
|
||||
if (prefix === this.alterColumnsPrefix) {
|
||||
TableCompiler_PG.prototype.addColumns.call(
|
||||
this,
|
||||
columns,
|
||||
prefix,
|
||||
colCompilers
|
||||
);
|
||||
super.addColumns(columns, prefix, colCompilers);
|
||||
} else {
|
||||
prefix = prefix || this.addColumnsPrefix;
|
||||
colCompilers = colCompilers || this.getColumns();
|
||||
|
||||
@ -9,6 +9,7 @@ const first = require('lodash/first');
|
||||
const has = require('lodash/has');
|
||||
const tail = require('lodash/tail');
|
||||
const { isObject } = require('../util/is');
|
||||
const { toNumber } = require('../util/helpers');
|
||||
|
||||
class ColumnCompiler {
|
||||
constructor(client, tableCompiler, columnBuilder) {
|
||||
@ -90,11 +91,11 @@ class ColumnCompiler {
|
||||
// Types
|
||||
// ------
|
||||
varchar(length) {
|
||||
return `varchar(${this._num(length, 255)})`;
|
||||
return `varchar(${toNumber(length, 255)})`;
|
||||
}
|
||||
|
||||
floating(precision, scale) {
|
||||
return `float(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
|
||||
return `float(${toNumber(precision, 8)}, ${toNumber(scale, 2)})`;
|
||||
}
|
||||
|
||||
decimal(precision, scale) {
|
||||
@ -103,7 +104,7 @@ class ColumnCompiler {
|
||||
'Specifying no precision on decimal columns is not supported for that SQL dialect.'
|
||||
);
|
||||
}
|
||||
return `decimal(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
|
||||
return `decimal(${toNumber(precision, 8)}, ${toNumber(scale, 2)})`;
|
||||
}
|
||||
|
||||
// Used to support custom types
|
||||
@ -142,12 +143,6 @@ class ColumnCompiler {
|
||||
}
|
||||
return `default ${value}`;
|
||||
}
|
||||
|
||||
_num(val, fallback) {
|
||||
if (val === undefined || val === null) return fallback;
|
||||
const number = parseInt(val, 10);
|
||||
return isNaN(number) ? fallback : number;
|
||||
}
|
||||
}
|
||||
|
||||
ColumnCompiler.prototype.binary = 'blob';
|
||||
|
||||
@ -83,10 +83,17 @@ function resolveClientNameWithAliases(clientName) {
|
||||
return CLIENT_ALIASES[clientName] || clientName;
|
||||
}
|
||||
|
||||
function toNumber(val, fallback) {
|
||||
if (val === undefined || val === null) return fallback;
|
||||
const number = parseInt(val, 10);
|
||||
return isNaN(number) ? fallback : number;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
addQueryContext,
|
||||
containsUndefined,
|
||||
getUndefinedIndices,
|
||||
normalizeArr,
|
||||
resolveClientNameWithAliases,
|
||||
getUndefinedIndices,
|
||||
toNumber,
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user