result_set.go 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631
  1. package utils
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "reflect"
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "runtime"
  11. "github.com/google/uuid"
  12. )
  13. // SQL注入检测关键词
  14. var sqlInjectionKeywords = []string{
  15. "select ", "insert ", "update ", "delete ", "drop ", "truncate ",
  16. "union ", "create ", "alter ", "exec ", "execute ", "script",
  17. "'", "\"", "--", "#", "/*", "*/", "sleep(", "waitfor",
  18. }
  19. // 后缀常量
  20. const (
  21. suffixBegin = "_begin"
  22. suffixEnd = "_end"
  23. suffixLike = "_like"
  24. suffixNot = "_not"
  25. suffixIn = "_in"
  26. suffixNotIn = "_not_in"
  27. suffixNull = "_null"
  28. suffixNotNull = "_not_null"
  29. suffixApplySQL = "apply_sql"
  30. )
  31. // 标签常量
  32. const (
  33. tagTableKey = "table_key"
  34. tagIdType = "id_type"
  35. tagDb = "db"
  36. )
  37. // IdType ID类型常量
  38. type IdType string
  39. const (
  40. IdTypeAuto IdType = "AUTO" // 自增
  41. IdTypeUUID IdType = "UUID" // UUID
  42. )
  43. // SQLBuilder SQL构建器结构体
  44. type SQLBuilder struct {
  45. conditions []string
  46. orderBy string
  47. groupBy string
  48. limit string
  49. }
  50. // NewSQLBuilder 创建新的SQL构建器
  51. func NewSQLBuilder() *SQLBuilder {
  52. return &SQLBuilder{
  53. conditions: make([]string, 0),
  54. }
  55. }
  56. // BuildWhereCondition 构建WHERE条件
  57. func BuildWhereCondition(params map[string]interface{}) (string, error) {
  58. builder := NewSQLBuilder()
  59. // 首先处理分页、排序、分组参数
  60. builder.extractPageParams(params)
  61. // 遍历参数构建条件
  62. for key, value := range params {
  63. if err := builder.processCondition(key, value); err != nil {
  64. return "", err
  65. }
  66. }
  67. // 构建完整的SQL
  68. sql := builder.buildSQL()
  69. return sql, nil
  70. }
  71. // FormatSelectByIdSql 根据对象主键生成SELECT SQL语句
  72. func FormatSelectByIdSql(obj interface{}) (string, error) {
  73. // 获取对象类型和值
  74. objType := reflect.TypeOf(obj)
  75. objValue := reflect.ValueOf(obj)
  76. // 如果是指针,获取指向的值
  77. if objType.Kind() == reflect.Ptr {
  78. objType = objType.Elem()
  79. objValue = objValue.Elem()
  80. }
  81. // 检查是否是结构体
  82. if objType.Kind() != reflect.Struct {
  83. return "", fmt.Errorf("参数必须是结构体或结构体指针")
  84. }
  85. // 获取表名(结构体名转换为下划线)
  86. tableName := objType.Name()
  87. tableName = humpToUnderline(tableName)
  88. // 查找主键字段
  89. primaryKeyField := ""
  90. primaryKeyValue := interface{}(nil)
  91. columnName := ""
  92. // 遍历结构体字段
  93. for i := 0; i < objType.NumField(); i++ {
  94. field := objType.Field(i)
  95. // 检查 TableKey 标签
  96. if tableKey := field.Tag.Get(tagTableKey); tableKey == "true" {
  97. // 获取字段值
  98. fieldValue := getRealValue(objValue.Field(i).Interface())
  99. primaryKeyField = field.Name
  100. primaryKeyValue = fieldValue
  101. if tableKey := field.Tag.Get(tagDb); tableKey != "-" {
  102. columnName = tableKey
  103. }
  104. break
  105. }
  106. }
  107. // 如果没有找到带标签的主键,尝试查找名为"id"的字段
  108. if primaryKeyField == "" {
  109. // 先尝试查找导出字段"Id"
  110. if field, ok := objType.FieldByName("Id"); ok {
  111. fieldValue := getRealValue(objValue.FieldByName("Id").Interface())
  112. primaryKeyField = field.Name
  113. primaryKeyValue = fieldValue
  114. if tableKey := field.Tag.Get(tagDb); tableKey != "-" {
  115. columnName = tableKey
  116. }
  117. } else if field, ok := objType.FieldByName("ID"); ok {
  118. // 尝试查找"ID"
  119. fieldValue := getRealValue(objValue.FieldByName("ID").Interface())
  120. primaryKeyField = field.Name
  121. primaryKeyValue = fieldValue
  122. if tableKey := field.Tag.Get(tagDb); tableKey != "-" {
  123. columnName = tableKey
  124. }
  125. } else {
  126. // 尝试查找小写"id"(非导出字段,通过CanInterface检查)
  127. for i := 0; i < objType.NumField(); i++ {
  128. field := objType.Field(i)
  129. if strings.ToLower(field.Name) == "id" {
  130. fieldValue := objValue.Field(i)
  131. if fieldValue.CanInterface() {
  132. primaryKeyField = field.Name
  133. primaryKeyValue = getRealValue(fieldValue.Interface())
  134. if tableKey := field.Tag.Get(tagDb); tableKey != "-" {
  135. columnName = tableKey
  136. }
  137. break
  138. }
  139. }
  140. }
  141. }
  142. }
  143. // 如果仍然没有找到主键字段,返回错误
  144. if primaryKeyField == "" {
  145. return "", fmt.Errorf("未找到主键字段")
  146. }
  147. // 如果主键值为空,返回错误
  148. if primaryKeyValue == nil {
  149. return "", fmt.Errorf("主键值为空")
  150. }
  151. if columnName == "" {
  152. columnName = humpToUnderline(primaryKeyField)
  153. }
  154. // 根据值类型决定是否添加引号
  155. sql := fmt.Sprintf("select * from %s where %s = ", tableName, columnName)
  156. // 根据值类型添加引号
  157. sql += formatWhereCondition(primaryKeyValue)
  158. return sql + " limit 1", nil
  159. }
  160. // FormatSelectCountSql 根据对象非空字段生成COUNT SQL语句
  161. func FormatSelectCountSql(obj interface{}) (string, error) {
  162. // 获取对象类型和值
  163. objType := reflect.TypeOf(obj)
  164. objValue := reflect.ValueOf(obj)
  165. // 如果是指针,获取指向的值
  166. if objType.Kind() == reflect.Ptr {
  167. objType = objType.Elem()
  168. objValue = objValue.Elem()
  169. }
  170. // 检查是否是结构体
  171. if objType.Kind() != reflect.Struct {
  172. return "", fmt.Errorf("参数必须是结构体或结构体指针")
  173. }
  174. // 获取表名(结构体名转换为下划线)
  175. tableName := objType.Name()
  176. tableName = humpToUnderline(tableName)
  177. // 构建WHERE条件
  178. var whereConditions []string
  179. // 遍历结构体字段
  180. for i := 0; i < objType.NumField(); i++ {
  181. field := objType.Field(i)
  182. fieldValue := objValue.Field(i)
  183. if db := field.Tag.Get(tagDb); db != "" && db == "-" {
  184. // 跳过非表字段
  185. continue
  186. }
  187. // 检查字段值是否为空
  188. if isEmptyValue(fieldValue) {
  189. continue
  190. }
  191. // 获取字段值(处理指针)
  192. actualValue := getRealValue(fieldValue.Interface())
  193. // 获取字段名并转换为下划线
  194. column := field.Tag.Get(tagDb)
  195. // 构建条件
  196. condition := fmt.Sprintf("`%s` = ", column)
  197. // 根据字段类型决定是否添加引号
  198. condition += formatWhereCondition(actualValue)
  199. whereConditions = append(whereConditions, condition)
  200. }
  201. // 构建完整的SQL
  202. sql := fmt.Sprintf("select count(1) from %s", tableName)
  203. if len(whereConditions) > 0 {
  204. sql += " where 1=1"
  205. for _, condition := range whereConditions {
  206. sql += " and " + condition
  207. }
  208. }
  209. return sql, nil
  210. }
  211. // FormatSelectCountSqlByMap 根据类和参数map生成COUNT SQL语句
  212. func FormatSelectCountSqlByMap(clazz interface{}, params map[string]interface{}) (string, error) {
  213. // 获取类型
  214. var objType reflect.Type
  215. switch v := clazz.(type) {
  216. case reflect.Type:
  217. objType = v
  218. default:
  219. objType = reflect.TypeOf(clazz)
  220. }
  221. // 如果是指针,获取指向的类型
  222. if objType.Kind() == reflect.Ptr {
  223. objType = objType.Elem()
  224. }
  225. // 获取表名(类型名转换为下划线)
  226. tableName := objType.Name()
  227. tableName = humpToUnderline(tableName)
  228. // 构建WHERE条件
  229. whereSQL, err := BuildWhereCondition(params)
  230. if err != nil {
  231. return "", err
  232. }
  233. // 构建完整的SQL
  234. sql := fmt.Sprintf("select count(1) from %s%s", tableName, whereSQL)
  235. return sql, nil
  236. }
  237. // FormatSelectSql 根据对象非空字段生成SELECT SQL语句(限制1条)
  238. func FormatSelectSql(obj interface{}) (string, error) {
  239. // 获取对象类型和值
  240. objType := reflect.TypeOf(obj)
  241. objValue := reflect.ValueOf(obj)
  242. // 如果是指针,获取指向的值
  243. if objType.Kind() == reflect.Ptr {
  244. objType = objType.Elem()
  245. objValue = objValue.Elem()
  246. }
  247. // 检查是否是结构体
  248. if objType.Kind() != reflect.Struct {
  249. return "", fmt.Errorf("参数必须是结构体或结构体指针")
  250. }
  251. // 获取表名(结构体名转换为下划线)
  252. tableName := objType.Name()
  253. tableName = humpToUnderline(tableName)
  254. // 构建WHERE条件
  255. var whereConditions []string
  256. // 遍历结构体字段
  257. for i := 0; i < objType.NumField(); i++ {
  258. field := objType.Field(i)
  259. fieldValue := objValue.Field(i)
  260. if db := field.Tag.Get(tagDb); db != "" && db == "-" {
  261. continue
  262. }
  263. // 检查字段值是否为空
  264. if isEmptyValue(fieldValue) {
  265. continue
  266. }
  267. // 获取字段值(处理指针)
  268. actualValue := getRealValue(fieldValue.Interface())
  269. // 获取字段名并转换为下划线
  270. column := field.Tag.Get(tagDb)
  271. // 构建条件
  272. condition := fmt.Sprintf("`%s` = ", column)
  273. // 根据字段类型决定是否添加引号
  274. condition += formatWhereCondition(actualValue)
  275. whereConditions = append(whereConditions, condition)
  276. }
  277. // 检查是否有条件
  278. if len(whereConditions) == 0 {
  279. return "", fmt.Errorf("关键参数缺失")
  280. }
  281. // 构建完整的SQL
  282. sql := fmt.Sprintf("select * from %s where 1=1", tableName)
  283. for _, condition := range whereConditions {
  284. sql += " and " + condition
  285. }
  286. return sql + " limit 1", nil
  287. }
  288. // FormatSelectSqlByMap 根据参数map和类生成SELECT SQL语句(限制1条)
  289. func FormatSelectSqlByMap(params map[string]interface{}, clazz interface{}) (string, error) {
  290. // 获取类型
  291. var objType reflect.Type
  292. switch v := clazz.(type) {
  293. case reflect.Type:
  294. objType = v
  295. default:
  296. objType = reflect.TypeOf(clazz)
  297. }
  298. // 如果是指针,获取指向的类型
  299. if objType.Kind() == reflect.Ptr {
  300. objType = objType.Elem()
  301. }
  302. // 获取表名(类型名转换为下划线)
  303. tableName := objType.Name()
  304. tableName = humpToUnderline(tableName)
  305. // 构建WHERE条件
  306. whereSQL, err := BuildWhereCondition(params)
  307. if err != nil {
  308. return "", err
  309. }
  310. // 检查是否有条件(排除默认的1=1)
  311. if whereSQL == " WHERE 1=1" {
  312. return "", fmt.Errorf("关键参数缺失")
  313. }
  314. // 构建完整的SQL
  315. sql := fmt.Sprintf("select * from %s%s limit 1", tableName, whereSQL)
  316. return sql, nil
  317. }
  318. // FormatSelectSqlByMapAndSql 根据参数map和基础SQL生成SELECT SQL语句
  319. func FormatSelectSqlByMapAndSql(params map[string]interface{}, baseSql string) (string, error) {
  320. // 构建WHERE条件
  321. whereSQL, err := BuildWhereCondition(params)
  322. if err != nil {
  323. return "", err
  324. }
  325. // 组合SQL
  326. sql := baseSql + whereSQL
  327. return sql, nil
  328. }
  329. // FormatInsertSql 根据对象生成INSERT SQL语句(修复指针和自增ID问题)
  330. func FormatInsertSql(obj interface{}, uuid string) (string, error) {
  331. // 获取对象类型和值
  332. objType := reflect.TypeOf(obj)
  333. objValue := reflect.ValueOf(obj)
  334. // 如果是指针,获取指向的值
  335. if objType.Kind() == reflect.Ptr {
  336. objType = objType.Elem()
  337. objValue = objValue.Elem()
  338. }
  339. // 检查是否是结构体
  340. if objType.Kind() != reflect.Struct {
  341. return "", fmt.Errorf("参数必须是结构体或结构体指针")
  342. }
  343. // 获取表名(结构体名转换为下划线)
  344. tableName := objType.Name()
  345. tableName = humpToUnderline(tableName)
  346. // 构建列名和值的列表
  347. var columns []string
  348. var values []string
  349. // 遍历结构体字段
  350. for i := 0; i < objType.NumField(); i++ {
  351. field := objType.Field(i)
  352. fieldValue := objValue.Field(i)
  353. if db := field.Tag.Get(tagDb); db != "" && db == "-" {
  354. continue
  355. }
  356. // 处理特殊字段
  357. fieldName := field.Name
  358. // 处理id字段 - 特别处理自增ID
  359. if strings.ToLower(fieldName) == "id" {
  360. // 获取实际值
  361. actualValue := getRealValue(fieldValue.Interface())
  362. // 检查是否为自增ID
  363. idType := field.Tag.Get(tagIdType)
  364. if idType == string(IdTypeAuto) {
  365. // 自增ID,如果值为0或nil则跳过
  366. if actualValue == nil {
  367. continue
  368. }
  369. if intVal, ok := actualValue.(int64); ok && intVal == 0 {
  370. continue
  371. }
  372. if intVal, ok := actualValue.(int32); ok && intVal == 0 {
  373. continue
  374. }
  375. if intVal, ok := actualValue.(int); ok && intVal == 0 {
  376. continue
  377. }
  378. } else if idType == string(IdTypeUUID) || idType == "" {
  379. // UUID或未指定,检查值是否为空
  380. if actualValue == nil {
  381. // 生成UUID
  382. uuidStr := uuid
  383. columns = append(columns, fmt.Sprintf("`%s`", humpToUnderline(fieldName)))
  384. values = append(values, fmt.Sprintf("'%s'", escapeSQLString(uuidStr)))
  385. continue
  386. }
  387. }
  388. }
  389. // 处理时间字段
  390. if strings.ToLower(fieldName) == "createtime" || strings.ToLower(fieldName) == "updatetime" {
  391. // 获取实际值
  392. actualValue := getRealValue(fieldValue.Interface())
  393. if actualValue == nil {
  394. // 设置为当前时间
  395. now := time.Now()
  396. create_time := now.Format("2006-01-02 15:04:05")
  397. columns = append(columns, fmt.Sprintf("`%s`", humpToUnderline(fieldName)))
  398. if field.Type.String() == "time.Time" {
  399. values = append(values, fmt.Sprintf("'%s'", create_time))
  400. } else {
  401. values = append(values, fmt.Sprintf("'%s'", create_time))
  402. }
  403. continue
  404. }
  405. }
  406. if strings.HasSuffix(fieldName, "JsonList") && fieldValue.Type() == reflect.TypeOf((*[]string)(nil)) {
  407. actualValue := FormatToString(fieldValue.Interface())
  408. if len(actualValue) > 0 {
  409. jsonBytes, err := json.Marshal(actualValue)
  410. if err != nil {
  411. build_time_str := time.Now().Format(time.DateTime)
  412. _, file, line, _ := runtime.Caller(0)
  413. fmt.Printf("%s JSON编码失败: %s %d======> %v\n", build_time_str, file, line, err)
  414. continue
  415. }
  416. jsonStr := string(jsonBytes)
  417. column := field.Tag.Get(tagDb)
  418. columns = append(columns, fmt.Sprintf("`%s`", column))
  419. values = append(values, fmt.Sprintf("'%s'", jsonStr))
  420. continue
  421. }
  422. }
  423. // 获取字段值(处理指针)
  424. actualValue := getRealValue(fieldValue.Interface())
  425. // if actualValue == nil {
  426. // 指针为nil,跳过或插入NULL,根据需求决定
  427. // 这里选择跳过,如果你需要插入NULL,可以取消下面的注释
  428. // columns = append(columns, fmt.Sprintf("`%s`", humpToUnderline(fieldName)))
  429. // values = append(values, "NULL")
  430. // continue
  431. // }
  432. // SQL注入检测
  433. // if containsSQLInjection(actualValue) {
  434. // return "", fmt.Errorf("非法参数: 字段 %s 包含SQL注入关键词", fieldName)
  435. // }
  436. // 获取列名(驼峰转下划线)
  437. column := field.Tag.Get(tagDb)
  438. columns = append(columns, fmt.Sprintf("`%s`", column))
  439. // 根据字段类型构建值
  440. valueStr := formatValue(actualValue)
  441. values = append(values, valueStr)
  442. }
  443. // 检查是否有列
  444. if len(columns) == 0 {
  445. return "", fmt.Errorf("没有有效的字段可以插入")
  446. }
  447. // 构建SQL
  448. sql := fmt.Sprintf("insert into %s (%s) values (%s)",
  449. tableName,
  450. strings.Join(columns, ", "),
  451. strings.Join(values, ", "))
  452. return sql, nil
  453. }
  454. // FormatUpdateByIdSql 根据对象主键生成UPDATE SQL语句
  455. func FormatUpdateByIdSql(obj interface{}) (string, error) {
  456. // 获取对象类型和值
  457. objType := reflect.TypeOf(obj)
  458. objValue := reflect.ValueOf(obj)
  459. // 如果是指针,获取指向的值
  460. if objType.Kind() == reflect.Ptr {
  461. objType = objType.Elem()
  462. objValue = objValue.Elem()
  463. }
  464. // 检查是否是结构体
  465. if objType.Kind() != reflect.Struct {
  466. return "", fmt.Errorf("参数必须是结构体或结构体指针")
  467. }
  468. // 查找主键字段和值
  469. primaryKeyField := ""
  470. primaryKeyValue := interface{}(nil)
  471. columnName := ""
  472. // 遍历结构体字段查找主键
  473. for i := 0; i < objType.NumField(); i++ {
  474. field := objType.Field(i)
  475. // 检查 TableKey 标签
  476. if tableKey := field.Tag.Get(tagTableKey); tableKey == "true" {
  477. fieldValue := getRealValue(objValue.Field(i).Interface())
  478. primaryKeyField = field.Name
  479. primaryKeyValue = fieldValue
  480. if tableKey := field.Tag.Get(tagDb); tableKey != "-" {
  481. columnName = tableKey
  482. }
  483. break
  484. }
  485. }
  486. // 如果没有找到带标签的主键,尝试查找名为"id"的字段
  487. if primaryKeyField == "" {
  488. // 先尝试查找导出字段"Id"
  489. if field, ok := objType.FieldByName("Id"); ok {
  490. fieldValue := getRealValue(objValue.FieldByName("Id").Interface())
  491. primaryKeyField = field.Name
  492. primaryKeyValue = fieldValue
  493. if tableKey := field.Tag.Get(tagDb); tableKey != "-" {
  494. columnName = tableKey
  495. }
  496. } else if field, ok := objType.FieldByName("ID"); ok {
  497. // 尝试查找"ID"
  498. fieldValue := getRealValue(objValue.FieldByName("ID").Interface())
  499. primaryKeyField = field.Name
  500. primaryKeyValue = fieldValue
  501. if tableKey := field.Tag.Get(tagDb); tableKey != "-" {
  502. columnName = tableKey
  503. }
  504. } else {
  505. // 尝试查找小写"id"(非导出字段,通过CanInterface检查)
  506. for i := 0; i < objType.NumField(); i++ {
  507. field := objType.Field(i)
  508. if strings.ToLower(field.Name) == "id" {
  509. fieldValue := objValue.Field(i)
  510. if fieldValue.CanInterface() {
  511. primaryKeyField = field.Name
  512. primaryKeyValue = getRealValue(fieldValue.Interface())
  513. if tableKey := field.Tag.Get(tagDb); tableKey != "-" {
  514. columnName = tableKey
  515. }
  516. break
  517. }
  518. }
  519. }
  520. }
  521. }
  522. // 如果仍然没有找到主键字段,返回错误
  523. if primaryKeyField == "" {
  524. return "", fmt.Errorf("未找到主键字段")
  525. }
  526. // 如果主键值为空,返回错误
  527. if primaryKeyValue == nil {
  528. return "", fmt.Errorf("主键值为空")
  529. }
  530. if columnName == "" {
  531. columnName = humpToUnderline(primaryKeyField)
  532. }
  533. // 创建WHERE条件map
  534. whereMap := map[string]interface{}{
  535. columnName: primaryKeyValue,
  536. }
  537. // 调用FormatUpdateSql
  538. return FormatUpdateSql(obj, whereMap)
  539. }
  540. // FormatUpdateSql 根据对象和WHERE条件map生成UPDATE SQL语句
  541. func FormatUpdateSql(obj interface{}, whereMap map[string]interface{}) (string, error) {
  542. // 获取对象类型和值
  543. objType := reflect.TypeOf(obj)
  544. objValue := reflect.ValueOf(obj)
  545. // 如果是指针,获取指向的值
  546. if objType.Kind() == reflect.Ptr {
  547. objType = objType.Elem()
  548. objValue = objValue.Elem()
  549. }
  550. // 检查是否是结构体
  551. if objType.Kind() != reflect.Struct {
  552. return "", fmt.Errorf("参数必须是结构体或结构体指针")
  553. }
  554. // 获取表名(结构体名转换为下划线)
  555. tableName := objType.Name()
  556. tableName = humpToUnderline(tableName)
  557. // 构建SET子句
  558. var setClauses []string
  559. // 遍历结构体字段
  560. for i := 0; i < objType.NumField(); i++ {
  561. field := objType.Field(i)
  562. fieldValue := objValue.Field(i)
  563. fieldName := field.Name
  564. if db := field.Tag.Get(tagDb); db == "-" {
  565. // 检查是否为emptyField字段(类型为[]string或*[]string)
  566. if strings.ToLower(fieldName) == "emptyfield" {
  567. // 获取字段值
  568. actualValue := getRealValue(fieldValue.Interface())
  569. if actualValue != nil {
  570. // 尝试将actualValue转换为[]string
  571. var strSlice []string
  572. // 判断actualValue的类型
  573. switch v := actualValue.(type) {
  574. case []string:
  575. strSlice = v
  576. case *[]string:
  577. if v != nil {
  578. strSlice = *v
  579. } else {
  580. continue
  581. }
  582. default:
  583. // 如果不是这两种类型,跳过
  584. continue
  585. }
  586. if len(strSlice) == 0 {
  587. continue
  588. } else {
  589. // 遍历切片中的每个字段名,生成条件并添加到setClauses
  590. for _, fieldNameToSet := range strSlice {
  591. // 用反引号括起字段名,避免SQL关键字冲突
  592. columnName := humpToUnderline(fieldNameToSet)
  593. condition := fmt.Sprintf("`%s` = NULL", columnName)
  594. setClauses = append(setClauses, condition)
  595. }
  596. }
  597. // 处理完emptyField后继续下一个字段
  598. continue
  599. }
  600. }
  601. continue
  602. }
  603. // 跳过主键字段
  604. if strings.ToLower(fieldName) == "id" ||
  605. field.Tag.Get(tagTableKey) == "true" {
  606. continue
  607. }
  608. // 处理updateTime字段
  609. if strings.ToLower(fieldName) == "updatetime" {
  610. // 获取实际值
  611. actualValue := getRealValue(fieldValue.Interface())
  612. if actualValue == nil {
  613. // 设置为当前时间
  614. now := time.Now()
  615. // 获取列名(驼峰转下划线)
  616. column := field.Tag.Get(tagDb)
  617. // 构建SET子句
  618. timeStr := now.Format("2006-01-02 15:04:05")
  619. setClause := fmt.Sprintf("`%s` = '%s'", column, timeStr)
  620. setClauses = append(setClauses, setClause)
  621. continue
  622. }
  623. }
  624. if strings.HasSuffix(fieldName, "JsonList") && fieldValue.Type() == reflect.TypeOf((*[]string)(nil)) {
  625. actualValue := FormatToString(fieldValue.Interface())
  626. if len(actualValue) > 0 {
  627. jsonBytes, err := json.Marshal(actualValue)
  628. if err != nil {
  629. build_time_str := time.Now().Format(time.DateTime)
  630. _, file, line, _ := runtime.Caller(0)
  631. fmt.Printf("%s JSON编码失败: %s %d======> %v\n", build_time_str, file, line, err)
  632. continue
  633. }
  634. jsonStr := string(jsonBytes)
  635. column := field.Tag.Get(tagDb)
  636. setClause := fmt.Sprintf("`%s` = '%s'", column, jsonStr)
  637. setClauses = append(setClauses, setClause)
  638. continue
  639. }
  640. }
  641. // 检查字段值是否为空
  642. if isEmptyValue(fieldValue) {
  643. continue
  644. }
  645. // 获取字段值(处理指针)
  646. actualValue := getRealValue(fieldValue.Interface())
  647. // SQL注入检测
  648. // if containsSQLInjection(actualValue) {
  649. // return "", fmt.Errorf("非法参数: 字段 %s 包含SQL注入关键词", fieldName)
  650. // }
  651. // 获取列名(驼峰转下划线)
  652. column := field.Tag.Get(tagDb)
  653. // 构建SET子句
  654. valueStr := formatValue(actualValue)
  655. setClause := fmt.Sprintf("`%s` = %s", column, valueStr)
  656. setClauses = append(setClauses, setClause)
  657. }
  658. // 检查是否有SET子句
  659. if len(setClauses) == 0 {
  660. return "", fmt.Errorf("没有有效的字段可以更新")
  661. }
  662. // 检查WHERE条件
  663. if len(whereMap) == 0 {
  664. return "", fmt.Errorf("关键参数缺失: WHERE条件不能为空")
  665. }
  666. // 构建WHERE条件
  667. whereSQL, err := BuildWhereCondition(whereMap)
  668. if err != nil {
  669. return "", err
  670. }
  671. // 检查WHERE条件是否有效(排除默认的1=1)
  672. if whereSQL == " WHERE 1=1" {
  673. return "", fmt.Errorf("关键参数缺失: WHERE条件无效")
  674. }
  675. // 构建SQL
  676. sql := fmt.Sprintf("update %s set %s%s",
  677. tableName,
  678. strings.Join(setClauses, ", "),
  679. whereSQL)
  680. return sql, nil
  681. }
  682. // FormatRemoveByIdSql 根据对象主键生成DELETE SQL语句
  683. func FormatRemoveByIdSql(obj interface{}) (string, error) {
  684. // 获取对象类型和值
  685. objType := reflect.TypeOf(obj)
  686. objValue := reflect.ValueOf(obj)
  687. // 如果是指针,获取指向的值
  688. if objType.Kind() == reflect.Ptr {
  689. objType = objType.Elem()
  690. objValue = objValue.Elem()
  691. }
  692. // 检查是否是结构体
  693. if objType.Kind() != reflect.Struct {
  694. return "", fmt.Errorf("参数必须是结构体或结构体指针")
  695. }
  696. // 获取表名(结构体名转换为下划线)
  697. tableName := objType.Name()
  698. tableName = humpToUnderline(tableName)
  699. // 查找主键字段
  700. primaryKeyField := ""
  701. primaryKeyValue := interface{}(nil)
  702. columnName := ""
  703. // 遍历结构体字段
  704. for i := 0; i < objType.NumField(); i++ {
  705. field := objType.Field(i)
  706. // 检查 TableKey 标签
  707. if tableKey := field.Tag.Get(tagTableKey); tableKey == "true" {
  708. // 获取字段值
  709. fieldValue := getRealValue(objValue.Field(i).Interface())
  710. primaryKeyField = field.Name
  711. primaryKeyValue = fieldValue
  712. if tableKey := field.Tag.Get(tagDb); tableKey != "-" {
  713. columnName = tableKey
  714. }
  715. break
  716. }
  717. }
  718. // 如果没有找到带标签的主键,尝试查找名为"id"的字段
  719. if primaryKeyField == "" {
  720. // 先尝试查找导出字段"Id"
  721. if field, ok := objType.FieldByName("Id"); ok {
  722. fieldValue := getRealValue(objValue.FieldByName("Id").Interface())
  723. primaryKeyField = field.Name
  724. primaryKeyValue = fieldValue
  725. if tableKey := field.Tag.Get(tagDb); tableKey != "-" {
  726. columnName = tableKey
  727. }
  728. } else if field, ok := objType.FieldByName("ID"); ok {
  729. // 尝试查找"ID"
  730. fieldValue := getRealValue(objValue.FieldByName("ID").Interface())
  731. primaryKeyField = field.Name
  732. primaryKeyValue = fieldValue
  733. if tableKey := field.Tag.Get(tagDb); tableKey != "-" {
  734. columnName = tableKey
  735. }
  736. } else {
  737. // 尝试查找小写"id"(非导出字段,通过CanInterface检查)
  738. for i := 0; i < objType.NumField(); i++ {
  739. field := objType.Field(i)
  740. if strings.ToLower(field.Name) == "id" {
  741. fieldValue := objValue.Field(i)
  742. if fieldValue.CanInterface() {
  743. primaryKeyField = field.Name
  744. primaryKeyValue = getRealValue(fieldValue.Interface())
  745. if tableKey := field.Tag.Get(tagDb); tableKey != "-" {
  746. columnName = tableKey
  747. }
  748. break
  749. }
  750. }
  751. }
  752. }
  753. }
  754. // 如果仍然没有找到主键字段,返回错误
  755. if primaryKeyField == "" {
  756. return "", fmt.Errorf("未找到主键字段")
  757. }
  758. // 如果主键值为空,返回错误
  759. if primaryKeyValue == nil {
  760. return "", fmt.Errorf("主键值为空")
  761. }
  762. if columnName == "" {
  763. columnName = humpToUnderline(primaryKeyField)
  764. }
  765. // 根据值类型决定是否添加引号
  766. sql := fmt.Sprintf("delete from %s where %s = ", tableName, columnName)
  767. // 根据值类型添加引号
  768. sql += formatWhereCondition(primaryKeyValue)
  769. return sql, nil
  770. }
  771. // FormatRemoveSql 根据对象非空字段生成DELETE SQL语句
  772. func FormatRemoveSql(obj interface{}) (string, error) {
  773. // 获取对象类型和值
  774. objType := reflect.TypeOf(obj)
  775. objValue := reflect.ValueOf(obj)
  776. // 如果是指针,获取指向的值
  777. if objType.Kind() == reflect.Ptr {
  778. objType = objType.Elem()
  779. objValue = objValue.Elem()
  780. }
  781. // 检查是否是结构体
  782. if objType.Kind() != reflect.Struct {
  783. return "", fmt.Errorf("参数必须是结构体或结构体指针")
  784. }
  785. // 获取表名(结构体名转换为下划线)
  786. tableName := objType.Name()
  787. tableName = humpToUnderline(tableName)
  788. // 将对象转换为map
  789. paramMap := objectToMap(obj)
  790. // 构建WHERE条件
  791. whereSQL, err := BuildWhereCondition(paramMap)
  792. if err != nil {
  793. return "", err
  794. }
  795. // 检查是否有条件(排除默认的1=1)
  796. if whereSQL == " WHERE 1=1" {
  797. return "", fmt.Errorf("关键参数缺失")
  798. }
  799. // 构建完整的SQL
  800. sql := fmt.Sprintf("delete from %s%s", tableName, whereSQL)
  801. return sql, nil
  802. }
  803. // getRealValue 获取参数的实际值(处理指针)
  804. func getRealValue(value interface{}) interface{} {
  805. if value == nil {
  806. return nil
  807. }
  808. v := reflect.ValueOf(value)
  809. // 如果是指针类型,获取指针指向的值
  810. if v.Kind() == reflect.Ptr {
  811. if v.IsNil() {
  812. return nil
  813. }
  814. // 递归调用,获取指针指向的实际值
  815. return getRealValue(v.Elem().Interface())
  816. }
  817. return value
  818. }
  819. // processCondition 处理单个条件
  820. func (b *SQLBuilder) processCondition(key string, value interface{}) error {
  821. if key == "" || value == nil {
  822. return nil
  823. }
  824. // 获取实际值(处理指针)
  825. realValue := getRealValue(value)
  826. // SQL注入检测
  827. if containsSQLInjection(realValue) && key != suffixApplySQL {
  828. println("非法参数", key, toString(realValue))
  829. return fmt.Errorf("非法参数")
  830. }
  831. column := humpToUnderline(key)
  832. // 根据后缀处理不同的条件类型
  833. switch {
  834. case strings.HasSuffix(column, suffixBegin):
  835. b.addRangeCondition(column, realValue, ">=", suffixBegin)
  836. case strings.HasSuffix(column, suffixEnd):
  837. b.addRangeCondition(column, realValue, "<=", suffixEnd)
  838. case strings.HasSuffix(column, suffixLike):
  839. b.addLikeCondition(column, realValue)
  840. case strings.HasSuffix(column, suffixNot):
  841. b.addNotEqualCondition(column, realValue)
  842. case strings.HasSuffix(column, suffixIn):
  843. b.addInCondition(column, realValue, false)
  844. case strings.HasSuffix(column, suffixNotIn):
  845. b.addInCondition(column, realValue, true)
  846. case strings.HasSuffix(column, suffixNull):
  847. b.addNullCondition(column, false)
  848. case strings.HasSuffix(column, suffixNotNull):
  849. b.addNullCondition(column, true)
  850. case column == suffixApplySQL:
  851. b.addCustomSQLCondition(realValue)
  852. default:
  853. b.addEqualCondition(column, realValue)
  854. }
  855. return nil
  856. }
  857. // addRangeCondition 添加范围条件
  858. func (b *SQLBuilder) addRangeCondition(column string, value interface{}, operator, suffix string) {
  859. col := strings.TrimSuffix(column, suffix)
  860. condition := fmt.Sprintf("%s %s %s", quoteColumn(col), operator, formatValue(value))
  861. b.conditions = append(b.conditions, condition)
  862. }
  863. // addLikeCondition 添加LIKE条件
  864. func (b *SQLBuilder) addLikeCondition(column string, value interface{}) {
  865. col := strings.TrimSuffix(column, suffixLike)
  866. condition := fmt.Sprintf("%s LIKE '%%%s%%'", quoteColumn(col), escapeSQLString(toString(value)))
  867. b.conditions = append(b.conditions, condition)
  868. }
  869. // addNotEqualCondition 添加不等于条件
  870. func (b *SQLBuilder) addNotEqualCondition(column string, value interface{}) {
  871. col := strings.TrimSuffix(column, suffixNot)
  872. condition := fmt.Sprintf("%s != %s", quoteColumn(col), formatValue(value))
  873. b.conditions = append(b.conditions, condition)
  874. }
  875. // addInCondition 添加IN/NOT IN条件
  876. func (b *SQLBuilder) addInCondition(column string, value interface{}, isNotIn bool) {
  877. // 获取实际值(处理指针)
  878. realValue := getRealValue(value)
  879. // 尝试将值转换为切片
  880. var list []interface{}
  881. switch v := realValue.(type) {
  882. case []interface{}:
  883. list = v
  884. case []string:
  885. list = make([]interface{}, len(v))
  886. for i, item := range v {
  887. list[i] = item
  888. }
  889. case []int:
  890. list = make([]interface{}, len(v))
  891. for i, item := range v {
  892. list[i] = item
  893. }
  894. case []int64:
  895. list = make([]interface{}, len(v))
  896. for i, item := range v {
  897. list[i] = item
  898. }
  899. case []float64:
  900. list = make([]interface{}, len(v))
  901. for i, item := range v {
  902. list[i] = item
  903. }
  904. default:
  905. // 如果不是切片类型,直接返回
  906. return
  907. }
  908. // 处理切片中的指针元素
  909. for i, item := range list {
  910. list[i] = getRealValue(item)
  911. }
  912. if len(list) == 0 {
  913. list = []interface{}{"null"}
  914. }
  915. col := strings.TrimSuffix(column, func() string {
  916. if isNotIn {
  917. return suffixNotIn
  918. }
  919. return suffixIn
  920. }())
  921. // 格式化每个值
  922. formattedValues := make([]string, len(list))
  923. for i, item := range list {
  924. formattedValues[i] = formatValue(item)
  925. }
  926. operator := "IN"
  927. if isNotIn {
  928. operator = "NOT IN"
  929. }
  930. condition := fmt.Sprintf("%s %s (%s)", quoteColumn(col), operator, strings.Join(formattedValues, ","))
  931. b.conditions = append(b.conditions, condition)
  932. }
  933. // addNullCondition 添加NULL条件
  934. func (b *SQLBuilder) addNullCondition(column string, isNotNull bool) {
  935. col := strings.TrimSuffix(column, func() string {
  936. if isNotNull {
  937. return suffixNotNull
  938. }
  939. return suffixNull
  940. }())
  941. operator := "IS NULL"
  942. if isNotNull {
  943. operator = "IS NOT NULL"
  944. }
  945. condition := fmt.Sprintf("%s %s", quoteColumn(col), operator)
  946. b.conditions = append(b.conditions, condition)
  947. }
  948. // addCustomSQLCondition 添加自定义SQL条件
  949. func (b *SQLBuilder) addCustomSQLCondition(value interface{}) {
  950. // 注意:自定义SQL条件不使用参数化,需要调用者确保安全
  951. condition := fmt.Sprintf("(%s)", toString(value))
  952. b.conditions = append(b.conditions, condition)
  953. }
  954. // addEqualCondition 添加等于条件
  955. func (b *SQLBuilder) addEqualCondition(column string, value interface{}) {
  956. condition := fmt.Sprintf("%s = %s", quoteColumn(column), formatValue(value))
  957. b.conditions = append(b.conditions, condition)
  958. }
  959. // extractPageParams 提取分页、排序、分组参数
  960. func (b *SQLBuilder) extractPageParams(params map[string]interface{}) {
  961. // 处理分页
  962. if current, ok := params["current"]; ok {
  963. currentInt := 0
  964. switch v := current.(type) {
  965. case int:
  966. currentInt = v
  967. case float64:
  968. currentInt = int(v)
  969. case string:
  970. if val, err := strconv.Atoi(v); err == nil {
  971. currentInt = val
  972. }
  973. }
  974. if size, ok := params["size"]; ok && currentInt > 0 {
  975. sizeInt := 0
  976. switch v := size.(type) {
  977. case int:
  978. sizeInt = v
  979. case float64:
  980. sizeInt = int(v)
  981. case string:
  982. if val, err := strconv.Atoi(v); err == nil {
  983. sizeInt = val
  984. }
  985. }
  986. if sizeInt > 0 {
  987. offset := (currentInt - 1) * sizeInt
  988. b.limit = fmt.Sprintf(" LIMIT %d, %d", offset, sizeInt)
  989. delete(params, "current")
  990. delete(params, "size")
  991. }
  992. }
  993. }
  994. // 处理排序
  995. if orderBy, ok := params["orderBy"]; ok {
  996. orderByStr := toString(orderBy)
  997. orders := strings.Split(orderByStr, ",")
  998. orderClauses := make([]string, 0, len(orders))
  999. for _, order := range orders {
  1000. parts := strings.Split(order, "_")
  1001. if len(parts) == 2 {
  1002. col := humpToUnderline(parts[0])
  1003. orderClauses = append(orderClauses, fmt.Sprintf("%s %s", quoteColumn(col), parts[1]))
  1004. }
  1005. }
  1006. if len(orderClauses) > 0 {
  1007. b.orderBy = " ORDER BY " + strings.Join(orderClauses, ", ")
  1008. }
  1009. delete(params, "orderBy")
  1010. }
  1011. // 处理分组
  1012. if groupBy, ok := params["groupBy"]; ok {
  1013. groupByStr := toString(groupBy)
  1014. groups := strings.Split(groupByStr, "-")
  1015. groupClauses := make([]string, 0, len(groups))
  1016. for _, group := range groups {
  1017. col := humpToUnderline(group)
  1018. groupClauses = append(groupClauses, quoteColumn(col))
  1019. }
  1020. if len(groupClauses) > 0 {
  1021. b.groupBy = " GROUP BY " + strings.Join(groupClauses, ", ")
  1022. }
  1023. delete(params, "groupBy")
  1024. }
  1025. }
  1026. // buildSQL 构建完整的SQL语句
  1027. func (b *SQLBuilder) buildSQL() string {
  1028. var sql strings.Builder
  1029. if len(b.conditions) > 0 {
  1030. sql.WriteString(" WHERE 1=1")
  1031. for _, condition := range b.conditions {
  1032. sql.WriteString(" AND ")
  1033. sql.WriteString(condition)
  1034. }
  1035. }
  1036. sql.WriteString(b.orderBy)
  1037. sql.WriteString(b.groupBy)
  1038. sql.WriteString(b.limit)
  1039. return sql.String()
  1040. }
  1041. // objectToMap 将对象转换为map[string]interface{}
  1042. func objectToMap(obj interface{}) map[string]interface{} {
  1043. result := make(map[string]interface{})
  1044. // 获取对象类型和值
  1045. objType := reflect.TypeOf(obj)
  1046. objValue := reflect.ValueOf(obj)
  1047. // 如果是指针,获取指向的值
  1048. if objType.Kind() == reflect.Ptr {
  1049. objType = objType.Elem()
  1050. objValue = objValue.Elem()
  1051. }
  1052. // 遍历结构体字段
  1053. for i := 0; i < objType.NumField(); i++ {
  1054. field := objType.Field(i)
  1055. fieldValue := objValue.Field(i)
  1056. if db := field.Tag.Get(tagDb); db != "" && db == "-" {
  1057. continue
  1058. }
  1059. // 检查字段值是否为空
  1060. if isEmptyValue(fieldValue) {
  1061. continue
  1062. }
  1063. // 添加字段到map(处理指针)
  1064. result[field.Name] = getRealValue(fieldValue.Interface())
  1065. }
  1066. return result
  1067. }
  1068. func formatWhereCondition(v interface{}) string {
  1069. sql := ""
  1070. actualValue := getRealValue(v)
  1071. // 根据值类型添加引号
  1072. switch val := actualValue.(type) {
  1073. case string:
  1074. sql = fmt.Sprintf("'%s'", escapeSQLString(val))
  1075. case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
  1076. sql = fmt.Sprintf("%v", val)
  1077. case bool:
  1078. if val {
  1079. sql = "true"
  1080. } else {
  1081. sql = "false"
  1082. }
  1083. default:
  1084. // 对于其他类型,使用字符串表示
  1085. sql = fmt.Sprintf("'%v'", escapeSQLString(fmt.Sprintf("%v", val)))
  1086. }
  1087. return sql
  1088. }
  1089. // isEmptyValue 检查值是否为空(零值)
  1090. // 核心调整:数值类型(int/uint/float)的 0 不再视为空值,会保留原值
  1091. func isEmptyValue(v reflect.Value) bool {
  1092. switch v.Kind() {
  1093. case reflect.String:
  1094. // 空字符串视为空
  1095. return v.String() == ""
  1096. case reflect.Bool:
  1097. // 布尔类型无"空"概念,始终返回 false
  1098. return false
  1099. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1100. // 整数 0 不视为空,返回 false
  1101. return false
  1102. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  1103. // 无符号整数 0 不视为空,返回 false
  1104. return false
  1105. case reflect.Float32, reflect.Float64:
  1106. // 浮点数 0 不视为空,返回 false
  1107. return false
  1108. case reflect.Ptr, reflect.Interface:
  1109. // nil 指针/接口视为空
  1110. return v.IsNil()
  1111. case reflect.Slice, reflect.Map, reflect.Array:
  1112. // 长度为 0 的切片/Map/数组视为空
  1113. return v.Len() == 0
  1114. default:
  1115. // 其他类型(如结构体):Go 1.13+ 用 IsZero 判断,但排除数值 0 场景
  1116. if v.CanInterface() {
  1117. // 先判断是否是数值类型(防止结构体字段中的 0 被误判)
  1118. switch v.Type().Kind() {
  1119. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  1120. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
  1121. reflect.Float32, reflect.Float64:
  1122. return false // 数值类型无论值是多少,都不视为空
  1123. default:
  1124. // 非数值类型,用 Zero 判断是否为零值
  1125. return reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()).Interface())
  1126. }
  1127. }
  1128. return false
  1129. }
  1130. }
  1131. // formatValue 格式化值
  1132. func formatValue(v interface{}) string {
  1133. if v == nil {
  1134. return "NULL"
  1135. }
  1136. // 获取实际值(处理指针)
  1137. actualValue := getRealValue(v)
  1138. switch val := actualValue.(type) {
  1139. case string:
  1140. return fmt.Sprintf("'%s'", escapeSQLString(val))
  1141. case *string:
  1142. if val == nil {
  1143. return "NULL"
  1144. }
  1145. return fmt.Sprintf("'%s'", escapeSQLString(*val))
  1146. case bool:
  1147. if val {
  1148. return "1"
  1149. }
  1150. return "0"
  1151. case *bool:
  1152. if val == nil {
  1153. return "NULL"
  1154. }
  1155. if *val {
  1156. return "1"
  1157. }
  1158. return "0"
  1159. case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
  1160. return fmt.Sprintf("%v", val)
  1161. case *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64:
  1162. // 解引用指针
  1163. rv := reflect.ValueOf(actualValue)
  1164. if rv.IsNil() {
  1165. return "NULL"
  1166. }
  1167. return fmt.Sprintf("%v", rv.Elem().Interface())
  1168. case float32, float64:
  1169. return fmt.Sprintf("%v", val)
  1170. case *float32, *float64:
  1171. rv := reflect.ValueOf(actualValue)
  1172. if rv.IsNil() {
  1173. return "NULL"
  1174. }
  1175. return fmt.Sprintf("%v", rv.Elem().Interface())
  1176. case time.Time:
  1177. return fmt.Sprintf("'%s'", val.Format("2006-01-02 15:04:05"))
  1178. case *time.Time:
  1179. if val == nil {
  1180. return "NULL"
  1181. }
  1182. return fmt.Sprintf("'%s'", val.Format("2006-01-02 15:04:05"))
  1183. default:
  1184. // 对于其他类型,尝试转换为字符串
  1185. str := fmt.Sprintf("%v", actualValue)
  1186. return fmt.Sprintf("'%s'", escapeSQLString(str))
  1187. }
  1188. }
  1189. // escapeSQLString 转义SQL字符串中的特殊字符
  1190. func escapeSQLString(str string) string {
  1191. // 转义单引号
  1192. str = strings.ReplaceAll(str, "'", "''")
  1193. // 转义反斜杠
  1194. str = strings.ReplaceAll(str, "\\", "\\\\")
  1195. return str
  1196. }
  1197. // generateUUID 生成UUID
  1198. func GenerateUUID() string {
  1199. uuid := uuid.New()
  1200. return uuid.String()
  1201. }
  1202. // containsSQLInjection 检测SQL注入
  1203. func containsSQLInjection(value interface{}) bool {
  1204. // 获取实际值(处理指针)
  1205. realValue := getRealValue(value)
  1206. if realValue == nil {
  1207. return false
  1208. }
  1209. str := strings.ToLower(toString(realValue))
  1210. for _, keyword := range sqlInjectionKeywords {
  1211. if strings.Contains(str, keyword) {
  1212. return true
  1213. }
  1214. }
  1215. // 检测特殊字符
  1216. if strings.Contains(str, ";") || strings.Contains(str, "\\") {
  1217. return true
  1218. }
  1219. return false
  1220. }
  1221. // humpToUnderline 驼峰转下划线
  1222. func humpToUnderline(str string) string {
  1223. // 如果已经是下划线格式,直接返回
  1224. if strings.Contains(str, "_") {
  1225. return strings.ToLower(str)
  1226. }
  1227. // 处理连续大写字母的情况
  1228. re := regexp.MustCompile(`([A-Z]+)([A-Z][a-z])`)
  1229. str = re.ReplaceAllString(str, "${1}_${2}")
  1230. // 处理单个大写字母的情况
  1231. re = regexp.MustCompile(`([a-z])([A-Z])`)
  1232. return strings.ToLower(re.ReplaceAllString(str, "${1}_${2}"))
  1233. }
  1234. // quoteColumn 给列名添加引号
  1235. func quoteColumn(column string) string {
  1236. // 如果已经是引号包裹的,直接返回
  1237. if strings.HasPrefix(column, "`") && strings.HasSuffix(column, "`") {
  1238. return column
  1239. }
  1240. if strings.HasPrefix(column, "\"") && strings.HasSuffix(column, "\"") {
  1241. return column
  1242. }
  1243. if strings.HasPrefix(column, "[") && strings.HasSuffix(column, "]") {
  1244. return column
  1245. }
  1246. return fmt.Sprintf("`%s`", column)
  1247. }
  1248. // toString 将任意类型转为字符串(处理指针)
  1249. func toString(value interface{}) string {
  1250. // 获取实际值(处理指针)
  1251. realValue := getRealValue(value)
  1252. switch v := realValue.(type) {
  1253. case string:
  1254. return v
  1255. case int:
  1256. return strconv.Itoa(v)
  1257. case int8:
  1258. return strconv.FormatInt(int64(v), 10)
  1259. case int16:
  1260. return strconv.FormatInt(int64(v), 10)
  1261. case int32:
  1262. return strconv.FormatInt(int64(v), 10)
  1263. case int64:
  1264. return strconv.FormatInt(v, 10)
  1265. case uint:
  1266. return strconv.FormatUint(uint64(v), 10)
  1267. case uint8:
  1268. return strconv.FormatUint(uint64(v), 10)
  1269. case uint16:
  1270. return strconv.FormatUint(uint64(v), 10)
  1271. case uint32:
  1272. return strconv.FormatUint(uint64(v), 10)
  1273. case uint64:
  1274. return strconv.FormatUint(v, 10)
  1275. case float32:
  1276. return strconv.FormatFloat(float64(v), 'f', -1, 32)
  1277. case float64:
  1278. return strconv.FormatFloat(v, 'f', -1, 64)
  1279. case bool:
  1280. if v {
  1281. return "true"
  1282. }
  1283. return "false"
  1284. default:
  1285. return fmt.Sprintf("%v", v)
  1286. }
  1287. }
  1288. func FormatToString(value interface{}) string {
  1289. return toString(value)
  1290. }
  1291. // GetTableName 获取表名
  1292. func GetTableName(obj interface{}) string {
  1293. objType := reflect.TypeOf(obj)
  1294. // 如果是指针,获取指向的类型
  1295. if objType.Kind() == reflect.Ptr {
  1296. objType = objType.Elem()
  1297. }
  1298. // 获取结构体名称并转换为下划线格式
  1299. tableName := objType.Name()
  1300. return humpToUnderline(tableName)
  1301. }
  1302. func GetIdValue(obj interface{}) (string, error) {
  1303. // 获取对象类型和值
  1304. objType := reflect.TypeOf(obj)
  1305. objValue := reflect.ValueOf(obj)
  1306. // 如果是指针,获取指向的值
  1307. if objType.Kind() == reflect.Ptr {
  1308. objType = objType.Elem()
  1309. objValue = objValue.Elem()
  1310. }
  1311. // 检查是否是结构体
  1312. if objType.Kind() != reflect.Struct {
  1313. return "", fmt.Errorf("参数必须是结构体或结构体指针")
  1314. }
  1315. // 查找主键字段
  1316. primaryKeyField := ""
  1317. primaryKeyValue := interface{}(nil)
  1318. // 遍历结构体字段
  1319. for i := 0; i < objType.NumField(); i++ {
  1320. field := objType.Field(i)
  1321. // 检查 TableKey 标签
  1322. if tableKey := field.Tag.Get(tagTableKey); tableKey != "" && tableKey != "false" {
  1323. // 获取字段值
  1324. fieldValue := getRealValue(objValue.Field(i).Interface())
  1325. primaryKeyField = field.Name
  1326. primaryKeyValue = fieldValue
  1327. break
  1328. }
  1329. }
  1330. // 如果没有找到带标签的主键,尝试查找名为"id"的字段
  1331. if primaryKeyField == "" {
  1332. // 先尝试查找导出字段"Id"
  1333. if field, ok := objType.FieldByName("Id"); ok {
  1334. fieldValue := getRealValue(objValue.FieldByName("Id").Interface())
  1335. primaryKeyField = field.Name
  1336. primaryKeyValue = fieldValue
  1337. } else if field, ok := objType.FieldByName("ID"); ok {
  1338. // 尝试查找"ID"
  1339. fieldValue := getRealValue(objValue.FieldByName("ID").Interface())
  1340. primaryKeyField = field.Name
  1341. primaryKeyValue = fieldValue
  1342. } else {
  1343. // 尝试查找小写"id"(非导出字段,通过CanInterface检查)
  1344. for i := 0; i < objType.NumField(); i++ {
  1345. field := objType.Field(i)
  1346. if strings.ToLower(field.Name) == "id" {
  1347. fieldValue := objValue.Field(i)
  1348. if fieldValue.CanInterface() {
  1349. primaryKeyField = field.Name
  1350. primaryKeyValue = getRealValue(fieldValue.Interface())
  1351. break
  1352. }
  1353. }
  1354. }
  1355. }
  1356. }
  1357. // 如果仍然没有找到主键字段,返回错误
  1358. if primaryKeyField == "" {
  1359. return "", fmt.Errorf("未找到主键字段")
  1360. }
  1361. // 如果主键值为空,返回错误
  1362. if primaryKeyValue == nil {
  1363. return "", fmt.Errorf("主键值为空")
  1364. }
  1365. // 根据值类型添加引号
  1366. var id_value string
  1367. switch v := primaryKeyValue.(type) {
  1368. case string:
  1369. id_value = escapeSQLString(v)
  1370. case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
  1371. id_value = fmt.Sprintf("%v", v)
  1372. case float32, float64:
  1373. id_value = fmt.Sprintf("%v", v)
  1374. case bool:
  1375. if v {
  1376. id_value = "true"
  1377. } else {
  1378. id_value = "false"
  1379. }
  1380. default:
  1381. // 对于其他类型,使用字符串表示
  1382. id_value = fmt.Sprintf("'%v'", escapeSQLString(fmt.Sprintf("%v", v)))
  1383. }
  1384. return id_value, nil
  1385. }