improved docs
This commit is contained in:
parent
1b47321704
commit
564f3b71fa
21
engine.go
21
engine.go
|
@ -101,8 +101,8 @@ func (engine *Engine) MapCacher(bean interface{}, cacher Cacher) {
|
|||
engine.Tables[t].Cacher = cacher
|
||||
}
|
||||
|
||||
func (e *Engine) OpenDB() (*sql.DB, error) {
|
||||
return sql.Open(e.DriverName, e.DataSourceName)
|
||||
func (engine *Engine) OpenDB() (*sql.DB, error) {
|
||||
return sql.Open(engine.DriverName, engine.DataSourceName)
|
||||
}
|
||||
|
||||
func (engine *Engine) NewSession() *Session {
|
||||
|
@ -115,6 +115,7 @@ func (engine *Engine) Close() error {
|
|||
return engine.Pool.Close(engine)
|
||||
}
|
||||
|
||||
// Test if databse is alive.
|
||||
func (engine *Engine) Test() error {
|
||||
session := engine.NewSession()
|
||||
defer session.Close()
|
||||
|
@ -249,7 +250,7 @@ func (engine *Engine) Having(conditions string) *Session {
|
|||
return session.Having(conditions)
|
||||
}
|
||||
|
||||
// some lock needed
|
||||
//
|
||||
func (engine *Engine) AutoMapType(t reflect.Type) *Table {
|
||||
engine.mutex.Lock()
|
||||
defer engine.mutex.Unlock()
|
||||
|
@ -448,7 +449,7 @@ func (engine *Engine) MapType(t reflect.Type) *Table {
|
|||
return table
|
||||
}
|
||||
|
||||
// Map should use after all operation because it's not thread safe
|
||||
// Map should use before all operation because it's not thread safe
|
||||
func (engine *Engine) Map(beans ...interface{}) (e error) {
|
||||
engine.mutex.Lock()
|
||||
defer engine.mutex.Unlock()
|
||||
|
@ -459,7 +460,7 @@ func (engine *Engine) Map(beans ...interface{}) (e error) {
|
|||
return
|
||||
}
|
||||
|
||||
// is a table has
|
||||
// Is a table has any reocrd
|
||||
func (engine *Engine) IsEmptyTable(bean interface{}) (bool, error) {
|
||||
t := rType(bean)
|
||||
if t.Kind() != reflect.Struct {
|
||||
|
@ -472,6 +473,7 @@ func (engine *Engine) IsEmptyTable(bean interface{}) (bool, error) {
|
|||
return !has, err
|
||||
}
|
||||
|
||||
// Is a table is exist
|
||||
func (engine *Engine) IsTableExist(bean interface{}) (bool, error) {
|
||||
t := rType(bean)
|
||||
if t.Kind() != reflect.Struct {
|
||||
|
@ -484,6 +486,7 @@ func (engine *Engine) IsTableExist(bean interface{}) (bool, error) {
|
|||
return has, err
|
||||
}
|
||||
|
||||
// If enabled cache, clear the cache bean
|
||||
func (engine *Engine) ClearCacheBean(bean interface{}, id int64) error {
|
||||
t := rType(bean)
|
||||
if t.Kind() != reflect.Struct {
|
||||
|
@ -497,6 +500,7 @@ func (engine *Engine) ClearCacheBean(bean interface{}, id int64) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// If enabled cache, clear some tables' cache
|
||||
func (engine *Engine) ClearCache(beans ...interface{}) error {
|
||||
for _, bean := range beans {
|
||||
t := rType(bean)
|
||||
|
@ -512,8 +516,9 @@ func (engine *Engine) ClearCache(beans ...interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// sync the new struct to database, this method will auto add column, index, unique
|
||||
// but will not delete or change anything.
|
||||
// Sync the new struct change to database, this method will automatically add
|
||||
// table, column, index, unique. but will not delete or change anything.
|
||||
// If you change some field, you should change the database manually.
|
||||
func (engine *Engine) Sync(beans ...interface{}) error {
|
||||
for _, bean := range beans {
|
||||
table := engine.AutoMap(bean)
|
||||
|
@ -618,6 +623,7 @@ func (engine *Engine) UnMap(beans ...interface{}) (e error) {
|
|||
return
|
||||
}
|
||||
|
||||
// Drop all mapped table
|
||||
func (e *Engine) DropAll() error {
|
||||
session := e.NewSession()
|
||||
defer session.Close()
|
||||
|
@ -634,6 +640,7 @@ func (e *Engine) DropAll() error {
|
|||
return session.Commit()
|
||||
}
|
||||
|
||||
// CreateTables create tabls according bean
|
||||
func (e *Engine) CreateTables(beans ...interface{}) error {
|
||||
session := e.NewSession()
|
||||
err := session.Begin()
|
||||
|
|
2
pool.go
2
pool.go
|
@ -110,7 +110,7 @@ type node struct {
|
|||
cond *sync.Cond
|
||||
}
|
||||
|
||||
func NewNode() *node {
|
||||
func newCondNode() *node {
|
||||
n := &node{}
|
||||
n.cond = sync.NewCond(&n.mutex)
|
||||
return n
|
||||
|
|
10
postgres.go
10
postgres.go
|
@ -11,13 +11,13 @@ type postgres struct {
|
|||
dbname string
|
||||
}
|
||||
|
||||
type Values map[string]string
|
||||
type values map[string]string
|
||||
|
||||
func (vs Values) Set(k, v string) {
|
||||
func (vs values) Set(k, v string) {
|
||||
vs[k] = v
|
||||
}
|
||||
|
||||
func (vs Values) Get(k string) (v string) {
|
||||
func (vs values) Get(k string) (v string) {
|
||||
return vs[k]
|
||||
}
|
||||
|
||||
|
@ -27,7 +27,7 @@ func errorf(s string, args ...interface{}) {
|
|||
panic(Error(fmt.Errorf("pq: %s", fmt.Sprintf(s, args...))))
|
||||
}
|
||||
|
||||
func parseOpts(name string, o Values) {
|
||||
func parseOpts(name string, o values) {
|
||||
if len(name) == 0 {
|
||||
return
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ func parseOpts(name string, o Values) {
|
|||
}
|
||||
|
||||
func (db *postgres) Init(uri string) error {
|
||||
o := make(Values)
|
||||
o := make(values)
|
||||
parseOpts(uri, o)
|
||||
|
||||
db.dbname = o.Get("dbname")
|
||||
|
|
|
@ -1355,7 +1355,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
|
|||
table := session.Engine.AutoMap(bean)
|
||||
session.Statement.RefTable = table
|
||||
|
||||
colNames, args, err := table.GenCols(session, bean, false, false)
|
||||
colNames, args, err := table.genCols(session, bean, false, false)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
@ -1580,7 +1580,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
|
|||
if session.Statement.ColumnStr == "" {
|
||||
colNames, args = buildConditions(session.Engine, table, bean)
|
||||
} else {
|
||||
colNames, args, err = table.GenCols(session, bean, true, true)
|
||||
colNames, args, err = table.genCols(session, bean, true, true)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
|
2
table.go
2
table.go
|
@ -253,7 +253,7 @@ func (table *Table) AddColumn(col *Column) {
|
|||
table.Columns[col.Name] = col
|
||||
}
|
||||
|
||||
func (table *Table) GenCols(session *Session, bean interface{}, useCol bool, includeQuote bool) ([]string, []interface{}, error) {
|
||||
func (table *Table) genCols(session *Session, bean interface{}, useCol bool, includeQuote bool) ([]string, []interface{}, error) {
|
||||
colNames := make([]string, 0)
|
||||
args := make([]interface{}, 0)
|
||||
|
||||
|
|
Loading…
Reference in New Issue