dbEngine

package
v1.1.163 Latest Latest
Warning

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

Go to latest
Published: May 1, 2024 License: GPL-3.0 Imports: 18 Imported by: 10

Documentation

Index

Constants

View Source
const (
	DB_SETTING              = TypeCfgDB("set of CfgDB")
	RECREATE_MATERIAZE_VIEW = TypeCfgDB("drop materiaze view before create")
)

DB_SETTING is name of value for setting to context

Variables

View Source
var ErrDBNotFound = errors.New("DB not found")

ErrDBNotFound error about wrong DB

Functions

func IsErrorAlreadyExists added in v1.0.1095

func IsErrorAlreadyExists(err error) bool

IsErrorAlreadyExists indicates about errors duplicated

func IsErrorCntChgView added in v1.0.1095

func IsErrorCntChgView(err error) bool

IsErrorCntChgView indicates about errors 'cannot change name of view column'

func IsErrorDoesNotExists added in v1.0.1151

func IsErrorDoesNotExists(err error) bool

IsErrorDoesNotExists indicates about errors not exists

func IsErrorDuplicated added in v1.0.11563

func IsErrorDuplicated(err error) (map[string]string, bool)

IsErrorDuplicated indicate about abort updating because there is a duplicated key found

func IsErrorForReplace added in v1.0.1095

func IsErrorForReplace(err error) bool

IsErrorForReplace indicates about errors 'cannot change or replace"

func IsErrorNullValues added in v1.1.20

func IsErrorNullValues(err error) bool

IsErrorNullValues indicates about column can't add because has NOT NULL

Types

type BuildSqlOptions

type BuildSqlOptions func(b *SQLBuilder) error

BuildSqlOptions set addition property on SQLbuilder

func Args added in v1.1.26

func Args(args ...any) BuildSqlOptions

Args set slice of arguments sql request

func ArgsForSelect

func ArgsForSelect(args ...any) BuildSqlOptions

ArgsForSelect set slice of arguments sql request

func Columns added in v1.0.1151

func Columns(columns ...string) BuildSqlOptions

Columns set inserted columns for SQLBuilder

func ColumnsForSelect

func ColumnsForSelect(columns ...string) BuildSqlOptions

ColumnsForSelect set columns for SQLBuilder

func FetchOnlyRows added in v1.0.1107

func FetchOnlyRows(i int) BuildSqlOptions

FetchOnlyRows parameter for sql query

func InsertOnConflict

func InsertOnConflict(onConflict string) BuildSqlOptions

InsertOnConflict parameter for sql query

func InsertOnConflictDoNothing

func InsertOnConflictDoNothing() BuildSqlOptions

InsertOnConflictDoNothing parameter for sql query

func Offset added in v1.0.1107

func Offset(i int) BuildSqlOptions

Offset parameter for sql query

func OrderBy added in v1.0.1067

func OrderBy(columns ...string) BuildSqlOptions

OrderBy parameter for sql query

func Values added in v1.0.1151

func Values(values ...any) BuildSqlOptions

Values set values sql insert

func WhereForSelect

func WhereForSelect(columns ...string) BuildSqlOptions

WhereForSelect set columns for WHERE clause may consisted first symbol as conditions rule:

'>', '<', '$', '~', '^', '@', '&', '+', '-', '*'

that will replace equals condition, instead:

field_name = $1
write:
field_name > $1, field_name < $1, etc

type CfgCreatorDB added in v1.1.29

type CfgCreatorDB struct {
	RecreateMaterView *struct{}
}

type CfgDB added in v1.1.0

type CfgDB struct {
	Url        string
	GetSchema  *struct{}
	CfgCreator *CfgCreatorDB
	// obsolete - change on CfgCreatorDB properties
	Excluded []string
	Included []string
	PathCfg  *string
	TestInit *string
}

CfgDB consist of setting for creating new DB

type Column

