Add PostgreSQL-specific character type constructors: Text, Char, and VarChar.

This commit is contained in:
go-jet 2024-11-01 12:34:46 +01:00
parent 4f0832b0e7
commit 2183af42f4
10 changed files with 152 additions and 144 deletions

View file

@ -34,47 +34,64 @@ func Int64(value int64) IntegerExpression {
return CAST(jet.Int(value)).AS_BIGINT()
}
// 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()
}
// Uint64 is constructor for 64 bit unsigned integer expressions literals.
func Uint64(value uint64) IntegerExpression {
return CAST(jet.Uint64(value)).AS_BIGINT()
}
// Float creates new float literal expression
var Float = jet.Float
// Float32 is constructor for 32 bit float literals
func Float32(value float32) FloatExpression {
// Real is placeholder constructor for 32-bit float literals
func Real(value float32) FloatExpression {
return CAST(jet.Literal(value)).AS_REAL()
}
// Float64 is constructor for 64 bit float literals
func Float64(value float64) FloatExpression {
// Double is placeholder constructor for 64-bit float literals
func Double(value float64) FloatExpression {
return CAST(jet.Literal(value)).AS_DOUBLE()
}
// Decimal creates new float literal expression
var Decimal = jet.Decimal
// String creates new string literal expression
// 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.
func String(value string) StringExpression {
return CAST(jet.String(value)).AS_TEXT()
}
// 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...)
}
}
// Json creates new json literal expression
func Json(value interface{}) StringExpression {
switch value.(type) {

View file

@ -34,32 +34,21 @@ func TestInt64(t *testing.T) {
assertSerialize(t, Int64(val), `$1::bigint`, val)
}
func TestUint8(t *testing.T) {
val := uint8(math.MaxUint8)
assertSerialize(t, Uint8(val), `$1::smallint`, val)
}
func TestUint16(t *testing.T) {
val := uint16(math.MaxUint16)
assertSerialize(t, Uint16(val), `$1::integer`, val)
}
func TestUint32(t *testing.T) {
val := uint32(math.MaxUint32)
assertSerialize(t, Uint32(val), `$1::bigint`, val)
}
func TestUint64(t *testing.T) {
val := uint64(math.MaxUint64)
assertSerialize(t, Uint64(val), `$1::bigint`, val)
}
func TestFloat(t *testing.T) {
assertSerialize(t, Float(12.34), `$1`, float64(12.34))
assertSerialize(t, Real(12.34), `$1::real`, float32(12.34))
assertSerialize(t, Double(12.34), `$1::double precision`, float64(12.34))
}
func TestString(t *testing.T) {
assertSerialize(t, String("Some text"), `$1::text`, "Some text")
assertSerialize(t, Text("Some text"), `$1::text`, "Some text")
assertSerialize(t, Char(20)("John Doe"), `$1::char(20)`, "John Doe")
assertSerialize(t, Char()("John Doe"), `$1::char`, "John Doe")
assertSerialize(t, VarChar(20)("John Doe"), `$1::varchar(20)`, "John Doe")
assertSerialize(t, VarChar()("John Doe"), `$1::varchar`, "John Doe")
}
func TestBytea(t *testing.T) {

View file

@ -12,8 +12,8 @@ type values struct {
// Example usage:
//
// VALUES(
// WRAP(Int32(204), Float32(1.21)),
// WRAP(Int32(207), Float32(1.02)),
// WRAP(Int32(204), Real(1.21)),
// WRAP(Int32(207), Real(1.02)),
// )
func VALUES(rows ...RowExpression) values {
return values{Values: jet.Values(rows)}