mysqldb

package module
v0.0.0-...-6b0d8fb Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2018 License: MIT Imports: 3 Imported by: 0

README

go-crud-mysql

Go Crud library for MySql

Installation

$ go get github.com/Ulbora/go-crud-mysql

Usage

InitializeMysql

res = InitializeMysql("localhost:3306", "admin", "admin", "some_database")	
if res != true {
	// do something
}

GetDb

Gets a pointer to the DB for creating transactions
db = GetDb()
tx, err := db.Begin()

Insert

var noTx *sql.Tx
var q = "INSERT INTO table (title, created_date, text, client_id) VALUES (?, ?, ?, ?)"
var a []interface{}
a = append(a, "test insert 2", time.Now(), "some content text", 125)	
success, insID := Insert(noTx, q, a...)

Update

var noTx *sql.Tx
var q = "UPDATE table set title = ?, modified_date = ?, text = ? where id = ? and client_id = ? "
a := []interface{}{"test insert update", time.Now(), "some content new text updated", insertID, 125}
success := Update(noTx, q, a...)

Get

a := []interface{}{insertID, 125}
var q = "select * from table WHERE id = ? and client_id = ?"
rowPtr := Get(q, a...)
if rowPtr != nil {
	foundRow := rowPtr.row
}

GetList

a := []interface{}{125}
var q = "select * from table WHERE client_id = ? order by id"
rowsPtr := GetList(q, a...)
if rowsPtr != nil {	
	foundRows := rowsPtr.rows
}

Delete

var noTx *sql.Tx
var q = "DELETE FROM table WHERE id = ? "
success := Delete(noTx, q, insertID)

Close Database

success := Close()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close() bool

Close close

func Delete

func Delete(tx *sql.Tx, query string, args ...interface{}) bool

Delete deletes records

func GetDb

func GetDb() *sql.DB

GetDb gets a pointer to db for transactions

func InitializeMysql

func InitializeMysql(host, user, pw, dbName string) bool

InitializeMysql Mysql init to mysql

func Insert

func Insert(tx *sql.Tx, query string, args ...interface{}) (bool, int64)

Insert inserts a row. Passing in tx allows for transactions

func Update

func Update(tx *sql.Tx, query string, args ...interface{}) bool

Update updates a row. Passing in tx allows for transactions

Types

type DbRow

type DbRow struct {
	Columns []string
	Row     []string
}

DbRow database row

func Get

func Get(query string, args ...interface{}) *DbRow

Get get a row. Passing in tx allows for transactions

type DbRows

type DbRows struct {
	Columns []string
	Rows    [][]string
}

DbRows array of database rows

func GetList

func GetList(query string, args ...interface{}) *DbRows

GetList get a list of rows. Passing in tx allows for transactions

Jump to

Keyboard shortcuts

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