Skip to content

Commit 1a75e6c

Browse files
committed
Update immediately obvious spots where string id is required
1 parent 246f749 commit 1a75e6c

File tree

10 files changed

+178
-57
lines changed

10 files changed

+178
-57
lines changed

relationships/r_foreignkey_reverse.go

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ func (fkr ForeignKeyReverse) GetValues(
4242
if len(ids) == 0 {
4343
return map[string]interface{}{}, map[string]map[string][]string{}
4444
}
45-
filter := fmt.Sprintf("%s in (%s)", fkr.ColumnName, strings.Join(ids, ", "))
45+
args := []interface{}{}
46+
spots := []string{}
47+
for index, id := range ids {
48+
spots = append(spots, fmt.Sprintf("$%d", index + 1))
49+
args = append(args, id)
50+
}
51+
filter := fmt.Sprintf("%s in (%s)", fkr.ColumnName, strings.Join(spots, ", "))
4652

4753
server := c.GetServer()
4854
otherModel := server.GetModel(fkr.Type)
@@ -66,7 +72,7 @@ func (fkr ForeignKeyReverse) GetValues(
6672
filter,
6773
order,
6874
)
69-
rows, err := c.Query(query)
75+
rows, err := c.Query(query, args...)
7076
if err != nil {
7177
panic(err)
7278
}
@@ -277,12 +283,14 @@ func (fkr *ForeignKeyReverse) GetInsertQueries(newId string, val interface{}) []
277283
panic("Bad pointer set value")
278284
}
279285

280-
var ids []string
281-
for _, pointer := range fkrVal.Data {
282-
ids = append(ids, *pointer.ID)
286+
spots := []string{}
287+
args := []interface{}{}
288+
for index, pointer := range fkrVal.Data {
289+
spots = append(spots, fmt.Sprintf("$%d", index + 2))
290+
args = append(args, *pointer.ID)
283291
}
284292

285-
if len(ids) == 0 {
293+
if len(spots) == 0 {
286294
return []schema.Query{}
287295
}
288296

@@ -291,14 +299,12 @@ func (fkr *ForeignKeyReverse) GetInsertQueries(newId string, val interface{}) []
291299
fkr.SourceTable,
292300
fkr.ColumnName,
293301
fkr.SourceIDColumn,
294-
strings.Join(ids, ", "),
302+
strings.Join(spots, ", "),
295303
)
296304
return []schema.Query{
297305
schema.Query{
298306
Query: query,
299-
Args: []interface{}{
300-
newId,
301-
},
307+
Args: append([]interface{}{newId}, args...),
302308
},
303309
}
304310
}
@@ -324,15 +330,21 @@ func (fkr *ForeignKeyReverse) GetUpdateQueries(id string, oldVal interface{}, ne
324330
}
325331
}
326332
if len(nulling) > 0 {
333+
spots := []string{}
334+
args := []interface{}{}
335+
for index, id := range nulling {
336+
spots = append(spots, fmt.Sprintf("$%d", index + 1))
337+
args = append(args, id)
338+
}
327339
queries = append(queries, schema.Query{
328340
Query: fmt.Sprintf(
329341
"update %s set %s = null where %s in (%s)",
330342
fkr.SourceTable,
331343
fkr.ColumnName,
332344
fkr.SourceIDColumn,
333-
strings.Join(nulling, ", "),
345+
strings.Join(spots, ", "),
334346
),
335-
Args: []interface{}{},
347+
Args: args,
336348
})
337349
}
338350

