diff --git a/mysql/cast.go b/mysql/cast.go index ca647a5..dcf1f57 100644 --- a/mysql/cast.go +++ b/mysql/cast.go @@ -5,66 +5,40 @@ import ( "strconv" ) -type cast interface { - // AS casts expressions as castType type - AS(castType string) Expression - // AS_CHAR casts expression as char with optional length - AS_CHAR(length ...int) StringExpression - // AS_DATE casts expression AS date type - AS_DATE() DateExpression - // AS_FLOAT casts expressions as float type - AS_FLOAT() FloatExpression - // AS_DOUBLE casts expressions as double type - AS_DOUBLE() FloatExpression - // AS_DECIMAL casts expression AS numeric type - AS_DECIMAL() FloatExpression - // AS_TIME casts expression AS time type - AS_TIME() TimeExpression - // AS_DATETIME casts expression as datetime type - AS_DATETIME() DateTimeExpression - // AS_SIGNED casts expressions as signed integer type - AS_SIGNED() IntegerExpression - // AS_UNSIGNED casts expression as unsigned integer type - AS_UNSIGNED() IntegerExpression - // AS_BINARY casts expression as binary type - AS_BINARY() StringExpression -} - -type castImpl struct { +type cast struct { jet.Cast } // CAST function converts a expr (of any type) into latter specified datatype. -func CAST(expr Expression) cast { - castImpl := &castImpl{} +func CAST(expr Expression) *cast { + ret := &cast{} + ret.Cast = jet.NewCastImpl(expr) - castImpl.Cast = jet.NewCastImpl(expr) - - return castImpl + return ret } // AS casts expressions to castType -func (c *castImpl) AS(castType string) Expression { +func (c *cast) AS(castType string) Expression { return c.Cast.AS(castType) } // AS_DATETIME cast expression to DATETIME type -func (c *castImpl) AS_DATETIME() DateTimeExpression { +func (c *cast) AS_DATETIME() DateTimeExpression { return DateTimeExp(c.AS("DATETIME")) } // AS_SIGNED casts expression to SIGNED type -func (c *castImpl) AS_SIGNED() IntegerExpression { +func (c *cast) AS_SIGNED() IntegerExpression { return IntExp(c.AS("SIGNED")) } // AS_UNSIGNED casts expression to UNSIGNED type -func (c *castImpl) AS_UNSIGNED() IntegerExpression { +func (c *cast) AS_UNSIGNED() IntegerExpression { return IntExp(c.AS("UNSIGNED")) } // AS_CHAR casts expression to CHAR type with optional length -func (c *castImpl) AS_CHAR(length ...int) StringExpression { +func (c *cast) AS_CHAR(length ...int) StringExpression { if len(length) > 0 { return StringExp(c.AS("CHAR(" + strconv.Itoa(length[0]) + ")")) } @@ -73,29 +47,29 @@ func (c *castImpl) AS_CHAR(length ...int) StringExpression { } // AS_DATE casts expression AS DATE type -func (c *castImpl) AS_DATE() DateExpression { +func (c *cast) AS_DATE() DateExpression { return DateExp(c.AS("DATE")) } -func (c *castImpl) AS_FLOAT() FloatExpression { +func (c *cast) AS_FLOAT() FloatExpression { return FloatExp(c.AS("FLOAT")) } -func (c *castImpl) AS_DOUBLE() FloatExpression { +func (c *cast) AS_DOUBLE() FloatExpression { return FloatExp(c.AS("DOUBLE")) } // AS_DECIMAL casts expression AS DECIMAL type -func (c *castImpl) AS_DECIMAL() FloatExpression { +func (c *cast) AS_DECIMAL() FloatExpression { return FloatExp(c.AS("DECIMAL")) } // AS_TIME casts expression AS TIME type -func (c *castImpl) AS_TIME() TimeExpression { +func (c *cast) AS_TIME() TimeExpression { return TimeExp(c.AS("TIME")) } // AS_BINARY casts expression as BINARY type -func (c *castImpl) AS_BINARY() StringExpression { +func (c *cast) AS_BINARY() StringExpression { return StringExp(c.AS("BINARY")) } diff --git a/postgres/cast.go b/postgres/cast.go index e3a17a4..935cb08 100644 --- a/postgres/cast.go +++ b/postgres/cast.go @@ -7,84 +7,45 @@ import ( "github.com/go-jet/jet/v2/internal/jet" ) -type cast interface { - AS(castType string) Expression - // Cast expression AS bool type - AS_BOOL() BoolExpression - // Cast expression AS smallint type - AS_SMALLINT() IntegerExpression - // Cast expression AS integer type - AS_INTEGER() IntegerExpression - // Cast expression AS bigint type - AS_BIGINT() IntegerExpression - // Cast expression AS numeric type, using precision and optionally scale - AS_NUMERIC(precisionAndScale ...int) FloatExpression - // Cast expression AS real type - AS_REAL() FloatExpression - // Cast expression AS double precision type - AS_DOUBLE() FloatExpression - // Cast expression AS char with optional length - AS_CHAR(length ...int) StringExpression - // Cast expression AS date type - AS_DATE() DateExpression - // Cast expression AS numeric type, using precision and optionally scale - AS_DECIMAL() FloatExpression - // Cast expression AS time type - AS_TIME() TimeExpression - // Cast expression AS text type - AS_TEXT() StringExpression - // Cast expression AS bytea type - AS_BYTEA() StringExpression - // Cast expression AS time with time timezone type - AS_TIMEZ() TimezExpression - // Cast expression AS timestamp type - AS_TIMESTAMP() TimestampExpression - // Cast expression AS timestamp with timezone type - AS_TIMESTAMPZ() TimestampzExpression - // Cast expression AS interval type - AS_INTERVAL() IntervalExpression -} - -type castImpl struct { +type cast struct { jet.Cast } -// CAST function converts a expr (of any type) into latter specified datatype. -func CAST(expr Expression) cast { - castImpl := &castImpl{} +// CAST function converts an expr (of any type) into later specified datatype. +func CAST(expr Expression) *cast { + ret := &cast{} + ret.Cast = jet.NewCastImpl(expr) - castImpl.Cast = jet.NewCastImpl(expr) - - return castImpl + return ret } -// Cast expression as castType -func (b *castImpl) AS(castType string) Expression { +// AS casts expression as castType +func (b *cast) AS(castType string) Expression { return b.Cast.AS(castType) } -// Cast expression as bool type -func (b *castImpl) AS_BOOL() BoolExpression { +// AS_BOOL casts expression as bool type +func (b *cast) AS_BOOL() BoolExpression { return BoolExp(b.AS("boolean")) } -// Cast expression as smallint type -func (b *castImpl) AS_SMALLINT() IntegerExpression { +// AS_SMALLINT casts expression as smallint type +func (b *cast) AS_SMALLINT() IntegerExpression { return IntExp(b.AS("smallint")) } -// Cast expression AS integer type -func (b *castImpl) AS_INTEGER() IntegerExpression { +// AS_INTEGER casts expression AS integer type +func (b *cast) AS_INTEGER() IntegerExpression { return IntExp(b.AS("integer")) } -// Cast expression AS bigint type -func (b *castImpl) AS_BIGINT() IntegerExpression { +// AS_BIGINT casts expression AS bigint type +func (b *cast) AS_BIGINT() IntegerExpression { return IntExp(b.AS("bigint")) } -// Cast expression AS numeric type, using precision and optionally scale -func (b *castImpl) AS_NUMERIC(precisionAndScale ...int) FloatExpression { +// AS_NUMERIC casts expression as numeric type, using precision and optionally scale +func (b *cast) AS_NUMERIC(precisionAndScale ...int) FloatExpression { var castArgs string var argLen = len(precisionAndScale) @@ -97,22 +58,23 @@ func (b *castImpl) AS_NUMERIC(precisionAndScale ...int) FloatExpression { return FloatExp(b.AS("numeric" + castArgs)) } -// Cast expression AS real type -func (b *castImpl) AS_REAL() FloatExpression { +// AS_REAL casts expression AS real type +func (b *cast) AS_REAL() FloatExpression { return FloatExp(b.AS("real")) } -// Cast expression AS double precision type -func (b *castImpl) AS_DOUBLE() FloatExpression { +// AS_DOUBLE casts expression AS double precision type +func (b *cast) AS_DOUBLE() FloatExpression { return FloatExp(b.AS("double precision")) } -// Cast expression AS text type -func (b *castImpl) AS_TEXT() StringExpression { +// AS_TEXT casts expression AS text type +func (b *cast) AS_TEXT() StringExpression { return StringExp(b.AS("text")) } -func (b *castImpl) AS_CHAR(length ...int) StringExpression { +// AS_CHAR casts expression AS a character type +func (b *cast) AS_CHAR(length ...int) StringExpression { if len(length) > 0 { return StringExp(b.AS("char(" + strconv.Itoa(length[0]) + ")")) } @@ -120,42 +82,51 @@ func (b *castImpl) AS_CHAR(length ...int) StringExpression { return StringExp(b.AS("char")) } -// Cast expression AS date type -func (b *castImpl) AS_DATE() DateExpression { +// AS_VARCHAR casts expression AS a character varying type +func (b *cast) AS_VARCHAR(length ...int) StringExpression { + if len(length) > 0 { + return StringExp(b.AS("varchar(" + strconv.Itoa(length[0]) + ")")) + } + + return StringExp(b.AS("varchar")) +} + +// AS_DATE casts expression AS date type +func (b *cast) AS_DATE() DateExpression { return DateExp(b.AS("date")) } -// Cast expression AS date type -func (b *castImpl) AS_DECIMAL() FloatExpression { +// AS_DECIMAL casts expression AS date type +func (b *cast) AS_DECIMAL() FloatExpression { return FloatExp(b.AS("decimal")) } -// Cast expression AS text type -func (b *castImpl) AS_BYTEA() StringExpression { +// AS_BYTEA casts expression AS text type +func (b *cast) AS_BYTEA() StringExpression { return StringExp(b.AS("bytea")) } -// Cast expression AS date type -func (b *castImpl) AS_TIME() TimeExpression { +// AS_TIME casts expression AS date type +func (b *cast) AS_TIME() TimeExpression { return TimeExp(b.AS("time without time zone")) } -// Cast expression AS time with time timezone type -func (b *castImpl) AS_TIMEZ() TimezExpression { +// AS_TIMEZ casts expression AS time with time timezone type +func (b *cast) AS_TIMEZ() TimezExpression { return TimezExp(b.AS("time with time zone")) } -// Cast expression AS timestamp type -func (b *castImpl) AS_TIMESTAMP() TimestampExpression { +// AS_TIMESTAMP casts expression AS timestamp type +func (b *cast) AS_TIMESTAMP() TimestampExpression { return TimestampExp(b.AS("timestamp without time zone")) } -// Cast expression AS timestamp with timezone type -func (b *castImpl) AS_TIMESTAMPZ() TimestampzExpression { +// AS_TIMESTAMPZ casts expression AS timestamp with timezone type +func (b *cast) AS_TIMESTAMPZ() TimestampzExpression { return TimestampzExp(b.AS("timestamp with time zone")) } -// Cast expression AS interval type -func (b *castImpl) AS_INTERVAL() IntervalExpression { +// AS_INTERVAL casts expression AS interval type +func (b *cast) AS_INTERVAL() IntervalExpression { return IntervalExp(b.AS("interval")) } diff --git a/sqlite/cast.go b/sqlite/cast.go index 517fb95..0f68f43 100644 --- a/sqlite/cast.go +++ b/sqlite/cast.go @@ -4,52 +4,44 @@ import ( "github.com/go-jet/jet/v2/internal/jet" ) -type cast interface { - AS(castType string) Expression - AS_TEXT() StringExpression - AS_NUMERIC() FloatExpression - AS_INTEGER() IntegerExpression - AS_REAL() FloatExpression - AS_BLOB() StringExpression -} - -type castImpl struct { +type cast struct { jet.Cast } // CAST function converts a expr (of any type) into latter specified datatype. -func CAST(expr Expression) cast { - castImpl := &castImpl{} - castImpl.Cast = jet.NewCastImpl(expr) - return castImpl +func CAST(expr Expression) *cast { + ret := &cast{} + ret.Cast = jet.NewCastImpl(expr) + + return ret } // AS casts expressions to castType -func (c *castImpl) AS(castType string) Expression { +func (c *cast) AS(castType string) Expression { return c.Cast.AS(castType) } // AS_TEXT cast expression to TEXT type -func (c *castImpl) AS_TEXT() StringExpression { +func (c *cast) AS_TEXT() StringExpression { return StringExp(c.AS("TEXT")) } // AS_NUMERIC cast expression to NUMERIC type -func (c *castImpl) AS_NUMERIC() FloatExpression { +func (c *cast) AS_NUMERIC() FloatExpression { return FloatExp(c.AS("NUMERIC")) } // AS_INTEGER cast expression to INTEGER type -func (c *castImpl) AS_INTEGER() IntegerExpression { +func (c *cast) AS_INTEGER() IntegerExpression { return IntExp(c.AS("INTEGER")) } // AS_REAL cast expression to REAL type -func (c *castImpl) AS_REAL() FloatExpression { +func (c *cast) AS_REAL() FloatExpression { return FloatExp(c.AS("REAL")) } // AS_BLOB cast expression to BLOB type -func (c *castImpl) AS_BLOB() StringExpression { +func (c *cast) AS_BLOB() StringExpression { return StringExp(c.AS("BLOB")) }