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

Accept non-column first argument in between and notBetween conditions #3829

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
118 changes: 74 additions & 44 deletions drizzle-orm/src/sql/expressions/conditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,27 +442,44 @@ export function notExists(subquery: SQLWrapper): SQL {
* @see notBetween for the inverse of this test
*/
export function between<T>(
column: SQL.Aliased,
min: T | SQLWrapper,
max: T | SQLWrapper,
): SQL;
export function between<TColumn extends AnyColumn>(
column: TColumn,
min: GetColumnData<TColumn, 'raw'> | SQLWrapper,
max: GetColumnData<TColumn, 'raw'> | SQLWrapper,
): SQL;
export function between<T extends SQLWrapper>(
column: Exclude<T, SQL.Aliased | Column>,
expression: AnyColumn<{ data: T }>,
min: T | SQL<T> | SQL.Aliased<T> | AnyColumn<{ data: T }>,
max: T | SQL<T> | SQL.Aliased<T> | AnyColumn<{ data: T }>
): SQL<T>
export function between<T>(
expression: T | SQL<T> | SQL.Aliased<T> | AnyColumn<{ data: T }>,
min: AnyColumn<{ data: T }>,
max: T | SQL<T> | SQL.Aliased<T> | AnyColumn<{ data: T }>
): SQL<T>
export function between<T>(
expression: T | SQL<T> | SQL.Aliased<T> | AnyColumn<{ data: T }>,
min: T | SQL<T> | SQL.Aliased<T> | AnyColumn<{ data: T }>,
max: AnyColumn<{ data: T }>
): SQL<T>
export function between<T>(
expression: unknown,
min: unknown,
max: unknown,
): SQL;
export function between(column: SQLWrapper, min: unknown, max: unknown): SQL {
return sql`${column} between ${bindIfParam(min, column)} and ${
bindIfParam(
max,
column,
)
}`;
max: unknown
): SQL<T> {
switch (true) {
case is(expression, Column): {
min = bindIfParam(min, expression)
max = bindIfParam(max, expression)
break
}
case is(min, Column): {
expression = bindIfParam(expression, min)
max = bindIfParam(max, min)
break
}
case is(max, Column): {
expression = bindIfParam(expression, max)
min = bindIfParam(min, max)
break
}
}

return sql`${expression} between ${min} and ${max}`
}

/**
Expand All @@ -483,31 +500,44 @@ export function between(column: SQLWrapper, min: unknown, max: unknown): SQL {
* @see between for the inverse of this test
*/
export function notBetween<T>(
column: SQL.Aliased,
min: T | SQLWrapper,
max: T | SQLWrapper,
): SQL;
export function notBetween<TColumn extends AnyColumn>(
column: TColumn,
min: GetColumnData<TColumn, 'raw'> | SQLWrapper,
max: GetColumnData<TColumn, 'raw'> | SQLWrapper,
): SQL;
export function notBetween<T extends SQLWrapper>(
column: Exclude<T, SQL.Aliased | Column>,
min: unknown,
max: unknown,
): SQL;
export function notBetween(
column: SQLWrapper,
expression: AnyColumn<{ data: T }>,
min: T | SQL<T> | SQL.Aliased<T> | AnyColumn<{ data: T }>,
max: T | SQL<T> | SQL.Aliased<T> | AnyColumn<{ data: T }>
): SQL<T>
export function notBetween<T>(
expression: T | SQL<T> | SQL.Aliased<T> | AnyColumn<{ data: T }>,
min: AnyColumn<{ data: T }>,
max: T | SQL<T> | SQL.Aliased<T> | AnyColumn<{ data: T }>
): SQL<T>
export function notBetween<T>(
expression: T | SQL<T> | SQL.Aliased<T> | AnyColumn<{ data: T }>,
min: T | SQL<T> | SQL.Aliased<T> | AnyColumn<{ data: T }>,
max: AnyColumn<{ data: T }>
): SQL<T>
export function notBetween<T>(
expression: unknown,
min: unknown,
max: unknown,
): SQL {
return sql`${column} not between ${
bindIfParam(
min,
column,
)
} and ${bindIfParam(max, column)}`;
max: unknown
): SQL<T> {
switch (true) {
case is(expression, Column): {
min = bindIfParam(min, expression)
max = bindIfParam(max, expression)
break
}
case is(min, Column): {
expression = bindIfParam(expression, min)
max = bindIfParam(max, min)
break
}
case is(max, Column): {
expression = bindIfParam(expression, max)
min = bindIfParam(min, max)
break
}
}

return sql`${expression} not between ${min} and ${max}`
}

/**
Expand Down
8 changes: 6 additions & 2 deletions drizzle-orm/type-tests/mysql/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,12 @@ const allOperators = await db
exists(sql`select id from ${users}`),
notExists(db.select({ id: users.id }).from(users)),
notExists(sql`select id from ${users}`),
between(users.id, 1, 2),
notBetween(users.id, 1, 2),
between(users.id, 1, sql`2`),
between(1, users.id, sql`2`),
between(sql`2`, 1, users.id),
notBetween(users.id, 1, sql`2`),
notBetween(1, users.id, sql`2`),
notBetween(sql`2`, 1, users.id),
like(users.id, '%1%'),
notLike(users.id, '%1%'),
ilike(users.id, '%1%'),
Expand Down
8 changes: 6 additions & 2 deletions drizzle-orm/type-tests/pg/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,12 @@ const allOperators = await db
exists(sql`select id from ${users}`),
notExists(db.select({ id: users.id }).from(users)),
notExists(sql`select id from ${users}`),
between(users.id, 1, 2),
notBetween(users.id, 1, 2),
between(users.id, 1, sql`2`),
between(1, users.id, sql`2`),
between(sql`2`, 1, users.id),
notBetween(users.id, 1, sql`2`),
notBetween(1, users.id, sql`2`),
notBetween(sql`2`, 1, users.id),
like(users.id, '%1%'),
notLike(users.id, '%1%'),
ilike(users.id, '%1%'),
Expand Down
8 changes: 6 additions & 2 deletions drizzle-orm/type-tests/singlestore/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,12 @@ const allOperators = await db
exists(sql`select id from ${users}`),
notExists(db.select({ id: users.id }).from(users)),
notExists(sql`select id from ${users}`),
between(users.id, 1, 2),
notBetween(users.id, 1, 2),
between(users.id, 1, sql`2`),
between(1, users.id, sql`2`),
between(sql`2`, 1, users.id),
notBetween(users.id, 1, sql`2`),
notBetween(1, users.id, sql`2`),
notBetween(sql`2`, 1, users.id),
like(users.id, '%1%'),
notLike(users.id, '%1%'),
ilike(users.id, '%1%'),
Expand Down
8 changes: 6 additions & 2 deletions drizzle-orm/type-tests/sqlite/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,12 @@ const allOperators = db
exists(sql`select id from ${users}`),
notExists(db.select({ id: users.id }).from(users)),
notExists(sql`select id from ${users}`),
between(users.id, 1, 2),
notBetween(users.id, 1, 2),
between(users.id, 1, sql`2`),
between(1, users.id, sql`2`),
between(sql`2`, 1, users.id),
notBetween(users.id, 1, sql`2`),
notBetween(1, users.id, sql`2`),
notBetween(sql`2`, 1, users.id),
like(users.id, '%1%'),
notLike(users.id, '%1%'),
ilike(users.id, '%1%'),
Expand Down