Browse Source

clickhouse

Alexey Kim 8 months ago
parent
commit
22844bb93a
3 changed files with 35 additions and 10 deletions
  1. 2 8
      connection/cursor.go
  2. 32 2
      database/clickhouse.go
  3. 1 0
      database/database.go

+ 2 - 8
connection/cursor.go

@@ -1,16 +1,10 @@
 package connection
 
+type Namespace string
+
 type Cursor interface {
 	NS() Namespace
 	Id() string
 
 	Cursor() string
 }
-
-type Namespace string
-
-const (
-	NSMatch  Namespace = "Match"
-	NSLeague Namespace = "League"
-	NSTeam   Namespace = "Team"
-)

+ 32 - 2
database/clickhouse.go

@@ -6,13 +6,12 @@ import (
 	"fmt"
 	"github.com/ClickHouse/clickhouse-go/v2"
 	"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
+	"strings"
 	"sync"
 	"time"
 )
 
 type _ch struct {
-	Database[driver.Rows, driver.Batch]
-
 	config *configCH
 	conn   clickhouse.Conn
 }
@@ -84,6 +83,37 @@ func (db *_ch) Ping(ctx context.Context) error {
 	return db.conn.Ping(ctx)
 }
 
+func (db *_ch) Exec(ctx context.Context, query string, args ...any) error {
+	return db.conn.Exec(ctx, query, args...)
+}
+
 func (db *_ch) Query(ctx context.Context, query string, args ...any) (driver.Rows, error) {
 	return db.conn.Query(ctx, query, args...)
 }
+
+func (db *_ch) Batch(ctx context.Context, table string, release bool) (driver.Batch, error) {
+	table = strings.TrimSpace(table)
+	if table == "" {
+		return nil, fmt.Errorf("illegal argumets: empty table name")
+	}
+
+	var (
+		batch driver.Batch
+		opts  []driver.PrepareBatchOption
+		err   error
+	)
+
+	if release {
+		opts = append(opts, driver.WithReleaseConnection())
+	}
+
+	if //goland:noinspection SqlNoDataSourceInspection
+	batch, err = db.conn.PrepareBatch(ctx,
+		fmt.Sprintf("INSERT INTO `%s`", table),
+		opts...,
+	); err != nil {
+		return nil, err
+	}
+
+	return batch, nil
+}

+ 1 - 0
database/database.go

@@ -9,6 +9,7 @@ type Database[T any, B any] interface {
 	Close() error
 
 	Ping(ctx context.Context) error
+	Exec(ctx context.Context, query string, args ...any) error
 	Query(ctx context.Context, query string, args ...any) (T, error)
 	Batch(ctx context.Context, table string, release bool) (B, error)
 }