Skip to content

优化resultSet.len判断,mysql单元测试修改 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,12 @@ func NewResultSet(rawRows *[]map[string][]byte) (rs *ResultSet) {
}

func (rs *ResultSet) Len() int {
return len(*rs.rawRows)
vo := reflect.ValueOf(rs.rawRows)
if vo.IsNil() {
return 0
} else {
return len(*rs.rawRows)
}
}
func (rs *ResultSet) MapRows(keyColumn string) (rowsMap map[string]map[string]string) {
rowsMap = map[string]map[string]string{}
Expand Down
50 changes: 19 additions & 31 deletions mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,11 @@ type User struct {
Height float32 `column:"height"`
Sex bool `column:"sex"`
CreateTime time.Time `column:"create_time"`
Foo string `column:"foo"`
Pid string `column:"pid"`
}

var rawRows = []map[string]interface{}{
map[string]interface{}{
var rawRows = []map[string][]byte{
map[string][]byte{
"name": []byte("jack"),
"id": []byte("229"),
"weight": []byte("60"),
Expand All @@ -218,7 +218,7 @@ var rawRows = []map[string]interface{}{
"create_time": []byte("2017-10-10 09:00:09"),
"pid": []byte("1"),
},
map[string]interface{}{
map[string][]byte{
"name": []byte("jack"),
"id": []byte("229"),
"weight": []byte("60"),
Expand All @@ -230,8 +230,7 @@ var rawRows = []map[string]interface{}{
}

func TestStruct(t *testing.T) {
rs := ResultSet{}
rs.Init(&rawRows)
rs := NewResultSet(&rawRows)
s, err := rs.Struct(User{})
if err != nil {
t.Errorf("\n==> Except : \nnil\n==> Got : \n%s", err)
Expand All @@ -240,29 +239,25 @@ func TestStruct(t *testing.T) {
t.Errorf("\n==> Except : \njack\n==> Got : \n%s", s.(User).Name)
}
if s.(User).ID != 229 {
t.Errorf("\n==> Except : \n229\n==> Got : \n%s", s.(User).ID)
t.Errorf("\n==> Except : \n229\n==> Got : \n%d", s.(User).ID)
}
if s.(User).Weight != 60 {
t.Errorf("\n==> Except : \njack\n==> Got : \n%s", s.(User).Weight)
t.Errorf("\n==> Except : \njack\n==> Got : \n%d", s.(User).Weight)
}
if s.(User).Height != 160.3 {
t.Errorf("\n==> Except : \njack\n==> Got : \n%s", s.(User).Height)
t.Errorf("\n==> Except : \njack\n==> Got : \n%f", s.(User).Height)
}
if s.(User).Sex != true {
t.Errorf("\n==> Except : \ntrue\n==> Got : \n%s", s.(User).Sex)
t.Errorf("\n==> Except : \ntrue\n==> Got : \n%v", s.(User).Sex)
}
if s.(User).CreateTime.String() != "2017-10-10 09:00:09 +0800 CST" {
t.Errorf("\n==> Except : \n\"2017-10-10 09:00:09 +0800 CST\"\n==> Got : \n%s", s.(User).CreateTime)
}
if s.(User).Sex != true {
t.Errorf("\n==> Except : \ntrue\n==> Got : \n%s", s.(User).Sex)
}
}

}
func TestStructs(t *testing.T) {
rs := ResultSet{}
rs.Init(&rawRows)
rs := NewResultSet(&rawRows)
sts, err := rs.Structs(User{})
if err != nil {
t.Errorf("\n==> Except : \nnil\n==> Got : \n%s", err)
Expand All @@ -272,29 +267,25 @@ func TestStructs(t *testing.T) {
t.Errorf("\n==> Except : \njack\n==> Got : \n%s", s.(User).Name)
}
if s.(User).ID != 229 {
t.Errorf("\n==> Except : \n229\n==> Got : \n%s", s.(User).ID)
t.Errorf("\n==> Except : \n229\n==> Got : \n%d", s.(User).ID)
}
if s.(User).Weight != 60 {
t.Errorf("\n==> Except : \njack\n==> Got : \n%s", s.(User).Weight)
t.Errorf("\n==> Except : \njack\n==> Got : \n%d", s.(User).Weight)
}
if s.(User).Height != 160.3 {
t.Errorf("\n==> Except : \njack\n==> Got : \n%s", s.(User).Height)
t.Errorf("\n==> Except : \njack\n==> Got : \n%f", s.(User).Height)
}
if s.(User).Sex != true {
t.Errorf("\n==> Except : \ntrue\n==> Got : \n%s", s.(User).Sex)
t.Errorf("\n==> Except : \ntrue\n==> Got : \n%v", s.(User).Sex)
}
if s.(User).CreateTime.String() != "2017-10-10 09:00:09 +0800 CST" {
t.Errorf("\n==> Except : \n\"2017-10-10 09:00:09 +0800 CST\"\n==> Got : \n%s", s.(User).CreateTime)
}
if s.(User).Sex != true {
t.Errorf("\n==> Except : \ntrue\n==> Got : \n%s", s.(User).Sex)
}
}
}
}
func TestMapStructs(t *testing.T) {
rs := ResultSet{}
rs.Init(&rawRows)
rs := NewResultSet(&rawRows)
sts, err := rs.MapStructs("pid", User{})
if err != nil {
t.Errorf("\n==> Except : \nnil\n==> Got : \n%s", err)
Expand All @@ -304,23 +295,20 @@ func TestMapStructs(t *testing.T) {
t.Errorf("\n==> Except : \njack\n==> Got : \n%s", s.(User).Name)
}
if s.(User).ID != 229 {
t.Errorf("\n==> Except : \n229\n==> Got : \n%s", s.(User).ID)
t.Errorf("\n==> Except : \n229\n==> Got : \n%d", s.(User).ID)
}
if s.(User).Weight != 60 {
t.Errorf("\n==> Except : \njack\n==> Got : \n%s", s.(User).Weight)
t.Errorf("\n==> Except : \njack\n==> Got : \n%d", s.(User).Weight)
}
if s.(User).Height != 160.3 {
t.Errorf("\n==> Except : \njack\n==> Got : \n%s", s.(User).Height)
t.Errorf("\n==> Except : \njack\n==> Got : \n%f", s.(User).Height)
}
if s.(User).Sex != true {
t.Errorf("\n==> Except : \ntrue\n==> Got : \n%s", s.(User).Sex)
t.Errorf("\n==> Except : \ntrue\n==> Got : \n%v", s.(User).Sex)
}
if s.(User).CreateTime.String() != "2017-10-10 09:00:09 +0800 CST" {
t.Errorf("\n==> Except : \n\"2017-10-10 09:00:09 +0800 CST\"\n==> Got : \n%s", s.(User).CreateTime)
}
if s.(User).Sex != true {
t.Errorf("\n==> Except : \ntrue\n==> Got : \n%s", s.(User).Sex)
}
}
}
}
Expand Down
55 changes: 26 additions & 29 deletions sqlite3/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ func (db *DB) getDB() (connPool *sql.DB, err error) {
if err != nil {
return
}
connPool.SetMaxIdleConns(0)
connPool.SetMaxOpenConns(0)
connPool.SetMaxIdleConns(db.Config.MaxIdleConns)
connPool.SetMaxOpenConns(db.Config.MaxOpenConns)
//err = connPool.Ping()
return
}
Expand All @@ -121,12 +121,10 @@ func (db *DB) Begin() (tx *sql.Tx, err error) {
return db.ConnPool.Begin()
}
func (db *DB) ExecTx(ar *ActiveRecord, tx *sql.Tx) (rs *ResultSet, err error) {
return db.execSQLTx(ar.SQL(), len(ar.arInsertBatch), tx, ar.values...)
return db.ExecSQLTx(ar.SQL(), tx, ar.values...)
}
func (db *DB) ExecSQLTx(tx *sql.Tx, sqlStr string, values ...interface{}) (rs *ResultSet, err error) {
return db.execSQLTx(sqlStr, 0, tx, values...)
}
func (db *DB) execSQLTx(sqlStr string, arInsertBatchCnt int, tx *sql.Tx, values ...interface{}) (rs *ResultSet, err error) {
func (db *DB) ExecSQLTx(sqlStr string, tx *sql.Tx, values ...interface{}) (rs *ResultSet, err error) {
sqlStr = strings.Replace(sqlStr, db.Config.TablePrefixSqlIdentifier, db.Config.TablePrefix, -1)
var stmt *sql.Stmt
var result sql.Result
rs = new(ResultSet)
Expand All @@ -144,20 +142,13 @@ func (db *DB) execSQLTx(sqlStr string, arInsertBatchCnt int, tx *sql.Tx, values
if err != nil {
return
}
l := int64(arInsertBatchCnt)
if l > 1 {
rs.LastInsertId = rs.LastInsertId - +1
rs.RowsAffected = l
}
return
}
func (db *DB) Exec(ar *ActiveRecord) (rs *ResultSet, err error) {
return db.execSQL(ar.SQL(), len(ar.arInsertBatch), ar.values...)
return db.ExecSQL(ar.SQL(), ar.values...)
}
func (db *DB) ExecSQL(sqlStr string, values ...interface{}) (rs *ResultSet, err error) {
return db.execSQL(sqlStr, 0, values...)
}
func (db *DB) execSQL(sqlStr string, arInsertBatchCnt int, values ...interface{}) (rs *ResultSet, err error) {
sqlStr = strings.Replace(sqlStr, db.Config.TablePrefixSqlIdentifier, db.Config.TablePrefix, -1)
var stmt *sql.Stmt
var result sql.Result
rs = new(ResultSet)
Expand All @@ -175,13 +166,9 @@ func (db *DB) execSQL(sqlStr string, arInsertBatchCnt int, values ...interface{}
if err != nil {
return
}
l := int64(arInsertBatchCnt)
if l > 1 {
rs.LastInsertId = rs.LastInsertId - +1
rs.RowsAffected = l
}
return
}

func (db *DB) Query(ar *ActiveRecord) (rs *ResultSet, err error) {
var results []map[string][]byte
if ar.cacheKey != "" {
Expand Down Expand Up @@ -209,10 +196,9 @@ func (db *DB) Query(ar *ActiveRecord) (rs *ResultSet, err error) {
return
}
defer rows.Close()
cols := []string{}
cols, err = rows.Columns()
if err != nil {
return
cols, e := rows.Columns()
if e != nil {
return nil, e
}
closCnt := len(cols)

Expand Down Expand Up @@ -256,8 +242,7 @@ func (db *DB) Query(ar *ActiveRecord) (rs *ResultSet, err error) {
}
}
}
rs = new(ResultSet)
rs.Init(&results)
rs = NewResultSet(&results)
return
}

Expand All @@ -282,6 +267,8 @@ type DBConfig struct {
SyncMode int
OpenMode string
CacheMode string
MaxIdleConns int
MaxOpenConns int
}

func NewDBConfigWith(dbfilename, openMode, cacheMode string, syncMode int) (cfg DBConfig) {
Expand Down Expand Up @@ -522,6 +509,7 @@ func (ar *ActiveRecord) Values() []interface{} {
return ar.values
}
func (ar *ActiveRecord) SQL() string {

if ar.currentSQL != "" {
return ar.currentSQL
}
Expand All @@ -543,6 +531,7 @@ func (ar *ActiveRecord) SQL() string {
case "delete":
ar.currentSQL = ar.getDeleteSQL()
}
ar.currentSQL = strings.Replace(ar.currentSQL, ar.tablePrefixSqlIdentifier, ar.tablePrefix, -1)
return ar.currentSQL
}
func (ar *ActiveRecord) getUpdateSQL() string {
Expand Down Expand Up @@ -982,15 +971,23 @@ type ResultSet struct {
RowsAffected int64
}

func (rs *ResultSet) Init(rawRows *[]map[string][]byte) {
func NewResultSet(rawRows *[]map[string][]byte) (rs *ResultSet) {
rs = &ResultSet{}
if rawRows != nil {
rs.rawRows = rawRows
} else {
rs.rawRows = &([]map[string][]byte{})
}
return
}

func (rs *ResultSet) Len() int {
return len(*rs.rawRows)
vo := reflect.ValueOf(rs.rawRows)
if vo.IsNil() {
return 0
} else {
return len(*rs.rawRows)
}
}
func (rs *ResultSet) MapRows(keyColumn string) (rowsMap map[string]map[string]string) {
rowsMap = map[string]map[string]string{}
Expand Down