dbrobj

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

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

Go to latest
Published: Apr 22, 2018 License: MIT Imports: 9 Imported by: 0

README

dbrobj

One-call CRUD on Go structs, layers on top of and compatible with github.com/gocraft/dbr

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoField = fmt.Errorf("field not found")
View Source
var ErrNoTable = fmt.Errorf("table not found for object/type")
View Source
var FieldNameMapper = CamelToSnake
View Source
var TableNameMapper = CamelToSnake

Functions

func CamelToSnake

func CamelToSnake(s string) string

func DerefType

func DerefType(t reflect.Type) reflect.Type

func DerefValue

func DerefValue(v reflect.Value) reflect.Value

func FieldIndex

func FieldIndex(obj interface{}, sqlFieldName string) (out int, rete error)

func FieldValue

func FieldValue(obj interface{}, sqlFieldName string) (interface{}, error)

func Split

func Split(src string) (entries []string)

Split splits the camelcase word and returns a list of words. It also supports digits. Both lower camel case and upper camel case are supported. For more info please check: http://en.wikipedia.org/wiki/CamelCase

Examples

"" =>                     [""]
"lowercase" =>            ["lowercase"]
"Class" =>                ["Class"]
"MyClass" =>              ["My", "Class"]
"MyC" =>                  ["My", "C"]
"HTML" =>                 ["HTML"]
"PDFLoader" =>            ["PDF", "Loader"]
"AString" =>              ["A", "String"]
"SimpleXMLParser" =>      ["Simple", "XML", "Parser"]
"vimRPCPlugin" =>         ["vim", "RPC", "Plugin"]
"GL11Version" =>          ["GL", "11", "Version"]
"99Bottles" =>            ["99", "Bottles"]
"May5" =>                 ["May", "5"]
"BFG9000" =>              ["BFG", "9000"]
"BöseÜberraschung" =>     ["Böse", "Überraschung"]
"Two  spaces" =>          ["Two", "  ", "spaces"]
"BadUTF8\xe2\xe2\xa1" =>  ["BadUTF8\xe2\xe2\xa1"]

Splitting rules

  1. If string is not valid UTF-8, return it without splitting as single item array.
  2. Assign all unicode characters into one of 4 sets: lower case letters, upper case letters, numbers, and all other characters.
  3. Iterate through characters of string, introducing splits between adjacent characters that belong to different sets.
  4. Iterate through array of split strings, and if a given string is upper case: if subsequent string is lower case: move last character of upper case string to beginning of lower case string

Types

type Config

type Config struct {
	TableInfoMap
}

func NewConfig

func NewConfig() *Config

NewConfig returns a new Config, properly intialized.

func (*Config) NewConnector

func (c *Config) NewConnector(defaultDbrConn *dbr.Connection, sharder Sharder) *Connector

type Connection

type Connection struct {
	TableInfoMap

	*dbr.Connection
}

func (*Connection) NewSession

func (c *Connection) NewSession(log dbr.EventReceiver) *Session

type Connector

type Connector struct {

	// embed default Connection, for ease of transitioning existing code
	*Connection

	// Sharder provides a means to obtain a sharded connection.
	Sharder
}

func (*Connector) NewShardSession

func (c *Connector) NewShardSession(log dbr.EventReceiver, i interface{}, keys ...interface{}) *Session

func (*Connector) NewShardSessionForType

func (c *Connector) NewShardSessionForType(log dbr.EventReceiver, t reflect.Type, keys ...interface{}) *Session

type Session

type Session struct {
	TableInfoMap

	*dbr.Session
}

func (*Session) Begin

func (s *Session) Begin() (*Tx, error)

func (*Session) BeginTx

func (s *Session) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)

func (*Session) ObjDelete

func (s *Session) ObjDelete(obj interface{}, pk ...interface{}) error

func (*Session) ObjGet

func (s *Session) ObjGet(obj interface{}, pk ...interface{}) error

func (*Session) ObjInsert

func (s *Session) ObjInsert(obj interface{}) error

func (*Session) ObjUpdate

func (s *Session) ObjUpdate(obj interface{}) error

func (*Session) ObjUpdateDiff

func (s *Session) ObjUpdateDiff(newObj interface{}, oldObj interface{}) error

func (*Session) ObjUpsert

func (s *Session) ObjUpsert(obj interface{}, sqlSet string, args ...interface{}) error

idea: what if we construct an INSERT ON DUPLICATE KEY UPDATE (db-specific logic) statement (actually, check out MERGE and see how wide the support is) and the sqlSet is in the form of "a = ?, b = ?" and that gets put into the update part of the statement. If sqlSet is empty then it becomes a full replace. This handles both the "counter" case and the "i just want to make sure this is there" case.

type Sharder

type Sharder interface {
	// can select connection and create session, and also
	// copy TableInfoMap and make mods as needed for table names
	Shard(i interface{}, keys ...interface{}) *Connection
	ShardForType(t reflect.Type, keys ...interface{}) *Connection
}

type TableInfo

type TableInfo struct {
	GoType         reflect.Type // underlying Go type (pointer removed)
	TableName      string       // SQL table names
	KeyNames       []string     // SQL primary key field names
	KeyAutoIncr    bool         // true if keys are auto-incremented by the database
	VersionColName string       // name of version col, empty disables optimistic locking

}

func (*TableInfo) ColNames

func (ti *TableInfo) ColNames() []string

func (*TableInfo) KeyAndColNames

func (ti *TableInfo) KeyAndColNames() []string

func (*TableInfo) SetKeys

func (ti *TableInfo) SetKeys(isAutoIncr bool, keyNames []string) *TableInfo

func (*TableInfo) SetVersionCol

func (ti *TableInfo) SetVersionCol(versionColName string) *TableInfo

type TableInfoMap

type TableInfoMap map[reflect.Type]*TableInfo

func (TableInfoMap) AddTable

func (m TableInfoMap) AddTable(i interface{}) *TableInfo

func (TableInfoMap) AddTableWithName

func (m TableInfoMap) AddTableWithName(i interface{}, tableName string) *TableInfo

func (TableInfoMap) TableFor

func (m TableInfoMap) TableFor(i interface{}) *TableInfo

func (TableInfoMap) TableForType

func (m TableInfoMap) TableForType(t reflect.Type) *TableInfo

type Tx

type Tx struct {
	TableInfoMap

	*dbr.Tx
}

func (*Tx) ObjDelete

func (tx *Tx) ObjDelete(obj interface{}, pk ...interface{}) error

func (*Tx) ObjGet

func (tx *Tx) ObjGet(obj interface{}, pk ...interface{}) error

func (*Tx) ObjInsert

func (tx *Tx) ObjInsert(obj interface{}) error

func (*Tx) ObjUpdate

func (tx *Tx) ObjUpdate(obj interface{}) error

func (*Tx) ObjUpdateDiff

func (tx *Tx) ObjUpdateDiff(newObj interface{}, oldObj interface{}) error

Jump to

Keyboard shortcuts

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