-
Notifications
You must be signed in to change notification settings - Fork 0
/
instafile.go
111 lines (80 loc) · 1.97 KB
/
instafile.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
110
111
package instafile
import (
"database/sql"
"time"
"github.com/dekoch/gouniversal/shared/io/sqlite3"
)
const TableName = "instafile"
type InstaFile struct {
UserID string
UserName string
Added time.Time
FileID string
URL string
}
func LoadConfig(dbconn *sqlite3.SQLite) error {
var lyt sqlite3.Layout
lyt.SetTableName(TableName)
lyt.AddField("id", sqlite3.TypeINTEGER, true, true)
lyt.AddField("userid", sqlite3.TypeTEXT, false, false)
lyt.AddField("username", sqlite3.TypeTEXT, false, false)
lyt.AddField("fileid", sqlite3.TypeTEXT, false, false)
lyt.AddField("added", sqlite3.TypeDATE, false, false)
lyt.AddField("url", sqlite3.TypeTEXT, false, false)
return dbconn.CreateTableFromLayout(lyt)
}
func (inf *InstaFile) Save(tx *sql.Tx) error {
_, err := tx.Exec("INSERT OR REPLACE INTO `"+TableName+"` (userid, username, fileid, added, url) values(?,?,?,?,?)", inf.UserID, inf.UserName, inf.FileID, inf.Added, inf.URL)
return err
}
func (inf *InstaFile) Load(id string, dbconn *sqlite3.SQLite) (bool, error) {
var (
err error
found bool
)
func() {
var rows *sql.Rows
for i := 0; i <= 2; i++ {
switch i {
case 0:
rows, err = dbconn.DB.Query("SELECT userid, username, fileid, added, url FROM `"+TableName+"` WHERE id=?", id)
case 1:
defer rows.Close()
case 2:
for rows.Next() {
err = rows.Scan(&inf.UserID, &inf.UserName, &inf.FileID, &inf.Added, &inf.URL)
found = true
}
}
if err != nil {
return
}
}
}()
return found, err
}
func (inf *InstaFile) Exists(dbconn *sqlite3.SQLite) (bool, error) {
var (
err error
found bool
)
func() {
var rows *sql.Rows
for i := 0; i <= 2; i++ {
switch i {
case 0:
rows, err = dbconn.DB.Query("SELECT id FROM `"+TableName+"` WHERE userid=? AND fileid=?", inf.UserID, inf.FileID)
case 1:
defer rows.Close()
case 2:
for rows.Next() {
found = true
}
}
if err != nil {
return
}
}
}()
return found, err
}