2024-08-09 14:56:38 +00:00
|
|
|
package adapter
|
2024-08-07 19:16:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
utils "l12.xyz/dal/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SQLiteContext struct {
|
2024-08-09 14:14:42 +00:00
|
|
|
TableName string
|
2024-08-07 19:16:40 +00:00
|
|
|
TableAlias string
|
|
|
|
FieldName string
|
|
|
|
}
|
|
|
|
|
2024-08-08 17:44:12 +00:00
|
|
|
func (c SQLiteContext) New(opts CtxOpts) Context {
|
2024-08-08 17:26:29 +00:00
|
|
|
ta := opts["TableAlias"]
|
|
|
|
if ta == "" {
|
|
|
|
ta = c.TableAlias
|
|
|
|
}
|
|
|
|
fn := opts["FieldName"]
|
|
|
|
if fn == "" {
|
|
|
|
fn = c.FieldName
|
|
|
|
}
|
|
|
|
return SQLiteContext{
|
|
|
|
TableAlias: ta,
|
|
|
|
FieldName: fn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-09 14:14:42 +00:00
|
|
|
func (c SQLiteContext) GetTableName() string {
|
|
|
|
return c.TableName
|
|
|
|
}
|
|
|
|
|
2024-08-07 19:16:40 +00:00
|
|
|
func (c SQLiteContext) GetFieldName() string {
|
|
|
|
if strings.Contains(c.FieldName, ".") {
|
|
|
|
return c.FieldName
|
|
|
|
}
|
|
|
|
if c.TableAlias != "" {
|
|
|
|
return c.TableAlias + "." + c.FieldName
|
|
|
|
}
|
|
|
|
return c.FieldName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c SQLiteContext) NormalizeValue(value interface{}) interface{} {
|
|
|
|
str, ok := value.(string)
|
2024-08-09 14:14:42 +00:00
|
|
|
if utils.IsSQLFunction(str) {
|
2024-08-07 19:16:40 +00:00
|
|
|
return str
|
|
|
|
}
|
|
|
|
if strings.Contains(str, ".") {
|
|
|
|
_, err := strconv.ParseFloat(str, 64)
|
|
|
|
if err != nil {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
val, err := utils.EscapeSQL(str)
|
|
|
|
if err != nil {
|
|
|
|
return str
|
|
|
|
}
|
2024-08-09 14:14:42 +00:00
|
|
|
return "'" + utils.EscapeSingleQuote(string(val)) + "'"
|
2024-08-07 19:16:40 +00:00
|
|
|
}
|