Documentation ¶
Overview ¶
Package csdb implements MySQL handling and general SQL functions.
Index ¶
- Constants
- Variables
- func Connect() (*sql.DB, *dbr.Connection, error)
- func GetDSN() (string, error)
- func GetDSNTest() (string, error)
- func LoadSlice(dbrSess dbr.SessionRunner, tsr TableStructurer, ti Index, dest interface{}, ...) (int, error)
- func MustConnectTest() *sql.DB
- type DbrSelectCb
- type Index
- type TableStructure
- func (ts *TableStructure) AllColumnAliasQuote(alias string) []string
- func (ts *TableStructure) ColumnAliasQuote(alias string) []string
- func (ts *TableStructure) In(n string) bool
- func (ts *TableStructure) Select(dbrSess dbr.SessionRunner) (*dbr.SelectBuilder, error)
- func (ts *TableStructure) TableAliasQuote(alias string) string
- type TableStructureSlice
- type TableStructurer
Constants ¶
const ( // EnvDSN is the name of the environment variable EnvDSN string = "CS_DSN" // EnvDSNTest test env DSN EnvDSNTest string = "CS_DSN_TEST" )
const ( // MainTable used in SQL to refer to the main table as an alias MainTable = "main_table" // AddTable additional table AdditionalTable = "additional_table" // ScopeTable used in SQl to refer to a website scope table as an alias ScopeTable = "scope_table" )
Variables ¶
var ( ErrDSNNotFound = errors.New("Env var: " + EnvDSN + " not found") ErrDSNTestNotFound = errors.New("Env var: " + EnvDSNTest + " not found") )
var (
ErrTableNotFound = errors.New("Table not found")
)
Functions ¶
func LoadSlice ¶
func LoadSlice(dbrSess dbr.SessionRunner, tsr TableStructurer, ti Index, dest interface{}, cbs ...DbrSelectCb) (int, error)
LoadSlice loads the slice dest with the table structure from tsr TableStructurer and table index ti. Returns the number of loaded rows and nil or 0 and an error. Slice must be a pointer to structs.
func MustConnectTest ¶
mustConnectTest is a helper function that creates a new database connection using environment variables.
Types ¶
type DbrSelectCb ¶
type DbrSelectCb func(*dbr.SelectBuilder) *dbr.SelectBuilder
type TableStructure ¶
type TableStructure struct { // Name is the table name Name string // IDFieldNames contains only primary keys IDFieldNames []string // Columns all other columns which are not primary keys Columns []string }
temporary place
func NewTableStructure ¶
func NewTableStructure(n string, IDs, c []string) *TableStructure
func (*TableStructure) AllColumnAliasQuote ¶
func (ts *TableStructure) AllColumnAliasQuote(alias string) []string
AllColumnAliasQuote prefixes all columns with an alias and puts quotes around them. Returns a copy.
func (*TableStructure) ColumnAliasQuote ¶
func (ts *TableStructure) ColumnAliasQuote(alias string) []string
ColumnAliasQuote prefixes non-id columns with an alias and puts quotes around them. Returns a copy.
func (*TableStructure) In ¶
func (ts *TableStructure) In(n string) bool
In checks if column name n is a column of this table
func (*TableStructure) Select ¶
func (ts *TableStructure) Select(dbrSess dbr.SessionRunner) (*dbr.SelectBuilder, error)
Select generates a SELECT * FROM tableName statement
func (*TableStructure) TableAliasQuote ¶
func (ts *TableStructure) TableAliasQuote(alias string) string
remove this once the ALIAS via []string is implemented in DBR
type TableStructureSlice ¶
type TableStructureSlice []*TableStructure
TableStructureSlice implements interface TableStructurer
func (TableStructureSlice) Len ¶
func (m TableStructureSlice) Len() Index
Len returns the length of the slice data
func (TableStructureSlice) Name ¶
func (m TableStructureSlice) Name(i Index) string
Name is a short hand to return a table name by given index i. Does not return an error when the table can't be found.
func (TableStructureSlice) Next ¶
func (m TableStructureSlice) Next(i Index) bool
Next iterator function where i is the current index starting with zero. Example:
for i := Index(0); tableMap.Next(i); i++ { table, err := tableMap.Structure(i) ... }
func (TableStructureSlice) Structure ¶
func (m TableStructureSlice) Structure(i Index) (*TableStructure, error)
Structure returns the TableStructure from a read-only map m by a giving index i.
type TableStructurer ¶
type TableStructurer interface { // Structure returns the TableStructure from a read-only map m by a giving index i. Structure(Index) (*TableStructure, error) // Name is a short hand to return a table name by given index i. Does not return an error // when the table can't be found. Name(Index) string // Next iterator function where i is the current index starting with zero. // Example: // for i := Index(0); tableMap.Next(i); i++ { // table, err := tableMap.Structure(i) // ... // } Next(Index) bool // Len returns the length of the underlying slice Len() Index }