add init connection with clickHouse
This commit is contained in:
parent
e3e07c8869
commit
1c53cb3c1c
27
dal/dal.go
27
dal/dal.go
@ -5,6 +5,7 @@ import (
|
||||
"database/sql"
|
||||
_ "embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen"
|
||||
@ -177,3 +178,29 @@ func (d *AmoDal) Close(ctx context.Context) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type ClickHouseDAL struct {
|
||||
conn *sql.DB
|
||||
}
|
||||
|
||||
func NewClickHouseDAL(ctx context.Context, cred string) (*ClickHouseDAL, error) {
|
||||
conn, err := sql.Open("clickhouse", cred)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error open database connection: %w", err)
|
||||
}
|
||||
|
||||
timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := conn.PingContext(timeoutCtx); err != nil {
|
||||
return nil, fmt.Errorf("error ping database: %w", err)
|
||||
}
|
||||
|
||||
return &ClickHouseDAL{
|
||||
conn: conn,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (d *ClickHouseDAL) Close(ctx context.Context) error {
|
||||
return d.conn.Close()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user