sqldb

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2019 License: MIT Imports: 9 Imported by: 0

README

go-sqldb

GoDoc Build Status Coverage Status Go Report Card

go-sqldb is a utility library for Go. The mainly features:

  • create table from Go structure.
  • database error handling

Documentation

Documentation can be found at Godoc

LICENSE

MIT.

Documentation

Overview

Package sqldb helps create tables from Go structures.

Field tag format: `sqldb:"key[:value] key[:value]..."`. Available keys:

table: table name
col: column name, col:- to skip.
type: char, text and Go builtin types: string/bool/int/uint/int8...
precision: for string and char type, it's the 'length', such as precision:100,
           for float and double it's 'precision, exact', such as precision: 32,5.
dbtype: the final database type, it will override type and precision key
pk: primary key
autoincr: auto increament
notnull: not null
default: default value, '-' to disable default
unique: unique constraint name or empty
fk: foreign key: TABLE.COLUMN

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrAllowNoRows

func ErrAllowNoRows(err error) error

func ErrOnNoAffects

func ErrOnNoAffects(n int64, err, newErr error) error

func ErrOnNoAffectsResult

func ErrOnNoAffectsResult(res sql.Result, err, newErr error) error

func ErrOnNoRows

func ErrOnNoRows(err, newErr error) error

func Open

func Open(dialect DBDialect, config DBConfig) (*sql.DB, error)

func ResultLastInsertId

func ResultLastInsertId(result sql.Result, err error) (int64, error)

func ResultRowsAffected

func ResultRowsAffected(result sql.Result, err error) (int64, error)

func SnakeCase

func SnakeCase(s string) string

func TxDone

func TxDone(tx Tx, err *error) error

func TxDoneClose

func TxDoneClose(tx TxCloser, err *error) error

Types

type CheckIsExistGroup added in v0.0.6

type CheckIsExistGroup struct {
	Model      interface{}
	ResultName string
	Where      string
}

type Column

type Column struct {
	Name         string
	Type         string
	Precision    string
	DBType       string
	Primary      bool
	AutoIncr     bool
	Notnull      bool
	Default      bool
	DefaultVal   string
	Unique       bool
	UniqueName   string
	ForeignTable string
	ForeignCol   string

	Field reflect.StructField
}

type ColumnNameJoinRule

type ColumnNameJoinRule interface {
	Separator() string
	Append(buffer *bytes.Buffer, c string)
}

type ColumnNames

type ColumnNames []string

func (ColumnNames) Cond

func (c ColumnNames) Cond(cond, check string) string

func (ColumnNames) Contains

func (c ColumnNames) Contains(col string) bool

func (ColumnNames) Copy

func (c ColumnNames) Copy() ColumnNames

func (ColumnNames) InplaceRemove

func (c ColumnNames) InplaceRemove(cols ...string) ColumnNames

func (ColumnNames) Join

func (c ColumnNames) Join(rule ColumnNameJoinRule) string

func (ColumnNames) List

func (c ColumnNames) List() string

func (ColumnNames) NamedCond

func (c ColumnNames) NamedCond(cond, check string) string

func (ColumnNames) NamedList

func (c ColumnNames) NamedList() string

func (ColumnNames) NamedUpdate

func (c ColumnNames) NamedUpdate() string

func (ColumnNames) Placeholders added in v0.0.4

func (c ColumnNames) Placeholders() string

func (ColumnNames) Update

func (c ColumnNames) Update() string

type DBConfig

type DBConfig struct {
	Type        string            `json:"type" yaml:"type" toml:"type"`
	Host        string            `json:"host" yaml:"host" toml:"host"`
	Port        int               `json:"port" yaml:"port" toml:"port"`
	DBName      string            `json:"dbname" yaml:"dbname" toml:"dbname"`
	User        string            `json:"user" yaml:"user" toml:"user"`
	Password    string            `json:"password" yaml:"password" toml:"password"`
	MaxIdle     int               `json:"maxIdle" yaml:"maxIdle" toml:"maxIdle"`
	MaxOpen     int               `json:"maxOpen" yaml:"maxOpen" toml:"maxOpen"`
	MaxLifetime int               `json:"maxLifetime" yaml:"maxLifetime" toml:"maxLifetime"`
	Options     map[string]string `json:"options" yaml:"options" toml:"options"`
}

func (*DBConfig) ApplyDefault

func (d *DBConfig) ApplyDefault(def DBConfig)