@@ -345,16 +357,22 @@ func (fkr *ForeignKeyReverse) GetUpdateQueries(id string, oldVal interface{}, ne
345357
}
346358
}
347359
if len(adding) > 0 {
360+
spots := []string{}
361+
args := []interface{}{}
362+
for index, id := range adding {
363+
spots = append(spots, fmt.Sprintf("$%d", index + 1))
364+
args = append(args, id)
365+
}
348366
queries = append(queries, schema.Query{
349367
Query: fmt.Sprintf(
350368
"update %s set %s = %s where %s in (%s)",
351369
fkr.SourceTable,
352370
fkr.ColumnName,
353371
id,
354372
fkr.SourceIDColumn,
355-
strings.Join(adding, ", "),
373+
strings.Join(spots, ", "),
356374
),
357-
Args: []interface{}{},
375+
Args: args,
358376
})
359377
}
360378

relationships/r_foreignkey_reverse_filters.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,29 @@ func (f ForeignKeyReverseContainsFilter) GetWhere(
5757

5858
if f.exclude {
5959
if len(ids) > 0 {
60+
spots := []string{}
61+
args := []interface{}{}
62+
for index, id := range ids {
63+
spots = append(spots, fmt.Sprintf("$%d", index + 1))
64+
args = append(args, id)
65+
}
6066
return []string{
61-
fmt.Sprintf("%s not in (%s)", idColumn, strings.Join(ids, ", ")),
62-
}, []interface{}{}
67+
fmt.Sprintf("%s not in (%s)", idColumn, strings.Join(spots, ", ")),
68+
}, args
6369
} else {
6470
return []string{}, []interface{}{}
6571
}
6672
} else {
6773
if len(ids) > 0 {
74+
spots := []string{}
75+
args := []interface{}{}
76+
for index, id := range ids {
77+
spots = append(spots, fmt.Sprintf("$%d", index + 1))
78+
args = append(args, id)
79+
}
6880
return []string{
69-
fmt.Sprintf("%s in (%s)", idColumn, strings.Join(ids, ", ")),
70-
}, []interface{}{}
81+
fmt.Sprintf("%s in (%s)", idColumn, strings.Join(spots, ", ")),
82+
}, args
7183
} else {
7284
return []string{"true is false"}, []interface{}{}
7385
}
@@ -140,9 +152,15 @@ func (f ForeignKeyReverseCountFilter) GetWhere(
140152
}
141153

142154
if len(ids) > 0 {
155+
spots := []string{}
156+
args := []interface{}{}
157+
for index, id := range ids {
158+
spots = append(spots, fmt.Sprintf("$%d", index + 1))
159+
args = append(args, id)
160+
}
143161
return []string{
144-
fmt.Sprintf("%s in (%s)", idColumn, strings.Join(ids, ", ")),
145-
}, []interface{}{}
162+
fmt.Sprintf("%s in (%s)", idColumn, strings.Join(spots, ", ")),
163+
}, args
146164
} else {
147165
return []string{"true is false"}, []interface{}{}
148166
}

relationships/r_genericforeignkey_reverse.go

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ func (gfkr GenericForeignKeyReverse) GetValues(
5252
panic(err)
5353
}
5454

55-
idFilter := fmt.Sprintf("%s in (%s)", gfkr.OwnIDColumn, strings.Join(ids, ", "))
55+
spots := []string{}
56+
args := []interface{}{}
57+
for index, id := range ids {
58+
spots = append(spots, fmt.Sprintf("$%d", index + 2))
59+
args = append(args, id)
60+
}
61+
idFilter := fmt.Sprintf("%s in (%s)", gfkr.OwnIDColumn, strings.Join(spots, ", "))
5662
typeFilter := fmt.Sprintf("%s = $1", gfkr.OwnTypeColumn)
5763
query := fmt.Sprintf(
5864
`
@@ -70,7 +76,7 @@ func (gfkr GenericForeignKeyReverse) GetValues(
7076
typeFilter,
7177
order,
7278
)
73-
rows, err := c.Query(query, gfkr.OwnType)
79+
rows, err := c.Query(query, append([]interface{}{gfkr.OwnType}, args...)...)
7480
if err != nil {
7581
panic(err)
7682
}
@@ -290,12 +296,14 @@ func (gfkr *GenericForeignKeyReverse) GetInsertQueries(
290296
panic("Bad pointer set value")
291297
}
292298

293-
var ids []string
294-
for _, pointer := range gfkrVal.Data {
295-
ids = append(ids, *pointer.ID)
299+
spots := []string{}
300+
args := []interface{}{}
301+
for index, pointer := range gfkrVal.Data {
302+
spots = append(spots, fmt.Sprintf("$%d", index + 3))
303+
args = append(args, *pointer.ID)
296304
}
297305

298-
if len(ids) == 0 {
306+
if len(spots) == 0 {
299307
return []schema.Query{}
300308
}
301309

@@ -305,15 +313,15 @@ func (gfkr *GenericForeignKeyReverse) GetInsertQueries(
305313
gfkr.OwnTypeColumn,
306314
gfkr.OwnIDColumn,
307315
gfkr.OtherIDColumn,
308-
strings.Join(ids, ", "),
316+
strings.Join(spots, ", "),
309317
)
310318
return []schema.Query{
311319
schema.Query{
312320
Query: query,
313-
Args: []interface{}{
321+
Args: append([]interface{}{
314322
gfkr.OwnType,
315323
newId,
316-
},
324+
}, args...),
317325
},
318326
}
319327
}
@@ -339,16 +347,22 @@ func (gfkr *GenericForeignKeyReverse) GetUpdateQueries(id string, oldVal interfa
339347
}
340348
}
341349
if len(nulling) > 0 {
350+
spots := []string{}
351+
args := []interface{}{}
352+
for index, id := range nulling {
353+
spots = append(spots, fmt.Sprintf("$%d", index + 1))
354+
args = append(args, id)
355+
}
342356
queries = append(queries, schema.Query{
343357
Query: fmt.Sprintf(
344358
"update %s set (%s = null, %s = null) where %s in (%s)",
345359
gfkr.Table,
346360
gfkr.OwnIDColumn,
347361
gfkr.OwnTypeColumn,
348362
gfkr.OtherIDColumn,
349-
strings.Join(nulling, ", "),
363+
strings.Join(spots, ", "),
350364
),
351-
Args: []interface{}{},
365+
Args: args,
352366
})
353367
}
354368

@@ -361,6 +375,12 @@ func (gfkr *GenericForeignKeyReverse) GetUpdateQueries(id string, oldVal interfa
361375
}
362376
}
363377
if len(adding) > 0 {
378+
spots := []string{}
379+
args := []interface{}{}
380+
for index, id := range adding {
381+
spots = append(spots, fmt.Sprintf("$%d", index + 2))
382+
args = append(args, id)
383+
}
364384
queries = append(queries, schema.Query{
365385
Query: fmt.Sprintf(
366386
"update %s set %s = %s, %s = $1 where %s in (%s)",
@@ -369,11 +389,11 @@ func (gfkr *GenericForeignKeyReverse) GetUpdateQueries(id string, oldVal interfa
369389
id,
370390
gfkr.OwnTypeColumn,
371391
gfkr.OtherIDColumn,
372-
strings.Join(adding, ", "),
392+
strings.Join(spots, ", "),
373393
),
374-
Args: []interface{}{
394+
Args: append([]interface{}{
375395
gfkr.OwnType,
376-
},
396+
}, args...),
377397
})
378398
}
379399

relationships/r_genericforeignkey_reverse_filters.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,29 @@ func (f GenericForeignKeyReverseContainsFilter) GetWhere(
6363

6464
if f.exclude {
6565
if len(ids) > 0 {
66+
spots := []string{}
67+
args := []interface{}{}
68+
for index, id := range ids {
69+
spots = append(spots, fmt.Sprintf("$%d", index + 1))
70+
args = append(args, id)
71+
}
6672
return []string{
67-
fmt.Sprintf("%s not in (%s)", idColumn, strings.Join(ids, ", ")),
68-
}, []interface{}{}
73+
fmt.Sprintf("%s not in (%s)", idColumn, strings.Join(spots, ", ")),
74+
}, args
6975
} else {
7076
return []string{}, []interface{}{}
7177
}
7278
} else {
7379
if len(ids) > 0 {
80+
spots := []string{}
81+
args := []interface{}{}
82+
for index, id := range ids {
83+
spots = append(spots, fmt.Sprintf("$%d", index + 1))
84+
args = append(args, id)
85+
}
7486
return []string{
75-
fmt.Sprintf("%s in (%s)", idColumn, strings.Join(ids, ", ")),
76-
}, []interface{}{}
87+
fmt.Sprintf("%s in (%s)", idColumn, strings.Join(spots, ", ")),
88+
}, args
7789
} else {
7890
return []string{"true is false"}, []interface{}{}
7991
}
@@ -153,9 +165,15 @@ func (f GenericForeignKeyReverseCountFilter) GetWhere(
153165
}
154166

155167
if len(ids) > 0 {
168+
spots := []string{}
169+
args := []interface{}{}
170+
for index, id := range ids {
171+
spots = append(spots, fmt.Sprintf("$%d", index + 1))
172+
args = append(args, id)
173+
}
156174
return []string{
157-
fmt.Sprintf("%s in (%s)", idColumn, strings.Join(ids, ", ")),
158-
}, []interface{}{}
175+
fmt.Sprintf("%s in (%s)", idColumn, strings.Join(spots, ", ")),
176+
}, args
159177
} else {
160178
return []string{"true is false"}, []interface{}{}
161179
}

relationships/r_manytomany.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ func (m2m ManyToMany) GetValues(
4444

4545
server := c.GetServer()
4646
otherModel := server.GetModel(m2m.OtherType)
47+
if otherModel == nil {
48+
panic(fmt.Sprintf("Invalid other model %s", m2m.OtherType))
49+
}
4750
order, _, err := otherModel.GetOrderQuery(otherModel.DefaultOrder)
4851
if err != nil {
4952
panic(err)
@@ -78,7 +81,13 @@ func (m2m ManyToMany) GetValues(
7881
}
7982
}
8083

81-
filter := fmt.Sprintf("%s in (%s)", m2m.OwnIDColumn, strings.Join(ids, ", "))
84+
spots := []string{}
85+
args := []interface{}{}
86+
for index, id := range ids {
87+
spots = append(spots, fmt.Sprintf("$%d", index + 1))
88+
args = append(args, id)
89+
}
90+
filter := fmt.Sprintf("%s in (%s)", m2m.OwnIDColumn, strings.Join(spots, ", "))
8291
query := fmt.Sprintf(
8392
`
8493
select
@@ -110,7 +119,7 @@ func (m2m ManyToMany) GetValues(
110119
m2m.OtherIDColumn,
111120
orderQuery,
112121
)
113-
rows, err := c.Query(query)
122+
rows, err := c.Query(query, args...)
114123
if err != nil {
115124
panic(err)
116125
}
@@ -350,17 +359,23 @@ func (m2m *ManyToMany) GetUpdateQueries(
350359
}
351360
}
352361
if len(nulling) > 0 {
362+
spots := []string{}
363+
args := []interface{}{}
364+
for index, id := range nulling {
365+
spots = append(spots, fmt.Sprintf("$%d", index + 2))
366+
args = append(args, id)
367+
}
353368
queries = append(queries, schema.Query{
354369
Query: fmt.Sprintf(
355370
"delete from %s where %s = $1 and %s in (%s)",
356371
m2m.Table,
357372
m2m.OwnIDColumn,
358373
m2m.OtherIDColumn,
359-
strings.Join(nulling, ", "),
374+
strings.Join(spots, ", "),
360375
),
361-
Args: []interface{}{
376+
Args: append([]interface{}{
362377
id,
363-
},
378+
}, args...),
364379
})
365380
}
366381

0 commit comments

Comments
 (0)