gosql

package module
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2022 License: LGPL-3.0 Imports: 11 Imported by: 0

README

gosql

Utility tools for working with sql databases

Instalation

$ go get gitlab.com/am.driver/gosql

MySQL Databases:

  • autoclosable connections
  • supporting transactions
  • connections pool
  • mysql handler
  • custom ResultSet struct for java developers :)

MySQL examples:

Create connection:

db, err = gosql.NewMySQLConnection("localhost", 3306, "username", "password", "dbname")

Create connections pool:

config := gosql.NewMySQLConfig("localhost", 3306, "username", "password", "dbname")
poolSize := 100

pool, err := gosql.NewMySQLConnectionPool(poolSize, config)
defer pool.CloseAll()

connection := pool.Get()
......
pool.Release(connection)

Working with MySQL HANDLER:

handler := dBase.CreateMySQLHandler("table_name")
defer handler.Close()

for handler.HasNext() {
	result := handler.Next("id < 100", "index-field", 1000)
	for result.Next() {
		......
	}
}

SQLite3 Databases:

Create connection:

db, err := gosql.NewSQLiteConnection("/tmp/sqlite.db")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConcatValues

func ConcatValues(begin string, values []string, delim string, end string) string

func GenerateHash

func GenerateHash() uint32

func PrepareStringValue

func PrepareStringValue(val interface{}) string

Types

type Cell

type Cell struct {
	Index int
	Name  string
	Value []byte
}

type InsertStmt

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

//////////////////////////////////////////////////////////// /////////////////// INSERT & REPLACE /////////////////////// ////////////////////////////////////////////////////////////

func (*InsertStmt) Fields

func (in *InsertStmt) Fields(fields ...string) *InsertStmt

func (*InsertStmt) ReturnGeneratedKey

func (in *InsertStmt) ReturnGeneratedKey() int64

func (*InsertStmt) Run

func (in *InsertStmt) Run() (int64, *SQLError)

func (*InsertStmt) String

func (in *InsertStmt) String() string

func (*InsertStmt) Value

func (in *InsertStmt) Value(values ...interface{}) *InsertStmt

type MySQLAdmin added in v1.0.1

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

func (*MySQLAdmin) CheckDatabaseExists added in v1.0.1

func (dba *MySQLAdmin) CheckDatabaseExists(dbName string) (bool, error)

func (*MySQLAdmin) CheckTableExists added in v1.0.2

func (dba *MySQLAdmin) CheckTableExists(tableName, dbName string) (bool, error)

func (*MySQLAdmin) CreateDatabase added in v1.0.1

func (dba *MySQLAdmin) CreateDatabase(dbName, charset, collation string) error

//////////////////// STRUCTURE //////////////////////////////

func (*MySQLAdmin) DropDatabase added in v1.0.1

func (dba *MySQLAdmin) DropDatabase(dbName string) error

func (*MySQLAdmin) DropTable added in v1.0.2

func (dba *MySQLAdmin) DropTable(tableName, dbName string) error

func (*MySQLAdmin) GetMySQLDBStructure added in v1.0.1

func (dba *MySQLAdmin) GetMySQLDBStructure(dbname string, withRoutines, withEvents bool) (*MySQLDBStructure, error)

func (*MySQLAdmin) RenameDatabase added in v1.0.1

func (dba *MySQLAdmin) RenameDatabase(oldName, newName string) error

func (*MySQLAdmin) RenameDatabaseWithDebug added in v1.0.1

func (dba *MySQLAdmin) RenameDatabaseWithDebug(oldName, newName string) error

type MySQLConfig

type MySQLConfig struct {
	Host   string
	Port   int
	User   string
	Pass   string
	DbName string
}

////////////////// MySQLConfig ////////////////////////

func NewMySQLConfig

func NewMySQLConfig(host string, port int, user string, password string, dataBase string) *MySQLConfig

func (*MySQLConfig) String

func (conf *MySQLConfig) String() string

type MySQLConnection

type MySQLConnection struct {
	Config *MySQLConfig
	// contains filtered or unexported fields
}

///////////////// MySQLConnection //////////////////////

func CreateMySQLConnection

func CreateMySQLConnection(config *MySQLConfig) (MySQLConnection, error)

Create connection to database. Working with Bind.close for auto closing connection

func GetMySQLConnection

func GetMySQLConnection(host string, port int, user string, password string, dataBase string) MySQLConnection

Get connection without error. If can't connect - panic

func NewMySQLConnection

func NewMySQLConnection(host string, port int, user string, password string, dataBase string) (MySQLConnection, error)

Get connection with error

func (*MySQLConnection) Admin added in v1.0.1

func (db *MySQLConnection) Admin() *MySQLAdmin

