Skip to content

Commit

Permalink
tidb: Split datasource name to uri and dbName
Browse files Browse the repository at this point in the history
The DBName may be different from data path name.
  • Loading branch information
shenli committed Sep 10, 2015
1 parent 89455ad commit 62b73d2
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
28 changes: 18 additions & 10 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,30 @@ func (d *sqlDriver) lock() func() {
// efficient re-use.
//
// The returned connection is only used by one goroutine at a time.
func (d *sqlDriver) Open(name string) (driver.Conn, error) {
store, err := NewStore(name)
func (d *sqlDriver) Open(dataSource string) (driver.Conn, error) {
// Split the dataSource to uri and dbName
i := strings.LastIndex(dataSource, "/")
if i == -1 {
return nil, errors.Errorf("Wrong format of datasource string: %q", dataSource)
}
uri := dataSource[:i]
dbName := dataSource[i+1:]

store, err := NewStore(uri)
if err != nil {
return nil, errors.Trace(err)
}

driver := &sqlDriver{}
switch {
case strings.HasPrefix(name, "file://"):
name = name[len("file://"):]
case strings.HasPrefix(name, "memory://"):
name = name[len("memory://"):]
case strings.HasPrefix(uri, "file://"):
uri = uri[len("file://"):]
case strings.HasPrefix(uri, "memory://"):
uri = uri[len("memory://"):]
}
name = filepath.Clean(name)
if name == "" || name == "." || name == string(os.PathSeparator) {
return nil, errors.Errorf("invalid DB name %q", name)
dbName = filepath.Clean(dbName)
if dbName == "" || dbName == "." || dbName == string(os.PathSeparator) {
return nil, errors.Errorf("invalid DB name %q", dbName)
}

sess, err := CreateSession(store)
Expand All @@ -155,7 +163,7 @@ func (d *sqlDriver) Open(name string) (driver.Conn, error) {
}
s := sess.(*session)
defer d.lock()()
DBName := model.NewCIStr(name[strings.LastIndex(name, "/")+1:])
DBName := model.NewCIStr(dbName)
domain := sessionctx.GetDomain(s)
if !domain.InfoSchema().SchemaExists(DBName) {
err = domain.DDL().CreateSchema(s, DBName)
Expand Down
2 changes: 1 addition & 1 deletion plan/plans/explain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func mustExplain(c *C, currDB *sql.DB, s string) string {
}

func (t *testExplainSuit) TestExplain(c *C) {
testDB, err := sql.Open(tidb.DriverName, tidb.EngineGoLevelDBMemory+"ex")
testDB, err := sql.Open(tidb.DriverName, tidb.EngineGoLevelDBMemory+"/ex/ex")
c.Assert(err, IsNil)
mustExec(c, testDB, "create table tt(id int);")
mustExec(c, testDB, "create table tt2(id int, KEY i_id(id));")
Expand Down
2 changes: 1 addition & 1 deletion plan/plans/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func mustExec(c *C, currDB *sql.DB, sql string) sql.Result {
}

func (p *testInfoSchemaSuit) TestInfoSchema(c *C) {
testDB, err := sql.Open(tidb.DriverName, tidb.EngineGoLevelDBMemory+"test")
testDB, err := sql.Open(tidb.DriverName, tidb.EngineGoLevelDBMemory+"/test/test")
c.Assert(err, IsNil)
mustExec(c, testDB, "create table t (id int);")
cnt := mustQuery(c, testDB, "select * from information_schema.schemata")
Expand Down
2 changes: 1 addition & 1 deletion stmt/stmts/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (s *testStmtSuite) SetUpTest(c *C) {
log.SetLevelByString("error")
s.dbName = "test"
var err error
s.testDB, err = sql.Open(tidb.DriverName, tidb.EngineGoLevelDBMemory+s.dbName)
s.testDB, err = sql.Open(tidb.DriverName, tidb.EngineGoLevelDBMemory+"/"+s.dbName+"/"+s.dbName)
c.Assert(err, IsNil)
// create db
s.createDBSql = fmt.Sprintf("create database if not exists %s;", s.dbName)
Expand Down
2 changes: 1 addition & 1 deletion stmt/stmts/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

func (s *testStmtSuite) TestUpdate(c *C) {
testDB, err := sql.Open(tidb.DriverName, tidb.EngineGoLevelDBMemory+s.dbName)
testDB, err := sql.Open(tidb.DriverName, tidb.EngineGoLevelDBMemory+"tmp/"+s.dbName)
c.Assert(err, IsNil)

s.fillData(testDB, c)
Expand Down
8 changes: 4 additions & 4 deletions tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (s *testMainSuite) TestConcurrent(c *C) {
dbName := "test_concurrent_db"
defer removeStore(c, dbName)

testDB, err := sql.Open(DriverName, *store+"://"+dbName)
testDB, err := sql.Open(DriverName, *store+"://"+dbName+"/"+dbName)
c.Assert(err, IsNil)
defer testDB.Close()

Expand Down Expand Up @@ -154,7 +154,7 @@ func (s *testMainSuite) TestConcurrent(c *C) {
}

func (s *testMainSuite) TestTableInfoMeta(c *C) {
testDB, err := sql.Open(DriverName, *store+"://"+s.dbName)
testDB, err := sql.Open(DriverName, *store+"://"+s.dbName+"/"+s.dbName)
c.Assert(err, IsNil)
defer testDB.Close()

Expand Down Expand Up @@ -234,7 +234,7 @@ func (s *testMainSuite) TestCaseInsensitive(c *C) {
}

func (s *testMainSuite) TestDriverPrepare(c *C) {
testDB, err := sql.Open(DriverName, *store+"://"+s.dbName)
testDB, err := sql.Open(DriverName, *store+"://"+s.dbName+"/"+s.dbName)
c.Assert(err, IsNil)
defer testDB.Close()

Expand All @@ -260,7 +260,7 @@ func (s *testMainSuite) TestDriverPrepare(c *C) {

// Testcase for delete panic
func (s *testMainSuite) TestDeletePanic(c *C) {
db, err := sql.Open("tidb", "memory://test")
db, err := sql.Open("tidb", "memory://test/test")
defer db.Close()
_, err = db.Exec("create table t (c int)")
c.Assert(err, IsNil)
Expand Down

0 comments on commit 62b73d2

Please sign in to comment.