mysql

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2021 License: MIT Imports: 26 Imported by: 0

README

mysql

一个随便写的mysql

import _ "github.com/luyu6056/mysql" 以代替其他mysql

例如gorm下调用此驱动

import (
	"fmt"

	"github.com/jinzhu/gorm"
	_ "github.com/luyu6056/mysql"
)

func main() {

	gorm.Open("mysql", "yy:123456@tcp(172.26.45.140:3306)/bbs?charset=utf8")

	
}

优化内容

  • 当语句不存在?时,跳过预处理Prepare与close流程,在Execute阶段,直接提交原始语句到mysql,减少2句提交,能降低服务器的cpu占用,减少网络通信数据。
  • 加入stmt(预处理)缓冲map,短时间内频繁提交同一句预处理语句时,重复使用同一个stmt结构体,能缓解高并发下占用过高的预处理资源,导致mysql报max_prepared_stmt_count错误。
  • 读取mysql返回消息时候,一次性读取16384个字节,避免官方库的先读4字节消息头,再读一个消息长度的模式,这将导致cpu占用异常高,优化了读取模式后,执行select的qps可以比官方库提升100%

可能存在的问题

  • 目前只做了mysql_native_password和caching_sha2_password密码协商套件,能保证mysql5与mysql8,mariadb默认设置下正常通讯,使用其他加密套件将会无法连接。
  • go-sql-driver/mysql里面对dsn定义了多个扩展参数,目前只能解析loc=xxxx,对于其他扩展参数将会无效

Documentation

Index

Constants

View Source
const (
	CLIENT_LONG_PASSWORD     = 0x00000001
	CLIENT_LONG_FLAG         = 0x00000004
	CLIENT_CONNECT_WITH_DB   = 0x00000008
	CLIENT_COMPRESS          = 0x00000020
	CLIENT_LOCAL_FILES       = 0x00000080
	CLIENT_PROTOCOL_41       = 0x00000200
	CLIENT_SSL               = 0x00000800
	CLIENT_SECURE_CONNECTION = 0x00008000
	CLIENT_TRANSACTIONS      = 0x00002000
	CLIENT_MULTI_RESULTS     = 0x00020000
	CLIENT_PLUGIN_AUTH       = 0x00080000
)

capabilities定义

View Source
const (
	ISDEBUG = true
)

Variables

View Source
var (
	Mysql_ssl_ca   string
	Mysql_ssl_cert string
	Mysql_ssl_key  string
)

Functions

func Bytes2str

func Bytes2str(b []byte) string

func DEBUG

func DEBUG(v ...interface{})

func Log

func Log(format string, v ...interface{})

func ReadLengthCodedStringFromBuffer

func ReadLengthCodedStringFromBuffer(msg *MsgBuffer, return_str bool) (string, error)

func ReadLength_Coded_Binary

func ReadLength_Coded_Binary(buf *MsgBuffer) (int, error)

func ReadLength_Coded_Byte

func ReadLength_Coded_Byte(msg *MsgBuffer) ([]byte, error)

func ReadLength_Coded_Slice added in v1.0.0

func ReadLength_Coded_Slice(data []byte, pos *int) (l int, err error)

func ReadNullTerminatedString

func ReadNullTerminatedString(msg *MsgBuffer) (string, error)

func Str2bytes

func Str2bytes(s string) []byte

一些与mysql无关的func

func Write1lenmsg

func Write1lenmsg(write *MsgBuffer, msg []byte)

func WriteNullTerminatedString

func WriteNullTerminatedString(write *MsgBuffer, msg string)

func WriteNullmsg

func WriteNullmsg(write *MsgBuffer, msg []byte)

func Writelenmsg added in v0.0.4

func Writelenmsg(write *MsgBuffer, msg []byte)

Types

type Database_mysql_conn

type Database_mysql_conn struct {
	*Mysql_Conn
	// contains filtered or unexported fields
}

func (*Database_mysql_conn) Begin

func (conn *Database_mysql_conn) Begin() (driver.Tx, error)

func (*Database_mysql_conn) Prepare

func (conn *Database_mysql_conn) Prepare(query string) (driver.Stmt, error)

type Database_mysql_stmt

type Database_mysql_stmt struct {
	// contains filtered or unexported fields
}

func (*Database_mysql_stmt) Close

func (stmt *Database_mysql_stmt) Close() (err error)

func (*Database_mysql_stmt) Exec

func (stmt *Database_mysql_stmt) Exec(args []driver.Value) (driver.Result, error)

func (*Database_mysql_stmt) Execute added in v0.0.4

