diff --git a/internal/repository/clickhouse/clickhouse.go b/internal/repository/clickhouse/clickhouse.go index 05eedf6f..6d757840 100644 --- a/internal/repository/clickhouse/clickhouse.go +++ b/internal/repository/clickhouse/clickhouse.go @@ -82,12 +82,13 @@ func (c *nativeConn) BatchInsert(ctx context.Context, table string, columns []st // insertSQL 构造 INSERT 语句(表名/列名由调用方白名单控制,防注入)。 func insertSQL(table string, columns []string) string { - sql := "INSERT INTO " + table + " (" + // S1-B:标识符反引号化——模块名含连字符(go-sms_*)的表名裸拼会语法错误。 + sql := "INSERT INTO `" + table + "` (" for i, c := range columns { if i > 0 { sql += "," } - sql += c + sql += "`" + c + "`" } return sql + ")" } diff --git a/test/integration/clickhouse_test.go b/test/integration/clickhouse_test.go index 42e58b3c..79e71013 100644 --- a/test/integration/clickhouse_test.go +++ b/test/integration/clickhouse_test.go @@ -46,10 +46,10 @@ func TestClickHouse_BatchInsert(t *testing.T) { t.Logf("测试表: %s", table) if out, err := execCH(fmt.Sprintf( - "CREATE TABLE %s (id UInt64, mobile String, status UInt8) ENGINE=MergeTree ORDER BY id", table)); err != nil { + "CREATE TABLE `%s` (id UInt64, mobile String, status UInt8) ENGINE=MergeTree ORDER BY id", table)); err != nil { t.Fatalf("建表失败: %v(%s)", err, out) } - defer execCH("DROP TABLE IF EXISTS " + table) + defer execCH("DROP TABLE IF EXISTS `" + table + "`") rows := make([][]any, 0, 3) for i := 0; i < 3; i++ { @@ -63,7 +63,7 @@ func TestClickHouse_BatchInsert(t *testing.T) { t.Fatalf("空批次应返回 nil: %v", err) } - out, err := execCH(fmt.Sprintf("SELECT count(), max(mobile) FROM %s", table)) + out, err := execCH(fmt.Sprintf("SELECT count(), max(mobile) FROM `%s`", table)) if err != nil { t.Fatal(err) } diff --git a/test/integration/schema_test.go b/test/integration/schema_test.go index c7ffbbeb..6dd46aeb 100644 --- a/test/integration/schema_test.go +++ b/test/integration/schema_test.go @@ -29,10 +29,10 @@ func TestMySQL_AutoMigrate(t *testing.T) { t.Skipf("MySQL 不可达,跳过(make up): %v", err) } dbName := fmt.Sprintf("go-sms_automigrate_%d", time.Now().UnixNano()%100000) - if err := admin.Exec("CREATE DATABASE " + dbName + " DEFAULT CHARACTER SET utf8mb4").Error; err != nil { + if err := admin.Exec("CREATE DATABASE `" + dbName + "` DEFAULT CHARACTER SET utf8mb4").Error; err != nil { t.Fatalf("建测试库失败: %v", err) } - t.Cleanup(func() { _ = admin.Exec("DROP DATABASE " + dbName).Error }) + t.Cleanup(func() { _ = admin.Exec("DROP DATABASE `" + dbName + "`").Error }) dsn := fmt.Sprintf("root:root123@tcp(127.0.0.1:13306)/%s?charset=utf8mb4&parseTime=True&loc=Local", dbName) db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})