package models // Code generated by xo. DO NOT EDIT. import ( "context" ) // User represents a row from 'users'. type User struct { ID string `json:"id"` // id Name string `json:"name"` // name Email string `json:"email"` // email Picture string `json:"picture"` // picture Completed bool `json:"completed"` // completed CreatedAt Time `json:"created_at"` // created_at // xo fields _exists, _deleted bool } // Exists returns true when the User exists in the database. func (u *User) Exists() bool { return u._exists } // Deleted returns true when the User has been marked for deletion from // the database. func (u *User) Deleted() bool { return u._deleted } // Insert inserts the User to the database. func (u *User) Insert(ctx context.Context, db DB) error { switch { case u._exists: // already exists return logerror(&ErrInsertFailed{ErrAlreadyExists}) case u._deleted: // deleted return logerror(&ErrInsertFailed{ErrMarkedForDeletion}) } // insert (manual) const sqlstr = `INSERT INTO users (` + `id, name, email, picture, completed, created_at` + `) VALUES (` + `$1, $2, $3, $4, $5, $6` + `)` // run logf(sqlstr, u.ID, u.Name, u.Email, u.Picture, u.Completed, u.CreatedAt) if _, err := db.ExecContext(ctx, sqlstr, u.ID, u.Name, u.Email, u.Picture, u.Completed, u.CreatedAt); err != nil { return logerror(err) } // set exists u._exists = true return nil } // Update updates a User in the database. func (u *User) Update(ctx context.Context, db DB) error { switch { case !u._exists: // doesn't exist return logerror(&ErrUpdateFailed{ErrDoesNotExist}) case u._deleted: // deleted return logerror(&ErrUpdateFailed{ErrMarkedForDeletion}) } // update with primary key const sqlstr = `UPDATE users SET ` + `name = $1, email = $2, picture = $3, completed = $4, created_at = $5 ` + `WHERE id = $6` // run logf(sqlstr, u.Name, u.Email, u.Picture, u.Completed, u.CreatedAt, u.ID) if _, err := db.ExecContext(ctx, sqlstr, u.Name, u.Email, u.Picture, u.Completed, u.CreatedAt, u.ID); err != nil { return logerror(err) } return nil } // Save saves the User to the database. func (u *User) Save(ctx context.Context, db DB) error { if u.Exists() { return u.Update(ctx, db) } return u.Insert(ctx, db) } // Upsert performs an upsert for User. func (u *User) Upsert(ctx context.Context, db DB) error { switch { case u._deleted: // deleted return logerror(&ErrUpsertFailed{ErrMarkedForDeletion}) } // upsert const sqlstr = `INSERT INTO users (` + `id, name, email, picture, completed, created_at` + `) VALUES (` + `$1, $2, $3, $4, $5, $6` + `)` + ` ON CONFLICT (id) DO ` + `UPDATE SET ` + `name = EXCLUDED.name, email = EXCLUDED.email, picture = EXCLUDED.picture, completed = EXCLUDED.completed, created_at = EXCLUDED.created_at ` // run logf(sqlstr, u.ID, u.Name, u.Email, u.Picture, u.Completed, u.CreatedAt) if _, err := db.ExecContext(ctx, sqlstr, u.ID, u.Name, u.Email, u.Picture, u.Completed, u.CreatedAt); err != nil { return logerror(err) } // set exists u._exists = true return nil } // Delete deletes the User from the database. func (u *User) Delete(ctx context.Context, db DB) error { switch { case !u._exists: // doesn't exist return nil case u._deleted: // deleted return nil } // delete with single primary key const sqlstr = `DELETE FROM users ` + `WHERE id = $1` // run logf(sqlstr, u.ID) if _, err := db.ExecContext(ctx, sqlstr, u.ID); err != nil { return logerror(err) } // set deleted u._deleted = true return nil } // UserByID retrieves a row from 'users' as a User. // // Generated from index 'sqlite_autoindex_users_1'. func UserByID(ctx context.Context, db DB, id string) (*User, error) { // query const sqlstr = `SELECT ` + `id, name, email, picture, completed, created_at ` + `FROM users ` + `WHERE id = $1` // run logf(sqlstr, id) u := User{ _exists: true, } if err := db.QueryRowContext(ctx, sqlstr, id).Scan(&u.ID, &u.Name, &u.Email, &u.Picture, &u.Completed, &u.CreatedAt); err != nil { return nil, logerror(err) } return &u, nil } // UserByEmail retrieves a row from 'users' as a User. // // Generated from index 'sqlite_autoindex_users_2'. func UserByEmail(ctx context.Context, db DB, email string) (*User, error) { // query const sqlstr = `SELECT ` + `id, name, email, picture, completed, created_at ` + `FROM users ` + `WHERE email = $1` // run logf(sqlstr, email) u := User{ _exists: true, } if err := db.QueryRowContext(ctx, sqlstr, email).Scan(&u.ID, &u.Name, &u.Email, &u.Picture, &u.Completed, &u.CreatedAt); err != nil { return nil, logerror(err) } return &u, nil } // UserByToken runs a custom query, returning results as User. func UserByToken(ctx context.Context, db DB, token string) (*User, error) { // query const sqlstr = `SELECT ` + `u.* ` + `FROM tokens t ` + `JOIN users u ON t.user_id = u.id ` + `WHERE t.token = $1` // run logf(sqlstr, token) var u User if err := db.QueryRowContext(ctx, sqlstr, token).Scan(&u.ID, &u.Name, &u.Email, &u.Picture, &u.Completed, &u.CreatedAt); err != nil { return nil, logerror(err) } return &u, nil }