-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
159 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,79 @@ | ||
# database | ||
YohGo database | ||
=== | ||
|
||
YohGo database manages the database connection of an application so that the database connection is wrapped in a configurable container that can be utilised in various packages that require a connection to a database. | ||
|
||
--------------------------------------- | ||
|
||
* [Requirements](#requirements) | ||
* [Features](#features) | ||
* [Installation](#installation) | ||
* [Usage](#usage) | ||
* [Password Grant](#get-an-access-token-using-the-password-grant-type) | ||
* [Refresh Token Grant](#refresh-the-token-using-the-refresh-token-grant-type) | ||
|
||
--------------------------------------- | ||
|
||
## Requirements | ||
* Go (1.5+) | ||
* MySQL (4.1+) | ||
|
||
--------------------------------------- | ||
|
||
## Features | ||
|
||
* Lightweight and fast | ||
* Native Go implementation | ||
* Currently only supports `mysql` and `gorm mysql` | ||
|
||
--------------------------------------- | ||
|
||
## Installation | ||
|
||
Simply install the package to your [$GOPATH](https://github.com/golang/go/wiki/GOPATH "GOPATH") with the [go tool](https://golang.org/cmd/go/ "go command") from shell: | ||
|
||
```bash | ||
$ go get github.com/yohgo/database | ||
``` | ||
|
||
Make sure [Git is installed](https://git-scm.com/downloads) on your machine and in your system's `PATH`. | ||
|
||
--------------------------------------- | ||
|
||
## Usage | ||
|
||
_YohGo Database_ is a Go database container that. You only need to import the YohGo database package of choice and can use the full [`database/sql`](https://golang.org/pkg/database/sql/) API then. | ||
|
||
Example of using the `gorm` package: | ||
```go | ||
|
||
import ( | ||
"github.com/yohgo/database/gorm" | ||
) | ||
|
||
// Configure database connection | ||
database, err := gorm.NewDatabase("DB_USER", "DB_PASS", "DB_HOST", "DB_PORT", "DB_NAME") | ||
// Check if connection failed | ||
if err != nil { | ||
log.Fatal("Failed to connect to database") | ||
} | ||
// Always ensure to close the connection | ||
defer database.Close() | ||
``` | ||
|
||
Example of using the `mysql` package: | ||
```go | ||
|
||
import ( | ||
"github.com/yohgo/database/mysql" | ||
) | ||
|
||
// Configure database connection | ||
database, err := mysql.NewDatabase("DB_USER", "DB_PASS", "DB_HOST", "DB_PORT", "DB_NAME") | ||
// Check if connection failed | ||
if err != nil { | ||
log.Fatal("Failed to connect to database") | ||
} | ||
// Always ensure to close the connection | ||
defer database.Close() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package database | ||
|
||
// Database is an interface of a configured application database. | ||
type Database interface { | ||
NewDatabase(username, password, host, port, dbName string) (*Database, error) | ||
Close() error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package gorm | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/jinzhu/gorm" | ||
// MySQL gorm dialect | ||
_ "github.com/jinzhu/gorm/dialects/mysql" | ||
) | ||
|
||
// Database is a container of a configured gorm database. | ||
type Database struct { | ||
DB *gorm.DB | ||
} | ||
|
||
// NewDatabase configures a new gorm database. | ||
func NewDatabase(username, password, host, port, dbName string) (*Database, error) { | ||
var err error | ||
var database Database | ||
// Open database connection | ||
database.DB, err = gorm.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", username, password, host, port, dbName)) | ||
|
||
// Check for database open errors | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &database, nil | ||
} | ||
|
||
// Close closes the current connection to a gorm database. | ||
func (factory *Database) Close() error { | ||
return factory.DB.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package mysql | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
|
||
// MySQL go sql driver | ||
_ "github.com/go-sql-driver/mysql" | ||
) | ||
|
||
// Database is a container of a configured mysql database. | ||
type Database struct { | ||
DB *sql.DB | ||
} | ||
|
||
// NewDatabase configures a new mysql database. | ||
func NewDatabase(username, password, host, port, dbName string) (*Database, error) { | ||
var err error | ||
var database Database | ||
// Open database connection | ||
database.DB, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", username, password, host, port, dbName)) | ||
|
||
// Check for database open errors | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Check to verify database connection | ||
if err = database.DB.Ping(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &database, nil | ||
} | ||
|
||
// Close closes the current connection to a mysql database. | ||
func (factory *Database) Close() error { | ||
return factory.DB.Close() | ||
} |