Skip to content

Commit

Permalink
* op.Interval(duration) shortcut
Browse files Browse the repository at this point in the history
 * op.SetDbParam(tx, arg, value) helper
  • Loading branch information
sdfsdhgjkbmnmxc committed Sep 4, 2021
1 parent fad37da commit fb2c54d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v0.13.0
* op.Interval(duration) shortcut
* op.SetDbParam(tx, arg, value) helper

v0.12.3
* experimental lorm.DisableLocks()/lorm.EnableLocks() functions

Expand Down
13 changes: 13 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lorm

import (
"database/sql"
"strings"

"github.com/jackc/pgx/v4"
)
Expand Down Expand Up @@ -29,3 +30,15 @@ var disableLocks = false

func DisableLocks() { disableLocks = true }
func EnableLocks() { disableLocks = false }

func SetDbParam(tx *Tx, arg, value string) (err error) {
value = strings.ReplaceAll(value, "'", "")
// Prepared statement doesn't work with SET. FIXME: add more sql safety
query := "SET " + arg + " = '" + value + "'"
if tx == nil {
_, err = conn.Exec(query)
} else {
_, err = tx.Exec(query)
}
return
}
7 changes: 7 additions & 0 deletions op/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package op

import (
"fmt"
"strconv"
"strings"
"sync/atomic"
"time"
)

var escapeSpecialReplacer = strings.NewReplacer(
Expand Down Expand Up @@ -168,3 +170,8 @@ func List(v ...Expr) Expr {
func HasPrefix(v string) string { return EscapeSpecialSymbols(v) + "%" }
func HasSuffix(v string) string { return "%" + EscapeSpecialSymbols(v) }
func Contains(v string) string { return "%" + EscapeSpecialSymbols(v) + "%" }

// Interval transform time.Duration into interval representation
func Interval(dur time.Duration) string {
return strconv.Itoa(int(dur.Seconds())) + "s"
}

0 comments on commit fb2c54d

Please sign in to comment.