go-gorm / clickhouse

GORM clickhouse driver
MIT License
240 stars 71 forks source link

OpenDB is not declared #74

Open nrbackback opened 1 year ago

nrbackback commented 1 year ago
image image

gorm.io/driver/clickhouse version is v0.5.0

huangliu commented 1 year ago

It declared in "github.com/ClickHouse/clickhouse-go/v2"

nrbackback commented 1 year ago

https://github.com/go-gorm/clickhouse in readme

image

is this wrong?

edhemphill commented 7 months ago

Yeah, the advanced README example seems to be a bit messed up.

This code worked for me:

var dsn = "clickhouse://default:@127.0.0.1:9000/db?dial_timeout=200ms&max_execution_time=60"

if _, err = gorm.Open(clickhouse.Open(dsn), &gorm.Config{}); err != nil {
   fmt.Printf("failed to connect database, got error %v", err)
   os.Exit(1)
}

fmt.Printf("No error. Connectivity ok.\n")

Assuming db is created already.

In the advanced example click is also not defined / imported.

edhemphill commented 7 months ago

Here is an advanced example:

package main

import (
    "context"
    "fmt"
    "os"
    "time"

    chapi "github.com/ClickHouse/clickhouse-go/v2"
    "github.com/tlalocweb/hulation/config"
    "gorm.io/driver/clickhouse"
    "gorm.io/gorm"
)

func main() {

    var confpath string

    if len(os.Args) > 1 {
        confpath = os.Args[1]
    }
    if len(confpath) < 1 {
        fmt.Printf("Error: config file path not provided.\n")
        os.Exit(1)
    }
    myconf, err := config.LoadConfig(confpath)

    if err != nil {
        fmt.Printf("Error loading config: (%s) %s", confpath, err.Error())
        os.Exit(1)
    }

    dsn := config.GetDSNFromConfig(myconf)
    fmt.Printf("Connecting to %s\n", dsn)
        // or ... whatver ...
    //  var dsn = "clickhouse://default:@127.0.0.1:9000/db?dial_timeout=200ms&max_execution_time=60"

    fmt.Printf("testing clickhouse-go library...\n")

    conn := chapi.OpenDB(&chapi.Options{
        Addr: []string{fmt.Sprintf("%s:%d", myconf.DBConfig.Host, myconf.DBConfig.Port)},
        Auth: chapi.Auth{
            Database: myconf.DBConfig.DBName,
            Username: myconf.DBConfig.Username,
            Password: myconf.DBConfig.Password,
        },
        Settings: chapi.Settings{
            "max_execution_time": 60,
        },
        DialTimeout: 5 * time.Second,
        Compression: &chapi.Compression{
            Method: chapi.CompressionLZ4,
        },
    })

    conn.SetMaxIdleConns(5)
    conn.SetMaxOpenConns(10)
    conn.SetConnMaxLifetime(time.Hour)
    ctx := chapi.Context(context.Background(), chapi.WithSettings(chapi.Settings{
        "max_block_size": 10,
    }), chapi.WithProgress(func(p *chapi.Progress) {
        fmt.Println("progress: ", p)
    }))
    if err := conn.PingContext(ctx); err != nil {
        if exception, ok := err.(*chapi.Exception); ok {
            fmt.Printf("Catch exception [%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
        } else {
                    fmt.Printf("Error: %s\n", err.Error())
                }
        os.Exit(1)
    } else {
        fmt.Println("Ping OK")
    }
    //  conn.Close()
    fmt.Printf("testing gorm w/ clickhouse driver...\n")

    if _, err = gorm.Open(clickhouse.New(clickhouse.Config{
        Conn: conn,
    }), &gorm.Config{}); err != nil {
        fmt.Printf("failed to connect database, got error %v", err)
        os.Exit(1)
    }

    fmt.Printf("No error. Connectivity ok.\n")