xormplus 9 роки тому
батько
коміт
c0e1f601a2
8 змінених файлів з 127 додано та 53 видалено
  1. 23 4
      engine.go
  2. 31 7
      logger.go
  3. 29 24
      lru_cacher.go
  4. 2 0
      processors.go
  5. 11 6
      session.go
  6. 4 4
      sqlite3_dialect.go
  7. 15 8
      statement.go
  8. 12 0
      syslogger.go

+ 23 - 4
engine.go

@@ -171,8 +171,10 @@ func (engine *Engine) quote(sql string) string {
 }
 
 // SqlType will be depracated, please use SQLType instead
+//
+// Deprecated: use SQLType instead
 func (engine *Engine) SqlType(c *core.Column) string {
-	return engine.dialect.SqlType(c)
+	return engine.SQLType(c)
 }
 
 // SQLType A simple wrapper to dialect's core.SqlType method
@@ -298,7 +300,10 @@ func (engine *Engine) logSQLExecutionTime(sqlStr string, args []interface{}, exe
 	return executionBlock()
 }
 
-// Sql will be depracated, please use SQL instead
+// Sql provides raw sql input parameter. When you have a complex SQL statement
+// and cannot use Where, Id, In and etc. Methods to describe, you can use SQL.
+//
+// Deprecated: use SQL instead.
 func (engine *Engine) Sql(querystring string, args ...interface{}) *Session {
 	return engine.SQL(querystring, args...)
 }
@@ -443,7 +448,7 @@ func (engine *Engine) dumpAll(w io.Writer, tp ...core.DbType) error {
 	} else {
 		dialect = core.QueryDialect(tp[0])
 		if dialect == nil {
-			return errors.New("Unsupported database type.")
+			return errors.New("Unsupported database type")
 		}
 		dialect.Init(nil, engine.dialect.URI(), "", "")
 	}
@@ -544,7 +549,7 @@ func (engine *Engine) dumpTables(tables []*core.Table, w io.Writer, tp ...core.D
 	} else {
 		dialect = core.QueryDialect(tp[0])
 		if dialect == nil {
-			return errors.New("Unsupported database type.")
+			return errors.New("Unsupported database type")
 		}
 		dialect.Init(nil, engine.dialect.URI(), "", "")
 	}
@@ -1217,12 +1222,26 @@ func (engine *Engine) IsTableExist(beanOrTableName interface{}) (bool, error) {
 }
 
 // IdOf get id from one struct
+//
+// Deprecated: use IDOf instead.
 func (engine *Engine) IdOf(bean interface{}) core.PK {
+	return engine.IDOf(bean)
+}
+
+// IDOf get id from one struct
+func (engine *Engine) IDOf(bean interface{}) core.PK {
 	return engine.IdOfV(reflect.ValueOf(bean))
 }
 
 // IdOfV get id from one value of struct
+//
+// Deprecated: use IDOfV instead.
 func (engine *Engine) IdOfV(rv reflect.Value) core.PK {
+	return engine.IDOfV(rv)
+}
+
+// IDOfV get id from one value of struct
+func (engine *Engine) IDOfV(rv reflect.Value) core.PK {
 	v := reflect.Indirect(rv)
 	table := engine.autoMapType(v)
 	pk := make([]interface{}, len(table.PrimaryKeys))

+ 31 - 7
logger.go

@@ -20,21 +20,45 @@ const (
 
 var _ core.ILogger = DiscardLogger{}
 
+// DiscardLogger don't log implementation for core.ILogger
 type DiscardLogger struct{}
 
-func (DiscardLogger) Debug(v ...interface{})                 {}
+// Debug empty implementation
+func (DiscardLogger) Debug(v ...interface{}) {}
+
+// Debugf empty implementation
 func (DiscardLogger) Debugf(format string, v ...interface{}) {}
-func (DiscardLogger) Error(v ...interface{})                 {}
+
+// Error empty implementation
+func (DiscardLogger) Error(v ...interface{}) {}
+
+// Errorf empty implementation
 func (DiscardLogger) Errorf(format string, v ...interface{}) {}
-func (DiscardLogger) Info(v ...interface{})                  {}
-func (DiscardLogger) Infof(format string, v ...interface{})  {}
-func (DiscardLogger) Warn(v ...interface{})                  {}
-func (DiscardLogger) Warnf(format string, v ...interface{})  {}
+
+// Info empty implementation
+func (DiscardLogger) Info(v ...interface{}) {}
+
+// Infof empty implementation
+func (DiscardLogger) Infof(format string, v ...interface{}) {}
+
+// Warn empty implementation
+func (DiscardLogger) Warn(v ...interface{}) {}
+
+// Warnf empty implementation
+func (DiscardLogger) Warnf(format string, v ...interface{}) {}
+
+// Level empty implementation
 func (DiscardLogger) Level() core.LogLevel {
 	return core.LOG_UNKNOWN
 }
+
+// SetLevel empty implementation
 func (DiscardLogger) SetLevel(l core.LogLevel) {}
-func (DiscardLogger) ShowSQL(show ...bool)     {}
+
+// ShowSQL empty implementation
+func (DiscardLogger) ShowSQL(show ...bool) {}
+
+// IsShowSQL empty implementation
 func (DiscardLogger) IsShowSQL() bool {
 	return false
 }

+ 29 - 24
lru_cacher.go

@@ -13,6 +13,7 @@ import (
 	"github.com/xormplus/core"
 )
 
+// LRUCacher implments cache object facilities
 type LRUCacher struct {
 	idList   *list.List
 	sqlList  *list.List
@@ -26,10 +27,12 @@ type LRUCacher struct {
 	GcInterval     time.Duration
 }
 
+// NewLRUCacher creates a cacher
 func NewLRUCacher(store core.CacheStore, maxElementSize int) *LRUCacher {
 	return NewLRUCacher2(store, 3600*time.Second, maxElementSize)
 }
 
+// NewLRUCacher2 creates a cache include different params
 func NewLRUCacher2(store core.CacheStore, expired time.Duration, maxElementSize int) *LRUCacher {
 	cacher := &LRUCacher{store: store, idList: list.New(),
 		sqlList: list.New(), Expired: expired,
@@ -41,10 +44,6 @@ func NewLRUCacher2(store core.CacheStore, expired time.Duration, maxElementSize
 	return cacher
 }
 
-//func NewLRUCacher3(store CacheStore, expired time.Duration, maxSize int) *LRUCacher {
-//    return newLRUCacher(store, expired, maxSize, 0)
-//}
-
 // RunGC run once every m.GcInterval
 func (m *LRUCacher) RunGC() {
 	time.AfterFunc(m.GcInterval, func() {
@@ -101,7 +100,7 @@ func (m *LRUCacher) GetIds(tableName, sql string) interface{} {
 	}
 	if v, err := m.store.Get(sql); err == nil {
 		if el, ok := m.sqlIndex[tableName][sql]; !ok {
-			el = m.sqlList.PushBack(newSqlNode(tableName, sql))
+			el = m.sqlList.PushBack(newSQLNode(tableName, sql))
 			m.sqlIndex[tableName][sql] = el
 		} else {
 			lastTime := el.Value.(*sqlNode).lastVisit
@@ -114,10 +113,10 @@ func (m *LRUCacher) GetIds(tableName, sql string) interface{} {
 			el.Value.(*sqlNode).lastVisit = time.Now()
 		}
 		return v
-	} else {
-		m.delIds(tableName, sql)
 	}
 
+	m.delIds(tableName, sql)
+
 	return nil
 }
 
@@ -128,7 +127,7 @@ func (m *LRUCacher) GetBean(tableName string, id string) interface{} {
 	if _, ok := m.idIndex[tableName]; !ok {
 		m.idIndex[tableName] = make(map[string]*list.Element)
 	}
-	tid := genId(tableName, id)
+	tid := genID(tableName, id)
 	if v, err := m.store.Get(tid); err == nil {
 		if el, ok := m.idIndex[tableName][id]; ok {
 			lastTime := el.Value.(*idNode).lastVisit
@@ -141,19 +140,19 @@ func (m *LRUCacher) GetBean(tableName string, id string) interface{} {
 			m.idList.MoveToBack(el)
 			el.Value.(*idNode).lastVisit = time.Now()
 		} else {
-			el = m.idList.PushBack(newIdNode(tableName, id))
+			el = m.idList.PushBack(newIDNode(tableName, id))
 			m.idIndex[tableName][id] = el
 		}
 		return v
-	} else {
-		// store bean is not exist, then remove memory's index
-		m.delBean(tableName, id)
-		//m.clearIds(tableName)
-		return nil
 	}
+
+	// store bean is not exist, then remove memory's index
+	m.delBean(tableName, id)
+	//m.clearIds(tableName)
+	return nil
 }
 
-// Clear all sql-ids mapping on table tableName from cache
+// clearIds clears all sql-ids mapping on table tableName from cache
 func (m *LRUCacher) clearIds(tableName string) {
 	if tis, ok := m.sqlIndex[tableName]; ok {
 		for sql, v := range tis {
@@ -164,6 +163,7 @@ func (m *LRUCacher) clearIds(tableName string) {
 	m.sqlIndex[tableName] = make(map[string]*list.Element)
 }
 
+// ClearIds clears all sql-ids mapping on table tableName from cache
 func (m *LRUCacher) ClearIds(tableName string) {
 	m.mutex.Lock()
 	defer m.mutex.Unlock()
@@ -174,19 +174,21 @@ func (m *LRUCacher) clearBeans(tableName string) {
 	if tis, ok := m.idIndex[tableName]; ok {
 		for id, v := range tis {
 			m.idList.Remove(v)
-			tid := genId(tableName, id)
+			tid := genID(tableName, id)
 			m.store.Del(tid)
 		}
 	}
 	m.idIndex[tableName] = make(map[string]*list.Element)
 }
 
+// ClearBeans clears all beans in some table
 func (m *LRUCacher) ClearBeans(tableName string) {
 	m.mutex.Lock()
 	defer m.mutex.Unlock()
 	m.clearBeans(tableName)
 }
 
+// PutIds pus ids into table
 func (m *LRUCacher) PutIds(tableName, sql string, ids interface{}) {
 	m.mutex.Lock()
 	defer m.mutex.Unlock()
@@ -194,7 +196,7 @@ func (m *LRUCacher) PutIds(tableName, sql string, ids interface{}) {
 		m.sqlIndex[tableName] = make(map[string]*list.Element)
 	}
 	if el, ok := m.sqlIndex[tableName][sql]; !ok {
-		el = m.sqlList.PushBack(newSqlNode(tableName, sql))
+		el = m.sqlList.PushBack(newSQLNode(tableName, sql))
 		m.sqlIndex[tableName][sql] = el
 	} else {
 		el.Value.(*sqlNode).lastVisit = time.Now()
@@ -207,6 +209,7 @@ func (m *LRUCacher) PutIds(tableName, sql string, ids interface{}) {
 	}
 }
 
+// PutBean puts beans into table
 func (m *LRUCacher) PutBean(tableName string, id string, obj interface{}) {
 	m.mutex.Lock()
 	defer m.mutex.Unlock()
@@ -214,13 +217,13 @@ func (m *LRUCacher) PutBean(tableName string, id string, obj interface{}) {
 	var ok bool
 
 	if el, ok = m.idIndex[tableName][id]; !ok {
-		el = m.idList.PushBack(newIdNode(tableName, id))
+		el = m.idList.PushBack(newIDNode(tableName, id))
 		m.idIndex[tableName][id] = el
 	} else {
 		el.Value.(*idNode).lastVisit = time.Now()
 	}
 
-	m.store.Put(genId(tableName, id), obj)
+	m.store.Put(genID(tableName, id), obj)
 	if m.idList.Len() > m.MaxElementSize {
 		e := m.idList.Front()
 		node := e.Value.(*idNode)
@@ -238,6 +241,7 @@ func (m *LRUCacher) delIds(tableName, sql string) {
 	m.store.Del(sql)
 }
 
+// DelIds deletes ids
 func (m *LRUCacher) DelIds(tableName, sql string) {
 	m.mutex.Lock()
 	defer m.mutex.Unlock()
@@ -245,7 +249,7 @@ func (m *LRUCacher) DelIds(tableName, sql string) {
 }
 
 func (m *LRUCacher) delBean(tableName string, id string) {
-	tid := genId(tableName, id)
+	tid := genID(tableName, id)
 	if el, ok := m.idIndex[tableName][id]; ok {
 		delete(m.idIndex[tableName], id)
 		m.idList.Remove(el)
@@ -254,6 +258,7 @@ func (m *LRUCacher) delBean(tableName string, id string) {
 	m.store.Del(tid)
 }
 
+// DelBean deletes beans in some table
 func (m *LRUCacher) DelBean(tableName string, id string) {
 	m.mutex.Lock()
 	defer m.mutex.Unlock()
@@ -272,18 +277,18 @@ type sqlNode struct {
 	lastVisit time.Time
 }
 
-func genSqlKey(sql string, args interface{}) string {
+func genSQLKey(sql string, args interface{}) string {
 	return fmt.Sprintf("%v-%v", sql, args)
 }
 
-func genId(prefix string, id string) string {
+func genID(prefix string, id string) string {
 	return fmt.Sprintf("%v-%v", prefix, id)
 }
 
-func newIdNode(tbName string, id string) *idNode {
+func newIDNode(tbName string, id string) *idNode {
 	return &idNode{tbName, id, time.Now()}
 }
 
-func newSqlNode(tbName, sql string) *sqlNode {
+func newSQLNode(tbName, sql string) *sqlNode {
 	return &sqlNode{tbName, sql, time.Now()}
 }

+ 2 - 0
processors.go

@@ -19,10 +19,12 @@ type BeforeDeleteProcessor interface {
 	BeforeDelete()
 }
 
+// BeforeSetProcessor executed before data set to the struct fields
 type BeforeSetProcessor interface {
 	BeforeSet(string, Cell)
 }
 
+// AfterSetProcessor executed after data set to the struct fields
 type AfterSetProcessor interface {
 	AfterSet(string, Cell)
 }

+ 11 - 6
session.go

@@ -119,7 +119,10 @@ func (session *Session) Prepare() *Session {
 	return session
 }
 
-// Sql !DEPRECIATED! will be deprecated, please use SQL instead.
+// Sql provides raw sql input parameter. When you have a complex SQL statement
+// and cannot use Where, Id, In and etc. Methods to describe, you can use SQL.
+//
+// Deprecated: use SQL instead.
 func (session *Session) Sql(query string, args ...interface{}) *Session {
 	return session.SQL(query, args...)
 }
@@ -150,15 +153,16 @@ func (session *Session) Or(query interface{}, args ...interface{}) *Session {
 	return session
 }
 
-// Id will be deprecated, please use ID instead
+// Id provides converting id as a query condition
+//
+// Deprecated: use ID instead
 func (session *Session) Id(id interface{}) *Session {
-	session.Statement.Id(id)
-	return session
+	return session.ID(id)
 }
 
 // ID provides converting id as a query condition
 func (session *Session) ID(id interface{}) *Session {
-	session.Statement.Id(id)
+	session.Statement.ID(id)
 	return session
 }
 
@@ -1869,6 +1873,7 @@ func (session *Session) _row2Bean(rows *core.Rows, fields []string, fieldsCount
 						hasAssigned = true
 
 						t := vv.Convert(core.TimeType).Interface().(time.Time)
+
 						z, _ := t.Zone()
 						dbTZ := session.Engine.DatabaseTZ
 						if dbTZ == nil {
@@ -3636,7 +3641,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
 		colNames = append(colNames, session.Engine.Quote(v.colName)+" = "+v.expr)
 	}
 
-	session.Statement.processIdParam()
+	session.Statement.processIDParam()
 
 	var autoCond builder.Cond
 	if !session.Statement.noAutoCondition && len(condiBean) > 0 {

+ 4 - 4
sqlite3_dialect.go

@@ -386,16 +386,16 @@ func (db *sqlite3) GetIndexes(tableName string) (map[string]*core.Index, error)
 
 	indexes := make(map[string]*core.Index, 0)
 	for rows.Next() {
-		var tmpSql sql.NullString
-		err = rows.Scan(&tmpSql)
+		var tmpSQL sql.NullString
+		err = rows.Scan(&tmpSQL)
 		if err != nil {
 			return nil, err
 		}
 
-		if !tmpSql.Valid {
+		if !tmpSQL.Valid {
 			continue
 		}
-		sql := tmpSql.String
+		sql := tmpSQL.String
 
 		index := new(core.Index)
 		nNStart := strings.Index(sql, "INDEX")

+ 15 - 8
statement.go

@@ -707,7 +707,14 @@ func (statement *Statement) TableName() string {
 }
 
 // Id generate "where id = ? " statment or for composite key "where key1 = ? and key2 = ?"
+//
+// Deprecated: use ID instead
 func (statement *Statement) Id(id interface{}) *Statement {
+	return statement.ID(id)
+}
+
+// ID generate "where id = ? " statment or for composite key "where key1 = ? and key2 = ?"
+func (statement *Statement) ID(id interface{}) *Statement {
 	idValue := reflect.ValueOf(id)
 	idType := reflect.TypeOf(idValue.Interface())
 
@@ -1106,7 +1113,7 @@ func (statement *Statement) genConds(bean interface{}) (string, []interface{}, e
 		statement.cond = statement.cond.And(autoCond)
 	}
 
-	statement.processIdParam()
+	statement.processIDParam()
 
 	return builder.ToSQL(statement.cond)
 }
@@ -1148,15 +1155,15 @@ func (statement *Statement) genCountSQL(bean interface{}) (string, []interface{}
 
 	condSQL, condArgs, _ := statement.genConds(bean)
 
-	var selectSql = statement.selectStr
-	if len(selectSql) <= 0 {
+	var selectSQL = statement.selectStr
+	if len(selectSQL) <= 0 {
 		if statement.IsDistinct {
-			selectSql = fmt.Sprintf("count(DISTINCT %s)", statement.ColumnStr)
+			selectSQL = fmt.Sprintf("count(DISTINCT %s)", statement.ColumnStr)
 		} else {
-			selectSql = "count(*)"
+			selectSQL = "count(*)"
 		}
 	}
-	return statement.genSelectSQL(selectSql, condSQL), append(statement.joinArgs, condArgs...)
+	return statement.genSelectSQL(selectSQL, condSQL), append(statement.joinArgs, condArgs...)
 }
 
 func (statement *Statement) genSumSQL(bean interface{}, columns ...string) (string, []interface{}) {
@@ -1183,7 +1190,7 @@ func (statement *Statement) genSelectSQL(columnStr, condSQL string) (a string) {
 	var top string
 	var mssqlCondi string
 
-	statement.processIdParam()
+	statement.processIDParam()
 
 	var buf bytes.Buffer
 	if len(condSQL) > 0 {
@@ -1280,7 +1287,7 @@ func (statement *Statement) genSelectSQL(columnStr, condSQL string) (a string) {
 	return
 }
 
-func (statement *Statement) processIdParam() {
+func (statement *Statement) processIDParam() {
 	if statement.IdParam == nil {
 		return
 	}

+ 12 - 0
syslogger.go

@@ -21,42 +21,52 @@ type SyslogLogger struct {
 	showSQL bool
 }
 
+// NewSyslogLogger implements core.ILogger
 func NewSyslogLogger(w *syslog.Writer) *SyslogLogger {
 	return &SyslogLogger{w: w}
 }
 
+// Debug log content as Debug
 func (s *SyslogLogger) Debug(v ...interface{}) {
 	s.w.Debug(fmt.Sprint(v...))
 }
 
+// Debugf log content as Debug and format
 func (s *SyslogLogger) Debugf(format string, v ...interface{}) {
 	s.w.Debug(fmt.Sprintf(format, v...))
 }
 
+// Error log content as Error
 func (s *SyslogLogger) Error(v ...interface{}) {
 	s.w.Err(fmt.Sprint(v...))
 }
 
+// Errorf log content as Errorf and format
 func (s *SyslogLogger) Errorf(format string, v ...interface{}) {
 	s.w.Err(fmt.Sprintf(format, v...))
 }
 
+// Info log content as Info
 func (s *SyslogLogger) Info(v ...interface{}) {
 	s.w.Info(fmt.Sprint(v...))
 }
 
+// Infof log content as Infof and format
 func (s *SyslogLogger) Infof(format string, v ...interface{}) {
 	s.w.Info(fmt.Sprintf(format, v...))
 }
 
+// Warn log content as Warn
 func (s *SyslogLogger) Warn(v ...interface{}) {
 	s.w.Warning(fmt.Sprint(v...))
 }
 
+// Warnf log content as Warnf and format
 func (s *SyslogLogger) Warnf(format string, v ...interface{}) {
 	s.w.Warning(fmt.Sprintf(format, v...))
 }
 
+// Level shows log level
 func (s *SyslogLogger) Level() core.LogLevel {
 	return core.LOG_UNKNOWN
 }
@@ -64,6 +74,7 @@ func (s *SyslogLogger) Level() core.LogLevel {
 // SetLevel always return error, as current log/syslog package doesn't allow to set priority level after syslog.Writer created
 func (s *SyslogLogger) SetLevel(l core.LogLevel) {}
 
+// ShowSQL set if logging SQL
 func (s *SyslogLogger) ShowSQL(show ...bool) {
 	if len(show) == 0 {
 		s.showSQL = true
@@ -72,6 +83,7 @@ func (s *SyslogLogger) ShowSQL(show ...bool) {
 	s.showSQL = show[0]
 }
 
+// IsShowSQL if logging SQL
 func (s *SyslogLogger) IsShowSQL() bool {
 	return s.showSQL
 }