diff --git a/mysql/mysql.go b/mysql/mysql.go index 138dc22..ef7ee3d 100755 --- a/mysql/mysql.go +++ b/mysql/mysql.go @@ -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{} diff --git a/mysql/mysql_test.go b/mysql/mysql_test.go index 957616d..9a3d26b 100644 --- a/mysql/mysql_test.go +++ b/mysql/mysql_test.go @@ -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"), @@ -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"), @@ -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) @@ -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) @@ -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) @@ -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) - } } } } diff --git a/sqlite3/sqlite3.go b/sqlite3/sqlite3.go index 61475f6..1a69edb 100644 --- a/sqlite3/sqlite3.go +++ b/sqlite3/sqlite3.go @@ -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 } @@ -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) @@ -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) @@ -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 != "" { @@ -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) @@ -256,8 +242,7 @@ func (db *DB) Query(ar *ActiveRecord) (rs *ResultSet, err error) { } } } - rs = new(ResultSet) - rs.Init(&results) + rs = NewResultSet(&results) return } @@ -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) { @@ -522,6 +509,7 @@ func (ar *ActiveRecord) Values() []interface{} { return ar.values } func (ar *ActiveRecord) SQL() string { + if ar.currentSQL != "" { return ar.currentSQL } @@ -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 { @@ -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{}