Skip to content

Commit

Permalink
feat: added util to generate sql update conditions placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
DeeStarks committed Aug 9, 2022
1 parent d97128e commit a2173b6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions utils/sqlitedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,12 @@ func GeneratePlaceholders(n int) string {
}
return strings.Join(holders, ", ")
}

// -> id = $1...
func GenerateSetConditions(keys []string) string {
conditions := make([]string, len(keys))
for i, k := range keys {
conditions[i] = fmt.Sprintf("%s = $%d", k, i+1)
}
return strings.Join(conditions, ", ")
}
18 changes: 18 additions & 0 deletions utils/sqlitedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,22 @@ func TestGeneratePlaceholders(t *testing.T) {
t.Errorf("Expected %s, got %s", tc.expected, res)
}
}
}

func TestGenerateSetConditions(t *testing.T) {
tests := []struct{
keys []string
expected string
} {
{keys: []string{"a", "b", "c"}, expected: "a = $1, b = $2, c = $3"},
{keys: []string{}, expected: ""},
{keys: []string{"id", "name"}, expected: "id = $1, name = $2"},
}

for _, tc := range tests {
res := utils.GenerateSetConditions(tc.keys)
if res != tc.expected {
t.Errorf("Expected %s, got %s", tc.expected, res)
}
}
}

0 comments on commit a2173b6

Please sign in to comment.