summaryrefslogtreecommitdiff
path: root/src/main/scala/org/xapek/influxdb/query.scala
diff options
context:
space:
mode:
authorYves Fischer <yvesf-git@xapek.org>2015-08-03 00:18:20 +0200
committerYves Fischer <yvesf-git@xapek.org>2015-08-03 00:18:20 +0200
commitdcabe1a774ac3ab6eee7e837aa09ee26b60e8b82 (patch)
tree1647576f1d327c8720acda6dc489e051402aa6e9 /src/main/scala/org/xapek/influxdb/query.scala
parentab3a129bfd7572d6d3d9457b5ded57c1a811f748 (diff)
downloadinfluxdb-tools-master.tar.gz
influxdb-tools-master.zip
Restructure into separate filesHEADmaster
Diffstat (limited to 'src/main/scala/org/xapek/influxdb/query.scala')
-rw-r--r--src/main/scala/org/xapek/influxdb/query.scala62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/main/scala/org/xapek/influxdb/query.scala b/src/main/scala/org/xapek/influxdb/query.scala
new file mode 100644
index 0000000..022c888
--- /dev/null
+++ b/src/main/scala/org/xapek/influxdb/query.scala
@@ -0,0 +1,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(", ")
+}