2020-11-25 20:36:07 +01:00
|
|
|
// Copyright 2019 The Xorm Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package statements
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-10-01 12:07:22 +02:00
|
|
|
"strconv"
|
2020-11-25 20:36:07 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"xorm.io/xorm/internal/utils"
|
|
|
|
"xorm.io/xorm/schemas"
|
|
|
|
)
|
|
|
|
|
2021-08-30 20:04:15 +02:00
|
|
|
// ConvertIDSQL converts SQL with id
|
2020-11-25 20:36:07 +01:00
|
|
|
func (statement *Statement) ConvertIDSQL(sqlStr string) string {
|
|
|
|
if statement.RefTable != nil {
|
|
|
|
cols := statement.RefTable.PKColumns()
|
|
|
|
if len(cols) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
colstrs := statement.joinColumns(cols, false)
|
|
|
|
sqls := utils.SplitNNoCase(sqlStr, " from ", 2)
|
|
|
|
if len(sqls) != 2 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-10-01 12:07:22 +02:00
|
|
|
var b strings.Builder
|
|
|
|
b.WriteString("SELECT ")
|
2020-11-25 20:36:07 +01:00
|
|
|
pLimitN := statement.LimitN
|
|
|
|
if pLimitN != nil && statement.dialect.URI().DBType == schemas.MSSQL {
|
2023-10-01 12:07:22 +02:00
|
|
|
b.WriteString("TOP ")
|
|
|
|
b.WriteString(strconv.Itoa(*pLimitN))
|
|
|
|
b.WriteString(" ")
|
2020-11-25 20:36:07 +01:00
|
|
|
}
|
2023-10-01 12:07:22 +02:00
|
|
|
b.WriteString(colstrs)
|
|
|
|
b.WriteString(" FROM ")
|
|
|
|
b.WriteString(sqls[1])
|
2020-11-25 20:36:07 +01:00
|
|
|
|
2023-10-01 12:07:22 +02:00
|
|
|
return b.String()
|
2020-11-25 20:36:07 +01:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-08-30 20:04:15 +02:00
|
|
|
// ConvertUpdateSQL converts update SQL
|
2020-11-25 20:36:07 +01:00
|
|
|
func (statement *Statement) ConvertUpdateSQL(sqlStr string) (string, string) {
|
|
|
|
if statement.RefTable == nil || len(statement.RefTable.PrimaryKeys) != 1 {
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
|
|
|
|
colstrs := statement.joinColumns(statement.RefTable.PKColumns(), true)
|
|
|
|
sqls := utils.SplitNNoCase(sqlStr, "where", 2)
|
|
|
|
if len(sqls) != 2 {
|
|
|
|
if len(sqls) == 1 {
|
|
|
|
return sqls[0], fmt.Sprintf("SELECT %v FROM %v",
|
|
|
|
colstrs, statement.quote(statement.TableName()))
|
|
|
|
}
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
|
2023-10-01 12:07:22 +02:00
|
|
|
whereStr := sqls[1]
|
2020-11-25 20:36:07 +01:00
|
|
|
|
|
|
|
// TODO: for postgres only, if any other database?
|
|
|
|
var paraStr string
|
|
|
|
if statement.dialect.URI().DBType == schemas.POSTGRES {
|
|
|
|
paraStr = "$"
|
|
|
|
} else if statement.dialect.URI().DBType == schemas.MSSQL {
|
|
|
|
paraStr = ":"
|
|
|
|
}
|
|
|
|
|
|
|
|
if paraStr != "" {
|
|
|
|
if strings.Contains(sqls[1], paraStr) {
|
|
|
|
dollers := strings.Split(sqls[1], paraStr)
|
|
|
|
whereStr = dollers[0]
|
|
|
|
for i, c := range dollers[1:] {
|
|
|
|
ccs := strings.SplitN(c, " ", 2)
|
|
|
|
whereStr += fmt.Sprintf(paraStr+"%v %v", i+1, ccs[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sqls[0], fmt.Sprintf("SELECT %v FROM %v WHERE %v",
|
|
|
|
colstrs, statement.quote(statement.TableName()),
|
|
|
|
whereStr)
|
|
|
|
}
|