mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 19:36:44 +01:00
Replace all []*Foo by []Foo in sql db return values
- Implement SlicePtrsToSlice and use it in all `meddler.QueryAll` sql db functions to always return []Foo instead of []*Foo
This commit is contained in:
29
db/utils.go
29
db/utils.go
@@ -52,7 +52,10 @@ func initMeddler() {
|
||||
|
||||
// BulkInsert performs a bulk insert with a single statement into the specified table. Example:
|
||||
// `db.BulkInsert(myDB, "INSERT INTO block (eth_block_num, timestamp, hash) VALUES %s", blocks[:])`
|
||||
// Note that all the columns must be specified in the query, and they must be in the same order as in the table.
|
||||
// Note that all the columns must be specified in the query, and they must be
|
||||
// in the same order as in the table.
|
||||
// Note that the fields in the structs need to be defined in the same order as
|
||||
// in the table columns.
|
||||
func BulkInsert(db meddler.DB, q string, args interface{}) error {
|
||||
arrayValue := reflect.ValueOf(args)
|
||||
arrayLen := arrayValue.Len()
|
||||
@@ -150,3 +153,27 @@ func (b BigIntNullMeddler) PreWrite(fieldPtr interface{}) (saveValue interface{}
|
||||
}
|
||||
return base64.StdEncoding.EncodeToString(field.Bytes()), nil
|
||||
}
|
||||
|
||||
// SliceToSlicePtrs converts any []Foo to []*Foo
|
||||
func SliceToSlicePtrs(slice interface{}) interface{} {
|
||||
v := reflect.ValueOf(slice)
|
||||
vLen := v.Len()
|
||||
typ := v.Type().Elem()
|
||||
res := reflect.MakeSlice(reflect.SliceOf(reflect.PtrTo(typ)), vLen, vLen)
|
||||
for i := 0; i < vLen; i++ {
|
||||
res.Index(i).Set(v.Index(i).Addr())
|
||||
}
|
||||
return res.Interface()
|
||||
}
|
||||
|
||||
// SlicePtrsToSlice converts any []*Foo to []Foo
|
||||
func SlicePtrsToSlice(slice interface{}) interface{} {
|
||||
v := reflect.ValueOf(slice)
|
||||
vLen := v.Len()
|
||||
typ := v.Type().Elem().Elem()
|
||||
res := reflect.MakeSlice(reflect.SliceOf(typ), vLen, vLen)
|
||||
for i := 0; i < vLen; i++ {
|
||||
res.Index(i).Set(v.Index(i).Elem())
|
||||
}
|
||||
return res.Interface()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user