gomysql

package module
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2023 License: MIT Imports: 10 Imported by: 0

README

GOMysql Build Status Coverage Status GoDoc License

GOMysql A Simple logging package, prefixes logging level. Also supports error logging to bugsnag.

Currently supported middlewares:

Usage

Get the library:

$ go get -v github.com/illuminasy/gomysql

Mysql DB Migration


import github.com/illuminasy/gomysql/migrate

//MigrationDir default directory "./internal/database/migration/sql"
// copy all your migration sql there
// check github.com/golang-migrate/migrate/v4 for more details
config := gomysql.Config{
	DBHost: "localhost",
	DBPort: "3306",
	DBUser: "root",
	DBPass: "root",
	DBName: "testdb",
	MigrationDir "somedir" // use this to use custom directory
}

client := gomysql.GetClient(config)
err := client.Migrate()
if err != nil {
	fmt.Println(err)
}

Mysql

config := gomysql.Config{
	DBHost: "localhost"
	DBPort: "3306"
	DBUser: "root"
	DBPass: "root"
	DBName: "testdb"
	MigrationDir "somedir" // use this to use custom directory
}

// To check connection
status, err := c.ConnCheck()

Mysql with Newrelic

config := gomysql.Config{
	DBHost: "localhost"
	DBPort: "3306"
	DBUser: "root"
	DBPass: "root"
	DBName: "testdb"
	MigrationDir "somedir" // use this to use custom directory
	NewrelicEnabled: true
}

// To check connection
status, err := c.ConnCheck()

// To Query rows
client := gomysql.GetClient(config)
rows, err := client.QueryWithContext(ctx, "select * from table")
if err != nil {
	fmt.Println(err)
}

Documentation

Index

Constants

View Source
const DefaultMigrationDir = "./internal/database/migration/sql"

DefaultMigrationDir Default directory for migration sql files

View Source
const MaxIdleConns = 5

MaxIdleConns maximum idle connections

View Source
const MaxIdleConnsTime = 1 * time.Second

MaxIdleConnsTime maximum idle connection timeout

View Source
const MaxOpenConns = 10

MaxOpenConns maximum open connections

View Source
const MaxOpenConnsTime = 30 * time.Second

MaxOpenConnsTime maximum connection timeout

Variables

This section is empty.

Functions

func PrepareBatchInsertColumns added in v0.1.3

func PrepareBatchInsertColumns(rowCount int, columnCount int) string

PrepareBatchInsertColumns prepares (?,?,?),(?,?,?),(?,?,?)

func PrepareInsertColumn added in v0.1.3

func PrepareInsertColumn(columnCount int) string

PrepareInsertColumn prepares (?,?,?)

Types

type Client

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

Client ...

func GetClient

func GetClient(c Config) *Client

GetClient ...

func (Client) CleanUp added in v0.2.1

func (c Client) CleanUp() error

CleanUp start db migration down

func (Client) ConnCheck

func (c Client) ConnCheck() (bool, error)

ConnCheck check if connection exists

func (Client) ConnCheckWithContext added in v0.4.0

func (c Client) ConnCheckWithContext(ctx context.Context) (bool, error)

ConnCheck check if connection exists

func (Client) Exec

func (c Client) Exec(query string, params []interface{}) (sql.Result, error)

Exec ...

func (Client) ExecWithContext added in v0.4.0

func (c Client) ExecWithContext(ctx context.Context, query string, params []interface{}) (sql.Result, error)

ExecWithContext ...

func (Client) GetStats added in v0.1.2

func (c Client) GetStats() (sql.DBStats, error)

GetStats return database stats

func (Client) Migrate

func (c Client) Migrate() error

Migrate start db migration up

func (Client) Prepare added in v0.1.2

func (c Client) Prepare(query string) (*sql.Stmt, error)

Prepare Prepare query

func (Client) PrepareWithContext added in v0.4.0

func (c Client) PrepareWithContext(ctx context.Context, query string) (*sql.Stmt, error)

PrepareWithContext Prepare query

func (Client) Query

func (c Client) Query(query string, params []interface{}) (*sql.Rows, error)

Query queries multiple rows

func (Client) QueryRow

func (c Client) QueryRow(query string, params []interface{}) (*sql.Row, error)

QueryRow Execute query and returns single row

func (Client) QueryRowWithContext added in v0.4.0

func (c Client) QueryRowWithContext(ctx context.Context, query string, params []interface{}) (*sql.Row, error)

QueryRowWithContext Execute query and returns single row

func (Client) QueryWithContext added in v0.4.0

func (c Client) QueryWithContext(ctx context.Context, query string, params []interface{}) (*sql.Rows, error)

QueryWithContext queries multiple rows

func (Client) StmtExec

func (c Client) StmtExec(stmt *sql.Stmt, params []interface{}) (sql.Result, error)

StmtExec Execute query

func (Client) StmtExecWithContext added in v0.4.0

func (c Client) StmtExecWithContext(ctx context.Context, stmt *sql.Stmt, params []interface{}) (sql.Result, error)

StmtExecWithContext Execute query

func (Client) StmtQuery added in v0.1.2

func (c Client) StmtQuery(stmt *sql.Stmt, params []interface{}) (*sql.Rows, error)

StmtQuery queries multiple rows

func (Client) StmtQueryRow added in v0.1.2

func (c Client) StmtQueryRow(stmt *sql.Stmt, params []interface{}) *sql.Row

StmtQueryRow Execute query and returns single row

