-
Notifications
You must be signed in to change notification settings - Fork 40
/
object.go
65 lines (51 loc) · 1.85 KB
/
object.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package database
import (
"context"
"gorm.io/gorm"
"github.com/forbole/juno/v4/common"
"github.com/forbole/juno/v4/models"
"gorm.io/gorm/clause"
"github.com/bnb-chain/greenfield-storage-provider/store/bsdb"
)
func (db *DB) SaveObject(ctx context.Context, object *models.Object) error {
return nil
}
func (db *DB) UpdateObject(ctx context.Context, object *models.Object) error {
return nil
}
func (db *DB) GetObject(ctx context.Context, objectId common.Hash) (*models.Object, error) {
var object models.Object
bucketName, err := db.GetBucketNameByObjectID(objectId)
if err != nil {
return nil, err
}
err = db.Db.WithContext(ctx).Table(bsdb.GetObjectsTableName(bucketName)).Where(
"object_id = ? AND removed IS NOT TRUE", objectId).Find(&object).Error
if err != nil {
return nil, err
}
return &object, nil
}
// GetBucketNameByObjectID get bucket name info by an object id
func (b *DB) GetBucketNameByObjectID(objectID common.Hash) (string, error) {
var (
objectIdMap *bsdb.ObjectIDMap
err error
)
err = b.Db.Table((&bsdb.ObjectIDMap{}).TableName()).
Select("*").
Where("object_id = ?", objectID).
Take(&objectIdMap).Error
return objectIdMap.BucketName, err
}
func (db *DB) SaveObjectToSQL(ctx context.Context, object *models.Object) (string, []interface{}) {
stat := db.Db.Session(&gorm.Session{DryRun: true}).Table(bsdb.GetObjectsTableName(object.BucketName)).Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "object_id"}},
UpdateAll: true,
}).Create(object).Statement
return stat.SQL.String(), stat.Vars
}
func (db *DB) UpdateObjectToSQL(ctx context.Context, object *models.Object) (string, []interface{}) {
stat := db.Db.Session(&gorm.Session{DryRun: true}).Table(bsdb.GetObjectsTableName(object.BucketName)).Where("object_id = ?", object.ObjectID).Updates(object).Statement
return stat.SQL.String(), stat.Vars
}