mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-01-29 03:36:10 +01:00
🐛 Database cannot filter out rows with relations that are empty or not empty Fix https://github.com/siyuan-note/siyuan/issues/10601
This commit is contained in:
parent
cc9aed6aec
commit
e02e4daa1c
3 changed files with 797 additions and 773 deletions
|
|
@ -16,6 +16,14 @@
|
|||
|
||||
package av
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
)
|
||||
|
||||
type Sortable interface {
|
||||
SortRows()
|
||||
}
|
||||
|
|
@ -31,3 +39,238 @@ const (
|
|||
SortOrderAsc SortOrder = "ASC"
|
||||
SortOrderDesc SortOrder = "DESC"
|
||||
)
|
||||
|
||||
func (value *Value) Compare(other *Value) int {
|
||||
switch value.Type {
|
||||
case KeyTypeBlock:
|
||||
if nil != value.Block && nil != other.Block {
|
||||
ret := strings.Compare(value.Block.Content, other.Block.Content)
|
||||
if 0 == ret {
|
||||
ret = int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
case KeyTypeText:
|
||||
if nil != value.Text && nil != other.Text {
|
||||
ret := strings.Compare(value.Text.Content, other.Text.Content)
|
||||
if 0 == ret {
|
||||
ret = int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
case KeyTypeNumber:
|
||||
if nil != value.Number && nil != other.Number {
|
||||
if value.Number.IsNotEmpty {
|
||||
if !other.Number.IsNotEmpty {
|
||||
return 1
|
||||
}
|
||||
|
||||
if value.Number.Content > other.Number.Content {
|
||||
return 1
|
||||
} else if value.Number.Content < other.Number.Content {
|
||||
return -1
|
||||
} else {
|
||||
return int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
} else {
|
||||
if other.Number.IsNotEmpty {
|
||||
return -1
|
||||
}
|
||||
return int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
}
|
||||
case KeyTypeDate:
|
||||
if nil != value.Date && nil != other.Date {
|
||||
if value.Date.IsNotEmpty {
|
||||
if !other.Date.IsNotEmpty {
|
||||
return 1
|
||||
}
|
||||
if value.Date.Content > other.Date.Content {
|
||||
return 1
|
||||
} else if value.Date.Content < other.Date.Content {
|
||||
return -1
|
||||
} else {
|
||||
return int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
} else {
|
||||
if other.Date.IsNotEmpty {
|
||||
return -1
|
||||
}
|
||||
return int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
}
|
||||
case KeyTypeCreated:
|
||||
if nil != value.Created && nil != other.Created {
|
||||
if value.Created.Content > other.Created.Content {
|
||||
return 1
|
||||
} else if value.Created.Content < other.Created.Content {
|
||||
return -1
|
||||
} else {
|
||||
return int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
}
|
||||
case KeyTypeUpdated:
|
||||
if nil != value.Updated && nil != other.Updated {
|
||||
if value.Updated.Content > other.Updated.Content {
|
||||
return 1
|
||||
} else if value.Updated.Content < other.Updated.Content {
|
||||
return -1
|
||||
} else {
|
||||
return int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
}
|
||||
case KeyTypeSelect, KeyTypeMSelect:
|
||||
if nil != value.MSelect && nil != other.MSelect {
|
||||
var v1 string
|
||||
for _, v := range value.MSelect {
|
||||
v1 += v.Content
|
||||
}
|
||||
var v2 string
|
||||
for _, v := range other.MSelect {
|
||||
v2 += v.Content
|
||||
}
|
||||
ret := strings.Compare(v1, v2)
|
||||
if 0 == ret {
|
||||
ret = int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
case KeyTypeURL:
|
||||
if nil != value.URL && nil != other.URL {
|
||||
ret := strings.Compare(value.URL.Content, other.URL.Content)
|
||||
if 0 == ret {
|
||||
ret = int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
case KeyTypeEmail:
|
||||
if nil != value.Email && nil != other.Email {
|
||||
ret := strings.Compare(value.Email.Content, other.Email.Content)
|
||||
if 0 == ret {
|
||||
ret = int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
case KeyTypePhone:
|
||||
if nil != value.Phone && nil != other.Phone {
|
||||
ret := strings.Compare(value.Phone.Content, other.Phone.Content)
|
||||
if 0 == ret {
|
||||
ret = int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
case KeyTypeMAsset:
|
||||
if nil != value.MAsset && nil != other.MAsset {
|
||||
var v1 string
|
||||
for _, v := range value.MAsset {
|
||||
v1 += v.Content
|
||||
}
|
||||
var v2 string
|
||||
for _, v := range other.MAsset {
|
||||
v2 += v.Content
|
||||
}
|
||||
ret := strings.Compare(v1, v2)
|
||||
if 0 == ret {
|
||||
ret = int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
case KeyTypeTemplate:
|
||||
if nil != value.Template && nil != other.Template {
|
||||
vContent := strings.TrimSpace(value.Template.Content)
|
||||
oContent := strings.TrimSpace(other.Template.Content)
|
||||
if util.IsNumeric(vContent) && util.IsNumeric(oContent) {
|
||||
v1, _ := strconv.ParseFloat(vContent, 64)
|
||||
v2, _ := strconv.ParseFloat(oContent, 64)
|
||||
if v1 > v2 {
|
||||
return 1
|
||||
}
|
||||
if v1 < v2 {
|
||||
return -1
|
||||
}
|
||||
return int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
ret := strings.Compare(value.Template.Content, other.Template.Content)
|
||||
if 0 == ret {
|
||||
ret = int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
case KeyTypeCheckbox:
|
||||
if nil != value.Checkbox && nil != other.Checkbox {
|
||||
if value.Checkbox.Checked && !other.Checkbox.Checked {
|
||||
return 1
|
||||
}
|
||||
if !value.Checkbox.Checked && other.Checkbox.Checked {
|
||||
return -1
|
||||
}
|
||||
return int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
case KeyTypeRelation:
|
||||
if nil != value.Relation && nil != other.Relation {
|
||||
vContentBuf := bytes.Buffer{}
|
||||
for _, c := range value.Relation.Contents {
|
||||
vContentBuf.WriteString(c.String())
|
||||
vContentBuf.WriteByte(' ')
|
||||
}
|
||||
vContent := strings.TrimSpace(vContentBuf.String())
|
||||
oContentBuf := bytes.Buffer{}
|
||||
for _, c := range other.Relation.Contents {
|
||||
oContentBuf.WriteString(c.String())
|
||||
oContentBuf.WriteByte(' ')
|
||||
}
|
||||
oContent := strings.TrimSpace(oContentBuf.String())
|
||||
|
||||
if util.IsNumeric(vContent) && util.IsNumeric(oContent) {
|
||||
v1, _ := strconv.ParseFloat(vContent, 64)
|
||||
v2, _ := strconv.ParseFloat(oContent, 64)
|
||||
if v1 > v2 {
|
||||
return 1
|
||||
}
|
||||
|
||||
if v1 < v2 {
|
||||
return -1
|
||||
}
|
||||
return int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
ret := strings.Compare(vContent, oContent)
|
||||
if 0 == ret {
|
||||
ret = int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
case KeyTypeRollup:
|
||||
if nil != value.Rollup && nil != other.Rollup {
|
||||
vContentBuf := bytes.Buffer{}
|
||||
for _, c := range value.Rollup.Contents {
|
||||
vContentBuf.WriteString(c.String())
|
||||
vContentBuf.WriteByte(' ')
|
||||
}
|
||||
vContent := strings.TrimSpace(vContentBuf.String())
|
||||
oContentBuf := bytes.Buffer{}
|
||||
for _, c := range other.Rollup.Contents {
|
||||
oContentBuf.WriteString(c.String())
|
||||
oContentBuf.WriteByte(' ')
|
||||
}
|
||||
oContent := strings.TrimSpace(oContentBuf.String())
|
||||
|
||||
if util.IsNumeric(vContent) && util.IsNumeric(oContent) {
|
||||
v1, _ := strconv.ParseFloat(vContent, 64)
|
||||
v2, _ := strconv.ParseFloat(oContent, 64)
|
||||
if v1 > v2 {
|
||||
return 1
|
||||
}
|
||||
if v1 < v2 {
|
||||
return -1
|
||||
}
|
||||
return int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
ret := strings.Compare(vContent, oContent)
|
||||
if 0 == ret {
|
||||
ret = int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
}
|
||||
return int(value.CreatedAt - other.CreatedAt)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue