Skip to content

Commit c4dd193

Browse files
committed
fix pgsql cast
1 parent 8ed634e commit c4dd193

File tree

4 files changed

+53
-48
lines changed

4 files changed

+53
-48
lines changed

_example/go-gen/model_orm.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

orm/orm_stmt_exec.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ func (v *exec) Params(args ...any) {
3232

3333
switch v.D {
3434
case PgSQLDialect:
35-
v.P = append(v.P, pgCastTypes(args))
35+
applyPGSqlCastTypes(args)
3636
default:
37-
v.P = append(v.P, args)
3837
}
38+
39+
v.P = append(v.P, args)
3940
}
4041

4142
func (v *exec) Bind(call func(rowsAffected, lastInsertId int64) error) {

orm/orm_stmt_query.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ type query struct {
2323
func (v *query) SQL(query string, args ...any) {
2424
switch v.D {
2525
case PgSQLDialect:
26-
v.Q, v.P = query, pgCastTypes(args)
26+
applyPGSqlCastTypes(args)
2727
default:
28-
v.Q, v.P = query, args
2928
}
29+
30+
v.Q, v.P = query, args
3031
}
3132

3233
func (v *query) Bind(call func(bind Scanner) error) {
@@ -45,10 +46,11 @@ type scan struct {
4546
func (v *scan) Scan(args ...any) error {
4647
switch v.D {
4748
case PgSQLDialect:
48-
return v.S.Scan(pgCastTypes(args)...)
49+
applyPGSqlCastTypes(args)
4950
default:
50-
return v.S.Scan(args...)
5151
}
52+
53+
return v.S.Scan(args...)
5254
}
5355

5456
type (

orm/types.go

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,28 @@
66
package orm
77

88
import (
9+
"database/sql"
910
"database/sql/driver"
1011
"encoding/json"
1112
"fmt"
13+
"reflect"
14+
"time"
1215

1316
"github.com/lib/pq"
1417
)
1518

19+
type (
20+
TypeScanValuer interface {
21+
driver.Valuer
22+
sql.Scanner
23+
}
24+
25+
TypeJSONb interface {
26+
json.Marshaler
27+
json.Unmarshaler
28+
}
29+
)
30+
1631
type jsonb struct {
1732
Any any
1833
}
@@ -48,49 +63,35 @@ func (jb *jsonb) Value() (driver.Value, error) {
4863
return driver.Value(b), nil
4964
}
5065

51-
func pgCastTypes(args []any) []any {
52-
out := make([]any, 0, len(args))
53-
for _, arg := range args {
54-
out = append(out, pgCastType(arg))
55-
}
56-
return out
57-
}
66+
func applyPGSqlCastTypes(args []any) {
67+
count := len(args)
5868

59-
func pgCastType(arg any) any {
60-
switch a := arg.(type) {
61-
case json.Marshaler, json.Unmarshaler:
62-
return &jsonb{Any: a}
69+
for i := 0; i < count; i++ {
70+
if reflect.ValueOf(args[i]).Kind() == reflect.Slice {
71+
args[i] = pq.Array(args[i])
72+
continue
73+
}
6374

64-
case []bool:
65-
return (*pq.BoolArray)(&a)
66-
case []float64:
67-
return (*pq.Float64Array)(&a)
68-
case []float32:
69-
return (*pq.Float32Array)(&a)
70-
case []int64:
71-
return (*pq.Int64Array)(&a)
72-
case []int32:
73-
return (*pq.Int32Array)(&a)
74-
case []string:
75-
return (*pq.StringArray)(&a)
76-
case [][]byte:
77-
return (*pq.ByteaArray)(&a)
78-
79-
case *[]bool:
80-
return (*pq.BoolArray)(a)
81-
case *[]float64:
82-
return (*pq.Float64Array)(a)
83-
case *[]float32:
84-
return (*pq.Float32Array)(a)
85-
case *[]int64:
86-
return (*pq.Int64Array)(a)
87-
case *[]int32:
88-
return (*pq.Int32Array)(a)
89-
case *[]string:
90-
return (*pq.StringArray)(a)
91-
case *[][]byte:
92-
return (*pq.ByteaArray)(a)
75+
switch args[i].(type) {
76+
case TypeScanValuer:
77+
// ---
78+
case []byte, string, *[]byte, *string:
79+
// ---
80+
case int, int8, int16, int32, int64, *int, *int8, *int16, *int32, *int64:
81+
// ---
82+
case uint, uint8, uint16, uint32, uint64, *uint, *uint8, *uint16, *uint32, *uint64:
83+
// ---
84+
case float32, float64, *float32, *float64:
85+
// ---
86+
case bool, *bool:
87+
// ---
88+
case complex64, complex128, *complex64, *complex128:
89+
// ---
90+
case time.Time, *time.Time:
91+
// ---
92+
case TypeJSONb:
93+
args[i] = &jsonb{Any: args[i]}
94+
default:
95+
}
9396
}
94-
95-
return arg
9697
}

0 commit comments

Comments
 (0)