goen

package module
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2020 License: MIT Imports: 15 Imported by: 0

README

Build Status wercker status Coverage Status godoc Go Report Card

goen

goen is a typesafe GOlang ENtity interface for relational databases.

It provides a way of programmatically to interact with relational databases. It aims to implement in a go way and not to provide fully ORM features.

goen has following concepts:

  • No any fields or methods introducing into user's struct
  • Work with plain go's database/sql
  • Based on RTTI
  • Support bulk operation
  • Go generate is used only for utilities, it's not a core logic

Installation

go get -u github.com/kamichidu/goen/...

goen provides a binary tool used by go generate, please be sure that $GOPATH/bin is on your $PATH .

Usage

Write your first entity is.

package entity

type User struct {
    UserID int `goen:"" primary_key:""`
    Name, Email, PasswordHash string
}

Then put the following on any file of that package:

//go:generate goen -o goen.go

Now, all you have to do is run go generate ./... and a goen.go file will be generated.

Define entities

An entity is just a go struct have a struct tag goen:"" . All fields of this struct will be columns in the database table. An entity also needs to have one primary key. The primary key is defined using the primary_key:"" struct tag on the primary key fields.

Let's review the rules and conventions for entity fields:

  • All the fields with basic types or types that implement sql.Scanner and driver.Valuer will be considered a column in the table of their matching type.
  • By default, the name of a table/view will be the name of the struct converted to lower snake case (e.g.User => user, UserFriend => user_friend)
  • By default, the name of a column will be the name of the struct field converted to lower snake case (e.g. UserName => user_name, UserID => user_id). You can override it with the struct tag column:"custom_name".

Struct tags

Tag Description
goen:"" Indicates this struct as an entity. goen finds structs that have this struct tag.
table:"table_name" Specifies a table name.
view:"view_name" Specifies a view name for readonly entity.
primary_key:"" Indicates this field is a part of primary key
primary_key:"column_name" Indicates this field is a part of primary key and specifies a column name
primary_key:"column_name,omitempty" Indicates this field is a part of primary key, specifies a column name and this field is omitting if empty
primary_key:",omitempty" Indicates this field is a part of primary key, and this field is omitting if empty
column:"column_name" Specifies a column name
column:"column_name,omitempty" Specifies a column name and this field is omitting if empty
column:",omitempty" Specifies this field is omitting if empty
foreign_key:"column_name" Indicates this field is referencing another entity, and specifies keys
foreign_key:"column_name1,column_name2:reference_column_name" Indicates this field is referencing another entity, and specifies key pairs
ignore:"" Specifies this columns is to be ignored

Documentation

Overview

Example (BulkOperation)
package main

import (
	"database/sql"
	"fmt"
	"log"
	"os"
	"strings"

	"github.com/kamichidu/goen"

	_ "github.com/kamichidu/goen/dialect/sqlite3"
	_ "github.com/mattn/go-sqlite3"
)

func main() {
	db, err := sql.Open("sqlite3", "./sqlite.db")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	ddl := []string{
		`drop table if exists testing`,
		`create table testing (id integer primary key, name varchar(256))`,
	}
	if _, err := db.Exec(strings.Join(ddl, ";")); err != nil {
		panic(err)
	}

	dbc := goen.NewDBContext("sqlite3", db)

	// set patch compiler, it compiles patches to bulk
	dbc.Compiler = goen.BulkCompiler

	for i := 0; i < 3; i++ {
		dbc.Patch(goen.InsertPatch(
			"testing",
			[]string{"name"},
			[]interface{}{fmt.Sprintf("name-%d", i)},
		))
	}
	dbc.DebugMode(true)
	dbc.Logger = log.New(os.Stdout, "", 0)
	if err := dbc.SaveChanges(); err != nil {
		panic(err)
	}
}
Output:

goen: "INSERT INTO `testing` (`name`) VALUES (?),(?),(?)" with [name-0 name-1 name-2]
Example (CachingPreparedStatements)
package main

import (
	"database/sql"
	"fmt"
	"strings"

	"github.com/kamichidu/goen"

	_ "github.com/kamichidu/goen/dialect/sqlite3"
	_ "github.com/mattn/go-sqlite3"
)

