From c79ab5cf60daebec0716f75dfc642df65549a9c7 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sun, 5 Feb 2023 20:03:36 +0400 Subject: [PATCH] Date cannot be scanned directly to time.Time type, so we need to read a date to a string first. --- database/sqlite/database.go | 41 ++++++++++++++++---------------- database/sqlite/database_test.go | 12 ++-------- guestbook/database.go | 4 ++-- 3 files changed, 25 insertions(+), 32 deletions(-) diff --git a/database/sqlite/database.go b/database/sqlite/database.go index 2d5b14e..35d6188 100644 --- a/database/sqlite/database.go +++ b/database/sqlite/database.go @@ -128,15 +128,21 @@ func (d *SQLiteDatabase) Entries(page, pageSize int64) (entries []*guestbook.Ent for rows.Next() { var entry guestbook.Entry + var entry_created string var reply_created sql.NullString var reply_message sql.NullString if err = rows.Scan( - &entry.ID, &entry.Created, &entry.Name, + &entry.ID, &entry_created, &entry.Name, &entry.Message, &entry.Website, &reply_created, &reply_message); err != nil { return } + entry.Created, err = time.Parse(guestbook.DateFormat, entry_created) + if err != nil { + return + } + if reply_message.Valid /* reply_created is also valid if reply is */ { date, err := time.Parse(guestbook.DateFormat, reply_created.String) if err != nil { @@ -183,7 +189,7 @@ func (d *SQLiteDatabase) NewEntry(entry *guestbook.Entry) error { } defer tx.Rollback() - r, err := tx.Stmt(stmtNewEntry).Exec(entry.Created, entry.Name, entry.Message, + r, err := tx.Stmt(stmtNewEntry).Exec(entry.Created.Format(guestbook.DateFormat), entry.Name, entry.Message, entry.Website, entry.HideWebsite) if err != nil { return err @@ -200,24 +206,20 @@ func (d *SQLiteDatabase) NewEntry(entry *guestbook.Entry) error { } // UpdateEntry -func (d *SQLiteDatabase) UpdateEntry(entry *guestbook.Entry) (*guestbook.Entry, error) { +func (d *SQLiteDatabase) UpdateEntry(entry *guestbook.Entry) error { tx, err := d.db.Begin() if err != nil { - return nil, err + return err } defer tx.Rollback() row := tx.Stmt(stmtUpdateEntry).QueryRow(entry.Name, entry.Message, entry.Website, entry.HideWebsite, entry.ID) - - uEntry := guestbook.Entry{} - err = row.Scan(&uEntry.ID, &uEntry.Created, &uEntry.Name, &uEntry.Message, &uEntry.Website, &uEntry.HideWebsite) - if err != nil { - return nil, err + if row.Err() != nil { + return row.Err() } - tx.Commit() - return &uEntry, nil + return nil } func (d *SQLiteDatabase) DeleteEntry(entryID int64) (int64, error) { @@ -249,7 +251,7 @@ func (d *SQLiteDatabase) NewReply(reply *guestbook.Reply) error { } defer tx.Rollback() - _, err = tx.Stmt(stmtNewReply).Exec(reply.ID, reply.Created, reply.Message) + _, err = tx.Stmt(stmtNewReply).Exec(reply.ID, reply.Created.Format(guestbook.DateFormat), reply.Message) if err != nil { return err } @@ -259,23 +261,22 @@ func (d *SQLiteDatabase) NewReply(reply *guestbook.Reply) error { return nil } -func (d *SQLiteDatabase) UpdateReply(reply *guestbook.Reply) (*guestbook.Reply, error) { +func (d *SQLiteDatabase) UpdateReply(reply *guestbook.Reply) error { tx, err := d.db.Begin() if err != nil { - return nil, err + return err } defer tx.Rollback() - uReply := guestbook.Reply{} - err = tx.Stmt(stmtUpdateReply).QueryRow( - reply.Created, reply.Message, reply.ID).Scan(&uReply.ID, &uReply.Created, &uReply.Message) - if err != nil { - return nil, err + row := tx.Stmt(stmtUpdateReply).QueryRow( + reply.Created.Format(guestbook.DateFormat), reply.Message, reply.ID) + if row.Err() != nil { + return row.Err() } tx.Commit() - return &uReply, nil + return nil } func (d *SQLiteDatabase) DeleteReply(entryID int64) error { diff --git a/database/sqlite/database_test.go b/database/sqlite/database_test.go index ebbc5e4..ec8b1a5 100644 --- a/database/sqlite/database_test.go +++ b/database/sqlite/database_test.go @@ -86,14 +86,10 @@ func TestSqliteUpdateEntry(t *testing.T) { me.ID = es0[0].ID me.Name = "NotSoAnonymous" - ne, err := db.UpdateEntry(me) + err = db.UpdateEntry(me) if err != nil { t.Error(err) } - - if ne.Name != me.Name { - t.Error(ne.Name, "!=", me.Name) - } } func TestSqliteUpdateReply(t *testing.T) { @@ -117,14 +113,10 @@ func TestSqliteUpdateReply(t *testing.T) { mr.ID = es0[0].ID mr.Message = "bur" - nr, err := db.UpdateReply(mr) + err = db.UpdateReply(mr) if err != nil { t.Error(err) } - - if nr.Message != mr.Message { - t.Error(nr.Message, "!=", mr.Message) - } } func TestSqliteDeleteEntry(t *testing.T) { diff --git a/guestbook/database.go b/guestbook/database.go index 2f3ad38..b9592bd 100644 --- a/guestbook/database.go +++ b/guestbook/database.go @@ -6,10 +6,10 @@ type Guestbook interface { Entries(page, pageSize int64) ([]*Entry, error) Count() (int64, error) NewEntry(entry *Entry) error - UpdateEntry(entry *Entry) (*Entry, error) + UpdateEntry(entry *Entry) error DeleteEntry(entryID int64) error NewReply(reply *Reply) error - UpdateReply(reply *Reply) (*Entry, error) + UpdateReply(reply *Reply) error DeleteReply(entryID int64) error Close() error }