2014-08-18 08:41:43 +00:00
|
|
|
// Copyright 2014 beego Author. All Rights Reserved.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2013-08-10 16:15:26 +00:00
|
|
|
package orm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2013-08-13 09:16:12 +00:00
|
|
|
"time"
|
2013-08-10 16:15:26 +00:00
|
|
|
)
|
|
|
|
|
2014-01-17 09:25:17 +00:00
|
|
|
// table info struct.
|
2013-08-10 16:15:26 +00:00
|
|
|
type dbTable struct {
|
|
|
|
id int
|
|
|
|
index string
|
|
|
|
name string
|
|
|
|
names []string
|
|
|
|
sel bool
|
|
|
|
inner bool
|
|
|
|
mi *modelInfo
|
|
|
|
fi *fieldInfo
|
|
|
|
jtl *dbTable
|
|
|
|
}
|
|
|
|
|
2014-01-17 09:25:17 +00:00
|
|
|
// tables collection struct, contains some tables.
|
2013-08-10 16:15:26 +00:00
|
|
|
type dbTables struct {
|
|
|
|
tablesM map[string]*dbTable
|
|
|
|
tables []*dbTable
|
|
|
|
mi *modelInfo
|
|
|
|
base dbBaser
|
2014-01-03 16:04:15 +00:00
|
|
|
skipEnd bool
|
2013-08-10 16:15:26 +00:00
|
|
|
}
|
|
|
|
|
2014-01-17 09:25:17 +00:00
|
|
|
// set table info to collection.
|
|
|
|
// if not exist, create new.
|
2013-08-10 16:15:26 +00:00
|
|
|
func (t *dbTables) set(names []string, mi *modelInfo, fi *fieldInfo, inner bool) *dbTable {
|
|
|
|
name := strings.Join(names, ExprSep)
|
|
|
|
if j, ok := t.tablesM[name]; ok {
|
|
|
|
j.name = name
|
|
|
|
j.mi = mi
|
|
|
|
j.fi = fi
|
|
|
|
j.inner = inner
|
|
|
|
} else {
|
|
|
|
i := len(t.tables) + 1
|
|
|
|
jt := &dbTable{i, fmt.Sprintf("T%d", i), name, names, false, inner, mi, fi, nil}
|
|
|
|
t.tablesM[name] = jt
|
|
|
|
t.tables = append(t.tables, jt)
|
|
|
|
}
|
|
|
|
return t.tablesM[name]
|
|
|
|
}
|
|
|
|
|
2014-01-17 09:25:17 +00:00
|
|
|
// add table info to collection.
|
2013-08-10 16:15:26 +00:00
|
|
|
func (t *dbTables) add(names []string, mi *modelInfo, fi *fieldInfo, inner bool) (*dbTable, bool) {
|
|
|
|
name := strings.Join(names, ExprSep)
|
2017-03-17 17:24:45 +00:00
|
|
|
if _, ok := t.tablesM[name]; !ok {
|
2013-08-10 16:15:26 +00:00
|
|
|
i := len(t.tables) + 1
|
|
|
|
jt := &dbTable{i, fmt.Sprintf("T%d", i), name, names, false, inner, mi, fi, nil}
|
|
|
|
t.tablesM[name] = jt
|
|
|
|
t.tables = append(t.tables, jt)
|
|
|
|
return jt, true
|
|
|
|
}
|
|
|
|
return t.tablesM[name], false
|
|
|
|
}
|
|
|
|
|
2014-01-17 09:25:17 +00:00
|
|
|
// get table info in collection.
|
2013-08-10 16:15:26 +00:00
|
|
|
func (t *dbTables) get(name string) (*dbTable, bool) {
|
|
|
|
j, ok := t.tablesM[name]
|
|
|
|
return j, ok
|
|
|
|
}
|
|
|
|
|
2014-01-17 09:25:17 +00:00
|
|
|
// get related fields info in recursive depth loop.
|
|
|
|
// loop once, depth decreases one.
|
2013-08-10 16:15:26 +00:00
|
|
|
func (t *dbTables) loopDepth(depth int, prefix string, fi *fieldInfo, related []string) []string {
|
|
|
|
if depth < 0 || fi.fieldType == RelManyToMany {
|
|
|
|
return related
|
|
|
|
}
|
|
|
|
|
|
|
|
if prefix == "" {
|
|
|
|
prefix = fi.name
|
|
|
|
} else {
|
|
|
|
prefix = prefix + ExprSep + fi.name
|
|
|
|
}
|
|
|
|
related = append(related, prefix)
|
|
|
|
|
|
|
|
depth--
|
|
|
|
for _, fi := range fi.relModelInfo.fields.fieldsRel {
|
|
|
|
related = t.loopDepth(depth, prefix, fi, related)
|
|
|
|
}
|
|
|
|
|
|
|
|
return related
|
|
|
|
}
|
|
|
|
|
2014-01-17 09:25:17 +00:00
|
|
|
// parse related fields.
|
2013-08-10 16:15:26 +00:00
|
|
|
func (t *dbTables) parseRelated(rels []string, depth int) {
|
|
|
|
|
|
|
|
relsNum := len(rels)
|
|
|
|
related := make([]string, relsNum)
|
|
|
|
copy(related, rels)
|
|
|
|
|
|
|
|
relDepth := depth
|
|
|
|
|
|
|
|
if relsNum != 0 {
|
|
|
|
relDepth = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
relDepth--
|
|
|
|
for _, fi := range t.mi.fields.fieldsRel {
|
|
|
|
related = t.loopDepth(relDepth, "", fi, related)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, s := range related {
|
|
|
|
var (
|
|
|
|
exs = strings.Split(s, ExprSep)
|
|
|
|
names = make([]string, 0, len(exs))
|
|
|
|
mmi = t.mi
|
2013-10-14 14:31:35 +00:00
|
|
|
cancel = true
|
2013-08-10 16:15:26 +00:00
|
|
|
jtl *dbTable
|
|
|
|
)
|
2013-10-14 14:31:35 +00:00
|
|
|
|
|
|
|
inner := true
|
|
|
|
|
2013-08-10 16:15:26 +00:00
|
|
|
for _, ex := range exs {
|
|
|
|
if fi, ok := mmi.fields.GetByAny(ex); ok && fi.rel && fi.fieldType != RelManyToMany {
|
|
|
|
names = append(names, fi.name)
|
|
|
|
mmi = fi.relModelInfo
|
|
|
|
|
2014-01-04 14:30:17 +00:00
|
|
|
if fi.null || t.skipEnd {
|
2013-10-14 14:31:35 +00:00
|
|
|
inner = false
|
|
|
|
}
|
|
|
|
|
|
|
|
jt := t.set(names, mmi, fi, inner)
|
2013-08-10 16:15:26 +00:00
|
|
|
jt.jtl = jtl
|
|
|
|
|
|
|
|
if fi.reverse {
|
2013-10-14 14:31:35 +00:00
|
|
|
cancel = false
|
2013-08-10 16:15:26 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 14:31:35 +00:00
|
|
|
if cancel {
|
2013-08-10 16:15:26 +00:00
|
|
|
jt.sel = depth > 0
|
|
|
|
|
|
|
|
if i < relsNum {
|
|
|
|
jt.sel = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
jtl = jt
|
|
|
|
|
|
|
|
} else {
|
2013-10-09 03:37:16 +00:00
|
|
|
panic(fmt.Errorf("unknown model/table name `%s`", ex))
|
2013-08-10 16:15:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-17 09:25:17 +00:00
|
|
|
// generate join string.
|
2015-09-12 13:46:43 +00:00
|
|
|
func (t *dbTables) getJoinSQL() (join string) {
|
2013-08-10 16:15:26 +00:00
|
|
|
Q := t.base.TableQuote()
|
|
|
|
|
|
|
|
for _, jt := range t.tables {
|
|
|
|
if jt.inner {
|
|
|
|
join += "INNER JOIN "
|
|
|
|
} else {
|
|
|
|
join += "LEFT OUTER JOIN "
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
table string
|
|
|
|
t1, t2 string
|
|
|
|
c1, c2 string
|
|
|
|
)
|
|
|
|
t1 = "T0"
|
|
|
|
if jt.jtl != nil {
|
|
|
|
t1 = jt.jtl.index
|
|
|
|
}
|
|
|
|
t2 = jt.index
|
|
|
|
table = jt.mi.table
|
|
|
|
|
|
|
|
switch {
|
2015-12-15 16:17:56 +00:00
|
|
|
case jt.fi.fieldType == RelManyToMany || jt.fi.fieldType == RelReverseMany || jt.fi.reverse && jt.fi.reverseFieldInfo.fieldType == RelManyToMany:
|
2013-08-10 16:15:26 +00:00
|
|
|
c1 = jt.fi.mi.fields.pk.column
|
|
|
|
for _, ffi := range jt.mi.fields.fieldsRel {
|
|
|
|
if jt.fi.mi == ffi.relModelInfo {
|
|
|
|
c2 = ffi.column
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
c1 = jt.fi.column
|
|
|
|
c2 = jt.fi.relModelInfo.fields.pk.column
|
|
|
|
|
|
|
|
if jt.fi.reverse {
|
|
|
|
c1 = jt.mi.fields.pk.column
|
|
|
|
c2 = jt.fi.reverseFieldInfo.column
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
join += fmt.Sprintf("%s%s%s %s ON %s.%s%s%s = %s.%s%s%s ", Q, table, Q, t2,
|
|
|
|
t2, Q, c2, Q, t1, Q, c1, Q)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-01-17 09:25:17 +00:00
|
|
|
// parse orm model struct field tag expression.
|
2013-10-14 14:31:35 +00:00
|
|
|
func (t *dbTables) parseExprs(mi *modelInfo, exprs []string) (index, name string, info *fieldInfo, success bool) {
|
2013-08-10 16:15:26 +00:00
|
|
|
var (
|
|
|
|
jtl *dbTable
|
2014-01-04 14:30:17 +00:00
|
|
|
fi *fieldInfo
|
|
|
|
fiN *fieldInfo
|
2013-08-10 16:15:26 +00:00
|
|
|
mmi = mi
|
|
|
|
)
|
|
|
|
|
|
|
|
num := len(exprs) - 1
|
2015-09-12 13:46:43 +00:00
|
|
|
var names []string
|
2013-08-10 16:15:26 +00:00
|
|
|
|
2013-10-14 14:31:35 +00:00
|
|
|
inner := true
|
|
|
|
|
2014-01-04 14:30:17 +00:00
|
|
|
loopFor:
|
2013-08-10 16:15:26 +00:00
|
|
|
for i, ex := range exprs {
|
|
|
|
|
2014-01-04 14:30:17 +00:00
|
|
|
var ok, okN bool
|
|
|
|
|
|
|
|
if fiN != nil {
|
|
|
|
fi = fiN
|
|
|
|
ok = true
|
|
|
|
fiN = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
fi, ok = mmi.fields.GetByAny(ex)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = okN
|
2013-08-10 16:15:26 +00:00
|
|
|
|
|
|
|
if ok {
|
|
|
|
|
2013-10-14 14:31:35 +00:00
|
|
|
isRel := fi.rel || fi.reverse
|
2013-08-10 16:15:26 +00:00
|
|
|
|
2013-10-14 14:31:35 +00:00
|
|
|
names = append(names, fi.name)
|
2013-08-10 16:15:26 +00:00
|
|
|
|
2013-10-14 14:31:35 +00:00
|
|
|
switch {
|
|
|
|
case fi.rel:
|
|
|
|
mmi = fi.relModelInfo
|
|
|
|
if fi.fieldType == RelManyToMany {
|
|
|
|
mmi = fi.relThroughModelInfo
|
2013-08-10 16:15:26 +00:00
|
|
|
}
|
2013-10-14 14:31:35 +00:00
|
|
|
case fi.reverse:
|
|
|
|
mmi = fi.reverseFieldInfo.mi
|
|
|
|
}
|
2013-08-10 16:15:26 +00:00
|
|
|
|
2014-01-04 14:30:17 +00:00
|
|
|
if i < num {
|
|
|
|
fiN, okN = mmi.fields.GetByAny(exprs[i+1])
|
|
|
|
}
|
|
|
|
|
2017-03-17 17:24:45 +00:00
|
|
|
if isRel && (!fi.mi.isThrough || num != i) {
|
2014-01-04 14:30:17 +00:00
|
|
|
if fi.null || t.skipEnd {
|
2013-10-14 14:31:35 +00:00
|
|
|
inner = false
|
2013-08-10 16:15:26 +00:00
|
|
|
}
|
|
|
|
|
2014-01-04 14:30:17 +00:00
|
|
|
if t.skipEnd && okN || !t.skipEnd {
|
|
|
|
if t.skipEnd && okN && fiN.pk {
|
|
|
|
goto loopEnd
|
|
|
|
}
|
|
|
|
|
2014-01-03 16:04:15 +00:00
|
|
|
jt, _ := t.add(names, mmi, fi, inner)
|
|
|
|
jt.jtl = jtl
|
|
|
|
jtl = jt
|
|
|
|
}
|
|
|
|
|
2013-10-14 14:31:35 +00:00
|
|
|
}
|
2013-08-10 16:15:26 +00:00
|
|
|
|
2014-01-04 14:30:17 +00:00
|
|
|
if num != i {
|
|
|
|
continue
|
|
|
|
}
|
2013-10-14 14:31:35 +00:00
|
|
|
|
2014-01-04 14:30:17 +00:00
|
|
|
loopEnd:
|
2013-10-14 14:31:35 +00:00
|
|
|
|
2014-01-04 14:30:17 +00:00
|
|
|
if i == 0 || jtl == nil {
|
|
|
|
index = "T0"
|
|
|
|
} else {
|
|
|
|
index = jtl.index
|
|
|
|
}
|
2013-08-10 16:15:26 +00:00
|
|
|
|
2014-01-04 14:30:17 +00:00
|
|
|
info = fi
|
2013-10-14 14:31:35 +00:00
|
|
|
|
2014-01-04 14:30:17 +00:00
|
|
|
if jtl == nil {
|
|
|
|
name = fi.name
|
|
|
|
} else {
|
|
|
|
name = jtl.name + ExprSep + fi.name
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case fi.rel:
|
|
|
|
|
|
|
|
case fi.reverse:
|
|
|
|
switch fi.reverseFieldInfo.fieldType {
|
|
|
|
case RelOneToOne, RelForeignKey:
|
|
|
|
index = jtl.index
|
|
|
|
info = fi.reverseFieldInfo.mi.fields.pk
|
|
|
|
name = info.name
|
2013-08-10 16:15:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-04 14:30:17 +00:00
|
|
|
break loopFor
|
|
|
|
|
2013-10-14 14:31:35 +00:00
|
|
|
} else {
|
2013-08-10 16:15:26 +00:00
|
|
|
index = ""
|
|
|
|
name = ""
|
2013-08-11 14:27:45 +00:00
|
|
|
info = nil
|
2013-08-10 16:15:26 +00:00
|
|
|
success = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-11 14:27:45 +00:00
|
|
|
success = index != "" && info != nil
|
2013-08-10 16:15:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-01-17 09:25:17 +00:00
|
|
|
// generate condition sql.
|
2015-09-12 13:46:43 +00:00
|
|
|
func (t *dbTables) getCondSQL(cond *Condition, sub bool, tz *time.Location) (where string, params []interface{}) {
|
2013-08-10 16:15:26 +00:00
|
|
|
if cond == nil || cond.IsEmpty() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-10-14 14:31:35 +00:00
|
|
|
Q := t.base.TableQuote()
|
2013-08-10 16:15:26 +00:00
|
|
|
|
2013-10-14 14:31:35 +00:00
|
|
|
mi := t.mi
|
2013-08-10 16:15:26 +00:00
|
|
|
|
|
|
|
for i, p := range cond.params {
|
|
|
|
if i > 0 {
|
|
|
|
if p.isOr {
|
|
|
|
where += "OR "
|
|
|
|
} else {
|
|
|
|
where += "AND "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if p.isNot {
|
|
|
|
where += "NOT "
|
|
|
|
}
|
|
|
|
if p.isCond {
|
2015-09-12 13:46:43 +00:00
|
|
|
w, ps := t.getCondSQL(p.cond, true, tz)
|
2013-08-10 16:15:26 +00:00
|
|
|
if w != "" {
|
|
|
|
w = fmt.Sprintf("( %s) ", w)
|
|
|
|
}
|
|
|
|
where += w
|
|
|
|
params = append(params, ps...)
|
|
|
|
} else {
|
|
|
|
exprs := p.exprs
|
|
|
|
|
|
|
|
num := len(exprs) - 1
|
|
|
|
operator := ""
|
|
|
|
if operators[exprs[num]] {
|
|
|
|
operator = exprs[num]
|
|
|
|
exprs = exprs[:num]
|
|
|
|
}
|
|
|
|
|
2013-10-14 14:31:35 +00:00
|
|
|
index, _, fi, suc := t.parseExprs(mi, exprs)
|
2017-03-17 17:24:45 +00:00
|
|
|
if !suc {
|
2013-08-10 16:15:26 +00:00
|
|
|
panic(fmt.Errorf("unknown field/column name `%s`", strings.Join(p.exprs, ExprSep)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if operator == "" {
|
|
|
|
operator = "exact"
|
|
|
|
}
|
|
|
|
|
2018-11-04 19:47:21 +00:00
|
|
|
var operSQL string
|
|
|
|
var args []interface{}
|
|
|
|
if p.isRaw {
|
|
|
|
operSQL = p.sql
|
|
|
|
} else {
|
|
|
|
operSQL, args = t.base.GenerateOperatorSQL(mi, fi, operator, p.args, tz)
|
|
|
|
}
|
2013-08-10 16:15:26 +00:00
|
|
|
|
2013-08-11 14:27:45 +00:00
|
|
|
leftCol := fmt.Sprintf("%s.%s%s%s", index, Q, fi.column, Q)
|
2013-10-14 14:31:35 +00:00
|
|
|
t.base.GenerateOperatorLeftCol(fi, operator, &leftCol)
|
2013-08-11 14:27:45 +00:00
|
|
|
|
2015-09-12 13:46:43 +00:00
|
|
|
where += fmt.Sprintf("%s %s ", leftCol, operSQL)
|
2013-08-10 16:15:26 +00:00
|
|
|
params = append(params, args...)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 17:24:45 +00:00
|
|
|
if !sub && where != "" {
|
2013-08-10 16:15:26 +00:00
|
|
|
where = "WHERE " + where
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-02 16:45:09 +00:00
|
|
|
// generate group sql.
|
2015-09-12 13:46:43 +00:00
|
|
|
func (t *dbTables) getGroupSQL(groups []string) (groupSQL string) {
|
|
|
|
if len(groups) == 0 {
|
2015-09-02 16:45:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
Q := t.base.TableQuote()
|
|
|
|
|
|
|
|
groupSqls := make([]string, 0, len(groups))
|
|
|
|
for _, group := range groups {
|
|
|
|
exprs := strings.Split(group, ExprSep)
|
|
|
|
|
|
|
|
index, _, fi, suc := t.parseExprs(t.mi, exprs)
|
2017-03-17 17:24:45 +00:00
|
|
|
if !suc {
|
2015-09-02 16:45:09 +00:00
|
|
|
panic(fmt.Errorf("unknown field/column name `%s`", strings.Join(exprs, ExprSep)))
|
|
|
|
}
|
|
|
|
|
|
|
|
groupSqls = append(groupSqls, fmt.Sprintf("%s.%s%s%s", index, Q, fi.column, Q))
|
|
|
|
}
|
|
|
|
|
2015-09-12 13:46:43 +00:00
|
|
|
groupSQL = fmt.Sprintf("GROUP BY %s ", strings.Join(groupSqls, ", "))
|
2015-09-02 16:45:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-01-17 09:25:17 +00:00
|
|
|
// generate order sql.
|
2015-09-12 13:46:43 +00:00
|
|
|
func (t *dbTables) getOrderSQL(orders []string) (orderSQL string) {
|
2013-08-10 16:15:26 +00:00
|
|
|
if len(orders) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-10-14 14:31:35 +00:00
|
|
|
Q := t.base.TableQuote()
|
2013-08-10 16:15:26 +00:00
|
|
|
|
|
|
|
orderSqls := make([]string, 0, len(orders))
|
|
|
|
for _, order := range orders {
|
|
|
|
asc := "ASC"
|
|
|
|
if order[0] == '-' {
|
|
|
|
asc = "DESC"
|
|
|
|
order = order[1:]
|
|
|
|
}
|
|
|
|
exprs := strings.Split(order, ExprSep)
|
|
|
|
|
2013-10-14 14:31:35 +00:00
|
|
|
index, _, fi, suc := t.parseExprs(t.mi, exprs)
|
2017-03-17 17:24:45 +00:00
|
|
|
if !suc {
|
2013-08-10 16:15:26 +00:00
|
|
|
panic(fmt.Errorf("unknown field/column name `%s`", strings.Join(exprs, ExprSep)))
|
|
|
|
}
|
|
|
|
|
2013-08-11 14:27:45 +00:00
|
|
|
orderSqls = append(orderSqls, fmt.Sprintf("%s.%s%s%s %s", index, Q, fi.column, Q, asc))
|
2013-08-10 16:15:26 +00:00
|
|
|
}
|
|
|
|
|
2015-09-12 13:46:43 +00:00
|
|
|
orderSQL = fmt.Sprintf("ORDER BY %s ", strings.Join(orderSqls, ", "))
|
2013-08-10 16:15:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-01-17 09:25:17 +00:00
|
|
|
// generate limit sql.
|
2015-09-12 13:46:43 +00:00
|
|
|
func (t *dbTables) getLimitSQL(mi *modelInfo, offset int64, limit int64) (limits string) {
|
2013-08-10 16:15:26 +00:00
|
|
|
if limit == 0 {
|
2013-09-13 10:06:44 +00:00
|
|
|
limit = int64(DefaultRowsLimit)
|
2013-08-10 16:15:26 +00:00
|
|
|
}
|
|
|
|
if limit < 0 {
|
|
|
|
// no limit
|
|
|
|
if offset > 0 {
|
2013-10-14 14:31:35 +00:00
|
|
|
maxLimit := t.base.MaxLimit()
|
2013-08-11 14:27:45 +00:00
|
|
|
if maxLimit == 0 {
|
|
|
|
limits = fmt.Sprintf("OFFSET %d", offset)
|
|
|
|
} else {
|
|
|
|
limits = fmt.Sprintf("LIMIT %d OFFSET %d", maxLimit, offset)
|
|
|
|
}
|
2013-08-10 16:15:26 +00:00
|
|
|
}
|
|
|
|
} else if offset <= 0 {
|
|
|
|
limits = fmt.Sprintf("LIMIT %d", limit)
|
|
|
|
} else {
|
|
|
|
limits = fmt.Sprintf("LIMIT %d OFFSET %d", limit, offset)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-01-17 09:25:17 +00:00
|
|
|
// crete new tables collection.
|
2013-08-10 16:15:26 +00:00
|
|
|
func newDbTables(mi *modelInfo, base dbBaser) *dbTables {
|
|
|
|
tables := &dbTables{}
|
|
|
|
tables.tablesM = make(map[string]*dbTable)
|
|
|
|
tables.mi = mi
|
|
|
|
tables.base = base
|
|
|
|
return tables
|
|
|
|
}
|