mysqlMgr

package module
v0.0.0-...-e339d9b Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2018 License: MIT Imports: 7 Imported by: 0

README

Go Report Card

GoDoc

Overview

mysqlMgr is a library which wraps database connections and prepared statments in a single data structure which is safe for concurrent use.

Installation

To install the library use go get:

go get github.com/koepkeca/mysqlMgr

Configuration

Each instance of the library is designed to manage one database connection. The New method takes an io.Reader which contains json configuration data. An example of the file is here:

{
    "database" : "mysqlMgr_Demo",
    "server" : "localhost",
    "port" : "3306",
    "driver" : "mysql",
    "user" : "changeme",
    "pw" : "changeme"
}

Usage

To use the library you can create a new instance with an io.Reader that contains the json configuration. A very basic example is here, please note, this example skips error checking and a more complete example is available in the example folder.

confRdr, _ := ioutil.ReadFile("config.conf")
confByte := bytes.NewBuffer(confRdr)
s, _ := mysqlMgr.New(confByte)
_ = s.AddStmt("statement","INSERT INTO table (value1, value2) VALUES (?,?)")
//then get the statement
stmt, _ := s.GetStmt("statement")
s.Close()

Example program

There is an example program that demonstrates the library. It is fully functional but requires a few steps to set it up.

Prereqs

The example program requires a few items.

  • A words file (linux dictionary) located in /usr/share/dict/words used to populate the random data pool. You can use any list of strings seperated by new lines.
  • A mysql database with the test database created. The schema is located in test_database.sql in the example directory.
  • A properly configured configuration file. There is an example configuration file which needs to be modified with your specific configuration values in order to function properly.

This example program creates a random data pool and the sequentially load 8192 random records into the example database. You can adjust this number by changing the maxElements in example.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MysqlConn

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

MysqlConn is the public structure that contains a channel controlling access to the underlying store

func New

func New(cfg io.Reader) (mc MysqlConn, err error)

New creates a new MysqlConn manager using the configuration passed to it in cfg. If there is an error creating the manager an error will be created. An error can be returned after the go routine is created, if this is the case, this method shuts down the running routing therefore, if this function returns any error, Close does not need to be called from the calling method/function.

func (MysqlConn) AddStmt

func (mc MysqlConn) AddStmt(k string, q string) (e error)

AddStmt adds statement q to the store with a key of k. Any error is returned as e To retrieve these, use GetStmt

func (MysqlConn) Close

func (mc MysqlConn) Close()

Close closes the underlying channel and terminates the go-routine. This should be run as cleanup for each MysqlConn

func (MysqlConn) GetStmt

func (mc MysqlConn) GetStmt(n string) (sm *sql.Stmt, e error)

GetStmt returns the statement stores as key n Error is returned if the statement does not exist in store

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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