func (*MySQLConnection) Close

func (db *MySQLConnection) Close()

func (*MySQLConnection) Commit

func (db *MySQLConnection) Commit() *SQLError

func (*MySQLConnection) CreateMySQLHandler

func (db *MySQLConnection) CreateMySQLHandler(table string) *MySQLHandler

func (*MySQLConnection) DumpByQuery

func (db *MySQLConnection) DumpByQuery(query string, tables ...string) ([]string, *SQLError)

//////////////////////////////////////////////////////////// ///////////////////// DUMPING DATA ///////////////////////// ////////////////////////////////////////////////////////////

func (*MySQLConnection) DumpTable

func (db *MySQLConnection) DumpTable(tableName, whereCond string, limit int) ([]string, *SQLError)

func (*MySQLConnection) ExecuteCall

func (db *MySQLConnection) ExecuteCall(procedureName string, params ...interface{}) (ResultSet, *SQLError)

func (*MySQLConnection) ExecuteInsert

func (db *MySQLConnection) ExecuteInsert(query string, params ...interface{}) (int64, *SQLError)

func (*MySQLConnection) ExecuteUpdate

func (db *MySQLConnection) ExecuteUpdate(query string, params ...interface{}) (int64, *SQLError)

func (*MySQLConnection) Hash

func (db *MySQLConnection) Hash() uint32

func (*MySQLConnection) Insert

func (db *MySQLConnection) Insert(table string) *InsertStmt

func (*MySQLConnection) MasterStatus added in v1.0.5

func (db *MySQLConnection) MasterStatus() *ReplicationInfo

/////////////// Replication ////////////////////

func (*MySQLConnection) Replace

func (db *MySQLConnection) Replace(table string) *InsertStmt

func (*MySQLConnection) ReplicationDelete added in v1.0.6

func (db *MySQLConnection) ReplicationDelete(channel string)

func (*MySQLConnection) ReplicationReset

func (db *MySQLConnection) ReplicationReset(channel string)

func (*MySQLConnection) ReplicationSetup

func (db *MySQLConnection) ReplicationSetup(host, user, pass, binlog, pos, channel string)

func (*MySQLConnection) ReplicationSetupByConfig added in v1.0.7

func (db *MySQLConnection) ReplicationSetupByConfig(config *ReplicationInfo)

func (*MySQLConnection) ReplicationStart

func (db *MySQLConnection) ReplicationStart(channel string)

func (*MySQLConnection) ReplicationStatus

func (db *MySQLConnection) ReplicationStatus(channel string) *ReplicationInfo

func (*MySQLConnection) ReplicationStop

func (db *MySQLConnection) ReplicationStop(channel string)

func (*MySQLConnection) Rollback

func (db *MySQLConnection) Rollback() *SQLError

func (*MySQLConnection) Select

func (db *MySQLConnection) Select(query string, params ...interface{}) (ResultSet, *SQLError)

returns ResutSet struct like an java object

func (*MySQLConnection) SelectNative

func (db *MySQLConnection) SelectNative(query string, params ...interface{}) (*sql.Rows, *SQLError)

returns native *sql.Rows

func (*MySQLConnection) ShowConfigVariablesLike

func (db *MySQLConnection) ShowConfigVariablesLike(pattern string) *MySQLVariables

//////////////////// VARIABLES /////////////////////////////

func (*MySQLConnection) String

func (db *MySQLConnection) String() string

func (*MySQLConnection) Transaction

func (db *MySQLConnection) Transaction() *SQLError

func (*MySQLConnection) Update

func (db *MySQLConnection) Update(table string) *UpdateStmt

type MySQLConnectionPool

type MySQLConnectionPool struct {
	sync.Mutex
	// contains filtered or unexported fields
}

/////////////// MySQLConnectionPool ////////////////////

func NewMySQLConnectionPool

func NewMySQLConnectionPool(size int, config *MySQLConfig) (*MySQLConnectionPool, error)

func (*MySQLConnectionPool) CloseAll

func (pool *MySQLConnectionPool) CloseAll()

func (*MySQLConnectionPool) Get

func (pool *MySQLConnectionPool) Get() *MySQLConnection

func (*MySQLConnectionPool) Release

func (pool *MySQLConnectionPool) Release(connection *MySQLConnection)

func (*MySQLConnectionPool) Size

func (pool *MySQLConnectionPool) Size() int

func (*MySQLConnectionPool) String

func (pool *MySQLConnectionPool) String() string

type MySQLDBStructure added in v1.0.1

type MySQLDBStructure struct {
	Tables   map[string]string
	Routines map[string]string
}

type MySQLHandler

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