type Column interface {
	BasicType() types.BasicKind                //+
	BasicTypeInfo() types.BasicInfo            //+
	CheckAttr(fieldDefine string) []FlagColumn //+
	CharacterMaximumLength() int               //+
	Comment() string                           //+
	Name() string                              //+
	AutoIncrement() bool                       //+
	IsNullable() bool                          //+
	Default() any                              //+
	SetDefault(any)                            //+
	Foreign() *ForeignKey
	UserDefinedType() *Types
	Table() Table
	Primary() bool    //+
	Type() string     //+
	Required() bool   //+
	SetNullable(bool) //+
}

Column describes methods for table/view/function builderOpts

func CheckColumn added in v1.0.1113

func CheckColumn(ddl string, table Table) (col Column, trueColumn bool)

CheckColumn check ddl for consists any columns of table

func SimpleColumns

func SimpleColumns(names ...string) []Column

type Connection

type Connection interface {
	InitConn(ctx context.Context, dbURL string) error
	GetRoutines(ctx context.Context, dbTypes map[string]Types, tables map[string]Table, cfg *CfgDB) (map[string]Routine, error)
	GetSchema(ctx context.Context, cfg *CfgDB) (database map[string]*string, tables map[string]Table, routines map[string]Routine, dbTypes map[string]Types, err error)
	GetStat() string
	ExecDDL(ctx context.Context, sql string, args ...any) error
	NewTable(name, typ string) Table
	LastRowAffected() int64
	SelectOneAndScan(ctx context.Context, rowValues any, sql string, args ...any) error
	SelectAndScanEach(ctx context.Context, each func() error, rowValue RowScanner, sql string, args ...any) error
	SelectAndRunEach(ctx context.Context, each FncEachRow, sql string, args ...any) error
	SelectAndPerformRaw(ctx context.Context, each FncRawRow, sql string, args ...any) error
	SelectToMap(ctx context.Context, sql string, args ...any) (map[string]any, error)
	SelectToMaps(ctx context.Context, sql string, args ...any) ([]map[string]any, error)
	SelectToMultiDimension(ctx context.Context, sql string, args ...any) ([][]any, []Column, error)
}

Connection implement conn operation

type DB

type DB struct {
	sync.RWMutex
	Cfg  map[string]any
	Conn Connection

	Name          string
	Schema        string
	Tables        map[string]Table
	Types         map[string]Types
	Routines      map[string]Routine
	FuncsReplaced []string
	FuncsAdded    []string

	DbSet map[string]*string
	// contains filtered or unexported fields
}

DB name & schema

func NewDB

func NewDB(ctx context.Context, conn Connection) (*DB, error)

NewDB create new DB instance & performs something migrations

func (*DB) ReadTableSQL

func (db *DB) ReadTableSQL(path string, info os.DirEntry, err error) error

ReadTableSQL performs ddl script for tables

func (*DB) ReadViewSQL added in v1.0.1090

func (db *DB) ReadViewSQL(path string, info os.DirEntry, err error) error

ReadViewSQL performs ddl script for view

type ErrNotFoundColumn

type ErrNotFoundColumn struct {
	Table  string
	Column string
}

ErrNotFoundColumn if not found in table {Table} field by name {Column}

func NewErrNotFoundColumn

func NewErrNotFoundColumn(table string, column string) *ErrNotFoundColumn

NewErrNotFoundColumn create new error

func (ErrNotFoundColumn) Error

func (err ErrNotFoundColumn) Error() string

Error implement error interface

type ErrNotFoundRoutine added in v1.0.1038

type ErrNotFoundRoutine struct {
	Name, SName string
}

ErrNotFoundRoutine if not found table by name {Table}

func (ErrNotFoundRoutine) Error added in v1.0.1038

func (err ErrNotFoundRoutine) Error() string

Error implement error interface

type ErrNotFoundTable

type ErrNotFoundTable struct {
	Table string
}

ErrNotFoundTable if not found table by name {Table}

func NewErrNotFoundTable added in v1.0.1059

func NewErrNotFoundTable(table string) *ErrNotFoundTable

NewErrNotFoundTable create new error

func (ErrNotFoundTable) Error

func (err ErrNotFoundTable) Error() string

