db

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2021 License: MIT Imports: 3 Imported by: 0

README

Gofiliate\godb

A simple to use Go Module to handle seperate Read/Write connections to a MySql database.

Installation

To add to your project

# sudo go get github.com/gofiliate/db

Configuration

Connection details for the database should be placed in a file called config.json. Simply copy the config.dist.json file to config.json

# sudo cp config.dist.json config.json 
# nano config.json 
{
  "DbrUser" : "read_username",
  "DbrPass" : "read_password",
  "DbrHost" : "read_host",
  "DbrPort" : "read_port",
  "DbrName" : "read_database",
  "DbwUser" : "write_username",
  "DbwPass" : "write_password",
  "DbwHost" : "write_host",
  "DbwPort" : "write_port",
  "DbwName" : "write_database"
}

Usage


package main


import (
    "github.com/gofiliate/godb"
    "fmt"
)

type Organization struct {
	organization_id string
	organization_name string
	status_id int
}


func main() {
    
        //Create a Read Only Connection
	godb.ConnectMysqlRead()

	query, err := godb.DBR.Prepare("SELECT organization_id as organization_id, organization_name as organization_name, status_id as status_id FROM organizations")

	if err != nil {
		fmt.Println("Cannot Prepare Query")
		return
	}

	rows, err := query.Query()

	var organizations []Organization
	for rows.Next() {

		var org Organization
		rows.Scan(&org.organization_id, &org.organization_name, &org.status_id)
		organizations = append(organizations, org)
	}

	fmt.Printf("Rows are: %v\n", organizations)

}






Documentation

Index

Constants

This section is empty.

Variables

View Source
var DBR *sql.DB
View Source
var DBW *sql.DB

Functions

func ConnectMysqlRead

func ConnectMysqlRead() (err error)

func ConnectMysqlWrite

func ConnectMysqlWrite() (err error)

func GetMysqlReadConnectionString

func GetMysqlReadConnectionString() (connectionString string)

func GetMysqlWriteConnectionString

func GetMysqlWriteConnectionString() (connectionString string)

func LoadConfigExternal

func LoadConfigExternal(mode string, username string, password string, host string, port int, database string)

Types

type ConnectionDetails

type ConnectionDetails struct {
	DbrUser string `json:"DbrUser"`
	DbrPass string `json:"DbrPass"`
	DbrHost string `json:"DbrHost"`
	DbrPort string `json:"DbrPort"`
	DbrName string `json:"DbrName"`
	DbwUser string `json:"DbwUser"`
	DbwPass string `json:"DbwPass"`
	DbwHost string `json:"DbwHost"`
	DbwPort string `json:"DbwPort"`
	DbwName string `json:"DbwName"`
	SslMode string `json:"SslMode"`
}

Jump to

Keyboard shortcuts

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