-
Notifications
You must be signed in to change notification settings - Fork 0
/
seqdao.go
58 lines (42 loc) · 1.22 KB
/
seqdao.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
// Copyright 2020 @thiinbit(thiinbit@gmail.com). All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file
package idgen
import (
"fmt"
)
const (
allColumn = ""
defaultSeqStep = 10000
)
func updateAndSelect(sectionId int64) (int64, error) {
_, tbName := sDBTBName(sectionId)
upsertSql := fmt.Sprintf("INSERT INTO %s (section, seq_step, seq_max) VALUES(?, ?, ?) ON DUPLICATE KEY UPDATE seq_max = seq_max + seq_step", tbName)
_, err := Exec(upsertSql, sectionId, defaultSeqStep, defaultSeqStep)
if err != nil {
return 0, err
}
runSql := fmt.Sprintf("SELECT seq_max FROM %s WHERE section = ?", tbName)
row, err := QueryRow(runSql, sectionId)
if err != nil {
return 0, err
}
var seqMax int64
err = row.Scan(&seqMax)
if err != nil {
return 0, err
}
return seqMax, nil
}
func delete(sectionId int64) (int64, error) {
_, tbName := sDBTBName(sectionId)
runSql := fmt.Sprintf("DELETE FROM %s WHERE uid = ?", tbName)
re, err := Exec(runSql, sectionId)
if err != nil {
return 0, err
}
return re.RowsAffected()
}
func sDBTBName(sectionId int64) (string, string) {
return DBTBName(sectionId, dbNamePrefix, tbNamePrefix, dbSize, tbSize)
}