From a02c41c650665e71a46bf975c1fdaccaf8527357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=93=E5=85=A5=C3=A4what?= Date: Sun, 16 Apr 2023 22:01:48 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20=E6=96=B0=E5=A2=9E=20NewOrderBy=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- support/{ => orm}/join.go | 0 support/orm/orderBy.go | 36 +++++++++++++++++++++++++++++++++++ support/{ => orm}/relation.go | 0 3 files changed, 36 insertions(+) rename support/{ => orm}/join.go (100%) create mode 100644 support/orm/orderBy.go rename support/{ => orm}/relation.go (100%) diff --git a/support/join.go b/support/orm/join.go similarity index 100% rename from support/join.go rename to support/orm/join.go diff --git a/support/orm/orderBy.go b/support/orm/orderBy.go new file mode 100644 index 0000000..d715992 --- /dev/null +++ b/support/orm/orderBy.go @@ -0,0 +1,36 @@ +package orm + +import ( + "git.fsdpf.net/go/contracts" + "git.fsdpf.net/go/db" +) + +type OrderByDirection string +type OrderByType string + +const ( + OrderByDirection_ASC OrderByDirection = db.ORDER_ASC + OrderByDirection_DESC OrderByDirection = db.ORDER_DESC +) + +type OrderBy struct { + sql string // SQL 或 字段 + direction OrderByDirection // 排序方向 +} + +func NewOrderBy(sql string, params ...OrderByDirection) contracts.OrderBy { + direction := OrderByDirection_ASC + if len(params) > 0 { + direction = params[0] + } + + return OrderBy{sql, direction} +} + +func (this OrderBy) ToSql() db.Expression { + return db.Raw(this.sql + " " + string(this.direction)) +} + +func (this OrderBy) Inject(dbBuilder *db.Builder, m contracts.Model) { + dbBuilder.OrderBy(this.ToSql()) +} diff --git a/support/relation.go b/support/orm/relation.go similarity index 100% rename from support/relation.go rename to support/orm/relation.go