func main() {
	db, err := sql.Open("sqlite3", "./sqlite.db")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	ddl := []string{
		`drop table if exists testing`,
		`create table testing (id integer primary key, name varchar(256))`,
	}
	if _, err := db.Exec(strings.Join(ddl, ";")); err != nil {
		panic(err)
	}

	// goen.StmtCacher caches *sql.Stmt until closed.
	preparedQueryRunner := goen.NewStmtCacher(db)
	defer preparedQueryRunner.Close()

	dbc := goen.NewDBContext("sqlite3", db)
	// set preparedQueryRunner to dbc.QueryRunner.
	dbc.QueryRunner = preparedQueryRunner

	fmt.Printf("cached statements %v\n", preparedQueryRunner.StmtStats().CachedStmts)

	err = goen.TxScope(dbc.DB.Begin())(func(tx *sql.Tx) error {
		// txc also use preparedQueryRunner when dbc uses it.
		txc := dbc.UseTx(tx)
		txc.Patch(&goen.Patch{
			Kind:      goen.PatchInsert,
			TableName: "testing",
			Columns:   []string{"name"},
			Values:    []interface{}{"kamichidu"},
		})
		return txc.SaveChanges()
	})
	if err != nil {
		panic(err)
	}
	fmt.Printf("cached statements %v\n", preparedQueryRunner.StmtStats().CachedStmts)

	var count int
	if err := dbc.QueryRow(`select count(*) from testing`).Scan(&count); err != nil {
		panic(err)
	}
	fmt.Printf("%v records found\n", count)
	fmt.Printf("cached statements %v\n", preparedQueryRunner.StmtStats().CachedStmts)
}
Output:

cached statements 0
cached statements 1
1 records found
cached statements 2
Example (QueryCount)
package main

import (
	"database/sql"
	"fmt"
	"strings"

	"github.com/kamichidu/goen"

	_ "github.com/kamichidu/goen/dialect/sqlite3"
	_ "github.com/mattn/go-sqlite3"
)

func main() {
	db, err := sql.Open("sqlite3", "./sqlite.db")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	ddl := []string{
		`drop table if exists testing`,
		`create table testing (id integer primary key, name varchar(256))`,
	}
	if _, err := db.Exec(strings.Join(ddl, ";")); err != nil {
		panic(err)
	}

	dbc := goen.NewDBContext("sqlite3", db)

	for i := 0; i < 3; i++ {
		dbc.Patch(goen.InsertPatch(
			"testing",
			[]string{"name"},
			[]interface{}{fmt.Sprintf("name-%d", i)},
		))
	}
	if err := dbc.SaveChanges(); err != nil {
		panic(err)
	}

	row := dbc.QueryRow(`select count(*) from testing`)

	var count int64
	if err := row.Scan(&count); err != nil {
		panic(err)
	}
	fmt.Printf("dbc founds %d records\n", count)

	var records []map[string]interface{}
	rows, err := dbc.Query(`select id, name from testing`)
	if err != nil {
		panic(err)
	}
	defer rows.Close()

	if err := dbc.Scan(rows, &records); err != nil {
		panic(err)
	}

	fmt.Print("records:\n")
	for _, record := range records {
		fmt.Printf("id=%d name=%q\n", record["id"], record["name"])
	}
}
Output:

dbc founds 3 records
records:
id=1 name="name-0"
id=2 name="name-1"
id=3 name="name-2"
Example (Transaction)
package main

import (
	"database/sql"
	"fmt"
	"strings"

	"github.com/kamichidu/goen"

	_ "github.com/kamichidu/goen/dialect/sqlite3"
	_ "github.com/mattn/go-sqlite3"
)

