-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
104 lines (88 loc) · 2.23 KB
/
database.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
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"io"
)
type Database interface {
io.Closer
CreateTableStatus() error
InsertStatus(tweetID int64, tootID string) error
GetLastestTweetID() (latestID int64, err error)
GetStatusByTweetID(tweetID int64) (tootID string, err error)
GetStatusByTootID(tootID string) (tweetID int64, err error)
CountStatus() (count int)
}
type TwtlDatabase struct {
*sql.DB
}
func (db *TwtlDatabase) CreateTableStatus() error {
sqlStmt := `
CREATE TABLE IF NOT EXISTS status(
id INTEGER PRIMARY KEY AUTOINCREMENT,
tweet_id INTEGER NOT NULL UNIQUE,
toot_id VARCHAR(255) NOT NULL,
inserted_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
`
_, err := db.Exec(sqlStmt)
return err
}
func (db *TwtlDatabase) InsertStatus(tweetID int64, tootID string) error {
tx, err := db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
res, err := tx.Exec("INSERT INTO status(tweet_id, toot_id) VALUES(?, ?);", tweetID, tootID)
if err != nil {
return err
}
_, err = res.LastInsertId()
if err != nil {
return err
}
if err = tx.Commit(); err != nil {
return err
}
return nil
}
func (db *TwtlDatabase) GetLastestTweetID() (latestID int64, err error) {
err = db.QueryRow("SELECT tweet_id FROM status ORDER BY tweet_id DESC;").Scan(&latestID)
if err != nil {
return 0, err
}
return latestID, nil
}
func (db *TwtlDatabase) GetStatusByTweetID(tweetID int64) (tootID string, err error) {
err = db.QueryRow("SELECT toot_id FROM status WHERE tweet_id = ?;", tweetID).Scan(&tootID)
if err != nil {
return "", err
}
return tootID, nil
}
func (db *TwtlDatabase) GetStatusByTootID(tootID string) (tweetID int64, err error) {
err = db.QueryRow("SELECT tweet_id FROM status WHERE toot_id = ?;", tootID).Scan(&tweetID)
if err != nil {
return 0, err
}
return tweetID, nil
}
func (db *TwtlDatabase) CountStatus() (count int) {
err := db.QueryRow("SELECT COUNT(*) FROM status").Scan(&count)
if err != nil {
return 0
}
return count
}
func NewDatabase(target string) (*TwtlDatabase, error) {
db, err := sql.Open("sqlite3", fmt.Sprintf("%s", target))
if err != nil {
return nil, err
}
if err = db.Ping(); err != nil {
return nil, err
}
return &TwtlDatabase{db}, nil
}