Skip to content

Commit

Permalink
Merge pull request #25 from ArtisanCloud/develop
Browse files Browse the repository at this point in the history
feature(database): GetPivots
  • Loading branch information
Matrix-X authored Jul 18, 2022
2 parents 914c418 + f8c24d0 commit 5721bed
Show file tree
Hide file tree
Showing 5 changed files with 447 additions and 196 deletions.
187 changes: 0 additions & 187 deletions database/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package database

import (
"database/sql"
"errors"
"fmt"
"github.com/ArtisanCloud/PowerLibs/v2/object"
"github.com/google/uuid"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
"math"
"reflect"
Expand Down Expand Up @@ -39,12 +37,6 @@ type PowerCompactModel struct {
UpdatedAt time.Time `gorm:"column:updated_at" json:"updatedAt"`
}

type PowerRelationship struct {
ID int32 `gorm:"AUTO_INCREMENT;PRIMARY_KEY;not null" json:"id"`
CreatedAt time.Time `gorm:"column:created_at; ->;<-:create " json:"createdAt"`
UpdatedAt time.Time `gorm:"column:updated_at" json:"updatedAt"`
}

const UNIQUE_ID = "uuid"
const COMPACT_UNIQUE_ID = "id"

Expand Down Expand Up @@ -78,14 +70,6 @@ func NewPowerCompactModel() *PowerCompactModel {
}
}

func NewPowerRelationship() *PowerRelationship {
now := time.Now()
return &PowerRelationship{
CreatedAt: now,
UpdatedAt: now,
}
}

func (mdl *PowerModel) GetID() int32 {
return mdl.ID
}
Expand All @@ -109,38 +93,6 @@ func (mdl *PowerModel) GetForeignKey() string {
return "model_uuid"
}

// --------------------------------------------------------------------
func (mdl *PowerRelationship) GetTableName(needFull bool) string {
return ""
}

func (mdl *PowerRelationship) GetPowerModel() ModelInterface {
return mdl
}
func (mdl *PowerRelationship) GetID() int32 {
return mdl.ID
}

func (mdl *PowerRelationship) GetUUID() string {
return ""
}

func (mdl *PowerRelationship) GetPrimaryKey() string {
return "id"
}
func (mdl *PowerRelationship) GetForeignKey() string {
return "model_id"
}

func GetPivotComposedUniqueID(foreignValue string, joinValue string) object.NullString {
if foreignValue != "" && joinValue != "" {
strUniqueID := foreignValue + "-" + joinValue
return object.NewNullString(strUniqueID, true)
} else {
return object.NewNullString("", false)
}
}

/**
* Scope Where Conditions
*/
Expand Down Expand Up @@ -251,145 +203,6 @@ func GetAllList(db *gorm.DB, conditions *map[string]interface{},
return nil
}

/**
* Association Relationship
*/
func AssociationRelationship(db *gorm.DB, conditions *map[string]interface{}, mdl interface{}, relationship string, withClauseAssociations bool) *gorm.Association {

tx := db.Model(mdl)

if withClauseAssociations {
tx.Preload(clause.Associations)
}

if conditions != nil {
tx = tx.Where(*conditions)
}

return tx.Association(relationship)
}

func AppendAssociates(db *gorm.DB, pivot ModelInterface, foreignKey string, foreignValue string, joinKey string, joinValues []string) (err error) {
var result *gorm.DB

err = db.Transaction(func(tx *gorm.DB) error {
for i := 0; i < len(joinValues); i++ {

result = SelectPivot(db, pivot, foreignKey, foreignValue, joinKey, joinValues[i])
if result.Error != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
if result.RowsAffected == 0 || result.Error == gorm.ErrRecordNotFound {
err = SavePivot(db, pivot, foreignKey, foreignValue, joinKey, joinValues[i])
if err != nil {
return err
}
} else {
err = UpdatePivot(db, pivot, foreignKey, foreignValue, joinKey, joinValues[i])
if err != nil {
return err
}
}
}
return result.Error
})

return err
}

func SyncAssociates(db *gorm.DB, pivot ModelInterface, foreignKey string, foreignValue string, joinKey string, joinValues []string) (err error) {

err = db.Transaction(func(tx *gorm.DB) error {

err = ClearPivots(db, pivot, foreignKey, foreignValue)
if err != nil {
return err
}
err = AppendAssociates(db, pivot, foreignKey, foreignValue, joinKey, joinValues)

return err
})

return err
}

func ClearAssociation(db *gorm.DB, object ModelInterface, foreignKey string, pivot ModelInterface) error {
result := db.Exec("DELETE FROM "+pivot.GetTableName(true)+" WHERE "+foreignKey+"=?", object.GetID())
if result.Error != nil {
return result.Error
}
return nil
}

func SelectPivot(db *gorm.DB, pivot ModelInterface, foreignKey string, foreignValue string, joinKey string, joinValue string) (result *gorm.DB) {
result = db.
Debug().
Exec("select * from "+pivot.GetTableName(true)+" where "+foreignKey+" = ? AND "+joinKey+"=?", foreignValue, joinValue)
return result
}

func SavePivot(db *gorm.DB, pivot ModelInterface, foreignKey string, foreignValue string, joinKey string, joinValue string) (err error) {
now := time.Now()
result := db.
Debug().
Exec("INSERT INTO "+pivot.GetTableName(true)+
" ("+foreignKey+", "+joinKey+", created_at,updated_at ) VALUES (?, ?, ?, ?)",
foreignValue,
joinValue,
now, now,
)

return result.Error
}

func UpdatePivot(db *gorm.DB, pivot ModelInterface, foreignKey string, foreignValue string, joinKey string, joinValue string) (err error) {
now := time.Now()
result := db.
Debug().
Exec("UPDATE "+pivot.GetTableName(true)+
" SET updated_at=?"+
" WHERE "+foreignKey+"=? AND "+joinKey+"=?",
now,
foreignValue,
joinValue,
)

return result.Error
}

func ClearPivots(db *gorm.DB, pivot ModelInterface, foreignKey string, foreignValue string) (err error) {
result := db.
Debug().
Exec("DELETE FROM "+pivot.GetTableName(true)+" WHERE "+foreignKey+"=?", foreignValue)
if result.Error != nil {
return result.Error
}

return nil

}

/**
* Pagination
*/
func Paginate(page int, pageSize int) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
if page == 0 {
page = 1
}

switch {
case pageSize > 100:
pageSize = 100
case pageSize <= 0:
pageSize = 10
}

offset := (page - 1) * pageSize
return db.Offset(offset).Limit(pageSize)
}
}

/**
* model methods
*/
Expand Down
39 changes: 30 additions & 9 deletions database/pagination.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package database

import "gorm.io/gorm"

type Pagination struct {
Limit int `json:"limit"`
Page int `json:"page"`
Sort string `json:"sort"`
TotalRows int64 `json:"totalRows"`
TotalPages int `json:"totalPages"`
Data interface{} `json:"data"`
Limit int `json:"limit"`
Page int `json:"page"`
Sort string `json:"sort"`
TotalRows int64 `json:"totalRows"`
TotalPages int `json:"totalPages"`
Data interface{} `json:"data"`
}

func NewPagination(page int, limit int, sort string) *Pagination {

p:= &Pagination{}
p := &Pagination{}
p.SetPage(page)
p.SetLimit(limit)
p.SetSort(sort)
Expand All @@ -21,6 +22,27 @@ func NewPagination(page int, limit int, sort string) *Pagination {

}

/**
* Pagination
*/
func Paginate(page int, pageSize int) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
if page == 0 {
page = 1
}

switch {
case pageSize > 100:
pageSize = 100
case pageSize <= 0:
pageSize = 10
}

offset := (page - 1) * pageSize
return db.Offset(offset).Limit(pageSize)
}
}

func (p *Pagination) GetOffset() int {
return (p.GetPage() - 1) * p.GetLimit()
}
Expand All @@ -31,7 +53,7 @@ func (p *Pagination) SetLimit(limit int) *Pagination {
p.Limit = 10
}

p.Limit= limit
p.Limit = limit
return p
}
func (p *Pagination) GetLimit() int {
Expand Down Expand Up @@ -67,4 +89,3 @@ func (p *Pagination) SetSort(sort string) *Pagination {
func (p *Pagination) GetSort() string {
return p.Sort
}

Loading

0 comments on commit 5721bed

Please sign in to comment.