func (*DBConfig) JoinOptions

func (d *DBConfig) JoinOptions(kvSep, optSep string) string

type DBDialect

type DBDialect interface {
	Type(typ, precision, val string) (dbtyp, defaultVal string, err error)
	DSN(config DBConfig) string
}

type MySQL

type MySQL struct{}

func (MySQL) DSN

func (MySQL) DSN(config DBConfig) string

func (MySQL) Type

func (m MySQL) Type(typ, precision, val string) (dbtyp, defval string, err error)

type NameMapper

type NameMapper func(string) string

type Postgres

type Postgres struct{}

func (Postgres) DSN

func (Postgres) DSN(config DBConfig) string

func (Postgres) Type

func (p Postgres) Type(typ, precision, val string) (dbtyp, defval string, err error)

type SQLBuilder added in v0.0.6

type SQLBuilder struct {
	SQLUtil *SQLUtil
	// contains filtered or unexported fields
}

func NewSQLBuilder added in v0.0.6

func NewSQLBuilder(su *SQLUtil) *SQLBuilder

func (*SQLBuilder) Delete added in v0.0.6

func (b *SQLBuilder) Delete(model interface{}, where string) string

func (*SQLBuilder) Insert added in v0.0.6

func (b *SQLBuilder) Insert(model interface{}) string

func (*SQLBuilder) InsertUnique added in v0.0.6

func (b *SQLBuilder) InsertUnique(model interface{}, checkExist string) string

func (*SQLBuilder) IsExist added in v0.0.6

func (b *SQLBuilder) IsExist(model interface{}, resultName string, where string) string

func (*SQLBuilder) MultiIsExist added in v0.0.6

func (b *SQLBuilder) MultiIsExist(groups ...CheckIsExistGroup) string

func (*SQLBuilder) Query added in v0.0.6

func (b *SQLBuilder) Query(model interface{}, columns []string, where string) string

func (*SQLBuilder) Update added in v0.0.6

func (b *SQLBuilder) Update(model interface{}, columns []string, where string) string

func (*SQLBuilder) WhereColumns added in v0.0.8

func (b *SQLBuilder) WhereColumns(cols ...string) string

func (*SQLBuilder) WithCache added in v0.0.7

func (b *SQLBuilder) WithCache(f func(b *SQLBuilder) string) string

func (*SQLBuilder) WithCacheAndIndex added in v0.0.7

func (b *SQLBuilder) WithCacheAndIndex(f func(b *SQLBuilder, index int) string, index, cap int) string

type SQLUtil

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

func NewSQLUtil

func NewSQLUtil(parser *TableParser, dialect DBDialect) *SQLUtil

func (*SQLUtil) CreateTableSQL

func (s *SQLUtil) CreateTableSQL(table Table) (string, error)

func (*SQLUtil) CreateTables

func (s *SQLUtil) CreateTables(db *sql.DB, models ...interface{}) error

func (*SQLUtil) DBDialect

func (s *SQLUtil) DBDialect() DBDialect

func (*SQLUtil) EscapeName added in v0.0.3

func (s *SQLUtil) EscapeName(name string) string

func (*SQLUtil) TableColumns

func (s *SQLUtil) TableColumns(v interface{}, excepts ...string) ColumnNames

func (*SQLUtil) TableName added in v0.0.6

func (s *SQLUtil) TableName(v interface{}) string

func (*SQLUtil) TableParser

func (s *SQLUtil) TableParser() *TableParser

type SQLite3

type SQLite3 struct{}

func (SQLite3) DSN

func (SQLite3) DSN(config DBConfig) string

func (SQLite3) Type

func (s SQLite3) Type(typ, precision, val string) (dbtyp, defval string, err error)

type Table

type Table struct {
	Name string
	Cols []Column
	Type reflect.Type
}

type TableParser

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

func NewTableParser

func NewTableParser(options ...TableParserOptions) *TableParser

func (*TableParser) StructTable

func (p *TableParser) StructTable(v interface{}) (Table, error)

type TableParserOptions

type TableParserOptions struct {
	FieldTag        string
	ColumnNameTag   string
	Default         bool
	Notnull         bool
	TablenamePrefix string
	NameMapper      NameMapper
}

type Tx

type Tx interface {
	Commit() error
	Rollback() error
}

type TxCloser

type TxCloser interface {
	Tx
	io.Closer
}

Jump to

Keyboard shortcuts

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