mirror of
https://github.com/aykhans/slash-e.git
synced 2025-07-03 20:21:40 +00:00
chore: update store statement execution
This commit is contained in:
@ -2,7 +2,6 @@ package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -30,13 +29,7 @@ type FindWorkspaceSetting struct {
|
||||
}
|
||||
|
||||
func (s *Store) UpsertWorkspaceSetting(ctx context.Context, upsert *WorkspaceSetting) (*WorkspaceSetting, error) {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
query := `
|
||||
stmt := `
|
||||
INSERT INTO workspace_setting (
|
||||
key,
|
||||
value
|
||||
@ -45,11 +38,7 @@ func (s *Store) UpsertWorkspaceSetting(ctx context.Context, upsert *WorkspaceSet
|
||||
ON CONFLICT(key) DO UPDATE
|
||||
SET value = EXCLUDED.value
|
||||
`
|
||||
if _, err := tx.ExecContext(ctx, query, upsert.Key, upsert.Value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
if _, err := s.db.ExecContext(ctx, stmt, upsert.Key, upsert.Value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -59,53 +48,8 @@ func (s *Store) UpsertWorkspaceSetting(ctx context.Context, upsert *WorkspaceSet
|
||||
}
|
||||
|
||||
func (s *Store) ListWorkspaceSettings(ctx context.Context, find *FindWorkspaceSetting) ([]*WorkspaceSetting, error) {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
list, err := listWorkspaceSettings(ctx, tx, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, workspaceSetting := range list {
|
||||
s.workspaceSettingCache.Store(workspaceSetting.Key, workspaceSetting)
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (s *Store) GetWorkspaceSetting(ctx context.Context, find *FindWorkspaceSetting) (*WorkspaceSetting, error) {
|
||||
if find.Key != "" {
|
||||
if cache, ok := s.workspaceSettingCache.Load(find.Key); ok {
|
||||
return cache.(*WorkspaceSetting), nil
|
||||
}
|
||||
}
|
||||
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
list, err := listWorkspaceSettings(ctx, tx, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(list) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
workspaceSetting := list[0]
|
||||
s.workspaceSettingCache.Store(workspaceSetting.Key, workspaceSetting)
|
||||
return workspaceSetting, nil
|
||||
}
|
||||
|
||||
func listWorkspaceSettings(ctx context.Context, tx *sql.Tx, find *FindWorkspaceSetting) ([]*WorkspaceSetting, error) {
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if find.Key != "" {
|
||||
where, args = append(where, "key = ?"), append(args, find.Key)
|
||||
}
|
||||
@ -116,7 +60,7 @@ func listWorkspaceSettings(ctx context.Context, tx *sql.Tx, find *FindWorkspaceS
|
||||
value
|
||||
FROM workspace_setting
|
||||
WHERE ` + strings.Join(where, " AND ")
|
||||
rows, err := tx.QueryContext(ctx, query, args...)
|
||||
rows, err := s.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -140,5 +84,30 @@ func listWorkspaceSettings(ctx context.Context, tx *sql.Tx, find *FindWorkspaceS
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, workspaceSetting := range list {
|
||||
s.workspaceSettingCache.Store(workspaceSetting.Key, workspaceSetting)
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (s *Store) GetWorkspaceSetting(ctx context.Context, find *FindWorkspaceSetting) (*WorkspaceSetting, error) {
|
||||
if find.Key != "" {
|
||||
if cache, ok := s.workspaceSettingCache.Load(find.Key); ok {
|
||||
return cache.(*WorkspaceSetting), nil
|
||||
}
|
||||
}
|
||||
|
||||
list, err := s.ListWorkspaceSettings(ctx, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(list) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
workspaceSetting := list[0]
|
||||
s.workspaceSettingCache.Store(workspaceSetting.Key, workspaceSetting)
|
||||
return workspaceSetting, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user