-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_parts_dao.go
executable file
·125 lines (105 loc) · 4.23 KB
/
file_parts_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
/*
* 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 FilePartsDAO struct {
db *sqlx.DB
}
func NewFilePartsDAO(db *sqlx.DB) *FilePartsDAO {
return &FilePartsDAO{db}
}
// insert into file_parts(creator_id, file_part_id, file_part, is_big_file, file_total_parts, file_path, file_size) values (:creator_id, :file_part_id, :file_part, :is_big_file, :file_total_parts, :file_path, :file_size)
// TODO(@benqi): sqlmap
func (dao *FilePartsDAO) Insert(do *dataobject.FilePartsDO) int64 {
var query = "insert into file_parts(creator_id, file_part_id, file_part, is_big_file, file_total_parts, file_path, file_size) values (:creator_id, :file_part_id, :file_part, :is_big_file, :file_total_parts, :file_path, :file_size)"
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, creator_id, file_part_id, file_part, is_big_file, file_total_parts, file_path, file_size from file_parts where creator_id = :creator_id and file_part_id = :file_part_id
// TODO(@benqi): sqlmap
func (dao *FilePartsDAO) SelectFileParts(creator_id int64, file_part_id int64) *dataobject.FilePartsDO {
var query = "select id, creator_id, file_part_id, file_part, is_big_file, file_total_parts, file_path, file_size from file_parts where creator_id = ? and file_part_id = ?"
rows, err := dao.db.Queryx(query, creator_id, file_part_id)
if err != nil {
errDesc := fmt.Sprintf("Queryx in SelectFileParts(_), error: %v", err)
glog.Error(errDesc)
panic(mtproto.NewRpcError(int32(mtproto.TLRpcErrorCodes_DBERR), errDesc))
}
defer rows.Close()
do := &dataobject.FilePartsDO{}
if rows.Next() {
err = rows.StructScan(do)
if err != nil {
errDesc := fmt.Sprintf("StructScan in SelectFileParts(_), 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 SelectFileParts(_), error: %v", err)
glog.Error(errDesc)
panic(mtproto.NewRpcError(int32(mtproto.TLRpcErrorCodes_DBERR), errDesc))
}
return do
}
// update file_parts set file_part = :file_part where id = :id
// TODO(@benqi): sqlmap
func (dao *FilePartsDAO) UpdateFilePart(file_part int32, id int64) int64 {
var query = "update file_parts set file_part = ? where id = ?"
r, err := dao.db.Exec(query, file_part, id)
if err != nil {
errDesc := fmt.Sprintf("Exec in UpdateFilePart(_), error: %v", err)
glog.Error(errDesc)
panic(mtproto.NewRpcError(int32(mtproto.TLRpcErrorCodes_DBERR), errDesc))
}
rows, err := r.RowsAffected()
if err != nil {
errDesc := fmt.Sprintf("RowsAffected in UpdateFilePart(_), error: %v", err)
glog.Error(errDesc)
panic(mtproto.NewRpcError(int32(mtproto.TLRpcErrorCodes_DBERR), errDesc))
}
return rows
}
// update file_parts set file_part = :file_part, file_total_parts = :file_total_parts, file_size = :file_size where id = :id
// TODO(@benqi): sqlmap
func (dao *FilePartsDAO) UpdateFilePartAndTotal(file_part int32, file_total_parts int32, file_size int64, id int64) int64 {
var query = "update file_parts set file_part = ?, file_total_parts = ?, file_size = ? where id = ?"
r, err := dao.db.Exec(query, file_part, file_total_parts, file_size, id)
if err != nil {
errDesc := fmt.Sprintf("Exec in UpdateFilePartAndTotal(_), error: %v", err)
glog.Error(errDesc)
panic(mtproto.NewRpcError(int32(mtproto.TLRpcErrorCodes_DBERR), errDesc))
}
rows, err := r.RowsAffected()
if err != nil {
errDesc := fmt.Sprintf("RowsAffected in UpdateFilePartAndTotal(_), error: %v", err)
glog.Error(errDesc)
panic(mtproto.NewRpcError(int32(mtproto.TLRpcErrorCodes_DBERR), errDesc))
}
return rows
}