mysql

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2020 License: MIT Imports: 12 Imported by: 1

Documentation

Index

Constants

View Source
const TimeFormat = "2006-01-02 15:04:05.000000000"

Sandard MySQL datetime format

Variables

View Source
var FieldTypeMap map[string]FieldType = map[string]FieldType{
	"tinyint":  INT,
	"smallint": INT,
	"int":      INT,

	"bigint": INT64,
	"number": INT64,

	"bool": BOOL,

	"float":   FLOAT64,
	"double":  FLOAT64,
	"real":    FLOAT64,
	"decimal": FLOAT64,

	"char":     STRING,
	"varchar":  STRING,
	"text":     STRING,
	"longtext": STRING,

	"datetime":  TIME,
	"timestamp": TIME,

	"date": DATE,
}
View Source
var FieldTypeNameMap map[FieldType]string

Functions

func DurationString

func DurationString(d time.Duration) string

Convert time.Duration to string representation of mysql.TIME

func GetEmptyValuesByType

func GetEmptyValuesByType(types []FieldType) []interface{}

Get the corresponding empty values of types.

func GetFieldNames

func GetFieldNames(db *sql.DB, dbName string, tableName string) []string

Get the names of all fields of this table.

func GetValueByTypeAtIndex

func GetValueByTypeAtIndex(row Row, fType FieldType, index int) interface{}

Cast the values in the 'row' into go type specific by 'fType' at index.

func GetValuesByType

func GetValuesByType(row Row, types []FieldType) []interface{}

Cast the values in the 'row' into go type specific by 'types'.

func ParseDuration

func ParseDuration(str string) (dur time.Duration, err error)

Parse duration from MySQL string format [+-]H+:MM:SS[.UUUUUUUUU]. Leading and trailing spaces are ignored. If format is invalid returns nil.

func ParseTime

func ParseTime(str string, loc *time.Location) (t time.Time, err error)

Parses string datetime in TimeFormat using loc location. Converts MySQL zero to time.Time zero.

func TimeString

func TimeString(t time.Time) string

Returns t as string in MySQL format Converts time.Time zero to MySQL zero.

Types

type Blob

type Blob []byte

type DBWrapper

type DBWrapper struct {
	DB   *sql.DB
	Name string
}

func ConnectMysqlDB

func ConnectMysqlDB(host string, port int, database, username, password string) DBWrapper

func ConnectMysqlDBByConfig

func ConnectMysqlDBByConfig(config MysqlConfig) DBWrapper

func ConnectMysqlDBConnStr

func ConnectMysqlDBConnStr(connStr string, name string) DBWrapper

connStr: tcp:<host>:<port>*<db>/<user>/<pwd> name: name of this connection.

func WrapDB

func WrapDB(db *sql.DB) (dbWrapper DBWrapper, err error)

func (DBWrapper) Exec

func (db DBWrapper) Exec(sqlText string, args ...interface{}) sql.Result

func (DBWrapper) ExecErr

func (db DBWrapper) ExecErr(sqlText string, args ...interface{}) (sql.Result, error)

func (DBWrapper) Query

func (db DBWrapper) Query(sqlText string, args ...interface{}) (qr QueryResult)

func (DBWrapper) QueryScalar

func (db DBWrapper) QueryScalar(sqlText string, args ...interface{}) interface{}

func (DBWrapper) QueryScalarBool

func (db DBWrapper) QueryScalarBool(sqlText string, args ...interface{}) bool

func (DBWrapper) QueryScalarBytes

func (db DBWrapper) QueryScalarBytes(sqlText string, args ...interface{}) []byte

func (DBWrapper) QueryScalarFloat

func (db DBWrapper) QueryScalarFloat(sqlText string, args ...interface{}) float64

func (DBWrapper) QueryScalarInt

func (db DBWrapper) QueryScalarInt(sqlText string, args ...interface{}) int

func (DBWrapper) QueryScalarStr

func (db DBWrapper) QueryScalarStr(sqlText string, args ...interface{}) string

