Skip to content

Commit

Permalink
Support insert into foo default values queries. Closes kysely-org#685
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas authored and thecodrr committed Sep 3, 2024
1 parent 163d61b commit 5ec5d69
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/operation-node/insert-query-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface InsertQueryNode extends OperationNode {
readonly ignore?: boolean
readonly replace?: boolean
readonly explain?: ExplainNode
readonly defaultValues?: boolean
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/operation-node/operation-node-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ export class OperationNodeTransformer {
ignore: node.ignore,
replace: node.replace,
explain: this.transformNode(node.explain),
defaultValues: node.defaultValues,
})
}

Expand Down
12 changes: 12 additions & 0 deletions src/query-builder/insert-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,18 @@ export class InsertQueryBuilder<DB, TB extends keyof DB, O>
})
}

/**
* Creates an `insert into "person" default values` query.
*/
defaultValues(): InsertQueryBuilder<DB, TB, O> {
return new InsertQueryBuilder({
...this.#props,
queryNode: InsertQueryNode.cloneWith(this.#props.queryNode, {
defaultValues: true,
}),
})
}

/**
* Changes an `insert into` query to an `insert ignore into` query.
*
Expand Down
5 changes: 5 additions & 0 deletions src/query-compiler/default-query-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ export class DefaultQueryCompiler
this.visitNode(node.values)
}

if (node.defaultValues) {
this.append(' ')
this.append('default values')
}

if (node.onConflict) {
this.append(' ')
this.visitNode(node.onConflict)
Expand Down
88 changes: 55 additions & 33 deletions test/node/src/insert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,29 @@ for (const dialect of DIALECTS) {
})
})

it('should insert one row with default values', async () => {
const query = ctx.db.insertInto('person').defaultValues()

testSql(query, dialect, {
postgres: {
sql: 'insert into "person" default values',
parameters: [],
},
mysql: {
sql: 'insert into `person` default values',
parameters: [],
},
mssql: {
sql: 'insert into "person" default values',
parameters: [],
},
sqlite: {
sql: 'insert into "person" default values',
parameters: [],
},
})
})

it('should insert one row with complex values', async () => {
const query = ctx.db.insertInto('person').values({
first_name: ctx.db
Expand Down Expand Up @@ -118,43 +141,42 @@ for (const dialect of DIALECTS) {
})
})

it.skip('should insert one row with expressions', async () => {
const query = ctx.db.insertInto('person').values(({ selectFrom }) => ({
first_name: selectFrom('pet')
.select('name')
.where('species', '=', 'dog')
.limit(1),
gender: 'female',
}))
if (dialect !== 'mssql') {
it('should insert one row with expressions', async () => {
const query = ctx.db.insertInto('person').values(({ selectFrom }) => ({
first_name: selectFrom('pet')
.select('name')
.where('species', '=', 'dog')
.limit(1),
gender: 'female',
}))

testSql(query, dialect, {
postgres: {
sql: `insert into "person" ("first_name", "gender") values ((select "first_name" from "person" where "last_name" = $1 limit $2), $3)`,
parameters: ['Aniston', 1, 'female'],
},
mysql: {
sql: 'insert into `person` (`first_name`, `gender`) values ((select `first_name` from `person` where `last_name` = ? limit ?), ?)',
parameters: ['Aniston', 1, 'female'],
},
mssql: {
sql: `insert into "person" ("first_name", "gender") values ((select "first_name" from "person" where "last_name" = @1 limit @2), @3)`,
parameters: ['Aniston', 1, 'female'],
},
sqlite: {
sql: `insert into "person" ("first_name", "gender") values ((select "first_name" from "person" where "last_name" = ? limit ?), ?)`,
parameters: ['Aniston', 1, 'female'],
},
})
testSql(query, dialect, {
postgres: {
sql: `insert into "person" ("first_name", "gender") values ((select "name" from "pet" where "species" = $1 limit $2), $3)`,
parameters: ['dog', 1, 'female'],
},
mysql: {
sql: 'insert into `person` (`first_name`, `gender`) values ((select `name` from `pet` where `species` = ? limit ?), ?)',
parameters: ['dog', 1, 'female'],
},
sqlite: {
sql: `insert into "person" ("first_name", "gender") values ((select "name" from "pet" where "species" = ? limit ?), ?)`,
parameters: ['dog', 1, 'female'],
},
mssql: NOT_SUPPORTED,
})

const result = await query.executeTakeFirst()
expect(result).to.be.instanceOf(InsertResult)
expect(result.numInsertedOrUpdatedRows).to.equal(1n)
const result = await query.executeTakeFirst()
expect(result).to.be.instanceOf(InsertResult)
expect(result.numInsertedOrUpdatedRows).to.equal(1n)

expect(await getNewestPerson(ctx.db)).to.eql({
first_name: 'Jennifer',
last_name: null,
expect(await getNewestPerson(ctx.db)).to.eql({
first_name: 'Doggo',
last_name: null,
})
})
})
}

it('should insert the result of a select query', async () => {
const query = ctx.db
Expand Down
4 changes: 2 additions & 2 deletions test/node/src/select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ for (const dialect of DIALECTS) {
parameters: ['Jennifer'],
},
mysql: {
sql: 'select `last_name` from `person` where `first_name` = ? for update of "person"',
sql: 'select `last_name` from `person` where `first_name` = ? for update of `person`',
parameters: ['Jennifer'],
},
mssql: NOT_SUPPORTED,
Expand Down Expand Up @@ -712,7 +712,7 @@ for (const dialect of DIALECTS) {
parameters: ['Jennifer'],
},
mysql: {
sql: 'select `last_name` from `person` where `first_name` = ? for update of "person" skip locked',
sql: 'select `last_name` from `person` where `first_name` = ? for update of `person` skip locked',
parameters: ['Jennifer'],
},
mssql: NOT_SUPPORTED,
Expand Down

0 comments on commit 5ec5d69

Please sign in to comment.