Giter Club home page Giter Club logo

sqlt's Introduction

GoDoc

sqlt

Sqlt is a wrapper package for jmoiron/sqlx

This wrapper build based on tsenart/nap master-slave and its load-balancing configuration with some modification

Since this package is just a wrapper, you can use it 100% like nap but with the taste of sqlx

Usage

To connect to database, you need an appended connection string with ; delimeter, but there is some notes:

  • First connection will always be considered as master connection
  • Another connection will be considered as slave
databaseCon := "con1;" + "con2;" + "con3"
db, err := sqlt.Open("postgres", databaseCon)

Or

databaseCon := "con1"
db, err := sqlt.Open("postgres", databaseCon)

Query Example:

row := db.QuerRowy(query, args)
rows, err := db.Query(query, args)
err := db.Select(&struct, query, args)
err := db.Get(&struct, query, args)

preapre and preparex for sql and sqlx are supported

use preparex to enable ScanStruct

statement := db.Prepare(query)
rows, err := statement.Query(param)
row := statement.QueryRows(param).Scan(&var)
statement := db.Preparex(query)
rows, err := statement.Query(param)
row := statement.QueryRows(param).ScanStruct(&struct)

Complete example:

package main

// lib/pq or go-sql-driver needed to import database driver
import (
    _"github.com/lib/pq"
    _ "github.com/go-sql-driver/mysql"
    "github.com/albert-widi/sqlt"
    "log"
)

func main() {
    masterDSN := "user:password@tcp(master_ip:master_port)/database_name"
    slave1DSN := "user:password@tcp(slave_ip:slave_port)/database_name"
    slave2DSN := "user:password@tcp(slave2_ip:slave2_port)/database_name"

    dsn := masterDSN + ";" + slave1DSN + ";" + slave2DSN
    
    db, err = sqlt.Open("mysql", dsn)
    if err != nil {
      log.Println("Error ", err.Error())
    }
    
    // do something using db connection
}

Heartbeat/Watcher

SQLT provide a mechanism to switch between slaves if something bad happen. If no slaves is available then all connection will go to master.

The watcher will auto-reconnect to your slaves if possible and start to load-balancing itself.

To watch all slaves connection, use DoHeartbeat. This will ping your database every second and make auto-reconnect if needed.

databaseCon := "con1;" + "con2;" + "con3"
db, err := sqlt.Open("postgres", databaseCon)

if err != nil {
  return err
}

//this will automatically ping the database and watch the connection
db.DoHeartBeat()

Don't forget to stop the heartbeat when your application stop, because it(goroutine) will most likely leak if you forgot to close it.

db.StopBeat()

Database status

You can also get the database status, for example:

databaseCon := "con1;" + "con2;" + "con3"
db, err := sqlt.OpenWithName("postgres", databaseCon, "order")

if err != nil {
  return err
}

//this will automatically ping the database and watch the connection
db.DoHeartBeat()

//this will return database status in JSON
status, _ := db.GetStatus()

Output:

DbStatus {
  Name:       "order",
  Connected:  true,
  LastActive: "21 September 2016",
  Error:      nil,
}

3rd party references:

sqlt's People

Contributors

albertwidi avatar sameervitian avatar ironecally avatar qzaidi avatar wendyadi avatar christianh13 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.