MCPcopy
hub / github.com/xo/dbtpl / Update

Method Update

_examples/django/sqlserver/book.dbtpl.go:72–89  ·  view source on GitHub ↗

Update updates a [Book] in the database.

(ctx context.Context, db DB)

Source from the content-addressed store, hash-verified

70
71// Update updates a [Book] in the database.
72func (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.
92func (b *Book) Save(ctx context.Context, db DB) error {

Callers 1

SaveMethod · 0.95

Calls 2

logerrorFunction · 0.70
ExecContextMethod · 0.65

Tested by

no test coverage detected