summaryrefslogtreecommitdiff
path: root/src/main/scala/org/xapek/influxdb/query.scala
blob: 022c8884c19a9c2a10129549ec4d1aeb4d5d40b3 (plain)
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
package org.xapek.influxdb

import shapeless.Sized
import shapeless.Nat
import shapeless.ops.nat.ToInt
import shapeless.Succ

import org.xapek.influxdb.types.InfluxIdentifier
import org.xapek.influxdb.types.Expr

abstract class QueryBuilderWithSelectAndFrom[L <: Nat](
    val columns: Sized[List[InfluxIdentifier], L],
    val table: InfluxIdentifier) {

  def count()(implicit toInt: ToInt[L]) = toInt()

  def count1()(implicit toInt: ToInt[Succ[L]]) = toInt() //length + 1 (for "time")

  def toString: String
}

class QueryBuilderSelect[L <: Nat](
    val columns: Sized[List[InfluxIdentifier], L]) {

  def apply(column: String) =
    new QueryBuilderSelect(columns :+ InfluxDB.influxIdentifier(column))

  def FROM(table: String) = new QueryBuilderFrom(this, InfluxDB.influxIdentifier(table))

  override def toString() = "SELECT " + columns.unsized.mkString(", ")
}

class QueryBuilderFrom[L <: Nat](
  select: QueryBuilderSelect[L],
  table: InfluxIdentifier)
    extends QueryBuilderWithSelectAndFrom[L](select.columns, table) {

  def WHERE[WhereT <: Expr](where: WhereT) =
    new QueryBuilderWhere(this, where)

  override def toString() = select.toString() + " FROM " + table
}

class QueryBuilderWhere[L <: Nat, WhereT <: Expr](
  val from: QueryBuilderFrom[L],
  val where: WhereT)
    extends QueryBuilderWithSelectAndFrom[L](from.columns, from.table) {

  def GROUP_BY(column: String) = new QueryBuilderGroupBy(this, List(InfluxDB.influxIdentifier(column)))

  override def toString() = from.toString() + " WHERE " + where
}

class QueryBuilderGroupBy[L <: Nat, WhereT <: Expr](
  where: QueryBuilderWhere[L, WhereT],
  groupBy: Seq[InfluxIdentifier])
    extends QueryBuilderWithSelectAndFrom[L](where.columns, where.table) {

  def apply(column: String) = new QueryBuilderGroupBy(where, groupBy :+ InfluxDB.influxIdentifier(column))

  override def toString() = where.toString() + " GROUP BY " + groupBy.mkString(", ")
}