mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: update postgres stmt builder
This commit is contained in:
@ -3,8 +3,8 @@ package postgres
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strings"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/pkg/errors"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
|
||||
@ -13,6 +13,14 @@ import (
|
||||
)
|
||||
|
||||
func (d *DB) UpsertUserSetting(ctx context.Context, upsert *storepb.UserSetting) (*storepb.UserSetting, error) {
|
||||
stmt := `
|
||||
INSERT INTO user_setting (
|
||||
user_id, key, value
|
||||
)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT(user_id, key) DO UPDATE
|
||||
SET value = EXCLUDED.value
|
||||
`
|
||||
var valueString string
|
||||
if upsert.Key == storepb.UserSettingKey_USER_SETTING_ACCESS_TOKENS {
|
||||
valueBytes, err := protojson.Marshal(upsert.GetAccessTokens())
|
||||
@ -32,20 +40,7 @@ func (d *DB) UpsertUserSetting(ctx context.Context, upsert *storepb.UserSetting)
|
||||
return nil, errors.Errorf("unknown user setting key: %s", upsert.Key.String())
|
||||
}
|
||||
|
||||
// Construct the query using Squirrel
|
||||
query, args, err := squirrel.
|
||||
Insert("user_setting").
|
||||
Columns("user_id", "key", "value").
|
||||
Values(upsert.UserId, upsert.Key.String(), valueString).
|
||||
Suffix("ON CONFLICT (user_id, key) DO UPDATE SET value = EXCLUDED.value").
|
||||
PlaceholderFormat(squirrel.Dollar).
|
||||
ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Execute the query
|
||||
if _, err := d.db.ExecContext(ctx, query, args...); err != nil {
|
||||
if _, err := d.db.ExecContext(ctx, stmt, upsert.UserId, upsert.Key.String(), valueString); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -53,31 +48,28 @@ func (d *DB) UpsertUserSetting(ctx context.Context, upsert *storepb.UserSetting)
|
||||
}
|
||||
|
||||
func (d *DB) ListUserSettings(ctx context.Context, find *store.FindUserSetting) ([]*storepb.UserSetting, error) {
|
||||
// Start building the query using Squirrel
|
||||
qb := squirrel.Select("user_id", "key", "value").From("user_setting").PlaceholderFormat(squirrel.Dollar)
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
// Add conditions based on the provided find parameters
|
||||
if v := find.Key; v != storepb.UserSettingKey_USER_SETTING_KEY_UNSPECIFIED {
|
||||
qb = qb.Where(squirrel.Eq{"key": v.String()})
|
||||
where, args = append(where, "key = "+placeholder(len(args)+1)), append(args, v.String())
|
||||
}
|
||||
if v := find.UserID; v != nil {
|
||||
qb = qb.Where(squirrel.Eq{"user_id": *v})
|
||||
where, args = append(where, "user_id = "+placeholder(len(args)+1)), append(args, *find.UserID)
|
||||
}
|
||||
|
||||
// Finalize the query
|
||||
query, args, err := qb.ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Execute the query
|
||||
query := `
|
||||
SELECT
|
||||
user_id,
|
||||
key,
|
||||
value
|
||||
FROM user_setting
|
||||
WHERE ` + strings.Join(where, " AND ")
|
||||
rows, err := d.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
// Process the rows
|
||||
userSettingList := make([]*storepb.UserSetting, 0)
|
||||
for rows.Next() {
|
||||
userSetting := &storepb.UserSetting{}
|
||||
@ -129,11 +121,7 @@ func (d *DB) ListUserSettings(ctx context.Context, find *store.FindUserSetting)
|
||||
}
|
||||
|
||||
func vacuumUserSetting(ctx context.Context, tx *sql.Tx) error {
|
||||
stmt := `
|
||||
DELETE FROM
|
||||
user_setting
|
||||
WHERE
|
||||
user_id NOT IN (SELECT id FROM "user")`
|
||||
stmt := `DELETE FROM user_setting WHERE user_id NOT IN (SELECT id FROM "user")`
|
||||
_, err := tx.ExecContext(ctx, stmt)
|
||||
if err != nil {
|
||||
return err
|
||||
|
Reference in New Issue
Block a user