Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add addIndex & dropIndex @ AlterTableBuilder #720

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/operation-node/add-index-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { freeze } from '../util/object-utils.js'
import { IdentifierNode } from './identifier-node.js'
import { OperationNode } from './operation-node.js'
import { RawNode } from './raw-node.js'

export type AddIndexNodeProps = Omit<AddIndexNode, 'kind' | 'name'>

export interface AddIndexNode extends OperationNode {
readonly kind: 'AddIndexNode'
readonly name: IdentifierNode
readonly columns?: OperationNode[]
readonly unique?: boolean
readonly using?: RawNode
readonly ifNotExists?: boolean
}

/**
* @internal
*/
export const AddIndexNode = freeze({
is(node: OperationNode): node is AddIndexNode {
return node.kind === 'AddIndexNode'
},

create(name: string): AddIndexNode {
return freeze({
kind: 'AddIndexNode',
name: IdentifierNode.create(name)
})
},

cloneWith(node: AddIndexNode, props: AddIndexNodeProps): AddIndexNode {
return freeze({
...node,
...props,
})
},

cloneWithColumns(node: AddIndexNode, columns: OperationNode[]): AddIndexNode {
return freeze({
...node,
columns: [...(node.columns || []), ...columns],
})
},
})
igalklebanov marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 5 additions & 1 deletion src/operation-node/alter-table-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import { AlterColumnNode } from './alter-column-node.js'
import { AddConstraintNode } from './add-constraint-node.js'
import { DropConstraintNode } from './drop-constraint-node.js'
import { ModifyColumnNode } from './modify-column-node.js'
import { DropIndexNode } from './drop-index-node.js'
import { AddIndexNode } from './add-index-node.js'

export type AlterTableNodeTableProps = Pick<
AlterTableNode,
'renameTo' | 'setSchema' | 'addConstraint' | 'dropConstraint'
'renameTo' | 'setSchema' | 'addConstraint' | 'dropConstraint' | 'addIndex' | 'dropIndex'
>

export type AlterTableColumnAlterationNode =
Expand All @@ -30,6 +32,8 @@ export interface AlterTableNode extends OperationNode {
readonly columnAlterations?: ReadonlyArray<AlterTableColumnAlterationNode>
readonly addConstraint?: AddConstraintNode
readonly dropConstraint?: DropConstraintNode
readonly addIndex?: AddIndexNode
readonly dropIndex?: DropIndexNode
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/operation-node/operation-node-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import { JSONPathNode } from './json-path-node.js'
import { JSONPathLegNode } from './json-path-leg-node.js'
import { JSONOperatorChainNode } from './json-operator-chain-node.js'
import { TupleNode } from './tuple-node.js'
import { AddIndexNode } from './add-index-node.js'

/**
* Transforms an operation node tree into another one.
Expand Down Expand Up @@ -208,6 +209,7 @@ export class OperationNodeTransformer {
JSONPathLegNode: this.transformJSONPathLeg.bind(this),
JSONOperatorChainNode: this.transformJSONOperatorChain.bind(this),
TupleNode: this.transformTuple.bind(this),
AddIndexNode: this.transformAddIndex.bind(this),
})

transformNode<T extends OperationNode | undefined>(node: T): T {
Expand Down Expand Up @@ -692,6 +694,8 @@ export class OperationNodeTransformer {
columnAlterations: this.transformNodeList(node.columnAlterations),
addConstraint: this.transformNode(node.addConstraint),
dropConstraint: this.transformNode(node.dropConstraint),
addIndex: this.transformNode(node.addIndex),
dropIndex: this.transformNode(node.dropIndex),
})
}

Expand Down Expand Up @@ -972,6 +976,17 @@ export class OperationNodeTransformer {
})
}

protected transformAddIndex(node: AddIndexNode): AddIndexNode {
return requireAllProps<AddIndexNode>({
kind: 'AddIndexNode',
name: this.transformNode(node.name),
columns: this.transformNodeList(node.columns),
unique: node.unique,
using: this.transformNode(node.using),
ifNotExists: node.ifNotExists,
})
}

protected transformDataType(node: DataTypeNode): DataTypeNode {
// An Object.freezed leaf node. No need to clone.
return node
Expand Down
3 changes: 3 additions & 0 deletions src/operation-node/operation-node-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import { JSONPathNode } from './json-path-node.js'
import { JSONPathLegNode } from './json-path-leg-node.js'
import { JSONOperatorChainNode } from './json-operator-chain-node.js'
import { TupleNode } from './tuple-node.js'
import { AddIndexNode } from './add-index-node.js'

export abstract class OperationNodeVisitor {
protected readonly nodeStack: OperationNode[] = []
Expand Down Expand Up @@ -185,6 +186,7 @@ export abstract class OperationNodeVisitor {
JSONPathLegNode: this.visitJSONPathLeg.bind(this),
JSONOperatorChainNode: this.visitJSONOperatorChain.bind(this),
TupleNode: this.visitTuple.bind(this),
AddIndexNode: this.visitAddIndex.bind(this),
})

protected readonly visitNode = (node: OperationNode): void => {
Expand Down Expand Up @@ -289,4 +291,5 @@ export abstract class OperationNodeVisitor {
protected abstract visitJSONPathLeg(node: JSONPathLegNode): void
protected abstract visitJSONOperatorChain(node: JSONOperatorChainNode): void
protected abstract visitTuple(node: TupleNode): void
protected abstract visitAddIndex(node: AddIndexNode): void
}
1 change: 1 addition & 0 deletions src/operation-node/operation-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export type OperationNodeKind =
| 'JSONPathLegNode'
| 'JSONOperatorChainNode'
| 'TupleNode'
| 'AddIndexNode'

export interface OperationNode {
readonly kind: OperationNodeKind
Expand Down
32 changes: 32 additions & 0 deletions src/query-compiler/default-query-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import { JSONPathNode } from '../operation-node/json-path-node.js'
import { JSONPathLegNode } from '../operation-node/json-path-leg-node.js'
import { JSONOperatorChainNode } from '../operation-node/json-operator-chain-node.js'
import { TupleNode } from '../operation-node/tuple-node.js'
import { AddIndexNode } from '../operation-node/add-index-node.js'

export class DefaultQueryCompiler
extends OperationNodeVisitor
Expand Down Expand Up @@ -1002,6 +1003,14 @@ export class DefaultQueryCompiler
if (node.columnAlterations) {
this.compileColumnAlterations(node.columnAlterations)
}

if (node.addIndex) {
this.visitNode(node.addIndex)
}

if (node.dropIndex) {
this.visitNode(node.dropIndex)
}
}

protected override visitAddColumn(node: AddColumnNode): void {
Expand Down Expand Up @@ -1403,6 +1412,29 @@ export class DefaultQueryCompiler
}
}

protected override visitAddIndex(node: AddIndexNode): void {
this.append('add ')

if (node.unique) {
this.append('unique ')
}

this.append('index ')

this.visitNode(node.name)

if (node.columns) {
this.append(' (')
this.compileList(node.columns)
this.append(')')
}

if (node.using) {
this.append(' using ')
this.visitNode(node.using)
}
}

protected append(str: string): void {
this.#sql += str
}
Expand Down
Loading
Loading