forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dao.go
159 lines (137 loc) · 3.73 KB
/
dao.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
// Package sql provides tools and DAOs for speaking SQL as well as managing tables migrations
package sql
import (
"database/sql"
"fmt"
"strings"
"sync"
"sync/atomic"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/dao"
)
var (
ErrNoRows = sql.ErrNoRows
)
// DAO interface definition
type DAO interface {
dao.DAO
DB() *sql.DB
Prepare(string, interface{}) error
GetStmt(string, ...interface{}) *sql.Stmt
UseExclusion()
Lock()
Unlock()
}
// Handler for the main functions of the DAO
type Handler struct {
dao.DAO
stmts map[string]*sql.Stmt
ifuncs map[string]func(...interface{}) string // TODO - replace next with this
funcs map[string]func(...string) string // Queries that need to be run before we get a statement
mu atomic.Value
replacer *strings.Replacer
}
func NewDAO(driver string, dsn string, prefix string) DAO {
conn, err := dao.NewConn(driver, dsn)
if err != nil {
return nil
}
// Special case for sqlite, we use a mutex to simulate locking as sqlite's locking is not quite up to the task
var mu atomic.Value
if driver == "sqlite3" {
mu.Store(&sync.Mutex{})
}
return &Handler{
DAO: dao.NewDAO(conn, driver, prefix),
stmts: make(map[string]*sql.Stmt),
ifuncs: make(map[string]func(...interface{}) string),
funcs: make(map[string]func(...string) string),
replacer: strings.NewReplacer("%%PREFIX%%", prefix, "%PREFIX%", prefix),
mu: mu,
}
}
func (h *Handler) Init(c config.Map) error {
return nil
}
// DB returns the sql DB object
func (h *Handler) DB() *sql.DB {
return h.GetConn().(*sql.DB)
}
// Prepare the statements that can be used by the DAO
func (h *Handler) Prepare(key string, query interface{}) error {
switch v := query.(type) {
case func(...interface{}) string:
h.ifuncs[key] = v
case func(...string) string:
h.funcs[key] = v
case string:
v = h.replacer.Replace(v)
stmt, err := h.DB().Prepare(v)
if err != nil {
return fmt.Errorf("Preparing statement : %v, %v", query, err)
}
h.stmts[key] = stmt
}
return nil
}
// GetStmts returns a list of all statements used by the dao
func (h *Handler) GetStmt(key string, args ...interface{}) *sql.Stmt {
if v, ok := h.stmts[key]; ok {
return v
}
if v, ok := h.ifuncs[key]; ok {
query := v(args...)
query = h.replacer.Replace(query)
stmt, err := h.DB().Prepare(query)
if err != nil {
fmt.Println(err)
return nil
}
return stmt
}
if v, ok := h.funcs[key]; ok {
var sargs []string
for _, s := range args {
sargs = append(sargs, fmt.Sprintf("%v", s))
}
query := v(sargs...)
query = h.replacer.Replace(query)
stmt, err := h.DB().Prepare(query)
if err != nil {
return nil
}
return stmt
}
return nil
}
func (h *Handler) UseExclusion() {
}
func (h *Handler) Lock() {
if current, ok := h.mu.Load().(*sync.Mutex); ok {
current.Lock()
}
}
func (h *Handler) Unlock() {
if current, ok := h.mu.Load().(*sync.Mutex); ok {
current.Unlock()
}
}