Error implement error interface

type ErrNotFoundType added in v1.1.45

type ErrNotFoundType struct {
	Name string
	Type string
}

ErrNotFoundType if not found in table {Table} field by name {Column}

func NewErrNotFoundType added in v1.1.45

func NewErrNotFoundType(name string, sType string) *ErrNotFoundType

NewErrNotFoundType create new error

func (ErrNotFoundType) Error added in v1.1.45

func (err ErrNotFoundType) Error() string

Error implement error interface

type ErrUnknownSql added in v1.0.1051

type ErrUnknownSql struct {
	Line int
	Msg  string
	// contains filtered or unexported fields
}

ErrUnknownSql if not found in field {Name} field by name {Column}

func NewErrUnknownSql added in v1.0.1051

func NewErrUnknownSql(sql string, line int) *ErrUnknownSql

NewErrUnknownSql create new error

func (*ErrUnknownSql) Error added in v1.0.1051

func (err *ErrUnknownSql) Error() string

Error implement error interface

type ErrWrongArgsLen

type ErrWrongArgsLen struct {
	Table  string
	Filter []string
	Args   []interface{}
}

ErrWrongArgsLen if not found in table {Table} field by name {Column}

func NewErrWrongArgsLen

func NewErrWrongArgsLen(table string, column []string, args []interface{}) *ErrWrongArgsLen

NewErrWrongArgsLen create new error

func (ErrWrongArgsLen) Error

func (err ErrWrongArgsLen) Error() string

Error implement error interface

type ErrWrongType added in v1.0.1025

type ErrWrongType struct {
	Name     string
	TypeName string
	Attr     string
}

ErrWrongType if not found in field {Name} field by name {Column}

func NewErrWrongType added in v1.0.1025

func NewErrWrongType(typeName, name, attr string) *ErrWrongType

NewErrWrongType create new error

func (ErrWrongType) Error added in v1.0.1025

func (err ErrWrongType) Error() string

Error implement error interface

type FlagColumn added in v1.1.22

type FlagColumn uint8
const (
	MustNotNull FlagColumn = iota
	Nullable
	ChgType
	ChgLength
	ChgToArray
)

type FncEachRow

type FncEachRow func(values []any, columns []Column) error

FncEachRow & FncRawRow are types of function which use as callback for select methods

type FncRawRow added in v1.0.1146

type FncRawRow func(values [][]byte, columns []Column) error

FncEachRow & FncRawRow are types of function which use as callback for select methods

type ForeignKey added in v1.0.1141

type ForeignKey struct {
	Parent     string `json:"parent"`
	Column     string `json:"column"`
	UpdateRule string `json:"update_rule"`
	DeleteRule string `json:"delete_rule"`
	ForeignCol Column `json:"-"`
}

ForeignKey consists of parameters of foreign key

type Index

type Index struct {
	Name string

	Expr    string
	Where   string
	Unique  bool
	Columns []string
	// contains filtered or unexported fields
}

Index consists of index properties

func (*Index) AddColumn added in v1.1.144

func (ind *Index) AddColumn(name string) bool

func (*Index) GetFields added in v1.0.1095

func (ind *Index) GetFields(columns []Column) []any

GetFields implements interface RowScanner

type Indexes added in v1.0.1095

type Indexes []*Index

Indexes cluster index of table

func (*Indexes) GetFields added in v1.0.1095

func (i *Indexes) GetFields(columns []Column) []any

GetFields implements interface RowScanner

func (Indexes) LastIndex added in v1.0.1113

func (i Indexes) LastIndex() *Index

LastIndex returns last index of list

type NumberColumn

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

NumberColumn implement store data of column for test tables

func NewNumberColumn

func NewNumberColumn(name, comment string, req bool) *NumberColumn

NewNumberColumn create new NumberColumn

func (*NumberColumn) AutoIncrement

func (c *NumberColumn) AutoIncrement() bool

AutoIncrement return true if column is autoincrement

func (*NumberColumn) BasicType

func (c *NumberColumn) BasicType() types.BasicKind

