Some performance optimization for get #2043
|
@ -92,8 +92,8 @@ func BenchmarkFindStruct(b *testing.B) {
|
|||
_, err := testEngine.Insert(&v)
|
||||
assert.NoError(b, err)
|
||||
|
||||
b.StartTimer()
|
||||
var mynames = make([]BenchmarkFindStruct, 0, 1)
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
err := testEngine.Find(&mynames)
|
||||
b.StopTimer()
|
||||
|
|
|
@ -213,7 +213,7 @@ func TestUnscopeDelete(t *testing.T) {
|
|||
cnt, err = testEngine.ID(1).Delete(&s)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 1, cnt)
|
||||
assert.EqualValues(t, nowUnix, s.DeletedAt.Unix())
|
||||
assert.LessOrEqual(t, int(s.DeletedAt.Unix()-nowUnix), 1)
|
||||
|
||||
var s1 UnscopeDeleteStruct
|
||||
has, err := testEngine.ID(1).Get(&s1)
|
||||
|
@ -225,7 +225,7 @@ func TestUnscopeDelete(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
assert.True(t, has)
|
||||
assert.EqualValues(t, "test", s2.Name)
|
||||
assert.EqualValues(t, nowUnix, s2.DeletedAt.Unix())
|
||||
assert.LessOrEqual(t, int(s2.DeletedAt.Unix()-nowUnix), 1)
|
||||
|
||||
cnt, err = testEngine.ID(1).Unscoped().Delete(new(UnscopeDeleteStruct))
|
||||
assert.NoError(t, err)
|
||||
|
|
|
@ -203,14 +203,42 @@ func (statement *Statement) GenCountSQL(beans ...interface{}) (string, []interfa
|
|||
return sqlStr, append(statement.joinArgs, condArgs...), nil
|
||||
}
|
||||
|
||||
func (statement *Statement) fromBuilder() *strings.Builder {
|
||||
var builder strings.Builder
|
||||
var quote = statement.quote
|
||||
var dialect = statement.dialect
|
||||
|
||||
builder.WriteString(" FROM ")
|
||||
|
||||
if dialect.URI().DBType == schemas.MSSQL && strings.Contains(statement.TableName(), "..") {
|
||||
builder.WriteString(statement.TableName())
|
||||
} else {
|
||||
builder.WriteString(quote(statement.TableName()))
|
||||
}
|
||||
|
||||
if statement.TableAlias != "" {
|
||||
if dialect.URI().DBType == schemas.ORACLE {
|
||||
builder.WriteString(" ")
|
||||
} else {
|
||||
builder.WriteString(" AS ")
|
||||
}
|
||||
builder.WriteString(quote(statement.TableAlias))
|
||||
}
|
||||
if statement.JoinStr != "" {
|
||||
builder.WriteString(" ")
|
||||
builder.WriteString(statement.JoinStr)
|
||||
}
|
||||
return &builder
|
||||
}
|
||||
|
||||
func (statement *Statement) genSelectSQL(columnStr string, needLimit, needOrderBy bool) (string, []interface{}, error) {
|
||||
var (
|
||||
distinct string
|
||||
dialect = statement.dialect
|
||||
quote = statement.quote
|
||||
fromStr = " FROM "
|
||||
fromStr = statement.fromBuilder().String()
|
||||
top, mssqlCondi, whereStr string
|
||||
)
|
||||
|
||||
if statement.IsDistinct && !strings.HasPrefix(columnStr, "count") {
|
||||
distinct = "DISTINCT "
|
||||
}
|
||||
|
@ -220,24 +248,7 @@ func (statement *Statement) genSelectSQL(columnStr string, needLimit, needOrderB
|
|||
return "", nil, err
|
||||
}
|
||||
if len(condSQL) > 0 {
|
||||
whereStr = " WHERE " + condSQL
|
||||
}
|
||||
|
||||
if dialect.URI().DBType == schemas.MSSQL && strings.Contains(statement.TableName(), "..") {
|
||||
fromStr += statement.TableName()
|
||||
} else {
|
||||
fromStr += quote(statement.TableName())
|
||||
}
|
||||
|
||||
if statement.TableAlias != "" {
|
||||
if dialect.URI().DBType == schemas.ORACLE {
|
||||
fromStr += " " + quote(statement.TableAlias)
|
||||
} else {
|
||||
fromStr += " AS " + quote(statement.TableAlias)
|
||||
}
|
||||
}
|
||||
if statement.JoinStr != "" {
|
||||
fromStr = fmt.Sprintf("%v %v", fromStr, statement.JoinStr)
|
||||
whereStr = fmt.Sprintf(" WHERE %s", condSQL)
|
||||
}
|
||||
|
||||
pLimitN := statement.LimitN
|
||||
|
@ -266,20 +277,20 @@ func (statement *Statement) genSelectSQL(columnStr string, needLimit, needOrderB
|
|||
}
|
||||
if statement.needTableName() {
|
||||
if len(statement.TableAlias) > 0 {
|
||||
column = statement.TableAlias + "." + column
|
||||
column = fmt.Sprintf("%s.%s", statement.TableAlias, column)
|
||||
} else {
|
||||
column = statement.TableName() + "." + column
|
||||
column = fmt.Sprintf("%s.%s", statement.TableName(), column)
|
||||
}
|
||||
}
|
||||
|
||||
var orderStr string
|
||||
if needOrderBy && len(statement.OrderStr) > 0 {
|
||||
orderStr = " ORDER BY " + statement.OrderStr
|
||||
orderStr = fmt.Sprintf(" ORDER BY %s", statement.OrderStr)
|
||||
}
|
||||
|
||||
var groupStr string
|
||||
if len(statement.GroupByStr) > 0 {
|
||||
groupStr = " GROUP BY " + statement.GroupByStr
|
||||
groupStr = fmt.Sprintf(" GROUP BY %s", statement.GroupByStr)
|
||||
}
|
||||
mssqlCondi = fmt.Sprintf("(%s NOT IN (SELECT TOP %d %s%s%s%s%s))",
|
||||
column, statement.Start, column, fromStr, whereStr, orderStr, groupStr)
|
||||
|
@ -311,7 +322,7 @@ func (statement *Statement) genSelectSQL(columnStr string, needLimit, needOrderB
|
|||
if pLimitN != nil {
|
||||
fmt.Fprintf(&buf, " LIMIT %v OFFSET %v", *pLimitN, statement.Start)
|
||||
} else {
|
||||
fmt.Fprintf(&buf, "LIMIT 0 OFFSET %v", statement.Start)
|
||||
fmt.Fprintf(&buf, " LIMIT 0 OFFSET %v", statement.Start)
|
||||
}
|
||||
} else if pLimitN != nil {
|
||||
fmt.Fprint(&buf, " LIMIT ", *pLimitN)
|
||||
|
|
|
@ -308,7 +308,7 @@ func (statement *Statement) colName(col *schemas.Column, tableName string) strin
|
|||
if len(statement.TableAlias) > 0 {
|
||||
nm = statement.TableAlias
|
||||
}
|
||||
return statement.quote(nm) + "." + statement.quote(col.Name)
|
||||
return fmt.Sprintf("%s.%s", statement.quote(nm), statement.quote(col.Name))
|
||||
}
|
||||
return statement.quote(col.Name)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user