gomysql

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2014 License: MIT Imports: 4 Imported by: 0

README

GoMysql

GoMysql is a Google Go Language Based Database Wraper.Using this Package you can easily intract with mysql database from Go lang.

Build Status

How to Install GoMysql

go get -u github.com/biswarupadhikari/gomysql

How to Test GoMysql

#Install GoMysql
go get -u github.com/biswarupadhikari/gomysql
#Run Tests
go test github.com/biswarupadhikari/gomysql

Connect To Mysql database

package main
import (
	"github.com/biswarupadhikari/gomysql"
	"log"
)

//Connect Using Default PORT 3306
db,err := gomysql.Connect("localhost", "DBUsername", "DBPassword", "DBName")
if err!=nil{
	log.Fatal("Failed to Connect to Database")
}
/**
 * Connect Using Alternative PORT AND Custom HOST
 * db,err := gomysql.Connect("example.com", "DBUsername", "DBPassword", "DBName", "9595")
 */

Select Record From DataBase Table

users:=db.Select("*").From("users").Get()
for i:=0;i<len(users);i++{
	user:=users[i]
	log.Println("Id => ",user["id"]," || Username ",user["username"])
}
//SQL OUTPUT => Select * FROM users

Select Record Using Specific Columns

users:=db.Select("id,username").From("users").Get()
for i:=0;i<len(users);i++{
	user:=users[i]
	log.Println("Id => ",user["id"]," || Username ",user["username"])
}
//SQL OUTPUT => Select id,username FROM users

How to Use where Condition

users:=db.Select("id,username").From("users").Where("id","=",157).Get()
for i:=0;i<len(users);i++{
	user:=users[i]
	log.Println("Id => ",user["id"]," || Username ",user["username"])
}
//SQL OUTPUT => Select id,username FROM users WHERE id=157

How to Use Multiple Where Condition

users:=db.Select("id,username").From("users").Where("username","=","userone").Where("password","=","secret").Get()
for i:=0;i<len(users);i++{
	user:=users[i]
	log.Println("Id => ",user["id"]," || Username ",user["username"])
}
//SQL OUTPUT => Select id,username FROM users WHERE username="userone" AND password="secret"

How to use AND And OR Condition

users:=db.Select("id,username").From("users").Where("role","=","administrator").ORWhere("role","=","superadmin").Get()
for i:=0;i<len(users);i++{
	user:=users[i]
	log.Println("Id => ",user["id"]," || Username ",user["username"])
}
//SQL OUTPUT => Select id,username FROM users WHERE role="administrator" OR role="superadmin"

How to Use Raw Where Condition

users:=db.Select("id,username").From("users").RawWhere("role=? OR role=?","administrator","superadmin").Get()
for i:=0;i<len(users);i++{
	user:=users[i]
	log.Println("Id => ",user["id"]," || Username ",user["username"])
}
//SQL OUTPUT => Select id,username FROM users WHERE role="administrator" OR role="superadmin"

How to Insert Data to Database Table

data := make(map[string]interface{})
data["username"] = "biswarupadhikari"
data["password"] = "mysecretpass"
data["age"] = 27
db.Table("users").Insert(data)
log.Println("Record Inserted")
//SQL OUTPUT => INSERT INTO users(username,password,age) VALUES("biswarupadhikari","mysecretpass",27)

Alternative Syntax For Insert Data

db.Table("users").InsertSQL(map[string]interface{}{"username":"biswarupadhikari","password":"mysecretpass","age":27})
log.Println("Record Inserted")
//SQL OUTPUT => INSERT INTO users(username,password,age) VALUES("biswarupadhikari","mysecretpass",27)

How to Get Insert ID After Inserting Data

data := make(map[string]interface{})
data["username"] = "biswarupadhikari"
data["password"] = "mysecretpass"
data["age"] = 27
db.Table("users").Insert(data)
result := query.Insert(data)
newId, _ := result.LastInsertId()
log.Println("Last Insert Id Is", newId)
//SQL OUTPUT => INSERT INTO users(username,password,age) VALUES("biswarupadhikari","mysecretpass",27)

Update Database Record

data := make(map[string]interface{})
data["username"] = "mynewusername"
data["password"] = "new secret pass"
db.Table("users").Where("id", "=", 158).Update(data)
log.Println("Record Updated")
//SQL OUTPUT => UPDATE users SET username="mynewusername",password="new secret pass" WHERE id= 158

