Documentation
¶
Overview ¶
Package godbi provides a generic database interface
Index ¶
- Constants
- func DevelopLogger(w io.Writer, level ...slog.Level) *slog.Logger
- func ProductLogger(w io.Writer, level ...slog.Level) *slog.Logger
- type Action
- type Atom
- func (a *Atom) GetAction(actionName string) Capability
- func (a *Atom) MergeCustomActions(custom ...Capability)
- func (a *Atom) RunAtom(db *sql.DB, action string, args any, extra ...map[string]any) ([]any, error)
- func (a *Atom) RunAtomContext(ctx context.Context, db *sql.DB, action string, args any, ...) ([]any, error)
- func (a *Atom) UnmarshalHCL(bs []byte, labels ...string) error
- func (a *Atom) UnmarshalJSON(bs []byte) error
- type Capability
- type Col
- type ConnectType
- type Connection
- type DBI
- func (d *DBI) DoSQL(query string, args ...any) (sql.Result, error)
- func (d *DBI) DoSQLContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (d *DBI) DoSQLs(query string, args ...[]any) (sql.Result, error)
- func (d *DBI) DoSQLsContext(ctx context.Context, query string, args ...[]any) (sql.Result, error)
- func (d *DBI) GetSQL(res map[string]any, query string, labels []any, args ...any) error
- func (d *DBI) GetSQLContext(ctx context.Context, res map[string]any, query string, labels []any, ...) error
- func (d *DBI) InsertID(query string, args ...any) (int64, error)
- func (d *DBI) InsertIDContext(ctx context.Context, query string, args ...any) (int64, error)
- func (d *DBI) InsertSerial(query string, args ...any) (int64, error)
- func (d *DBI) InsertSerialContext(ctx context.Context, query string, args ...any) (int64, error)
- func (d *DBI) Select(lists *[]any, query string, args ...any) error
- func (d *DBI) SelectContext(ctx context.Context, lists *[]any, query string, args ...any) error
- func (d *DBI) SelectSQL(lists *[]any, query string, labels []any, args ...any) error
- func (d *DBI) SelectSQLContext(ctx context.Context, lists *[]any, query string, labels []any, args ...any) error
- func (d *DBI) TxSQL(query string, args ...any) (sql.Result, error)
- func (d *DBI) TxSQLContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- type DBType
- type Delecs
- type Delete
- type Edit
- type Fk
- type Insert
- type Insupd
- type Joint
- type Molecule
- type PreStopper
- type RunOption
- type Slogger
- type Stmt
- type StmtContext
- type Stopper
- type Table
- type Topics
- type Update
Constants ¶
const (
STMT = "stmt"
)
Variables ¶
This section is empty.
Functions ¶
func DevelopLogger ¶ added in v1.3.1
Types ¶
type Action ¶
type Action struct {
ActionName string `json:"actionName,omitempty" hcl:"actionName,label"`
Picked []string `json:"picked,omitempty" hcl:"picked,optional"`
Prepares []*Connection `json:"prepares,omitempty" hcl:"prepares,block"`
Nextpages []*Connection `json:"nextpages,omitempty" hcl:"nextpages,block"`
IsDo bool `json:"-" hcl:"-"`
}
Action is the base struct for REST actions. Prepares and Nextpages are edges to other tables before and after the action.
func (*Action) GetBaseAction ¶ added in v1.4.0
GetBaseAction gets the base action
type Atom ¶
type Atom struct {
AtomName string `json:"atomName,omitempty" hcl:"atomName,label"`
Table
Actions []Capability `json:"actions,omitempty" hcl:"actions,block"`
// contains filtered or unexported fields
}
Atom is a table with multiple actions
func (*Atom) GetAction ¶
func (a *Atom) GetAction(actionName string) Capability
GetAction gets a specific action by name
func (*Atom) MergeCustomActions ¶ added in v1.3.0
func (a *Atom) MergeCustomActions(custom ...Capability)
MergeCustomActions merges custom actions
func (*Atom) RunAtomContext ¶
func (a *Atom) RunAtomContext(ctx context.Context, db *sql.DB, action string, args any, extra ...map[string]any) ([]any, error)
RunAtomContext runs an action with context by name
func (*Atom) UnmarshalHCL ¶ added in v1.3.0
UnmarshalHCL is a HCL unmarshaler
func (*Atom) UnmarshalJSON ¶ added in v1.0.4
UnmarshalJSON is a JSON unmarshaler
type Capability ¶
type Capability interface {
GetBaseAction() *Action
// RunActionContext runs the action with context, db, table, and args
RunActionContext(context.Context, *sql.DB, *Table, map[string]any, ...map[string]any) ([]any, error)
}
Capability is to implement Capability interface
type Col ¶
type Col struct {
ColumnName string `json:"columnName" hcl:"columnName,label"`
TypeName string `json:"typeName" hcl:"typeName,optional"`
Label string `json:"label" hcl:"columnLabel,optional"`
Notnull bool `json:"notnull,omitempty" hcl:"notnull,optional"`
Constraint bool `json:"constraint,omitempty" hcl:"constraint,optional"`
Auto bool `json:"auto,omitempty" hcl:"auto,optional"`
// true for a one-to-may recurse column
Recurse bool `json:"recurse,omitempty" hcl:"recurse,optional"`
}
Col defines table column in GO struct
type ConnectType ¶
type ConnectType int
const ( CONNECTDefault ConnectType = iota CONNECTOne CONNECTArray CONNECTMap CONNECTMany )
type Connection ¶
type Connection struct {
// AtomName: the name of the table
AtomName string `json:"atomName" hcl:"atomName,label"`
// ActionName: the action on the atom
ActionName string `json:"actionName" hcl:"actionName,label"`
// RelateArgs: map current page's columns to nextpage's columns as input
RelateArgs map[string]string `json:"relateArgs,omitempty" hcl:"relateArgs,optional"`
// RelateExtra: map current page's columns to nextpage's columns (for Nextpages), or prepared page's columns to current page's columns (for Prepares) as constrains.
RelateExtra map[string]string `json:"relateExtra,omitempty" hcl:"relateExtra,optional"`
// Dimension: for nextpage's output format
Dimension ConnectType `json:"dimension,omitempty" hcl:"dimension,optional"`
// Marker: for input data, this marks a whole data set for the next or previous object; for output data, this is the key for the next whole data set.
Marker string `json:"marker,omitempty" hcl:"marker,optional"`
}
Connection describes linked page 1) for Nextpages, it maps item in lists to next ARGS and next Extra 2) for Prepares, it maps current ARGS to the next ARGS and next Extra
func (*Connection) Subname ¶
func (c *Connection) Subname() string
Subname is the marker string used to store the output
type DBI ¶
type DBI struct {
// Embedding the generic database handle.
*sql.DB
// contains filtered or unexported fields
}
DBI embeds GO's generic SQL handler and adds few functions for database executions and queries.
func (*DBI) DoSQLContext ¶
DoSQLContext executes a SQL the same as DB's Exec, only save the last inserted ID
func (*DBI) DoSQLs ¶
DoSQLs executes multiple rows using the same prepared statement, The rows are expressed as a slice of slices.
func (*DBI) DoSQLsContext ¶
DoSQLsContext executes multiple rows using the same prepared statement, The rows are expressed as a slice of slices.
func (*DBI) GetSQLContext ¶
func (d *DBI) GetSQLContext(ctx context.Context, res map[string]any, query string, labels []any, args ...any) error
GetSQLContext returns single row 'res'.
func (*DBI) InsertID ¶
InsertID executes a SQL the same as DB's Exec, only save the last inserted ID
func (*DBI) InsertIDContext ¶
InsertIDContext executes a SQL the same as DB's Exec, only save the last inserted ID
func (*DBI) InsertSerial ¶
InsertSerial insert a SQL row into Postgres table with Serail , only save the last inserted ID
func (*DBI) InsertSerialContext ¶
InsertSerialContext insert a SQL into Postgres table with Serial, only save the last inserted ID
func (*DBI) Select ¶
Select returns queried data 'list' as a slice of maps, whose data types are determined dynamically by the generic handle.
func (*DBI) SelectContext ¶
SelectContext returns queried data 'lists' as a slice of maps, whose data types are determined dynamically by the generic handle.
func (*DBI) SelectSQL ¶
SelectSQL returns queried data 'list' as a slice of maps. The map keys and their data types are pre-defined in 'labels', expressed as a slice of interfaces:
- when an interface is a string, this is the key name. The data types are determined dynamically by the generic handler.
- when an interface is a 2-string slice, the first element is the key and the second the data type in "int64", "int", "string" etc.
func (*DBI) SelectSQLContext ¶
func (d *DBI) SelectSQLContext(ctx context.Context, lists *[]any, query string, labels []any, args ...any) error
SelectSQLContext returns queried data 'list' as a slice of maps. The map keys and their data types are pre-defined in 'labels', expressed as a slice of interfaces:
- when an interface is a string, this is the key name. The data types are determined dynamically by the generic handler.
- when an interface is a 2-string slice, the first element is the key and the second the data type in "int64", "int", "string" etc.
type Delecs ¶
type Delecs struct {
Action
}
Delecs is a special Topics action that returns all foreign keys
func (*Delecs) RunActionContext ¶
func (d *Delecs) RunActionContext(ctx context.Context, db *sql.DB, t *Table, args map[string]any, extra ...map[string]any) ([]any, error)
RunActionContext of Delecs is to populate Fks before making delete, so that Fks could be passed to other Prepared delete actions. If there is no Fks, we putput the input, then Delecs does nothing.
type Delete ¶
type Delete struct {
Action
}
Delete struct for row deletion by primary key
type Fk ¶
type Fk struct {
// the parent table name
FkTable string `json:"fkTable" hcl:"fkTable,optional"`
// the parant table column
FkColumn string `json:"fkColumn" hcl:"fkColumn,optional"`
// column of this table
Column string `json:"column" hcl:"column,optional"`
}
Fk defines foreign key struct
type Insert ¶
type Insert struct {
Action
}
Insert struct for table insert
type Insupd ¶
type Insupd struct {
Action
}
Insupd struct for table update, if not existing according to the unique key, do insert
type Joint ¶
type Joint struct {
TableName string `json:"tableName" hcl:"tableName,label"`
Alias string `json:"alias,omitempty" hcl:"alias,optional"`
JoinType string `json:"type,omitempty" hcl:"type,optional"`
JoinUsing string `json:"using,omitempty" hcl:"using,optional"`
JoinOn string `json:"on,omitempty" hcl:"on,optional"`
Sortby string `json:"sortby,omitempty" hcl:"sortby,optional"`
}
Joint struct describes the joined search
type Molecule ¶
type Molecule struct {
Atoms []*Atom `json:"atoms" hcl:"atoms,block"`
DBDriver DBType `json:"dbDriver" hcl:"dbDriver,optional"`
Stopper
PreStopper
// contains filtered or unexported fields
}
Molecule describes all atoms and actions in a database schema
func (*Molecule) RunContext ¶
func (m *Molecule) RunContext(ctx context.Context, db *sql.DB, atom, action string, opt *RunOption) ([]any, error)
RunContext runs action by atom and action string names. It returns the searched data and optional error code. atom is the atom name, and action the action name.
type PreStopper ¶ added in v1.3.0
type RunOption ¶ added in v1.4.0
type RunOption struct {
Args any
Extra map[string]any
GlobalArgs map[string]any
GlobalExtra map[string]any
}
RunOption holds the arguments for RunContext
type Stmt ¶ added in v1.3.0
type Stmt struct {
Action
StmtContext
Others map[string]*StmtContext `json:"others,omitempty" hcl:"others,block"`
}
Stmt struct for search one specific row by primary key
type StmtContext ¶ added in v1.3.0
type Stopper ¶ added in v1.0.3
type Stopper interface {
PreStopper
Sign(tableObj *Table, item map[string]any)
}
type Table ¶
type Table struct {
TableName string `json:"tableName" hcl:"tableName,optional"`
Columns []*Col `json:"columns" hcl:"columns,block"`
Pks []string `json:"pks,omitempty" hcl:"pks,optional"`
IDAuto string `json:"idAuto,omitempty" hcl:"idAuto,optional"`
Fks []*Fk `json:"fks,omitempty" hcl:"fks,block"`
Uniques []string `json:"uniques,omitempty" hcl:"uniques,optional"`
// contains filtered or unexported fields
}
Table defines a table by name, columns, primary key, foreign keys, auto id and unique columns
func (*Table) IsRecursive ¶
IsRecursive indicates if table references to itself in one to multiple relations
func (*Table) RecursiveColumn ¶
RecursiveColumn returns the name of the resursive column
func (*Table) SetDBDriver ¶
SetDBDriver sets the driver type
type Topics ¶
type Topics struct {
Action
FIELDS string `json:"fields,omitempty" hcl:"fields,optional"`
Totalforce int `json:"totalforce,omitempty" hcl:"totalforce,optional"`
MAXPAGENO string `json:"maxpageno,omitempty" hcl:"maxpageno,optional"`
TOTALNO string `json:"totalno,omitempty" hcl:"totalno,optional"`
PAGESIZE string `json:"pagesize,omitempty" hcl:"pagesize,optional"`
PAGENO string `json:"pageno,omitempty" hcl:"pageno,optional"`
SORTBY string `json:"sortby,omitempty" hcl:"sortby,optional"`
SORTREVERSE string `json:"sortreverse,omitempty" hcl:"sortreverse,optional"`
}
Topics struct for search multiple rows by constraints