-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_entity.go
More file actions
66 lines (54 loc) · 1.6 KB
/
test_entity.go
File metadata and controls
66 lines (54 loc) · 1.6 KB
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
package main
import (
"context"
"time"
)
//go:generate go run github.com/solta-dev/gentity
type SomeInts int
// type DBExecutorKey string
// type DBExecutor interface {
// Exec(ctx context.Context, sql string, arguments ...any) (commandTag pgconn.CommandTag, err error)
// Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
// QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
// }
const (
Int1 SomeInts = iota
Int2 SomeInts = iota
Int3 SomeInts = iota
)
type jsonType struct {
Str string `json:"str"`
Int int `json:"int"`
}
// gentity
type Test struct {
ID uint64 `gentity:"unique=primary autoincrement"`
IntA int `gentity:"index=test_int_a_int_b"`
IntB SomeInts `gentity:"index=test_int_a_int_b"`
StrA string `gentity:"unique=test_str_a"`
TimeA time.Time `gentity:""`
Json jsonType `gentity:""`
}
func (Test) createTable(ctx context.Context) error {
pgConn, ok := ctx.Value(DBExecutorKey("dbExecutor")).(DBExecutor)
if !ok {
panic("ctx value dbExecutor has bad type")
}
if _, err := pgConn.Exec(context.Background(), `CREATE TABLE tests (
id bigserial PRIMARY KEY,
int_a integer NOT NULL,
int_b integer NOT NULL,
str_a varchar(256) NOT NULL,
time_a timestamp NOT NULL DEFAULT now(),
json jsonb NOT NULL DEFAULT '{}'::jsonb
)`); err != nil {
return err
}
if _, err := pgConn.Exec(context.Background(), `CREATE INDEX test_int_a_int_b ON tests (int_a, int_b)`); err != nil {
return err
}
if _, err := pgConn.Exec(context.Background(), `CREATE UNIQUE INDEX test_str_a ON tests (str_a)`); err != nil {
return err
}
return nil
}