Skip to content

Commit 129ecc0

Browse files
committed
code cleanup
1 parent 6aa309a commit 129ecc0

File tree

10 files changed

+55
-72
lines changed

10 files changed

+55
-72
lines changed

src/operation-node/create-schema-node.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ export const CreateSchemaNode = freeze({
3030
})
3131
},
3232

33-
cloneWithModifier(
33+
cloneWith(
3434
createSchema: CreateSchemaNode,
35-
modifier: CreateSchemaNodeModifier
35+
params: CreateSchemaNodeParams
3636
): CreateSchemaNode {
3737
return freeze({
3838
...createSchema,
39-
modifier,
39+
...params,
4040
})
4141
},
4242
})

src/operation-node/create-table-node.ts

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { freeze } from '../util/object-utils.js'
2-
import { CheckConstraintNode } from './check-constraint-node.js'
32
import { OperationNode } from './operation-node.js'
43
import { TableNode } from './table-node.js'
5-
import { PrimaryConstraintNode } from './primary-constraint-node.js'
6-
import { UniqueConstraintNode } from './unique-constraint-node.js'
7-
import { ForeignKeyConstraintNode } from './foreign-key-constraint-node.js'
84
import { ConstraintNode } from './constraint-node.js'
95
import { ColumnDefinitionNode } from './column-definition-node.js'
106

@@ -54,54 +50,9 @@ export const CreateTableNode = freeze({
5450
})
5551
},
5652

57-
cloneWithPrimaryKeyConstraint(
53+
cloneWithConstraint(
5854
createTable: CreateTableNode,
59-
constraintName: string,
60-
columns: string[]
61-
): CreateTableNode {
62-
const constraint = PrimaryConstraintNode.create(columns, constraintName)
63-
64-
return freeze({
65-
...createTable,
66-
constraints: createTable.constraints
67-
? freeze([...createTable.constraints, constraint])
68-
: freeze([constraint]),
69-
})
70-
},
71-
72-
cloneWithUniqueConstraint(
73-
createTable: CreateTableNode,
74-
constraintName: string,
75-
columns: string[]
76-
): CreateTableNode {
77-
const constraint = UniqueConstraintNode.create(columns, constraintName)
78-
79-
return freeze({
80-
...createTable,
81-
constraints: createTable.constraints
82-
? freeze([...createTable.constraints, constraint])
83-
: freeze([constraint]),
84-
})
85-
},
86-
87-
cloneWithCheckConstraint(
88-
createTable: CreateTableNode,
89-
constraintName: string,
90-
sql: string
91-
): CreateTableNode {
92-
const constraint = CheckConstraintNode.create(sql, constraintName)
93-
94-
return freeze({
95-
...createTable,
96-
constraints: createTable.constraints
97-
? freeze([...createTable.constraints, constraint])
98-
: freeze([constraint]),
99-
})
100-
},
101-
102-
cloneWithForeignKeyConstraint(
103-
createTable: CreateTableNode,
104-
constraint: ForeignKeyConstraintNode
55+
constraint: ConstraintNode
10556
): CreateTableNode {
10657
return freeze({
10758
...createTable,

src/operation-node/on-duplicate-key-node.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { freeze } from '../util/object-utils.js'
2-
import { ColumnNode } from './column-node.js'
32
import { ColumnUpdateNode } from './column-update-node.js'
43
import { OperationNode } from './operation-node.js'
54

src/operation-node/operation-node-source.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export interface OperationNodeSource {
55
toOperationNode(): OperationNode
66
}
77

8-
export function isOperationNodeSource(obj: any): obj is OperationNodeSource {
8+
export function isOperationNodeSource(
9+
obj: unknown
10+
): obj is OperationNodeSource {
911
return isObject(obj) && isFunction(obj.toOperationNode)
1012
}

src/operation-node/operation-node-transformer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,11 +583,12 @@ export class OperationNodeTransformer {
583583
kind: 'AlterTableNode',
584584
table: this.transformNode(node.table),
585585
renameTo: this.transformNode(node.renameTo),
586-
renameColumn: this.transformNode(node.renameColumn),
587586
setSchema: this.transformNode(node.setSchema),
587+
renameColumn: this.transformNode(node.renameColumn),
588588
addColumn: this.transformNode(node.addColumn),
589589
dropColumn: this.transformNode(node.dropColumn),
590590
alterColumn: this.transformNode(node.alterColumn),
591+
modifyColumn: this.transformNode(node.modifyColumn),
591592
addConstraint: this.transformNode(node.addConstraint),
592593
dropConstraint: this.transformNode(node.dropConstraint),
593594
}

src/query-compiler/default-query-compiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ const SELECT_MODIFIER_SQL: Record<SelectModifier, string> = {
930930
ForNoKeyUpdate: 'for no key update',
931931
ForUpdate: 'for update',
932932
ForShare: 'for share',
933-
NoWait: 'no wait',
933+
NoWait: 'nowait',
934934
SkipLocked: 'skip locked',
935935
}
936936

src/schema/create-schema-builder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export class CreateSchemaBuilder implements OperationNodeSource, Compilable {
1717
ifNotExists(): CreateSchemaBuilder {
1818
return new CreateSchemaBuilder({
1919
...this.#props,
20-
createSchemaNode: CreateSchemaNode.cloneWithModifier(
20+
createSchemaNode: CreateSchemaNode.cloneWith(
2121
this.#props.createSchemaNode,
22-
'IfNotExists'
22+
{ modifier: 'IfNotExists' }
2323
),
2424
})
2525
}

src/schema/create-table-builder.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import {
1616
DataTypeExpression,
1717
parseDataTypeExpression,
1818
} from '../parser/data-type-parser.js'
19+
import { PrimaryConstraintNode } from '../operation-node/primary-constraint-node.js'
20+
import { UniqueConstraintNode } from '../operation-node/unique-constraint-node.js'
21+
import { CheckConstraintNode } from '../operation-node/check-constraint-node.js'
1922

2023
/**
2124
* This builder can be used to create a `create table` query.
@@ -128,10 +131,9 @@ export class CreateTableBuilder<TB extends string, C extends string = never>
128131
): CreateTableBuilder<TB, C> {
129132
return new CreateTableBuilder({
130133
...this.#props,
131-
createTableNode: CreateTableNode.cloneWithPrimaryKeyConstraint(
134+
createTableNode: CreateTableNode.cloneWithConstraint(
132135
this.#props.createTableNode,
133-
constraintName,
134-
columns
136+
PrimaryConstraintNode.create(columns, constraintName)
135137
),
136138
})
137139
}
@@ -153,10 +155,9 @@ export class CreateTableBuilder<TB extends string, C extends string = never>
153155
): CreateTableBuilder<TB, C> {
154156
return new CreateTableBuilder({
155157
...this.#props,
156-
createTableNode: CreateTableNode.cloneWithUniqueConstraint(
158+
createTableNode: CreateTableNode.cloneWithConstraint(
157159
this.#props.createTableNode,
158-
constraintName,
159-
columns
160+
UniqueConstraintNode.create(columns, constraintName)
160161
),
161162
})
162163
}
@@ -178,10 +179,9 @@ export class CreateTableBuilder<TB extends string, C extends string = never>
178179
): CreateTableBuilder<TB, C> {
179180
return new CreateTableBuilder({
180181
...this.#props,
181-
createTableNode: CreateTableNode.cloneWithCheckConstraint(
182+
createTableNode: CreateTableNode.cloneWithConstraint(
182183
this.#props.createTableNode,
183-
constraintName,
184-
checkExpression
184+
CheckConstraintNode.create(checkExpression, constraintName)
185185
),
186186
})
187187
}
@@ -233,7 +233,7 @@ export class CreateTableBuilder<TB extends string, C extends string = never>
233233

234234
return new CreateTableBuilder({
235235
...this.#props,
236-
createTableNode: CreateTableNode.cloneWithForeignKeyConstraint(
236+
createTableNode: CreateTableNode.cloneWithConstraint(
237237
this.#props.createTableNode,
238238
builder.toOperationNode()
239239
),

test/src/select.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,33 @@ for (const dialect of BUILT_IN_DIALECTS) {
405405
{ first_name: 'Arnold' },
406406
])
407407
})
408+
409+
for (const [methods, sql] of [
410+
[['forUpdate'], 'for update'],
411+
[['forShare'], 'for share'],
412+
[['forNoKeyUpdate'], 'for no key update'],
413+
[['forKeyShare'], 'for key share'],
414+
[['forUpdate', 'noWait'], 'for update nowait'],
415+
[['forUpdate', 'skipLocked'], 'for update skip locked'],
416+
] as const) {
417+
it(`should support "${sql}"`, async () => {
418+
let query = ctx.db.selectFrom('person').selectAll()
419+
420+
for (const method of methods) {
421+
query = query[method]()
422+
}
423+
424+
testSql(query, dialect, {
425+
postgres: {
426+
sql: `select * from "person" ${sql}`,
427+
parameters: [],
428+
},
429+
mysql: NOT_SUPPORTED,
430+
})
431+
432+
await query.execute()
433+
})
434+
}
408435
}
409436

410437
it('should use an aggregate function in a select call', async () => {

test/src/test-setup.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ const PLUGINS: KyselyPlugin[] = []
7474

7575
if (process.env.TEST_TRANSFORMER) {
7676
console.log('running tests with a transformer')
77-
PLUGINS.push(createNoopPlugin())
77+
// Add a noop transformer using a plugin to make sure that the
78+
// OperationNodeTransformer base class is implemented correctly
79+
// and all nodes and properties get cloned by default.
80+
PLUGINS.push(createNoopTransformerPlugin())
7881
}
7982

8083
const DB_CONFIGS: PerDialect<KyselyConfig> = {
@@ -307,7 +310,7 @@ function getIdFromInsertResult<T>(result: any): T {
307310
}
308311
}
309312

310-
function createNoopPlugin(): KyselyPlugin {
313+
function createNoopTransformerPlugin(): KyselyPlugin {
311314
const transformer = new OperationNodeTransformer()
312315

313316
return {

0 commit comments

Comments
 (0)