func main() {
	db, err := sql.Open("sqlite3", "./sqlite.db")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	ddl := []string{
		`drop table if exists testing`,
		`create table testing (id integer primary key, name varchar(256))`,
	}
	if _, err := db.Exec(strings.Join(ddl, ";")); err != nil {
		panic(err)
	}

	dbc := goen.NewDBContext("sqlite3", db)

	tx, err := dbc.DB.Begin()
	if err != nil {
		panic(err)
	}
	txc := dbc.UseTx(tx)
	if txc.Tx != tx {
		panic("UseTx makes clones DBContext and set Tx with given value")
	}
	txc.Patch(&goen.Patch{
		Kind:      goen.PatchInsert,
		TableName: "testing",
		Columns:   []string{"name"},
		Values:    []interface{}{"kamichidu"},
	})
	if err := txc.SaveChanges(); err != nil {
		panic(err)
	}

	func() {
		rows, err := dbc.Query(`select * from testing`)
		if err != nil {
			panic(err)
		}
		defer rows.Close()

		var records []map[string]interface{}
		if err := dbc.Scan(rows, &records); err != nil {
			panic(err)
		}
		fmt.Printf("dbc founds %d records when not committed yet\n", len(records))
	}()
	func() {
		rows, err := txc.Query(`select * from testing`)
		if err != nil {
			panic(err)
		}
		defer rows.Close()

		var records []map[string]interface{}
		if err := txc.Scan(rows, &records); err != nil {
			panic(err)
		}
		fmt.Printf("txc founds %d records when not committed yet since it's same transaction\n", len(records))
	}()

	if err := tx.Commit(); err != nil {
		panic(err)
	}

	func() {
		rows, err := dbc.Query(`select * from testing`)
		if err != nil {
			panic(err)
		}
		defer rows.Close()

		var records []map[string]interface{}
		if err := dbc.Scan(rows, &records); err != nil {
			panic(err)
		}
		fmt.Printf("dbc founds %d records after committed\n", len(records))
	}()
	func() {
		_, err := txc.Query(`select * from testing`)
		if err == sql.ErrTxDone {
			fmt.Print("txc returns an error sql.ErrTxDone after committed\n")
		}
	}()
}
Output:

dbc founds 0 records when not committed yet
txc founds 1 records when not committed yet since it's same transaction
dbc founds 1 records after committed
txc returns an error sql.ErrTxDone after committed

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(name string, dialect dialect.Dialect)

func TxScope

func TxScope(tx *sql.Tx, err error) txScope
Example (Do)
package main

import (
	"database/sql"

	"github.com/kamichidu/goen"
	_ "github.com/mattn/go-sqlite3"
)

func main() {
	db, err := sql.Open("sqlite3", "./sqlite.db")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	dbc := goen.NewDBContext("sqlite3", db)

	err = goen.TxScope(db.Begin()).Do(func(tx *sql.Tx) error {
		txc := dbc.UseTx(tx)

		// manipulate with txc
		txc.QueryRow("select 1")

		// return nil, will be called tx.Commit() by TxScope
		// otherwise, will be called tx.Rollback()
		return nil
	})
	if err != nil {
		panic(err)
	}

}
Output:

Example (FuncCall)
package main

import (
	"database/sql"

	"github.com/kamichidu/goen"
	_ "github.com/mattn/go-sqlite3"
)

func main() {
	db, err := sql.Open("sqlite3", "./sqlite.db")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	dbc := goen.NewDBContext("sqlite3", db)

	err = goen.TxScope(db.Begin())(func(tx *sql.Tx) error {
		txc := dbc.UseTx(tx)

		// manipulate with txc
		txc.QueryRow("select 1")

		// return nil, will be called tx.Commit() by TxScope
		// otherwise, will be called tx.Rollback()
		return nil
	})
	if err != nil {
		panic(err)
	}

}
Output:

Types

type BulkCompilerOptions added in v0.0.6

type BulkCompilerOptions struct {
	// MaxPatches limits values per bulk operations.
	// MaxPatches<=0 means unlimited.
	MaxPatches int
}

func (*BulkCompilerOptions) Compile added in v0.0.6

func (c *BulkCompilerOptions) Compile(options *CompilerOptions) (sqlizers *SqlizerList)

type Cardinality added in v0.0.10

type Cardinality int
const (
	CardinalityNone Cardinality = iota
	CardinalityOneToMany
	CardinalityManyToOne
)

type CompilerHook added in v0.0.2

type CompilerHook interface {
	PostInsertBuilder(sqr.InsertBuilder) sqr.Sqlizer
	PostUpdateBuilder(sqr.UpdateBuilder) sqr.Sqlizer
	PostDeleteBuilder(sqr.DeleteBuilder) sqr.Sqlizer
}
Example
package main

import (
	"database/sql"
	"fmt"
	"strings"

	"github.com/Masterminds/squirrel"
	"github.com/kamichidu/goen"

	_ "github.com/kamichidu/goen/dialect/sqlite3"
	_ "github.com/mattn/go-sqlite3"
)

type exampleHook struct {
	PostInsertBuilderHandler func(squirrel.InsertBuilder) squirrel.Sqlizer
	PostUpdateBuilderHandler func(squirrel.UpdateBuilder) squirrel.Sqlizer
	PostDeleteBuilderHandler func(squirrel.DeleteBuilder) squirrel.Sqlizer
}

func (v *exampleHook) PostInsertBuilder(stmt squirrel.InsertBuilder) squirrel.Sqlizer {
	if v.PostInsertBuilderHandler != nil {
		return v.PostInsertBuilderHandler(stmt)
	} else {
		return stmt
	}
}

func (v *exampleHook) PostUpdateBuilder(stmt squirrel.UpdateBuilder) squirrel.Sqlizer {
	if v.PostUpdateBuilderHandler != nil {
		return v.PostUpdateBuilderHandler(stmt)
	} else {
		return stmt
	}
}

func (v *exampleHook) PostDeleteBuilder(stmt squirrel.DeleteBuilder) squirrel.Sqlizer {
	if v.PostDeleteBuilderHandler != nil {
		return v.PostDeleteBuilderHandler(stmt)
	} else {
		return stmt
	}
}

func main() {
	db, err := sql.Open("sqlite3", "./sqlite.db")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	ddl := []string{
		`drop table if exists testing`,
		`create table testing (id integer primary key, name varchar(256))`,
	}
	if _, err := db.Exec(strings.Join(ddl, ";")); err != nil {
		panic(err)
	}

	dbc := goen.NewDBContext("sqlite3", db)
	dbc.Compiler = goen.CompilerWithHook(goen.DefaultCompiler, &exampleHook{
		PostInsertBuilderHandler: func(stmt squirrel.InsertBuilder) squirrel.Sqlizer {
			fmt.Println("PostInsertBuilder: hello")
			return stmt
		},
		PostUpdateBuilderHandler: func(stmt squirrel.UpdateBuilder) squirrel.Sqlizer {
			fmt.Println("PostUpdateBuilder: compiler")
			return stmt
		},
		PostDeleteBuilderHandler: func(stmt squirrel.DeleteBuilder) squirrel.Sqlizer {
			fmt.Println("PostDeleteBuilder: hook")
			return stmt
		},
	})
	dbc.Patch(goen.InsertPatch("testing", []string{"name"}, []interface{}{"ExampleCompilerHook"}))
	dbc.Patch(goen.UpdatePatch("testing", []string{"name"}, []interface{}{"ExampleCompilerHook"}, nil))
	dbc.Patch(goen.DeletePatch("testing", nil))
	if err := dbc.SaveChanges(); err != nil {
		panic(err)
	}
}
Output:

PostInsertBuilder: hello
PostUpdateBuilder: compiler
PostDeleteBuilder: hook

type CompilerOptions

type CompilerOptions struct {
	Dialect dialect.Dialect

	Patches *PatchList

	Hook CompilerHook
}

type DBContext

type DBContext struct {
	// This field is readonly.
	// Sets via NewDBContext().
	DB *sql.DB

	// This field is readonly.
	// Sets via UseTx(); or nil.
	Tx *sql.Tx

	// The patch compiler.
	// This is used when need to compile patch to query.
	Compiler PatchCompiler

	// The logger for debugging.
	// This is used when DebugMode(true).
	Logger Logger

	// The depth limit to avoid circular Include work.
	// When this value is positive, Include works N-times.
	// When this value is negative, Include works unlimited.
	// When this value is 0, Include never works.
	// Default is 10.
	MaxIncludeDepth int

	// The runner for each query.
	// This field is indented to hold one of *sql.DB, *sql.Tx or *StmtCacher.
	QueryRunner QueryRunner
	// contains filtered or unexported fields
}

DBContext holds *sql.DB (and *sql.Tx) with contextual values. goen is intended to work with *sql.DB (and *sql.Tx) via DBContext.

func NewDBContext

func NewDBContext(dialectName string, db *sql.DB) *DBContext

NewDBContext creates DBContext with given dialectName and db.

func (*DBContext) CompilePatch

func (dbc *DBContext) CompilePatch() *SqlizerList

CompilePatch compiles patches added by Patch() to SqlizerList. When this function is called, will clear internal patch buffer.

func (*DBContext) DebugMode

func (dbc *DBContext) DebugMode(enabled bool)

DebugMode sets debug flag.

func (*DBContext) Dialect

