-
Notifications
You must be signed in to change notification settings - Fork 32
/
DbConnection.go
136 lines (105 loc) · 4.28 KB
/
DbConnection.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
package main
import (
"strings"
"database/sql"
_ "github.com/go-sql-driver/mysql"
"github.com/golang/glog"
)
type DBConnection struct {
DbHandle *sql.DB
}
type SubmitAuxBlockInfo struct {
ChainName string
AuxBlockTableName string
ParentChainBllockHash string
AuxChainBlockHash string
AuxPow string
CurrentTime string
SubmitResponse string
IsSubmitSuccess bool
}
func (handle *DBConnection) InitDB(conf DBConnectionInfo) {
path := strings.Join([]string{conf.Username, ":", conf.Password, "@tcp(",conf.Host, ":", conf.Port, ")/", conf.Dbname, "?charset=utf8"}, "")
glog.Info("dbpath : " + path )
handle.DbHandle, _ = sql.Open("mysql", path)
handle.DbHandle.SetConnMaxLifetime(100)
handle.DbHandle.SetMaxIdleConns(10)
if err := handle.DbHandle.Ping(); err != nil{
glog.Info("opon database fail : %s", err)
return
}
glog.Info("connnect success")
}
func (handle *DBConnection) InsertAuxBlock(blockinfo SubmitAuxBlockInfo) (bool){
iscolumnexistsql := "SELECT COUNT(*) FROM information_schema.columns WHERE table_name = '"+ blockinfo.AuxBlockTableName
iscolumnexistsql += "' and column_name = 'submit_response'"
var count int
result := handle.DbHandle.QueryRow(iscolumnexistsql).Scan(&count)
if result != nil{
glog.Info("Exec fail : ", iscolumnexistsql, result)
}
glog.Info("Number of rows are ", count)
tx, err := handle.DbHandle.Begin()
if err != nil{
glog.Info("tx fail")
return false
}
var sql string
if count != 0 {
sql = "INSERT INTO " + blockinfo.AuxBlockTableName
sql += " (`bitcoin_block_hash`,`aux_block_hash`, `aux_pow`,`created_at`, `submit_response`, `chain_name`) "
sql += " values(?,?,?,?,?,?)"
res, err := tx.Exec(sql,blockinfo.ParentChainBllockHash,blockinfo.AuxChainBlockHash,blockinfo.AuxPow, blockinfo.CurrentTime, blockinfo.SubmitResponse, blockinfo.ChainName)
if err != nil{
glog.Info("Exec fail : ", err)
if blockinfo.IsSubmitSuccess {
glog.Info("because auxblock submited successfully, we need update submit_response")
updatesql := "UPDATE "+ blockinfo.AuxBlockTableName + " SET submit_response=? WHERE aux_block_hash=?"
_, fail := tx.Exec(updatesql, blockinfo.SubmitResponse, blockinfo.AuxChainBlockHash)
if fail != nil {
glog.Info( updatesql ,"Exec fail : ", fail)
} else {
tx.Commit()
return true;
}
}
tx.Commit()
return false
}
glog.Info(res.LastInsertId())
} else {
sql = "INSERT INTO " + blockinfo.AuxBlockTableName
sql += " (`bitcoin_block_hash`,`aux_block_hash`, `aux_pow`,`created_at`) "
sql += " values(?,?,?,?)"
res, err := tx.Exec(sql,blockinfo.ParentChainBllockHash,blockinfo.AuxChainBlockHash,blockinfo.AuxPow, blockinfo.CurrentTime)
if err != nil{
glog.Info("Exec fail : ", err)
tx.Commit()
return false
}
glog.Info(res.LastInsertId())
}
tx.Commit()
return true
}
// func main() {
// var config DBConnectionInfo
// config.Host = "127.0.0.1";
// config.Port = "3306";
// config.Username = "root";
// config.Password = "";
// config.Dbname = "bpool_local_db";
// var dbhandle DBConnection
// dbhandle.InitDB(config);
// var info SubmitAuxBlockInfo
// info.AuxBlockTableName = "found_doge_blocks"
// info.ParentChainBllockHash = "a4ee7a37411ce2b50138148e70f7506d132556103e84e186c0da4a8e781812d6"
// info.AuxChainBlockHash = "b4ee7a37411ce2b50138f48e70f7506d132556103e84e186c0da4a8e781812d6"
// info.AuxPow = "a4ee7a37411ce2b50138f48e70f7506d132556103e84e186c0da4a8e781812d6"
// info.CurrentTime = "2019-12-21 02:15:33"
// info.RpcResponse = "{\"id\":0, \"result\": false}"
// info.IsSubmitSuccess = true
// // timeStr:=time.Now().Format("2006-01-02 15:04:05") //当前时间的字符串,2006-01-02 15:04:05据说是golang的诞生时间,固定写法
// // glog.Info(timeStr)
// dbhandle.InsertAuxBlock(info)
// }