/
asset_content.go
96 lines (84 loc) · 2.78 KB
/
asset_content.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
// SiYuan - Refactor your thinking
// Copyright (c) 2020-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package sql
import (
"database/sql"
"fmt"
"strings"
"github.com/siyuan-note/eventbus"
)
type AssetContent struct {
ID string
Name string
Ext string
Path string
Size int64
Updated int64
Content string
}
const (
AssetContentsFTSCaseInsensitiveInsert = "INSERT INTO asset_contents_fts_case_insensitive (id, name, ext, path, size, updated, content) VALUES %s"
AssetContentsPlaceholder = "(?, ?, ?, ?, ?, ?, ?)"
)
func insertAssetContents(tx *sql.Tx, assetContents []*AssetContent, context map[string]interface{}) (err error) {
if 1 > len(assetContents) {
return
}
var bulk []*AssetContent
for _, assetContent := range assetContents {
bulk = append(bulk, assetContent)
if 512 > len(bulk) {
continue
}
if err = insertAssetContents0(tx, bulk, context); nil != err {
return
}
bulk = []*AssetContent{}
}
if 0 < len(bulk) {
if err = insertAssetContents0(tx, bulk, context); nil != err {
return
}
}
return
}
func insertAssetContents0(tx *sql.Tx, bulk []*AssetContent, context map[string]interface{}) (err error) {
valueStrings := make([]string, 0, len(bulk))
valueArgs := make([]interface{}, 0, len(bulk)*strings.Count(AssetContentsPlaceholder, "?"))
for _, b := range bulk {
valueStrings = append(valueStrings, AssetContentsPlaceholder)
valueArgs = append(valueArgs, b.ID)
valueArgs = append(valueArgs, b.Name)
valueArgs = append(valueArgs, b.Ext)
valueArgs = append(valueArgs, b.Path)
valueArgs = append(valueArgs, b.Size)
valueArgs = append(valueArgs, b.Updated)
valueArgs = append(valueArgs, b.Content)
}
stmt := fmt.Sprintf(AssetContentsFTSCaseInsensitiveInsert, strings.Join(valueStrings, ","))
if err = prepareExecInsertTx(tx, stmt, valueArgs); nil != err {
return
}
eventbus.Publish(eventbus.EvtSQLInsertAssetContent, context)
return
}
func deleteAssetContentsByPath(tx *sql.Tx, path string, context map[string]interface{}) (err error) {
stmt := "DELETE FROM asset_contents_fts_case_insensitive WHERE path = ?"
if err = execStmtTx(tx, stmt, path); nil != err {
return
}
return
}