Skip to content

Commit

Permalink
Merge pull request #3561 from drizzle-team/beta
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
AndriiSherman authored Nov 15, 2024
2 parents 2677718 + d1bdce0 commit 9e51fbe
Show file tree
Hide file tree
Showing 25 changed files with 2,152 additions and 246 deletions.
154 changes: 154 additions & 0 deletions changelogs/drizzle-orm/0.36.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# New Features

## Support for `UPDATE ... FROM` in PostgreSQL and SQLite

As the SQLite documentation mentions:

> [!NOTE]
> The UPDATE-FROM idea is an extension to SQL that allows an UPDATE statement to be driven by other tables in the database.
The "target" table is the specific table that is being updated. With UPDATE-FROM you can join the target table
against other tables in the database in order to help compute which rows need updating and what
the new values should be on those rows

Similarly, the PostgreSQL documentation states:

> [!NOTE]
> A table expression allowing columns from other tables to appear in the WHERE condition and update expressions
Drizzle also supports this feature starting from this version

For example, current query:

```ts
await db
.update(users)
.set({ cityId: cities.id })
.from(cities)
.where(and(eq(cities.name, 'Seattle'), eq(users.name, 'John')))
```

Will generate this sql

```sql
update "users" set "city_id" = "cities"."id"
from "cities"
where ("cities"."name" = $1 and "users"."name" = $2)

-- params: [ 'Seattle', 'John' ]
```

You can also alias tables that are joined (in PG, you can also alias the updating table too).

```ts
const c = alias(cities, 'c');
await db
.update(users)
.set({ cityId: c.id })
.from(c);
```

Will generate this sql

```sql
update "users" set "city_id" = "c"."id"
from "cities" "c"
```

In PostgreSQL, you can also return columns from the joined tables.

```ts
const updatedUsers = await db
.update(users)
.set({ cityId: cities.id })
.from(cities)
.returning({ id: users.id, cityName: cities.name });
```

Will generate this sql

```sql
update "users" set "city_id" = "cities"."id"
from "cities"
returning "users"."id", "cities"."name"
```

## Support for `INSERT INTO ... SELECT` in all dialects

As the SQLite documentation mentions:

> [!NOTE]
> The second form of the INSERT statement contains a SELECT statement instead of a VALUES clause.
A new entry is inserted into the table for each row of data returned by executing the SELECT statement.
If a column-list is specified, the number of columns in the result of the SELECT must be the same as
the number of items in the column-list. Otherwise, if no column-list is specified, the number of
columns in the result of the SELECT must be the same as the number of columns in the table.
Any SELECT statement, including compound SELECTs and SELECT statements with ORDER BY and/or LIMIT clauses,
may be used in an INSERT statement of this form.

> [!CAUTION]
> To avoid a parsing ambiguity, the SELECT statement should always contain a WHERE clause, even if that clause is simply "WHERE true", if the upsert-clause is present. Without the WHERE clause, the parser does not know if the token "ON" is part of a join constraint on the SELECT, or the beginning of the upsert-clause.
As the PostgreSQL documentation mentions:
> [!NOTE]
> A query (SELECT statement) that supplies the rows to be inserted
And as the MySQL documentation mentions:

> [!NOTE]
> With INSERT ... SELECT, you can quickly insert many rows into a table from the result of a SELECT statement, which can select from one or many tables
Drizzle supports the current syntax for all dialects, and all of them share the same syntax. Let's review some common scenarios and API usage.
There are several ways to use select inside insert statements, allowing you to choose your preferred approach:

- You can pass a query builder inside the select function.
- You can use a query builder inside a callback.
- You can pass an SQL template tag with any custom select query you want to use

**Query Builder**

```ts
const insertedEmployees = await db
.insert(employees)
.select(
db.select({ name: users.name }).from(users).where(eq(users.role, 'employee'))
)
.returning({
id: employees.id,
name: employees.name
});
```

```ts
const qb = new QueryBuilder();
await db.insert(employees).select(
qb.select({ name: users.name }).from(users).where(eq(users.role, 'employee'))
);
```

**Callback**

```ts
await db.insert(employees).select(
() => db.select({ name: users.name }).from(users).where(eq(users.role, 'employee'))
);
```

```ts
await db.insert(employees).select(
(qb) => qb.select({ name: users.name }).from(users).where(eq(users.role, 'employee'))
);
```

**SQL template tag**

```ts
await db.insert(employees).select(
sql`select "users"."name" as "name" from "users" where "users"."role" = 'employee'`
);
```

```ts
await db.insert(employees).select(
() => sql`select "users"."name" as "name" from "users" where "users"."role" = 'employee'`
);
```
4 changes: 2 additions & 2 deletions drizzle-orm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drizzle-orm",
"version": "0.36.2",
"version": "0.36.3",
"description": "Drizzle ORM package for SQL databases",
"type": "module",
"scripts": {
Expand Down Expand Up @@ -203,4 +203,4 @@
"zod": "^3.20.2",
"zx": "^7.2.2"
}
}
}
23 changes: 13 additions & 10 deletions drizzle-orm/src/aws-data-api/pg/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,25 @@ export class AwsPgDialect extends PgDialect {
}

