README
¶
GORM ClickHouse Driver
Clickhouse support for GORM
Quick Start
You can simply test your connection to your database with the following:
package main
import (
"gorm.io/driver/clickhouse"
"gorm.io/gorm"
)
type User struct {
Name string
Age int
}
func main() {
dsn := "tcp://localhost:9000?database=gorm&username=gorm&password=gorm&read_timeout=10&write_timeout=20"
db, err := gorm.Open(clickhouse.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
// Auto Migrate
db.AutoMigrate(&User{})
// Set table options
db.Set("gorm:table_options", "ENGINE=Distributed(cluster, default, hits)").AutoMigrate(&User{})
// Set table cluster options
db.Set("gorm:table_cluster_options", "on cluster default").AutoMigrate(&User{})
// Insert
db.Create(&User{Name: "Angeliz", Age: 18})
// Select
db.Find(&User{}, "name = ?", "Angeliz")
// Batch Insert
user1 := User{Age: 12, Name: "Bruce Lee"}
user2 := User{Age: 13, Name: "Feynman"}
user3 := User{Age: 14, Name: "Angeliz"}
var users = []User{user1, user2, user3}
db.Create(&users)
// ...
}
Advanced Configuration
package main
import (
"gorm.io/driver/clickhouse"
"gorm.io/gorm"
)
// refer to https://github.com/ClickHouse/clickhouse-go
var dsn = "tcp://localhost:9000?database=gorm&username=gorm&password=gorm&read_timeout=10&write_timeout=20"
func main() {
db, err := gorm.Open(clickhouse.New(click.Config{
DSN: dsn,
Conn: conn, // initialize with existing database conn
DisableDatetimePrecision: true, // disable datetime64 precision, not supported before clickhouse 20.4
DontSupportRenameColumn: true, // rename column not supported before clickhouse 20.4
SkipInitializeWithVersion: false, // smart configure based on used version
DefaultGranularity: 3, // 1 granule = 8192 rows
DefaultCompression: "LZ4", // default compression algorithm. LZ4 is lossless
DefaultIndexType: "minmax", // index stores extremes of the expression
DefaultTableEngineOpts: "ENGINE=MergeTree() ORDER BY tuple()",
}), &gorm.Config{})
}
Checkout https://gorm.io for details.
Documentation
¶
Index ¶
- Variables
- func Create(db *gorm.DB)
- func New(config Config) gorm.Dialector
- func Open(dsn string) gorm.Dialector
- type Config
- type Dialector
- func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{})
- func (dialector Dialector) ClauseBuilders() map[string]clause.ClauseBuilder
- func (dialector Dialector) DataTypeOf(field *schema.Field) string
- func (dialector Dialector) DefaultValueOf(field *schema.Field) clause.Expression
- func (dialector Dialector) Explain(sql string, vars ...interface{}) string
- func (dialector Dialector) Initialize(db *gorm.DB) (err error)
- func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator
- func (dialector Dialector) Name() string
- func (dialector Dialector) QuoteTo(writer clause.Writer, str string)
- func (dialectopr Dialector) RollbackTo(tx *gorm.DB, name string) error
- func (dialectopr Dialector) SavePoint(tx *gorm.DB, name string) error
- type Migrator
- func (m Migrator) AddColumn(value interface{}, field string) error
- func (m Migrator) AlterColumn(value interface{}, field string) error
- func (m Migrator) BuildIndexOptions(opts []schema.IndexOption, stmt *gorm.Statement) (results []interface{})
- func (m Migrator) CreateIndex(value interface{}, name string) error
- func (m Migrator) CreateTable(models ...interface{}) error
- func (m Migrator) CurrentDatabase() (name string)
- func (m Migrator) DropColumn(value interface{}, name string) error
- func (m Migrator) DropIndex(value interface{}, name string) error
- func (m Migrator) FullDataTypeOf(field *schema.Field) (expr clause.Expr)
- func (m Migrator) HasColumn(value interface{}, field string) bool
- func (m Migrator) HasTable(value interface{}) bool
- func (m Migrator) RenameColumn(value interface{}, oldName, newName string) error
- func (m Migrator) RenameIndex(value interface{}, oldName, newName string) error
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrRenameColumnUnsupported = errors.New("renaming column is not supported in your clickhouse version < 20.4.") ErrRenameIndexUnsupported = errors.New("renaming index is not supported") ErrCreateIndexFailed = errors.New("failed to create index with name") )
Errors enumeration
Functions ¶
Types ¶
type Config ¶
type Config struct { DriverName string DSN string Conn gorm.ConnPool DisableDatetimePrecision bool DontSupportRenameColumn bool SkipInitializeWithVersion bool DefaultGranularity int // 1 granule = 8192 rows DefaultCompression string // default compression algorithm. LZ4 is lossless DefaultIndexType string // index stores extremes of the expression DefaultTableEngineOpts string }
type Dialector ¶
type Dialector struct {
*Config
}
func (Dialector) ClauseBuilders ¶
func (dialector Dialector) ClauseBuilders() map[string]clause.ClauseBuilder
func (Dialector) DefaultValueOf ¶
func (dialector Dialector) DefaultValueOf(field *schema.Field) clause.Expression
func (Dialector) RollbackTo ¶
type Migrator ¶
func (Migrator) AlterColumn ¶
func (Migrator) BuildIndexOptions ¶
func (m Migrator) BuildIndexOptions(opts []schema.IndexOption, stmt *gorm.Statement) (results []interface{})
func (Migrator) CreateIndex ¶
func (Migrator) CreateTable ¶
func (Migrator) CurrentDatabase ¶
func (Migrator) DropColumn ¶
func (Migrator) FullDataTypeOf ¶
func (Migrator) RenameColumn ¶
NOTE: Only supported after ClickHouse 20.4 and above. See: https://github.com/ClickHouse/ClickHouse/issues/146
func (Migrator) RenameIndex ¶
Click to show internal directories.
Click to hide internal directories.