diff options
author | Yves Fischer <yvesf-git@xapek.org> | 2015-06-05 23:55:38 +0200 |
---|---|---|
committer | Yves Fischer <yvesf-git@xapek.org> | 2015-06-05 23:55:38 +0200 |
commit | 8a17a2f6d77cb6bd3e5629d3944b30f449cb1237 (patch) | |
tree | fe4375cbfb298a1c0e1db1e322e72368bafb7af2 | |
parent | a52a91d6a96529dbefaae9f682af3a6025190e8d (diff) | |
download | influxdb-tools-8a17a2f6d77cb6bd3e5629d3944b30f449cb1237.tar.gz influxdb-tools-8a17a2f6d77cb6bd3e5629d3944b30f449cb1237.zip |
size typed query builder until WHERE
-rw-r--r-- | src/main/scala/org/xapek/influxdb/Influxdb.scala | 154 | ||||
-rw-r--r-- | src/main/scala/org/xapek/influxdb/Main.scala | 14 | ||||
-rw-r--r-- | src/main/scala/org/xapek/influxdb/Sized.scala | 6 | ||||
-rw-r--r-- | src/test/scala/org/xapek/influxdb/AppTest.scala | 43 | ||||
-rw-r--r-- | src/test/scala/org/xapek/influxdb/InfluxdbTest.scala | 45 |
5 files changed, 148 insertions, 114 deletions
diff --git a/src/main/scala/org/xapek/influxdb/Influxdb.scala b/src/main/scala/org/xapek/influxdb/Influxdb.scala index 15e2021..9ed778c 100644 --- a/src/main/scala/org/xapek/influxdb/Influxdb.scala +++ b/src/main/scala/org/xapek/influxdb/Influxdb.scala @@ -6,50 +6,77 @@ trait InfluxValue { def toJava: JavaT } -class QueryBuilder1(select: Seq[InfluxColumn]) { - def FROM(table: String): QueryBuilder2 = { - new QueryBuilder2(select, table) - } -} - -class QueryBuilder2(select: Seq[InfluxColumn], from: String) extends QueryBuilder1(select) { - def WHERE[E <: Expr](eq: E): QueryBuilder3[E] = { - new QueryBuilder3(eq, select, from) - } - - override def toString(): String = { - "SELECT " + select.mkString(", ") + " FROM " + from - } -} - -class QueryBuilder3[WhereT <: Expr](whereExpr: WhereT, select: Seq[InfluxColumn], from: String) extends QueryBuilder2(select, from) { - def &&[E2 <: Expr](other: E2): QueryBuilder3[AndExpr[WhereT, E2]] = { - new QueryBuilder3(new AndExpr(whereExpr, other), select, from) - } - - def ||[E2 <: Expr](other: E2): QueryBuilder3[OrExpr[WhereT, E2]] = { - new QueryBuilder3(new OrExpr(whereExpr, other), select, from) - } - - def GROUP_BY[ColumnT <% InfluxColumn](column: ColumnT): QueryBuilder4[WhereT] = { - new QueryBuilder4(List(column), whereExpr, select, from) - } - - override def toString(): String = { - super.toString() + " WHERE " + whereExpr.toString() - } -} - -class QueryBuilder4[WhereT <: Expr](groupBy: Seq[InfluxColumn], whereExpr: WhereT, select: Seq[InfluxColumn], from: String) extends QueryBuilder3(whereExpr, select, from) { - def <<=[ColumnT <% InfluxColumn](column: ColumnT): QueryBuilder4[WhereT] = { - val x: InfluxColumn = column - new QueryBuilder4(groupBy :+ x, whereExpr, select, from) - } - - override def toString(): String = { - super.toString() + " GROUP BY " + groupBy.mkString(", ") - } -} +class QueryBuilderSelect[L <: Nat, IdentT <% InfluxIdentifier](val columns: Sized[IdentT, L]) { + def apply(column: IdentT): QueryBuilderSelect[Succ[L], IdentT] = + new QueryBuilderSelect(SizedOp.add(columns, column)) + + def FROM(table: IdentT) = new QueryBuilderFrom(columns, table) + + implicit def influxIdentifier(s: String): InfluxIdentifier = { + new InfluxIdentifier(s) + } + + override def toString() = "SELECT " + columns.unsized.mkString(", ") +} + +class QueryBuilderFrom[L <: Nat, IdentT <% InfluxIdentifier](columns: Sized[IdentT, L], val table: IdentT) + extends QueryBuilderSelect(columns) { + def WHERE[WhereT <: Expr](where: WhereT) = + new QueryBuilderWhere(columns, table, where) + + override def toString() = super.toString() + " FROM " + table +} + +class QueryBuilderWhere[L <: Nat, IdentT <% InfluxIdentifier, WhereT <: Expr](columns: Sized[IdentT, L], + table: IdentT, + val where: WhereT) + extends QueryBuilderFrom(columns, table) { + override def toString() = super.toString() + " WHERE " + where +} +//class QueryBuilder1(select: Seq[InfluxIdentifier]) { +// def FROM(table: String): QueryBuilder2 = { +// new QueryBuilder2(select, table) +// } +//} + +//class QueryBuilder2(select: Seq[InfluxIdentifier], from: String) extends QueryBuilder1(select) { +// def WHERE[E <: Expr](eq: E): QueryBuilder3[E] = { +// new QueryBuilder3(eq, select, from) +// } +// +// override def toString(): String = { +// "SELECT " + select.mkString(", ") + " FROM " + from +// } +//} + +//class QueryBuilder3[WhereT <: Expr](whereExpr: WhereT, select: Seq[InfluxIdentifier], from: String) extends QueryBuilder2(select, from) { +// def &&[E2 <: Expr](other: E2): QueryBuilder3[AndExpr[WhereT, E2]] = { +// new QueryBuilder3(new AndExpr(whereExpr, other), select, from) +// } +// +// def ||[E2 <: Expr](other: E2): QueryBuilder3[OrExpr[WhereT, E2]] = { +// new QueryBuilder3(new OrExpr(whereExpr, other), select, from) +// } +// +// def GROUP_BY[ColumnT <% InfluxIdentifier](column: ColumnT): QueryBuilder4[WhereT] = { +// new QueryBuilder4(List(column), whereExpr, select, from) +// } +// +// override def toString(): String = { +// super.toString() + " WHERE " + whereExpr.toString() +// } +//} +// +//class QueryBuilder4[WhereT <: Expr](groupBy: Seq[InfluxIdentifier], whereExpr: WhereT, select: Seq[InfluxIdentifier], from: String) extends QueryBuilder3(whereExpr, select, from) { +// def <<=[ColumnT <% InfluxIdentifier](column: ColumnT): QueryBuilder4[WhereT] = { +// val x: InfluxIdentifier = column +// new QueryBuilder4(groupBy :+ x, whereExpr, select, from) +// } +// +// override def toString(): String = { +// super.toString() + " GROUP BY " + groupBy.mkString(", ") +// } +//} protected trait E { def &&[E2 <: Expr](other: E2): AndExpr[this.type, E2] @@ -91,11 +118,13 @@ class OrExpr[E1 <: E, E2 <: E](op1: E1, op2: E2) extends BinaryOp[E1, E2](op1, o override def str = "OR" } -class EqExpr[T <: InfluxValue](column: InfluxColumn, op: ValueExpr[T]) extends BinaryOp[InfluxColumn, ValueExpr[T]](column, op) { +class EqExpr[T <: InfluxValue](column: InfluxIdentifier, op: ValueExpr[T]) + extends BinaryOp[InfluxIdentifier, ValueExpr[T]](column, op) { def str = "==" } -class NeqExpr[T <: InfluxValue](column: InfluxColumn, op: ValueExpr[T]) extends BinaryOp[InfluxColumn, ValueExpr[T]](column, op) { +class NeqExpr[T <: InfluxValue](column: InfluxIdentifier, op: ValueExpr[T]) + extends BinaryOp[InfluxIdentifier, ValueExpr[T]](column, op) { def str = "==" } @@ -116,7 +145,7 @@ class InfluxNumber(val value: Number) extends Expr with InfluxValue { override def toJava(): Number = value } -class InfluxColumn(val name: String) extends Expr { +class InfluxIdentifier(val name: String) extends Expr { override def toString(): String = { "\"" + name.replace("\\", "\\\\").replace("\"", "\\\"") + "\"" } @@ -131,18 +160,9 @@ class InfluxColumn(val name: String) extends Expr { } object Influxdb { - def SELECT(select: Seq[String]): QueryBuilder1 = { - new QueryBuilder1(select.map { x: String => influxColumn(x) }) - } - - def SELECT[ColumnT <% InfluxColumn](select: ColumnT): QueryBuilder1 = { - new QueryBuilder1(List(select)) - } - - def col(col: String): InfluxColumn = { - new InfluxColumn(col) - } - + def SELECT(column: String) = + new QueryBuilderSelect(SizedOp.wrap(Influxdb.influxColumn(column))) + implicit def influxString(s: String): InfluxString = { new InfluxString(s) } @@ -155,11 +175,15 @@ object Influxdb { new InfluxNumber(n) } - implicit def influxColumn(s: String): InfluxColumn = { - new InfluxColumn(s) + implicit def influxColumn(s: String): InfluxIdentifier = { + new InfluxIdentifier(s) } - - implicit def queryToString(q: QueryBuilder2): String = q.toString - implicit def queryToString[T <: Expr](q: QueryBuilder3[T]): String = q.toString - implicit def queryToString[T <: Expr](q: QueryBuilder4[T]): String = q.toString + + def col(col: String): InfluxIdentifier = { + new InfluxIdentifier(col) + } + + // implicit def queryToString(q: QueryBuilder2): String = q.toString + // implicit def queryToString[T <: Expr](q: QueryBuilder3[T]): String = q.toString + // implicit def queryToString[T <: Expr](q: QueryBuilder4[T]): String = q.toString } diff --git a/src/main/scala/org/xapek/influxdb/Main.scala b/src/main/scala/org/xapek/influxdb/Main.scala index c56d3bb..58fc318 100644 --- a/src/main/scala/org/xapek/influxdb/Main.scala +++ b/src/main/scala/org/xapek/influxdb/Main.scala @@ -17,14 +17,20 @@ class GenericReader(query: String) extends Actor with ActorLogging { } } -object MyReader extends GenericReader(Influxdb SELECT "value" FROM "test" WHERE col("time") == 30) - - - +//object MyReader extends GenericReader(Influxdb SELECT "value" FROM "test" WHERE col("time") == 30) object Main { def main(args: Array[String]): Unit = { + def url = "http://localhost:8083/query" + val s1 = Influxdb.SELECT("test") + println(s1.columns) + + val s2 = Influxdb.SELECT("foo")("asd").FROM("table").WHERE(col("asd") == "asd") + println(s2) + + val s3 = Influxdb.SELECT("foo")("bla")("baz") FROM "asd" WHERE col("asd") == "asd" + println(s3) // val system = ActorSystem("test") // val alice = system.actorOf(Props(MyReader), "alice") // diff --git a/src/main/scala/org/xapek/influxdb/Sized.scala b/src/main/scala/org/xapek/influxdb/Sized.scala index fc402cd..bf3bca6 100644 --- a/src/main/scala/org/xapek/influxdb/Sized.scala +++ b/src/main/scala/org/xapek/influxdb/Sized.scala @@ -28,7 +28,7 @@ object ToInt { def apply[N <: Nat](implicit toInt: ToInt[N]): ToInt[N] = toInt implicit val toInt0 = new ToInt[_0] { - def apply() = 1 + def apply() = 0 } implicit def toIntSucc[N <: Nat](implicit toIntN: ToInt[N]) = new ToInt[Succ[N]] { def apply() = toIntN() + 1 @@ -41,7 +41,9 @@ class Sized[Repr, L <: Nat](val unsized: Seq[Repr]) extends AnyVal { } object SizedOp { - def wrap[Repr, L <: Nat](r: Repr) = new Sized[Repr, _0](List(r)) + def empty[Repr]() = new Sized[Repr, _0](List.empty[Repr]) + + def wrap[Repr, L <: Nat](r: Repr) = new Sized[Repr, Succ[_0]](List(r)) def add[Repr, L <: Nat](s: Sized[Repr, L], r: Repr) = new Sized[Repr, Succ[L]](s.unsized ++ List(r)) diff --git a/src/test/scala/org/xapek/influxdb/AppTest.scala b/src/test/scala/org/xapek/influxdb/AppTest.scala deleted file mode 100644 index 26ed08f..0000000 --- a/src/test/scala/org/xapek/influxdb/AppTest.scala +++ /dev/null @@ -1,43 +0,0 @@ -package org.xapek.influxdb - -import org.junit._ -import Assert._ -import org.xapek.influxdb.Influxdb._ - -@Test -class AppTest { - - @Test - def testStatements() = { - assertTrue((Influxdb SELECT "foo" FROM "bla" WHERE col("a") == "asd") - .toString().contains("as")) - - assertTrue((Influxdb SELECT List("foo", "bla") FROM "bla" WHERE col("a") == "asd") - .toString().contains("foo")) - - assertTrue((Influxdb SELECT List("foo", "bla") FROM "bla" WHERE col("a") == 1) - .toString().contains("== 1")) - - assertTrue((Influxdb - SELECT col("foo") - FROM "bla" - WHERE col("a") == "asd" || col("b") == "C" && col("c") == "d").toString().contains("bla")) - - assertTrue((Influxdb - SELECT "foo" - FROM "bla" - WHERE (col("a") == "asd" || col("b") == "C") && col("c") == "d").toString().contains("foo")) - - assertTrue((Influxdb - SELECT "foo" - FROM "bla" - WHERE (col("a") == "asd" || col("b") == "C") && col("c") == "d" - GROUP_BY "asd").toString().contains("GROUP BY")) - - assertTrue((Influxdb - SELECT col("foo") - FROM "bla" - WHERE (col("a") == "asd" || col("b") == "C") && col("c") == "d" - GROUP_BY "asd" <<= "asd2").toString().contains("asd2")) - } -} diff --git a/src/test/scala/org/xapek/influxdb/InfluxdbTest.scala b/src/test/scala/org/xapek/influxdb/InfluxdbTest.scala new file mode 100644 index 0000000..806c798 --- /dev/null +++ b/src/test/scala/org/xapek/influxdb/InfluxdbTest.scala @@ -0,0 +1,45 @@ +package org.xapek.influxdb + +import org.junit._ +import Assert._ +import org.xapek.influxdb.Influxdb._ + +@Test +class AppTest { + + @Test + def testStatements() = { + val url = "http://localhost:8086/query" + + assertTrue((Influxdb.SELECT("foo") FROM "bla" WHERE col("a") == "asd") + .toString().contains("as")) + + assertTrue((Influxdb.SELECT("foo")("bla") FROM "bla" WHERE col("a") == "asd") + .toString().contains("foo")) + + assertTrue((Influxdb.SELECT("foo")("bla") FROM "bla") + .toString().contains("== 1")) + + assertTrue((Influxdb + SELECT "foo" + FROM "bla" + WHERE col("a") == "asd" || col("b") == "C" && col("c") == "d").toString().contains("bla")) + + assertTrue((Influxdb + SELECT "foo" + FROM "bla" + WHERE (col("a") == "asd" || col("b") == "C") && col("c") == "d").toString().contains("foo")) + + // assertTrue((Influxdb + // SELECT "foo" + // FROM "bla" + // WHERE (col("a") == "asd" || col("b") == "C") && col("c") == "d" + // GROUP_BY "asd").toString().contains("GROUP BY")) + + // assertTrue((Influxdb + // SELECT col("foo") + // FROM "bla" + // WHERE (col("a") == "asd" || col("b") == "C") && col("c") == "d" + // GROUP_BY "asd" <<= "asd2").toString().contains("asd2")) + } +} |