Comparing version 0.95.12-rc4 to 0.95.12-rc5
const QueryCompiler_PG = require('../postgres/query/pg-querycompiler'); | ||
const isEmpty = require('lodash/isEmpty'); | ||
const { columnize: columnize_ } = require('../../formatter/wrappingFormatter'); | ||
@@ -7,4 +9,56 @@ class QueryCompiler_CRDB extends QueryCompiler_PG { | ||
} | ||
upsert() { | ||
let sql = this._upsert(); | ||
if (sql === '') return sql; | ||
const { returning } = this.single; | ||
if (returning) sql += this._returning(returning); | ||
return { | ||
sql: sql, | ||
returning, | ||
}; | ||
} | ||
_upsert() { | ||
const upsertValues = this.single.upsert || []; | ||
let sql = this.with() + `upsert into ${this.tableName} `; | ||
if (Array.isArray(upsertValues)) { | ||
if (upsertValues.length === 0) return ''; | ||
} else if (typeof upsertValues === 'object' && isEmpty(upsertValues)) { | ||
return sql + this._emptyInsertValue; | ||
} | ||
const upsertData = this._prepInsert(upsertValues); | ||
if (typeof upsertData === 'string') { | ||
sql += upsertData; | ||
} else { | ||
if (upsertData.columns.length) { | ||
sql += `(${columnize_( | ||
upsertData.columns, | ||
this.builder, | ||
this.client, | ||
this.bindingsHolder | ||
)}`; | ||
sql += ') values ('; | ||
let i = -1; | ||
while (++i < upsertData.values.length) { | ||
if (i !== 0) sql += '), ('; | ||
sql += this.client.parameterize( | ||
upsertData.values[i], | ||
this.client.valueForUndefined, | ||
this.builder, | ||
this.bindingsHolder | ||
); | ||
} | ||
sql += ')'; | ||
} else if (upsertValues.length === 1 && upsertValues[0]) { | ||
sql += this._emptyInsertValue; | ||
} else { | ||
sql = ''; | ||
} | ||
} | ||
return sql; | ||
} | ||
} | ||
module.exports = QueryCompiler_CRDB; |
@@ -8,2 +8,3 @@ // CockroachDB Client | ||
const ViewCompiler = require('./crdb-viewcompiler'); | ||
const QueryBuilder = require('./crdb-querybuilder'); | ||
@@ -30,2 +31,6 @@ // Always initialize with the "QueryBuilder" and "QueryCompiler" | ||
queryBuilder() { | ||
return new QueryBuilder(this); | ||
} | ||
_parseVersion(versionString) { | ||
@@ -32,0 +37,0 @@ return versionString.split(' ')[2]; |
@@ -222,10 +222,18 @@ /* eslint max-len:0 */ | ||
index(columns, indexName) { | ||
index(columns, indexName, options) { | ||
indexName = indexName | ||
? this.formatter.wrap(indexName) | ||
: this._indexCommand('index', this.tableNameRaw, columns); | ||
let predicate; | ||
if (isObject(options)) { | ||
({ predicate } = options); | ||
} | ||
const predicateQuery = predicate | ||
? ' ' + this.client.queryCompiler(predicate).where() | ||
: ''; | ||
this.pushQuery( | ||
`CREATE INDEX ${indexName} ON ${this.tableName()} (${this.formatter.columnize( | ||
columns | ||
)})` | ||
)})${predicateQuery}` | ||
); | ||
@@ -232,0 +240,0 @@ } |
@@ -6,3 +6,3 @@ /* eslint max-len:0*/ | ||
const TableCompiler = require('../../../schema/tablecompiler'); | ||
const { isObject } = require('../../../util/is'); | ||
const { isObject, isString } = require('../../../util/is'); | ||
@@ -215,10 +215,24 @@ // Table Compiler | ||
index(columns, indexName, indexType) { | ||
index(columns, indexName, options) { | ||
let storageEngineIndexType; | ||
let indexType; | ||
if (isString(options)) { | ||
indexType = options; | ||
} else if (isObject(options)) { | ||
({ indexType, storageEngineIndexType } = options); | ||
} | ||
indexName = indexName | ||
? this.formatter.wrap(indexName) | ||
: this._indexCommand('index', this.tableNameRaw, columns); | ||
storageEngineIndexType = storageEngineIndexType | ||
? ` using ${storageEngineIndexType}` | ||
: ''; | ||
this.pushQuery( | ||
`alter table ${this.tableName()} add${ | ||
indexType ? ` ${indexType}` : '' | ||
} index ${indexName}(${this.formatter.columnize(columns)})` | ||
} index ${indexName}(${this.formatter.columnize( | ||
columns | ||
)})${storageEngineIndexType}` | ||
); | ||
@@ -248,5 +262,6 @@ } | ||
unique(columns, indexName) { | ||
let storageEngineIndexType; | ||
let deferrable; | ||
if (isObject(indexName)) { | ||
({ indexName, deferrable } = indexName); | ||
({ indexName, deferrable, storageEngineIndexType } = indexName); | ||
} | ||
@@ -261,6 +276,9 @@ if (deferrable && deferrable !== 'not deferrable') { | ||
: this._indexCommand('unique', this.tableNameRaw, columns); | ||
storageEngineIndexType = storageEngineIndexType | ||
? ` using ${storageEngineIndexType}` | ||
: ''; | ||
this.pushQuery( | ||
`alter table ${this.tableName()} add unique ${indexName}(${this.formatter.columnize( | ||
columns | ||
)})` | ||
)})${storageEngineIndexType}` | ||
); | ||
@@ -267,0 +285,0 @@ } |
@@ -150,18 +150,24 @@ // PostgreSQL Query Builder & Compiler | ||
forUpdate() { | ||
_lockingClause(lockMode) { | ||
const tables = this.single.lockTables || []; | ||
return ( | ||
'for update' + (tables.length ? ' of ' + this._tableNames(tables) : '') | ||
); | ||
return lockMode + (tables.length ? ' of ' + this._tableNames(tables) : ''); | ||
} | ||
forUpdate() { | ||
return this._lockingClause('for update'); | ||
} | ||
forShare() { | ||
const tables = this.single.lockTables || []; | ||
return this._lockingClause('for share'); | ||
} | ||
return ( | ||
'for share' + (tables.length ? ' of ' + this._tableNames(tables) : '') | ||
); | ||
forNoKeyUpdate() { | ||
return this._lockingClause('for no key update'); | ||
} | ||
forKeyShare() { | ||
return this._lockingClause('for key share'); | ||
} | ||
skipLocked() { | ||
@@ -168,0 +174,0 @@ return 'skip locked'; |
@@ -8,3 +8,3 @@ /* eslint max-len: 0 */ | ||
const TableCompiler = require('../../../schema/tablecompiler'); | ||
const { isObject } = require('../../../util/is'); | ||
const { isObject, isString } = require('../../../util/is'); | ||
@@ -165,6 +165,20 @@ class TableCompiler_PG extends TableCompiler { | ||
index(columns, indexName, indexType) { | ||
index(columns, indexName, options) { | ||
indexName = indexName | ||
? this.formatter.wrap(indexName) | ||
: this._indexCommand('index', this.tableNameRaw, columns); | ||
let predicate; | ||
let indexType; | ||
if (isString(options)) { | ||
indexType = options; | ||
} else if (isObject(options)) { | ||
({ indexType, predicate } = options); | ||
} | ||
const predicateQuery = predicate | ||
? ' ' + this.client.queryCompiler(predicate).where() | ||
: ''; | ||
this.pushQuery( | ||
@@ -176,3 +190,4 @@ `create index ${indexName} on ${this.tableName()}${ | ||
this.formatter.columnize(columns) + | ||
')' | ||
')' + | ||
`${predicateQuery}` | ||
); | ||
@@ -179,0 +194,0 @@ } |
@@ -64,2 +64,14 @@ // Redshift Query Builder & Compiler | ||
forNoKeyUpdate() { | ||
this.client.logger.warn('table lock is not supported by redshift dialect'); | ||
return ''; | ||
} | ||
forKeyShare() { | ||
this.client.logger.warn( | ||
'lock for share is not supported by redshift dialect' | ||
); | ||
return ''; | ||
} | ||
// Compiles a columnInfo query | ||
@@ -66,0 +78,0 @@ columnInfo() { |
@@ -14,3 +14,3 @@ /* eslint max-len: 0 */ | ||
index(columns, indexName, indexType) { | ||
index(columns, indexName, options) { | ||
this.client.logger.warn( | ||
@@ -17,0 +17,0 @@ 'Redshift does not support the creation of indexes.' |
@@ -30,3 +30,5 @@ // SQLite3 Query Builder & Compiler | ||
this.forShare = emptyStr; | ||
this.forKeyShare = emptyStr; | ||
this.forUpdate = emptyStr; | ||
this.forNoKeyUpdate = emptyStr; | ||
} | ||
@@ -33,0 +35,0 @@ |
@@ -148,3 +148,3 @@ const filter = require('lodash/filter'); | ||
// Compile a plain index key command. | ||
index(columns, indexName) { | ||
index(columns, indexName, options) { | ||
indexName = indexName | ||
@@ -154,4 +154,12 @@ ? this.formatter.wrap(indexName) | ||
columns = this.formatter.columnize(columns); | ||
let predicate; | ||
if (isObject(options)) { | ||
({ predicate } = options); | ||
} | ||
const predicateQuery = predicate | ||
? ' ' + this.client.queryCompiler(predicate).where() | ||
: ''; | ||
this.pushQuery( | ||
`create index ${indexName} on ${this.tableName()} (${columns})` | ||
`create index ${indexName} on ${this.tableName()} (${columns})${predicateQuery}` | ||
); | ||
@@ -158,0 +166,0 @@ } |
@@ -8,2 +8,4 @@ /** | ||
forUpdate: 'forUpdate', | ||
forNoKeyUpdate: 'forNoKeyUpdate', | ||
forKeyShare: 'forKeyShare', | ||
}, | ||
@@ -10,0 +12,0 @@ waitMode: { |
@@ -49,3 +49,8 @@ // Builder | ||
]); | ||
const LOCK_MODES = new Set([lockMode.forShare, lockMode.forUpdate]); | ||
const LOCK_MODES = new Set([ | ||
lockMode.forShare, | ||
lockMode.forUpdate, | ||
lockMode.forNoKeyUpdate, | ||
lockMode.forKeyShare, | ||
]); | ||
@@ -1202,2 +1207,16 @@ // Typically called from `knex.builder`, | ||
// Set a lock for no key update constraint. | ||
forNoKeyUpdate(...tables) { | ||
this._single.lock = lockMode.forNoKeyUpdate; | ||
this._single.lockTables = tables; | ||
return this; | ||
} | ||
// Set a lock for key share constraint. | ||
forKeyShare(...tables) { | ||
this._single.lock = lockMode.forKeyShare; | ||
this._single.lockTables = tables; | ||
return this; | ||
} | ||
// Skips locked rows when using a lock constraint. | ||
@@ -1259,2 +1278,8 @@ skipLocked() { | ||
upsert(values, returning, options) { | ||
throw new Error( | ||
`Upsert is not yet supported for dialect ${this.client.dialect}` | ||
); | ||
} | ||
_analytic(alias, second, third) { | ||
@@ -1261,0 +1286,0 @@ let analytic; |
{ | ||
"name": "knex", | ||
"version": "0.95.12-rc4", | ||
"version": "0.95.12-rc5", | ||
"description": "A batteries-included SQL query & schema builder for PostgresSQL, MySQL, CockroachDB, MSSQL and SQLite3", | ||
@@ -36,4 +36,4 @@ "main": "knex", | ||
"test:cli": "cross-env KNEX_PATH=../knex.js KNEX=bin/cli.js jake -f test/jake/Jakefile", | ||
"db:start": "docker-compose -f scripts/docker-compose.yml up --build -d mysql oracledbxe postgres mssql && docker-compose -f scripts/docker-compose.yml up waitmssql waitmysql waitpostgres waitoracledbxe", | ||
"db:start:no-oracle": "docker-compose -f scripts/docker-compose.yml up --build -d mysql postgres mssql && docker-compose -f scripts/docker-compose.yml up waitmssql waitmysql waitpostgres", | ||
"db:start": "docker-compose -f scripts/docker-compose.yml up --build -d mysql oracledbxe postgres mssql cockroachdb pgnative && docker-compose -f scripts/docker-compose.yml up waitmssql waitmysql waitpostgres waitoracledbxe", | ||
"db:start:no-oracle": "docker-compose -f scripts/docker-compose.yml up --build -d mysql postgres mssql cockroachdb pgnative && docker-compose -f scripts/docker-compose.yml up waitmssql waitmysql waitpostgres", | ||
"db:stop": "docker-compose -f scripts/docker-compose.yml down", | ||
@@ -105,6 +105,6 @@ "db:start:postgres": "docker-compose -f scripts/docker-compose.yml up --build -d postgres && docker-compose -f scripts/docker-compose.yml up waitpostgres", | ||
"cross-env": "^7.0.3", | ||
"dtslint": "4.1.6", | ||
"eslint": "^7.32.0", | ||
"dtslint": "4.2.0", | ||
"eslint": "^8.1.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-import": "^2.24.2", | ||
"eslint-plugin-import": "^2.25.2", | ||
"husky": "^4.3.8", | ||
@@ -111,0 +111,0 @@ "jake": "^8.1.1", |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
768253
175
20323