-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
148 lines (122 loc) · 3.01 KB
/
db.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
package db
import (
"database/sql"
"errors"
_ "github.com/go-sql-driver/mysql"
)
var (
NotInit = errors.New("db wasn't initialized")
AlreadyInit = errors.New("db already initialized")
)
var dbObj *sql.DB
func Ping() error {
return dbObj.Ping()
}
func Open() (err error) {
if dbObj != nil {
return AlreadyInit
}
dbObj, err = sql.Open("mysql", username+":"+password+"@tcp("+host+":"+port+")/")
if err != nil {
return
}
err = dbObj.Ping()
return
}
func Close() error {
return dbObj.Close()
}
func QueryRow(query string, args ...interface{}) (*sql.Row, error) {
if dbObj == nil {
return nil, NotInit
}
return dbObj.QueryRow(query, args...), nil
}
func Query(query string, args ...interface{}) (*sql.Rows, error) {
if dbObj == nil {
return nil, NotInit
}
return dbObj.Query(query, args...)
}
func Exec(query string, args ...interface{}) (sql.Result, error) {
if dbObj == nil {
var emptyResult sql.Result
return emptyResult, NotInit
}
return dbObj.Exec(query, args...)
}
func isExists(dbName string, tableName string, where string, args ...interface{}) (id int64, err error) {
row, err := findRowBy(dbName, tableName, "id", where, args...)
if err != nil {
return
}
err = row.Scan(&id)
if err != nil {
if err == sql.ErrNoRows {
return 0, nil
}
return
}
return id, nil
}
func insert(dbName string, tableName string, cols string, values string, args ...interface{}) (int64, error) {
result, err := Exec("INSERT INTO "+dbName+"."+tableName+" ("+cols+") VALUES ("+values+")", args...)
if err != nil {
return 0, err
}
return result.LastInsertId()
}
func findRowBy(dbName string, tableName string, cols string, where string, args ...interface{}) (*sql.Row, error) {
if where == "" {
where = "1"
}
return QueryRow("SELECT "+cols+" FROM "+dbName+"."+tableName+" WHERE "+where, args...)
}
// For future use
//
// func findRowsBy(dbName string, tableName string, cols string, where string, args ...interface{}) (*sql.Rows, error) {
// if dbObj == nil {
// return nil, NotInit
// }
//
// if where == "" {
// where = "1"
// }
// return Query("SELECT "+cols+" FROM "+dbName+"."+tableName+" WHERE "+where, args...)
// }
func updateBy(dbName string, tableName string, set string, where string, args ...interface{}) (int64, error) {
if dbObj == nil {
return 0, NotInit
}
if where == "" {
where = "1"
}
result, err := Exec("UPDATE "+dbName+"."+tableName+" SET "+set+" WHERE "+where, args...)
if err != nil {
return 0, err
}
return result.RowsAffected()
}
func removeBy(dbName string, tableName string, where string, args ...interface{}) (int64, error) {
if dbObj == nil {
return 0, NotInit
}
if where == "" {
where = "1"
}
result, err := Exec("DELETE FROM "+dbName+"."+tableName+" WHERE "+where, args...)
if err != nil {
return 0, err
}
return result.RowsAffected()
}
func truncate(dbName string, tableName string) error {
if dbObj == nil {
return NotInit
}
_, err := Exec("TRUNCATE TABLE " + dbName + "." + tableName)
if err != nil {
return err
}
return nil
}