Skip to content

Commit

Permalink
Feature/support json column type (#20)
Browse files Browse the repository at this point in the history
* refactor to proton go driver
* support json type column
  • Loading branch information
yl-lisen authored Jun 15, 2022
1 parent 8c82937 commit fc92dc7
Show file tree
Hide file tree
Showing 133 changed files with 1,367 additions and 629 deletions.
2 changes: 1 addition & 1 deletion bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package clickhouse
package proton

import (
std_driver "database/sql/driver"
Expand Down
2 changes: 1 addition & 1 deletion bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package clickhouse
package proton

import (
"testing"
Expand Down
46 changes: 23 additions & 23 deletions clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package clickhouse
package proton

import (
"context"
Expand All @@ -40,10 +40,10 @@ type (
)

var (
ErrBatchAlreadySent = errors.New("clickhouse: batch has already been sent")
ErrAcquireConnTimeout = errors.New("clickhouse: acquire conn timeout. you can increase the number of max open conn or the dial timeout")
ErrUnsupportedServerRevision = errors.New("clickhouse: unsupported server revision")
ErrBindMixedNamedAndNumericParams = errors.New("clickhouse [bind]: mixed named and numeric parameters")
ErrBatchAlreadySent = errors.New("proton: batch has already been sent")
ErrAcquireConnTimeout = errors.New("proton: acquire conn timeout. you can increase the number of max open conn or the dial timeout")
ErrUnsupportedServerRevision = errors.New("proton: unsupported server revision")
ErrBindMixedNamedAndNumericParams = errors.New("proton [bind]: mixed named and numeric parameters")
)

type OpError struct {
Expand All @@ -55,46 +55,46 @@ type OpError struct {
func (e *OpError) Error() string {
switch err := e.Err.(type) {
case *column.Error:
return fmt.Sprintf("clickhouse [%s]: (%s %s) %s", e.Op, e.ColumnName, err.ColumnType, err.Err)
return fmt.Sprintf("proton [%s]: (%s %s) %s", e.Op, e.ColumnName, err.ColumnType, err.Err)
case *column.ColumnConverterError:
var hint string
if len(err.Hint) != 0 {
hint += ". " + err.Hint
}
return fmt.Sprintf("clickhouse [%s]: (%s) converting %s to %s is unsupported%s",
return fmt.Sprintf("proton [%s]: (%s) converting %s to %s is unsupported%s",
err.Op, e.ColumnName,
err.From, err.To,
hint,
)
}
return fmt.Sprintf("clickhouse [%s]: %s", e.Op, e.Err)
return fmt.Sprintf("proton [%s]: %s", e.Op, e.Err)
}

func Open(opt *Options) (driver.Conn, error) {
opt.setDefaults()
return &clickhouse{
return &proton{
opt: opt,
idle: make(chan *connect, opt.MaxIdleConns),
open: make(chan struct{}, opt.MaxOpenConns),
}, nil
}

type clickhouse struct {
type proton struct {
opt *Options
idle chan *connect
open chan struct{}
connID int64
}

func (clickhouse) Contributors() []string {
func (proton) Contributors() []string {
list := contributors.List
if len(list[len(list)-1]) == 0 {
return list[:len(list)-1]
}
return list
}

func (ch *clickhouse) ServerVersion() (*driver.ServerVersion, error) {
func (ch *proton) ServerVersion() (*driver.ServerVersion, error) {
var (
ctx, cancel = context.WithTimeout(context.Background(), ch.opt.DialTimeout)
conn, err = ch.acquire(ctx)
Expand All @@ -107,15 +107,15 @@ func (ch *clickhouse) ServerVersion() (*driver.ServerVersion, error) {
return &conn.server, nil
}

func (ch *clickhouse) Query(ctx context.Context, query string, args ...interface{}) (rows driver.Rows, err error) {
func (ch *proton) Query(ctx context.Context, query string, args ...interface{}) (rows driver.Rows, err error) {
conn, err := ch.acquire(ctx)
if err != nil {
return nil, err
}
return conn.query(ctx, ch.release, query, args...)
}

func (ch *clickhouse) QueryRow(ctx context.Context, query string, args ...interface{}) (rows driver.Row) {
func (ch *proton) QueryRow(ctx context.Context, query string, args ...interface{}) (rows driver.Row) {
conn, err := ch.acquire(ctx)
if err != nil {
return &row{
Expand All @@ -125,7 +125,7 @@ func (ch *clickhouse) QueryRow(ctx context.Context, query string, args ...interf
return conn.queryRow(ctx, ch.release, query, args...)
}

func (ch *clickhouse) Exec(ctx context.Context, query string, args ...interface{}) error {
func (ch *proton) Exec(ctx context.Context, query string, args ...interface{}) error {
conn, err := ch.acquire(ctx)
if err != nil {
return err
Expand All @@ -138,15 +138,15 @@ func (ch *clickhouse) Exec(ctx context.Context, query string, args ...interface{
return nil
}

func (ch *clickhouse) PrepareBatch(ctx context.Context, query string) (driver.Batch, error) {
func (ch *proton) PrepareBatch(ctx context.Context, query string) (driver.Batch, error) {
conn, err := ch.acquire(ctx)
if err != nil {
return nil, err
}
return conn.prepareBatch(ctx, query, ch.release)
}

func (ch *clickhouse) AsyncInsert(ctx context.Context, query string, wait bool) error {
func (ch *proton) AsyncInsert(ctx context.Context, query string, wait bool) error {
conn, err := ch.acquire(ctx)
if err != nil {
return err
Expand All @@ -159,7 +159,7 @@ func (ch *clickhouse) AsyncInsert(ctx context.Context, query string, wait bool)
return nil
}

func (ch *clickhouse) Ping(ctx context.Context) (err error) {
func (ch *proton) Ping(ctx context.Context) (err error) {
conn, err := ch.acquire(ctx)
if err != nil {
return err
Expand All @@ -172,7 +172,7 @@ func (ch *clickhouse) Ping(ctx context.Context) (err error) {
return nil
}

func (ch *clickhouse) Stats() driver.Stats {
func (ch *proton) Stats() driver.Stats {
return driver.Stats{
Open: len(ch.open),
Idle: len(ch.idle),
Expand All @@ -181,7 +181,7 @@ func (ch *clickhouse) Stats() driver.Stats {
}
}

func (ch *clickhouse) dial(ctx context.Context) (conn *connect, err error) {
func (ch *proton) dial(ctx context.Context) (conn *connect, err error) {
connID := int(atomic.AddInt64(&ch.connID, 1))
for num := range ch.opt.Addr {
if ch.opt.ConnOpenStrategy == ConnOpenRoundRobin {
Expand All @@ -194,7 +194,7 @@ func (ch *clickhouse) dial(ctx context.Context) (conn *connect, err error) {
return nil, err
}

func (ch *clickhouse) acquire(ctx context.Context) (conn *connect, err error) {
func (ch *proton) acquire(ctx context.Context) (conn *connect, err error) {
timer := time.NewTimer(ch.opt.DialTimeout)
defer timer.Stop()
select {
Expand Down Expand Up @@ -235,7 +235,7 @@ func (ch *clickhouse) acquire(ctx context.Context) (conn *connect, err error) {
return conn, nil
}

func (ch *clickhouse) release(conn *connect, err error) {
func (ch *proton) release(conn *connect, err error) {
if conn.released {
return
}
Expand All @@ -255,7 +255,7 @@ func (ch *clickhouse) release(conn *connect, err error) {
}
}

func (ch *clickhouse) Close() error {
func (ch *proton) Close() error {
for {
select {
case c := <-ch.idle:
Expand Down
4 changes: 2 additions & 2 deletions clickhouse_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package clickhouse
package proton

import (
"context"
Expand Down Expand Up @@ -104,7 +104,7 @@ func (o *Options) fromDSN(in string) error {
case "dial_timeout":
duration, err := time.ParseDuration(params.Get(v))
if err != nil {
return fmt.Errorf("clickhouse [dsn parse]: dial timeout: %s", err)
return fmt.Errorf("proton [dsn parse]: dial timeout: %s", err)
}
o.DialTimeout = duration
case "secure":
Expand Down
2 changes: 1 addition & 1 deletion clickhouse_rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package clickhouse
package proton

import (
"database/sql"
Expand Down
2 changes: 1 addition & 1 deletion clickhouse_rows_column_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package clickhouse
package proton

import (
"reflect"
Expand Down
6 changes: 3 additions & 3 deletions clickhouse_std.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package clickhouse
package proton

import (
"context"
Expand Down Expand Up @@ -64,7 +64,7 @@ func (o *stdConnOpener) Connect(ctx context.Context) (_ driver.Conn, err error)
}

func init() {
sql.Register("clickhouse", &stdDriver{})
sql.Register("proton", &stdDriver{})
}

func OpenDB(opt *Options) *sql.DB {
Expand Down Expand Up @@ -134,7 +134,7 @@ func (std *stdDriver) CheckNamedValue(nv *driver.NamedValue) error { return nil
func (std *stdDriver) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
if options := queryOptions(ctx); options.async.ok {
if len(args) != 0 {
return nil, errors.New("clickhouse: you can't use parameters in an asynchronous insert")
return nil, errors.New("proton: you can't use parameters in an asynchronous insert")
}
return driver.RowsAffected(0), std.conn.asyncInsert(ctx, query, options.async.wait)
}
Expand Down
4 changes: 2 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package clickhouse
package proton

import (
"context"
Expand Down Expand Up @@ -53,7 +53,7 @@ func dial(ctx context.Context, addr string, num int, opt *Options) (*connect, er
return nil, err
}
if opt.Debug {
debugf = log.New(os.Stdout, fmt.Sprintf("[clickhouse][conn=%d][%s]", num, conn.RemoteAddr()), 0).Printf
debugf = log.New(os.Stdout, fmt.Sprintf("[proton][conn=%d][%s]", num, conn.RemoteAddr()), 0).Printf
}
var compression bool
if opt.Compression != nil {
Expand Down
2 changes: 1 addition & 1 deletion conn_async_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package clickhouse
package proton

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion conn_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package clickhouse
package proton

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion conn_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//go:build linux || darwin || dragonfly || freebsd || netbsd || openbsd || solaris || illumos
// +build linux darwin dragonfly freebsd netbsd openbsd solaris illumos

package clickhouse
package proton

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion conn_check_ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//go:build !linux && !darwin && !dragonfly && !freebsd && !netbsd && !openbsd && !solaris && !illumos
// +build !linux,!darwin,!dragonfly,!freebsd,!netbsd,!openbsd,!solaris,!illumos

package clickhouse
package proton

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion conn_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package clickhouse
package proton

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion conn_handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package clickhouse
package proton

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion conn_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package clickhouse
package proton

import (
"time"
Expand Down
2 changes: 1 addition & 1 deletion conn_ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package clickhouse
package proton

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion conn_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package clickhouse
package proton

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion conn_profile_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package clickhouse
package proton

import (
"reflect"
Expand Down
2 changes: 1 addition & 1 deletion conn_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package clickhouse
package proton

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion conn_send_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package clickhouse
package proton

import (
"github.com/timeplus-io/proton-go-driver/v2/lib/proto"
Expand Down
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package clickhouse
package proton

import (
"context"
Expand Down
Loading

0 comments on commit fc92dc7

Please sign in to comment.