BasicType return GoLangs type of column

func (*NumberColumn) BasicTypeInfo

func (c *NumberColumn) BasicTypeInfo() types.BasicInfo

BasicTypeInfo return types.BasicInfo of column

func (*NumberColumn) CharacterMaximumLength

func (c *NumberColumn) CharacterMaximumLength() int

CharacterMaximumLength return max of length text columns

func (*NumberColumn) CheckAttr

func (c *NumberColumn) CheckAttr(fieldDefine string) []FlagColumn

CheckAttr check attributes of column on DB schema according to ddl-file

func (*NumberColumn) Comment

func (c *NumberColumn) Comment() string

Comment of column

func (*NumberColumn) Default

func (c *NumberColumn) Default() interface{}

Default return default value of column

func (*NumberColumn) Foreign added in v1.1.13

func (c *NumberColumn) Foreign() *ForeignKey

func (*NumberColumn) IsNullable

func (c *NumberColumn) IsNullable() bool

IsNullable return isNullable flag

func (*NumberColumn) Name

func (c *NumberColumn) Name() string

Name of column

func (*NumberColumn) Primary

func (c *NumberColumn) Primary() bool

Primary return true if column is primary key

func (*NumberColumn) Required

func (c *NumberColumn) Required() bool

Required return true if column need a value

func (*NumberColumn) SetDefault added in v1.0.1060

func (c *NumberColumn) SetDefault(str interface{})

SetDefault set default value into column

func (*NumberColumn) SetNullable

func (c *NumberColumn) SetNullable(f bool)

SetNullable set nullable flag of column

func (*NumberColumn) Table added in v1.1.55

func (c *NumberColumn) Table() Table

func (*NumberColumn) Type

func (c *NumberColumn) Type() string

Type of column

func (*NumberColumn) UserDefinedType added in v1.1.55

func (c *NumberColumn) UserDefinedType() *Types

type ParserTableDDL

type ParserTableDDL struct {
	Table
	DB *DB
	// contains filtered or unexported fields
}

ParserTableDDL is interface for parsing DDL file

func NewParserTableDDL added in v1.0.1025

func NewParserTableDDL(table Table, db *DB) *ParserTableDDL

NewParserTableDDL create new instance of ParserTableDDL

func (*ParserTableDDL) Parse

func (p *ParserTableDDL) Parse(ddl string) error

Parse perform queries from ddl text

type Routine

type Routine interface {
	Name() string
	BuildSql(Options ...BuildSqlOptions) (sql string, args []any, err error)
	Select(ctx context.Context, args ...any) error
	Call(ctx context.Context, args ...any) error
	Overlay() Routine
	Params() []Column
	ReturnType() string
	SelectAndScanEach(ctx context.Context, each func() error, rowValue RowScanner, Options ...BuildSqlOptions) error
	SelectOneAndScan(ctx context.Context, row any, Options ...BuildSqlOptions) error
	SelectAndRunEach(ctx context.Context, each FncEachRow, Options ...BuildSqlOptions) error
}

Routine describes methods for function/procedures operations

type RowScanner

type RowScanner interface {
	GetFields(columns []Column) []any
}

RowScanner must return slice variables for pgx.Rows.Scan

type SQLBuilder

type SQLBuilder struct {
	Args []any

	Table Table

	OrderBy       []string
	Offset, Limit int
	// contains filtered or unexported fields
}

SQLBuilder implement sql native constructor

func NewSQLBuilder added in v1.0.1109

func NewSQLBuilder(t Table, Options ...BuildSqlOptions) (*SQLBuilder, error)

NewSQLBuilder create SQLBuilder for table

func (SQLBuilder) DeleteSql added in v1.0.1134

func (b SQLBuilder) DeleteSql() (string, error)

DeleteSql construct delete sql

func (SQLBuilder) InsertSql

func (b SQLBuilder) InsertSql() (string, error)

InsertSql construct insert sql

func (*SQLBuilder) OnConflict

func (b *SQLBuilder) OnConflict() string

OnConflict return sql-text for handling error on insert

