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 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
6 changes: 4 additions & 2 deletions docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ func main() {
// Default log level is debug, set it to error to turn off debug log.
log.SetLevelByString("error")

// DriverName is 'tidb', dataSourceName is in the form of "<engine>://<dbPath>".
// DriverName is 'tidb', dataSourceName is in the form of "<engine>://<dbPath>/<dbName>".
// dbPath is the directory that stores the data files if you use a local storage engine.
// dbName is the name of the database which you want to use.
dbPath := "/tmp/tidb"
db, err := sql.Open("tidb", "goleveldb://" + dbPath)
dbName := "tidb"
db, err := sql.Open("tidb", "goleveldb://" + dbPath + "/" + dbName)
if err != nil {
log.Fatal(err)
}
Expand Down
27 changes: 14 additions & 13 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,24 @@ 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("Invalid dataSource: %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://"):]
}
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,15 +157,14 @@ 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)
if err != nil {
return nil, errors.Trace(err)
}
}

return newDriverConn(s, driver, DBName.O)
}

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