Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c922023740 | |||
| 9949231d7c | |||
| 3fd0975427 |
@ -3,6 +3,7 @@ package hotime
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
@ -59,6 +60,9 @@ func (that *Context) Display(statu int, data interface{}) {
|
||||
resp["result"] = temp
|
||||
//兼容android等需要json转对象的服务
|
||||
resp["error"] = temp
|
||||
|
||||
that.Error.SetError(errors.New(resp.ToJsonString()))
|
||||
|
||||
} else {
|
||||
resp["result"] = data
|
||||
}
|
||||
|
||||
@ -83,21 +83,22 @@ func (p *IdentifierProcessor) ProcessTableNameNoPrefix(name string) string {
|
||||
|
||||
// ProcessColumn 处理 table.column 格式
|
||||
// 输入: "name" 或 "order.name" 或 "`order`.name" 或 "`order`.`name`"
|
||||
// 输出: "`name`" 或 "`app_order`.`name`" (MySQL)
|
||||
// 输出: "`name`" 或 "app_order.name"
|
||||
// 注意: 单独的列名加引号(避免关键字冲突),table.column 格式不加引号
|
||||
func (p *IdentifierProcessor) ProcessColumn(name string) string {
|
||||
// 检查是否包含点号
|
||||
if !strings.Contains(name, ".") {
|
||||
// 单独的列名,只加引号
|
||||
// 单独的列名,需要加引号(避免关键字冲突)
|
||||
return p.dialect.QuoteIdentifier(p.stripQuotes(name))
|
||||
}
|
||||
|
||||
// 处理 table.column 格式
|
||||
// 处理 table.column 格式,不加引号,只添加前缀
|
||||
parts := p.splitTableColumn(name)
|
||||
if len(parts) == 2 {
|
||||
tableName := p.stripQuotes(parts[0])
|
||||
columnName := p.stripQuotes(parts[1])
|
||||
// 表名添加前缀
|
||||
return p.dialect.QuoteIdentifier(p.prefix+tableName) + "." + p.dialect.QuoteIdentifier(columnName)
|
||||
// table.column 格式不加反引号
|
||||
return p.prefix + tableName + "." + columnName
|
||||
}
|
||||
|
||||
// 无法解析,返回原样但转换引号
|
||||
@ -107,14 +108,16 @@ func (p *IdentifierProcessor) ProcessColumn(name string) string {
|
||||
// ProcessColumnNoPrefix 处理 table.column 格式(不添加前缀)
|
||||
func (p *IdentifierProcessor) ProcessColumnNoPrefix(name string) string {
|
||||
if !strings.Contains(name, ".") {
|
||||
// 单独的列名,需要加引号(避免关键字冲突)
|
||||
return p.dialect.QuoteIdentifier(p.stripQuotes(name))
|
||||
}
|
||||
|
||||
// table.column 格式不加引号
|
||||
parts := p.splitTableColumn(name)
|
||||
if len(parts) == 2 {
|
||||
tableName := p.stripQuotes(parts[0])
|
||||
columnName := p.stripQuotes(parts[1])
|
||||
return p.dialect.QuoteIdentifier(tableName) + "." + p.dialect.QuoteIdentifier(columnName)
|
||||
return tableName + "." + columnName
|
||||
}
|
||||
|
||||
return p.convertQuotes(name)
|
||||
@ -122,7 +125,9 @@ func (p *IdentifierProcessor) ProcessColumnNoPrefix(name string) string {
|
||||
|
||||
// ProcessConditionString 智能解析条件字符串(如 ON 条件)
|
||||
// 输入: "user.id = order.user_id AND order.status = 1"
|
||||
// 输出: "`app_user`.`id` = `app_order`.`user_id` AND `app_order`.`status` = 1" (MySQL)
|
||||
// 输出: "app_user.id = app_order.user_id AND app_order.status = 1"
|
||||
// 注意: table.column 格式不加反引号,因为 MySQL/SQLite/PostgreSQL 都能正确解析
|
||||
// 这样可以保持返回的列名与原始列名一致,便于聚合函数等场景读取结果
|
||||
func (p *IdentifierProcessor) ProcessConditionString(condition string) string {
|
||||
if condition == "" {
|
||||
return condition
|
||||
@ -131,20 +136,21 @@ func (p *IdentifierProcessor) ProcessConditionString(condition string) string {
|
||||
result := condition
|
||||
|
||||
// 首先处理已有完整引号的情况 `table`.`column` 或 "table"."column"
|
||||
// 这些需要先处理,因为它们的格式最明确
|
||||
// 去除引号,只添加前缀
|
||||
fullyQuotedPattern := regexp.MustCompile("[`\"]([a-zA-Z_][a-zA-Z0-9_]*)[`\"]\\.[`\"]([a-zA-Z_][a-zA-Z0-9_]*)[`\"]")
|
||||
result = fullyQuotedPattern.ReplaceAllStringFunc(result, func(match string) string {
|
||||
parts := fullyQuotedPattern.FindStringSubmatch(match)
|
||||
if len(parts) == 3 {
|
||||
tableName := parts[1]
|
||||
colName := parts[2]
|
||||
return p.dialect.QuoteIdentifier(p.prefix+tableName) + "." + p.dialect.QuoteIdentifier(colName)
|
||||
// table.column 格式不加反引号,只添加前缀
|
||||
return p.prefix + tableName + "." + colName
|
||||
}
|
||||
return match
|
||||
})
|
||||
|
||||
// 然后处理部分引号的情况 `table`.column 或 "table".column
|
||||
// 注意:需要避免匹配已处理的内容(已经是双引号包裹的)
|
||||
// 去除引号,只添加前缀
|
||||
quotedTablePattern := regexp.MustCompile("[`\"]([a-zA-Z_][a-zA-Z0-9_]*)[`\"]\\.([a-zA-Z_][a-zA-Z0-9_]*)(?:[^`\"]|$)")
|
||||
result = quotedTablePattern.ReplaceAllStringFunc(result, func(match string) string {
|
||||
parts := quotedTablePattern.FindStringSubmatch(match)
|
||||
@ -159,7 +165,8 @@ func (p *IdentifierProcessor) ProcessConditionString(condition string) string {
|
||||
suffix = string(lastChar)
|
||||
}
|
||||
}
|
||||
return p.dialect.QuoteIdentifier(p.prefix+tableName) + "." + p.dialect.QuoteIdentifier(colName) + suffix
|
||||
// table.column 格式不加反引号,只添加前缀
|
||||
return p.prefix + tableName + "." + colName + suffix
|
||||
}
|
||||
return match
|
||||
})
|
||||
@ -174,7 +181,8 @@ func (p *IdentifierProcessor) ProcessConditionString(condition string) string {
|
||||
tableName := parts[2]
|
||||
colName := parts[3]
|
||||
suffix := parts[4] // 后面的边界字符
|
||||
return prefix + p.dialect.QuoteIdentifier(p.prefix+tableName) + "." + p.dialect.QuoteIdentifier(colName) + suffix
|
||||
// table.column 格式不加反引号,只添加前缀
|
||||
return prefix + p.prefix + tableName + "." + colName + suffix
|
||||
}
|
||||
return match
|
||||
})
|
||||
|
||||
136
example/main.go
136
example/main.go
@ -20,6 +20,9 @@ func main() {
|
||||
appIns.Run(Router{
|
||||
"app": {
|
||||
"test": {
|
||||
"test": func(that *Context) {
|
||||
that.Display(2, "dsadasd")
|
||||
},
|
||||
// 测试入口 - 运行所有测试
|
||||
"all": func(that *Context) {
|
||||
results := Map{}
|
||||
@ -99,7 +102,7 @@ func main() {
|
||||
"cache-compat": func(that *Context) { that.Display(0, testCacheCompatible(that)) },
|
||||
// 批量缓存操作测试
|
||||
"cache-batch": func(that *Context) {
|
||||
TestBatchCacheOperations(that.Application)
|
||||
//TestBatchCacheOperations(that.Application)
|
||||
that.Display(0, Map{"message": "批量缓存测试完成,请查看控制台输出和日志文件"})
|
||||
},
|
||||
},
|
||||
@ -561,32 +564,36 @@ func testAggregate(that *Context) Map {
|
||||
test2["count"] = count2
|
||||
tests = append(tests, test2)
|
||||
|
||||
// 5.3 Sum 求和
|
||||
test3 := Map{"name": "Sum 求和"}
|
||||
// 5.3 Sum 求和 - 单字段名
|
||||
test3 := Map{"name": "Sum 求和 (单字段名)"}
|
||||
sum3 := that.Db.Sum("article", "click_num", Map{"state": 0})
|
||||
test3["result"] = sum3 >= 0
|
||||
test3["sum"] = sum3
|
||||
test3["lastQuery"] = that.Db.LastQuery
|
||||
tests = append(tests, test3)
|
||||
|
||||
// 5.4 Avg 平均值
|
||||
test4 := Map{"name": "Avg 平均值"}
|
||||
// 5.4 Avg 平均值 - 单字段名
|
||||
test4 := Map{"name": "Avg 平均值 (单字段名)"}
|
||||
avg4 := that.Db.Avg("article", "click_num", Map{"state": 0})
|
||||
test4["result"] = avg4 >= 0
|
||||
test4["avg"] = avg4
|
||||
test4["lastQuery"] = that.Db.LastQuery
|
||||
tests = append(tests, test4)
|
||||
|
||||
// 5.5 Max 最大值
|
||||
test5 := Map{"name": "Max 最大值"}
|
||||
// 5.5 Max 最大值 - 单字段名
|
||||
test5 := Map{"name": "Max 最大值 (单字段名)"}
|
||||
max5 := that.Db.Max("article", "click_num", Map{"state": 0})
|
||||
test5["result"] = max5 >= 0
|
||||
test5["max"] = max5
|
||||
test5["lastQuery"] = that.Db.LastQuery
|
||||
tests = append(tests, test5)
|
||||
|
||||
// 5.6 Min 最小值
|
||||
test6 := Map{"name": "Min 最小值"}
|
||||
// 5.6 Min 最小值 - 单字段名
|
||||
test6 := Map{"name": "Min 最小值 (单字段名)"}
|
||||
min6 := that.Db.Min("article", "sort", Map{"state": 0})
|
||||
test6["result"] = true // sort 可能为 0
|
||||
test6["min"] = min6
|
||||
test6["lastQuery"] = that.Db.LastQuery
|
||||
tests = append(tests, test6)
|
||||
|
||||
// 5.7 GROUP BY 分组统计
|
||||
@ -603,6 +610,117 @@ func testAggregate(that *Context) Map {
|
||||
test7["stats"] = stats7
|
||||
tests = append(tests, test7)
|
||||
|
||||
// ==================== 新增:table.column 格式测试 ====================
|
||||
// 5.8 Sum 求和 - table.column 格式(修复验证)
|
||||
test8 := Map{"name": "Sum 求和 (table.column 格式)"}
|
||||
sum8 := that.Db.Sum("article", "article.click_num", Map{"state": 0})
|
||||
test8["result"] = sum8 >= 0
|
||||
test8["sum"] = sum8
|
||||
test8["expected"] = "与单字段名 Sum 结果相同"
|
||||
test8["match_single_field"] = sum8 == sum3 // 应该与 test3 结果相同
|
||||
test8["lastQuery"] = that.Db.LastQuery
|
||||
tests = append(tests, test8)
|
||||
|
||||
// 5.9 Avg 平均值 - table.column 格式
|
||||
test9 := Map{"name": "Avg 平均值 (table.column 格式)"}
|
||||
avg9 := that.Db.Avg("article", "article.click_num", Map{"state": 0})
|
||||
test9["result"] = avg9 >= 0
|
||||
test9["avg"] = avg9
|
||||
test9["match_single_field"] = avg9 == avg4
|
||||
test9["lastQuery"] = that.Db.LastQuery
|
||||
tests = append(tests, test9)
|
||||
|
||||
// 5.10 Max 最大值 - table.column 格式
|
||||
test10 := Map{"name": "Max 最大值 (table.column 格式)"}
|
||||
max10 := that.Db.Max("article", "article.click_num", Map{"state": 0})
|
||||
test10["result"] = max10 >= 0
|
||||
test10["max"] = max10
|
||||
test10["match_single_field"] = max10 == max5
|
||||
test10["lastQuery"] = that.Db.LastQuery
|
||||
tests = append(tests, test10)
|
||||
|
||||
// 5.11 Min 最小值 - table.column 格式
|
||||
test11 := Map{"name": "Min 最小值 (table.column 格式)"}
|
||||
min11 := that.Db.Min("article", "article.sort", Map{"state": 0})
|
||||
test11["result"] = true
|
||||
test11["min"] = min11
|
||||
test11["match_single_field"] = min11 == min6
|
||||
test11["lastQuery"] = that.Db.LastQuery
|
||||
tests = append(tests, test11)
|
||||
|
||||
// ==================== 带 JOIN 的聚合函数测试 ====================
|
||||
// 5.12 Sum 带 JOIN - table.column 格式
|
||||
test12 := Map{"name": "Sum 带 JOIN (table.column 格式)"}
|
||||
joinSlice := Slice{
|
||||
Map{"[>]ctg": "article.ctg_id = ctg.id"},
|
||||
}
|
||||
sum12 := that.Db.Sum("article", "article.click_num", joinSlice, Map{"article.state": 0})
|
||||
test12["result"] = sum12 >= 0
|
||||
test12["sum"] = sum12
|
||||
test12["lastQuery"] = that.Db.LastQuery
|
||||
tests = append(tests, test12)
|
||||
|
||||
// 5.13 Count 带 JOIN
|
||||
test13 := Map{"name": "Count 带 JOIN"}
|
||||
count13 := that.Db.Count("article", joinSlice, Map{"article.state": 0})
|
||||
test13["result"] = count13 >= 0
|
||||
test13["count"] = count13
|
||||
test13["lastQuery"] = that.Db.LastQuery
|
||||
tests = append(tests, test13)
|
||||
|
||||
// ==================== Select 方法验证 table.column 格式 ====================
|
||||
// 5.14 Select 使用 table.column 选择字段
|
||||
test14 := Map{"name": "Select table.column 字段选择"}
|
||||
articles14 := that.Db.Select("article",
|
||||
"article.id, article.title, article.click_num",
|
||||
Map{"article.state": 0, "LIMIT": 3})
|
||||
test14["result"] = len(articles14) >= 0
|
||||
test14["count"] = len(articles14)
|
||||
// 验证返回的 Map 中字段名是否正确(不带反引号)
|
||||
if len(articles14) > 0 {
|
||||
keys := []string{}
|
||||
for k := range articles14[0] {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
test14["returned_keys"] = keys
|
||||
// 检查是否能正确读取值(字段名是 article.id 还是 id)
|
||||
test14["sample_data"] = articles14[0]
|
||||
}
|
||||
test14["lastQuery"] = that.Db.LastQuery
|
||||
tests = append(tests, test14)
|
||||
|
||||
// 5.15 Select 带 JOIN 使用 table.column
|
||||
test15 := Map{"name": "Select 带 JOIN table.column"}
|
||||
articles15 := that.Db.Select("article",
|
||||
joinSlice,
|
||||
"article.id, article.title, ctg.name as ctg_name",
|
||||
Map{"article.state": 0, "LIMIT": 3})
|
||||
test15["result"] = len(articles15) >= 0
|
||||
test15["count"] = len(articles15)
|
||||
if len(articles15) > 0 {
|
||||
keys := []string{}
|
||||
for k := range articles15[0] {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
test15["returned_keys"] = keys
|
||||
test15["sample_data"] = articles15[0]
|
||||
}
|
||||
test15["lastQuery"] = that.Db.LastQuery
|
||||
tests = append(tests, test15)
|
||||
|
||||
// ==================== 聚合函数结果对比验证 ====================
|
||||
// 5.16 验证 table.column 与单字段结果一致性
|
||||
test16 := Map{"name": "聚合函数一致性验证"}
|
||||
allMatch := (sum8 == sum3) && (avg9 == avg4) && (max10 == max5) && (min11 == min6)
|
||||
test16["result"] = allMatch
|
||||
test16["sum_match"] = sum8 == sum3
|
||||
test16["avg_match"] = avg9 == avg4
|
||||
test16["max_match"] = max10 == max5
|
||||
test16["min_match"] = min11 == min6
|
||||
test16["summary"] = fmt.Sprintf("Sum: %v=%v, Avg: %v=%v, Max: %v=%v, Min: %v=%v",
|
||||
sum3, sum8, avg4, avg9, max5, max10, min6, min11)
|
||||
tests = append(tests, test16)
|
||||
|
||||
result["tests"] = tests
|
||||
result["success"] = true
|
||||
return result
|
||||
|
||||
@ -141,8 +141,13 @@ func isHoTimeFrameworkFile(file string) bool {
|
||||
// 对caller进行递归查询, 直到找到非框架层产生的第一个调用.
|
||||
// 遍历调用栈,跳过框架层文件,找到应用层代码
|
||||
// 使用层数限制确保不会误过滤应用层同名目录
|
||||
// 返回优先级:应用层代码 > application.go > 其他框架文件
|
||||
func findCaller(skip int) string {
|
||||
frameworkCount := 0 // 连续框架层计数
|
||||
var lastFrameworkFile string
|
||||
var lastFrameworkLine int
|
||||
var applicationFile string // 优先记录 application.go 位置
|
||||
var applicationLine int
|
||||
|
||||
// 遍历调用栈,找到第一个非框架文件
|
||||
for i := 0; i < 20; i++ {
|
||||
@ -151,8 +156,17 @@ func findCaller(skip int) string {
|
||||
break
|
||||
}
|
||||
|
||||
if isHoTimeFrameworkFile(file) {
|
||||
isFramework := isHoTimeFrameworkFile(file)
|
||||
|
||||
if isFramework {
|
||||
frameworkCount++
|
||||
lastFrameworkFile = file
|
||||
lastFrameworkLine = line
|
||||
// 优先记录 application.go 位置(HoTime 框架入口)
|
||||
if strings.Contains(file, "application.go") {
|
||||
applicationFile = file
|
||||
applicationLine = line
|
||||
}
|
||||
// 层数限制:如果已经跳过太多层,停止跳过
|
||||
if frameworkCount >= maxFrameworkDepth {
|
||||
return fmt.Sprintf("%s:%d", file, line)
|
||||
@ -164,7 +178,18 @@ func findCaller(skip int) string {
|
||||
return fmt.Sprintf("%s:%d", file, line)
|
||||
}
|
||||
|
||||
// 如果找不到应用层,返回最初的调用者
|
||||
// 如果找不到应用层,返回最后记录的框架文件位置
|
||||
// 优先级:application.go > 其他框架文件 > 第一个调用者
|
||||
// 确保不会返回 logrus 或 runtime 等三方组件位置
|
||||
if applicationFile != "" {
|
||||
return fmt.Sprintf("%s:%d", applicationFile, applicationLine)
|
||||
}
|
||||
|
||||
if lastFrameworkFile != "" {
|
||||
return fmt.Sprintf("%s:%d", lastFrameworkFile, lastFrameworkLine)
|
||||
}
|
||||
|
||||
// 最后的回退:返回第一个调用者
|
||||
file, line := getCaller(skip)
|
||||
return fmt.Sprintf("%s:%d", file, line)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user