|
| 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 | + "fmt" |
| 23 | + |
| 24 | + "github.com/ClickHouse/clickhouse-go/v2" |
| 25 | +) |
| 26 | + |
| 27 | +func VariantExample() error { |
| 28 | + ctx := context.Background() |
| 29 | + |
| 30 | + conn, err := GetNativeConnection(clickhouse.Settings{ |
| 31 | + "allow_experimental_variant_type": true, |
| 32 | + "allow_suspicious_variant_types": true, |
| 33 | + }, nil, nil) |
| 34 | + if err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + |
| 38 | + if !CheckMinServerVersion(conn, 24, 4, 0) { |
| 39 | + fmt.Print("unsupported clickhouse version for Variant type") |
| 40 | + return nil |
| 41 | + } |
| 42 | + |
| 43 | + err = conn.Exec(ctx, "DROP TABLE IF EXISTS go_variant_example") |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + err = conn.Exec(ctx, ` |
| 49 | + CREATE TABLE go_variant_example ( |
| 50 | + c Variant(Bool, Int64, String) |
| 51 | + ) ENGINE = Memory |
| 52 | + `) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + batch, err := conn.PrepareBatch(ctx, "INSERT INTO go_variant_example (c)") |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + if err = batch.Append(true); err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + if err = batch.Append(int64(42)); err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + if err = batch.Append("example"); err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + if err = batch.Append(clickhouse.NewVariant("example variant")); err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + if err = batch.Append(clickhouse.NewVariantWithType("example variant with specific type", "String")); err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + if err = batch.Append(nil); err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + if err = batch.Send(); err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + // Switch on Go Type |
| 91 | + |
| 92 | + rows, err := conn.Query(ctx, "SELECT c FROM go_variant_example") |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + for i := 0; rows.Next(); i++ { |
| 98 | + var row clickhouse.Variant |
| 99 | + err := rows.Scan(&row) |
| 100 | + if err != nil { |
| 101 | + return fmt.Errorf("failed to scan row index %d: %w", i, err) |
| 102 | + } |
| 103 | + |
| 104 | + switch row.Any().(type) { |
| 105 | + case bool: |
| 106 | + fmt.Printf("row at index %d is Bool: %v\n", i, row.Any()) |
| 107 | + case int64: |
| 108 | + fmt.Printf("row at index %d is Int64: %v\n", i, row.Any()) |
| 109 | + case string: |
| 110 | + fmt.Printf("row at index %d is String: %v\n", i, row.Any()) |
| 111 | + case nil: |
| 112 | + fmt.Printf("row at index %d is NULL\n", i) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Switch on ClickHouse Type |
| 117 | + |
| 118 | + rows, err = conn.Query(ctx, "SELECT c FROM go_variant_example") |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + |
| 123 | + for i := 0; rows.Next(); i++ { |
| 124 | + var row clickhouse.Variant |
| 125 | + err := rows.Scan(&row) |
| 126 | + if err != nil { |
| 127 | + return fmt.Errorf("failed to scan row index %d: %w", i, err) |
| 128 | + } |
| 129 | + |
| 130 | + switch row.Type() { |
| 131 | + case "Bool": |
| 132 | + fmt.Printf("row at index %d is bool: %v\n", i, row.Any()) |
| 133 | + case "Int64": |
| 134 | + fmt.Printf("row at index %d is int64: %v\n", i, row.Any()) |
| 135 | + case "String": |
| 136 | + fmt.Printf("row at index %d is string: %v\n", i, row.Any()) |
| 137 | + case "": |
| 138 | + fmt.Printf("row at index %d is nil\n", i) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return nil |
| 143 | +} |
0 commit comments