You've already forked go-clickhouse
mirror of
https://github.com/uptrace/go-clickhouse.git
synced 2025-08-06 22:12:48 +02:00
feat: initial commit
This commit is contained in:
13
example/basic/README.md
Normal file
13
example/basic/README.md
Normal file
@ -0,0 +1,13 @@
|
||||
# Basic example
|
||||
|
||||
To run this example, you need a ClickHouse database:
|
||||
|
||||
```shell
|
||||
clickhouse-client -q "CREATE DATABASE test"
|
||||
```
|
||||
|
||||
Then run:
|
||||
|
||||
```shell
|
||||
go run .
|
||||
```
|
23
example/basic/go.mod
Normal file
23
example/basic/go.mod
Normal file
@ -0,0 +1,23 @@
|
||||
module github.com/uptrace/go-clickhouse/example/basic
|
||||
|
||||
go 1.18
|
||||
|
||||
replace github.com/uptrace/go-clickhouse => ../..
|
||||
|
||||
replace github.com/uptrace/go-clickhouse/chdebug => ../../chdebug
|
||||
|
||||
require (
|
||||
github.com/uptrace/go-clickhouse v0.1.1
|
||||
github.com/uptrace/go-clickhouse/chdebug v0.1.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/codemodus/kace v0.5.1 // indirect
|
||||
github.com/fatih/color v1.13.0 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/pierrec/lz4/v4 v4.1.14 // indirect
|
||||
golang.org/x/exp v0.0.0-20220317015231-48e79f11773a // indirect
|
||||
golang.org/x/sys v0.0.0-20220317061510-51cd9980dadf // indirect
|
||||
)
|
27
example/basic/go.sum
Normal file
27
example/basic/go.sum
Normal file
@ -0,0 +1,27 @@
|
||||
github.com/bradleyjkemp/cupaloy v2.3.0+incompatible h1:UafIjBvWQmS9i/xRg+CamMrnLTKNzo+bdmT/oH34c2Y=
|
||||
github.com/codemodus/kace v0.5.1 h1:4OCsBlE2c/rSJo375ggfnucv9eRzge/U5LrrOZd47HA=
|
||||
github.com/codemodus/kace v0.5.1/go.mod h1:coddaHoX1ku1YFSe4Ip0mL9kQjJvKkzb9CfIdG1YR04=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
|
||||
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
|
||||
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
github.com/pierrec/lz4/v4 v4.1.14 h1:+fL8AQEZtz/ijeNnpduH0bROTu0O3NZAlPjQxGn8LwE=
|
||||
github.com/pierrec/lz4/v4 v4.1.14/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
golang.org/x/exp v0.0.0-20220317015231-48e79f11773a h1:DAzrdbxsb5tXNOhMCSwF7ZdfMbW46hE9fSVO6BsmUZM=
|
||||
golang.org/x/exp v0.0.0-20220317015231-48e79f11773a/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220317061510-51cd9980dadf h1:Fm4IcnUL803i92qDlmB0obyHmosDrxZWxJL3gIeNqOw=
|
||||
golang.org/x/sys v0.0.0-20220317061510-51cd9980dadf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
50
example/basic/main.go
Normal file
50
example/basic/main.go
Normal file
@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/uptrace/go-clickhouse/ch"
|
||||
"github.com/uptrace/go-clickhouse/chdebug"
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
ch.CHModel `ch:"partition:toYYYYMM(time)"`
|
||||
|
||||
ID uint64
|
||||
Text string `ch:",lc"`
|
||||
Time time.Time `ch:",pk"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
|
||||
db := ch.Connect(ch.WithDatabase("test"))
|
||||
db.AddQueryHook(chdebug.NewQueryHook(chdebug.WithVerbose(true)))
|
||||
|
||||
if err := db.Ping(ctx); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var num int
|
||||
if err := db.QueryRowContext(ctx, "SELECT 123").Scan(&num); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(num)
|
||||
|
||||
if err := db.ResetModel(ctx, (*Model)(nil)); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
src := &Model{ID: 1, Text: "hello", Time: time.Now()}
|
||||
if _, err := db.NewInsert().Model(src).Exec(ctx); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
dest := new(Model)
|
||||
if err := db.NewSelect().Model(dest).Where("id = ?", src.ID).Limit(1).Scan(ctx); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(dest)
|
||||
}
|
Reference in New Issue
Block a user