...

Package mysql

import "github.com/je3f0o/go-mysql"
Overview
Index

Overview ▾

This is a very simple and lightweight MySQL library for learning the Go programming language. It was written in my first three days of learning Go.

Please note that this library currently does not support multiple different database connections. Most microservices typically only have one database, but for projects that require multiple different database connections, this library may be updated in the future to support them.

Variables

Set to `true` will be logging every query with values before executing.

var Debug = false

func Delete

func Delete(
    table string,
    where map[string]interface{},
    args ...map[string]interface{},
) sql.Result

Deletes data from a specified table.

Parameters:

Returns:

func DeleteFirst

func DeleteFirst(
    table string,
    where map[string]interface{},
    options ...map[string]interface{},
) sql.Result

Same api with `Delete(...)` method except it will override `options["limit"]` to set 1.

func EscapeId

func EscapeId(id string, ignore_dot ...bool) string

Escapes a SQL identifier for safe use in a query.

Parameters:

Returns:

Example:

EscapeId("INFORMATION_SCHEMA.COLUMNS")       // output: `INFORMATION_SCHEMA`.`COLUMNS`
EscapeId("some.weird.table.or.column", true) // output: `some.weird.table.or.column`

func Exec

func Exec(query string, values ...interface{}) sql.Result

Executes an user defined query.

Parameters:

Returns:

func ExecQuery

func ExecQuery(query string, values ...interface{}) 
      *sql.Rows

Executes an user defined query with values. Which is useful when user wants to use `sql.Rows.Scan(...)` method to convert datatypes.

Parameters:

Returns:

func First

func First(
    table string,
    where map[string]interface{},
    options ...map[string]interface{},
) map[string]interface{}

Same api with `Select(...)` method except it will override `options["limit"]` to set 1 and returns a single row if found.

func Init

func Init(cfg *Config)

Initialize database connection with given configuration.

func Insert

func Insert(table string, data map[string]interface{}) sql.Result

Inserts data into a table.

Parameters:

Returns:

TODO: update this method to support multiple rows

func InsertRow

func InsertRow(table string, data map[string]interface{}) sql.Result

Insert a single row data into a table.

Parameters:

Returns:

func ParseDatetime

func ParseDatetime(value interface{}) time.Time

Parse a SQL datetime string in the format "2006-01-02 15:04:05.000" and convert to a `time.Time`.

Parameters:

Returns:

Example:

type _json map[string]interface{}

where := _json{ "access_token": access_token }
data  := mysql.First(oauth2.TokensTable, where, _json{
  "columns": []string{
    "user_id",
    "access_token_expires_at",
    "refresh_token_expires_at",
  },
})
if data == nil { return }
expires_at := mysql.ParseDatetime(data["access_token_expires_at"])
// code...

func ParseUint32

func ParseUint32(value interface{}) uint32

Converts a string to uint32

Parameters:

Returns:

func Select

func Select(
    table string,
    where map[string]interface{},
    args ...map[string]interface{},
) []map[string]interface{}

Retrieve data from specified `table` with the given `where` condition and options.

Parameters:

Options:

Returns:

Example:

type _json map[string]interface{}

where   := _json{"user_id": user_id}
options := _json{"order": "created_at DESC", "limit": 30}
rows    := mysql.Select("producst", where, optioins)

func Update

func Update(
    table string,
    data, where map[string]interface{},
    args ...map[string]interface{},
) sql.Result

Updates the data in a table with specified conditions.

Parameters:

Returns:

func UpdateFirst

func UpdateFirst(
    table string,
    data, where map[string]interface{},
    options ...map[string]interface{},
) sql.Result

Same api with `Update(...)` method except it will override `options["limit"]` to set 1.

type Config

type Config struct {
    Host     string `yaml:"host,omitempty"`
    Port     int16  `yaml:"port,omitempty"`
    Socket   string `yaml:"socket,omitempty"`
    DBName   string `yaml:"name"`
    Username string `yaml:"user"`
    Password string `yaml:"pass"`
}

func NewConfig

func NewConfig() *Config

Returns a pointer to a newly allocated `Config` struct with default values for: