-
Notifications
You must be signed in to change notification settings - Fork 0
/
entries.go
91 lines (64 loc) · 1.58 KB
/
entries.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
package models
import (
"strings"
"time"
)
type Entry struct {
ID int64
Slug string
Name string
Abbrs string
Caption string
CTime time.Time
ATime time.Time
}
type Entries struct {
}
const (
SQL_UPDATE_ENTRY = `UPDATE entries SET slug=?,name=?,abbrs=?,caption=?,atime = datetime('now','localtime') WHERE id = ?;`
SQL_INSERT_ENTRY = `INSERT INTO entries (slug,name,abbrs,caption,ctime,atime) VALUES (?,?,?,?,datetime('now','localtime'),datetime('now','localtime'));`
SQL_SELECT_ENTRIES = `SELECT id,slug,name,ctime,atime,abbrs,caption FROM entries;`
)
func (self *Entry) Save() (err error) {
// update
if self.ID != 0 {
_, err = db.Exec(SQL_UPDATE_ENTRY, self.Slug, self.Name, self.Abbrs, self.Caption, self.ID)
return
}
// insert
result, err := db.Exec(SQL_INSERT_ENTRY, self.Slug, self.Name, self.Abbrs, self.Caption)
if err != nil {
return
}
self.ID, err = result.LastInsertId()
return
}
func (self *Entries) Search(offset, count int, words string) (entries []Entry, total int, err error) {
rows, err := db.Query(SQL_SELECT_ENTRIES)
if err != nil {
return
}
defer rows.Close()
total = 5
for rows.Next() {
var entry Entry
err = rows.Scan(&entry.ID, &entry.Slug, &entry.Name, &entry.CTime, &entry.ATime, &entry.Abbrs, &entry.Caption)
if err != nil {
return
}
if !strings.Contains(entry.Name+entry.Abbrs+entry.Caption, words) {
continue
}
offset = offset - 1
if offset >= 0 {
continue
}
entries = append(entries, entry)
count = count - 1
if count <= 0 {
return
}
}
err = rows.Err()
return
}