|
| 1 | +// Licensed to ClickHouse, Inc. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. ClickHouse, Inc. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package clickhouse_api |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "encoding/json" |
| 23 | + "fmt" |
| 24 | + "github.com/ClickHouse/clickhouse-go/v2" |
| 25 | + "time" |
| 26 | +) |
| 27 | + |
| 28 | +type ProductPricing struct { |
| 29 | + Price int64 `json:"price"` |
| 30 | + Currency string `json:"currency"` |
| 31 | +} |
| 32 | + |
| 33 | +type Product struct { |
| 34 | + ID clickhouse.Dynamic `json:"id"` |
| 35 | + Name string `json:"name"` |
| 36 | + Tags []string `json:"tags"` |
| 37 | + Pricing ProductPricing `json:"pricing"` |
| 38 | + Metadata map[string]interface{} `json:"metadata"` |
| 39 | + CreatedAt time.Time `json:"created_at" chType:"DateTime64(3)"` |
| 40 | +} |
| 41 | + |
| 42 | +func NewExampleProduct() *Product { |
| 43 | + return &Product{ |
| 44 | + ID: clickhouse.NewDynamicWithType(uint64(1234), "UInt64"), |
| 45 | + Name: "Book", |
| 46 | + Tags: []string{"library", "fiction"}, |
| 47 | + Pricing: ProductPricing{ |
| 48 | + Price: 750, |
| 49 | + Currency: "usd", |
| 50 | + }, |
| 51 | + Metadata: map[string]interface{}{ |
| 52 | + "region": "us", |
| 53 | + "page_count": int64(852), |
| 54 | + }, |
| 55 | + CreatedAt: time.Now().UTC().Truncate(time.Millisecond), |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func JSONStructExample() error { |
| 60 | + ctx := context.Background() |
| 61 | + |
| 62 | + conn, err := GetNativeConnection(clickhouse.Settings{ |
| 63 | + "allow_experimental_json_type": true, |
| 64 | + }, nil, nil) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + if !CheckMinServerVersion(conn, 24, 9, 0) { |
| 70 | + fmt.Print("unsupported clickhouse version for JSON type") |
| 71 | + return nil |
| 72 | + } |
| 73 | + |
| 74 | + err = conn.Exec(ctx, "DROP TABLE IF EXISTS go_json_example") |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + err = conn.Exec(ctx, ` |
| 80 | + CREATE TABLE go_json_example (product JSON) ENGINE=Memory |
| 81 | + `) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + batch, err := conn.PrepareBatch(ctx, "INSERT INTO go_json_example (product)") |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + insertProduct := NewExampleProduct() |
| 92 | + |
| 93 | + if err = batch.Append(insertProduct); err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + if err = batch.Send(); err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + var selectedProduct Product |
| 102 | + |
| 103 | + if err = conn.QueryRow(ctx, "SELECT product FROM go_json_example").Scan(&selectedProduct); err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + insertProductBytes, err := json.Marshal(insertProduct) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + selectedProductBytes, err := json.Marshal(&selectedProduct) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + fmt.Printf("inserted product: %s\n", string(insertProductBytes)) |
| 118 | + fmt.Printf("selected product: %s\n", string(selectedProductBytes)) |
| 119 | + fmt.Printf("inserted product matches selected product: %t\n", string(insertProductBytes) == string(selectedProductBytes)) |
| 120 | + return nil |
| 121 | +} |
0 commit comments