Skip to content

Commit 5dd0c61

Browse files
gaultierory-bot
authored andcommitted
refactor: move database meta functions to root x folder for reusability
GitOrigin-RevId: 30ee938ea5f1d19bac8967e0ebfe2d595ec27d2b
1 parent 682fcc1 commit 5dd0c61

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

oryx/popx/db_columns.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package popx
2+
3+
import (
4+
"github.com/ory/pop/v6"
5+
)
6+
7+
func DBColumns[T any](quoter Quoter) string {
8+
return (&pop.Model{Value: new(T)}).Columns().QuotedString(quoter)
9+
}
10+
11+
// IndexHint returns the table name including the index hint, if the database
12+
// supports it.
13+
func IndexHint(conn *pop.Connection, table string, index string) string {
14+
if conn.Dialect.Name() == "cockroach" {
15+
return table + "@" + index
16+
}
17+
return table
18+
}
19+
20+
func WritableDBColumnNames[T any]() []string {
21+
var names []string
22+
for _, c := range (&pop.Model{Value: new(T)}).Columns().Writeable().Cols {
23+
names = append(names, c.Name)
24+
}
25+
return names
26+
}
27+
28+
func DBColumnsExcluding[T any](quoter Quoter, exclude ...string) string {
29+
cols := (&pop.Model{Value: new(T)}).Columns()
30+
for _, e := range exclude {
31+
cols.Remove(e)
32+
}
33+
return cols.QuotedString(quoter)
34+
}
35+
36+
type (
37+
PrefixQuoter struct {
38+
Prefix string
39+
Quoter Quoter
40+
}
41+
Quoter interface {
42+
Quote(key string) string
43+
}
44+
)
45+
46+
func (pq *PrefixQuoter) Quote(key string) string {
47+
return pq.Quoter.Quote(pq.Prefix + key)
48+
}

0 commit comments

Comments
 (0)