override buildInsertQuery(
{ table, values, onConflict, returning }: PgInsertConfig<PgTable<TableConfig>>,
{ table, values, onConflict, returning, select, withList }: PgInsertConfig<PgTable<TableConfig>>,
): SQL<unknown> {
const columns: Record<string, PgColumn> = table[Table.Symbol.Columns];
for (const value of values) {
for (const fieldName of Object.keys(columns)) {
const colValue = value[fieldName];
if (
is(colValue, Param) && colValue.value !== undefined && is(colValue.encoder, PgArray)
&& Array.isArray(colValue.value)
) {
value[fieldName] = sql`cast(${colValue} as ${sql.raw(colValue.encoder.getSQLType())})`;

if (!select) {
for (const value of (values as Record<string, Param | SQL>[])) {
for (const fieldName of Object.keys(columns)) {
const colValue = value[fieldName];
if (
is(colValue, Param) && colValue.value !== undefined && is(colValue.encoder, PgArray)
&& Array.isArray(colValue.value)
) {
value[fieldName] = sql`cast(${colValue} as ${sql.raw(colValue.encoder.getSQLType())})`;
}
}
}
}

return super.buildInsertQuery({ table, values, onConflict, returning });
return super.buildInsertQuery({ table, values, onConflict, returning, withList });
}

override buildUpdateSet(table: PgTable<TableConfig>, set: UpdateSet): SQL<unknown> {
Expand Down
80 changes: 49 additions & 31 deletions drizzle-orm/src/mysql-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ import { ViewBaseConfig } from '~/view-common.ts';
import { MySqlColumn } from './columns/common.ts';
import type { MySqlDeleteConfig } from './query-builders/delete.ts';
import type { MySqlInsertConfig } from './query-builders/insert.ts';
import type { MySqlSelectConfig, MySqlSelectJoinConfig, SelectedFieldsOrdered } from './query-builders/select.types.ts';
import type {
AnyMySqlSelectQueryBuilder,
MySqlSelectConfig,
MySqlSelectJoinConfig,
SelectedFieldsOrdered,
} from './query-builders/select.types.ts';
import type { MySqlUpdateConfig } from './query-builders/update.ts';
import type { MySqlSession } from './session.ts';
import { MySqlTable } from './table.ts';
Expand Down Expand Up @@ -439,7 +444,7 @@ export class MySqlDialect {
}

buildInsertQuery(
{ table, values, ignore, onConflict }: MySqlInsertConfig,
{ table, values: valuesOrSelect, ignore, onConflict, select }: MySqlInsertConfig,
): { sql: SQL; generatedIds: Record<string, unknown>[] } {
// const isSingleValue = values.length === 1;
const valuesSqlList: ((SQLChunk | SQL)[] | SQL)[] = [];
Expand All @@ -451,39 +456,52 @@ export class MySqlDialect {
const insertOrder = colEntries.map(([, column]) => sql.identifier(this.casing.getColumnCasing(column)));
const generatedIdsResponse: Record<string, unknown>[] = [];

for (const [valueIndex, value] of values.entries()) {
const generatedIds: Record<string, unknown> = {};

const valueList: (SQLChunk | SQL)[] = [];
for (const [fieldName, col] of colEntries) {
const colValue = value[fieldName];
if (colValue === undefined || (is(colValue, Param) && colValue.value === undefined)) {
// eslint-disable-next-line unicorn/no-negated-condition
if (col.defaultFn !== undefined) {
const defaultFnResult = col.defaultFn();
generatedIds[fieldName] = defaultFnResult;
const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
valueList.push(defaultValue);
if (select) {
const select = valuesOrSelect as AnyMySqlSelectQueryBuilder | SQL;

if (is(select, SQL)) {
valuesSqlList.push(select);
} else {
valuesSqlList.push(select.getSQL());
}
} else {
const values = valuesOrSelect as Record<string, Param | SQL>[];
valuesSqlList.push(sql.raw('values '));

for (const [valueIndex, value] of values.entries()) {
const generatedIds: Record<string, unknown> = {};

const valueList: (SQLChunk | SQL)[] = [];
for (const [fieldName, col] of colEntries) {
const colValue = value[fieldName];
if (colValue === undefined || (is(colValue, Param) && colValue.value === undefined)) {
// eslint-disable-next-line unicorn/no-negated-condition
} else if (!col.default && col.onUpdateFn !== undefined) {
const onUpdateFnResult = col.onUpdateFn();
const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
valueList.push(newValue);
if (col.defaultFn !== undefined) {
const defaultFnResult = col.defaultFn();
generatedIds[fieldName] = defaultFnResult;
const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col);
valueList.push(defaultValue);
// eslint-disable-next-line unicorn/no-negated-condition
} else if (!col.default && col.onUpdateFn !== undefined) {
const onUpdateFnResult = col.onUpdateFn();
const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col);
valueList.push(newValue);
} else {
valueList.push(sql`default`);
}
} else {
valueList.push(sql`default`);
}
} else {
if (col.defaultFn && is(colValue, Param)) {
generatedIds[fieldName] = colValue.value;
if (col.defaultFn && is(colValue, Param)) {
generatedIds[fieldName] = colValue.value;
}
valueList.push(colValue);
}
valueList.push(colValue);
}
}

generatedIdsResponse.push(generatedIds);
valuesSqlList.push(valueList);
if (valueIndex < values.length - 1) {
valuesSqlList.push(sql`, `);
generatedIdsResponse.push(generatedIds);
valuesSqlList.push(valueList);
if (valueIndex < values.length - 1) {
valuesSqlList.push(sql`, `);
}
}
}

Expand All @@ -494,7 +512,7 @@ export class MySqlDialect {
const onConflictSql = onConflict ? sql` on duplicate key ${onConflict}` : undefined;

return {
sql: sql`insert${ignoreSql} into ${table} ${insertOrder} values ${valuesSql}${onConflictSql}`,
sql: sql`insert${ignoreSql} into ${table} ${insertOrder} ${valuesSql}${onConflictSql}`,
generatedIds: generatedIdsResponse,
};
}
Expand Down
Loading

0 comments on commit 9e51fbe

Please sign in to comment.