From 3f29b549bac90bb9e79d2e40ec5c6985a57126ec Mon Sep 17 00:00:00 2001 From: PennyYoon <55304072+Chenxulin97@users.noreply.github.com> Date: Tue, 30 Jan 2024 21:25:17 +0800 Subject: [PATCH] chore: auto escape the database name (#47) Signed-off-by: PennyYoon <525296438@qq.com> --- opengemini/database.go | 6 +++--- opengemini/database_test.go | 8 ++++++++ opengemini/retention_policy.go | 4 ++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/opengemini/database.go b/opengemini/database.go index 7a113fd..f402be1 100644 --- a/opengemini/database.go +++ b/opengemini/database.go @@ -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 @@ -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)) } @@ -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 diff --git a/opengemini/database_test.go b/opengemini/database_test.go index 13f9ff7..1441330 100644 --- a/opengemini/database_test.go +++ b/opengemini/database_test.go @@ -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) +} diff --git a/opengemini/retention_policy.go b/opengemini/retention_policy.go index a1f29eb..0e8bc9a 100644 --- a/opengemini/retention_policy.go +++ b/opengemini/retention_policy.go @@ -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)) } @@ -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