xxsql

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package xxsql provides a Go SQL driver for XxSql database.

Index

Constants

View Source
const (
	IsolationLevelDefault         driver.IsolationLevel = iota // Default
	IsolationLevelReadUncommitted                              // ReadUncommitted
	IsolationLevelReadCommitted                                // ReadCommitted
	IsolationLevelRepeatableRead                               // RepeatableRead
	IsolationLevelSerializable                                 // Serializable
)

Isolation levels matching sql.IsolationLevel

View Source
const (
	DefaultAddr         = "127.0.0.1:3306"
	DefaultNet          = "tcp"
	DefaultTimeout      = 10 * time.Second
	DefaultReadTimeout  = 30 * time.Second
	DefaultWriteTimeout = 30 * time.Second
	DefaultCharset      = "utf8mb4"
	DefaultCollation    = "utf8mb4_general_ci"
	MaxAllowedPacket    = 4 * 1024 * 1024 // 4MB
)

Default values for connection configuration.

View Source
const (
	ErrCodeAccessDenied   = 1045
	ErrCodeDuplicateEntry = 1062
	ErrCodeSyntax         = 1064
	ErrCodeTableNotExist  = 1146
	ErrCodeDeadlock       = 1213
	ErrCodeServerGone     = 2006
	ErrCodeConnectionLost = 2013
)

MySQL error codes

View Source
const (
	ProtocolVersion = 10
	MaxPacketSize   = 1<<24 - 1 // 16MB

	// Client capabilities
	ClientLongPassword    = 0x00000001
	ClientFoundRows       = 0x00000002
	ClientLongFlag        = 0x00000004
	ClientConnectWithDB   = 0x00000008
	ClientNoSchema        = 0x00000010
	ClientCompress        = 0x00000020
	ClientODBC            = 0x00000040
	ClientLocalFiles      = 0x00000080
	ClientIgnoreSpace     = 0x00000100
	ClientProtocol41      = 0x00000200
	ClientInteractive     = 0x00000400
	ClientSSL             = 0x00000800
	ClientIgnoreSigpipe   = 0x00001000
	ClientTransactions    = 0x00002000
	ClientSecureConn      = 0x00008000
	ClientMultiStatements = 0x00010000
	ClientMultiResults    = 0x00020000
	ClientPluginAuth      = 0x00080000
	ClientDeprecateEOF    = 0x01000000

	// Default client capabilities
	DefaultClientCapabilities = ClientLongPassword |
		ClientFoundRows |
		ClientLongFlag |
		ClientConnectWithDB |
		ClientProtocol41 |
		ClientTransactions |
		ClientSecureConn |
		ClientMultiStatements |
		ClientMultiResults |
		ClientPluginAuth |
		ClientDeprecateEOF

	// Character sets
	CharacterSetUTF8MB4 = 45

	// Command types
	ComQuit        = 0x01
	ComInitDB      = 0x02
	ComQuery       = 0x03
	ComPing        = 0x0E
	ComStmtPrepare = 0x16
	ComStmtExecute = 0x17
	ComStmtClose   = 0x19

	// Response types
	OKPacket  = 0x00
	ERRPacket = 0xFF
	EOFPacket = 0xFE
)

MySQL protocol constants

View Source
const (
	TypeDecimal    = 0x00
	TypeTiny       = 0x01
	TypeShort      = 0x02
	TypeLong       = 0x03
	TypeFloat      = 0x04
	TypeDouble     = 0x05
	TypeNull       = 0x06
	TypeTimestamp  = 0x07
	TypeLongLong   = 0x08
	TypeInt24      = 0x09
	TypeDate       = 0x0A
	TypeTime       = 0x0B
	TypeDateTime   = 0x0C
	TypeYear       = 0x0D
	TypeNewDate    = 0x0E
	TypeVarChar    = 0x0F
	TypeBit        = 0x10
	TypeNewDecimal = 0xE6
	TypeEnum       = 0xF7
	TypeSet        = 0xF8
	TypeTinyBlob   = 0xF9
	TypeMediumBlob = 0xFA
	TypeLongBlob   = 0xFB
	TypeBlob       = 0xFC
	TypeVarString  = 0xFD
	TypeString     = 0xFE
	TypeGeometry   = 0xFF
)

MySQL column type codes

View Source
const DriverName = "xxsql"

Driver name for registration.

Variables

View Source
var (
	// ErrBadConn indicates the connection is in a bad state.
	ErrBadConn = errors.New("bad connection")

	// ErrTxDone indicates the transaction has already been committed or rolled back.
	ErrTxDone = errors.New("transaction has already been committed or rolled back")

	// ErrDuplicateEntry indicates a unique constraint violation.
	ErrDuplicateEntry = errors.New("duplicate entry")

	// ErrTableNotExist indicates the table does not exist.
	ErrTableNotExist = errors.New("table does not exist")

	// ErrDeadlock indicates a deadlock was detected.
	ErrDeadlock = errors.New("deadlock detected")

	// ErrAccessDenied indicates authentication failed.
	ErrAccessDenied = errors.New("access denied")

	// ErrSyntax indicates a SQL syntax error.
	ErrSyntax = errors.New("syntax error")
)

Sentinel errors

Functions

func Open

func Open(dsn string) (*sql.DB, error)

Open is a convenience function that opens a database connection.

func OpenDB

func OpenDB(dsn string) (*sql.DB, error)

OpenDB opens a database using a connector.

func RegisterDriver

func RegisterDriver()

RegisterDriver registers the xxsql driver with database/sql.

Types

type Config

type Config struct {
	User             string
	Passwd           string
	Net              string        // Network type: "tcp"
	Addr             string        // Network address: "host:port"
	DBName           string        // Database name
	Timeout          time.Duration // Connection timeout
	ReadTimeout      time.Duration // Read timeout
	WriteTimeout     time.Duration // Write timeout
	Charset          string        // Character set
	Collation        string        // Collation
	TLS              bool          // Enable TLS
	AllowOldPassword bool          // Allow old password authentication
	MaxAllowedPacket int           // Maximum packet size allowed
}

Config represents the configuration for an XxSql connection.

func NewConfig

func NewConfig() *Config

NewConfig creates a new Config with default values.

func ParseDSN

func ParseDSN(dsn string) (*Config, error)

ParseDSN parses a MySQL-style DSN string into a Config. DSN format: [username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]

Examples:

  • root@tcp(localhost:3306)/testdb
  • admin:secret@tcp(127.0.0.1:3306)/mydb?charset=utf8mb4&timeout=10s
  • user:pass@/dbname

func (*Config) Clone

func (c *Config) Clone() *Config

Clone returns a copy of the configuration.

func (*Config) FormatDSN

func (c *Config) FormatDSN() string

FormatDSN returns a DSN string from the configuration.

Jump to

Keyboard shortcuts

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