From 0e4c79d24ce2d1c7865e167e109ad74dab71bddc Mon Sep 17 00:00:00 2001 From: Tory Date: Fri, 13 Nov 2026 17:55:13 +0800 Subject: [PATCH] =?UTF-8?q?S1-B=20D2.6+D1.5:=20router=20=E8=90=BD=E4=BD=8D?= =?UTF-8?q?(MySQL=20=E7=A1=AC=E6=96=AD=E8=A8=80=E5=8F=A3=E5=BE=84=20p99=3D?= =?UTF-8?q?9.4ms/=E5=90=9E=E5=90=901620s=20-race=20=E5=85=A8=E5=8C=85=2082?= =?UTF-8?q?.1%=20=E7=BB=BF=E2=80=94=E2=80=94sqlite=20=E6=A8=A1=E5=BC=8F=20?= =?UTF-8?q?222/s=20=E4=B8=BA=E6=97=A0=E4=B8=AD=E9=97=B4=E4=BB=B6=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=B7=AE=E5=BC=82,=E9=9D=9E=E5=9B=9E=E5=BD=92)+?= =?UTF-8?q?=E9=9B=86=E6=88=90=E6=B5=8B=E8=AF=95=E4=BF=AE=E7=9B=B4(?= =?UTF-8?q?=E5=BA=93=E5=90=8D/=E8=A1=A8=E5=90=8D=E5=90=AB=E8=BF=9E?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E6=A0=87=E8=AF=86=E7=AC=A6=E5=8F=8D=E5=BC=95?= =?UTF-8?q?=E5=8F=B7=E5=8C=96=C3=973=20=E5=A4=84,BatchInsert=20=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E4=BE=A7=E5=BC=95=E5=8F=B7=E5=8C=96)+=E9=9B=86?= =?UTF-8?q?=E6=88=90=E5=85=AD=E4=BB=B6=E5=85=A8=E7=BB=BF(MySQL=20AutoMigra?= =?UTF-8?q?te/Kafka=20=E7=94=9F=E4=BA=A7=E6=B6=88=E8=B4=B9/DLQ=20=E6=B6=88?= =?UTF-8?q?=E8=B4=B9=E7=BB=84/CH=20BatchInsert/casbin/cache=5Flock)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/repository/clickhouse/clickhouse.go | 5 +++-- test/integration/clickhouse_test.go | 6 +++--- test/integration/schema_test.go | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) 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{})