Update updates a [Book] in the database.
(ctx context.Context, db DB)
| 70 | |
| 71 | // Update updates a [Book] in the database. |
| 72 | func (b *Book) Update(ctx context.Context, db DB) error { |
| 73 | switch { |
| 74 | case !b._exists: // doesn't exist |
| 75 | return logerror(&ErrUpdateFailed{ErrDoesNotExist}) |
| 76 | case b._deleted: // deleted |
| 77 | return logerror(&ErrUpdateFailed{ErrMarkedForDeletion}) |
| 78 | } |
| 79 | // update with primary key |
| 80 | const sqlstr = `UPDATE django.books SET ` + |
| 81 | `isbn = @p1, book_type = @p2, title = @p3, year = @p4, available = @p5, books_author_id_fkey = @p6 ` + |
| 82 | `WHERE book_id = @p7` |
| 83 | // run |
| 84 | logf(sqlstr, b.ISBN, b.BookType, b.Title, b.Year, b.Available, b.BooksAuthorIDFkey, b.BookID) |
| 85 | if _, err := db.ExecContext(ctx, sqlstr, b.ISBN, b.BookType, b.Title, b.Year, b.Available, b.BooksAuthorIDFkey, b.BookID); err != nil { |
| 86 | return logerror(err) |
| 87 | } |
| 88 | return nil |
| 89 | } |
| 90 | |
| 91 | // Save saves the [Book] to the database. |
| 92 | func (b *Book) Save(ctx context.Context, db DB) error { |
no test coverage detected