-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
109 lines (93 loc) · 2.16 KB
/
db.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
package main
import (
"fmt"
"hash/crc32"
"time"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func hashMail(date, from, subject string) string {
str := date + from + subject
h := crc32.ChecksumIEEE([]byte(str))
return fmt.Sprintf("%X", h)
}
type localDB struct {
read []string
}
func newLocalDB() *localDB {
return &localDB{}
}
func (db *localDB) wasRead(hash string) bool {
for i := range db.read {
if db.read[i] == hash {
return true
}
}
return false
}
func (db *localDB) markRead(hash string) {
db.read = append(db.read, hash)
}
type sqlMessage struct {
ID uint
Date time.Time
From string
Subject string
Summary string
Original string
// Metadata
Tags []string
Deleted bool
}
type sqliteDB struct {
db *gorm.DB
showDeleted bool
}
func newSqlite() (*sqliteDB, error) {
db, err := gorm.Open(sqlite.Open("mailassist.db"), &gorm.Config{})
if err != nil {
return nil, err
}
// Migrate the schema
db.AutoMigrate(&sqlMessage{})
/*
// Create
db.Create(&Product{Code: "D42", Price: 100})
// Read
var product Product
db.First(&product, 1) // find product with integer primary key
db.First(&product, "code = ?", "D42") // find product with code D42
// Update - update product's price to 200
db.Model(&product).Update("Price", 200)
// Update - update multiple fields
db.Model(&product).Updates(Product{Price: 200, Code: "F42"}) // non-zero fields
db.Model(&product).Updates(map[string]interface{}{"Price": 200, "Code": "F42"})
// Delete - delete product
db.Delete(&product, 1)
*/
return &sqliteDB{
db: db,
}, nil
}
func (db *sqliteDB) saveMessage(date, from, subject, message, original string) error {
// Fri, 29 Mar 2024 17:48:24 +0000 (UTC)
// Mon Jan 2 15:04:05 MST 2006
d, err := time.Parse("Mon, 02 Jan 2006 15:04:05 +0000 (MST)", date)
if err != nil {
return err
}
db.db.Create(&sqlMessage{
Date: d,
From: from,
Subject: subject,
Original: original,
Summary: message,
})
return nil
}
func (db *sqliteDB) getMessages(from, to time.Time) []sqlMessage {
return []sqlMessage{}
}
func (db *sqliteDB) getTags(tags []string) []sqlMessage {
return []sqlMessage{}
}