func (dbc *DBContext) Dialect() dialect.Dialect

Dialect returns dialect.Dialect holds by this context.

func (*DBContext) Include

func (dbc *DBContext) Include(v interface{}, sc *ScopeCache, loader IncludeLoader) error

func (*DBContext) IncludeContext

func (dbc *DBContext) IncludeContext(ctx context.Context, v interface{}, sc *ScopeCache, loader IncludeLoader) error

func (*DBContext) Patch

func (dbc *DBContext) Patch(v *Patch)

Patch adds raw patch into the buffer; without executing a query.

func (*DBContext) Query

func (dbc *DBContext) Query(query string, args ...interface{}) (*sql.Rows, error)

func (*DBContext) QueryContext

func (dbc *DBContext) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

func (*DBContext) QueryRow

func (dbc *DBContext) QueryRow(query string, args ...interface{}) *sql.Row

func (*DBContext) QueryRowContext

func (dbc *DBContext) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

func (*DBContext) QueryRowSqlizer

func (dbc *DBContext) QueryRowSqlizer(sqlizer sqr.Sqlizer) *sql.Row

func (*DBContext) QueryRowSqlizerContext

func (dbc *DBContext) QueryRowSqlizerContext(ctx context.Context, sqlizer sqr.Sqlizer) *sql.Row

func (*DBContext) QuerySqlizer

func (dbc *DBContext) QuerySqlizer(sqlizer sqr.Sqlizer) (*sql.Rows, error)

func (*DBContext) QuerySqlizerContext

func (dbc *DBContext) QuerySqlizerContext(ctx context.Context, sqlizer sqr.Sqlizer) (*sql.Rows, error)

func (*DBContext) SaveChanges

func (dbc *DBContext) SaveChanges() error

func (*DBContext) SaveChangesContext

func (dbc *DBContext) SaveChangesContext(ctx context.Context) error

func (*DBContext) Scan

func (dbc *DBContext) Scan(rows *sql.Rows, v interface{}) error

func (*DBContext) UseTx

func (dbc *DBContext) UseTx(tx *sql.Tx) *DBContext

UseTx returns clone of current DBContext with given *sql.Tx.

type IncludeBuffer

type IncludeBuffer list.List

func (*IncludeBuffer) AddRecords

func (l *IncludeBuffer) AddRecords(records interface{})

type IncludeLoader

type IncludeLoader interface {
	Load(ctx context.Context, later *IncludeBuffer, sc *ScopeCache, records interface{}) error
}

type IncludeLoaderFunc

type IncludeLoaderFunc func(context.Context, *IncludeBuffer, *ScopeCache, interface{}) error

func (IncludeLoaderFunc) Load

func (fn IncludeLoaderFunc) Load(ctx context.Context, later *IncludeBuffer, sc *ScopeCache, records interface{}) error

type IncludeLoaderList

type IncludeLoaderList []IncludeLoader

func (*IncludeLoaderList) Append

func (list *IncludeLoaderList) Append(v ...IncludeLoader)

func (IncludeLoaderList) Load

func (list IncludeLoaderList) Load(ctx context.Context, later *IncludeBuffer, sc *ScopeCache, records interface{}) error

type Logger

type Logger interface {
	Print(...interface{})
	Printf(string, ...interface{})
}

type LoggerFunc added in v0.0.5

type LoggerFunc func(...interface{})

func (LoggerFunc) Print added in v0.0.5

func (fn LoggerFunc) Print(args ...interface{})

func (LoggerFunc) Printf added in v0.0.5

func (fn LoggerFunc) Printf(format string, args ...interface{})

type MapRowKey

type MapRowKey struct {
	Table string

	Key map[string]interface{}
}

func (*MapRowKey) RowKey

func (key *MapRowKey) RowKey() ([]string, []interface{})

func (*MapRowKey) TableName

func (key *MapRowKey) TableName() string

func (*MapRowKey) ToSql

func (key *MapRowKey) ToSql() (string, []interface{}, error)

func (*MapRowKey) ToSqlizerWithDialect added in v0.0.1

func (key *MapRowKey) ToSqlizerWithDialect(dialect dialect.Dialect) sqr.Sqlizer

type MetaColumn added in v0.0.1

