import { CheckConstraintNode } from '../operation-node/check-constraint-node.js' ; import { OperationNodeSource } from '../operation-node/operation-node-source.js' ; import { ReferenceNode } from '../operation-node/reference-node.js' ; import { OnModifyForeignAction , ReferencesNode , } from '../operation-node/references-node.js' ; import { SelectAllNode } from '../operation-node/select-all-node.js' ; import { parseStringReference } from '../parser/reference-parser.js' ; import { preventAwait } from '../util/prevent-await.js' ; import { ColumnDefinitionNode } from '../operation-node/column-definition-node.js' ; import { AnyRawBuilder } from '../util/type-utils.js' ; import { DefaultValueExpression , parseDefaultValueExpression , } from '../parser/default-value-parser.js' ; import { GeneratedNode } from '../operation-node/generated-node.js' ; import { DefaultValueNode } from '../operation-node/default-value-node.js' ; export interface ColumnDefinitionBuilderInterface { /**
     * Adds `auto_increment` or `autoincrement` to the column definition
     * depending on the dialect.
     *
     * Some dialects like PostgreSQL don't support this. On PostgreSQL
     * you can use the `serial` or `bigserial` data type instead.
     */ autoIncrement ( ) : ColumnDefinitionBuilderInterface ; /**
     * Makes the column the primary key.
     *
     * If you want to specify a composite primary key use the
     *  TableBuilder . addPrimaryKeyConstraint method.
      primaryKey ( ) : ColumnDefinitionBuilderInterface ; /**
     * Adds a foreign key constraint for the column.
     *
     * If your database engine doesn't support foreign key constraints in the
     * column definition (like MySQL 5) you need to call the table level
     *  TableBuilder . addForeignKeyConstraint method instead.
     *
     * ### Examples
     *
     * ```ts
     * col.references('person.id')
     * ```
      references ( ref ) : ColumnDefinitionBuilderInterface ; /**
     * Adds an `on delete` constraint for the foreign key column.
     *
     * If your database engine doesn't support foreign key constraints in the
     * column definition (like MySQL 5) you need to call the table level
     *  TableBuilder . addForeignKeyConstraint method instead.
     *
     * ### Examples
     *
     * ```ts
     * col.references('person.id').onDelete('cascade')
     * ```
      onDelete ( onDelete ) : ColumnDefinitionBuilderInterface ; /**
     * Adds an `on update` constraint for the foreign key column.
     *
     * ### Examples
     *
     * ```ts
     * col.references('person.id').onUpdate('cascade')
     * ```
     */ onUpdate ( onUpdate ) : ColumnDefinitionBuilderInterface ; /**
     * Adds a unique constraint for the column.
     */ unique ( ) : ColumnDefinitionBuilderInterface ; /**
     * Adds a `not null` constraint for the column.
     */ notNull ( ) : ColumnDefinitionBuilderInterface ; /**
     * Adds a `unsigned` modifier for the column.
     *
     * This only works on some dialects like MySQL.
     */ unsigned ( ) : ColumnDefinitionBuilderInterface ; /**
     * Adds a default value constraint for the column.
     *
     * ### Examples
     *
     * ```ts
     * db.schema
     *   .createTable('pet')
     *   .addColumn('number_of_legs', 'integer', (col) => col.defaultTo(4))
     *   .execute()
     * ```
     *
     * Raw expressions are also supported:
     *
     * ```ts
     * db.schema
     *   .createTable('pet')
     *   .addColumn(
     *     'number_of_legs',
     *     'integer',
     *     (col) => col.defaultTo(db.raw('any SQL here'))
     *   )
     *   .execute()
     * ```
     */ defaultTo ( value ) : ColumnDefinitionBuilderInterface ; /**
     * Adds a check constraint for the column.
     *
     * ### Examples
     *
     * ```ts
     * db.schema
     *   .createTable('pet')
     *   .addColumn('number_of_legs', 'integer', (col) =>
     *     col.check(db.raw('number_of_legs < 5'))
     *   )
     *   .execute()
     * ```
     */ check ( expression ) : ColumnDefinitionBuilderInterface ; /**
     * Makes the column a generated column using a `generated always as` statement.
     *
     * ### Examples
     *
     * ```ts
     * db.schema
     *   .createTable('person')
     *   .addColumn('full_name', 'varchar(255)',
     *     (col) => col.generatedAlwaysAs(db.raw("concat(first_name, ' ', last_name)"))
     *   )
     *   .execute()
     * ```
     */ generatedAlwaysAs ( expression ) : ColumnDefinitionBuilderInterface ; /**
     * Adds the `generated always as identity` specifier on supported dialects.
     */ generatedAlwaysAsIdentity ( ) : ColumnDefinitionBuilderInterface ; /**
     * Adds the `generated by default as identity` specifier on supported dialects.
     */ generatedByDefaultAsIdentity ( ) : ColumnDefinitionBuilderInterface ; /**
     * Makes a generated column stored instead of virtual. This method can only
     * be used with  generatedAlwaysAs *
     * ### Examples
     *
     * ```ts
     * db.schema
     *   .createTable('person')
     *   .addColumn('full_name', 'varchar(255)', (col) => col
     *     .generatedAlwaysAs("concat(first_name, ' ', last_name)")
     *     .stored()
     *   )
     *   .execute()
     * ```
      stored ( ) : ColumnDefinitionBuilderInterface ; } export class ColumnDefinitionBuilder implements ColumnDefinitionBuilderInterface , OperationNodeSource { readonly #node : ColumnDefinitionNode ; constructor ( node ) { this . #node = node ; } autoIncrement ( ) { return new ColumnDefinitionBuilder ( ColumnDefinitionNode . cloneWith ( this . #node , { autoIncrement : true } ) ) ; } primaryKey ( ) { return new ColumnDefinitionBuilder ( ColumnDefinitionNode . cloneWith ( this . #node , { primaryKey : true } ) ) ; } references ( ref ) { const references = parseStringReference ( ref ) ; if ( ! ReferenceNode . is ( references ) || SelectAllNode . is ( references . column ) ) { throw new Error ( `invalid call references('${ ref }'). The reference must have format table.column or schema.table.column` ) ; } return new ColumnDefinitionBuilder ( ColumnDefinitionNode . cloneWith ( this . #node , { references : ReferencesNode . create ( references . table , [ references . column , ] ) , } ) ) ; } onDelete ( onDelete ) { if ( ! this . #node . references ) { throw new Error ( 'on delete constraint can only be added for foreign keys' ) ; } return new ColumnDefinitionBuilder ( ColumnDefinitionNode . cloneWith ( this . #node , { references : ReferencesNode . cloneWithOnDelete ( this . #node . references , onDelete ) , } ) ) ; } onUpdate ( onUpdate ) { if ( ! this . #node . references ) { throw new Error ( 'on update constraint can only be added for foreign keys' ) ; } return new ColumnDefinitionBuilder ( ColumnDefinitionNode . cloneWith ( this . #node , { references : ReferencesNode . cloneWithOnUpdate ( this . #node . references , onUpdate ) , } ) ) ; } unique ( ) { return new ColumnDefinitionBuilder ( ColumnDefinitionNode . cloneWith ( this . #node , { unique : true } ) ) ; } notNull ( ) { return new ColumnDefinitionBuilder ( ColumnDefinitionNode . cloneWith ( this . #node , { notNull : true } ) ) ; } unsigned ( ) { return new ColumnDefinitionBuilder ( ColumnDefinitionNode . cloneWith ( this . #node , { unsigned : true } ) ) ; } defaultTo ( value ) { return new ColumnDefinitionBuilder ( ColumnDefinitionNode . cloneWith ( this . #node , { defaultTo : DefaultValueNode . create ( parseDefaultValueExpression ( value ) ) , } ) ) ; } check ( expression ) { return new ColumnDefinitionBuilder ( ColumnDefinitionNode . cloneWith ( this . #node , { check : CheckConstraintNode . create ( expression . toOperationNode ( ) ) , } ) ) ; } generatedAlwaysAs ( expression ) { return new ColumnDefinitionBuilder ( ColumnDefinitionNode . cloneWith ( this . #node , { generated : GeneratedNode . createWithExpression ( expression . toOperationNode ( ) ) , } ) ) ; } generatedAlwaysAsIdentity ( ) { return new ColumnDefinitionBuilder ( ColumnDefinitionNode . cloneWith ( this . #node , { generated : GeneratedNode . create ( { identity : true , always : true } ) , } ) ) ; } generatedByDefaultAsIdentity ( ) { return new ColumnDefinitionBuilder ( ColumnDefinitionNode . cloneWith ( this . #node , { generated : GeneratedNode . create ( { identity : true , byDefault : true } ) , } ) ) ; } stored ( ) { if ( ! this . #node . generated ) { throw new Error ( 'stored() can only be called after generatedAlwaysAs' ) ; } return new ColumnDefinitionBuilder ( ColumnDefinitionNode . cloneWith ( this . #node , { generated : GeneratedNode . cloneWith ( this . #node . generated , { stored : true , } ) , } ) ) ; } toOperationNode ( ) { return this . #node ; } } preventAwait ( ColumnDefinitionBuilder , "don't await ColumnDefinitionBuilder instances directly." ) ;