mirror of
https://github.com/nkanaev/yarr.git
synced 2025-09-13 09:55:36 +00:00
bulk-insert items
This commit is contained in:
@@ -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{} {
|
||||
|
Reference in New Issue
Block a user