huge

package module
v0.0.0-...-89a5b3b Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2021 License: MIT Imports: 16 Imported by: 0

README

Go huge CRUD library and SQL builder

Reinventing the wheel, reinventing the best wheel.

Features
  • CRUD/Load/Upsert/Convert/RUD by Primary Key
  • Auto Increment/Auto Now/Auto Now Add
  • Encoding GOB/JSON/XML
  • Collapse SQL NULL&Go Zero Value
  • Version
  • Inline/Inline Static
  • Primary Key/Foreign Key/One to One/One to Many/Many to One/Many to Many
  • Scan One/All to Struct/Slice/Map/Array
  • Scan interface{} Slice/Map with Type
  • Time Precision/Unix Seconds/Unix Milliseconds/Integer Date
  • Exclude Columns/Transform Column Name
  • Building SQL Programmatically/SQL Debug Log
Usage
package main

import (
  "fmt"
  "time"

  "github.com/cxr29/huge"
  "github.com/cxr29/huge/query"
  "github.com/cxr29/log"
)

type Node struct {
  Id       int
  Name     string
  Code     string                 `huge:",collapse"`
  Data     map[string]interface{} `huge:",json"`
  Parent   *Node                  `huge:",foreign_key"`
  Children []*Node                `huge:",one_to_many"`
  Siblings map[int]*Node          `huge:",many_to_many"`
  Version  int                    `huge:",version"`
  Created  int64                  `huge:",auto_now_add"`
  Updated  time.Time              `huge:",auto_now"`
}

func main() {
  n := &Node{ /* ... */ }

  h, err := huge.Open("driverName", "dataSourceName")
  log.ErrPanic(err)

  // CRUD row
  r, err := h.Create(n)
  log.ErrPanic(err)
  fmt.Println(r.(bool), n.Id, n.Name, n.Version, n.Created, n.Updated)

  n.Name = "..."
  r, err = h.Update(n, "Name")
  log.ErrPanic(err)
  fmt.Println(r.(bool), n.Id, n.Name, n.Version, n.Created, n.Updated)

  a := &Node{Id: n.Id, Version: -1} // force version
  r, err = h.Read(a)
  log.ErrPanic(err)
  fmt.Println(r.(bool), a.Id, a.Name, a.Version, a.Created, a.Updated)

  r, err = h.Delete(a)
  log.ErrPanic(err)
  fmt.Println(r.(bool))

  // CRUD rows
  s := []*Node{ /* ... */ }
  r, err = h.Create(s)
  log.ErrPanic(err)
  fmt.Println(r.(int))

  m := map[string]*Node{ /* ... */ }
  r, err = h.Read(m, huge.Exclude, "Data") // exlcude columns
  log.ErrPanic(err)
  fmt.Println(r.(map[string]struct{}))

  r, err = h.Update(m)
  log.ErrPanic(err)
  fmt.Println(r.(map[string]struct{}))

  r, err = h.Delete(s)
  log.ErrPanic(err)
  fmt.Println(r.(map[int]struct{}))

  // RUD by Primary Keys
  keys := map[int]struct{}{ /* ... */ }
  r, err = h.ReadBy(keys, Node{})
  log.ErrPanic(err)
  fmt.Println(r.(map[int]*Node))

  r, err = h.UpdateBy([]int{7, 29}, &Node{Name: "foobar", Version: 2}, "Name") // only version = 2
  log.ErrPanic(err)
  fmt.Println(r.(int))

  r, err = h.DeleteBy(1, Node{}) // only one primary key
  log.ErrPanic(err)
  fmt.Println(r.(int))

  // Build SQL and Scan
  var all []*Node
  log.ErrPanic(h.Q(
    query.Select(),
    query.From("Node"),
    query.Where(
      query.Contains("Name", "foo"),
      query.Eq("Version", 1),
    ),
    query.OrderBy("-Code", "+Id"),
  ).All(&all))
  fmt.Println(all)

  version := query.IQ("Version")
  set := query.X.Set()
  set.Add("Name", "foo")
  set.Append(version, version.Inc())
  result, err := h.Exec(query.Q(
    query.Update("Node"),
    set,
    query.Where(query.Eq("Id", 10)),
  ))
  log.ErrPanic(err)
  affected, err := result.RowsAffected()
  log.ErrPanic(err)
  fmt.Println(affected)

  // ...
}
I hate writing documentation but RTFSC.
I hate writing test cases but I have tested it. I did my best.

Documentation

Index

Constants

View Source
const (
	Millisecond = 1
	Second      = 1e3 * Millisecond
	Minute      = 60 * Second
	Hour        = 60 * Minute
	Day         = 24 * Hour
	Week        = 7 * Day
)
View Source
const Exclude = ""

Variables

View Source
var (
	ErrNoRows = sql.ErrNoRows
	ErrTxDone = sql.ErrTxDone
)

Functions

func Convert

func Convert(collapse, encoding bool, dst, src interface{}, columns ...string) error

Convert T to []interface{} or map[string]interface{}; []T or map[]T to [][]interface{}, []map[string]interface{}, map[PK][]interface{} or map[PK]map[string]interface{}.

func CreateTable

func CreateTable(i interface{}, s query.Starter, temporary, ifNotExists bool) string

func FormatDate

func FormatDate(n int) (s string)

func FromDate

func FromDate(n int) (t time.Time, ok bool)

func FromUnix

func FromUnix(ms int64) time.Time

func IsDate

func IsDate(n int) bool

func LimitTime

func LimitTime(t time.Time, prec int) time.Time

func Now

func Now(prec int) time.Time

func NowDate

func NowDate() int

func NowUnix

func NowUnix() int64

func ParseDate

func ParseDate(s string) int

func ToDate

func ToDate(t time.Time) int

func ToUnix

func ToUnix(t time.Time) int64

Types

type Column

type Column struct {
	Name string
	query.Operand
	// contains filtered or unexported fields
}

func (*Column) Clone

func (c *Column) Clone(name string, collapse int) *Column

func (*Column) Qualifier

func (c *Column) Qualifier(a ...string) query.Operand

func (*Column) Rename

func (c *Column) Rename(s string)

type Columns

type Columns []*Column

func (Columns) Empty

func (a Columns) Empty() bool

func (Columns) Len

func (a Columns) Len() int

func (Columns) Map

func (a Columns) Map() (m map[string]interface{})

func (Columns) Slice

func (a Columns) Slice() (b []interface{})

func (Columns) Strings

func (a Columns) Strings() (b []string)

type Field

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

func (*Field) CanNil

func (f *Field) CanNil() bool

func (*Field) Is

func (f *Field) Is(o uint) bool

func (*Field) IsAuto

func (f *Field) IsAuto() bool

func (*Field) IsEncoding

func (f *Field) IsEncoding() bool

func (*Field) IsInline

func (f *Field) IsInline() bool

func (*Field) IsMany

func (f *Field) IsMany() bool

func (*Field) IsOne

func (f *Field) IsOne() bool

func (*Field) Type

func (f *Field) Type() reflect.Type

type Huge

type Huge struct {
	Starter  query.Starter
	Querier  Querier
	DealName func(string) string
	TimePrec int
}

func Open

func Open(driverName, dataSourceName string) (h Huge, err error)

func (Huge) Begin

func (h Huge) Begin() (_ Huge, err error)

func (Huge) Close

func (h Huge) Close() error

func (Huge) Commit

func (h Huge) Commit() (err error)

func (Huge) Create

func (h Huge) Create(i interface{}) (interface{}, error)

Create T returns bool, []T returns int, map[]T returns map[]struct{}.

func (Huge) Delete

func (h Huge) Delete(i interface{}) (interface{}, error)

Delete T returns bool, []T returns map[int]struct{}, map[]T returns map[]struct{}.

func (Huge) DeleteBy

func (h Huge) DeleteBy(primaryKeys, row interface{}) (int64, error)

DeleteBy PK, []PK, map[PK] returns the number of rows affected by delete.

func (Huge) Driver

func (h Huge) Driver() driver.Driver

func (Huge) Exec

func (h Huge) Exec(q query.Expression) (sql.Result, error)

func (Huge) Expand

func (h Huge) Expand(q query.Expression) (string, []interface{}, error)

func (Huge) LimitTime

func (h Huge) LimitTime(t time.Time) time.Time

func (Huge) Load

func (h Huge) Load(i interface{}) (interface{}, error)

Load T returns bool, []T returns int, map[]T returns map[]struct{}.

func (Huge) Now

func (h Huge) Now() time.Time

func (Huge) Ping

func (h Huge) Ping() error

func (Huge) Prepare

func (h Huge) Prepare(q query.Expression) (*sql.Stmt, []interface{}, error)

func (Huge) Q

func (h Huge) Q(a ...query.Expression) *Rows

func (Huge) Query

func (h Huge) Query(q query.Expression) *Rows

func (Huge) Read

func (h Huge) Read(i interface{}, columns ...string) (interface{}, error)

Read *T returns bool, []T returns map[int]struct{}, map[]*T returns map[]struct{}.

func (Huge) ReadBy

func (h Huge) ReadBy(primaryKeys, row interface{}, columns ...string) (interface{}, error)

ReadBy PK returns *T, []PK returns []*T, map[PK] returns map[PK]*T or []*T only if without PK column.

func (Huge) Rollback

func (h Huge) Rollback() (err error)

func (Huge) SetConnMaxLifetime

func (h Huge) SetConnMaxLifetime(d time.Duration)

func (Huge) SetMaxIdleConns

func (h Huge) SetMaxIdleConns(n int)

func (Huge) SetMaxOpenConns

func (h Huge) SetMaxOpenConns(n int)

func (Huge) Stats

func (h Huge) Stats() sql.DBStats

func (Huge) Update

func (h Huge) Update(i interface{}, columns ...string) (interface{}, error)

Update T returns bool, []T returns map[int]struct{}, map[]T returns map[]struct{}.

func (Huge) UpdateBy

func (h Huge) UpdateBy(primaryKeys, row interface{}, columns ...string) (int64, error)

UpdateBy PK, []PK, map[PK] returns the number of rows affected by update.

func (Huge) Upsert

func (h Huge) Upsert(z bool, i interface{}, columns ...string) (ok bool, err error)

Upsert if z is true, PK = 0 Create, PK > 0 Update, PK < 0 noop; otherwise if Read PK returns true then Update else Create.

type Kind

type Kind int
const (
	Interface Kind = iota
	Bool
	Int
	Int8
	Int16
	Int32
	Int64
	Uint
	Uint8
	Uint16
	Uint32
	Uint64
	Float32
	Float64
	String
	Time
	Bytes
)

type Querier

type Querier interface {
	Exec(string, ...interface{}) (sql.Result, error)
	Prepare(string) (*sql.Stmt, error)
	Query(string, ...interface{}) (*sql.Rows, error)
	QueryRow(string, ...interface{}) *sql.Row
}

type Rows

type Rows struct {
	DealName func(string) string
	// contains filtered or unexported fields
}

func (*Rows) All

func (r *Rows) All(i interface{}) error

All *[]T, map[PK]T, *[][] or *[]map[string].

func (*Rows) Close

func (r *Rows) Close() error

func (*Rows) Columns

func (r *Rows) Columns() ([]string, error)

func (*Rows) Err

func (r *Rows) Err() error

func (*Rows) Next

func (r *Rows) Next() bool

func (*Rows) One

func (r *Rows) One(i interface{}) (ok bool, err error)

One Scan and Close, be careful with sql.RawBytes.

func (*Rows) Scan

func (r *Rows) Scan(i interface{}) error

Scan T, [] or map[string]. For straightforward Scan just given an array.

type Struct

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

type Table

type Table struct {
	Name string
	query.Operand
	// contains filtered or unexported fields
}

func NewTable

func NewTable(i interface{}) *Table

func (*Table) AutoIncrement

func (t *Table) AutoIncrement() *Column

func (*Table) AutoNow

func (t *Table) AutoNow() *Column

func (*Table) AutoNowAdd

func (t *Table) AutoNowAdd() *Column

func (*Table) CreateTable

func (t *Table) CreateTable(s query.Starter, temporary, ifNotExists bool) (string, error)

func (*Table) Filter

func (t *Table) Filter(columns ...string) Columns

func (*Table) Find

func (t *Table) Find(s string) *Column

func (*Table) PrimaryKey

func (t *Table) PrimaryKey() *Column

func (*Table) Qualifier

func (t *Table) Qualifier(a ...string) query.Operand

func (*Table) Rename

func (t *Table) Rename(s string)

func (*Table) Version

func (t *Table) Version() *Column

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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