type MetaColumn interface {
	// Field gets go struct field associated with this column.
	Field() reflect.StructField

	// OmitEmpty indicates this column allowing to omit column specifier on a insert statement.
	OmitEmpty() bool

	// PartOfPrimaryKey indicates this column is part of primary key of the table.
	PartOfPrimaryKey() bool

	// ColumnName gets this column name.
	ColumnName() string
}

MetaColumn represents a column meta info.

type MetaSchema

type MetaSchema interface {
	// Register adds given object type to this MetaSchema.
	Register(entity interface{})

	// Compute computes meta schemata for registered entities.
	Compute()

	// LoadOf gets meta schema of table that associated with given entity.
	LoadOf(entity interface{}) MetaTable

	// KeyStringFromRowKey gets identity string for given RowKey.
	KeyStringFromRowKey(RowKey) string

	// PrimaryKeyOf gets RowKey that represents given entity.
	PrimaryKeyOf(entity interface{}) RowKey

	// ReferenceKeysOf gets RowKeys that references to entity by other entities.
	ReferenceKeysOf(entity interface{}) []RowKey

	// OneToManyReferenceKeysOf gets RowKeysone that references to entity by other entities with one to many cardinal.
	OneToManyReferenceKeysOf(entity interface{}) []RowKey

	// ManyToOneReferenceKeysOf gets RowKeysone that references to entity by other entities with many to one cardinal.
	ManyToOneReferenceKeysOf(entity interface{}) []RowKey

	// InsertPatchOf gets a patch that represents insert statement.
	InsertPatchOf(entity interface{}) *Patch

	// UpdatePatchOf gets a patch that represents update statement.
	UpdatePatchOf(entity interface{}) *Patch

	// DeletePatchOf gets a patch that represents delete statement.
	DeletePatchOf(entity interface{}) *Patch
}

MetaSchema manages meta schemata computed by struct (tags). Provide some utility functions for using with DBContext.

func NewMetaSchema added in v0.0.1

func NewMetaSchema() MetaSchema

NewMetaSchema creates new MetaSchema object.

type MetaTable added in v0.0.1

type MetaTable interface {
	// Type gets go struct type associated with this table.
	Type() reflect.Type

	// TableName gets a table name.
	TableName() string

	// PrimaryKey gets primary key meta columns.
	PrimaryKey() []MetaColumn

	// ReferenceKeys gets all reference meta columns by other entities.
	ReferenceKeys() [][]MetaColumn

	// OneToManyReferenceKeys gets all one to many reference meta columns by other entities.
	OneToManyReferenceKeys() [][]MetaColumn

	// ManyToOneReferenceKeys gets all many to one reference meta columns by other entities.
	ManyToOneReferenceKeys() [][]MetaColumn

	// Columns gets all meta columns of this table.
	Columns() []MetaColumn
}

MetaTable represents a table meta info.

type Patch

type Patch struct {
	Kind PatchKind

	TableName string

	RowKey RowKey

	Columns []string

	Values []interface{}
}

func DeletePatch

func DeletePatch(tableName string, rowKey RowKey) *Patch

func InsertPatch

func InsertPatch(tableName string, columns []string, values []interface{}) *Patch

func UpdatePatch

func UpdatePatch(tableName string, columns []string, values []interface{}, rowKey RowKey) *Patch

type PatchCompiler

type PatchCompiler interface {
	Compile(*CompilerOptions) *SqlizerList
}
var (
	DefaultCompiler PatchCompiler = &defaultCompiler{}

	BulkCompiler PatchCompiler = &BulkCompilerOptions{}
)

func CompilerWithHook added in v0.0.2

func CompilerWithHook(c PatchCompiler, v CompilerHook) PatchCompiler

type PatchCompilerFunc

type PatchCompilerFunc func(*CompilerOptions) *SqlizerList

func (PatchCompilerFunc) Compile

func (fn PatchCompilerFunc) Compile(opts *CompilerOptions) (sqlizers *SqlizerList)

type PatchElement

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

PatchElement is typed version of list.Element

func (*PatchElement) GetValue

func (e *PatchElement) GetValue() *Patch

GetValue gets the value stored with this element.

func (*PatchElement) Next

func (e *PatchElement) Next() *PatchElement

Next returns the next list element or nil.

func (*PatchElement) Prev

func (e *PatchElement) Prev() *PatchElement

Prev returns the previous list element or nil.

func (*PatchElement) SetValue

