/
Operators.scala
254 lines (212 loc) · 8.46 KB
/
Operators.scala
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* Copyright 2013 - 2019 Outworkers Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.outworkers.phantom.builder.ops
import java.util.Date
import com.datastax.driver.core.Session
import com.outworkers.phantom.builder.QueryBuilder
import com.outworkers.phantom.builder.clauses.OperatorClause.Condition
import com.outworkers.phantom.builder.clauses.{OperatorClause, TypedClause, WhereClause}
import com.outworkers.phantom.builder.primitives.Primitive
import com.outworkers.phantom.builder.query.engine.CQLQuery
import com.outworkers.phantom.builder.query.prepared.PrepareMark
import com.outworkers.phantom.builder.syntax.CQLSyntax
import com.outworkers.phantom.column.{AbstractColumn, Column, TimeUUIDColumn}
import com.outworkers.phantom.connectors.SessionAugmenterImplicits
import com.outworkers.phantom.{CassandraTable, TableAliases}
import org.joda.time.{DateTime, DateTimeZone}
import shapeless.{=:!=, HList}
sealed class CqlFunction extends SessionAugmenterImplicits
sealed class UnixTimestampOfCqlFunction extends CqlFunction {
def apply[T <: CassandraTable[T, R], R](pf: TableAliases[T, R]#TimeUUIDColumn)(
implicit ev: Primitive[Long],
session: Session
): TypedClause.Condition[Option[Long]] = {
new TypedClause.Condition(QueryBuilder.Select.unixTimestampOf(pf.name), row => {
if (row.getColumnDefinitions.contains(s"system.unixtimestampof(${pf.name})")) {
ev.fromRow(s"system.unixtimestampof(${pf.name})", row).toOption
} else {
ev.fromRow(s"unixtimestampof(${pf.name})", row).toOption
}
})
}
}
sealed class TTLOfFunction extends CqlFunction {
def apply(pf: Column[_, _, _])(implicit ev: Primitive[Int]): TypedClause.Condition[Option[Int]] = {
new TypedClause.Condition(QueryBuilder.Select.ttl(pf.name), row => {
ev.fromRow(s"ttl(${pf.name})", row).toOption
})
}
}
sealed class DateOfCqlFunction extends CqlFunction {
protected[this] def apply(nm: String)(
implicit ev: Primitive[DateTime],
session: Session
): TypedClause.Condition[Option[DateTime]] = {
new TypedClause.Condition(QueryBuilder.Select.dateOf(nm), row => {
if (row.getColumnDefinitions.contains(s"system.dateof($nm)")) {
ev.fromRow(s"system.dateof($nm)", row).toOption
} else {
ev.fromRow(s"dateof($nm)", row).toOption
}
})
}
def apply[T <: CassandraTable[T, R], R](pf: TimeUUIDColumn[T, R])(
implicit ev: Primitive[DateTime],
session: Session
): TypedClause.Condition[Option[DateTime]] = apply(pf.name)
def apply(op: OperatorClause.Condition)(
implicit ev: Primitive[DateTime],
session: Session
): TypedClause.Condition[Option[DateTime]] = apply(op.qb.queryString)
}
sealed class AggregationFunction(operator: String) extends CqlFunction {
protected[this] def apply[T](nm: String)(
implicit ev: Primitive[T],
numeric: Numeric[T],
session: Session
): TypedClause.Condition[Option[T]] = {
new TypedClause.Condition(QueryBuilder.Select.aggregation(operator, nm), row => {
if (row.getColumnDefinitions.contains(s"system.$operator($nm)")) {
ev.fromRow(s"system.$operator($nm)", row).toOption
} else {
ev.fromRow(s"$operator($nm)", row).toOption
}
})
}
def apply[T](pf: AbstractColumn[T])(
implicit ev: Primitive[T],
numeric: Numeric[T],
session: Session
): TypedClause.Condition[Option[T]] = apply(pf.name)
}
sealed class CountCqlFunction extends CqlFunction {
val operator = CQLSyntax.Selection.count
val nm = CQLSyntax.Symbols.`*`
def apply()(
implicit ev: Primitive[Long],
session: Session
): TypedClause.Condition[Option[Long]] = {
new TypedClause.Condition(QueryBuilder.Select.aggregation(operator, nm), row => {
if (row.getColumnDefinitions.contains(s"system.$operator($nm)")) {
ev.fromRow(s"system.$operator", row).toOption
} else {
ev.fromRow(s"$operator", row).toOption
}
})
}
def apply(col: AbstractColumn[_])(
implicit ev: Primitive[Long],
session: Session
): TypedClause.Condition[Option[Long]] = {
new TypedClause.Condition(QueryBuilder.Select.aggregation(operator, col.name), row => {
if (row.getColumnDefinitions.contains(s"system.$operator(${col.name})")) {
ev.fromRow(s"system.$operator(${col.name})", row).toOption
} else {
ev.fromRow(s"$operator(${col.name})", row).toOption
}
})
}
}
sealed class SumCqlFunction extends AggregationFunction(CQLSyntax.Selection.sum)
sealed class AvgCqlFunction extends AggregationFunction(CQLSyntax.Selection.avg)
sealed class MinCqlFunction extends AggregationFunction(CQLSyntax.Selection.min)
sealed class MaxCqlFunction extends AggregationFunction(CQLSyntax.Selection.max)
sealed class NowCqlFunction extends CqlFunction {
def apply()(implicit ev: Primitive[Long], session: Session): OperatorClause.Condition = {
new OperatorClause.Condition(QueryBuilder.Select.now())
}
}
private[phantom] trait TimeUUIDOperator {
def apply(date: Date): OperatorClause.Condition = apply(new DateTime(date, DateTimeZone.UTC))
def apply(date: DateTime): OperatorClause.Condition = {
new Condition(fn(CQLQuery.escape(new DateTime(date).toString())))
}
def apply(mark: PrepareMark): OperatorClause.Prepared[Long] = {
new OperatorClause.Prepared[Long](fn(mark.qb.queryString))
}
def fn: String => CQLQuery
}
private[phantom] class MaxTimeUUID extends CqlFunction with TimeUUIDOperator {
override def fn: (String) => CQLQuery = QueryBuilder.Select.maxTimeuuid
}
private[phantom] class MinTimeUUID extends CqlFunction with TimeUUIDOperator {
override def fn: (String) => CQLQuery = QueryBuilder.Select.minTimeuuid
}
sealed class WritetimeCqlFunction extends CqlFunction {
def apply(col: AbstractColumn[_])(implicit ev: Primitive[Long]): TypedClause.Condition[Long] = {
val qb = QueryBuilder.Select.writetime(col.name)
new TypedClause.Condition(qb, row =>
ev.deserialize(row.getBytesUnsafe(qb.queryString), row.version)
)
}
}
sealed class TokenConstructor[P <: HList, TP <: TokenTypes.Root](val mapper: Seq[String]) {
private[this] def joinOp(comp: Seq[String], op: String): WhereClause.Condition = {
new WhereClause.Condition(
QueryBuilder.Where.token(mapper: _*)
.pad.append(op)
.pad.append(QueryBuilder.Where.token(comp: _*))
)
}
/**
* An equals comparison clause between token definitions.
*
* @param tk The token constructor to compare against.
* @tparam VL
* @return
*/
def eqs[VL <: HList, TT <: TokenTypes.Root](tk: TokenConstructor[VL, TT])(
implicit ev: TT =:!= TP
): WhereClause.Condition = {
joinOp(tk.mapper, CQLSyntax.Operators.eqs)
}
def <[VL <: HList, TT <: TokenTypes.Root](tk: TokenConstructor[VL, TT])(
implicit ev: TT =:!= TP
): WhereClause.Condition = {
joinOp(tk.mapper, CQLSyntax.Operators.lt)
}
def <=[VL <: HList, TT <: TokenTypes.Root](tk: TokenConstructor[VL, TT])(
implicit ev: TT =:!= TP
): WhereClause.Condition = {
joinOp(tk.mapper, CQLSyntax.Operators.lte)
}
def >[VL <: HList, TT <: TokenTypes.Root](tk: TokenConstructor[VL, TT])(
implicit ev: TT =:!= TP
): WhereClause.Condition = {
joinOp(tk.mapper, CQLSyntax.Operators.gt)
}
def >=[VL <: HList, TT <: TokenTypes.Root](tk: TokenConstructor[VL, TT])(
implicit ev: TT =:!= TP
): WhereClause.Condition = {
joinOp(tk.mapper, CQLSyntax.Operators.gte)
}
}
sealed class TokenCqlFunction extends CqlFunction with TokenComparisonOps
trait Operators {
object dateOf extends DateOfCqlFunction
object unixTimestampOf extends UnixTimestampOfCqlFunction
object minTimeuuid extends MinTimeUUID
object maxTimeuuid extends MaxTimeUUID
object token extends TokenCqlFunction
object now extends NowCqlFunction
object writetime extends WritetimeCqlFunction
object ttl extends TTLOfFunction
object count extends CountCqlFunction
object sum extends SumCqlFunction
object min extends MinCqlFunction
object max extends MaxCqlFunction
object avg extends AvgCqlFunction
}