Skip to content
/ sqlr Public

Set of useful interfaces to work with the GO 'database/sql'

License

Notifications You must be signed in to change notification settings

cycloidio/sqlr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SQLR

PkgGoDev

Defines a default set of Interfaces to work with the default database/sql.

Querier

The sql.DB and the sql.Tx have the same signature when quering so for that we have created the sqlr.Querier which is fulfilled for either sql.DB and sql.Tx so in some contexts you don't need to have the specific implementation this interface can be used

Scanner

Used to abstract the logic of Scanning an sql.Row and sql.Rows reusable:

type Scanner interface {
  Scan(dest ...interface{}) error
}

Example usage:

func scanUsers(rows *sql.Rows) ([]*user.User, error) {
	users := make([]*user.User, 0, 0)
	defer rows.Close()
	for rows.Next() {
		u, err := scanUser(rows)
		if err != nil {
			return nil, err
		}
		users = append(users, u)
	}

	if err := rows.Err(); err != nil {
		return nil, err
	}

	return users, nil
}

func scanUser(s sqlr.Scanner) (*user.User, error) {
	var u user.User

	err := s.Scan(
		&u.Name,
	)
	if err != nil {
		return nil, err
	}

	return u, nil
}

About

Set of useful interfaces to work with the GO 'database/sql'

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages