This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Manual SQL gluing replaced by squirrel
- Loading branch information
Showing
8 changed files
with
112 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,15 @@ | ||
package repository | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/v1adhope/crypto-diary/internal/entity" | ||
) | ||
|
||
var allowedFilters = map[string]string{ | ||
entity.FilterDate: "open_date", | ||
entity.FilterPair: "pair", | ||
entity.FilterStrategically: "strategically", | ||
} | ||
|
||
type filterBuilderDeps struct { | ||
Query string | ||
Filter entity.Filter | ||
} | ||
|
||
func BuildFilterString(deps filterBuilderDeps) (string, []string) { | ||
var ( | ||
filterRaw strings.Builder | ||
mu sync.RWMutex | ||
) | ||
|
||
fmt.Fprint(&filterRaw, deps.Query, " ") | ||
|
||
argsCounter := strings.Count(deps.Query, "$") + 1 | ||
|
||
args := make([]string, 0) | ||
|
||
for fieldKey, fieldVal := range deps.Filter.Fields { | ||
mu.RLock() | ||
if realFilterName, ok := allowedFilters[fieldKey]; ok { | ||
fmt.Fprintf(&filterRaw, "AND %s = $%d ", realFilterName, argsCounter) | ||
args = append(args, fieldVal) | ||
argsCounter++ | ||
} | ||
mu.RUnlock() | ||
} | ||
|
||
fmt.Fprintf(&filterRaw, "ORDER by position_id ASC LIMIT $%d", argsCounter) | ||
var allowedFilters sync.Map | ||
|
||
return filterRaw.String(), args | ||
func init() { | ||
allowedFilters.Store(entity.FilterDate, "open_date") | ||
allowedFilters.Store(entity.FilterPair, "pair") | ||
allowedFilters.Store(entity.FilterStrategically, "strategically") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters