entigo

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

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

Go to latest
Published: Oct 26, 2018 License: MIT Imports: 7 Imported by: 0

README

#Constraints

  • No reflection
  • Reduce repetitive sql scans
  • No dependencies outside of stdlib
  • Idiomatic Go
  • Not meant to replace complex database/sql code.
  • Meant to replace standard inserts,updates and queries.

#TODO

  • Tests for query.go

Support for transactions. For example atomic batch inserts.

Documentation

Overview

Copyright (c) 2017 Derek Santos. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Copyright (c) 2017 Derek Santos. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Copyright (c) 2017 Derek Santos. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Copyright (c) 2017 Derek Santos. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Copyright (c) 2017 Derek Santos. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Copyright (c) 2017 Derek Santos. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exec

func Exec(db *sql.DB, query string, args ...interface{}) error

Exec executes a statemen and returns an error if there was one.

func Insert

func Insert(db *sql.DB, query string, args ...interface{}) (int64, error)

Insert executes an insert statement and returns the last insert ID or an error.

func QueryRow

func QueryRow(db *sql.DB, query string, scanner func(*sql.Row) error, args ...interface{}) error

QueryRow facilitates queries that retrieve a single row. The scanner func argument is expected to handle scanning the row if it's found. The args parameter is passed into the query for parameterized queries.

func QueryRows

func QueryRows(db *sql.DB, query string, scanner func(*sql.Rows) error, args ...interface{}) error

QueryRows facilitates queries that retrieve 0 or more rows. The scanner fucn argument is expected to handle scanning each row as desired, typically into a slice of structs. The args parameter is passed into the query for parameterized queries.

Types

type Entity

type Entity struct {
	Name   string
	Key    *Field
	Fields []*Field
}

An Entity defines the relationship between an instance of a struct and a row in a database table or collection. It's important to note that it's related to an instance, and so has pointers to the stucts fields. This facilitates row scanning and prepared statements.

func (*Entity) Delete

func (ent *Entity) Delete(db *sql.DB) error

Delete executes a delete statement using the Key value of the receiver. The entity requires only the Key to be set for the delete to work.

Example:

person := &Person{ID:1}
err := person.Entity().Delete()
if err != nil {
	panic(err)
}

func (*Entity) Get

func (ent *Entity) Get(db *sql.DB) error

Get scans a single row of data from an auto-generated sql statement into the Entity's value pointers. It returns the row based on the primary key value of the entity.

Example:

person := &Person{ID:1}
err := person.Entity().Get()

func (*Entity) Insert

func (ent *Entity) Insert(db *sql.DB) (int64, error)

Insert executes an insert statement on a single row of data using the values in the receiver. The new ID will be returned if applicable. Fields with ReadOnly set to true will not be included in the insert.

Example:

person := &Person{Name:"John Doe", Email"test@test.com"}
id, err := person.Entity().Insert()
if err != nil {
	panic(err)
}
person.ID = id

func (*Entity) Update

func (ent *Entity) Update(db *sql.DB) error

Update executes an update statement on a single row of data using values in the receiver. Fields with ReadOnly set to true will not be included in the update.

Example:

person := &Person{ID:1}
e := person.Entity()
err := e.Get()
if err != nil {
	panic(err)
}

person.Name = "New Name"
err = e.Update()
if err != nil {
	panic(err)
}

func (*Entity) Where

func (ent *Entity) Where(db *sql.DB, clause string, args ...interface{}) error

Where scans a single row of data using the specified sql clause into the Entity's value pointers.

Example:

person := &Person{}
err := person.Entity().Where(db, "email = ?", "john@test.com")

type EntityCollection

type EntityCollection func() EntityDefiner

func (EntityCollection) Select

func (c EntityCollection) Select(db *sql.DB, clause string, args ...interface{}) ([]EntityDefiner, error)

type EntityDefiner

type EntityDefiner interface {
	Entity() *Entity
}

An EntityDefiner must return a pointer to an Entity struct. This interface should be implemented by structs that relate to a database table or collection.

type Field

type Field struct {
	Name     string
	Value    interface{}
	ReadOnly bool

	// NonIncrementing should be set to true when you need a primary key
	// whos value does not auto increment.
	NonIncrementing bool
}

A Field maps the name of a database column to a pointer in a struct. If ReadOnly is true, this field will not be included in writes to the database.

type MD5

type MD5 string

func (*MD5) Hash

func (g *MD5) Hash()

func (*MD5) Scan

func (p *MD5) Scan(value interface{}) error

func (MD5) Value

func (p MD5) Value() (driver.Value, error)

type Password

type Password string

Password is a string type used to represent secure password fields that are hashed using BCrypt.

func (*Password) Compare

func (p *Password) Compare(candidate string) error

Compare will use bcrypt to check for equality between the specified candidate parameter and the receiver. It will return an error if they do not match.

func (*Password) Hash

func (p *Password) Hash() error

Hash will bcrypt the pointer receiver's value and set it through the pointer receiver. It will return an error if bcrypt fails.

func (*Password) Scan

func (p *Password) Scan(value interface{}) error

func (Password) Value

func (p Password) Value() (driver.Value, error)

type Sha256

type Sha256 string

func (*Sha256) Hash

func (g *Sha256) Hash()

func (*Sha256) Scan

func (p *Sha256) Scan(value interface{}) error

func (Sha256) Value

func (p Sha256) Value() (driver.Value, error)

Jump to

Keyboard shortcuts

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