Skip to content

Commit ee0606b

Browse files
committed
rename SQLLogicalFunction to SQLConditionalFunction, update operators mixed in
1 parent 14c1e9b commit ee0606b

File tree

4 files changed

+49
-42
lines changed

4 files changed

+49
-42
lines changed

sql/src/main/scala/app/softnetwork/elastic/sql/SQLFunction.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -528,18 +528,18 @@ case class FormatDateTime(identifier: SQLIdentifier, format: String)
528528
s"DateTimeFormatter.ofPattern('$format').format($base)"
529529
}
530530

531-
sealed trait SQLLogicalFunction[In <: SQLType]
531+
sealed trait SQLConditionalFunction[In <: SQLType]
532532
extends SQLTransformFunction[In, SQLBool]
533533
with SQLFunctionWithIdentifier {
534-
def operator: SQLLogicalOperator
534+
def operator: SQLConditionalOperator
535535
override def outputType: SQLBool = SQLTypes.Boolean
536536
override def toPainless(base: String, idx: Int): String = s"($base$painless)"
537537
}
538538

539539
case class SQLIsNullFunction(identifier: SQLIdentifier)
540540
extends SQLExpr("isnull")
541-
with SQLLogicalFunction[SQLAny] {
542-
override def operator: SQLLogicalOperator = IsNull
541+
with SQLConditionalFunction[SQLAny] {
542+
override def operator: SQLConditionalOperator = IsNull
543543
override def inputType: SQLAny = SQLTypes.Any
544544
override def painless: String = s" == null"
545545
override def toPainless(base: String, idx: Int): String = {
@@ -552,8 +552,8 @@ case class SQLIsNullFunction(identifier: SQLIdentifier)
552552

553553
case class SQLIsNotNullFunction(identifier: SQLIdentifier)
554554
extends SQLExpr("isnotnull")
555-
with SQLLogicalFunction[SQLAny] {
556-
override def operator: SQLLogicalOperator = IsNotNull
555+
with SQLConditionalFunction[SQLAny] {
556+
override def operator: SQLConditionalOperator = IsNotNull
557557
override def inputType: SQLAny = SQLTypes.Any
558558
override def painless: String = s" != null"
559559
override def toPainless(base: String, idx: Int): String = {
@@ -564,8 +564,8 @@ case class SQLIsNotNullFunction(identifier: SQLIdentifier)
564564
}
565565
}
566566

567-
case class SQLCoalesce(values: List[PainlessScript]) extends SQLLogicalFunction[SQLAny] {
568-
override def operator: SQLLogicalOperator = Coalesce
567+
case class SQLCoalesce(values: List[PainlessScript]) extends SQLConditionalFunction[SQLAny] {
568+
override def operator: SQLConditionalOperator = Coalesce
569569

570570
override def identifier: SQLIdentifier = SQLIdentifier("")
571571

@@ -605,8 +605,8 @@ case class SQLCoalesce(values: List[PainlessScript]) extends SQLLogicalFunction[
605605
}
606606

607607
case class SQLNullIf(expr1: PainlessScript, expr2: PainlessScript)
608-
extends SQLLogicalFunction[SQLAny] {
609-
override def operator: SQLLogicalOperator = NullIf
608+
extends SQLConditionalFunction[SQLAny] {
609+
override def operator: SQLConditionalOperator = NullIf
610610

611611
override def identifier: SQLIdentifier = SQLIdentifier("")
612612

sql/src/main/scala/app/softnetwork/elastic/sql/SQLOperator.scala

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package app.softnetwork.elastic.sql
22

3-
trait SQLOperator extends SQLToken with PainlessScript {
3+
trait SQLOperator extends SQLToken with PainlessScript with SQLRegex {
44
override def painless: String = this match {
55
case And => "&&"
66
case Or => "||"
77
case Not => "!"
88
case In => ".contains"
99
case Like | Match => ".matches"
10+
case Eq => "=="
11+
case Ne => "!="
1012
case _ => sql
1113
}
1214
}
@@ -24,12 +26,6 @@ case object Modulo extends SQLExpr("%") with ArithmeticOperator
2426
sealed trait SQLExpressionOperator extends SQLOperator
2527

2628
sealed trait SQLComparisonOperator extends SQLExpressionOperator with PainlessScript {
27-
override def painless: String = this match {
28-
case Eq => "=="
29-
case Ne => "!="
30-
case other => other.sql
31-
}
32-
3329
def not: SQLComparisonOperator = this match {
3430
case Eq => Ne
3531
case Ne | Diff => Eq
@@ -47,22 +43,31 @@ case object Ge extends SQLExpr(">=") with SQLComparisonOperator
4743
case object Gt extends SQLExpr(">") with SQLComparisonOperator
4844
case object Le extends SQLExpr("<=") with SQLComparisonOperator
4945
case object Lt extends SQLExpr("<") with SQLComparisonOperator
46+
case object In extends SQLExpr("in") with SQLComparisonOperator
47+
case object Like extends SQLExpr("like") with SQLComparisonOperator
48+
case object Between extends SQLExpr("between") with SQLComparisonOperator
49+
case object IsNull extends SQLExpr("is null") with SQLComparisonOperator with SQLConditionalOperator
50+
case object IsNotNull
51+
extends SQLExpr("is not null")
52+
with SQLComparisonOperator
53+
with SQLConditionalOperator
54+
case object Match extends SQLExpr("match") with SQLComparisonOperator
55+
case object Against extends SQLExpr("against") with SQLRegex
5056

51-
sealed trait SQLLogicalOperator extends SQLExpressionOperator with SQLRegex
57+
sealed trait SQLLogicalOperator extends SQLExpressionOperator
5258

53-
case object In extends SQLExpr("in") with SQLLogicalOperator
54-
case object Like extends SQLExpr("like") with SQLLogicalOperator
55-
case object Between extends SQLExpr("between") with SQLLogicalOperator
56-
case object IsNull extends SQLExpr("is null") with SQLLogicalOperator
57-
case object IsNotNull extends SQLExpr("is not null") with SQLLogicalOperator
5859
case object Not extends SQLExpr("not") with SQLLogicalOperator
59-
case object Match extends SQLExpr("match") with SQLLogicalOperator
60-
case object Coalesce extends SQLExpr("coalesce") with SQLLogicalOperator
61-
case object NullIf extends SQLExpr("nullif") with SQLLogicalOperator
62-
case object Exists extends SQLExpr("exists") with SQLLogicalOperator
63-
case object Cast extends SQLExpr("cast") with SQLLogicalOperator
6460

65-
case object Against extends SQLExpr("against") with SQLRegex
61+
sealed trait SQLConditionalOperator extends SQLExpressionOperator
62+
case object Coalesce extends SQLExpr("coalesce") with SQLConditionalOperator
63+
case object NullIf extends SQLExpr("nullif") with SQLConditionalOperator
64+
case object Exists extends SQLExpr("exists") with SQLConditionalOperator
65+
case object Cast extends SQLExpr("cast") with SQLConditionalOperator
66+
case object Case extends SQLExpr("case") with SQLConditionalOperator
67+
case object When extends SQLExpr("when") with SQLConditionalOperator
68+
case object Then extends SQLExpr("then") with SQLConditionalOperator
69+
case object Else extends SQLExpr("else") with SQLConditionalOperator
70+
case object End extends SQLExpr("end") with SQLConditionalOperator
6671

6772
sealed trait SQLPredicateOperator extends SQLLogicalOperator
6873

sql/src/main/scala/app/softnetwork/elastic/sql/SQLParser.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,12 @@ trait SQLParser extends RegexParsers with PackratParsers {
290290
SQLIdentifier("", functions = dd :: Nil)
291291
}
292292

293-
def is_null: PackratParser[SQLLogicalFunction[_]] =
293+
def is_null: PackratParser[SQLConditionalFunction[_]] =
294294
"(?i)isnull".r ~ start ~ (identifierWithTransformation | identifierWithArithmeticFunction | identifierWithTemporalFunction | identifier) ~ end ^^ {
295295
case _ ~ _ ~ i ~ _ => SQLIsNullFunction(i)
296296
}
297297

298-
def is_notnull: PackratParser[SQLLogicalFunction[_]] =
298+
def is_notnull: PackratParser[SQLConditionalFunction[_]] =
299299
"(?i)isnotnull".r ~ start ~ (identifierWithTransformation | identifierWithArithmeticFunction | identifierWithTemporalFunction | identifier) ~ end ^^ {
300300
case _ ~ _ ~ i ~ _ => SQLIsNotNullFunction(i)
301301
}
@@ -610,7 +610,7 @@ trait SQLWhereParser {
610610
def not: PackratParser[Not.type] = Not.regex ^^ (_ => Not)
611611

612612
def logical_criteria: PackratParser[SQLCriteria] =
613-
(is_null | is_notnull) ^^ { case SQLLogicalFunctionAsCriteria(c) =>
613+
(is_null | is_notnull) ^^ { case SQLConditionalFunctionAsCriteria(c) =>
614614
c
615615
}
616616

sql/src/main/scala/app/softnetwork/elastic/sql/SQLWhere.scala

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -297,26 +297,26 @@ case class SQLIsNotNull(identifier: SQLIdentifier) extends Expression {
297297
override def asFilter(currentQuery: Option[ElasticBoolQuery]): ElasticFilter = this
298298
}
299299

300-
sealed trait SQLCriteriaWithLogicalFunction[In <: SQLType] extends Expression {
301-
def logicalFunction: SQLLogicalFunction[In]
300+
sealed trait SQLCriteriaWithConditionalFunction[In <: SQLType] extends Expression {
301+
def conditionalFunction: SQLConditionalFunction[In]
302302
override def maybeValue: Option[SQLToken] = None
303303
override def maybeNot: Option[Not.type] = None
304304
override def asFilter(currentQuery: Option[ElasticBoolQuery]): ElasticFilter = this
305-
override val functions: List[SQLFunction] = List(logicalFunction)
306-
override def sql = s"${logicalFunction.sql}($identifier)"
305+
override val functions: List[SQLFunction] = List(conditionalFunction)
306+
override def sql = s"${conditionalFunction.sql}($identifier)"
307307
}
308308

309-
object SQLLogicalFunctionAsCriteria {
310-
def unapply(f: SQLLogicalFunction[_]): Option[SQLCriteria] = f match {
309+
object SQLConditionalFunctionAsCriteria {
310+
def unapply(f: SQLConditionalFunction[_]): Option[SQLCriteria] = f match {
311311
case SQLIsNullFunction(id) => Some(SQLIsNullCriteria(id))
312312
case SQLIsNotNullFunction(id) => Some(SQLIsNotNullCriteria(id))
313313
case _ => None
314314
}
315315
}
316316

317317
case class SQLIsNullCriteria(identifier: SQLIdentifier)
318-
extends SQLCriteriaWithLogicalFunction[SQLAny] {
319-
override val logicalFunction: SQLLogicalFunction[SQLAny] = SQLIsNullFunction(identifier)
318+
extends SQLCriteriaWithConditionalFunction[SQLAny] {
319+
override val conditionalFunction: SQLConditionalFunction[SQLAny] = SQLIsNullFunction(identifier)
320320
override val operator: SQLOperator = IsNull
321321
override def update(request: SQLSearchRequest): SQLCriteria = {
322322
val updated = this.copy(identifier = identifier.update(request))
@@ -328,8 +328,10 @@ case class SQLIsNullCriteria(identifier: SQLIdentifier)
328328
}
329329

330330
case class SQLIsNotNullCriteria(identifier: SQLIdentifier)
331-
extends SQLCriteriaWithLogicalFunction[SQLAny] {
332-
override val logicalFunction: SQLLogicalFunction[SQLAny] = SQLIsNotNullFunction(identifier)
331+
extends SQLCriteriaWithConditionalFunction[SQLAny] {
332+
override val conditionalFunction: SQLConditionalFunction[SQLAny] = SQLIsNotNullFunction(
333+
identifier
334+
)
333335
override val operator: SQLOperator = IsNotNull
334336
override def update(request: SQLSearchRequest): SQLCriteria = {
335337
val updated = this.copy(identifier = identifier.update(request))

0 commit comments

Comments
 (0)