-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference.go
More file actions
97 lines (81 loc) · 3.34 KB
/
reference.go
File metadata and controls
97 lines (81 loc) · 3.34 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package morph
import (
"slices"
)
// Reference represents a reference between two tables via a foreign key.
type Reference struct {
parent Table
child Table
foreignKey []Column
}
// Parent returns the parent table of the reference.
func (r *Reference) Parent() Table {
return r.parent
}
// Child returns the child table of the reference.
func (r *Reference) Child() Table {
return r.child
}
// ForeignKey returns the foreign key columns of the reference.
func (r *Reference) ForeignKey() []Column {
key := make([]Column, len(r.foreignKey))
copy(key, r.foreignKey)
return key
}
// NonForeignKeyColumns returns the non-foreign key columns of the child table.
func (r Reference) NonForeignKeyColumns() []Column {
return r.child.FindColumns(func(c Column) bool {
return !slices.ContainsFunc(r.foreignKey, func(fk Column) bool {
return fk.equals(c)
})
})
}
// equal checks if two references are equal.
func (r Reference) equals(other Reference) bool {
sameParent := r.Parent().Equals(other.Parent())
sameChild := r.Child().Equals(other.Child())
sameForeignKey := slices.EqualFunc(r.ForeignKey(), other.ForeignKey(), func(a, b Column) bool {
return a.equals(b)
})
return sameParent && sameChild && sameForeignKey
}
// InsertQuery generates an INSERT query for the reference.
func (r *Reference) InsertQuery(options ...QueryOption) (string, error) {
return r.child.InsertQuery(options...)
}
// InsertQueryWithArgs generates an INSERT query for the reference along with arguments
// derived from the provided object.
func (r *Reference) InsertQueryWithArgs(obj any, options ...QueryOption) (string, []any, error) {
return r.child.InsertQueryWithArgs(obj, options...)
}
// UpdateQuery generates an UPDATE query for the reference.
func (r *Reference) UpdateQuery(options ...QueryOption) (string, error) {
return r.child.UpdateQuery(options...)
}
// UpdateQueryWithArgs generates an UPDATE query for the reference along with arguments
// derived from the provided object.
func (r *Reference) UpdateQueryWithArgs(obj any, options ...QueryOption) (string, []any, error) {
return r.child.UpdateQueryWithArgs(obj, options...)
}
// SelectQuery generates a SELECT query for the reference.
func (r *Reference) SelectQuery(options ...QueryOption) (string, error) {
generator := newQueryGenerator(&r.child, r.ForeignKey(), r.NonForeignKeyColumns())
return generator.SelectQuery(options...)
}
// SelectQueryWithArgs generates a SELECT query with arguments for the reference.
func (r *Reference) SelectQueryWithArgs(obj any, options ...QueryOption) (string, []any, error) {
opts := append(options, WithNamedParameters())
generator := newQueryGenerator(&r.child, r.ForeignKey(), r.NonForeignKeyColumns())
return generator.SelectQueryWithArgs(obj, opts...)
}
// DeleteQuery generates a DELETE query for the reference.
func (r *Reference) DeleteQuery(options ...QueryOption) (string, error) {
generator := newQueryGenerator(&r.child, r.ForeignKey(), r.NonForeignKeyColumns())
return generator.DeleteQuery(options...)
}
// DeleteQueryWithArgs generates a DELETE query with arguments for the reference.
func (r *Reference) DeleteQueryWithArgs(obj any, options ...QueryOption) (string, []any, error) {
opts := append(options, WithNamedParameters())
generator := newQueryGenerator(&r.child, r.ForeignKey(), r.NonForeignKeyColumns())
return generator.DeleteQueryWithArgs(obj, opts...)
}