/
documents_dao.go
executable file
·120 lines (101 loc) · 4.27 KB
/
documents_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
/*
* Copyright (c) 2018, https://github.com/airwide-code
* All rights reserved.
*
*
*
*/
package mysql_dao
import (
"fmt"
"github.com/golang/glog"
"github.com/jmoiron/sqlx"
"github.com/airwide-code/airwide.datacenter/mtproto"
"github.com/airwide-code/airwide.datacenter/nbfs/biz/dal/dataobject"
)
type DocumentsDAO struct {
db *sqlx.DB
}
func NewDocumentsDAO(db *sqlx.DB) *DocumentsDAO {
return &DocumentsDAO{db}
}
// insert into documents(document_id, access_hash, dc_id, file_path, file_size, uploaded_file_name, ext, mime_type, thumb_id, attributes) values (:document_id, :access_hash, :dc_id, :file_path, :file_size, :uploaded_file_name, :ext, :mime_type, :thumb_id, :attributes)
// TODO(@benqi): sqlmap
func (dao *DocumentsDAO) Insert(do *dataobject.DocumentsDO) int64 {
var query = "insert into documents(document_id, access_hash, dc_id, file_path, file_size, uploaded_file_name, ext, mime_type, thumb_id, attributes) values (:document_id, :access_hash, :dc_id, :file_path, :file_size, :uploaded_file_name, :ext, :mime_type, :thumb_id, :attributes)"
r, err := dao.db.NamedExec(query, do)
if err != nil {
errDesc := fmt.Sprintf("NamedExec in Insert(%v), error: %v", do, err)
glog.Error(errDesc)
panic(mtproto.NewRpcError(int32(mtproto.TLRpcErrorCodes_DBERR), errDesc))
}
id, err := r.LastInsertId()
if err != nil {
errDesc := fmt.Sprintf("LastInsertId in Insert(%v)_error: %v", do, err)
glog.Error(errDesc)
panic(mtproto.NewRpcError(int32(mtproto.TLRpcErrorCodes_DBERR), errDesc))
}
return id
}
// select id, document_id, access_hash, dc_id, file_path, file_size, uploaded_file_name, ext, mime_type, thumb_id, attributes, version from documents where dc_id = 2 and document_id = :document_id and access_hash = :access_hash and version = :version
// TODO(@benqi): sqlmap
func (dao *DocumentsDAO) SelectByFileLocation(document_id int64, access_hash int64, version int32) *dataobject.DocumentsDO {
var query = "select id, document_id, access_hash, dc_id, file_path, file_size, uploaded_file_name, ext, mime_type, thumb_id, attributes, version from documents where dc_id = 2 and document_id = ? and access_hash = ? and version = ?"
rows, err := dao.db.Queryx(query, document_id, access_hash, version)
if err != nil {
errDesc := fmt.Sprintf("Queryx in SelectByFileLocation(_), error: %v", err)
glog.Error(errDesc)
panic(mtproto.NewRpcError(int32(mtproto.TLRpcErrorCodes_DBERR), errDesc))
}
defer rows.Close()
do := &dataobject.DocumentsDO{}
if rows.Next() {
err = rows.StructScan(do)
if err != nil {
errDesc := fmt.Sprintf("StructScan in SelectByFileLocation(_), error: %v", err)
glog.Error(errDesc)
panic(mtproto.NewRpcError(int32(mtproto.TLRpcErrorCodes_DBERR), errDesc))
}
} else {
return nil
}
err = rows.Err()
if err != nil {
errDesc := fmt.Sprintf("rows in SelectByFileLocation(_), error: %v", err)
glog.Error(errDesc)
panic(mtproto.NewRpcError(int32(mtproto.TLRpcErrorCodes_DBERR), errDesc))
}
return do
}
// select id, document_id, access_hash, dc_id, file_path, file_size, uploaded_file_name, ext, mime_type, thumb_id, attributes, version from documents where document_id in (:idList)
// TODO(@benqi): sqlmap
func (dao *DocumentsDAO) SelectByIdList(idList []int64) []dataobject.DocumentsDO {
var q = "select id, document_id, access_hash, dc_id, file_path, file_size, uploaded_file_name, ext, mime_type, thumb_id, attributes, version from documents where document_id in (?)"
query, a, err := sqlx.In(q, idList)
rows, err := dao.db.Queryx(query, a...)
if err != nil {
errDesc := fmt.Sprintf("Queryx in SelectByIdList(_), error: %v", err)
glog.Error(errDesc)
panic(mtproto.NewRpcError(int32(mtproto.TLRpcErrorCodes_DBERR), errDesc))
}
defer rows.Close()
var values []dataobject.DocumentsDO
for rows.Next() {
v := dataobject.DocumentsDO{}
// TODO(@benqi): 不使用反射
err := rows.StructScan(&v)
if err != nil {
errDesc := fmt.Sprintf("StructScan in SelectByIdList(_), error: %v", err)
glog.Error(errDesc)
panic(mtproto.NewRpcError(int32(mtproto.TLRpcErrorCodes_DBERR), errDesc))
}
values = append(values, v)
}
err = rows.Err()
if err != nil {
errDesc := fmt.Sprintf("rows in SelectByIdList(_), error: %v", err)
glog.Error(errDesc)
panic(mtproto.NewRpcError(int32(mtproto.TLRpcErrorCodes_DBERR), errDesc))
}
return values
}