-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
152 lines (121 loc) · 3.68 KB
/
table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package chorm
import (
"context"
"fmt"
"reflect"
"github.com/0x19/go-clickhouse-orm/models"
"github.com/0x19/go-clickhouse-orm/sql"
"github.com/vahid-sohrabloo/chconn/v3"
"github.com/vahid-sohrabloo/chconn/v3/column"
"go.uber.org/zap"
)
type TableBuilder[T models.Model] struct {
ctx context.Context
orm *ORM
model T
builder *sql.TableBuilder
}
func (b *TableBuilder[T]) Build() (string, error) {
return b.builder.Build()
}
func (b *TableBuilder[T]) ExecContext(ctx context.Context, queryOptions *chconn.QueryOptions, columns ...column.ColumnBasic) error {
return b.orm.GetConn().ExecWithOption(ctx, b.SQL(), queryOptions)
}
func (b *TableBuilder[T]) Exec(ctx context.Context) error {
return b.orm.GetConn().Exec(ctx, b.SQL())
}
func (b *TableBuilder[T]) GetBuilder() *sql.TableBuilder {
return b.builder
}
func (b *TableBuilder[T]) SQL() string {
return b.builder.String()
}
func NewCreateTable[T models.Model](ctx context.Context, orm *ORM, model T, queryOptions *chconn.QueryOptions) (*TableBuilder[T], error) {
// Check if the underlying value of the interface is nil. Unfortunately, it is a T and we cannot
// directly check if it's nil due to type missmatch.
{
modelValue := reflect.ValueOf(model)
if !modelValue.IsValid() {
return nil, fmt.Errorf("model cannot be nil")
}
if modelValue.Kind() == reflect.Ptr && modelValue.IsNil() {
return nil, fmt.Errorf("model cannot be nil")
}
}
stmtBuilder := sql.NewCreateTableBuilder()
stmtBuilder.Model(model)
declaration, err := orm.GetManager().GetDeclaration(model)
if err != nil {
return nil, err
}
if declaration == nil {
return nil, fmt.Errorf("model declaration cannot be nil")
}
if declaration.DatabaseName != "" {
stmtBuilder.Database(declaration.DatabaseName)
} else {
stmtBuilder.Database(orm.GetDatabaseName())
}
fields := make([]string, 0)
for _, field := range declaration.GetFields() {
fields = append(fields, field.GetDDL())
}
stmtBuilder.Fields(fields...)
stmtBuilder.Engine(declaration.Engine)
stmtBuilder.PartitionBy(declaration.PartitionBy)
pkFields := make([]string, 0)
for _, field := range declaration.GetPKFields() {
pkFields = append(pkFields, field.Name)
}
stmtBuilder.PrimaryKeys(pkFields...)
stmtBuilder.OrderBy(declaration.OrderBy...)
stmtBuilder.Settings(declaration.Settings...)
builder := &TableBuilder[T]{
ctx: ctx,
orm: orm,
model: model,
builder: stmtBuilder,
}
zap.L().Debug("Create table statement", zap.String("sql", builder.SQL()))
if err := builder.Exec(ctx); err != nil {
return builder, err
}
return builder, nil
}
func NewDropTable[T models.Model](ctx context.Context, orm *ORM, model T, queryOptions *chconn.QueryOptions) (*TableBuilder[T], error) {
// Check if the underlying value of the interface is nil. Unfortunately, it is a T and we cannot
// directly check if it's nil due to type missmatch.
{
modelValue := reflect.ValueOf(model)
if !modelValue.IsValid() {
return nil, fmt.Errorf("model cannot be nil")
}
if modelValue.Kind() == reflect.Ptr && modelValue.IsNil() {
return nil, fmt.Errorf("model cannot be nil")
}
}
stmtBuilder := sql.NewDropTableBuilder()
stmtBuilder.Model(model)
declaration, err := orm.GetManager().GetDeclaration(model)
if err != nil {
return nil, err
}
if declaration == nil {
return nil, fmt.Errorf("model declaration cannot be nil")
}
if declaration.DatabaseName != "" {
stmtBuilder.Database(declaration.DatabaseName)
} else {
stmtBuilder.Database(orm.GetDatabaseName())
}
builder := &TableBuilder[T]{
ctx: ctx,
orm: orm,
model: model,
builder: stmtBuilder,
}
if err := builder.Exec(ctx); err != nil {
return builder, err
}
return builder, nil
}