-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
catalog.go
58 lines (53 loc) · 2.14 KB
/
catalog.go
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
package zetasqlite
import (
"database/sql"
"fmt"
"reflect"
internal "github.com/goccy/go-zetasqlite/internal"
)
type (
ChangedCatalog = internal.ChangedCatalog
ChangedTable = internal.ChangedTable
ChangedFunction = internal.ChangedFunction
TableSpec = internal.TableSpec
FunctionSpec = internal.FunctionSpec
NameWithType = internal.NameWithType
ColumnSpec = internal.ColumnSpec
Type = internal.Type
)
// ChangedCatalogFromRows retrieve modified catalog information from sql.Rows.
// NOTE: This API relies on the internal structure of sql.Rows, so not will work for all Go versions.
func ChangedCatalogFromRows(rows *sql.Rows) (*ChangedCatalog, error) {
if rows == nil {
return nil, fmt.Errorf("zetasqlite: sql.Rows instance required not nil")
}
rv := reflect.ValueOf(rows)
rowsi := rv.Elem().FieldByName("rowsi")
if !rowsi.IsValid() {
return nil, fmt.Errorf("zetasqlite: unexpected sql.Rows layout")
}
driverValue := rowsi.Elem()
if driverValue.Type() != reflect.TypeOf(new(internal.Rows)) {
return nil, fmt.Errorf("zetasqlite: sql.Rows must be an instance created using the zetasqlite database driver")
}
zetasqliteRows := (*internal.Rows)(driverValue.UnsafePointer())
return zetasqliteRows.ChangedCatalog(), nil
}
// ChangedCatalogFromResult retrieve modified catalog information from sql.Result.
// NOTE: This API relies on the internal structure of sql.Result, so not will work for all Go versions.
func ChangedCatalogFromResult(result sql.Result) (*ChangedCatalog, error) {
rv := reflect.ValueOf(result)
if rv.Kind() != reflect.Struct {
return nil, fmt.Errorf("zetasqlite: unexpected sql.Result layout. expected sql.Result type is struct but got %T", result)
}
resi := rv.FieldByName("resi")
if !resi.IsValid() {
return nil, fmt.Errorf("zetasqlite: unexpected sql.Result layout")
}
driverValue := resi.Elem()
if driverValue.Type() != reflect.TypeOf(new(internal.Result)) {
return nil, fmt.Errorf("zetasqlite: sql.Result must be an instance created using the zetasqlite database driver")
}
zetasqliteResult := (*internal.Result)(driverValue.UnsafePointer())
return zetasqliteResult.ChangedCatalog(), nil
}