-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathany.go
More file actions
88 lines (75 loc) · 2.91 KB
/
any.go
File metadata and controls
88 lines (75 loc) · 2.91 KB
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
/*
Copyright 2026 Olivier Mengué
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 sqlfunc
import (
"context"
"database/sql"
"reflect"
)
// AnyAPI provides a more flexible API, less strictly typed than the main API,
// at the cost of little more runtime overhead and less compile-time safety.
// It is intended for use cases where the main API cannot be used, such as when
// the function signature is not known at compile time.
type AnyAPI [0]struct{}
// Any provides a more flexible API, less strictly typed than the main API,
// at the cost of little more runtime overhead and less compile-time safety.
// It is intended for use cases where the main API cannot be used, such as when
// the function signature is not known at compile time.
var Any AnyAPI
func checkFnPtr(fnPtr any) reflect.Type {
fnValue := reflect.ValueOf(fnPtr)
if fnValue.Kind() != reflect.Pointer {
panic("fnPtr must be a pointer to a *func* variable")
}
if fnValue.IsNil() {
panic("fnPtr must be non-nil")
}
return fnValue.Type().Elem()
}
// ForEach is same as [ForEach].
func (AnyAPI) ForEach(rows *sql.Rows, callback any) error {
fnType := reflect.TypeOf(callback)
if fnType.Kind() != reflect.Func {
panic("callback must be a func")
}
if reflect.ValueOf(callback).IsNil() {
panic("callback must be non-nil")
}
f := registryForEach(fnType)
switch f := f.(type) {
case func(any) func(rows *sql.Rows) error:
return runForEach(rows, f(callback))
case nil: // not in cache
return dynamicForEach(rows, fnType, callback)
default:
// call f with callback as argument
scanRow := reflect.ValueOf(f).Call([]reflect.Value{reflect.ValueOf(callback)})[0].Interface().(func(rows *sql.Rows) error)
return runForEach(rows, scanRow)
}
}
// Scan is same as [Scan].
func (AnyAPI) Scan(fnPtr any) {
doScan(checkFnPtr(fnPtr), fnPtr)
}
// Exec is same as [Exec].
func (AnyAPI) Exec(ctx context.Context, db PrepareConn, query string, fnPtr any) (close func() error, err error) {
return makeStmtFuncAny(ctx, db, query, fnPtr, checkExec, makeExec)
}
// QueryRow is same as [QueryRow].
func (AnyAPI) QueryRow(ctx context.Context, db PrepareConn, query string, fnPtr any) (close func() error, err error) {
return makeStmtFuncAny(ctx, db, query, fnPtr, checkQueryRow, makeQueryRow)
}
// Query is same as [Query].
func (AnyAPI) Query(ctx context.Context, db PrepareConn, query string, fnPtr any) (close func() error, err error) {
return makeStmtFuncAny(ctx, db, query, fnPtr, checkQuery, makeQuery)
}