type Date

type Date struct {
	Year       int16
	Month, Day byte
}

For MySQL DATE type

func ParseDate

func ParseDate(str string) (dd Date, err error)

Convert string date in format YYYY-MM-DD to Date. Leading and trailing spaces are ignored.

func (Date) IsZero

func (dd Date) IsZero() bool

True if date is 0000-00-00

func (Date) Localtime

func (dd Date) Localtime() time.Time

Converts Date to time.Time using Local location. Converts MySQL zero to time.Time zero.

func (Date) String

func (dd Date) String() string

func (Date) Time

func (dd Date) Time(loc *time.Location) (t time.Time)

Converts Date to time.Time using loc location. Converts MySQL zero to time.Time zero.

type Field

type Field struct {
	Name string
	Type FieldType
}

func GetFields

func GetFields(db *sql.DB, dbName string, tableName string) []Field

Get the fields of one table, and the order is exactly the same as they are in the table.

type FieldType

type FieldType uint8
const (
	UNKNOWN FieldType = iota
	BYTE
	STRING
	INT
	UINT
	INT64
	UINT64
	DATE
	TIME
	BOOL
	FLOAT64
)

func GetFieldTypes

func GetFieldTypes(db *sql.DB, dbName string, tableName string) []FieldType

Get the types of all fields of this table.

func GetFieldTypesByOrder

func GetFieldTypesByOrder(db *sql.DB, dbName string, tableName string, fieldNames []string) []FieldType

Get the specific field types of one table. Returns the types in the order of 'fieldNames'. If a field cannot be found in the db, a panic rise.

type MysqlConfig

type MysqlConfig struct {
	Host     string
	Port     int
	Database string
	UserName string
	Password string
}

type QueryResult

type QueryResult struct {
	Cols []string
	Rows []Row

	Error error
	// contains filtered or unexported fields
}

func (*QueryResult) ColID

func (q *QueryResult) ColID(colName string) int

type Raw

type Raw struct {
	Typ uint16
	Val *[]byte
}

type Row

type Row []interface{}

Result row - contains values for any column of received row.

If row is a result of ordinary text query, its element can be []byte slice, contained result text or nil if NULL is returned.

If it is result of prepared statement execution, its element field can be: intX, uintX, floatX, []byte, Date, Time, time.Time (in Local location) or nil

func (Row) Bin

func (tr Row) Bin(nn int) (bin []byte)

Get the nn-th value and return it as []byte ([]byte{} if NULL)

func (Row) Bool

func (tr Row) Bool(nn int) (val bool)

It is like BoolErr but panics if conversion is impossible.

func (Row) BoolErr

func (tr Row) BoolErr(nn int) (val bool, err error)

Get the nn-th value and return it as bool. Return error if conversion is impossible.

func (Row) Date

func (tr Row) Date(nn int) (val Date)

It is like DateErr but panics if conversion is impossible.

func (Row) DateErr

func (tr Row) DateErr(nn int) (val Date, err error)

Get the nn-th value and return it as Date (0000-00-00 if NULL). Return error if conversion is impossible.

func (Row) Duration

func (tr Row) Duration(nn int) (val time.Duration)

It is like DurationErr but panics if conversion is impossible.

func (Row) DurationErr

func (tr Row) DurationErr(nn int) (val time.Duration, err error)

Get the nn-th value and return it as time.Duration (0 if NULL). Return error if conversion is impossible.

func (Row) Float

func (tr Row) Float(nn int) (val float64)

Get the nn-th value and return it as float64 (0 if NULL). Panic if conversion is impossible.

func (Row) FloatErr

func (tr Row) FloatErr(nn int) (val float64, err error)

Get the nn-th value and return it as float64 (0 if NULL). Return error if conversion is impossible.

func (Row) ForceBool

func (tr Row) ForceBool(nn int) (val bool)

It is like BoolErr but return false if conversion is impossible.

func (Row) ForceDate

func (tr Row) ForceDate(nn int) (val Date)

