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

USE databasename will work only for existing databases. #5183

Merged
merged 2 commits into from
Dec 22, 2015
Merged
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
38 changes: 36 additions & 2 deletions cmd/influx/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,42 @@ func (c *CommandLine) use(cmd string) {
return
}
d := args[1]
c.Database = d
fmt.Printf("Using database %s\n", d)

// validate if specified database exists
response, err := c.Client.Query(client.Query{Command: "SHOW DATABASES"})
if err != nil {
fmt.Printf("ERR: %s\n", err)
return
}

if err := response.Error(); err != nil {
fmt.Printf("ERR: %s\n", err)
return
}

// verify the provided database exists
databaseExists := func() bool {
for _, result := range response.Results {
for _, row := range result.Series {
if row.Name == "databases" {
for _, values := range row.Values {
for _, database := range values {
if database == d {
return true
}
}
}
}
}
}
return false
}()
if databaseExists {
c.Database = d
fmt.Printf("Using database %s\n", d)
} else {
fmt.Printf("ERR: Database %s doesn't exist. Run SHOW DATABASES for a list of existing databases.\n", d)
}
}

// SetPrecision sets client precision
Expand Down