app-cloudep-trade-service/internal/model/mysql_query_builder.go

38 lines
884 B
Go
Raw Permalink Normal View History

2024-10-30 11:13:56 +00:00
package model
import (
"fmt"
"strings"
)
// queryBindINBuilder 用來動態生成 WHERE 條件和參數
func queryBindINBuilder(baseQuery string, conditions map[string][]any) (string, []any) {
args := make([]any, 0)
whereClause := make([]string, 0)
for column, values := range conditions {
if len(values) > 0 {
placeholders := strings.Repeat("?,", len(values))
placeholders = placeholders[:len(placeholders)-1]
whereClause = append(whereClause, fmt.Sprintf("%s IN (%s)", column, placeholders))
args = append(args, values...)
}
}
// 將 WHERE 條件添加到查詢中
if len(whereClause) > 0 {
baseQuery += " WHERE " + strings.Join(whereClause, " AND ")
}
return baseQuery, args
}
func convertSliceToInterface[T any](slice []T) []any {
result := make([]any, 0, len(slice))
for _, v := range slice {
result = append(result, v)
}
return result
}