Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | 16x 16x 16x 16x 16x 10x 10x 10x 8x 1x 7x 8x 5x 5x 5x 3x 3x 3x 3x 5x 5x 5x 5x 15x 2x 2x 13x 1x 12x 12x 12x 46x 46x 20x 26x 12x 1x 10x 1x 1x 8x 5x 5x 5x 5x 5x 3x 3x 3x 3x 1x 1x 3x 3x 3x 1x 1x 1x 1x | import { is } from 'drizzle-orm/entity'
import { QueryPromise } from 'drizzle-orm/query-promise'
import { SQL, type SQL as SQLType, sql as yql } from 'drizzle-orm/sql/sql'
import type { Subquery } from 'drizzle-orm/subquery'
import { Table } from 'drizzle-orm/table'
import type { YdbPreparedQueryConfig, YdbSession } from '../session.js'
import type { YdbTable } from '../table.js'
import { type YdbSelectedFieldsOrdered, orderSelectedFields } from '../result-mapping.js'
import { YdbDialect } from '../../ydb/dialect.js'
import {
getTableColumns,
resolveUpdateValue,
validateSetBasedMutationSelection,
validateTableColumnKeys,
} from './utils.js'
import { YdbQueryBuilder } from './query-builder.js'
type UpdateValues = Record<string, unknown>
type UpdateOnQuery =
| SQLType
| {
getSQL(): SQLType
getSelectedFields(): Record<string, unknown> | undefined
}
function getAllReturningFields(table: YdbTable): Record<string, unknown> {
return (table as any)[(Table as any).Symbol.Columns] ?? {}
}
export class YdbUpdateBuilder<TResult = unknown> extends QueryPromise<TResult> {
#valuesData: UpdateValues | undefined
#whereClause: SQLType | undefined
#onQuery: UpdateOnQuery | undefined
#returningFields: YdbSelectedFieldsOrdered | undefined
readonly #table: YdbTable
readonly #session: YdbSession
readonly #dialect: YdbDialect
readonly #withList: Subquery[]
constructor(
table: YdbTable,
session: YdbSession,
dialect = new YdbDialect(),
withList: Subquery[] = []
) {
super()
this.#table = table
this.#session = session
this.#dialect = dialect
this.#withList = withList
}
set(values: UpdateValues): this {
this.#valuesData = values
this.#onQuery = undefined
return this
}
where(where: SQLType | undefined): this {
if (this.#onQuery) {
throw new Error('YDB update().on() does not support where()')
}
this.#whereClause = where ?? undefined
return this
}
on(query: UpdateOnQuery | ((qb: YdbQueryBuilder) => UpdateOnQuery)): this {
let resolved =
typeof query === 'function' ? query(new YdbQueryBuilder(this.#dialect)) : query
Eif (!is(resolved, SQL)) {
validateSetBasedMutationSelection(this.#table, resolved.getSelectedFields(), 'update')
}
this.#onQuery = resolved
this.#valuesData = undefined
this.#whereClause = undefined
return this
}
returning(fields: Record<string, unknown> = getAllReturningFields(this.#table)): this {
let orderedFields = orderSelectedFields(fields)
Iif (orderedFields.length === 0) {
throw new Error('YDB returning() requires at least one field')
}
this.#returningFields = orderedFields
return this
}
getSQL(): SQLType {
if (this.#onQuery) {
let onSql = is(this.#onQuery, SQL) ? this.#onQuery : this.#onQuery.getSQL()
return this.#dialect.buildUpdateQuery({
table: this.#table,
on: onSql,
withList: this.#withList,
returning: this.#returningFields,
})
}
if (!this.#valuesData) {
throw new Error('Update values are missing')
}
validateTableColumnKeys(this.#table, this.#valuesData, 'update')
let columns = getTableColumns(this.#table)
let setEntries = Object.entries(columns).flatMap(([key, column]) => {
let value = resolveUpdateValue(column, this.#valuesData?.[key])
if (value === undefined) {
return []
}
return [yql`${yql.identifier(column.name)} = ${value}`]
})
if (setEntries.length === 0) {
throw new Error('Update values are empty')
}
return this.#dialect.buildUpdateQuery({
table: this.#table,
set: this.#valuesData,
where: this.#whereClause,
withList: this.#withList,
returning: this.#returningFields,
})
}
toSQL() {
let { typings: _typings, ...query } = this.#dialect.sqlToQuery(this.getSQL())
return query
}
prepare(name?: string) {
return this.#session.prepareQuery<YdbPreparedQueryConfig & { execute: TResult }>(
this.getSQL(),
this.#returningFields,
name,
this.#returningFields !== undefined
)
}
override execute(): Promise<TResult> {
return this.prepare().execute() as Promise<TResult>
}
}
export class YdbBatchUpdateBuilder<TResult = unknown> extends QueryPromise<TResult> {
#valuesData: UpdateValues | undefined
#whereClause: SQLType | undefined
readonly #table: YdbTable
readonly #session: YdbSession
readonly #dialect: YdbDialect
constructor(table: YdbTable, session: YdbSession, dialect = new YdbDialect()) {
super()
this.#table = table
this.#session = session
this.#dialect = dialect
}
set(values: UpdateValues): this {
this.#valuesData = values
return this
}
where(where: SQLType | undefined): this {
this.#whereClause = where ?? undefined
return this
}
returning(): never {
throw new Error('YDB batchUpdate().returning() is not supported')
}
on(): never {
throw new Error('YDB batchUpdate().on() is not supported')
}
getSQL(): SQLType {
Iif (!this.#valuesData) {
throw new Error('Update values are missing')
}
validateTableColumnKeys(this.#table, this.#valuesData, 'update')
return this.#dialect.buildUpdateQuery({
table: this.#table,
set: this.#valuesData,
where: this.#whereClause,
batch: true,
})
}
toSQL() {
let { typings: _typings, ...query } = this.#dialect.sqlToQuery(this.getSQL())
return query
}
prepare(name?: string) {
return this.#session.prepareQuery<YdbPreparedQueryConfig & { execute: TResult }>(
this.getSQL(),
undefined,
name,
false
)
}
override execute(): Promise<TResult> {
return this.prepare().execute() as Promise<TResult>
}
}
|