Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
/scanPublic

scan sql rows into any type powered by generics

License

NotificationsYou must be signed in to change notification settings

wroge/scan

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Take a look atgithub.com/wroge/esquel.


go.dev referenceGo Report Cardgolangci-lintcodecovGitHub tag (latest SemVer)

Scan

This package offers a convenient and flexible way to scan SQL rows into any type, leveraging the power of generics.

Features

  • Efficient and Reusable: Avoid repetitive code and define the column-mapping in one place.
  • Auto Closing: No need to worry about resource leaks.
  • No Reflection: Faster than reflection based mappers.
  • Robust Error Handling: Best practices for managing errors.

Usage

import"github.com/wroge/scan"typeAuthorstruct {IDint64Namestring}typePoststruct {IDint64TitlestringAuthors []Author}// Define mapping of database columns to struct fields.varcolumns= scan.Columns[Post]{// Map the 'id' column to the 'ID' field in the 'Post' struct.// Uses the 'scan.Any' function for direct assignment without additional processing."id":scan.Any(func(p*Post,idint64) {p.ID=id }),// Map the 'title' column to the 'Title' field in the 'Post' struct.// The 'scan.Null' function allows handling of nullable database columns.// If the 'title' column is null, 'default title' is used as the value."title":scan.Null("default title",func(p*Post,titlestring) {p.Title=title }),// Map the 'authors' column, expected to be in JSON format, to the 'Authors' field in the 'Post' struct.// The 'scan.JSON' function automatically handles unmarshalling of the JSON data into the 'Author' struct slice."authors":scan.JSON(func(p*Post,authors []Author) {p.Authors=authors }),// Or you could create a custom scanner with this function.// "column": scan.Func[Post, V](func(p *Post, value V) error {// return nil// }),}rows,err:=db.Query("SELECT ...")// handle error

Scanning all rows

posts,err:=scan.All(rows,columns)// handle error

Scanning the first row

post,err:=scan.First(rows,columns)iferr!=nil {iferrors.Is(err,scan.ErrNoRows) {// handle no rows}// handle other error}

Scanning exactly one row

post,err:=scan.One(rows,columns)iferr!=nil {iferrors.Is(err,scan.ErrTooManyRows) {// handle too many rows// post is valid}iferrors.Is(err,scan.ErrNoRows) {// handle no rows}// handle other error}

Scanning a limited number of rows

posts,err:=scan.Limit(10,rows,columns)iferr!=nil {iferrors.Is(err,scan.ErrTooManyRows) {// ignore if result set has more than 10 rows// len(posts) == 10}// handle other error}

Using the Iterator directly

iter,err:=scan.Iter(rows,columns)// handle errordeferiter.Close()foriter.Next() {varpostPosterr=iter.Scan(&post)// handle error// Or use the Value method:post,err:=iter.Value()// handle error}

About

scan sql rows into any type powered by generics

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp