dbscan

package module
v0.0.0-...-d2c51c0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 18, 2020 License: MIT Imports: 4 Imported by: 0

README

dbscan

Documentation

Overview

dbscan package is really cool

Example (Bar)
package main

import (
	"fmt"
)

type Bar string

func main() {
	fmt.Println(Bar("bar"))
}
Output:
bar
Example (Foo)
package main

import (
	"fmt"
)

func main() {
	fmt.Println("foo")
}
Output:
foo
Example (ScanAll)
package main

import (
	"database/sql"
	"github.com/georgysavva/dbscan"
)

func main() {
	// package
	type User struct {
		ID    string
		Name  string
		Email string
		Age   int
	}

	// Query rows from the database that implement dbscan.Rows interface, e.g. *sql.Rows:
	db, _ := sql.Open("pgx", "example-connection-url")
	rows, _ := db.Query(`SELECT id, name, email, age from users`)

	var users []*User
	if err := dbscan.ScanAll(&users, rows); err != nil {
		// Handle rows processing error
	}
	// users variable now contains data from all rows.
}
Example (ScanOne)
package main

import (
	"database/sql"

	"github.com/georgysavva/dbscan"
)

type User struct {
	ID    string
	Name  string
	Email string
	Age   int
}

func main() {
	// package

	// Query rows from the database that implement dbscan.Rows interface, e.g. *sql.Rows:
	db, _ := sql.Open("pgx", "example-connection-url")
	rows, _ := db.Query(`SELECT id, name, email, age from users where id='bob'`)

	var user User
	if err := dbscan.ScanOne(&user, rows); err != nil {
		// Handle rows processing error.
	}
	// user variable now contains data from the single row.
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NotFound

func NotFound(err error) bool

NotFound returns true if err is a not found error. This error is returned by ScanOne if there were no rows.

Example
package main

import (
	"fmt"
)

func main() {
	fmt.Println("not found")
}
Output:
not found
Example (Custom)
package main

import (
	"fmt"
)

type not string

func main() {
	// package

	fmt.Println(not("not found"))
}
Output:
not found

func ScanAll

func ScanAll(dst interface{}, rows Rows) error

ScanAll iterates all rows to the end. After iterating it closes the rows, and propagates any errors that could pop up. It expected that destination should be a slice. For each row it scans data and appends it to the destination slice. It resets the destination slice, so if it's not empty it will overwrite all previous elements.

Example
package main

import (
	"database/sql"
	"github.com/georgysavva/dbscan"
)

type User3 struct {
	ID    string
	Name  string
	Email string
	Age   int
}

func main() {
	// package

	// Query rows from the database that implement dbscan.Rows interface, e.g. *sql.Rows:
	db, _ := sql.Open("pgx", "example-connection-url")
	rows, _ := db.Query(`SELECT id, name, email, age from users`)

	var users []*User3
	if err := dbscan.ScanAll(&users, rows); err != nil {
		// Handle rows processing error
	}
	// users variable now contains data from all rows.
}

func ScanOne

func ScanOne(dst interface{}, rows Rows) error

ScanOne iterates all rows to the end and makes sure that there was exactly one row, otherwise it returns an error. After iterating it closes the rows, and propagates any errors that could pop up. It scans data from that single row into destination.

Example
package main

import (
	"database/sql"
	"github.com/georgysavva/dbscan"
)

func main() {
	type User struct {
		ID    string
		Name  string
		Email string
		Age   int
	}

	// Query rows from the database that implement dbscan.Rows interface, e.g. *sql.Rows:
	db, _ := sql.Open("pgx", "example-connection-url")
	rows, _ := db.Query(`SELECT id, name, email, age from users where id='bob'`)

	var user User
	if err := dbscan.ScanOne(&user, rows); err != nil {
		// Handle rows processing error.
	}
	// user variable now contains data from the single row.
}

func ScanRow

func ScanRow(dst interface{}, rows Rows) error

ScanRow creates a new RowScanner and calls RowScanner.Scan that scans current row data into the destination. It's just a helper function if you don't bother with efficiency and don't want to instantiate a new RowScanner before iterating the rows, so it could cache the reflection work between Scan calls. See RowScanner for details.

Types

type RowScanner

type RowScanner struct {
	// contains filtered or unexported fields
}

RowScanner embraces the Rows and exposes the Scan method that allows to scan data from the current row into destination. The first time the Scan method is called it parses the destination type by reflection and caches all required information for further scans. Due to this caching mechanism it's not allowed to call Scan for destinations of different types, the behaviour is unknown in that case. RowScanner doesn't processed to the next row nor close them, it should be done by the client code.

You can instantiate a RowScanner and manually iterate over the rows and control how data is scanned from each row. This can be beneficial if the result set is large and you don't want to allocate a slice for all rows at once as it would be done with ScanAll.

ScanOne and ScanAll both use this type internally.

Example
package main

import (
	"database/sql"
	"github.com/georgysavva/dbscan"
)

type User2 struct {
	ID    string
	Name  string
	Email string
	Age   int
}

func main() {
	// Query rows from the database that implement dbscan.Rows interface, e.g. *sql.Rows:
	db, _ := sql.Open("pgx", "example-connection-url")
	rows, _ := db.Query(`SELECT id, name, email, age from users`)

	// Make sure rows are closed.
	defer rows.Close()

	rs := dbscan.NewRowScanner(rows)
	for rows.Next() {
		var user User2
		if err := rs.Scan(&user); err != nil {
			// Handle row scanning error.
		}
		// user variable now contains data from the current row.
	}
	if err := rows.Err(); err != nil {
		// Handle rows final error.
	}
	if err := rows.Close(); err != nil {
		// Handle rows closing error.
	}
}

func NewRowScanner

func NewRowScanner(rows Rows) *RowScanner

NewRowsScanner returns a new instance of the RowScanner.

func (*RowScanner) Scan

func (rs *RowScanner) Scan(dst interface{}) error

Scan scans data from the current row into the destination. On the first call it caches expensive reflection work and use it the future calls. See RowScanner for details.

type Rows

type Rows interface {
	Close() error
	Err() error
	Next() bool
	Columns() ([]string, error)
	Scan(dest ...interface{}) error
}

Rows is an abstract database rows that dbscan can iterate over and get the data from. This interface is used to decouple from any particular database.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL