Skip to content
Simon Koch edited this page Jun 15, 2021 · 1 revision

Welcome to the (unofficial) JSqlParser wiki!

Let's try and make sense on how to use this lib. We are using Scala for this.

Parsing

val stmt = CCJSqlParserUtil.parse(query)

returns any of net.sf.jsqlparser.statement.{select, insert,...}.{Select, Insert, ...} use your IDE to get the whole selection. We will continue using stmt to reference the parsed query/statement.

Note: For some reason the only transaction related statement that is supported is 'COMMIT'.

Select Statements

Selected Items

To iterate over the items the query selects the below statement works:

val stmt = stmt.asInstanceOf[Select].getSelectBody().asInstanceOf[PlainSelect].getSelectItems.toArray.map {
   case item : SelectExpressionItem => // the basic?? item expression and with toString you can get the name
   case _ : AllColumns => //this is the wild card
}

Where Clause

val where = Option(stmt.asInstanceOf[Select].getSelectBody().asInstanceOf[PlainSelect].getWhere)

Where Elements

It seems like the where returns an expression that is the tree representing the expression

Clone this wiki locally