It is like DateErr but return 0000-00-00 if conversion is impossible.

func (Row) ForceDuration

func (tr Row) ForceDuration(nn int) (val time.Duration)

It is like DurationErr but return 0 if conversion is impossible.

func (Row) ForceFloat

func (tr Row) ForceFloat(nn int) (val float64)

Get the nn-th value and return it as float64. Return 0 if value is NULL or if conversion is impossible.

func (Row) ForceInt

func (tr Row) ForceInt(nn int) (val int)

Get the nn-th value and return it as int. Return 0 if value is NULL or conversion is impossible.

func (Row) ForceInt64

func (tr Row) ForceInt64(nn int) (val int64)

Get the nn-th value and return it as int64. Return 0 if value is NULL or conversion is impossible.

func (Row) ForceLocaltime

func (tr Row) ForceLocaltime(nn int) (val time.Time)

It is like LocaltimeErr but returns 0000-00-00 00:00:00 if conversion is impossible.

func (Row) ForceTime

func (tr Row) ForceTime(nn int, loc *time.Location) (val time.Time)

It is like TimeErr but returns 0000-00-00 00:00:00 if conversion is impossible.

func (Row) ForceUint

func (tr Row) ForceUint(nn int) (val uint)

Get the nn-th value and return it as uint. Return 0 if value is NULL or conversion is impossible.

func (Row) ForceUint64

func (tr Row) ForceUint64(nn int) (val uint64)

Get the nn-th value and return it as uint64. Return 0 if value is NULL or conversion is impossible.

func (Row) Int

func (tr Row) Int(nn int) (val int)

Get the nn-th value and return it as int (0 if NULL). Panic if conversion is impossible.

func (Row) Int64

func (tr Row) Int64(nn int) (val int64)

Get the nn-th value and return it as int64 (0 if NULL). Panic if conversion is impossible.

func (Row) Int64Err

func (tr Row) Int64Err(nn int) (val int64, err error)

Get the nn-th value and return it as int64 (0 if NULL). Return error if conversion is impossible.

func (Row) IntErr

func (tr Row) IntErr(nn int) (val int, err error)

Get the nn-th value and return it as int (0 if NULL). Return error if conversion is impossible.

func (Row) Localtime

func (tr Row) Localtime(nn int) (val time.Time)

As LocaltimeErr but panics if conversion is impossible.

func (Row) LocaltimeErr

func (tr Row) LocaltimeErr(nn int) (t time.Time, err error)

Get the nn-th value and return it as time.Time in Local location (zero if NULL). Returns error if conversion is impossible. It can convert Date to time.Time.

func (Row) Str

func (tr Row) Str(nn int) (str string)

Get the nn-th value and return it as string ("" if NULL)

func (Row) Time

func (tr Row) Time(nn int, loc *time.Location) (val time.Time)

As TimeErr but panics if conversion is impossible.

func (Row) TimeErr

func (tr Row) TimeErr(nn int, loc *time.Location) (t time.Time, err error)

Get the nn-th value and return it as time.Time in loc location (zero if NULL) Returns error if conversion is impossible. It can convert Date to time.Time.

func (Row) Uint

func (tr Row) Uint(nn int) (val uint)

Get the nn-th value and return it as uint (0 if NULL). Panic if conversion is impossible.

func (Row) Uint64

func (tr Row) Uint64(nn int) (val uint64)

Get the nn-th value and return it as uint64 (0 if NULL). Panic if conversion is impossible.

func (Row) Uint64Err

func (tr Row) Uint64Err(nn int) (val uint64, err error)

Get the nn-th value and return it as uint64 (0 if NULL). Return error if conversion is impossible.

func (Row) UintErr

func (tr Row) UintErr(nn int) (val uint, err error)

Get the nn-th value and return it as uint (0 if NULL). Return error if conversion is impossible.

type Timestamp

type Timestamp struct {
	time.Time
}

func (Timestamp) String

func (t Timestamp) String() string

Jump to

Keyboard shortcuts

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