Golang(7)Database Layer

Golang(7)Database Layer

5.1 database/sql Interface
sql.Register
Register(name string, driver driver.Driver) register the driver you add to the golang system.

https://github.com/mattn/go-sqlite3

func init() {
     sql.Register(“sqlite3”, &SQLiteDriver{})
}

https://github.com/mikespook/mymysql

var d = Driver{proto: “tcp”, raddr: “127.0.0.1:3306” }
func init() {
     Register(“SET NAMES utf8”)
     sql.Register(“mymysql”, &d)
}

For example
import (
     “database/sql”
     _ “github.com/mattn/go-sqlite3"
)

If we import this package, it will automatically call the init method in that package.

driver.Driver
type Driver interface {
     Open(name string) (Conn, error)
}
The name parameter will be used to retrieve the database connection info.
We can use this Conn to do one go routine execution, but you can not use it twice.

driver.Conn
type Conn interface {
     Prepare(query string) (Stmt, error)
     Close() error
     Begin() (Tx, error)
}

Tx is short for transaction.

driver.Stmt
type Stmt interface {
     Close() error
     NumInput() int
     Exec(args []Value) (Result, error)
     Query(args []Value) (Rows, error)
}

driver.Tx
type Tx interface {
     Commit() error
     Rollback() error
}

driver.Execer
type Execer interface {
     Exec(query string, args []Value) (Result, error)
}

driver.Result
The response and return value of Update/Insert
type Result interface {
     LastInsertId() (int64, error)
     RowsAffected() (int64, error)
}

driver.Rows
Return value of Query

type Rows interface {
     Columns() []string
     Close() error
     Next(dest []Value) error
}

Columns is the Column information from Table

driver.RowsAffected
type RowsAffected int64

driver.Value
type Value interface{}

It can hold all the different types of values. For example: int64, float64, bool, []byte, string, time.Time

driver.ValueConverter
driver.Valuer

database/sql
type DB struct {
     driver driver.Driver
     dsn     string
     mu      sync.Mutex
     freeConn     []driver.Conn
     closed     bool
}

5.2 Using MYSQL DB
https://github.com/go-sql-driver/mysql

5.3 Using Sqlite DB
https://github.com/mattn/go-sqlite3

Sqlite3 administrator tool
http://sqliteadmin.orbmu2k.de/

5.4 Using PostgreSQL DB
https://github.com/lib/pq

5.5 BeeDB

5.6 NoSQL (Not Only SQL)
redis, mongoDB, Cassandra, Membase
…todo…

References:
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/05.0.md

Redis 1~ 6
http://sillycat.iteye.com/blog/1549504
http://sillycat.iteye.com/blog/1553507
http://sillycat.iteye.com/blog/1553508
http://sillycat.iteye.com/blog/1553509
http://sillycat.iteye.com/blog/2028180
http://sillycat.iteye.com/blog/2033094

MongoDB 1 ~ 5
http://sillycat.iteye.com/blog/1547291
http://sillycat.iteye.com/blog/1547292
http://sillycat.iteye.com/blog/1547294
http://sillycat.iteye.com/blog/1965857
http://sillycat.iteye.com/blog/1965880

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值