/
create.go
131 lines (116 loc) · 3.48 KB
/
create.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
// Copyright (C) 2017 Space Monkey, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package golang
import (
"fmt"
"gopkg.in/spacemonkeygo/dbx.v1/ir"
"gopkg.in/spacemonkeygo/dbx.v1/sql"
"gopkg.in/spacemonkeygo/dbx.v1/sqlgen/sqlembedgo"
)
type RawCreate struct {
Info sqlembedgo.Info
Suffix string
Return *Var
Arg *Var
Fields []*Var
SupportsReturning bool
}
func RawCreateFromIR(ir_cre *ir.Create, dialect sql.Dialect) *RawCreate {
insert_sql := sql.InsertSQL(ir_cre, dialect)
ins := &RawCreate{
Info: sqlembedgo.Embed("__", insert_sql),
Suffix: convertSuffix(ir_cre.Suffix),
SupportsReturning: dialect.Features().Returning,
}
if !ir_cre.NoReturn {
ins.Return = VarFromModel(ir_cre.Model)
}
// the model struct is the only arg.
ins.Arg = VarFromModel(ir_cre.Model)
ins.Arg.Name = "raw_" + ins.Arg.Name
// each field in the model is initialized from the raw model struct.
for _, field := range ir_cre.Fields() {
f := ModelFieldFromIR(field)
v := VarFromField(field)
if field.Nullable {
v.InitVal = fmt.Sprintf("%s_%s_Raw(%s.%s).value()",
ins.Arg.Type, f.Name, ins.Arg.Name, f.Name)
} else {
v.InitVal = fmt.Sprintf("%s_%s(%s.%s).value()",
ins.Arg.Type, f.Name, ins.Arg.Name, f.Name)
}
v.Name = fmt.Sprintf("__%s_val", v.Name)
ins.Fields = append(ins.Fields, v)
}
return ins
}
type Create struct {
Info sqlembedgo.Info
Suffix string
Return *Var
Args []*Var
Fields []*Var
SupportsReturning bool
NeedsNow bool
}
func CreateFromIR(ir_cre *ir.Create, dialect sql.Dialect) *Create {
insert_sql := sql.InsertSQL(ir_cre, dialect)
ins := &Create{
Info: sqlembedgo.Embed("__", insert_sql),
Suffix: convertSuffix(ir_cre.Suffix),
SupportsReturning: dialect.Features().Returning,
}
if !ir_cre.NoReturn {
ins.Return = VarFromModel(ir_cre.Model)
}
args := map[string]*Var{}
// All of the manual fields are arguments to the function. The Field struct
// type is used (pointer if nullable).
has_nullable := false
for _, field := range ir_cre.InsertableFields() {
arg := ArgFromField(field)
args[field.Name] = arg
if !field.Nullable {
ins.Args = append(ins.Args, arg)
} else {
has_nullable = true
}
}
if has_nullable {
ins.Args = append(ins.Args, &Var{
Name: "optional",
Type: ModelStructFromIR(ir_cre.Model).CreateStructName(),
})
}
// Now for each field
for _, field := range ir_cre.Fields() {
if field == ir_cre.Model.BasicPrimaryKey() {
continue
}
v := VarFromField(field)
v.Name = fmt.Sprintf("__%s_val", v.Name)
if arg := args[field.Name]; arg != nil {
if field.Nullable {
f := ModelFieldFromIR(field)
v.InitVal = fmt.Sprintf("optional.%s.value()", f.Name)
} else {
v.InitVal = fmt.Sprintf("%s.value()", arg.Name)
}
} else if field.IsTime() {
ins.NeedsNow = true
}
ins.Fields = append(ins.Fields, v)
}
return ins
}