Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cockroachdb): add support upsert (#4767)
Co-authored-by: Igor Savin <iselwin@gmail.com>
- Loading branch information
Showing
8 changed files
with
287 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const QueryBuilder = require('../../query/querybuilder'); | ||
const isEmpty = require('lodash/isEmpty'); | ||
|
||
module.exports = class QueryBuilder_CockroachDB extends QueryBuilder { | ||
upsert(values, returning, options) { | ||
this._method = 'upsert'; | ||
if (!isEmpty(returning)) this.returning(returning, options); | ||
this._single.upsert = values; | ||
return this; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,63 @@ | ||
const QueryCompiler_PG = require('../postgres/query/pg-querycompiler'); | ||
const isEmpty = require('lodash/isEmpty'); | ||
const { columnize: columnize_ } = require('../../formatter/wrappingFormatter'); | ||
|
||
class QueryCompiler_CRDB extends QueryCompiler_PG { | ||
truncate() { | ||
return `truncate ${this.tableName}`; | ||
} | ||
|
||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict'; | ||
|
||
const { expect } = require('chai'); | ||
const { | ||
Db, | ||
getKnexForDb, | ||
getAllDbs, | ||
} = require('../util/knex-instance-provider'); | ||
|
||
describe('CockroachDB dialect', () => { | ||
getAllDbs() | ||
.filter((db) => db === Db.CockroachDB) | ||
.forEach((db) => { | ||
describe(db, () => { | ||
let knex; | ||
before(async () => { | ||
knex = getKnexForDb(db); | ||
|
||
await knex.schema.createTable('test', function () { | ||
this.increments('id').primary(); | ||
this.decimal('balance'); | ||
this.string('name'); | ||
}); | ||
}); | ||
|
||
after(async () => { | ||
await knex.schema.dropTable('test'); | ||
await knex.destroy(); | ||
}); | ||
|
||
describe('Upsert into query with unique', () => { | ||
it('select empty table', async () => { | ||
const results = await knex('test').select(); | ||
expect(results).is.empty; | ||
}); | ||
|
||
it('upsert id=1 with result insert', async () => { | ||
const results = await knex('test').upsert({ id: 1, balance: 10 }); | ||
expect(results.command).to.eql('INSERT'); | ||
expect(results.rowCount).to.eql(1); | ||
}); | ||
|
||
it('select rows', async () => { | ||
const results = await knex('test').select(); | ||
expect(results).to.eql([{ id: '1', balance: '10.00', name: null }]); | ||
}); | ||
|
||
it('upsert id=1 with result update balance', async () => { | ||
const results = await knex('test').upsert({ id: 1, balance: 5 }); | ||
expect(results.command).to.eql('INSERT'); | ||
expect(results.rowCount).to.eql(1); | ||
}); | ||
|
||
it('select rows', async () => { | ||
const results = await knex('test').select(); | ||
expect(results.length).to.eql(1); | ||
expect(results).to.eql([{ id: '1', balance: '5.00', name: null }]); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use strict'; | ||
const expect = require('chai').expect; | ||
const knex = require('../../../knex'); | ||
|
||
describe('CockroachDB unit tests', () => { | ||
const knexInstance = knex({ | ||
client: 'cockroachdb', | ||
connection: { | ||
user: 'user', | ||
password: 'password', | ||
connectString: 'connect-string', | ||
externalAuth: true, | ||
host: 'host', | ||
database: 'database', | ||
}, | ||
}); | ||
|
||
it('correctly builds single value upsert', async () => { | ||
const qb = knexInstance.client.queryBuilder(); | ||
qb.upsert({ value: 1 }).into('fakeTable'); | ||
const compiler = knexInstance.client.queryCompiler(qb); | ||
const sql = compiler.upsert(); | ||
expect(sql).to.eql({ | ||
sql: 'upsert into "fakeTable" ("value") values (?)', | ||
returning: undefined, | ||
}); | ||
}); | ||
|
||
it('correctly builds default values only upsert', async () => { | ||
const qb = knexInstance.client.queryBuilder(); | ||
qb.upsert({}).into('fakeTable'); | ||
const compiler = knexInstance.client.queryCompiler(qb); | ||
const sql = compiler.upsert(); | ||
expect(sql).to.eql({ | ||
sql: 'upsert into "fakeTable" default values', | ||
returning: undefined, | ||
}); | ||
}); | ||
|
||
it('correctly builds default values only upsert and returning', async () => { | ||
const qb = knexInstance.client.queryBuilder(); | ||
qb.upsert({}).into('fakeTable').returning('id'); | ||
const compiler = knexInstance.client.queryCompiler(qb); | ||
const sql = compiler.upsert(); | ||
expect(sql).to.eql({ | ||
sql: 'upsert into "fakeTable" default values returning "id"', | ||
returning: 'id', | ||
}); | ||
}); | ||
|
||
it('correctly builds bulk values only upsert and returning', async () => { | ||
const qb = knexInstance.client.queryBuilder(); | ||
qb.upsert([{ a: 1 }, { a: 2 }]) | ||
.into('fakeTable') | ||
.returning('a'); | ||
const compiler = knexInstance.client.queryCompiler(qb); | ||
const sql = compiler.upsert(); | ||
expect(sql).to.eql({ | ||
sql: 'upsert into "fakeTable" ("a") values (?), (?) returning "a"', | ||
returning: 'a', | ||
}); | ||
}); | ||
|
||
it('correctly builds bulk values different columns upsert and returning', async () => { | ||
const qb = knexInstance.client.queryBuilder(); | ||
qb.upsert([{ a: 1 }, { b: 2 }]) | ||
.into('fakeTable') | ||
.returning(['a', 'b']); | ||
const compiler = knexInstance.client.queryCompiler(qb); | ||
const sql = compiler.upsert(); | ||
expect(sql).to.eql({ | ||
sql: 'upsert into "fakeTable" ("a", "b") values (?, DEFAULT), (DEFAULT, ?) returning "a", "b"', | ||
returning: ['a', 'b'], | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters