forked from go-jet/jet
/
cast.go
161 lines (133 loc) · 4.22 KB
/
cast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package postgres
import (
"fmt"
"strconv"
"github.com/go-jet/jet/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 {
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
}
// Cast expression as castType
func (b *castImpl) AS(castType string) Expression {
return b.Cast.AS(castType)
}
// Cast expression as bool type
func (b *castImpl) AS_BOOL() BoolExpression {
return BoolExp(b.AS("boolean"))
}
// Cast expression as smallint type
func (b *castImpl) AS_SMALLINT() IntegerExpression {
return IntExp(b.AS("smallint"))
}
// Cast expression AS integer type
func (b *castImpl) AS_INTEGER() IntegerExpression {
return IntExp(b.AS("integer"))
}
// Cast expression AS bigint type
func (b *castImpl) 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 {
var castArgs string
var argLen = len(precisionAndScale)
if argLen >= 2 {
castArgs = fmt.Sprintf("(%d, %d)", precisionAndScale[0], precisionAndScale[1])
} else if argLen == 1 {
castArgs = fmt.Sprintf("(%d)", precisionAndScale[0])
}
return FloatExp(b.AS("numeric" + castArgs))
}
// Cast expression AS real type
func (b *castImpl) AS_REAL() FloatExpression {
return FloatExp(b.AS("real"))
}
// Cast expression AS double precision type
func (b *castImpl) AS_DOUBLE() FloatExpression {
return FloatExp(b.AS("double precision"))
}
// Cast expression AS text type
func (b *castImpl) AS_TEXT() StringExpression {
return StringExp(b.AS("text"))
}
func (b *castImpl) AS_CHAR(length ...int) StringExpression {
if len(length) > 0 {
return StringExp(b.AS("char(" + strconv.Itoa(length[0]) + ")"))
}
return StringExp(b.AS("char"))
}
// Cast expression AS date type
func (b *castImpl) AS_DATE() DateExpression {
return DateExp(b.AS("date"))
}
// Cast expression AS date type
func (b *castImpl) AS_DECIMAL() FloatExpression {
return FloatExp(b.AS("decimal"))
}
// Cast expression AS text type
func (b *castImpl) AS_BYTEA() StringExpression {
return StringExp(b.AS("bytea"))
}
// Cast expression AS date type
func (b *castImpl) 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 {
return TimezExp(b.AS("time with time zone"))
}
// Cast expression AS timestamp type
func (b *castImpl) AS_TIMESTAMP() TimestampExpression {
return TimestampExp(b.AS("timestamp without time zone"))
}
// Cast expression AS timestamp with timezone type
func (b *castImpl) AS_TIMESTAMPZ() TimestampzExpression {
return TimestampzExp(b.AS("timestamp with time zone"))
}
// Cast expression AS interval type
func (b *castImpl) AS_INTERVAL() IntervalExpression {
return IntervalExp(b.AS("interval"))
}