2019-08-03 14:10:47 +02:00
|
|
|
package postgres
|
|
|
|
|
|
2019-08-06 10:29:04 +02:00
|
|
|
import (
|
|
|
|
|
"time"
|
2021-05-12 12:29:22 +02:00
|
|
|
|
|
|
|
|
"github.com/go-jet/jet/v2/internal/jet"
|
2019-08-06 10:29:04 +02:00
|
|
|
)
|
2019-08-03 14:10:47 +02:00
|
|
|
|
2021-12-29 19:07:59 +01:00
|
|
|
// Bool is boolean literal constructor
|
2021-12-26 17:15:43 +01:00
|
|
|
func Bool(value bool) BoolExpression {
|
|
|
|
|
return CAST(jet.Bool(value)).AS_BOOL()
|
|
|
|
|
}
|
2019-08-17 14:49:35 +02:00
|
|
|
|
2024-09-03 15:39:36 +02:00
|
|
|
// BoolArray creates new bool array literal expression
|
|
|
|
|
func BoolArray(elements []bool) BoolArrayExpression {
|
|
|
|
|
return jet.BoolArray(elements)
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-22 13:58:28 -05:00
|
|
|
// Int is constructor for 64 bit signed integer expressions literals.
|
2019-08-03 14:10:47 +02:00
|
|
|
var Int = jet.Int
|
2019-08-17 14:49:35 +02:00
|
|
|
|
2021-02-22 13:58:28 -05:00
|
|
|
// Int8 is constructor for 8 bit signed integer expressions literals.
|
2021-12-26 17:15:43 +01:00
|
|
|
func Int8(value int8) IntegerExpression {
|
|
|
|
|
return CAST(jet.Int8(value)).AS_SMALLINT()
|
|
|
|
|
}
|
2021-02-22 13:58:28 -05:00
|
|
|
|
|
|
|
|
// Int16 is constructor for 16 bit signed integer expressions literals.
|
2021-12-26 17:15:43 +01:00
|
|
|
func Int16(value int16) IntegerExpression {
|
|
|
|
|
return CAST(jet.Int16(value)).AS_SMALLINT()
|
|
|
|
|
}
|
2021-02-22 13:58:28 -05:00
|
|
|
|
|
|
|
|
// Int32 is constructor for 32 bit signed integer expressions literals.
|
2021-12-26 17:15:43 +01:00
|
|
|
func Int32(value int32) IntegerExpression {
|
|
|
|
|
return CAST(jet.Int32(value)).AS_INTEGER()
|
|
|
|
|
}
|
2021-02-22 13:58:28 -05:00
|
|
|
|
|
|
|
|
// Int64 is constructor for 64 bit signed integer expressions literals.
|
2021-12-26 17:15:43 +01:00
|
|
|
func Int64(value int64) IntegerExpression {
|
|
|
|
|
return CAST(jet.Int(value)).AS_BIGINT()
|
|
|
|
|
}
|
2021-02-22 13:58:28 -05:00
|
|
|
|
2024-11-03 11:58:39 +01:00
|
|
|
// Uint8 is constructor for 8 bit unsigned integer expressions literals.
|
|
|
|
|
func Uint8(value uint8) IntegerExpression {
|
|
|
|
|
return CAST(jet.Uint8(value)).AS_SMALLINT()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Uint16 is constructor for 16 bit unsigned integer expressions literals.
|
|
|
|
|
func Uint16(value uint16) IntegerExpression {
|
|
|
|
|
return CAST(jet.Uint16(value)).AS_INTEGER()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Uint32 is constructor for 32 bit unsigned integer expressions literals.
|
|
|
|
|
func Uint32(value uint32) IntegerExpression {
|
|
|
|
|
return CAST(jet.Uint32(value)).AS_BIGINT()
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-04 19:57:42 +01:00
|
|
|
// Uint64 is constructor for 64 bit unsigned integer expressions literals.
|
|
|
|
|
func Uint64(value uint64) IntegerExpression {
|
|
|
|
|
return CAST(jet.Uint64(value)).AS_BIGINT()
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-17 14:49:35 +02:00
|
|
|
// Float creates new float literal expression
|
2019-08-03 14:10:47 +02:00
|
|
|
var Float = jet.Float
|
2019-08-17 14:49:35 +02:00
|
|
|
|
2024-11-01 12:34:46 +01:00
|
|
|
// Real is placeholder constructor for 32-bit float literals
|
|
|
|
|
func Real(value float32) FloatExpression {
|
2024-10-06 14:21:42 +02:00
|
|
|
return CAST(jet.Literal(value)).AS_REAL()
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-01 12:34:46 +01:00
|
|
|
// Double is placeholder constructor for 64-bit float literals
|
|
|
|
|
func Double(value float64) FloatExpression {
|
2024-10-06 14:21:42 +02:00
|
|
|
return CAST(jet.Literal(value)).AS_DOUBLE()
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-09 16:25:54 +02:00
|
|
|
// Decimal creates new float literal expression
|
|
|
|
|
var Decimal = jet.Decimal
|
|
|
|
|
|
2024-11-01 12:34:46 +01:00
|
|
|
// String is a parameter constructor for the PostgreSQL text type. Using the `Text` constructor is
|
|
|
|
|
// generally preferable.
|
|
|
|
|
//
|
|
|
|
|
// WARNING: String always applies a `text` type cast, which can be problematic if a parameter is compared
|
|
|
|
|
// to a `character` column, as this may prevent index usage. In such cases, consider using the Char
|
|
|
|
|
// constructor instead. See also other PostgreSQL-specific constructors: Text, Char, and VarChar.
|
2022-05-05 13:01:42 +02:00
|
|
|
func String(value string) StringExpression {
|
|
|
|
|
return CAST(jet.String(value)).AS_TEXT()
|
|
|
|
|
}
|
2019-08-06 10:29:04 +02:00
|
|
|
|
2024-09-03 15:39:36 +02:00
|
|
|
// StringArray creates new string array literal expression
|
|
|
|
|
func StringArray(elements []string) StringArrayExpression {
|
|
|
|
|
return jet.StringArray(elements)
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-01 12:34:46 +01:00
|
|
|
// Text is a parameter constructor for the PostgreSQL text type. This constructor also adds an
|
|
|
|
|
// explicit placeholder type cast to text in the generated query, such as `$3::text`.
|
|
|
|
|
// Example usage:
|
|
|
|
|
//
|
|
|
|
|
// Text("English")
|
|
|
|
|
func Text(value string) StringExpression {
|
|
|
|
|
return CAST(jet.Literal(value)).AS_TEXT()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Char is a parameter constructor for the PostgreSQL character type. This constructor also adds an
|
|
|
|
|
// explicit placeholder type cast to text in the generated query, such as `$3::char(30)`.
|
|
|
|
|
// Example usage:
|
|
|
|
|
//
|
|
|
|
|
// Char(20)("English")
|
|
|
|
|
func Char(length ...int) func(value string) StringExpression {
|
|
|
|
|
return func(value string) StringExpression {
|
|
|
|
|
return CAST(StringExp(jet.Literal(value))).AS_CHAR(length...)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VarChar is a parameter constructor for the PostgreSQL character varying type. This constructor
|
|
|
|
|
// also adds an explicit placeholder type cast to text in the generated query, such as `$3::varchar(30)`.
|
|
|
|
|
// Example usage:
|
|
|
|
|
//
|
|
|
|
|
// VarChar(20)("English")
|
|
|
|
|
// VarChar()("English")
|
|
|
|
|
func VarChar(length ...int) func(value string) StringExpression {
|
|
|
|
|
return func(value string) StringExpression {
|
|
|
|
|
return CAST(StringExp(jet.Literal(value))).AS_VARCHAR(length...)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-04 14:12:08 +02:00
|
|
|
// Json creates new json literal expression
|
2022-06-04 14:22:33 +02:00
|
|
|
func Json(value interface{}) StringExpression {
|
|
|
|
|
switch value.(type) {
|
|
|
|
|
case string, []byte:
|
|
|
|
|
default:
|
|
|
|
|
panic("Bytea parameter value has to be of the type string or []byte")
|
|
|
|
|
}
|
|
|
|
|
return StringExp(CAST(jet.Literal(value)).AS("json"))
|
2022-06-04 14:12:08 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-11 13:20:07 +02:00
|
|
|
// UUID is a helper function to create string literal expression from uuid object
|
|
|
|
|
// value can be any uuid type with a String method
|
|
|
|
|
var UUID = jet.UUID
|
|
|
|
|
|
2021-05-12 12:29:22 +02:00
|
|
|
// Bytea creates new bytea literal expression
|
2025-02-28 18:23:15 +01:00
|
|
|
func Bytea(value interface{}) ByteaExpression {
|
2021-05-12 12:29:22 +02:00
|
|
|
switch value.(type) {
|
|
|
|
|
case string, []byte:
|
|
|
|
|
default:
|
|
|
|
|
panic("Bytea parameter value has to be of the type string or []byte")
|
|
|
|
|
}
|
|
|
|
|
return CAST(jet.Literal(value)).AS_BYTEA()
|
2019-08-06 10:29:04 +02:00
|
|
|
}
|
2019-08-17 14:49:35 +02:00
|
|
|
|
|
|
|
|
// Date creates new date literal expression
|
2022-05-16 11:51:47 +02:00
|
|
|
func Date(year int, month time.Month, day int) DateExpression {
|
2019-08-03 14:10:47 +02:00
|
|
|
return CAST(jet.Date(year, month, day)).AS_DATE()
|
|
|
|
|
}
|
2019-08-17 14:49:35 +02:00
|
|
|
|
|
|
|
|
// DateT creates new date literal expression from time.Time object
|
2022-05-16 11:51:47 +02:00
|
|
|
func DateT(t time.Time) DateExpression {
|
2019-08-06 11:41:45 +02:00
|
|
|
return CAST(jet.DateT(t)).AS_DATE()
|
|
|
|
|
}
|
2019-08-17 14:49:35 +02:00
|
|
|
|
|
|
|
|
// Time creates new time literal expression
|
2022-05-16 11:51:47 +02:00
|
|
|
func Time(hour, minute, second int, nanoseconds ...time.Duration) TimeExpression {
|
2019-08-13 10:16:26 +02:00
|
|
|
return CAST(jet.Time(hour, minute, second, nanoseconds...)).AS_TIME()
|
2019-08-03 14:10:47 +02:00
|
|
|
}
|
2019-08-17 14:49:35 +02:00
|
|
|
|
|
|
|
|
// TimeT creates new time literal expression from time.Time object
|
2022-05-16 11:51:47 +02:00
|
|
|
func TimeT(t time.Time) TimeExpression {
|
2019-08-06 11:41:45 +02:00
|
|
|
return CAST(jet.TimeT(t)).AS_TIME()
|
|
|
|
|
}
|
2019-08-17 14:49:35 +02:00
|
|
|
|
|
|
|
|
// Timez creates new time with time zone literal expression
|
2022-05-16 11:51:47 +02:00
|
|
|
func Timez(hour, minute, second int, milliseconds time.Duration, timezone string) TimezExpression {
|
2019-08-03 14:10:47 +02:00
|
|
|
return CAST(jet.Timez(hour, minute, second, milliseconds, timezone)).AS_TIMEZ()
|
|
|
|
|
}
|
2019-08-17 14:49:35 +02:00
|
|
|
|
|
|
|
|
// TimezT creates new time with time zone literal expression from time.Time object
|
2022-05-16 11:51:47 +02:00
|
|
|
func TimezT(t time.Time) TimezExpression {
|
2019-08-06 11:41:45 +02:00
|
|
|
return CAST(jet.TimezT(t)).AS_TIMEZ()
|
|
|
|
|
}
|
2019-08-17 14:49:35 +02:00
|
|
|
|
|
|
|
|
// Timestamp creates new timestamp literal expression
|
2022-05-16 11:51:47 +02:00
|
|
|
func Timestamp(year int, month time.Month, day, hour, minute, second int, milliseconds ...time.Duration) TimestampExpression {
|
2019-08-13 10:16:26 +02:00
|
|
|
return CAST(jet.Timestamp(year, month, day, hour, minute, second, milliseconds...)).AS_TIMESTAMP()
|
2019-08-03 14:10:47 +02:00
|
|
|
}
|
2019-08-17 14:49:35 +02:00
|
|
|
|
|
|
|
|
// TimestampT creates new timestamp literal expression from time.Time object
|
2022-05-16 11:51:47 +02:00
|
|
|
func TimestampT(t time.Time) TimestampExpression {
|
2019-08-17 14:49:35 +02:00
|
|
|
return CAST(jet.TimestampT(t)).AS_TIMESTAMP()
|
2019-08-06 11:41:45 +02:00
|
|
|
}
|
2019-08-17 14:49:35 +02:00
|
|
|
|
|
|
|
|
// Timestampz creates new timestamp with time zone literal expression
|
2022-05-16 11:51:47 +02:00
|
|
|
func Timestampz(year int, month time.Month, day, hour, minute, second int, milliseconds time.Duration, timezone string) TimestampzExpression {
|
2019-08-03 14:10:47 +02:00
|
|
|
return CAST(jet.Timestampz(year, month, day, hour, minute, second, milliseconds, timezone)).AS_TIMESTAMPZ()
|
|
|
|
|
}
|
2019-08-17 14:49:35 +02:00
|
|
|
|
|
|
|
|
// TimestampzT creates new timestamp literal expression from time.Time object
|
2022-05-16 11:51:47 +02:00
|
|
|
func TimestampzT(t time.Time) TimestampzExpression {
|
2019-08-06 11:41:45 +02:00
|
|
|
return CAST(jet.TimestampzT(t)).AS_TIMESTAMPZ()
|
|
|
|
|
}
|