func (stmt *Database_mysql_stmt) Execute(args []driver.Value) error

func (Database_mysql_stmt) LastInsertId

func (stmt Database_mysql_stmt) LastInsertId() (int64, error)

func (Database_mysql_stmt) NumInput

func (stmt Database_mysql_stmt) NumInput() int

func (*Database_mysql_stmt) Query

func (stmt *Database_mysql_stmt) Query(args []driver.Value) (driver.Rows, error)

func (Database_mysql_stmt) RowsAffected

func (stmt Database_mysql_stmt) RowsAffected() (int64, error)

type Database_mysql_tx

type Database_mysql_tx struct {
	*Mysql_Conn
}

func (*Database_mysql_tx) Commit

func (tx *Database_mysql_tx) Commit() error

func (*Database_mysql_tx) Rollback

func (tx *Database_mysql_tx) Rollback() error

type Database_rows

type Database_rows struct {
	// contains filtered or unexported fields
}

func (*Database_rows) Close

func (rows *Database_rows) Close() error

func (*Database_rows) Columns

func (rows *Database_rows) Columns() (columns []string)

func (*Database_rows) Next

func (rows *Database_rows) Next(dest []driver.Value) (err error)

type Field_struct

type Field_struct struct {
	Offset  uintptr
	Kind    reflect.Kind
	Field_t reflect.Type
}

type MsgBuffer

type MsgBuffer struct {
	// contains filtered or unexported fields
}

func New

func New(size int) *MsgBuffer

New returns a new MsgBuffer whose buffer has the given size.

func NewBuffer

func NewBuffer(n int) *MsgBuffer

func (*MsgBuffer) Bytes

func (w *MsgBuffer) Bytes() []byte

func (*MsgBuffer) Close

func (r *MsgBuffer) Close() error

func (*MsgBuffer) Len

func (w *MsgBuffer) Len() int

func (*MsgBuffer) Make

func (w *MsgBuffer) Make(l int) []byte

func (*MsgBuffer) Next

func (w *MsgBuffer) Next(l int) []byte

func (*MsgBuffer) PreBytes

func (w *MsgBuffer) PreBytes(n int) []byte

func (*MsgBuffer) Read

func (r *MsgBuffer) Read(p []byte) (n int, err error)

func (*MsgBuffer) ReadByte

func (r *MsgBuffer) ReadByte() (b byte, err error)

ReadByte reads and returns the next byte from the input or ErrIsEmpty.

func (*MsgBuffer) Reset

func (w *MsgBuffer) Reset()

func (*MsgBuffer) ResetBuf

func (w *MsgBuffer) ResetBuf(b []byte)

func (*MsgBuffer) Shift

func (r *MsgBuffer) Shift(len int)

Shift shifts the "read" pointer.

func (*MsgBuffer) String

func (w *MsgBuffer) String() string

func (*MsgBuffer) Truncate

func (w *MsgBuffer) Truncate(i int)

func (*MsgBuffer) Write

func (w *MsgBuffer) Write(b []byte) (int, error)

func (*MsgBuffer) WriteByte

func (w *MsgBuffer) WriteByte(s byte)

func (*MsgBuffer) WriteString

func (w *MsgBuffer) WriteString(s string)

type MySQLDriver

type MySQLDriver struct{}

func (MySQLDriver) Open

func (d MySQLDriver) Open(dsn string) (driver.Conn, error)

返回一个全新的mysql conn

type MysqlColumn added in v1.0.0

type MysqlColumn struct {
	// contains filtered or unexported fields
}

type MysqlRows

type MysqlRows struct {
	Buffer *MsgBuffer
	// contains filtered or unexported fields
}

func (*MysqlRows) Columns

func (row *MysqlRows) Columns(mysql *Mysql_Conn) (err error)

func (*MysqlRows) ReadResultMsg added in v1.0.1

func (row *MysqlRows) ReadResultMsg(mysql *Mysql_Conn) (err error)

type Mysql_Conn

type Mysql_Conn struct {
	//Version            string
	Thread_id uint32 //线程ID
	//seed               []byte
	Capabilities uint32 //协议协商

	Status bool
	// contains filtered or unexported fields
}

func (*Mysql_Conn) Close

func (mysql *Mysql_Conn) Close() error

func (*Mysql_Conn) Exec

func (mysql *Mysql_Conn) Exec(sql []byte) (lastInsertId int64, rowsAffected int64, err error)

func (*Mysql_Conn) Query

func (mysql *Mysql_Conn) Query(sql []byte, rows *MysqlRows) (err error)

Jump to

Keyboard shortcuts

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