func (e *PatchElement) SetValue(v *Patch)

SetValue sets value with this element.

type PatchKind

type PatchKind int
const (
	PatchInsert PatchKind = iota
	PatchUpdate
	PatchDelete
)

type PatchList

type PatchList list.List

PatchList is typed version of list.List

func NewPatchList

func NewPatchList() *PatchList

func (*PatchList) Back

func (l *PatchList) Back() *PatchElement

Back is typed version of (*list.List).Back

func (*PatchList) Front

func (l *PatchList) Front() *PatchElement

Front is typed version of (*list.List).Front

func (*PatchList) Init

func (l *PatchList) Init() *PatchList

Init is typed version of (*list.List).Init

func (*PatchList) InsertAfter

func (l *PatchList) InsertAfter(v *Patch, mark *PatchElement) *PatchElement

InsertAfter is typed version of (*list.List).InsertAfter

func (*PatchList) InsertBefore

func (l *PatchList) InsertBefore(v *Patch, mark *PatchElement) *PatchElement

InsertBefore is typed version of (*list.List).InsertBefore

func (*PatchList) Len

func (l *PatchList) Len() int

Len is typed version of (*list.List).Len

func (*PatchList) MoveAfter

func (l *PatchList) MoveAfter(e *PatchElement, mark *PatchElement)

MoveAfter is typed version of (*list.List).MoveAfter

func (*PatchList) MoveBefore

func (l *PatchList) MoveBefore(e *PatchElement, mark *PatchElement)

MoveBefore is typed version of (*list.List).MoveBefore

func (*PatchList) MoveToBack

func (l *PatchList) MoveToBack(e *PatchElement)

MoveToBack is typed version of (*list.List).MoveToBack

func (*PatchList) MoveToFront

func (l *PatchList) MoveToFront(e *PatchElement)

MoveToFront is typed version of (*list.List).MoveToFront

func (*PatchList) PushBack

func (l *PatchList) PushBack(v *Patch) *PatchElement

PushBack is typed version of (*list.List).PushBack

func (*PatchList) PushBackList

func (l *PatchList) PushBackList(other *PatchList)

PushBackList is typed version of (*list.List).PushBackList

func (*PatchList) PushFront

func (l *PatchList) PushFront(v *Patch) *PatchElement

PushFront is typed version of (*list.List).PushFront

func (*PatchList) PushFrontList

func (l *PatchList) PushFrontList(other *PatchList)

PushFrontList is typed version of (*list.List).PushFrontList

func (*PatchList) Remove

func (l *PatchList) Remove(e *PatchElement) *Patch

Remove is typed version of (*list.List).Remove

type QueryRunner

type QueryRunner interface {
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)

	QueryRowContext(context.Context, string, ...interface{}) *sql.Row

	QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
}

QueryRunner ...

type RowKey

type RowKey interface {
	sqr.Sqlizer

	TableName() string

	RowKey() ([]string, []interface{})

	ToSqlizerWithDialect(dialect.Dialect) sqr.Sqlizer
}

type ScopeCache

type ScopeCache struct {
	Meta MetaSchema
	// contains filtered or unexported fields
}

func NewScopeCache

func NewScopeCache(meta MetaSchema) *ScopeCache

func (*ScopeCache) AddObject

func (sc *ScopeCache) AddObject(v interface{})

func (*ScopeCache) Dump added in v0.0.10

func (sc *ScopeCache) Dump(w io.Writer)

for debugging

func (*ScopeCache) GetObject

func (sc *ScopeCache) GetObject(cardinality Cardinality, rowKey RowKey) interface{}

GetObject gets stored object with given cardinality

func (*ScopeCache) HasObject

func (sc *ScopeCache) HasObject(cardinality Cardinality, rowKey RowKey) bool

HasObject checks stored object with given cardinality is exists or not.

func (*ScopeCache) RemoveObject

func (sc *ScopeCache) RemoveObject(v interface{})

type SqlizerElement

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

SqlizerElement is typed version of list.Element

func (*SqlizerElement) GetValue

func (e *SqlizerElement) GetValue() squirrel.Sqlizer

GetValue gets the value stored with this element.

func (*SqlizerElement) Next

func (e *SqlizerElement) Next() *SqlizerElement

Next returns the next list element or nil.

func (*SqlizerElement) Prev

func (e *SqlizerElement) Prev() *SqlizerElement

