Skip to content

Commit

Permalink
Merge branch 'go-sql-driver' of github.com:XiaoMi/soar into go-sql-dr…
Browse files Browse the repository at this point in the history
…iver
  • Loading branch information
martianzhang committed Dec 24, 2018
2 parents b9e152b + 62f0b8e commit fcb471f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 15 deletions.
27 changes: 12 additions & 15 deletions database/sampling.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@
package database

import (
"database/sql"
"fmt"
"time"

"strings"

"database/sql"
"time"

"github.com/XiaoMi/soar/common"
"github.com/ziutek/mymysql/mysql"
Expand Down Expand Up @@ -92,7 +90,6 @@ func (db *Connector) SamplingData(onlineConn *Connector, database string, tables
} else {
where = common.Config.SamplingCondition
}

err = db.startSampling(onlineConn.Conn, database, table, where)
}
return err
Expand Down Expand Up @@ -143,17 +140,17 @@ func (db *Connector) startSampling(onlineConn *sql.DB, database, table string, w
values = append(values, fmt.Sprintf(`unhex("%s")`, fmt.Sprintf("%x", val)))
}
}
}
valuesStr = append(valuesStr, "("+strings.Join(values, `,`)+")")
valuesCount++
if maxValuesCount <= valuesCount {
err = db.doSampling(table, columnsStr, strings.Join(valuesStr, `,`))
if err != nil {
break
valuesStr = append(valuesStr, "("+strings.Join(values, `,`)+")")
valuesCount++
if maxValuesCount <= valuesCount {
err = db.doSampling(table, columnsStr, strings.Join(valuesStr, `,`))
if err != nil {
break
}
values = make([]string, 0)
valuesStr = make([]string, 0)
valuesCount = 0
}
values = make([]string, 0)
valuesStr = make([]string, 0)
valuesCount = 0
}
}
res.Close()
Expand Down
57 changes: 57 additions & 0 deletions env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,60 @@ func TestCreateDatabase(t *testing.T) {
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestCreateTable(t *testing.T) {
orgSamplingCondition := common.Config.SamplingCondition
common.Config.SamplingCondition = "LIMIT 1"

vEnv, rEnv := BuildEnv()
defer vEnv.CleanUp()
// TODO: support VIEW,
tables := []string{
"actor",
// "actor_info", // VIEW
"address",
"category",
"city",
"country",
"customer",
"customer_list",
"film",
"film_actor",
"film_category",
"film_list",
"film_text",
"inventory",
"language",
"nicer_but_slower_film_list",
"payment",
"rental",
// "sales_by_film_category", // VIEW
// "sales_by_store", // VIEW
"staff",
"staff_list",
"store",
}
for _, table := range tables {
err := vEnv.createTable(rEnv, "sakila", table)
if err != nil {
t.Error(err)
}
}
common.Config.SamplingCondition = orgSamplingCondition
}

func TestCreateDatabase(t *testing.T) {
vEnv, rEnv := BuildEnv()
defer vEnv.CleanUp()
err := vEnv.createDatabase(rEnv, "sakila")
if err != nil {
t.Error(err)
}
if vEnv.DBHash("sakila") == "sakila" {
t.Errorf("database: sakila rehashed failed!")
}

if vEnv.DBHash("not_exist_db") != "not_exist_db" {
t.Errorf("database: not_exist_db rehashed!")
}
}

0 comments on commit fcb471f

Please sign in to comment.