Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
// Copyright © 2025 Ory Corp
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package popx
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/ory/pop/v6"
|
|
)
|
|
|
|
func DBColumns[T any](quoter Quoter) string {
|
|
return (&pop.Model{Value: new(T)}).Columns().QuotedString(quoter)
|
|
}
|
|
|
|
// IndexHint returns the table name including the index hint, if the database
|
|
// supports it.
|
|
func IndexHint(conn *pop.Connection, table string, index string) string {
|
|
if conn.Dialect.Name() == "cockroach" {
|
|
return table + "@" + index
|
|
}
|
|
// PostgreSQL and YugabyteDB do not accept CockroachDB's @index syntax.
|
|
return table
|
|
}
|
|
|
|
func DBColumnsExcluding[T any](quoter Quoter, exclude ...string) string {
|
|
cols := (&pop.Model{Value: new(T)}).Columns()
|
|
for _, e := range exclude {
|
|
cols.Remove(e)
|
|
}
|
|
return cols.QuotedString(quoter)
|
|
}
|
|
|
|
type (
|
|
AliasQuoter struct {
|
|
Alias string
|
|
Quoter Quoter
|
|
}
|
|
Quoter interface {
|
|
Quote(key string) string
|
|
}
|
|
)
|
|
|
|
func (pq *AliasQuoter) Quote(key string) string {
|
|
return fmt.Sprintf("%s.%s", pq.Quoter.Quote(pq.Alias), pq.Quoter.Quote(key))
|
|
}
|
|
|
|
// Placeholders returns `num` placeholders ?, e.g. "?, ?, ?".
|
|
// Placeholders(0) returns "".
|
|
func Placeholders(num int) string {
|
|
switch num {
|
|
case 0:
|
|
return ""
|
|
case 1:
|
|
return "?"
|
|
default:
|
|
return strings.Repeat("?, ", num-1) + "?"
|
|
}
|
|
}
|