Documentation ¶
Index ¶
- Constants
- func CreateSchema(db *sql.DB, name string) error
- func CurrentSchema(db *sql.DB) (string, error)
- func DisableGeneralLog(db *sql.DB) error
- func DropSchema(db *sql.DB, name string) error
- func EnableGeneralLog(db *sql.DB) error
- func ErrorIs(err error, number int) bool
- func FlushGeneralLog(db *sql.DB) error
- func GeneralQueryLogEnabled(db *sql.DB) (bool, error)
- func GlobalVariable(db *sql.DB, name string) (string, error)
- func IsDBCreateExists(err error) bool
- func MaskPasswordInDSN(dsn string) string
- func MustSQLw(statement string, keyValuePairs ...string) string
- func ReplaceDSNDatabase(dsn string, name string) (string, error)
- func SQLw(statement string, keyValuePairs ...string) (string, error)
- func SchemaExists(db *sql.DB, name string) (bool, error)
- func SetDSNParams(dsn string, parameters map[string]string) (string, error)
- func SetLogOutput(db *sql.DB, outputs ...LogOutput) error
- func SetTableComment(db *sql.DB, table string, comment string, schema ...string) error
- func SetTableCommentJSON(db *sql.DB, table string, comment any, schema ...string) error
- func TableComment(db *sql.DB, table string, schema ...string) (string, error)
- func TableCommentJSON(db *sql.DB, table string, dest any, schema ...string) error
- func TableExists(db *sql.DB, name string) (bool, error)
- type Error
- type GeneralLogEvent
- type LogOutput
Constants ¶
const ( ErrDBCreateExists int = 1007 ErrDBDropExists int = 1008 ErrDubEntry int = 1062 ErrClientConnRefused int = 2005 )
Error numbers of MySQL handled by this package.
Variables ¶
This section is empty.
Functions ¶
func CreateSchema ¶
CreateSchema creates schema (or database) using the already open connection db.
When error is returned, it is of type xmysql.Error.
func CurrentSchema ¶ added in v1.1.0
CurrentSchema returns the name of the current schema of db.
func DisableGeneralLog ¶
DisableGeneralLog turns off the General Log.
func DropSchema ¶
DropSchema drops schema (or database) using the already open connection db.
When error is returned, it is of type xmysql.Error.
func EnableGeneralLog ¶
EnableGeneralLog turns on the General Log. Since all queries are logged, it is recommended not doing this on a production instance.
func FlushGeneralLog ¶
FlushGeneralLog will flush the general log file and truncate the general_log table. During these operations, the genera log is disabled, and enabled again after when it was enabled before.
func GeneralQueryLogEnabled ¶
GeneralQueryLogEnabled turns whether the General Query Log is enabled. Since all queries are logged, it is recommended not doing this on a production instance.
func GlobalVariable ¶
GlobalVariable returns value of a global variable by its name.
func IsDBCreateExists ¶
IsDBCreateExists returns whether err is Error and ErrDBCreateExists.
func MaskPasswordInDSN ¶
MaskPasswordInDSN masks the password within the MySQL data source name dsn. This function is usually used when displaying or logging the DSN.
When password is empty (not provided) the mask is added anyway. When the DSN is something that was not a DSN, the mask itself is returned to prevent possible mistakes. Deprecated: use github.com/golistic/xgo/xsql
func ReplaceDSNDatabase ¶
ReplaceDSNDatabase takes dsn and replaces the database name. It returns the new Data Source Name. Deprecated: use github.com/golistic/xgo/xsql
func SQLw ¶ added in v1.2.0
SQLw takes the SQL statement and substitutes the key/value pairs using the placeholder format `$(key)`. For example, executing `SQLw("SELECT c1 FROM $(tblName)", tblName, "t1")` results in `SELECT c1 FROM $(tblName)`. If a key is found within a quoted part of the statement, it is not substituted. Dangling keys (key without value) are ignored.
Important! You must use as much as possible parameter placeholders such as '?' or '$1'. This is however not always possible: for example, when using a dynamic schema or table based on context.
func SchemaExists ¶
SchemaExists returns whether schema with given name exists.
func SetDSNParams ¶
SetDSNParams sets parameters for the given dsn.
Some parameters are always set, if not provided through parameters or when none were given: * parseTime = true Deprecated: use github.com/golistic/xgo/xsql
func SetLogOutput ¶
SetLogOutput sets the destination of the general and slow query logs. It is possible to specify multiple destination. If LogOutputNone is provided, it will take precedence over all others. No-op if no output is provided.
func SetTableComment ¶ added in v1.2.0
SetTableComment sets the table's comment. If schema is not provided, current schema will be used.
func SetTableCommentJSON ¶ added in v1.2.0
SetTableCommentJSON sets the table's comment marshalled as JSON. If schema is not provided, current schema will be used.
func TableComment ¶ added in v1.2.0
TableComment retrieves the comment of a table. If schema is not provided, current schema will be used.
func TableCommentJSON ¶ added in v1.2.0
TableCommentJSON retrieves the comment of a table, unmarshalls it as JSON, and stores it in dest. If schema is not provided, current schema will be used. Panics for same reasons as Go's json.Unmarshal would.
Types ¶
type Error ¶
type Error struct { Message string DriverError error Query string Values []any Filename string Line int Number int }
Error wraps mysql.MySQLError with additional information such as file information where the error occurred, query with values when available, and normalized and nicer message.
func ErrorTxBegin ¶
ErrorTxBegin returns a xmysql.Error, storing err, and setting a fixed message 'failed starting transaction'.
func ErrorTxCommit ¶
ErrorTxCommit returns a xmysql.Error, storing err, and setting a fixed message 'failed committing transaction'.
func NewError ¶
NewError returns a new xmysql.Error, storing err. The filename and line number where the error occurred is saved. Panics when the app.Package is not part of the caller's filename.
func NewErrorQuery ¶
NewErrorQuery returns a new xmysql.Error, storing err with query and its values that were interpolated. Note: use this with care and only in debugging/development situations.
func NewErrorSprintf ¶
NewErrorSprintf returns a new xmysql.Error, storing err, but sets also the message to format and its optional arguments.
type GeneralLogEvent ¶
type GeneralLogEvent struct { Time time.Time UserHost string ThreadID int ServerID int CommandType string Argument string }
func GetGeneralLogEvents ¶
GetGeneralLogEvents retrieves events from the General Query Log using the argLike string, if not empty, to filter through the argument field. When limit is 0, or over the hard limit of 1000, the hard limit will be used. Note that this only works when log output TABLE is active.