func (*SQLBuilder) Select

func (b *SQLBuilder) Select() string

Select return select clause of sql query

func (*SQLBuilder) SelectColumns

func (b *SQLBuilder) SelectColumns() []Column

SelectColumns construct select clause for sql

func (*SQLBuilder) SelectSql

func (b *SQLBuilder) SelectSql() (string, error)

SelectSql construct select sql

func (*SQLBuilder) Set

func (b *SQLBuilder) Set() (string, error)

Set return SET clause of sql update query

func (*SQLBuilder) SetUpsert added in v1.0.1058

func (b *SQLBuilder) SetUpsert() (string, error)

SetUpsert return SET clause of sql query for handling error on insert

func (SQLBuilder) UpdateSql

func (b SQLBuilder) UpdateSql() (string, error)

UpdateSql construct update sql

func (SQLBuilder) UpsertSql

func (b SQLBuilder) UpsertSql() (string, error)

UpsertSql perform sql-script for insert with update according onConflict

func (*SQLBuilder) Where

func (b *SQLBuilder) Where() string

Where return where-clause of sql query

type StringColumn

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

func NewStringColumn

func NewStringColumn(name, comment string, req bool, maxLen ...int) *StringColumn

func (*StringColumn) AutoIncrement

func (s *StringColumn) AutoIncrement() bool

func (*StringColumn) BasicType

func (s *StringColumn) BasicType() types.BasicKind

func (*StringColumn) BasicTypeInfo

func (s *StringColumn) BasicTypeInfo() types.BasicInfo

func (*StringColumn) CharacterMaximumLength

func (s *StringColumn) CharacterMaximumLength() int

func (*StringColumn) CheckAttr

func (s *StringColumn) CheckAttr(fieldDefine string) []FlagColumn

func (*StringColumn) Comment

func (s *StringColumn) Comment() string

func (*StringColumn) Default

func (s *StringColumn) Default() interface{}

func (*StringColumn) Foreign added in v1.0.1141

func (s *StringColumn) Foreign() *ForeignKey

Foreign

func (*StringColumn) IsNullable

func (s *StringColumn) IsNullable() bool

func (*StringColumn) Name

func (s *StringColumn) Name() string

func (*StringColumn) Primary

func (s *StringColumn) Primary() bool

func (*StringColumn) Required

func (s *StringColumn) Required() bool

func (*StringColumn) SetDefault added in v1.0.1060

func (s *StringColumn) SetDefault(str interface{})

func (*StringColumn) SetNullable

func (s *StringColumn) SetNullable(f bool)

func (*StringColumn) Table added in v1.1.41

func (s *StringColumn) Table() Table

func (*StringColumn) Type

func (s *StringColumn) Type() string

func (*StringColumn) UserDefinedType added in v1.1.41

func (s *StringColumn) UserDefinedType() *Types

type Table

type Table interface {
	Columns() []Column
	Comment() string
	FindColumn(name string) Column
	FindIndex(name string) *Index
	Delete(ctx context.Context, Options ...BuildSqlOptions) (int64, error)
	Indexes() Indexes
	GetColumns(ctx context.Context, dbTypes map[string]Types) error
	Insert(ctx context.Context, Options ...BuildSqlOptions) (int64, error)
	Update(ctx context.Context, Options ...BuildSqlOptions) (int64, error)
	Upsert(ctx context.Context, Options ...BuildSqlOptions) (int64, error)
	Name() string
	ReReadColumn(ctx context.Context, name string) Column
	Select(ctx context.Context, Options ...BuildSqlOptions) error
	SelectAndScanEach(ctx context.Context, each func() error, rowValue RowScanner, Options ...BuildSqlOptions) error
	SelectOneAndScan(ctx context.Context, row any, Options ...BuildSqlOptions) error
	SelectAndRunEach(ctx context.Context, each FncEachRow, Options ...BuildSqlOptions) error
}

Table describes methods for table operations

type TableString added in v1.0.1093

type TableString struct {
	Rows [][]string
	// contains filtered or unexported fields
}

