Skip to content

Commit b5d0df9

Browse files
authored
Update helpers.go
1 parent 0343a8e commit b5d0df9

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

helpers.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,31 @@ func TimeInLoc(t time.Time, loc string) (time.Time, error) {
157157
func FormatMoney(dec float64) string {
158158
return "$" + strconv.FormatFloat(ToFixed(dec, 2), 'f', 2, 64)
159159
}
160+
161+
func appendSiteULID(siteULID string, whereSql string, args ...interface{}) (string, []interface{}, error) {
162+
if !strings.Contains(whereSql, "$SITEULID") {
163+
return whereSql, args, errors.New("No $SITEULID placeholder defined")
164+
}
165+
args = append(args, siteULID)
166+
position := len(args)
167+
if strings.Contains(whereSql, ".$SITEULID") {
168+
newSQL := strings.Split(whereSql, "$SITEULID")[0]
169+
replaceSQLParts := strings.Split(newSQL, " ")
170+
replaceSQLTablePrefix := replaceSQLParts[len(replaceSQLParts)-1]
171+
172+
whereSql = strings.Replace(whereSql, replaceSQLTablePrefix+"$SITEULID", " and "+replaceSQLTablePrefix+"site_ulid = $"+strconv.Itoa(position), -1)
173+
} else if strings.Contains(whereSql, "$SITEULID") {
174+
whereSql = strings.Replace(whereSql, "$SITEULID", " site_ulid = $"+strconv.Itoa(position), -1)
175+
} else {
176+
whereSql += " and site_ulid = $" + strconv.Itoa(position)
177+
}
178+
return whereSql, args, nil
179+
}
180+
181+
func BastardizeSql(siteULID string, whereSql string, args ...interface{}) (string, []interface{}, error) {
182+
whereSql = strings.Trim(whereSql, " ")
183+
if !strings.HasPrefix(strings.ToLower(whereSql), "where") {
184+
whereSql += "where "
185+
}
186+
return appendSiteULID(siteULID, " "+whereSql+" ", args)
187+
}

0 commit comments

Comments
 (0)