Skip to content

Commit

Permalink
Merge branch 'release-2.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Chad Petersen authored and Chad Petersen committed Sep 8, 2017
2 parents d88aaa7 + 727fe3d commit 82e370a
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 26 deletions.
80 changes: 79 additions & 1 deletion README.md
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()
```
7 changes: 7 additions & 0 deletions database.go
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
}
34 changes: 34 additions & 0 deletions gorm/database.go
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()
}
25 changes: 0 additions & 25 deletions mysql.go

This file was deleted.

39 changes: 39 additions & 0 deletions mysql/database.go
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()
}

0 comments on commit 82e370a

Please sign in to comment.