Prev returns the previous list element or nil.

func (*SqlizerElement) SetValue

func (e *SqlizerElement) SetValue(v squirrel.Sqlizer)

SetValue sets value with this element.

type SqlizerList

type SqlizerList list.List

SqlizerList is typed version of list.List

func NewSqlizerList

func NewSqlizerList() *SqlizerList

func (*SqlizerList) Back

func (l *SqlizerList) Back() *SqlizerElement

Back is typed version of (*list.List).Back

func (*SqlizerList) Front

func (l *SqlizerList) Front() *SqlizerElement

Front is typed version of (*list.List).Front

func (*SqlizerList) Init

func (l *SqlizerList) Init() *SqlizerList

Init is typed version of (*list.List).Init

func (*SqlizerList) InsertAfter

func (l *SqlizerList) InsertAfter(v squirrel.Sqlizer, mark *SqlizerElement) *SqlizerElement

InsertAfter is typed version of (*list.List).InsertAfter

func (*SqlizerList) InsertBefore

func (l *SqlizerList) InsertBefore(v squirrel.Sqlizer, mark *SqlizerElement) *SqlizerElement

InsertBefore is typed version of (*list.List).InsertBefore

func (*SqlizerList) Len

func (l *SqlizerList) Len() int

Len is typed version of (*list.List).Len

func (*SqlizerList) MoveAfter

func (l *SqlizerList) MoveAfter(e *SqlizerElement, mark *SqlizerElement)

MoveAfter is typed version of (*list.List).MoveAfter

func (*SqlizerList) MoveBefore

func (l *SqlizerList) MoveBefore(e *SqlizerElement, mark *SqlizerElement)

MoveBefore is typed version of (*list.List).MoveBefore

func (*SqlizerList) MoveToBack

func (l *SqlizerList) MoveToBack(e *SqlizerElement)

MoveToBack is typed version of (*list.List).MoveToBack

func (*SqlizerList) MoveToFront

func (l *SqlizerList) MoveToFront(e *SqlizerElement)

MoveToFront is typed version of (*list.List).MoveToFront

func (*SqlizerList) PushBack

func (l *SqlizerList) PushBack(v squirrel.Sqlizer) *SqlizerElement

PushBack is typed version of (*list.List).PushBack

func (*SqlizerList) PushBackList

func (l *SqlizerList) PushBackList(other *SqlizerList)

PushBackList is typed version of (*list.List).PushBackList

func (*SqlizerList) PushFront

func (l *SqlizerList) PushFront(v squirrel.Sqlizer) *SqlizerElement

PushFront is typed version of (*list.List).PushFront

func (*SqlizerList) PushFrontList

func (l *SqlizerList) PushFrontList(other *SqlizerList)

PushFrontList is typed version of (*list.List).PushFrontList

func (*SqlizerList) Remove

Remove is typed version of (*list.List).Remove

type StmtCacher

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

StmtCacher implements QueryRunner. It caches prepared statements for executing or querying.

func NewStmtCacher

func NewStmtCacher(prep sqr.PreparerContext) *StmtCacher

NewStmtCacher returns new StmtCacher with given prep.

func (*StmtCacher) Close

func (pqr *StmtCacher) Close() error

Close closes and removes all cached prepared statements.

func (*StmtCacher) ExecContext

func (pqr *StmtCacher) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext executes given query with args via prepared statement.

func (*StmtCacher) Prepare

func (pqr *StmtCacher) Prepare(query string) (*sql.Stmt, error)

Prepare is only for implements squirrel.PreparerContext. DO NOT USE THIS.

func (*StmtCacher) PrepareContext

func (pqr *StmtCacher) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

PrepareContext returns new or cached prepared statement.

func (*StmtCacher) QueryContext

func (pqr *StmtCacher) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

QueryContext queries by given query with args via prepared statement.

func (*StmtCacher) QueryRowContext

func (pqr *StmtCacher) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

QueryRowContext queries a row by given query with args via prepared statement.

func (*StmtCacher) StmtStats

func (pqr *StmtCacher) StmtStats() StmtStats

StmtStats returns statistics values.

type StmtStats

type StmtStats struct {
	CachedStmts int
}

StmtStats holds some statistics values of StmtCacher.

Directories

Path Synopsis
cmd
test

Jump to

Keyboard shortcuts

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