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(", ") }