Skip to content

Commit

Permalink
chore: auto escape the database name (openGemini#47)
Browse files Browse the repository at this point in the history
Signed-off-by: PennyYoon <525296438@qq.com>
  • Loading branch information
Chenxulin97 authored Jan 30, 2024
1 parent 057be87 commit 3f29b54
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
6 changes: 3 additions & 3 deletions opengemini/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func (c *client) CreateDatabase(database string) error {
return errors.New("empty database name")
}

cmd := fmt.Sprintf("CREATE DATABASE %s", database)
cmd := fmt.Sprintf("CREATE DATABASE \"%s\"", database)
queryResult, err := c.queryPost(Query{Command: cmd})
if err != nil {
return err
Expand All @@ -31,7 +31,7 @@ func (c *client) CreateDatabaseWithRp(database string, rpConfig RpConfig) error
}

var buf strings.Builder
buf.WriteString(fmt.Sprintf("CREATE DATABASE %s WITH DURATION %s REPLICATION 1", database, rpConfig.Duration))
buf.WriteString(fmt.Sprintf("CREATE DATABASE \"%s\" WITH DURATION %s REPLICATION 1", database, rpConfig.Duration))
if len(rpConfig.ShardGroupDuration) > 0 {
buf.WriteString(fmt.Sprintf(" SHARD DURATION %s", rpConfig.ShardGroupDuration))
}
Expand Down Expand Up @@ -85,7 +85,7 @@ func (c *client) DropDatabase(database string) error {
if len(database) == 0 {
return errors.New("empty database name")
}
cmd := fmt.Sprintf("DROP DATABASE %s", database)
cmd := fmt.Sprintf("DROP DATABASE \"%s\"", database)
queryResult, err := c.queryPost(Query{Command: cmd})
if err != nil {
return err
Expand Down
8 changes: 8 additions & 0 deletions opengemini/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,11 @@ func TestClientDropDatabaseEmptyDatabase(t *testing.T) {
err := c.DropDatabase("")
require.NotNil(t, err)
}

func TestCreateAndDropDatabaseWithSpecificSymbol(t *testing.T) {
c := testDefaultClient(t)
err := c.CreateDatabase("Specific-Symbol")
require.Nil(t, err)
err = c.DropDatabase("Specific-Symbol")
require.Nil(t, err)
}
4 changes: 2 additions & 2 deletions opengemini/retention_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (c *client) CreateRetentionPolicy(database string, rpConfig RpConfig, isDef
return errors.New("empty database name")
}
var buf strings.Builder
buf.WriteString(fmt.Sprintf("CREATE RETENTION POLICY %s ON %s DURATION %s REPLICATION 1", rpConfig.Name, database, rpConfig.Duration))
buf.WriteString(fmt.Sprintf("CREATE RETENTION POLICY %s ON \"%s\" DURATION %s REPLICATION 1", rpConfig.Name, database, rpConfig.Duration))
if len(rpConfig.ShardGroupDuration) > 0 {
buf.WriteString(fmt.Sprintf(" SHARD DURATION %s", rpConfig.ShardGroupDuration))
}
Expand Down Expand Up @@ -136,7 +136,7 @@ func (c *client) DropRetentionPolicy(database, retentionPolicy string) error {
return errors.New("empty database name")
}

cmd := fmt.Sprintf("DROP RETENTION POLICY %s ON %s", retentionPolicy, database)
cmd := fmt.Sprintf("DROP RETENTION POLICY %s ON \"%s\"", retentionPolicy, database)
queryResult, err := c.queryPost(Query{Command: cmd})
if err != nil {
return err
Expand Down

0 comments on commit 3f29b54

Please sign in to comment.