Skip to content

Commit 3443106

Browse files
committed
Cleanup + Add missing bench for sqlite
1 parent bbb7579 commit 3443106

11 files changed

Lines changed: 199 additions & 170 deletions

File tree

docs/.vitepress/config.mts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ export default defineConfig({
3232
{
3333
text: 'Advanced Features',
3434
items: [
35-
// { text: 'ID Generation', link: '/api/mutations' },
35+
// { text: 'UUID Generation', link: '/api/mutations' },
3636
{ text: 'Soft Delete', link: '/features/soft-delete' },
3737
// { text: 'Logging', link: '/api/mutations' },
38+
// { text: 'Migrations', link: '/api/mutations' },
3839
]
3940
},
4041
// {

internal/database/database.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ func Generate(schema *core.SQLSchema, contents io.Writer, pkg string, driver str
5050
return err
5151
}
5252

53-
ctxTmpl, err := template.ParseFS(templates, fmt.Sprintf("templates/ctx_%s.tmpl", templateType))
54-
if err != nil {
55-
return err
56-
}
57-
5853
modelTmpl, err := template.ParseFS(templates, "templates/model.tmpl")
5954
if err != nil {
6055
return err
@@ -116,11 +111,11 @@ func Generate(schema *core.SQLSchema, contents io.Writer, pkg string, driver str
116111
}
117112

118113
if err = headerTmpl.Execute(contents, HeaderData{
119-
Package: pkg,
120-
Url: "https://github.com/kefniark/mangosql",
121-
Date: time.Now().String(),
122-
Version: "0.0.1",
123-
Deps: maps.Values(deps),
114+
Package: pkg,
115+
Url: "https://github.com/kefniark/mangosql",
116+
Date: time.Now().String(),
117+
Version: "0.0.1",
118+
Deps: maps.Values(deps),
124119
Placeholder: placeholder,
125120
}); err != nil {
126121
return err
@@ -138,12 +133,6 @@ func Generate(schema *core.SQLSchema, contents io.Writer, pkg string, driver str
138133
return err
139134
}
140135

141-
if err = ctxTmpl.Execute(contents, PostgresCtx{
142-
Const: ctxConstBuf.String(),
143-
}); err != nil {
144-
return err
145-
}
146-
147136
for _, table := range postgresTables {
148137
if err = modelTmpl.Execute(contents, table); err != nil {
149138
return err

internal/database/filters.go

Lines changed: 84 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ func GetFilterMethods(tables []*PostgresTable, driver string) []FilterMethod {
6868
if err != nil {
6969
return f.IsNull()
7070
}
71+
sql := fmt.Sprintf("%s.%s IN (SELECT value FROM json_each(?))", f.table, f.field)
7172
`,
7273
Comment: `Only include Records with a field contains in a set of values`,
73-
Sql: `.Where(fmt.Sprintf("%s.%s IN (SELECT value FROM json_each(?))", f.table, f.field), jsonArgs)`,
74+
Sql: `.Where(sql, jsonArgs)`,
7475
Args: []string{"args ...T"},
7576
},
7677
SelectFilter{
@@ -80,73 +81,90 @@ func GetFilterMethods(tables []*PostgresTable, driver string) []FilterMethod {
8081
if err != nil {
8182
return f.IsNull()
8283
}
84+
sql := fmt.Sprintf("%s.%s NOT INT (SELECT value FROM json_each(?))", f.table, f.field)
8385
`,
8486
Comment: `Exclude Records with a field not contains in a set of values`,
85-
Sql: `.Where(fmt.Sprintf("%s.%s NOT INT (SELECT value FROM json_each(?))", f.table, f.field), jsonArgs)`,
87+
Sql: `.Where(sql, jsonArgs)`,
8688
Args: []string{"args ...T"},
8789
},
8890
)
8991
} else {
9092
method.Filters = append(method.Filters,
9193
SelectFilter{
92-
Model: t,
93-
Name: "In",
94+
Model: t,
95+
Name: "In",
96+
Pre: `sql := fmt.Sprintf("%s.%s = ANY(?)", f.table, f.field)
97+
`,
9498
Comment: `Only include Records with a field contains in a set of values`,
95-
Sql: `.Where(fmt.Sprintf("%s.%s = ANY(?)", f.table, f.field), pq.Array(args))`,
99+
Sql: `.Where(sql, pq.Array(args))`,
96100
Args: []string{"args ...T"},
97101
},
98102
SelectFilter{
99-
Model: t,
100-
Name: "NotIn",
103+
Model: t,
104+
Name: "NotIn",
105+
Pre: `sql := fmt.Sprintf("%s.%s != ANY(?)", f.table, f.field)
106+
`,
101107
Comment: `Exclude Records with a field not contains in a set of values`,
102-
Sql: `.Where(fmt.Sprintf("%s.%s != ANY(?)", f.table, f.field), pq.Array(args))`,
108+
Sql: `.Where(sql, pq.Array(args))`,
103109
Args: []string{"args ...T"},
104110
},
105111
)
106112
}
107113

108114
method.Filters = append(method.Filters,
109115
SelectFilter{
110-
Model: t,
111-
Name: "Equal",
116+
Model: t,
117+
Name: "Equal",
118+
Pre: `sql := fmt.Sprintf("%s.%s = ?", f.table, f.field)
119+
`,
112120
Comment: `Only include Records with a field specific value`,
113-
Sql: `.Where(fmt.Sprintf("%s.%s = ?", f.table, f.field), arg)`,
121+
Sql: `.Where(sql, arg)`,
114122
Args: []string{"arg T"},
115123
},
116124
SelectFilter{
117-
Model: t,
118-
Name: "NotEqual",
125+
Model: t,
126+
Name: "NotEqual",
127+
Pre: `sql := fmt.Sprintf("%s.%s != ?", f.table, f.field)
128+
`,
119129
Comment: `Exclude Records with a field specific value`,
120-
Sql: `.Where(fmt.Sprintf("%s.%s != ?", f.table, f.field), arg)`,
130+
Sql: `.Where(sql, arg)`,
121131
Args: []string{"arg T"},
122132
},
123133

124134
SelectFilter{
125-
Model: t,
126-
Name: "IsNull",
135+
Model: t,
136+
Name: "IsNull",
137+
Pre: `sql := fmt.Sprintf("%s.%s IS NULL", f.table, f.field)
138+
`,
127139
Comment: `Only include Records with a field has undefined value`,
128-
Sql: `.Where(fmt.Sprintf("%s.%s IS NULL", f.table, f.field))`,
140+
Sql: `.Where(sql)`,
129141
Args: []string{},
130142
},
131143
SelectFilter{
132-
Model: t,
133-
Name: "IsNotNull",
144+
Model: t,
145+
Name: "IsNotNull",
146+
Pre: `sql := fmt.Sprintf("%s.%s IS NOT NULL", f.table, f.field)
147+
`,
134148
Comment: `Only include Records with a field has defined values`,
135-
Sql: `.Where(fmt.Sprintf("%s.%s IS NOT NULL", f.table, f.field))`,
149+
Sql: `.Where(sql)`,
136150
Args: []string{},
137151
},
138152
SelectFilter{
139-
Model: t,
140-
Name: "OrderAsc",
153+
Model: t,
154+
Name: "OrderAsc",
155+
Pre: `sql := fmt.Sprintf("%s.%s ASC", f.table, f.field)
156+
`,
141157
Comment: `Sort Records in ASC order`,
142-
Sql: `.OrderBy(fmt.Sprintf("%s.%s ASC", f.table, f.field))`,
158+
Sql: `.OrderBy(sql)`,
143159
Args: []string{},
144160
},
145161
SelectFilter{
146-
Model: t,
147-
Name: "OrderDesc",
162+
Model: t,
163+
Name: "OrderDesc",
164+
Pre: `sql := fmt.Sprintf("%s.%s DESC", f.table, f.field)
165+
`,
148166
Comment: `Sort Records in DESC order`,
149-
Sql: `.OrderBy(fmt.Sprintf("%s.%s DESC", f.table, f.field))`,
167+
Sql: `.OrderBy(sql)`,
150168
Args: []string{},
151169
},
152170
)
@@ -155,17 +173,21 @@ func GetFilterMethods(tables []*PostgresTable, driver string) []FilterMethod {
155173
if t == "FilterStringField" {
156174
method.Filters = append(method.Filters,
157175
SelectFilter{
158-
Model: t,
159-
Name: "Like",
176+
Model: t,
177+
Name: "Like",
178+
Pre: `sql := fmt.Sprintf("%s.%s LIKE ?", f.table, f.field)
179+
`,
160180
Comment: `Only include Records with a field contains a specific value (use % as wildcard)`,
161-
Sql: `.Where(fmt.Sprintf("%s.%s LIKE ?", f.table, f.field), arg)`,
181+
Sql: `.Where(sql, arg)`,
162182
Args: []string{"arg T"},
163183
},
164184
SelectFilter{
165-
Model: t,
166-
Name: "NotLike",
185+
Model: t,
186+
Name: "NotLike",
187+
Pre: `sql := fmt.Sprintf("%s.%s NOT LIKE ?", f.table, f.field)
188+
`,
167189
Comment: `Exclude Records with a field contains a specific value (use % as wildcard)`,
168-
Sql: `.Where(fmt.Sprintf("%s.%s NOT LIKE ?", f.table, f.field), arg)`,
190+
Sql: `.Where(sql, arg)`,
169191
Args: []string{"arg T"},
170192
},
171193
)
@@ -174,38 +196,48 @@ func GetFilterMethods(tables []*PostgresTable, driver string) []FilterMethod {
174196
if t == "FilterNumericField" {
175197
method.Filters = append(method.Filters,
176198
SelectFilter{
177-
Model: t,
178-
Name: "GreaterThan",
199+
Model: t,
200+
Name: "GreaterThan",
201+
Pre: `sql := fmt.Sprintf("%s.%s > ?", f.table, f.field)
202+
`,
179203
Comment: `Only include Records with a field greater than a specific value`,
180-
Sql: `.Where(fmt.Sprintf("%s.%s > ?", f.table, f.field), arg)`,
204+
Sql: `.Where(sql, arg)`,
181205
Args: []string{"arg T"},
182206
},
183207
SelectFilter{
184-
Model: t,
185-
Name: "GreaterThanOrEqual",
208+
Model: t,
209+
Name: "GreaterThanOrEqual",
210+
Pre: `sql := fmt.Sprintf("%s.%s >= ?", f.table, f.field)
211+
`,
186212
Comment: `Only include Records with a field greater or equal than a specific value`,
187-
Sql: `.Where(fmt.Sprintf("%s.%s >= ?", f.table, f.field), arg)`,
213+
Sql: `.Where(sql, arg)`,
188214
Args: []string{"arg T"},
189215
},
190216
SelectFilter{
191-
Model: t,
192-
Name: "LesserThan",
217+
Model: t,
218+
Name: "LesserThan",
219+
Pre: `sql := fmt.Sprintf("%s.%s < ?", f.table, f.field)
220+
`,
193221
Comment: `Only include Records with a field lesser than a specific value`,
194-
Sql: `.Where(fmt.Sprintf("%s.%s < ?", f.table, f.field), arg)`,
222+
Sql: `.Where(sql, arg)`,
195223
Args: []string{"arg T"},
196224
},
197225
SelectFilter{
198-
Model: t,
199-
Name: "LesserThanOrEqual",
226+
Model: t,
227+
Name: "LesserThanOrEqual",
228+
Pre: `sql := fmt.Sprintf("%s.%s <= ?", f.table, f.field)
229+
`,
200230
Comment: `Only include Records with a field lesser or equal than a specific value`,
201-
Sql: `.Where(fmt.Sprintf("%s.%s <= ?", f.table, f.field), arg)`,
231+
Sql: `.Where(sql, arg)`,
202232
Args: []string{"arg T"},
203233
},
204234
SelectFilter{
205-
Model: t,
206-
Name: "Between",
235+
Model: t,
236+
Name: "Between",
237+
Pre: `sql := fmt.Sprintf("%s.%s BETWEEN ? AND ?", f.table, f.field)
238+
`,
207239
Comment: `Only include Records with a field value in a specified range`,
208-
Sql: `.Where(fmt.Sprintf("%s.%s BETWEEN ? AND ?", f.table, f.field), from, to)`,
240+
Sql: `.Where(sql, from, to)`,
209241
Args: []string{"from T", "to T"},
210242
},
211243
)
@@ -214,10 +246,12 @@ func GetFilterMethods(tables []*PostgresTable, driver string) []FilterMethod {
214246
if t == "FilterArrayField" {
215247
method.Filters = append(method.Filters,
216248
SelectFilter{
217-
Model: t,
218-
Name: "Contains",
249+
Model: t,
250+
Name: "Contains",
251+
Pre: `sql := fmt.Sprintf("%s.%s @> ?", f.table, f.field)
252+
`,
219253
Comment: `Check if a array field contains a specific value`,
220-
Sql: `.Where(fmt.Sprintf("%s.%s @> ?", f.table, f.field), pq.Array(args))`,
254+
Sql: `.Where(sql, pq.Array(args))`,
221255
Args: []string{"args T"},
222256
},
223257
)

internal/database/templates/ctx_pgx.tmpl

Lines changed: 0 additions & 9 deletions
This file was deleted.

internal/database/templates/ctx_pq.tmpl

Lines changed: 0 additions & 10 deletions
This file was deleted.

internal/database/templates/factory_pgx.tmpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,8 @@ func limitFirst(cond SelectBuilder) SelectBuilder {
202202
return cond.Offset(0).Limit(1)
203203
}
204204

205+
type DBContext struct {
206+
db DbPgx
207+
tx pgx.Tx
208+
}
209+

internal/database/templates/factory_pq.tmpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,9 @@ func limitFirst(cond SelectBuilder) SelectBuilder {
187187
return cond.Offset(0).Limit(1)
188188
}
189189

190+
type DBContext struct {
191+
db *sqlx.DB
192+
tx *sqlx.Tx
193+
prepared map[string]*sqlx.Stmt
194+
}
195+

internal/database/templates/queries.tmpl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// // ...
66
// })
77
func (q *{{ .NameNormalized }}Queries) Insert(input {{ .NameNormalized }}Create) (*{{ .NameNormalized }}Model, error) {
8-
sql := `{{ .GetCreateSqlContent }}`
8+
const sql = `{{ .GetCreateSqlContent }}`
99
return QueryOne[{{ .NameNormalized }}Model](q.ctx, sql, {{ range .GetPrimaryKeyConstructors }} {{ .Init }}, {{ end }} {{ range .ColumnsCreate }}{{ if .IsArray }}pq.Array(input.{{ .NameNormalized }}), {{ else }}input.{{ .NameNormalized }}, {{ end }}{{ end }})
1010
}
1111

@@ -16,8 +16,8 @@ func (q *{{ .NameNormalized }}Queries) Insert(input {{ .NameNormalized }}Create)
1616
// // ...
1717
// })
1818
func (q *{{ .NameNormalized }}Queries) InsertMany(inputs []{{ .NameNormalized }}Create) ([]{{ .NameNormalized }}PrimaryKeySerialized, error) {
19-
sql := `{{ .GetCreateManySqlContent }}`
20-
ids := []{{ .NameNormalized }}PrimaryKeySerialized{}
19+
const sql = `{{ .GetCreateManySqlContent }}`
20+
ids := make([]{{ .NameNormalized }}PrimaryKeySerialized, 0, len(inputs))
2121
for _, chunk := range chunkBy(inputs, 250) {
2222
records := make([]{{ .NameNormalized }}Update, len(chunk))
2323
for i, input := range chunk {
@@ -50,7 +50,7 @@ func (q *{{ .NameNormalized }}Queries) InsertMany(inputs []{{ .NameNormalized }}
5050
// // ...
5151
// })
5252
func (q *{{ .NameNormalized }}Queries) Upsert(input {{ .NameNormalized }}Update) (*{{ .NameNormalized }}Model, error) {
53-
sql := `{{ .GetUpsertSqlContent }}`
53+
const sql = `{{ .GetUpsertSqlContent }}`
5454
return QueryOne[{{ .NameNormalized }}Model](q.ctx, sql, {{ range .ColumnsUpdate }}input.{{ .NameNormalized }}, {{ end }});
5555
}
5656

@@ -61,8 +61,8 @@ func (q *{{ .NameNormalized }}Queries) Upsert(input {{ .NameNormalized }}Update)
6161
// // ...
6262
// })
6363
func (q *{{ .NameNormalized }}Queries) UpsertMany(inputs []{{ .NameNormalized }}Update) ([]{{ .NameNormalized }}PrimaryKeySerialized, error) {
64-
sql := `{{ .GetUpsertManySqlContent }}`
65-
ids := []{{ .NameNormalized }}PrimaryKeySerialized{}
64+
const sql = `{{ .GetUpsertManySqlContent }}`
65+
ids := make([]{{ .NameNormalized }}PrimaryKeySerialized, 0, len(inputs))
6666
for _, chunk := range chunkBy(inputs, 250) {
6767
data, err := json.Marshal(chunk)
6868
if err != nil {
@@ -86,7 +86,7 @@ func (q *{{ .NameNormalized }}Queries) UpsertMany(inputs []{{ .NameNormalized }}
8686
// // ...
8787
// })
8888
func (q *{{ .NameNormalized }}Queries) Update(input {{ .NameNormalized }}Update) (*{{ .NameNormalized }}Model, error) {
89-
sql := `{{ .GetUpdateSqlContent }}`
89+
const sql = `{{ .GetUpdateSqlContent }}`
9090
return QueryOne[{{ .NameNormalized }}Model](q.ctx, sql, {{ range .ColumnsUpdate }}input.{{ .NameNormalized }}, {{ end }})
9191
}
9292

@@ -97,8 +97,8 @@ func (q *{{ .NameNormalized }}Queries) Update(input {{ .NameNormalized }}Update)
9797
// // ...
9898
// })
9999
func (q *{{ .NameNormalized }}Queries) UpdateMany(inputs []{{ .NameNormalized }}Update) ([]{{ .NameNormalized }}PrimaryKeySerialized, error) {
100-
sql := `{{ .GetUpdateManySqlContent }}`
101-
ids := []{{ .NameNormalized }}PrimaryKeySerialized{}
100+
const sql = `{{ .GetUpdateManySqlContent }}`
101+
ids := make([]{{ .NameNormalized }}PrimaryKeySerialized, 0, len(inputs))
102102
for _, chunk := range chunkBy(inputs, 250) {
103103
data, err := json.Marshal(chunk)
104104
if err != nil {

0 commit comments

Comments
 (0)