-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.go
60 lines (55 loc) · 987 Bytes
/
add.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
package database
import (
"database/sql"
"time"
)
// Add inserts a File entry into the database if it doesn't exist already.
func (db *DB) Add(input Node) (err error) {
// Attempt to grab lock.
db.lock.Lock()
defer db.lock.Unlock()
// Ping the DB and open a connection if necessary
err = db.conn.Ping()
if err != nil {
return err
}
_, err = db.get(input.Path)
if err != nil {
if err == sql.ErrNoRows {
err = db.insert(input)
} else {
return err
}
} else {
err = db.update(input)
}
return err
}
// Insert adds a Node entry to the database.
func (db *DB) insert(entry Node) (err error) {
stmt, err := db.conn.Prepare(
`INSERT INTO nodes(
path,
name,
type,
size,
cid,
parent,
modified,
replications
) VALUES(?,?,?,?,?,?,?,?);`)
if err != nil {
return err
}
_, err = stmt.Exec(
entry.Path,
entry.Name,
entry.Type,
entry.Size,
entry.CID,
entry.Parent,
time.Now().UTC(),
entry.Replications,
)
return err
}