From 3ac67391c1414e010959918c40501a88a22d506c Mon Sep 17 00:00:00 2001 From: Naoki Takezoe Date: Fri, 2 Sep 2016 20:27:54 +0900 Subject: [PATCH] Add column checking --- .../scala/jdbc/validation/Models.scala | 7 ++++-- .../scala/jdbc/validation/SelectVisitor.scala | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/main/scala/com/github/takezoe/scala/jdbc/validation/Models.scala b/src/main/scala/com/github/takezoe/scala/jdbc/validation/Models.scala index 948869e..f9f4797 100644 --- a/src/main/scala/com/github/takezoe/scala/jdbc/validation/Models.scala +++ b/src/main/scala/com/github/takezoe/scala/jdbc/validation/Models.scala @@ -15,14 +15,17 @@ class TableModel { class SelectModel { var from = new ListBuffer[TableModel] val columns = new ListBuffer[ColumnModel] + val orderBy = new ListBuffer[ColumnModel] + val where = new ListBuffer[ColumnModel] override def toString(): String = { - s"Select(from: $from, columns: $columns)" + s"Select(from: $from, columns: $columns, orderBy; $orderBy, where: $where)" } // TODO def validate() = { - columns.foreach { column => + (columns ++ where ++ orderBy).foreach { column => + println(column) val table = if (column.table != null){ from.find(x => x.name == column.table || x.alias == column.table) } else { diff --git a/src/main/scala/com/github/takezoe/scala/jdbc/validation/SelectVisitor.scala b/src/main/scala/com/github/takezoe/scala/jdbc/validation/SelectVisitor.scala index ef512ba..0b87a57 100644 --- a/src/main/scala/com/github/takezoe/scala/jdbc/validation/SelectVisitor.scala +++ b/src/main/scala/com/github/takezoe/scala/jdbc/validation/SelectVisitor.scala @@ -48,6 +48,30 @@ class SelectVisitor extends SelectVisitorAdapter { } + Option(plainSelect.getWhere).foreach(_.accept(new ExpressionVisitorAdapter { + override def visit(column: Column): Unit = { + val c = new ColumnModel() + c.name = column.getColumnName + c.table = Option(column.getTable).map(_.getName).orNull + select.where += c + } + })) + Option(plainSelect.getOrderByElements).foreach(_.asScala.foreach { orderBy => + println("OrderBy: " + orderBy) + orderBy.accept(new OrderByVisitor { + override def visit(orderBy: OrderByElement): Unit = { + orderBy.getExpression.accept(new ExpressionVisitorAdapter(){ + override def visit(column: Column): Unit = { + val c = new ColumnModel() + c.name = column.getColumnName + c.table = Option(column.getTable).map(_.getName).orNull + select.orderBy += c + } + }) + } + }) + }) + println(select) select.validate() }