2022-02-11 10:02:47 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2023-08-22 12:49:02 +00:00
|
|
|
"context"
|
2022-02-11 10:02:47 +00:00
|
|
|
"database/sql"
|
2023-12-05 17:01:03 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-07-28 14:25:42 +00:00
|
|
|
"reflect"
|
2024-01-02 15:27:36 +00:00
|
|
|
"strings"
|
2022-03-28 08:05:09 +00:00
|
|
|
|
2024-10-04 13:15:41 +00:00
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
2023-10-19 10:19:10 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2023-08-22 12:49:02 +00:00
|
|
|
"github.com/zitadel/logging"
|
|
|
|
|
2022-07-28 14:25:42 +00:00
|
|
|
_ "github.com/zitadel/zitadel/internal/database/cockroach"
|
|
|
|
"github.com/zitadel/zitadel/internal/database/dialect"
|
2022-08-31 07:52:43 +00:00
|
|
|
_ "github.com/zitadel/zitadel/internal/database/postgres"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-02-11 10:02:47 +00:00
|
|
|
)
|
|
|
|
|
2024-12-04 13:51:40 +00:00
|
|
|
type ContextQuerier interface {
|
2024-11-22 16:25:28 +00:00
|
|
|
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
|
2024-12-04 13:51:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ContextExecuter interface {
|
2024-11-22 16:25:28 +00:00
|
|
|
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
|
|
|
|
}
|
|
|
|
|
2024-12-04 13:51:40 +00:00
|
|
|
type ContextQueryExecuter interface {
|
|
|
|
ContextQuerier
|
|
|
|
ContextExecuter
|
|
|
|
}
|
|
|
|
|
2024-11-22 16:25:28 +00:00
|
|
|
type Client interface {
|
2024-12-04 13:51:40 +00:00
|
|
|
ContextQueryExecuter
|
|
|
|
Beginner
|
|
|
|
Conn(ctx context.Context) (*sql.Conn, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Beginner interface {
|
2024-11-22 16:25:28 +00:00
|
|
|
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Tx interface {
|
2024-12-04 13:51:40 +00:00
|
|
|
ContextQueryExecuter
|
2024-11-22 16:25:28 +00:00
|
|
|
Commit() error
|
|
|
|
Rollback() error
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ Client = (*sql.DB)(nil)
|
|
|
|
_ Tx = (*sql.Tx)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
func CloseTransaction(tx Tx, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
rollbackErr := tx.Rollback()
|
|
|
|
logging.OnError(rollbackErr).Error("failed to rollback transaction")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
commitErr := tx.Commit()
|
|
|
|
logging.OnError(commitErr).Error("failed to commit transaction")
|
|
|
|
return commitErr
|
|
|
|
}
|
|
|
|
|
2022-07-28 14:25:42 +00:00
|
|
|
type Config struct {
|
2023-12-20 16:13:04 +00:00
|
|
|
Dialects map[string]interface{} `mapstructure:",remain"`
|
|
|
|
EventPushConnRatio float64
|
|
|
|
ProjectionSpoolerConnRatio float64
|
|
|
|
connector dialect.Connector
|
2022-07-28 14:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) SetConnector(connector dialect.Connector) {
|
|
|
|
c.connector = connector
|
|
|
|
}
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
type DB struct {
|
|
|
|
*sql.DB
|
|
|
|
dialect.Database
|
2024-10-04 13:15:41 +00:00
|
|
|
Pool *pgxpool.Pool
|
2023-02-27 21:36:43 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 12:49:02 +00:00
|
|
|
func (db *DB) Query(scan func(*sql.Rows) error, query string, args ...any) error {
|
|
|
|
return db.QueryContext(context.Background(), scan, query, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) QueryContext(ctx context.Context, scan func(rows *sql.Rows) error, query string, args ...any) (err error) {
|
perf(query): remove transactions for queries (#8614)
# Which Problems Are Solved
Queries currently execute 3 statements, begin, query, commit
# How the Problems Are Solved
remove transaction handling from query methods in database package
# Additional Changes
- Bump versions of `core_grpc_dependencies`-receipt in Makefile
# Additional info
During load tests we saw a lot of idle transactions of `zitadel_queries`
application name which is the connection pool used to query data in
zitadel. Executed query:
`select query_start - xact_start, pid, application_name, backend_start,
xact_start, query_start, state_change, wait_event_type,
wait_event,substring(query, 1, 200) query from pg_stat_activity where
datname = 'zitadel' and state <> 'idle';`
Mostly the last query executed was `begin isolation level read committed
read only`.
example:
```
?column? | pid | application_name | backend_start | xact_start | query_start | state_change | wait_event_type | wait_event | query
-----------------+-------+----------------------------+-------------------------------+-------------------------------+-------------------------------+-------------------------------+-----------------+--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00:00:00 | 33030 | zitadel_queries | 2024-10-16 16:25:53.906036+00 | 2024-10-16 16:30:19.191661+00 | 2024-10-16 16:30:19.191661+00 | 2024-10-16 16:30:19.19169+00 | Client | ClientRead | begin isolation level read committed read only
00:00:00 | 33035 | zitadel_queries | 2024-10-16 16:25:53.909629+00 | 2024-10-16 16:30:19.19179+00 | 2024-10-16 16:30:19.19179+00 | 2024-10-16 16:30:19.191805+00 | Client | ClientRead | begin isolation level read committed read only
00:00:00.00412 | 33028 | zitadel_queries | 2024-10-16 16:25:53.904247+00 | 2024-10-16 16:30:19.187734+00 | 2024-10-16 16:30:19.191854+00 | 2024-10-16 16:30:19.191964+00 | Client | ClientRead | SELECT created_at, event_type, "sequence", "position", payload, creator, "owner", instance_id, aggregate_type, aggregate_id, revision FROM eventstore.events2 WHERE instance_id = $1 AND aggregate_type
00:00:00.084662 | 33134 | zitadel_es_pusher | 2024-10-16 16:29:54.979692+00 | 2024-10-16 16:30:19.178578+00 | 2024-10-16 16:30:19.26324+00 | 2024-10-16 16:30:19.263267+00 | Client | ClientRead | RELEASE SAVEPOINT cockroach_restart
00:00:00.084768 | 33139 | zitadel_es_pusher | 2024-10-16 16:29:54.979585+00 | 2024-10-16 16:30:19.180762+00 | 2024-10-16 16:30:19.26553+00 | 2024-10-16 16:30:19.265531+00 | LWLock | WALWriteLock | commit
00:00:00.077377 | 33136 | zitadel_es_pusher | 2024-10-16 16:29:54.978582+00 | 2024-10-16 16:30:19.187883+00 | 2024-10-16 16:30:19.26526+00 | 2024-10-16 16:30:19.265431+00 | Client | ClientRead | WITH existing AS ( +
| | | | | | | | | (SELECT instance_id, aggregate_type, aggregate_id, "sequence" FROM eventstore.events2 WHERE instance_id = $1 AND aggregate_type = $2 AND aggregate_id = $3 ORDER BY "sequence" DE
00:00:00.012309 | 33123 | zitadel_es_pusher | 2024-10-16 16:29:54.963484+00 | 2024-10-16 16:30:19.175066+00 | 2024-10-16 16:30:19.187375+00 | 2024-10-16 16:30:19.187376+00 | IO | WalSync | commit
00:00:00 | 33034 | zitadel_queries | 2024-10-16 16:25:53.90791+00 | 2024-10-16 16:30:19.262921+00 | 2024-10-16 16:30:19.262921+00 | 2024-10-16 16:30:19.263133+00 | Client | ClientRead | begin isolation level read committed read only
00:00:00 | 33039 | zitadel_queries | 2024-10-16 16:25:53.914106+00 | 2024-10-16 16:30:19.191676+00 | 2024-10-16 16:30:19.191676+00 | 2024-10-16 16:30:19.191687+00 | Client | ClientRead | begin isolation level read committed read only
00:00:00.24539 | 33083 | zitadel_projection_spooler | 2024-10-16 16:27:49.895548+00 | 2024-10-16 16:30:19.020058+00 | 2024-10-16 16:30:19.265448+00 | 2024-10-16 16:30:19.26546+00 | Client | ClientRead | SAVEPOINT exec_stmt
00:00:00 | 33125 | zitadel_es_pusher | 2024-10-16 16:29:54.963859+00 | 2024-10-16 16:30:19.191715+00 | 2024-10-16 16:30:19.191715+00 | 2024-10-16 16:30:19.191729+00 | Client | ClientRead | begin
00:00:00.004292 | 33032 | zitadel_queries | 2024-10-16 16:25:53.906624+00 | 2024-10-16 16:30:19.187713+00 | 2024-10-16 16:30:19.192005+00 | 2024-10-16 16:30:19.192062+00 | Client | ClientRead | SELECT created_at, event_type, "sequence", "position", payload, creator, "owner", instance_id, aggregate_type, aggregate_id, revision FROM eventstore.events2 WHERE instance_id = $1 AND aggregate_type
00:00:00 | 33031 | zitadel_queries | 2024-10-16 16:25:53.906422+00 | 2024-10-16 16:30:19.191625+00 | 2024-10-16 16:30:19.191625+00 | 2024-10-16 16:30:19.191645+00 | Client | ClientRead | begin isolation level read committed read only
```
The amount of idle transactions is significantly less if the query
transactions are removed:
example:
```
?column? | pid | application_name | backend_start | xact_start | query_start | state_change | wait_event_type | wait_event | query
-----------------+-------+----------------------------+-------------------------------+-------------------------------+-------------------------------+-------------------------------+-----------------+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00:00:00.000094 | 32741 | zitadel_queries | 2024-10-16 16:23:49.73935+00 | 2024-10-16 16:24:59.785589+00 | 2024-10-16 16:24:59.785683+00 | 2024-10-16 16:24:59.785684+00 | | | SELECT created_at, event_type, "sequence", "position", payload, creator, "owner", instance_id, aggregate_type, aggregate_id, revision FROM eventstore.events2 WHERE instance_id = $1 AND aggregate_type
00:00:00 | 32762 | zitadel_es_pusher | 2024-10-16 16:24:02.275136+00 | 2024-10-16 16:24:59.784586+00 | 2024-10-16 16:24:59.784586+00 | 2024-10-16 16:24:59.784607+00 | Client | ClientRead | begin
00:00:00.000167 | 32742 | zitadel_queries | 2024-10-16 16:23:49.740489+00 | 2024-10-16 16:24:59.784274+00 | 2024-10-16 16:24:59.784441+00 | 2024-10-16 16:24:59.784442+00 | | | with usr as ( +
| | | | | | | | | select u.id, u.creation_date, u.change_date, u.sequence, u.state, u.resource_owner, u.username, n.login_name as preferred_login_name +
| | | | | | | | | from projections.users13 u +
| | | | | | | | | left join projections.l
00:00:00.256014 | 32759 | zitadel_projection_spooler | 2024-10-16 16:24:01.418429+00 | 2024-10-16 16:24:59.52959+00 | 2024-10-16 16:24:59.785604+00 | 2024-10-16 16:24:59.785649+00 | Client | ClientRead | UPDATE projections.milestones SET reached_date = $1 WHERE (instance_id = $2) AND (type = $3) AND (reached_date IS NULL)
00:00:00.014199 | 32773 | zitadel_es_pusher | 2024-10-16 16:24:02.320404+00 | 2024-10-16 16:24:59.769509+00 | 2024-10-16 16:24:59.783708+00 | 2024-10-16 16:24:59.783709+00 | IO | WalSync | commit
00:00:00 | 32765 | zitadel_es_pusher | 2024-10-16 16:24:02.28173+00 | 2024-10-16 16:24:59.780413+00 | 2024-10-16 16:24:59.780413+00 | 2024-10-16 16:24:59.780426+00 | Client | ClientRead | begin
00:00:00.012729 | 32777 | zitadel_es_pusher | 2024-10-16 16:24:02.339737+00 | 2024-10-16 16:24:59.767432+00 | 2024-10-16 16:24:59.780161+00 | 2024-10-16 16:24:59.780195+00 | Client | ClientRead | RELEASE SAVEPOINT cockroach_restart
```
---------
Co-authored-by: Tim Möhlmann <tim+github@zitadel.com>
Co-authored-by: Livio Spring <livio.a@gmail.com>
Co-authored-by: Max Peintner <max@caos.ch>
Co-authored-by: Elio Bischof <elio@zitadel.com>
Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
Co-authored-by: Miguel Cabrerizo <30386061+doncicuto@users.noreply.github.com>
Co-authored-by: Joakim Lodén <Loddan@users.noreply.github.com>
Co-authored-by: Yxnt <Yxnt@users.noreply.github.com>
Co-authored-by: Stefan Benz <stefan@caos.ch>
Co-authored-by: Harsha Reddy <harsha.reddy@klaviyo.com>
Co-authored-by: Zach H <zhirschtritt@gmail.com>
2024-11-04 09:06:14 +00:00
|
|
|
rows, err := db.DB.QueryContext(ctx, query, args...)
|
2023-08-22 12:49:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
closeErr := rows.Close()
|
|
|
|
logging.OnError(closeErr).Info("rows.Close failed")
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err = scan(rows); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return rows.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) QueryRow(scan func(*sql.Row) error, query string, args ...any) (err error) {
|
|
|
|
return db.QueryRowContext(context.Background(), scan, query, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) QueryRowContext(ctx context.Context, scan func(row *sql.Row) error, query string, args ...any) (err error) {
|
perf(query): remove transactions for queries (#8614)
# Which Problems Are Solved
Queries currently execute 3 statements, begin, query, commit
# How the Problems Are Solved
remove transaction handling from query methods in database package
# Additional Changes
- Bump versions of `core_grpc_dependencies`-receipt in Makefile
# Additional info
During load tests we saw a lot of idle transactions of `zitadel_queries`
application name which is the connection pool used to query data in
zitadel. Executed query:
`select query_start - xact_start, pid, application_name, backend_start,
xact_start, query_start, state_change, wait_event_type,
wait_event,substring(query, 1, 200) query from pg_stat_activity where
datname = 'zitadel' and state <> 'idle';`
Mostly the last query executed was `begin isolation level read committed
read only`.
example:
```
?column? | pid | application_name | backend_start | xact_start | query_start | state_change | wait_event_type | wait_event | query
-----------------+-------+----------------------------+-------------------------------+-------------------------------+-------------------------------+-------------------------------+-----------------+--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00:00:00 | 33030 | zitadel_queries | 2024-10-16 16:25:53.906036+00 | 2024-10-16 16:30:19.191661+00 | 2024-10-16 16:30:19.191661+00 | 2024-10-16 16:30:19.19169+00 | Client | ClientRead | begin isolation level read committed read only
00:00:00 | 33035 | zitadel_queries | 2024-10-16 16:25:53.909629+00 | 2024-10-16 16:30:19.19179+00 | 2024-10-16 16:30:19.19179+00 | 2024-10-16 16:30:19.191805+00 | Client | ClientRead | begin isolation level read committed read only
00:00:00.00412 | 33028 | zitadel_queries | 2024-10-16 16:25:53.904247+00 | 2024-10-16 16:30:19.187734+00 | 2024-10-16 16:30:19.191854+00 | 2024-10-16 16:30:19.191964+00 | Client | ClientRead | SELECT created_at, event_type, "sequence", "position", payload, creator, "owner", instance_id, aggregate_type, aggregate_id, revision FROM eventstore.events2 WHERE instance_id = $1 AND aggregate_type
00:00:00.084662 | 33134 | zitadel_es_pusher | 2024-10-16 16:29:54.979692+00 | 2024-10-16 16:30:19.178578+00 | 2024-10-16 16:30:19.26324+00 | 2024-10-16 16:30:19.263267+00 | Client | ClientRead | RELEASE SAVEPOINT cockroach_restart
00:00:00.084768 | 33139 | zitadel_es_pusher | 2024-10-16 16:29:54.979585+00 | 2024-10-16 16:30:19.180762+00 | 2024-10-16 16:30:19.26553+00 | 2024-10-16 16:30:19.265531+00 | LWLock | WALWriteLock | commit
00:00:00.077377 | 33136 | zitadel_es_pusher | 2024-10-16 16:29:54.978582+00 | 2024-10-16 16:30:19.187883+00 | 2024-10-16 16:30:19.26526+00 | 2024-10-16 16:30:19.265431+00 | Client | ClientRead | WITH existing AS ( +
| | | | | | | | | (SELECT instance_id, aggregate_type, aggregate_id, "sequence" FROM eventstore.events2 WHERE instance_id = $1 AND aggregate_type = $2 AND aggregate_id = $3 ORDER BY "sequence" DE
00:00:00.012309 | 33123 | zitadel_es_pusher | 2024-10-16 16:29:54.963484+00 | 2024-10-16 16:30:19.175066+00 | 2024-10-16 16:30:19.187375+00 | 2024-10-16 16:30:19.187376+00 | IO | WalSync | commit
00:00:00 | 33034 | zitadel_queries | 2024-10-16 16:25:53.90791+00 | 2024-10-16 16:30:19.262921+00 | 2024-10-16 16:30:19.262921+00 | 2024-10-16 16:30:19.263133+00 | Client | ClientRead | begin isolation level read committed read only
00:00:00 | 33039 | zitadel_queries | 2024-10-16 16:25:53.914106+00 | 2024-10-16 16:30:19.191676+00 | 2024-10-16 16:30:19.191676+00 | 2024-10-16 16:30:19.191687+00 | Client | ClientRead | begin isolation level read committed read only
00:00:00.24539 | 33083 | zitadel_projection_spooler | 2024-10-16 16:27:49.895548+00 | 2024-10-16 16:30:19.020058+00 | 2024-10-16 16:30:19.265448+00 | 2024-10-16 16:30:19.26546+00 | Client | ClientRead | SAVEPOINT exec_stmt
00:00:00 | 33125 | zitadel_es_pusher | 2024-10-16 16:29:54.963859+00 | 2024-10-16 16:30:19.191715+00 | 2024-10-16 16:30:19.191715+00 | 2024-10-16 16:30:19.191729+00 | Client | ClientRead | begin
00:00:00.004292 | 33032 | zitadel_queries | 2024-10-16 16:25:53.906624+00 | 2024-10-16 16:30:19.187713+00 | 2024-10-16 16:30:19.192005+00 | 2024-10-16 16:30:19.192062+00 | Client | ClientRead | SELECT created_at, event_type, "sequence", "position", payload, creator, "owner", instance_id, aggregate_type, aggregate_id, revision FROM eventstore.events2 WHERE instance_id = $1 AND aggregate_type
00:00:00 | 33031 | zitadel_queries | 2024-10-16 16:25:53.906422+00 | 2024-10-16 16:30:19.191625+00 | 2024-10-16 16:30:19.191625+00 | 2024-10-16 16:30:19.191645+00 | Client | ClientRead | begin isolation level read committed read only
```
The amount of idle transactions is significantly less if the query
transactions are removed:
example:
```
?column? | pid | application_name | backend_start | xact_start | query_start | state_change | wait_event_type | wait_event | query
-----------------+-------+----------------------------+-------------------------------+-------------------------------+-------------------------------+-------------------------------+-----------------+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00:00:00.000094 | 32741 | zitadel_queries | 2024-10-16 16:23:49.73935+00 | 2024-10-16 16:24:59.785589+00 | 2024-10-16 16:24:59.785683+00 | 2024-10-16 16:24:59.785684+00 | | | SELECT created_at, event_type, "sequence", "position", payload, creator, "owner", instance_id, aggregate_type, aggregate_id, revision FROM eventstore.events2 WHERE instance_id = $1 AND aggregate_type
00:00:00 | 32762 | zitadel_es_pusher | 2024-10-16 16:24:02.275136+00 | 2024-10-16 16:24:59.784586+00 | 2024-10-16 16:24:59.784586+00 | 2024-10-16 16:24:59.784607+00 | Client | ClientRead | begin
00:00:00.000167 | 32742 | zitadel_queries | 2024-10-16 16:23:49.740489+00 | 2024-10-16 16:24:59.784274+00 | 2024-10-16 16:24:59.784441+00 | 2024-10-16 16:24:59.784442+00 | | | with usr as ( +
| | | | | | | | | select u.id, u.creation_date, u.change_date, u.sequence, u.state, u.resource_owner, u.username, n.login_name as preferred_login_name +
| | | | | | | | | from projections.users13 u +
| | | | | | | | | left join projections.l
00:00:00.256014 | 32759 | zitadel_projection_spooler | 2024-10-16 16:24:01.418429+00 | 2024-10-16 16:24:59.52959+00 | 2024-10-16 16:24:59.785604+00 | 2024-10-16 16:24:59.785649+00 | Client | ClientRead | UPDATE projections.milestones SET reached_date = $1 WHERE (instance_id = $2) AND (type = $3) AND (reached_date IS NULL)
00:00:00.014199 | 32773 | zitadel_es_pusher | 2024-10-16 16:24:02.320404+00 | 2024-10-16 16:24:59.769509+00 | 2024-10-16 16:24:59.783708+00 | 2024-10-16 16:24:59.783709+00 | IO | WalSync | commit
00:00:00 | 32765 | zitadel_es_pusher | 2024-10-16 16:24:02.28173+00 | 2024-10-16 16:24:59.780413+00 | 2024-10-16 16:24:59.780413+00 | 2024-10-16 16:24:59.780426+00 | Client | ClientRead | begin
00:00:00.012729 | 32777 | zitadel_es_pusher | 2024-10-16 16:24:02.339737+00 | 2024-10-16 16:24:59.767432+00 | 2024-10-16 16:24:59.780161+00 | 2024-10-16 16:24:59.780195+00 | Client | ClientRead | RELEASE SAVEPOINT cockroach_restart
```
---------
Co-authored-by: Tim Möhlmann <tim+github@zitadel.com>
Co-authored-by: Livio Spring <livio.a@gmail.com>
Co-authored-by: Max Peintner <max@caos.ch>
Co-authored-by: Elio Bischof <elio@zitadel.com>
Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
Co-authored-by: Miguel Cabrerizo <30386061+doncicuto@users.noreply.github.com>
Co-authored-by: Joakim Lodén <Loddan@users.noreply.github.com>
Co-authored-by: Yxnt <Yxnt@users.noreply.github.com>
Co-authored-by: Stefan Benz <stefan@caos.ch>
Co-authored-by: Harsha Reddy <harsha.reddy@klaviyo.com>
Co-authored-by: Zach H <zhirschtritt@gmail.com>
2024-11-04 09:06:14 +00:00
|
|
|
row := db.DB.QueryRowContext(ctx, query, args...)
|
2024-01-02 15:27:36 +00:00
|
|
|
logging.OnError(row.Err()).Error("unexpected query error")
|
2023-08-22 12:49:02 +00:00
|
|
|
|
|
|
|
err = scan(row)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return row.Err()
|
|
|
|
}
|
|
|
|
|
2023-12-05 17:01:03 +00:00
|
|
|
func QueryJSONObject[T any](ctx context.Context, db *DB, query string, args ...any) (*T, error) {
|
|
|
|
var data []byte
|
|
|
|
err := db.QueryRowContext(ctx, func(row *sql.Row) error {
|
|
|
|
return row.Scan(&data)
|
|
|
|
}, query, args...)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, zerrors.ThrowInternal(err, "DATAB-Oath6", "Errors.Internal")
|
|
|
|
}
|
|
|
|
obj := new(T)
|
|
|
|
if err = json.Unmarshal(data, obj); err != nil {
|
|
|
|
return nil, zerrors.ThrowInternal(err, "DATAB-Vohs6", "Errors.Internal")
|
|
|
|
}
|
|
|
|
return obj, nil
|
|
|
|
}
|
|
|
|
|
2023-12-20 16:13:04 +00:00
|
|
|
func Connect(config Config, useAdmin bool, purpose dialect.DBPurpose) (*DB, error) {
|
2024-10-04 13:15:41 +00:00
|
|
|
client, pool, err := config.connector.Connect(useAdmin, config.EventPushConnRatio, config.ProjectionSpoolerConnRatio, purpose)
|
2022-02-11 10:02:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
if err := client.Ping(); err != nil {
|
2023-12-05 17:01:03 +00:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(err, "DATAB-0pIWD", "Errors.Database.Connection.Failed")
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
return &DB{
|
|
|
|
DB: client,
|
|
|
|
Database: config.connector,
|
2024-10-04 13:15:41 +00:00
|
|
|
Pool: pool,
|
2023-02-27 21:36:43 +00:00
|
|
|
}, nil
|
2022-02-11 10:02:47 +00:00
|
|
|
}
|
2022-07-28 14:25:42 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func DecodeHook(from, to reflect.Value) (_ interface{}, err error) {
|
2022-07-28 14:25:42 +00:00
|
|
|
if to.Type() != reflect.TypeOf(Config{}) {
|
|
|
|
return from.Interface(), nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
config := new(Config)
|
|
|
|
if err = mapstructure.Decode(from.Interface(), config); err != nil {
|
|
|
|
return nil, err
|
2022-07-28 14:25:42 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
configuredDialect := dialect.SelectByConfig(config.Dialects)
|
|
|
|
configs := make([]interface{}, 0, len(config.Dialects)-1)
|
2022-07-28 14:25:42 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
for name, dialectConfig := range config.Dialects {
|
2022-07-28 14:25:42 +00:00
|
|
|
if !configuredDialect.Matcher.MatchName(name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
configs = append(configs, dialectConfig)
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
config.connector, err = configuredDialect.Matcher.Decode(configs)
|
2022-07-28 14:25:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
return config, nil
|
2022-07-28 14:25:42 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
func (c Config) DatabaseName() string {
|
2022-07-28 14:25:42 +00:00
|
|
|
return c.connector.DatabaseName()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) Username() string {
|
|
|
|
return c.connector.Username()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) Password() string {
|
|
|
|
return c.connector.Password()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) Type() string {
|
|
|
|
return c.connector.Type()
|
|
|
|
}
|
2024-01-02 15:27:36 +00:00
|
|
|
|
|
|
|
func EscapeLikeWildcards(value string) string {
|
|
|
|
value = strings.ReplaceAll(value, "%", "\\%")
|
|
|
|
value = strings.ReplaceAll(value, "_", "\\_")
|
|
|
|
return value
|
|
|
|
}
|