TableString implements Table interface for test

func NewTableString added in v1.1.37

func NewTableString(name string, comment string, columns []Column, indexes Indexes, rows [][]string) *TableString

func (TableString) Columns added in v1.0.1093

func (t TableString) Columns() []Column

Columns of Table

func (TableString) Comment added in v1.0.1093

func (t TableString) Comment() string

Comment of Table

func (TableString) Delete added in v1.0.1134

func (t TableString) Delete(ctx context.Context, Options ...BuildSqlOptions) (int64, error)

Delete row of table according to Options

func (TableString) FindColumn added in v1.0.1093

func (t TableString) FindColumn(name string) Column

FindColumn return column 'name' on Table or nil

func (TableString) FindIndex added in v1.0.1093

func (t TableString) FindIndex(name string) *Index

FindIndex get index according to name

func (TableString) GetColumns added in v1.0.1093

func (t TableString) GetColumns(ctx context.Context, dbTypes map[string]Types) error

GetColumns получение значений полей для форматирования данных

func (TableString) Indexes added in v1.0.1113

func (t TableString) Indexes() Indexes

Indexes collect index of table

func (TableString) Insert added in v1.0.1093

func (t TableString) Insert(ctx context.Context, Options ...BuildSqlOptions) (int64, error)

Insert new row & return new ID or rowsAffected if there not autoinc field

func (TableString) Name added in v1.0.1093

func (t TableString) Name() string

Name of Table

func (TableString) ReReadColumn added in v1.0.1093

func (t TableString) ReReadColumn(ctx context.Context, name string) Column

ReReadColumn renew properties of column 'name'

func (TableString) Select added in v1.0.1093

func (t TableString) Select(ctx context.Context, Options ...BuildSqlOptions) error

Select run sql with Options (deprecated)

func (TableString) SelectAndRunEach added in v1.0.1093

func (t TableString) SelectAndRunEach(ctx context.Context, each FncEachRow, Options ...BuildSqlOptions) error

SelectAndRunEach run sql of table with Options & performs each every row of query results

func (TableString) SelectAndScanEach added in v1.0.1093

func (t TableString) SelectAndScanEach(ctx context.Context, each func() error, rowValue RowScanner, Options ...BuildSqlOptions) error

SelectAndScanEach run sql of table with Options & return every row into rowValues & run each

func (TableString) SelectOneAndScan added in v1.0.1101

func (t TableString) SelectOneAndScan(ctx context.Context, row interface{}, Options ...BuildSqlOptions) error

SelectOneAndScan run sqlof table with Options & return rows into rowValues

func (TableString) Update added in v1.0.1093

func (t TableString) Update(ctx context.Context, Options ...BuildSqlOptions) (int64, error)

Update table according to Options

func (TableString) Upsert added in v1.0.1093

func (t TableString) Upsert(ctx context.Context, Options ...BuildSqlOptions) (int64, error)

Upsert preforms INSERT sql or UPDATE if record with primary keys exists

type TypeCfgDB added in v1.1.0

type TypeCfgDB string

TypeCfgDB is type for context values

type Types added in v1.0.1040

type Types struct {
	Id         uint32
	Name       string
	Type       rune
	Attr       TypesAttrs
	Enumerates []string
}

Types consists of parameters of DB types

func NewTypes added in v1.1.45

func NewTypes() *Types

func (*Types) GetFields added in v1.1.41

func (t *Types) GetFields(columns []Column) []any

type TypesAttr added in v1.1.45

type TypesAttr struct {
	Name      string
	Type      string
	IsNotNull bool
}

TypesAttr consists of parameters of DB types

func (*TypesAttr) DecodeText added in v1.1.45

func (dst *TypesAttr) DecodeText(ci *pgtype.ConnInfo, src []byte) error

type TypesAttrs added in v1.1.45

type TypesAttrs []TypesAttr

func (*TypesAttrs) DecodeText added in v1.1.45

func (dst *TypesAttrs) DecodeText(ci *pgtype.ConnInfo, src []byte) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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