Skip to content
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

tidb: Split datasource name to uri and dbName #96

Merged
merged 2 commits into from
Sep 10, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong -> Bad?

}
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://"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does any engine use file:// as the prefix ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The switch statement is useless now. I will remove it.

uri = uri[len("file://"):]
case strings.HasPrefix(uri, "memory://"):
uri = uri[len("memory://"):]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need use default to return an error?

}
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