func (Client) StmtQueryRowWithContext added in v0.4.0

func (c Client) StmtQueryRowWithContext(ctx context.Context, stmt *sql.Stmt, params []interface{}) *sql.Row

StmtQueryRowWithContext Execute query and returns single row

func (Client) StmtQueryWithContext added in v0.4.0

func (c Client) StmtQueryWithContext(ctx context.Context, stmt *sql.Stmt, params []interface{}) (*sql.Rows, error)

StmtQueryWithContext queries multiple rows

func (Client) TxBegin added in v0.1.2

func (c Client) TxBegin() (*sql.Tx, error)

TxBegin Start and return transaction

func (Client) TxBeginWithContext added in v0.4.0

func (c Client) TxBeginWithContext(ctx context.Context) (*sql.Tx, error)

TxBeginWithContext Start and return transaction

func (Client) TxCommit added in v0.1.2

func (c Client) TxCommit(tx *sql.Tx) error

TxCommit commits transaction

func (Client) TxExec added in v0.1.2

func (c Client) TxExec(tx *sql.Tx, query string, params []interface{}) (sql.Result, error)

TxExec Execute query

func (Client) TxExecWithContext added in v0.4.0

func (c Client) TxExecWithContext(ctx context.Context, tx *sql.Tx, query string, params []interface{}) (sql.Result, error)

TxExecWithContext Execute query

func (Client) TxQuery added in v0.1.2

func (c Client) TxQuery(tx *sql.Tx, query string, params []interface{}) (*sql.Rows, error)

TxQuery queries multiple rows

func (Client) TxQueryRow added in v0.1.2

func (c Client) TxQueryRow(tx *sql.Tx, query string, params []interface{}) *sql.Row

TxQueryRow Execute query and returns single row

func (Client) TxQueryRowWithContext added in v0.4.0

func (c Client) TxQueryRowWithContext(ctx context.Context, tx *sql.Tx, query string, params []interface{}) *sql.Row

TxQueryRowWithContext Execute query and returns single row

func (Client) TxQueryWithContext added in v0.4.0

func (c Client) TxQueryWithContext(ctx context.Context, tx *sql.Tx, query string, params []interface{}) (*sql.Rows, error)

TxQueryWithContext queries multiple rows

func (Client) TxRollback added in v0.1.2

func (c Client) TxRollback(tx *sql.Tx) error

TxRollback rollback transaction

type Config

type Config struct {
	DBHost           string
	DBPort           string
	DBUser           string
	DBPass           string
	DBName           string
	DBSSL            string
	DBTimeout        string
	DBCharset        string
	DBCollation      string
	MigrationDir     string
	MigrationsTable  string
	ParaseTime       string
	MaxOpenConns     int
	MaxIdleConns     int
	MaxOpenConnsTime time.Duration
	MaxIdleConnsTime time.Duration
	NewrelicEnabled  bool
}

Config ...

type Datastore

type Datastore interface {
	ConnCheck() (bool, error)
	GetStats()

	Query(query string, params []interface{}) (*sql.Rows, error)
	QueryRow(query string, params []interface{}) (*sql.Row, error)
	Exec(query string, params []interface{}) (sql.Result, error)

	QueryWithContext(ctx context.Context, query string, params []interface{}) (*sql.Rows, error)
	QueryRowWithContext(ctx context.Context, query string, params []interface{}) (*sql.Row, error)
	ExecWithContext(ctx context.Context, query string, params []interface{}) (sql.Result, error)

	Prepare(query string) (*sql.Stmt, error)
	StmtQuery(stmt *sql.Stmt, params []interface{}) (*sql.Rows, error)
	StmtQueryRow(stmt *sql.Stmt, params []interface{}) *sql.Row
	StmtExec(stmt *sql.Stmt, params []interface{}) (sql.Result, error)

	PrepareWithContext(ctx context.Context, query string) (*sql.Stmt, error)
	StmtQueryWithContext(ctx context.Context, stmt *sql.Stmt, params []interface{}) (*sql.Rows, error)
	StmtQueryRowWithContext(ctx context.Context, stmt *sql.Stmt, params []interface{}) *sql.Row
	StmtExecWithContext(ctx context.Context, stmt *sql.Stmt, params []interface{}) (sql.Result, error)

	BeginTx() (*sql.Tx, error)
	TxPrepare(tx *sql.Tx, query string) (*sql.Stmt, error)
	TxQuery(tx *sql.Tx, query string, params []interface{}) (*sql.Rows, error)
	TxQueryRow(tx *sql.Tx, query string, params []interface{}) *sql.Row
	TxExec(tx *sql.Tx, query string, params []interface{}) (sql.Result, error)
	TxCommit(tx *sql.Tx) error
	TxRollback(tx *sql.Tx) error

	BeginTxWithContext(ctx context.Context) (*sql.Tx, error)
	TxPrepareWithContext(ctx context.Context, tx *sql.Tx, query string) (*sql.Stmt, error)
	TxQueryWithContext(ctx context.Context, tx *sql.Tx, query string, params []interface{}) (*sql.Rows, error)
	TxQueryRowWithContext(ctx context.Context, tx *sql.Tx, query string, params []interface{}) *sql.Row
	TxExecWithContext(ctx context.Context, tx *sql.Tx, query string, params []interface{}) (sql.Result, error)

	Migrate() error
	// contains filtered or unexported methods
}

Datastore ...

Jump to

Keyboard shortcuts

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