//////////////////////////////////////////////////////////// /////////////////////// HANDLER //////////////////////////// ////////////////////////////////////////////////////////////

func (*MySQLHandler) Close

func (handler *MySQLHandler) Close()

func (*MySQLHandler) HasNext

func (handler *MySQLHandler) HasNext() bool

func (*MySQLHandler) Next

func (handler *MySQLHandler) Next(condition, keyName string, limit int) ResultSet

func (*MySQLHandler) Prev

func (handler *MySQLHandler) Prev(condition, keyName string, limit int) ResultSet

type MySQLVariables

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

func (*MySQLVariables) Get

func (v *MySQLVariables) Get(vrName string) string

func (*MySQLVariables) GetBool

func (v *MySQLVariables) GetBool(vrName string) bool

func (*MySQLVariables) GetInt

func (v *MySQLVariables) GetInt(vrName string) int

func (*MySQLVariables) String

func (v *MySQLVariables) String() string

/////////////////// variables //////////////////////////////

type ReplicationInfo

type ReplicationInfo struct {
	MasterId        string
	Status          bool
	SlaveLag        int
	MasterHost      string
	MasterUser      string
	MasterPass      string
	BinlogFile      string
	BinlogPos       string
	ChannelName     string
	IgnoreServerIds []string
}

func (*ReplicationInfo) String

func (replica *ReplicationInfo) String() string

/////////////////// replication ////////////////////////////

func (*ReplicationInfo) StringFull added in v1.0.7

func (replica *ReplicationInfo) StringFull() string

type ResultSet

type ResultSet struct {
	Rows []Row
	// contains filtered or unexported fields
}

func (*ResultSet) GetBytes

func (rs *ResultSet) GetBytes(column interface{}) []byte

func (*ResultSet) GetInt

func (rs *ResultSet) GetInt(column interface{}) int

func (*ResultSet) GetLong

func (rs *ResultSet) GetLong(column interface{}) int64

func (*ResultSet) GetMetaData

func (rs *ResultSet) GetMetaData() *ResultSetMetaData

func (*ResultSet) GetString

func (rs *ResultSet) GetString(column interface{}) string

func (*ResultSet) MergeWith

func (rs *ResultSet) MergeWith(rs2 ResultSet)

func (*ResultSet) Next

func (rs *ResultSet) Next() bool

func (*ResultSet) Print

func (rs *ResultSet) Print()

func (*ResultSet) Size

func (rs *ResultSet) Size() int

type ResultSetMetaData

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

func (*ResultSetMetaData) GetColumnCount

func (rsmd *ResultSetMetaData) GetColumnCount() int

func (*ResultSetMetaData) GetColumnName

func (rsmd *ResultSetMetaData) GetColumnName(i int) string

func (*ResultSetMetaData) GetColumnType

func (rsmd *ResultSetMetaData) GetColumnType(i int) string

func (*ResultSetMetaData) Hash

func (rsmd *ResultSetMetaData) Hash() uint32

type Row

type Row struct {
	Values []Cell
}

func NewRow

func NewRow() Row

type SQLError

type SQLError struct {
	Code    int
	Message string
	// contains filtered or unexported fields
}

func (*SQLError) Error

func (err *SQLError) Error() error

func (*SQLError) String

func (err *SQLError) String() string

type SQLiteConnection

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

func NewSQLiteConnection

func NewSQLiteConnection(path string) (*SQLiteConnection, error)

func (*SQLiteConnection) Close

func (con *SQLiteConnection) Close()

func (*SQLiteConnection) ExecuteInsert

func (con *SQLiteConnection) ExecuteInsert(query string, params ...interface{}) (int64, error)

returns last insert id or error

func (*SQLiteConnection) ExecuteQuery

func (con *SQLiteConnection) ExecuteQuery(query string, params ...interface{}) (ResultSet, error)

returns ResutSet struct like an java object

func (*SQLiteConnection) ExecuteUpdate

func (con *SQLiteConnection) ExecuteUpdate(query string, params ...interface{}) (int64, error)

returns affected rows count or error

type UpdateStmt

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

//////////////////////////////////////////////////////////// //////////////////////// UPDATE //////////////////////////// ////////////////////////////////////////////////////////////

func (*UpdateStmt) Limit

func (up *UpdateStmt) Limit(limit int) *UpdateStmt

func (*UpdateStmt) Run

func (up *UpdateStmt) Run() (int64, *SQLError)

func (*UpdateStmt) SetField

func (up *UpdateStmt) SetField(fName string, fValue interface{}) *UpdateStmt

func (*UpdateStmt) String

func (up *UpdateStmt) String() string

func (*UpdateStmt) WhereField

func (up *UpdateStmt) WhereField(fName string, fValue interface{}) *UpdateStmt

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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