mirror of
https://github.com/nkanaev/yarr.git
synced 2025-05-24 00:33:14 +00:00
bulk-insert items
This commit is contained in:
parent
61ed9aabf9
commit
7abe2a8c05
46
main.go
46
main.go
@ -6,10 +6,11 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
storage, err := storage.New()
|
||||
store, err := storage.New()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Print(store)
|
||||
/*
|
||||
folder := storage.CreateFolder("foo")
|
||||
storage.RenameFolder(folder.Id, "bar")
|
||||
@ -20,4 +21,47 @@ func main() {
|
||||
"title", "description", "link", "feedlink", "icon", 1)
|
||||
storage.RenameFeed(feed.Id, "newtitle")
|
||||
*/
|
||||
/*
|
||||
items := make([]storage.Item, 3, 3)
|
||||
items = append(items, storage.Item{
|
||||
Id: "id",
|
||||
FeedId: 0,
|
||||
Title: "title",
|
||||
Link: "link",
|
||||
Description: "description",
|
||||
Content: "content",
|
||||
Author: "author",
|
||||
Date: 1,
|
||||
DateUpdated: 1,
|
||||
Status: storage.UNREAD,
|
||||
Image: "image",
|
||||
})
|
||||
items = append(items, storage.Item{
|
||||
Id: "id2",
|
||||
FeedId: 0,
|
||||
Title: "title",
|
||||
Link: "link",
|
||||
Description: "description",
|
||||
Content: "content",
|
||||
Author: "author",
|
||||
Date: 1,
|
||||
DateUpdated: 50,
|
||||
Status: storage.UNREAD,
|
||||
Image: "image",
|
||||
})
|
||||
items = append(items, storage.Item{
|
||||
Id: "id",
|
||||
FeedId: 0,
|
||||
Title: "title",
|
||||
Link: "link",
|
||||
Description: "description",
|
||||
Content: "content",
|
||||
Author: "author",
|
||||
Date: 1,
|
||||
DateUpdated: 100,
|
||||
Status: storage.UNREAD,
|
||||
Image: "image",
|
||||
})
|
||||
log.Print(store.CreateItems(items))
|
||||
*/
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ const (
|
||||
)
|
||||
|
||||
type Item struct {
|
||||
Id int64
|
||||
Id string
|
||||
FeedId int64
|
||||
Title string
|
||||
Link string
|
||||
@ -23,5 +23,36 @@ type Item struct {
|
||||
}
|
||||
|
||||
func (s *Storage) CreateItems(items []Item) bool {
|
||||
tx, err := s.db.Begin()
|
||||
if err != nil {
|
||||
s.log.Print(err)
|
||||
return false
|
||||
}
|
||||
for _, item := range items {
|
||||
_, err = tx.Exec(`
|
||||
insert into items (
|
||||
id, feed_id, title, link, description,
|
||||
content, author, date, date_updated, status, image
|
||||
)
|
||||
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
on conflict (id) do update set date_updated=?`,
|
||||
item.Id, item.FeedId, item.Title, item.Link, item.Description,
|
||||
item.Content, item.Author, item.Date, item.DateUpdated, UNREAD, item.Image,
|
||||
// upsert values
|
||||
item.DateUpdated,
|
||||
)
|
||||
if err != nil {
|
||||
s.log.Print(err)
|
||||
if err = tx.Rollback(); err != nil {
|
||||
s.log.Print(err)
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
if err = tx.Commit(); err != nil {
|
||||
s.log.Print(err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"os"
|
||||
"log"
|
||||
"database/sql"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
@ -44,6 +46,7 @@ create index if not exists idx_item_status on items(status);
|
||||
|
||||
type Storage struct {
|
||||
db *sql.DB
|
||||
log *log.Logger
|
||||
}
|
||||
|
||||
func New() (*Storage, error) {
|
||||
@ -56,7 +59,8 @@ func New() (*Storage, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Storage{db: db}, nil
|
||||
logger := log.New(os.Stdout, "storage: ", log.Ldate | log.Ltime | log.Lshortfile)
|
||||
return &Storage{db: db, log: logger}, nil
|
||||
}
|
||||
|
||||
func intOrNil(id int64) interface{} {
|
||||
|
Loading…
x
Reference in New Issue
Block a user