Documentation ¶
Overview ¶
Package csdb implements MySQL helper for tables, columns, statements, replication, validation and DB variables.
Index ¶
- Constants
- Variables
- func GetDSN() (string, error)
- func GetParsedDSN() (*mysql.Config, error)
- func IsValidIdentifier(names ...string) error
- func MustGetDSN() string
- type Column
- func (c *Column) DataTypeSimple() string
- func (c *Column) GoPrimitive() string
- func (c *Column) GoPrimitiveNull() string
- func (c *Column) GoString() string
- func (c *Column) IsAutoIncrement() bool
- func (c *Column) IsCurrentTimestamp() bool
- func (c *Column) IsNull() bool
- func (c *Column) IsPK() bool
- func (c *Column) IsUnique() bool
- func (c *Column) IsUnsigned() bool
- type Columns
- func (cs Columns) ByName(fieldName string) *Column
- func (cs Columns) ColumnsNoPK() Columns
- func (cs Columns) FieldNames() (fieldNames []string)
- func (cs Columns) Filter(f func(*Column) bool) (cols Columns)
- func (cs Columns) First() *Column
- func (cs Columns) GoString() string
- func (cs Columns) Hash() ([]byte, error)
- func (cs Columns) JoinFields(sep ...string) string
- func (cs Columns) Len() int
- func (cs Columns) Less(i, j int) bool
- func (cs Columns) Map(f func(*Column) *Column) Columns
- func (cs Columns) PrimaryKeys() Columns
- func (cs Columns) String() string
- func (cs Columns) Swap(i, j int)
- func (cs Columns) UniqueKeys() Columns
- type MasterStatus
- type Preparer
- type Querier
- type QueryRower
- type ResurrectStmt
- type Table
- func (t *Table) AllColumnAliasQuote(alias string) []string
- func (t *Table) ColumnAliasQuote(alias string) []string
- func (t *Table) In(n string) bool
- func (t *Table) LoadColumns(ctx context.Context, db Querier) (err error)
- func (t *Table) LoadSlice(dbrSess dbr.SessionRunner, dest interface{}, cbs ...dbr.SelectCb) (int, error)
- func (t *Table) Select(dbrSess dbr.SessionRunner) (*dbr.SelectBuilder, error)
- func (t *Table) TableAliasQuote(alias string) string
- type TableOption
- func WithLoadColumnDefinitions(ctx context.Context, db Querier) TableOption
- func WithTable(idx int, tableName string, cols ...*Column) TableOption
- func WithTableLoadColumns(ctx context.Context, db Querier, idx int, tableName string) TableOption
- func WithTableNames(idx []int, tableName []string) TableOption
- type Tables
- func (tm *Tables) Delete(idxs ...int)
- func (tm *Tables) Insert(i int, ts *Table) error
- func (tm *Tables) Len() int
- func (tm *Tables) MustTable(i int) *Table
- func (tm *Tables) Name(i int) string
- func (tm *Tables) Options(opts ...TableOption) error
- func (tm *Tables) Table(i int) (*Table, error)
- func (tm *Tables) Update(i int, ts *Table) error
- type Variable
- type Variables
Constants ¶
const ( MainTable = "main_table" AdditionalTable = "additional_table" ScopeTable = "scope_table" )
@deprecated
const DMLLoadColumns = `` /* 265-byte string literal not displayed */
DMLLoadColumns specifies the data manipulation language for retrieving all columns in the current database for a specific table.
const EnvDSN string = "CS_DSN"
EnvDSN is the name of the environment variable
Variables ¶
var DefaultResurrectStmtIdleTime = time.Second * 10
DefaultResurrectStmtIdleTime is the global idle time when you create a new PersistentStmt. If no query will be executed within this idle time the statement gets closed.
Functions ¶
func GetParsedDSN ¶
GetParsedDSN checks the environment variable EnvDSN and if a DSN can be found it gets parsed into an URL.
func IsValidIdentifier ¶
IsValidIdentifier checks the permissible syntax for identifiers. Certain objects within MySQL, including database, table, index, column, alias, view, stored procedure, partition, tablespace, and other object names are known as identifiers. ASCII: [0-9,a-z,A-Z$_] (basic Latin letters, digits 0-9, dollar, underscore) Max length 63 characters. Returns errors.NotValid
func MustGetDSN ¶
func MustGetDSN() string
MustGetDSN returns the data source name from an environment variable or panics on error.
Types ¶
type Column ¶
type Column struct { Field string `db:"COLUMN_NAME"` //`COLUMN_NAME` varchar(64) NOT NULL DEFAULT ”, Pos int64 `db:"ORDINAL_POSITION"` //`ORDINAL_POSITION` bigint(21) unsigned NOT NULL DEFAULT '0', Default null.String `db:"COLUMN_DEFAULT"` //`COLUMN_DEFAULT` longtext, Null string `db:"IS_NULLABLE"` //`IS_NULLABLE` varchar(3) NOT NULL DEFAULT ”, // DataType contains the basic type of a column like smallint, int, mediumblob, // float, double, etc... but always transformed to lower case. DataType string `db:"DATA_TYPE"` //`DATA_TYPE` varchar(64) NOT NULL DEFAULT ”, CharMaxLength null.Int64 `db:"CHARACTER_MAXIMUM_LENGTH"` //`CHARACTER_MAXIMUM_LENGTH` bigint(21) unsigned DEFAULT NULL, Precision null.Int64 `db:"NUMERIC_PRECISION"` //`NUMERIC_PRECISION` bigint(21) unsigned DEFAULT NULL, Scale null.Int64 `db:"NUMERIC_SCALE"` //`NUMERIC_SCALE` bigint(21) unsigned DEFAULT NULL, // ColumnType full SQL string of the column type ColumnType string `db:"COLUMN_TYPE"` //`COLUMN_TYPE` longtext NOT NULL, // Key primary or unique or ... Key string `db:"COLUMN_KEY"` //`COLUMN_KEY` varchar(3) NOT NULL DEFAULT ”, Extra string `db:"EXTRA"` //`EXTRA` varchar(30) NOT NULL DEFAULT ”, Comment string `db:"COLUMN_COMMENT"` //`COLUMN_COMMENT` varchar(1024) NOT NULL DEFAULT ”, // contains filtered or unexported fields }
Column contains information about one database table column retrieved from information_schema.COLUMNS
func (*Column) DataTypeSimple ¶
DataTypeSimple calculates the simplified data type of the field DataType. The calculated result will be cached. For example bigint, smallint, tinyint will result in "int". The returned string guarantees to be lower case. Available returned types are: bool, bytes, date, float, int, money, string, time. Data type money is special for the database schema. This function is thread safe.
func (*Column) GoPrimitive ¶
GoPrimitive detects the Go type of a SQL table column as a non nullable type.
func (*Column) GoPrimitiveNull ¶
GoPrimitiveNull detects the Go type of a SQL table column as a nullable type.
func (*Column) GoString ¶
GoString returns the Go types representation. See interface fmt.GoStringer
func (*Column) IsAutoIncrement ¶
IsAutoIncrement checks if column has an auto increment property
func (*Column) IsCurrentTimestamp ¶
IsCurrentTimestamp checks if the Default field is a current timestamp
func (*Column) IsUnsigned ¶
IsUnsigned checks if field TypeRaw contains the word unsigned.
type Columns ¶
type Columns []*Column
Columns contains a slice of column types
func LoadColumns ¶
LoadColumns returns all columns from a table in the current database. For now MySQL DSN must have set interpolateParams to true.
func (Columns) ByName ¶
ByName finds a column by its name. Case sensitive. Guaranteed to a non-nil return value.
func (Columns) ColumnsNoPK ¶
ColumnsNoPK returns all non primary key columns
func (Columns) FieldNames ¶
FieldNames returns all column names
func (Columns) First ¶
First returns the first column from the slice. Guaranteed to a non-nil return value.
func (Columns) GoString ¶
GoString returns the Go types representation. See interface fmt.GoStringer
func (Columns) Hash ¶
Hash calculates a non-cryptographic, fast and efficient hash value from all columns. Current hash algorithm is fnv64a.
func (Columns) JoinFields ¶
JoinFields joins the field names into a string, separated by the optional first argument.
func (Columns) Map ¶
Map will run function f on all items in Columns and returns a copy of the slice and the item.
func (Columns) PrimaryKeys ¶
PrimaryKeys returns all primary key columns
func (Columns) UniqueKeys ¶
UniqueKeys returns all unique key columns
type MasterStatus ¶
type MasterStatus struct { File string Position uint Binlog_Do_DB string Binlog_Ignore_DB string // Executed_Gtid_Set: When global transaction IDs are in use, Executed_Gtid_Set // shows the set of GTIDs for transactions that have been executed on the // master. This is the same as the value for the gtid_executed system variable // on this server, as well as the value for Executed_Gtid_Set in the output of // SHOW SLAVE STATUS on this server. Executed_Gtid_Set string }
MasterStatus: This statement provides status information about the binary log files of the master. It requires either the SUPER or REPLICATION CLIENT privilege.
func (MasterStatus) Compare ¶
func (ms MasterStatus) Compare(other MasterStatus) int
Compare compares with another MasterStatus. Returns 1 if left hand side is bigger, 0 if both are equal and -1 if right hand side is bigger.
func (*MasterStatus) FromString ¶
func (ms *MasterStatus) FromString(str string) error
FromString parses as string in the format: mysql-bin.000002;236423 means filename;position.
func (*MasterStatus) Load ¶
func (ms *MasterStatus) Load(ctx context.Context, db QueryRower) error
Load retrieves the current master status from the database and puts it into variable ms.
func (MasterStatus) String ¶
func (ms MasterStatus) String() string
String converts the file name and the position to a string, separated by a semi-colon.
type Preparer ¶
Preparer defines the only needed function to create a new statement in the database. The provided context is used for the preparation of the statement, not for the execution of the statement.
type Querier ¶
type Querier interface { // QueryContext executes a query that returns rows, typically a SELECT. The // args are for any placeholder parameters in the query. QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) }
Querier can query multiple rows in a database.
type QueryRower ¶
type QueryRower interface { // QueryRowContext executes a query that is expected to return at most one // row. QueryRowContext always returns a non-nil value. Errors are deferred // until Row's Scan method is called. QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row }
QueryRower can query one row from a database.
type ResurrectStmt ¶
type ResurrectStmt struct { // DB contains for now only the prepare() function for a new statement // may be extended in the far future. DB Preparer // SQL is any prepareable SQL command, use ? for argument placeholders SQL string // Idle defines the duration how to wait until no query will be executed. Idle time.Duration // Log default logger is PkgLof Log log.Logger // contains filtered or unexported fields }
ResurrectStmt creates a long living sql.Stmt in the database but closes it if within an idle time no query will be executed. Once there is a new query the statement gets resurrected. The ResurrectStmt type is safe for concurrent use with every of its function.
A full working implementation can be found in package config with type DBStorage.
func NewResurrectStmt ¶
func NewResurrectStmt(p Preparer, SQL string) *ResurrectStmt
NewResurrectStmt creates a new resurrected statement via a DB connection to prepare the stmt and a SQL query string. Default idle time is defined in DefaultResurrectStmtIdleTime. Default logger: PkgLog.
func (*ResurrectStmt) IsIdle ¶
func (su *ResurrectStmt) IsIdle() bool
IsIdle returns true if the statement has been closed.
func (*ResurrectStmt) StartIdleChecker ¶
func (su *ResurrectStmt) StartIdleChecker()
StartIdleChecker starts the internal goroutine which checks the idle time. You can only start it once. sql.Stmt.Close() errors gets logged to Info. Those errors will only be returned if you stop the idle checker goroutine. Starting the idle checker is recommended because otherwise you might have a very long lived prepared statement.
func (*ResurrectStmt) StartStmtUse ¶
func (su *ResurrectStmt) StartStmtUse()
StartStmtUse tells the ResurrectStmt type that Stmt() will be used.
func (*ResurrectStmt) Stmt ¶
Stmt returns a prepared statement or an error. The statement gets automatically re-opened once it is closed after an idle time.
func (*ResurrectStmt) StopIdleChecker ¶
func (su *ResurrectStmt) StopIdleChecker() error
StopIdleChecker stops the internal goroutine if it's started. Returns the sql.Stmt.Close error.
func (*ResurrectStmt) StopStmtUse ¶
func (su *ResurrectStmt) StopStmtUse()
StopStmtUse tells the ResurrectStmt type that the Stmt() has been used.
type Table ¶
type Table struct { // Schema represents the name of the database. Might be empty. Schema string // Name of the table Name string // Columns all table columns Columns Columns // CountPK number of primary keys. Auto updated. CountPK int // CountUnique number of unique keys. Auto updated. CountUnique int // contains filtered or unexported fields }
Table represents a table from a database.
func (*Table) AllColumnAliasQuote ¶
AllColumnAliasQuote prefixes all columns with an alias and puts quotes around them. Returns a copy.
func (*Table) ColumnAliasQuote ¶
ColumnAliasQuote prefixes non-id columns with an alias and puts quotes around them. Returns a copy.
func (*Table) LoadColumns ¶
LoadColumns reads the column information from the DB.
func (*Table) LoadSlice ¶
func (t *Table) LoadSlice(dbrSess dbr.SessionRunner, dest interface{}, cbs ...dbr.SelectCb) (int, error)
LoadSlice performs a SELECT * FROM `tableName` query and puts the results into the pointer slice `dest`. Returns the number of loaded rows and nil or 0 and an error. The variadic thrid arguments can modify the SQL query. DEPRECATED
func (*Table) Select ¶
func (t *Table) Select(dbrSess dbr.SessionRunner) (*dbr.SelectBuilder, error)
Select generates a SELECT * FROM tableName statement. DEPRECATED
func (*Table) TableAliasQuote ¶
TableAliasQuote returns a table name with the alias. catalog_product_entity with alias e would become `catalog_product_entity` AS `e`.
type TableOption ¶
TableOption applies options to the Tables struct.
func WithLoadColumnDefinitions ¶
func WithLoadColumnDefinitions(ctx context.Context, db Querier) TableOption
WithLoadColumnDefinitions loads the column definitions from the database for each table in the internal map. Thread safe.
func WithTable ¶
func WithTable(idx int, tableName string, cols ...*Column) TableOption
WithTable inserts a new table to the Tables struct, identified by its index. You can optionally specify the columns. What is the reason to use int as the table index and not a name? Because table names between M1 and M2 get renamed and in a Go SQL code generator script of the CoreStore project, we can guarantee that the generated index constant will always stay the same but the name of the table differs.
func WithTableLoadColumns ¶
WithTableLoadColumns inserts a new table to the Tables struct, identified by its index. What is the reason to use int as the table index and not a name? Because table names between M1 and M2 get renamed and in a Go SQL code generator script of the CoreStore project, we can guarantee that the generated index constant will always stay the same but the name of the table differs.
func WithTableNames ¶
func WithTableNames(idx []int, tableName []string) TableOption
WithTableNames creates for each table name and its index a new table pointer. You should call afterwars the functional option WithLoadColumnDefinitions. This function returns an error if a table index already exists.
type Tables ¶
type Tables struct { // Schema represents the name of the database. Might be empty. Schema string // Prefix will be put in front of each table name. TODO implement table prefix. Prefix string // contains filtered or unexported fields }
Tables handles all the tables defined for a package. Thread safe.
func MustNewTables ¶
func MustNewTables(opts ...TableOption) *Tables
MustNewTables same as NewTableService but panics on error.
func NewTables ¶
func NewTables(opts ...TableOption) (*Tables, error)
NewTables creates a new TableService satisfying interface Manager.
func (*Tables) Delete ¶
Delete removes tables by their given indexes. If no index has been passed then all entries get removed and the map reinitialized.
func (*Tables) Insert ¶
Insert adds a new table into the map. If an entry already exists, it will return an AlreadyExists error behaviour.
func (*Tables) MustTable ¶
MustTable same as Table function but panics when the table cannot be found or any other error occurs.
func (*Tables) Name ¶
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 but returns an empty string.
func (*Tables) Options ¶
func (tm *Tables) Options(opts ...TableOption) error
Options applies options to the Tables service.
func (*Tables) Table ¶
Table returns the structure from a map m by a giving index i. What is the reason to use int as the table index and not a name? Because table names between M1 and M2 get renamed and in a Go SQL code generator script of the CoreStore project, we can guarantee that the generated index constant will always stay the same but the name of the table differs.
type Variables ¶
type Variables []*Variable
Variables contains multiple MySQL configurations.
func (*Variables) AppendFiltered ¶
AppendFiltered appends multiple variables to the current slice. If name is empty, all variables will be loaded. Name argument can contain the SQL wildcard. For now MySQL DSN must have set interpolateParams to true.