Add Postgres DATE_TRUNC function
This commit is contained in:
parent
b835e25665
commit
b25b2aa213
2 changed files with 21 additions and 1 deletions
|
|
@ -332,6 +332,15 @@ var LOCALTIMESTAMP = jet.LOCALTIMESTAMP
|
||||||
// NOW returns current date and time
|
// NOW returns current date and time
|
||||||
var NOW = jet.NOW
|
var NOW = jet.NOW
|
||||||
|
|
||||||
|
// DATE_TRUNC returns the truncated date and time using optional time zone
|
||||||
|
func DATE_TRUNC(field unit, source Expression, timezone ...string) TimestampExpression {
|
||||||
|
if len(timezone) > 0 {
|
||||||
|
return jet.NewTimestampFunc("DATE_TRUNC", jet.FixedLiteral(unitToString(field)), source, jet.FixedLiteral(timezone[0]))
|
||||||
|
}
|
||||||
|
|
||||||
|
return jet.NewTimestampFunc("DATE_TRUNC", jet.FixedLiteral(unitToString(field)), source)
|
||||||
|
}
|
||||||
|
|
||||||
// --------------- Conditional Expressions Functions -------------//
|
// --------------- Conditional Expressions Functions -------------//
|
||||||
|
|
||||||
// COALESCE function returns the first of its arguments that is not null.
|
// COALESCE function returns the first of its arguments that is not null.
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
package postgres
|
package postgres
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestROW(t *testing.T) {
|
func TestROW(t *testing.T) {
|
||||||
assertSerialize(t, ROW(SELECT(Int(1))), `ROW((
|
assertSerialize(t, ROW(SELECT(Int(1))), `ROW((
|
||||||
|
|
@ -10,3 +12,12 @@ func TestROW(t *testing.T) {
|
||||||
SELECT $2
|
SELECT $2
|
||||||
), $3)`)
|
), $3)`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDATE_TRUNC(t *testing.T) {
|
||||||
|
assertSerialize(t, DATE_TRUNC(YEAR, NOW()), "DATE_TRUNC('YEAR', NOW())")
|
||||||
|
assertSerialize(
|
||||||
|
t,
|
||||||
|
DATE_TRUNC(DAY, NOW().ADD(INTERVAL(1, HOUR)), "Australia/Sydney"),
|
||||||
|
"DATE_TRUNC('DAY', NOW() + INTERVAL '1 HOUR', 'Australia/Sydney')",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue