-
Notifications
You must be signed in to change notification settings - Fork 124
/
database.go
127 lines (106 loc) · 3.05 KB
/
database.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
/*
* This file is part of The AnnChain.
*
* The AnnChain is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The AnnChain 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The www.annchain.io. If not, see <http://www.gnu.org/licenses/>.
*/
package database
import (
"database/sql"
"errors"
"github.com/spf13/viper"
"go.uber.org/zap"
//"github.com/dappledger/AnnChain/module/lib/go-config"
)
const (
DBTypeSQLite3 = "sqlite3"
DBTypeMySQL = "mysql" // TODO
)
// Feild database feild
type Feild struct {
Name string
Value interface{}
}
// Where query feild
type Where struct {
Name string
Value interface{}
Op string // can be =、>、<、<> and any operator supported by sql-database
}
// GetOp get operator of current where clause, default =
func (w *Where) GetOp() string {
if w.Op == "" {
return "="
}
return w.Op
}
// Order used to identify query order
type Order struct {
Type string // "asc" or "desc"
Feilds []string // order by x
}
// GetOp used in sql
func (o *Order) GetOp() string {
if o != nil && o.Type == "desc" {
return "<="
}
return ">="
}
type Paging struct {
CursorName string // cursor column
CursorValue uint64 // cursor column
Limit uint // limit
}
// Database interface for app database-operation
type Database interface {
Init(dbname string, cfg *viper.Viper, logger *zap.Logger) error
Close()
InitTables(ctsqls, cisqls []string) error
Count(table string, where []Where) (int, error)
Insert(table string, feilds []Feild) (sql.Result, error)
Replace(table string, feilds []Feild) (sql.Result, error) // insert or replace
Delete(table string, where []Where) (sql.Result, error)
Update(table string, toupdate []Feild, where []Where) (sql.Result, error)
SelectRows(table string, where []Where, order *Order, paging *Paging, result interface{}) error
SelectRowsOffset(table string, where []Where, order *Order, offset, limit uint64, result interface{}) error
Begin() error
Commit() error
Rollback() error
}
//MakeOrder make a order object
func MakeOrder(ordertype string, feilds ...string) (*Order, error) {
if ordertype == "" {
ordertype = "asc"
}
if ordertype != "asc" && ordertype != "ASC" && ordertype != "desc" && ordertype != "DESC" {
return nil, errors.New("invalid order type :" + ordertype)
}
return &Order{
Type: ordertype,
Feilds: feilds,
}, nil
}
// MakePaging make a paging object
func MakePaging(colName string, colValue uint64, limit uint) *Paging {
if limit == 0 {
limit = 10
}
if limit > 200 {
limit = 200
}
return &Paging{
CursorName: colName,
CursorValue: colValue,
Limit: limit,
}
}