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

OpenDB is not declared #74

Open
nrbackback opened this issue Dec 16, 2022 · 4 comments
Open

OpenDB is not declared #74

nrbackback opened this issue Dec 16, 2022 · 4 comments
Assignees

Comments

@nrbackback
Copy link

image

image

gorm.io/driver/clickhouse version is v0.5.0
@huangliu
Copy link

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

@nrbackback
Copy link
Author

https://github.com/go-gorm/clickhouse
in readme
image
is this wrong?

@edhemphill
Copy link

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
Copy link

edhemphill commented Jan 11, 2024

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")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants