All Projects → blockloop → Scan

blockloop / Scan

Licence: mit
Scan database/sql rows directly to structs, slices, and primitive types

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Scan

Squid
🦑 Provides SQL tagged template strings and schema definition functions.
Stars: ✭ 57 (-17.39%)
Mutual labels:  sql, database
Directus Docker
Directus 6 Docker — Legacy Container [EOL]
Stars: ✭ 68 (-1.45%)
Mutual labels:  sql, database
Dolt
Dolt – It's Git for Data
Stars: ✭ 9,880 (+14218.84%)
Mutual labels:  sql, database
Rqlite
The lightweight, distributed relational database built on SQLite
Stars: ✭ 9,147 (+13156.52%)
Mutual labels:  sql, database
Dbx
A neat codegen-based database wrapper written in Go
Stars: ✭ 68 (-1.45%)
Mutual labels:  sql, database
Fluent
Vapor ORM (queries, models, and relations) for NoSQL and SQL databases
Stars: ✭ 1,071 (+1452.17%)
Mutual labels:  sql, database
Data Science Best Resources
Carefully curated resource links for data science in one place
Stars: ✭ 1,104 (+1500%)
Mutual labels:  sql, database
Sqlz
SQL Query Builder for Go
Stars: ✭ 36 (-47.83%)
Mutual labels:  sql, database
Event Management
helps to register an users for on events conducted in college fests with simple logic with secured way
Stars: ✭ 65 (-5.8%)
Mutual labels:  sql, database
Reporting Services Examples
📕 Various example reports I use for SQL Server Reporting Services (SSRS) as well as documents for unit testing, requirements and a style guide template.
Stars: ✭ 63 (-8.7%)
Mutual labels:  sql, database
Cosyan
Transactional SQL based RDBMS with sophisticated multi table constraint logic.
Stars: ✭ 45 (-34.78%)
Mutual labels:  sql, database
Awesome Business Intelligence
Actively curated list of awesome BI tools. PRs welcome!
Stars: ✭ 1,157 (+1576.81%)
Mutual labels:  sql, database
Declarativesql
Attribute-based database access
Stars: ✭ 41 (-40.58%)
Mutual labels:  sql, database
Interference
opensource distributed database with base JPA implementation and event processing support
Stars: ✭ 57 (-17.39%)
Mutual labels:  sql, database
Goqu
SQL builder and query library for golang
Stars: ✭ 984 (+1326.09%)
Mutual labels:  sql, database
Sql.js
A javascript library to run SQLite on the web.
Stars: ✭ 9,594 (+13804.35%)
Mutual labels:  sql, database
Siodb
The simplicity of REST and the power of SQL combined in a database that automatized security and performance. Forget the database, develop faster and safer!
Stars: ✭ 31 (-55.07%)
Mutual labels:  sql, database
Mysqldump Php
PHP version of mysqldump cli that comes with MySQL
Stars: ✭ 975 (+1313.04%)
Mutual labels:  sql, database
Eventql
Distributed "massively parallel" SQL query engine
Stars: ✭ 1,121 (+1524.64%)
Mutual labels:  sql, database
Ahwen
A simple SQL database
Stars: ✭ 66 (-4.35%)
Mutual labels:  sql, database

Scan

GoDoc go test Coveralls github Report Card Dependabot Status

Scan standard lib database rows directly to structs or slices. For the most comprehensive and up-to-date docs see the godoc

Examples

Multiple Rows

db, err := sql.Open("sqlite3", "database.sqlite")
rows, err := db.Query("SELECT * FROM persons")

var persons []Person
err := scan.Rows(&persons, rows)

fmt.Printf("%#v", persons)
// []Person{
//    {ID: 1, Name: "brett"},
//    {ID: 2, Name: "fred"},
//    {ID: 3, Name: "stacy"},
// }

Multiple rows of primitive type

rows, err := db.Query("SELECT name FROM persons")
var names []string
err := scan.Rows(&names, rows)

fmt.Printf("%#v", names)
// []string{
//    "brett",
//    "fred",
//    "stacy",
// }

Single row

rows, err := db.Query("SELECT * FROM persons where name = 'brett' LIMIT 1")
var person Person
err := scan.Row(&person, rows)

fmt.Printf("%#v", person)
// Person{ ID: 1, Name: "brett" }

Scalar value

rows, err := db.Query("SELECT age FROM persons where name = 'brett' LIMIT 1")
var age int8
err := scan.Row(&age, row)

fmt.Printf("%d", age)
// 100

Strict Scanning

Both Rows and Row have strict alternatives to allow scanning to structs strictly based on their db tag. To avoid unwanted behavior you can use RowsStrict or RowStrict to scan without using field names. Any fields not tagged with the db tag will be ignored even if columns are found that match the field names.

Columns

Columns scans a struct and returns a string slice of the assumed column names based on the db tag or the struct field name respectively. To avoid assumptions, use ColumnsStrict which will only return the fields tagged with the db tag. Both Columns and ColumnsStrict are variadic. They both accept a string slice of column names to exclude from the list. It is recommended that you cache this slice.

package main

type User struct {
        ID        int64
        Name      string
        Age       int
        BirthDate string `db:"bday"`
        Zipcode   string `db:"-"`
        Store     struct {
                ID int
                // ...
        }
}

var nobody = new(User)
var userInsertCols = scan.Columns(nobody, "ID")
// []string{ "Name", "Age", "bday" }

var userSelectCols = scan.Columns(nobody)
// []string{ "ID", "Name", "Age", "bday" }

Values

Values scans a struct and returns the values associated with the provided columns. Values uses a sync.Map to cache fields of structs to greatly improve the performance of scanning types. The first time a struct is scanned it's exported fields locations are cached. When later retrieving values from the same struct it should be much faster. See Benchmarks below.

user := &User{
        ID: 1,
        Name: "Brett",
        Age: 100,
}

vals := scan.Values([]string{"ID", "Name"}, user)
// []interface{}{ 1, "Brett" }

I find that the usefulness of both Values and Columns lies within using a library such as sq.

sq.Insert(userCols...).
        Into("users").
        Values(scan.Values(userCols, &user)...)

Configuration

AutoClose: Automatically call rows.Close() after scan completes (default true)

Why

While many other projects support similar features (i.e. sqlx) scan allows you to use any database lib such as the stdlib or squirrel to write fluent SQL statements and pass the resulting rows to scan for scanning.

Benchmarks

λ go test -bench=. -benchtime=10s ./...
goos: linux
goarch: amd64
pkg: github.com/blockloop/scan
BenchmarkColumnsLargeStruct-8           50000000               272 ns/op
BenchmarkValuesLargeStruct-8             2000000              8611 ns/op
BenchmarkScanRowOneField-8               2000000              8528 ns/op
BenchmarkScanRowFiveFields-8             1000000             12234 ns/op
BenchmarkScanTenRowsOneField-8           1000000             16802 ns/op
BenchmarkScanTenRowsTenFields-8           100000            104587 ns/op
PASS
ok      github.com/blockloop/scan       116.055s
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].