Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#733 driver implemets the sql/driver/ConnBeginTx interface #779

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions clickhouse_std.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ func (std *stdDriver) Ping(ctx context.Context) error { return std.conn.ping(ctx

func (std *stdDriver) Begin() (driver.Tx, error) { return std, nil }

func (std *stdDriver) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
// Check the transaction level. If the transaction level is non-default
// then return an error here as the BeginTx driver value is not supported.
if int(opts.Isolation) != int(sql.LevelDefault) {
return nil, errors.New("sql: driver does not support non-default isolation level")
}

// If a read-only transaction is requested return an error as the
// BeginTx driver value is not supported.
if opts.ReadOnly {
return nil, errors.New("sql: driver does not support read-only transactions")
}

return std.Begin()
}

func (std *stdDriver) Commit() error {
if std.commit == nil {
return nil
Expand Down
128 changes: 128 additions & 0 deletions tests/issues/733_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package issues

import (
"context"
"crypto/tls"
"database/sql"
"fmt"
"github.com/ClickHouse/clickhouse-go/v2"
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
"github.com/stretchr/testify/require"
"strconv"
"testing"
)

func Test733_txWithOptionsDoesntThrowWithNilOptions(t *testing.T) {
env, err := GetIssuesTestEnvironment()
require.NoError(t, err)
useSSL, err := strconv.ParseBool(clickhouse_tests.GetEnv("CLICKHOUSE_USE_SSL", "false"))
require.NoError(t, err)
var tlsConfig *tls.Config
port := env.Port
if useSSL {
tlsConfig = &tls.Config{}
port = env.SslPort
}
options := &clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", env.Host, port)},
Auth: clickhouse.Auth{
Database: "default",
Username: env.Username,
Password: env.Password,
},
TLS: tlsConfig,
}
conn := clickhouse.OpenDB(options)
ctx := context.Background()
require.NoError(t, conn.Ping())
tx, err := conn.BeginTx(ctx, nil)
require.NoError(t, err)
require.NoError(t, tx.Commit())
}

func Test733_defaultTxDoesntThrow(t *testing.T) {
env, err := GetIssuesTestEnvironment()
require.NoError(t, err)
useSSL, err := strconv.ParseBool(clickhouse_tests.GetEnv("CLICKHOUSE_USE_SSL", "false"))
require.NoError(t, err)
var tlsConfig *tls.Config
port := env.Port
if useSSL {
tlsConfig = &tls.Config{}
port = env.SslPort
}
options := &clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", env.Host, port)},
Auth: clickhouse.Auth{
Database: "default",
Username: env.Username,
Password: env.Password,
},
TLS: tlsConfig,
}
conn := clickhouse.OpenDB(options)
require.NoError(t, conn.Ping())
tx, err := conn.Begin()
require.NoError(t, err)
require.NoError(t, tx.Commit())
}

func Test733_errorIfInvalidIsolationLevel(t *testing.T) {
env, err := GetIssuesTestEnvironment()
require.NoError(t, err)
useSSL, err := strconv.ParseBool(clickhouse_tests.GetEnv("CLICKHOUSE_USE_SSL", "false"))
require.NoError(t, err)
var tlsConfig *tls.Config
port := env.Port
if useSSL {
tlsConfig = &tls.Config{}
port = env.SslPort
}
options := &clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", env.Host, port)},
Auth: clickhouse.Auth{
Database: "default",
Username: env.Username,
Password: env.Password,
},
TLS: tlsConfig,
}
conn := clickhouse.OpenDB(options)
ctx := context.Background()
require.NoError(t, conn.Ping())
tx, err := conn.BeginTx(ctx, &sql.TxOptions{
Isolation: sql.LevelWriteCommitted,
})
require.Nil(t, tx)
require.Error(t, err)
}

func Test733_errorIfReadOnlyTx(t *testing.T) {
env, err := GetIssuesTestEnvironment()
require.NoError(t, err)
useSSL, err := strconv.ParseBool(clickhouse_tests.GetEnv("CLICKHOUSE_USE_SSL", "false"))
require.NoError(t, err)
var tlsConfig *tls.Config
port := env.Port
if useSSL {
tlsConfig = &tls.Config{}
port = env.SslPort
}
options := &clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", env.Host, port)},
Auth: clickhouse.Auth{
Database: "default",
Username: env.Username,
Password: env.Password,
},
TLS: tlsConfig,
}
conn := clickhouse.OpenDB(options)
ctx := context.Background()
require.NoError(t, conn.Ping())
tx, err := conn.BeginTx(ctx, &sql.TxOptions{
ReadOnly: true,
})
require.Nil(t, tx)
require.Error(t, err)
}