Skip to content

Document pattern of checking for zero rows #85

@flowchartsman

Description

@flowchartsman

I am using sqlitex.Execute for both queries and updates, and I can't seem to figure out if there is a package-supported way to instruct these methods to return an error if zero rows are returned. Currently, I am doing something like the following:

/* ... */
	found := false
	err = sqlitex.ExecuteFS(conn, sqlFS, "path/to/embedded.sql", &sqlitex.ExecOptions{
		Named: args{
			":key": key,
		},
		ResultFunc: func(stmt *sqlite.Stmt) error {
			found = true
			r := stmt.ColumnReader(0)
                        // parameter v is an any to be gob decoded or json decoded to
			return gob.NewDecoder(r).Decode(v)
		},
	})
	if err != nil {
		return err
	}
	if !found {
		return ErrNotFound
	}

But this is annoying to keep writing. Have I missed something? I know we have sqlitex.Result* methods, but there isn't one for Bytes, which I'm using, and they don't really fit the workflow of Execute and friends.

Provided there is no other way, I propose an additional field in sqlitex.ExecOptions called ExpectRows bool or ExpectResults bool that, when true, returns either a sentinel error from sqlitex (like sqlitex.ErrNoRows) or a sqlite error whose code resolves to sqlite.ResultNotFound if no rows exist.

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions