-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
88 lines (68 loc) · 2.51 KB
/
functions.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
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
package fs
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2023 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"database/sql"
"fmt"
"path"
"strings"
sqlite3 "github.com/mattn/go-sqlite3"
)
// ////////////////////////////////////////////////////////////////////////////////// //
// customFunc contains inforamation about custom function
type customFunc struct {
Name string
Impl interface{}
IsPure bool
}
// ////////////////////////////////////////////////////////////////////////////////// //
var customFuncs = map[string][]*customFunc{}
var hasCustomDriver = map[string]bool{}
// ////////////////////////////////////////////////////////////////////////////////// //
// RegisterFunc registers new custom function for given DB type
// Notice that you can not register new functions after creating new storage
func RegisterFunc(dbType, name string, impl interface{}, pure bool) error {
if len(hasCustomDriver) != 0 {
return fmt.Errorf("Can't register new custom function after creating storage")
}
customFuncs[dbType] = append(customFuncs[dbType], &customFunc{
Name: name, Impl: impl, IsPure: pure,
})
return nil
}
// ////////////////////////////////////////////////////////////////////////////////// //
// registerDrivers registers drivers with all custom functions
func registerDrivers() {
for dbType, funcs := range customFuncs {
sql.Register("sqlite3_"+dbType,
&sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
for _, f := range funcs {
conn.RegisterFunc(f.Name, f.Impl, f.IsPure)
}
return nil
},
},
)
hasCustomDriver[dbType] = true
}
customFuncs = nil
}
// ////////////////////////////////////////////////////////////////////////////////// //
// filelistGlobberFunc is special function for checking glob
func filelistGlobberFunc(glob, dir, files string, isNegative int) bool {
for _, file := range strings.Split(files, "/") {
isMatch, _ := path.Match(glob, dir+"/"+file)
switch {
case isNegative == 1 && isMatch:
return false
case isNegative == 0 && isMatch:
return true
}
}
return isNegative == 1
}