From d92984f7440ea96fe9a6f91d7acacaee6537f299 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki <7413593+a8m@users.noreply.github.com> Date: Wed, 6 Apr 2022 14:10:33 +0300 Subject: [PATCH] doc/website: add sql/execquery feature-flag to document (#2453) --- doc/md/features.md | 31 ++++++++++++++++++++++++++++ entc/integration/integration_test.go | 1 + 2 files changed, 32 insertions(+) diff --git a/doc/md/features.md b/doc/md/features.md index 8afd26eee..1d1630738 100644 --- a/doc/md/features.md +++ b/doc/md/features.md @@ -245,6 +245,37 @@ ORDER BY `groups`.`id` ASC ``` +#### SQL Raw API + +The `sql/execquery` option allows executing statements using the `ExecContext`/`QueryContext` methods of the underlying +driver. For full documentation, see: [DB.ExecContext](https://pkg.go.dev/database/sql#DB.ExecContext), and +[DB.QueryContext](https://pkg.go.dev/database/sql#DB.QueryContext). + +```go +// From ent.Client. +if _, err := client.ExecContext(ctx, "TRUNCATE t1"); err != nil { + return err +} + +// From ent.Tx. +tx, err := client.Tx(ctx) +if err != nil { + return err +} +if err := tx.User.Create().Exec(ctx); err != nil { + return err +} +if _, err := tx.ExecContext("SAVEPOINT user_created"); err != nil { + return err +} +// ... +``` + +:::warning Note +Statements executed using `ExecContext`/`QueryContext` do not go through Ent, and may skip fundamental layers in your +application such as hooks, privacy (authorization), and validators. +::: + #### Upsert The `sql/upsert` option lets configure upsert and bulk-upsert logic using the SQL `ON CONFLICT` / `ON DUPLICATE KEY` diff --git a/entc/integration/integration_test.go b/entc/integration/integration_test.go index d080099c2..5c74317b3 100644 --- a/entc/integration/integration_test.go +++ b/entc/integration/integration_test.go @@ -697,6 +697,7 @@ func ExecQuery(t *testing.T, client *ent.Client) { require.NoError(err) count, err := sql.ScanInt(rows) require.NoError(err) + require.NoError(rows.Close()) require.Equal(1, count) require.NoError(tx.Commit()) }