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