Skip to content

Commit

Permalink
Add column checking
Browse files Browse the repository at this point in the history
  • Loading branch information
takezoe committed Sep 2, 2016
1 parent 73ef11a commit 3ac6739
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit 3ac6739

Please sign in to comment.