csdb

package
v0.0.0-...-6f8fa1e Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2016 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package csdb implements MySQL helper for tables, columns, statements, replication, validation and DB variables.

Index

Constants

View Source
const (
	MainTable       = "main_table"
	AdditionalTable = "additional_table"
	ScopeTable      = "scope_table"
)

@deprecated

View Source
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.

View Source
const EnvDSN string = "CS_DSN"

EnvDSN is the name of the environment variable

Variables

View Source
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 GetDSN

func GetDSN() (string, error)

GetDSN returns the data source name from an environment variable or an error

func GetParsedDSN

func GetParsedDSN() (*mysql.Config, error)

GetParsedDSN checks the environment variable EnvDSN and if a DSN can be found it gets parsed into an URL.

func IsValidIdentifier

func IsValidIdentifier(names ...string) error

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

http://dev.mysql.com/doc/refman/5.7/en/identifiers.html

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

func (c *Column) DataTypeSimple() string

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

func (c *Column) GoPrimitive() string

GoPrimitive detects the Go type of a SQL table column as a non nullable type.

func (*Column) GoPrimitiveNull

func (c *Column) GoPrimitiveNull() string

GoPrimitiveNull detects the Go type of a SQL table column as a nullable type.

func (*Column) GoString

func (c *Column) GoString() string

GoString returns the Go types representation. See interface fmt.GoStringer

func (*Column) IsAutoIncrement

func (c *Column) IsAutoIncrement() bool

IsAutoIncrement checks if column has an auto increment property

func (*Column) IsCurrentTimestamp

func (c *Column) IsCurrentTimestamp() bool

IsCurrentTimestamp checks if the Default field is a current timestamp

func (*Column) IsNull

func (c *Column) IsNull() bool

IsNull checks if column can have null values

func (*Column) IsPK

func (c *Column) IsPK() bool

IsPK checks if column is a primary key

func (*Column) IsUnique

func (c *Column) IsUnique() bool

IsUnique checks if column is a unique key

func (*Column) IsUnsigned

func (c *Column) IsUnsigned() bool

IsUnsigned checks if field TypeRaw contains the word unsigned.

type Columns

type Columns []*Column

Columns contains a slice of column types

func LoadColumns

func LoadColumns(ctx context.Context, db Querier, table string) (Columns, error)

LoadColumns returns all columns from a table in the current database. For now MySQL DSN must have set interpolateParams to true.

func (Columns) ByName

func (cs Columns) ByName(fieldName string) *Column

ByName finds a column by its name. Case sensitive. Guaranteed to a non-nil return value.

func (Columns) ColumnsNoPK

func (cs Columns) ColumnsNoPK() Columns

ColumnsNoPK returns all non primary key columns

func (Columns) FieldNames

func (cs Columns) FieldNames() (fieldNames []string)

FieldNames returns all column names

func (Columns) Filter

func (cs Columns) Filter(f func(*Column) bool) (cols Columns)

Filter returns a new slice filtered by predicate f

func (Columns) First

func (cs Columns) First() *Column

First returns the first column from the slice. Guaranteed to a non-nil return value.

func (Columns) GoString

func (cs Columns) GoString() string

GoString returns the Go types representation. See interface fmt.GoStringer

func (Columns) Hash

func (cs Columns) Hash() ([]byte, error)

Hash calculates a non-cryptographic, fast and efficient hash value from all columns. Current hash algorithm is fnv64a.

func (Columns) JoinFields

func (cs Columns) JoinFields(sep ...string) string

JoinFields joins the field names into a string, separated by the optional first argument.

func (Columns) Len

func (cs Columns) Len() int

Len returns the length

func (Columns) Less

func (cs Columns) Less(i, j int) bool

Less compares via the Pos field.

func (Columns) Map

func (cs Columns) Map(f func(*Column) *Column) Columns

Map will run function f on all items in Columns and returns a copy of the slice and the item.

func (Columns) PrimaryKeys

func (cs Columns) PrimaryKeys() Columns

PrimaryKeys returns all primary key columns

func (Columns) String

func (cs Columns) String() string

String same as GoString()

func (Columns) Swap

func (cs Columns) Swap(i, j int)

Swap changes the position

func (Columns) UniqueKeys

func (cs Columns) UniqueKeys() Columns

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

type Preparer interface {
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}

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

func (su *ResurrectStmt) Stmt(ctx context.Context) (*sql.Stmt, error)

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 NewTable

func NewTable(tableName string, cs ...*Column) *Table

NewTable initializes a new table structure

func (*Table) AllColumnAliasQuote

func (t *Table) AllColumnAliasQuote(alias string) []string

AllColumnAliasQuote prefixes all columns with an alias and puts quotes around them. Returns a copy.

func (*Table) ColumnAliasQuote

func (t *Table) ColumnAliasQuote(alias string) []string

ColumnAliasQuote prefixes non-id columns with an alias and puts quotes around them. Returns a copy.

func (*Table) In

func (t *Table) In(n string) bool

In checks if column name n is a column of this table. Case sensitive.

func (*Table) LoadColumns

func (t *Table) LoadColumns(ctx context.Context, db Querier) (err error)

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

func (t *Table) TableAliasQuote(alias string) string

TableAliasQuote returns a table name with the alias. catalog_product_entity with alias e would become `catalog_product_entity` AS `e`.

type TableOption

type TableOption func(*Tables) error

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

func WithTableLoadColumns(ctx context.Context, db Querier, idx int, tableName string) TableOption

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

func (tm *Tables) Delete(idxs ...int)

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

func (tm *Tables) Insert(i int, ts *Table) error

Insert adds a new table into the map. If an entry already exists, it will return an AlreadyExists error behaviour.

func (*Tables) Len

func (tm *Tables) Len() int

Len returns the number of all tables.

func (*Tables) MustTable

func (tm *Tables) MustTable(i int) *Table

MustTable same as Table function but panics when the table cannot be found or any other error occurs.

func (*Tables) Name

func (tm *Tables) Name(i int) 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 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

func (tm *Tables) Table(i int) (*Table, error)

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.

func (*Tables) Update

func (tm *Tables) Update(i int, ts *Table) error

Update sets a new table for a given index. Overrides silently existing entries.

type Variable

type Variable struct {
	Name  string
	Value string
}

Variable represents one MySQL configuration value retrieved from the database.

func (*Variable) LoadOne

func (v *Variable) LoadOne(ctx context.Context, db QueryRower, name string) error

LoadOne loads a single variable identified by name for the current session. For now MySQL DSN must have set interpolateParams to true.

type Variables

type Variables []*Variable

Variables contains multiple MySQL configurations.

func (*Variables) AppendFiltered

func (vs *Variables) AppendFiltered(ctx context.Context, db Querier, name string) error

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.

func (Variables) FindOne

func (vs Variables) FindOne(name string) (v Variable)

FindOne finds one entry in the slice. May return an empty type. Comparing is case sensitive.

func (Variables) Len

func (vs Variables) Len() int

Len returns the length

func (Variables) Less

func (vs Variables) Less(i, j int) bool

Less compares two slice values

func (Variables) Swap

func (vs Variables) Swap(i, j int)

Swap changes the position

Jump to

Keyboard shortcuts

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