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"
|
2022-07-28 14:25:42 +00:00
|
|
|
"reflect"
|
2022-03-28 08:05:09 +00:00
|
|
|
|
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"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
2022-02-11 10:02:47 +00:00
|
|
|
)
|
|
|
|
|
2022-07-28 14:25:42 +00:00
|
|
|
type Config struct {
|
2023-10-19 10:19:10 +00:00
|
|
|
Dialects map[string]interface{} `mapstructure:",remain"`
|
|
|
|
EventPushConnRatio float32
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
tx, err := db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
rollbackErr := tx.Rollback()
|
|
|
|
logging.OnError(rollbackErr).Info("commit of read only transaction failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = tx.Commit()
|
|
|
|
}()
|
|
|
|
|
|
|
|
rows, err := tx.QueryContext(ctx, query, args...)
|
|
|
|
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) {
|
|
|
|
tx, err := db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
rollbackErr := tx.Rollback()
|
|
|
|
logging.OnError(rollbackErr).Info("commit of read only transaction failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = tx.Commit()
|
|
|
|
}()
|
|
|
|
|
|
|
|
row := tx.QueryRowContext(ctx, query, args...)
|
|
|
|
|
|
|
|
err = scan(row)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return row.Err()
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
const (
|
|
|
|
zitadelAppName = "zitadel"
|
|
|
|
EventstorePusherAppName = "zitadel_es_pusher"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Connect(config Config, useAdmin, isEventPusher bool) (*DB, error) {
|
|
|
|
appName := zitadelAppName
|
|
|
|
if isEventPusher {
|
|
|
|
appName = EventstorePusherAppName
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := config.connector.Connect(useAdmin, isEventPusher, config.EventPushConnRatio, appName)
|
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 {
|
2022-08-31 07:52:43 +00:00
|
|
|
return nil, errors.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,
|
|
|
|
}, 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()
|
|
|
|
}
|