Update Record Using Multiple Condition

data := make(map[string]interface{})
data["username"] = "mynewusername"
data["password"] = "new secret pass"
db.Table("users").Where("id", "=", 158).Where("role_id","=",15).Update(data)
log.Println("Record Updated")
//SQL OUTPUT => UPDATE users SET username="mynewusername",password="new secret pass" WHERE id= 158  AND role_id=15

Get Affected Rows after Update

data := make(map[string]interface{})
data["password"] = "new Password"
result:=db.Table("users").Where("id", ">", 2).Update(data)
affectedRows,_:=result.RowsAffected()
log.Println("Affected Rows",affectedRows)
//SQL OUTPUT => UPDATE users SET password="new Password" WHERE id > 2

Delete Record From Database Table

db.Table("users").Where("id", "=",158).Delete()
log.Println("Record Deleted")
//SQL OUTPUT => DELETE FROM users WHERE id > 158

Get Affected Rows after Delete

result:=db.Table("users").Where("id", ">", 2).Delete()
affectedRows,_:=result.RowsAffected()
log.Println("Affected Rows",affectedRows)
//SQL OUTPUT => DELETE FROM users WHERE id > 158
Authors and Contributors

@biswarupadhikari Biswarup Adhikari.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GoMysql

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

*

  • GoMysql Structure

func Connect

func Connect(dbHost string, dbUsername string, dbPassword string, dbName string, params ...string) (*GoMysql, error)

*

  • Connect To DataBase using username pass and dbname Host Name and Port
  • host localhost
  • port 3306

func (*GoMysql) Delete

func (gomysql *GoMysql) Delete() sql.Result

*

  • Delete Records From Mysql DataBase Table

func (*GoMysql) DeleteSQL

func (gomysql *GoMysql) DeleteSQL() (string, []interface{})

*

  • Get Delete Sql Query

func (*GoMysql) From

func (gomysql *GoMysql) From(tableName string) *GoMysql

*

  • Select From a Table

func (*GoMysql) Get

func (gomysql *GoMysql) Get() []map[string]interface{}

*

  • Get Records

func (*GoMysql) GetMappedValues

func (gomysql *GoMysql) GetMappedValues() []interface{}

*

*Get Combined DataValues and Condtion Mapped Values

func (*GoMysql) GetQuery

func (gomysql *GoMysql) GetQuery() *GoMysql

*

  • Get New Query Start New Query

func (*GoMysql) GetSQL

func (gomysql *GoMysql) GetSQL() (string, []interface{})

*

  • Get Sql Query

func (*GoMysql) Insert

func (gomysql *GoMysql) Insert(data map[string]interface{}) sql.Result

*

  • Insert Data Into Table Using Data

func (*GoMysql) InsertSQL

func (gomysql *GoMysql) InsertSQL(data map[string]interface{}) (string, []interface{})

*

  • Get Insert Sql Query

func (*GoMysql) Join

func (gomysql *GoMysql) Join(joinType string, joinTable string, joinCond string, joinDataValues ...interface{}) *GoMysql

*

  • Join Query

func (*GoMysql) ORWhere

func (gomysql *GoMysql) ORWhere(key string, operator string, dataValue interface{}) *GoMysql

*

  • Add OR Where Conditions

func (*GoMysql) RawWhere

func (gomysql *GoMysql) RawWhere(condition string, dataValues ...interface{}) *GoMysql

*

  • Add OR Where Conditions

func (*GoMysql) RessetQuery

func (gomysql *GoMysql) RessetQuery()

*

  • Reset Query Data

func (*GoMysql) Select

func (gomysql *GoMysql) Select(fields string) *GoMysql

*

  • Select Fields

func (*GoMysql) Table

func (gomysql *GoMysql) Table(tableName string) *GoMysql

*

  • Select Table

func (*GoMysql) Update

func (gomysql *GoMysql) Update(data map[string]interface{}) sql.Result

*

  • Update Data

func (*GoMysql) UpdateSQL

func (gomysql *GoMysql) UpdateSQL(data map[string]interface{}) (string, []interface{})

*

  • Get Insert Sql Query

func (*GoMysql) Where

func (gomysql *GoMysql) Where(key string, operator string, dataValue interface{}) *GoMysql

*

  • Add AND Where Conditions

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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