Documentation ¶
Index ¶
- Constants
- Variables
- func BuildDSN(dbkind dbsys, username, password, host, database string, port uint16, ...) (string, error)
- type CreateOption
- type DBhelper
- func (dbhelper *DBhelper) AddQueryChain(chain QueryChain) *DBhelper
- func (dbhelper *DBhelper) CreateTable(data interface{}, options ...*CreateOption) error
- func (dbhelper *DBhelper) Exec(query string, args ...interface{}) (sql.Result, error)
- func (dbhelper *DBhelper) Execf(queryFormat string, formatParams []string, args ...interface{}) (sql.Result, error)
- func (dbhelper *DBhelper) Insert(data interface{}, options ...*InsertOption) (*sql.Result, error)
- func (dbhelper *DBhelper) LoadQueries(name, file string, chainOrder int) error
- func (dbhelper *DBhelper) Open(params ...string) (*DBhelper, error)
- func (dbhelper *DBhelper) QueryRow(a interface{}, query string, args ...interface{}) error
- func (dbhelper *DBhelper) QueryRowf(a interface{}, queryf string, queryArgs []string, args ...interface{}) error
- func (dbhelper *DBhelper) QueryRows(a interface{}, query string, args ...interface{}) error
- func (dbhelper *DBhelper) QueryRowsf(a interface{}, query string, queryArgs []string, args ...interface{}) error
- func (dbhelper *DBhelper) RunUpdate(options ...bool) error
- func (dbhelper *DBhelper) SetErrHook(hook ErrHookFunc, options ...ErrHookOptions)
- func (dbhelper *DBhelper) WithHook(hook ErrHookFunc, options ...ErrHookOptions) *DBhelper
- func (dbhelper *DBhelper) WithMessage(s string) *DBhelper
- func (dbhelper *DBhelper) WithOption(option ErrHookOptions) *DBhelper
- type DBhelperOptions
- type ErrHookFunc
- type ErrHookOptions
- type InitSQL
- type InsertOption
- type QueryChain
- type SQLQuery
Constants ¶
const ( //Sqlite sqlite db Sqlite dbsys = iota //SqliteEncrypted Sqlite encrypted SqliteEncrypted //Mysql mysql db Mysql //Postgres postgres db Postgres )
const ( //MysqlURIFormat formats mysql uri MysqlURIFormat = "%s:%s@tcp(%s:%d)/%s%s" //PostgresURIFormat formats mysql uri PostgresURIFormat = "user='%s' password='%s' host='%s' port=%d dbname='%s' %s" )
const ( //OrmTag orm-tag OrmTag = "orm" //DBTag db-tag DBTag = "db" //DefaultTag default DefaultTag = "default" )
Tags
const ( TagIgnore = "-" TagPrimaryKey = "pk" TagAutoincrement = "ai" TagInsertAutoincrement = "iai" TagNotNull = "nn" )
Tag values
const (
//TableDBVersion tableName for db version store
TableDBVersion = "DBVersion"
)
Variables ¶
var ( //ErrDBNotSupported error if database is not supported ErrDBNotSupported = errors.New("Database not supported") //ErrPostgresURIMissingArg error if Open() mysqlDB and missing an arg ErrPostgresURIMissingArg = errors.New("Postgres missing argument. Use Open(username, password, address, port, database)") //ErrMysqlURIMissingArg error if Open() mysqlDB and missing an arg ErrMysqlURIMissingArg = errors.New("MYSQL missing argument. Use Open(username, password, address, port, database)") //ErrPortInvalid if given port is invalid ErrPortInvalid = errors.New("Port invalid. Port must be <= 65535 and > 0") //ErrSqliteEncryptMissingArg error if Open() SqliteEncrypt and missing argument ErrSqliteEncryptMissingArg = errors.New("SqliteEncrypt missing argument. Use Open(file, key)") //ErrSqliteMissingArg error if Open() Sqlite and missing argument ErrSqliteMissingArg = errors.New("Sqlite missing argument. Use Open(file)") //ErrVersionStoreTooManyVersions if VersionStore contains more than one version ErrVersionStoreTooManyVersions = errors.New("Too many versions stored in VersionStore") //ErrCantStoreVersionInDB err if running update and StoreVersionInDB=false ErrCantStoreVersionInDB = errors.New("Can't store Version in Database. Set StoreVersionInDB=true") //ErrInvalidDatabase an invalid dbsys was used ErrInvalidDatabase = errors.New("Invalid database") //ErrNoStruct if the given data is no struct ErrNoStruct = errors.New("Data must be a struct") //ErrNoRowsInResultSet error if no rows in resultSet ErrNoRowsInResultSet = "sql: no rows in result set" //ErrCantAddress if input is no pointer ErrCantAddress = errors.New("Can't address value") )
var NoHook = func(err error, s1, s2 string) {}
NoHook run a query without a hook
Functions ¶
Types ¶
type CreateOption ¶ added in v1.1.2
CreateOption options for inserting structs into DB
type DBhelper ¶
type DBhelper struct { //Versions for upgrading //CurrentVersion the version currently running CurrentVersion float32 //AvailableVersion the version which is newly added AvailableVersion float32 //DBhelper data DB *sqlx.DB Options DBhelperOptions IsOpen bool QueryChains []QueryChain `json:"chains"` ErrHookFunc ErrHookFunc ErrHookOptions *ErrHookOptions NextErrHookFunc ErrHookFunc NextErrHookOption *ErrHookOptions // contains filtered or unexported fields }
DBhelper the dbhelper object
func NewDBHelper ¶
NewDBHelper the DBhelper constructor NewDBHelper(database, debug, stopUpdateOnError, storeVersionInDB, useColors)
func (*DBhelper) AddQueryChain ¶
func (dbhelper *DBhelper) AddQueryChain(chain QueryChain) *DBhelper
AddQueryChain adds a queryChain
func (*DBhelper) CreateTable ¶
func (dbhelper *DBhelper) CreateTable(data interface{}, options ...*CreateOption) error
CreateTable creates a table for struct Leave name empty to use the name of the struct
func (*DBhelper) Execf ¶
func (dbhelper *DBhelper) Execf(queryFormat string, formatParams []string, args ...interface{}) (sql.Result, error)
Execf executes a formatted query in DB
func (*DBhelper) Insert ¶
func (dbhelper *DBhelper) Insert(data interface{}, options ...*InsertOption) (*sql.Result, error)
Insert creates a table for struct Leave name empty to use the name of the struct
func (*DBhelper) LoadQueries ¶
LoadQueries loads queries from a .sql file and executes the statements (row for row). The SQLQuery Version of the statements are 0. This is intended to initialize the database-schema
func (*DBhelper) Open ¶
Open db Sqlite - Open(filename) SqliteEncrypted - Open(filename, key) Mysql - Open(username, password, address, port, database) Postgres - Open(username, password, address, port, database)
func (*DBhelper) QueryRowf ¶
func (dbhelper *DBhelper) QueryRowf(a interface{}, queryf string, queryArgs []string, args ...interface{}) error
QueryRowf like QueryRow but formatted
func (*DBhelper) QueryRowsf ¶
func (dbhelper *DBhelper) QueryRowsf(a interface{}, query string, queryArgs []string, args ...interface{}) error
QueryRowsf like QueryRows but formatted
func (*DBhelper) RunUpdate ¶
RunUpdate updates new sql queries RunUpdate(fullUpdate, dropAllTables bool)
func (*DBhelper) SetErrHook ¶
func (dbhelper *DBhelper) SetErrHook(hook ErrHookFunc, options ...ErrHookOptions)
SetErrHook sets the error hook function
func (*DBhelper) WithHook ¶
func (dbhelper *DBhelper) WithHook(hook ErrHookFunc, options ...ErrHookOptions) *DBhelper
WithHook adds next log prefix
func (*DBhelper) WithMessage ¶
WithMessage adds next log prefix
func (*DBhelper) WithOption ¶ added in v1.0.3
func (dbhelper *DBhelper) WithOption(option ErrHookOptions) *DBhelper
WithOption adds next ErrHookOption
type DBhelperOptions ¶
type DBhelperOptions struct { Debug bool StopUpdateOnError bool StoreVersionInDB bool UseColors bool }
DBhelperOptions options for DBhelper
type ErrHookFunc ¶
ErrHookFunc error hook 1. string is the query; 2. string is the hookPrefix if set
type ErrHookOptions ¶
ErrHookOptions options for ErrorHooks
type InsertOption ¶ added in v1.1.2
type InsertOption struct { TableName string IgnoreFields []string SetPK bool FillNotSetFields bool }
InsertOption options for inserting structs into DB
type QueryChain ¶
type QueryChain struct { Name string `json:"name"` Order int `json:"order"` Queries []SQLQuery `json:"queries"` }
QueryChain a list of SQL queries over time
func LoadQueries ¶
func LoadQueries(name, file string, chainOrder int) (*QueryChain, error)
LoadQueries loads queries from a .sql file and executes the statements (row for row). The SQLQuery Version of the statements are 0. This is intended to initialize the database-schema
func NewQueryChain ¶
func NewQueryChain(name string, order int) *QueryChain
NewQueryChain QueryChain constructor
func RestoreQueryChain ¶
func RestoreQueryChain(file string) (*QueryChain, error)
RestoreQueryChain loads an exported queryChain from file
func (*QueryChain) ExportQueryChain ¶
func (queryChain *QueryChain) ExportQueryChain(file string, perm os.FileMode) error
ExportQueryChain saves/exports a queryChain to a file
type SQLQuery ¶
type SQLQuery struct { VersionAdded float32 `json:"vs"` QueryString string `json:"query"` Params []string `json:"params"` FqueryString string `json:"queryf"` Fparams []string `json:"fparams"` }
SQLQuery a query
func CreateInitVersionSQL ¶
CreateInitVersionSQL creates SQLQuery[] for init version