@@ -5,22 +5,22 @@ import dev.mongocamp.driver.mongodb.database.DatabaseProvider
55import SQLCommandType .SQLCommandType
66import com .mongodb .client .model .DropIndexOptions
77import net .sf .jsqlparser .statement .Statement
8- import net .sf .jsqlparser .expression .operators .conditional .{AndExpression , OrExpression }
8+ import net .sf .jsqlparser .expression .operators .conditional .{ AndExpression , OrExpression }
99import net .sf .jsqlparser .expression .operators .relational ._
10- import net .sf .jsqlparser .expression .{Expression , Parenthesis }
11- import net .sf .jsqlparser .parser .{CCJSqlParser , StreamProvider }
12- import net .sf .jsqlparser .schema .{Column , Table }
10+ import net .sf .jsqlparser .expression .{ Expression , Parenthesis }
11+ import net .sf .jsqlparser .parser .{ CCJSqlParser , StreamProvider }
12+ import net .sf .jsqlparser .schema .{ Column , Table }
1313import net .sf .jsqlparser .statement .create .index .CreateIndex
1414import net .sf .jsqlparser .statement .delete .Delete
1515import net .sf .jsqlparser .statement .drop .Drop
1616import net .sf .jsqlparser .statement .insert .Insert
17- import net .sf .jsqlparser .statement .select .{AllColumns , FromItem , PlainSelect , Select , SelectExpressionItem , SelectItem , SubSelect }
17+ import net .sf .jsqlparser .statement .select .{ AllColumns , FromItem , PlainSelect , Select , SelectItem }
1818import net .sf .jsqlparser .statement .truncate .Truncate
1919import net .sf .jsqlparser .statement .update .Update
2020import org .bson .conversions .Bson
2121import org .mongodb .scala .model .IndexOptions
2222import org .mongodb .scala .model .Sorts .ascending
23- import org .mongodb .scala .{Document , Observable }
23+ import org .mongodb .scala .{ Document , Observable }
2424
2525import java .util .concurrent .TimeUnit
2626import scala .collection .mutable
@@ -29,7 +29,7 @@ import scala.jdk.CollectionConverters._
2929
3030class MongoSqlQueryHolder {
3131 private val aggregatePipeline : ArrayBuffer [Document ] = ArrayBuffer ()
32- private var sqlTable : Table = _
32+ private var sqlTable : Table = _
3333 private var alias : Option [String ] = None
3434 private var sqlCommandType : SQLCommandType = _
3535 private var updateOrDeleteFilter : Option [Map [String , Any ]] = None
@@ -61,7 +61,7 @@ class MongoSqlQueryHolder {
6161 convertCreateIndexStatement(createIndex)
6262 }
6363 else if (classOf [Drop ].isAssignableFrom(statement.getClass)) {
64- val drop = statement.asInstanceOf [Drop ]
64+ val drop = statement.asInstanceOf [Drop ]
6565 drop.getType.toUpperCase match {
6666 case " TABLE" =>
6767 sqlCommandType = SQLCommandType .DropTable
@@ -131,8 +131,11 @@ class MongoSqlQueryHolder {
131131
132132 case SQLCommandType .DropIndex =>
133133 val collectionName = sqlTable.getSchemaName
134- val indexName = sqlTable.getName
135- provider.dao(collectionName).dropIndexForName(indexName, new DropIndexOptions ().maxTime(1 , TimeUnit .MINUTES )).map(_ => org.mongodb.scala.Document (" indexName" -> indexName))
134+ val indexName = sqlTable.getName
135+ provider
136+ .dao(collectionName)
137+ .dropIndexForName(indexName, new DropIndexOptions ().maxTime(1 , TimeUnit .MINUTES ))
138+ .map(_ => org.mongodb.scala.Document (" indexName" -> indexName))
136139
137140 case SQLCommandType .DropTable =>
138141 provider.dao(getCollection).drop().map(_ => org.mongodb.scala.Document (" wasAcknowledged" -> true ))
@@ -209,9 +212,9 @@ class MongoSqlQueryHolder {
209212 case e : Parenthesis =>
210213 parseWhere(e.getExpression, queryMap)
211214 case e : InExpression =>
212- val value = e.getRightItemsList match {
213- case l : ExpressionList => l.getExpressions .asScala.map(convertValue)
214- case i : ItemsList => throw new IllegalArgumentException (s " ${i.getClass.getSimpleName} not supported " )
215+ val value = e.getRightExpression match {
216+ case l : ParenthesedExpressionList [ Expression ] => l.asScala.map(convertValue)
217+ case i : Any => throw new IllegalArgumentException (s " ${i.getClass.getSimpleName} not supported " )
215218 }
216219 val functionName = if (e.isNot) " $nin" else " $in"
217220 queryMap.put(e.getLeftExpression.toString, Map (functionName -> value))
@@ -238,20 +241,17 @@ class MongoSqlQueryHolder {
238241 val groupId = mutable.Map [String , Any ]()
239242 val group = mutable.Map [String , Any ]()
240243 groupBy.foreach(g => groupId += g -> (" $" + g))
241- selectItems.foreach {
242- case e : SelectExpressionItem =>
243- val expressionName = e.getExpression.toString
244- if (expressionName.contains(" count" )) {
245- group += expressionName -> Map (" $sum" -> 1 )
246- }
247- else {
248- if (! groupBy.contains(expressionName)) {
249- val espr = expressionName.split('(' ).map(_.trim.replace(" )" , " " )).map(s => (" $" + s))
250- group += expressionName -> Map (espr.head -> espr.last)
251- }
244+ selectItems.foreach { case e : SelectItem [Expression ] =>
245+ val expressionName = e.getExpression.toString
246+ if (expressionName.contains(" count" )) {
247+ group += expressionName -> Map (" $sum" -> 1 )
248+ }
249+ else {
250+ if (! groupBy.contains(expressionName)) {
251+ val espr = expressionName.split('(' ).map(_.trim.replace(" )" , " " )).map(s => (" $" + s))
252+ group += expressionName -> Map (espr.head -> espr.last)
252253 }
253- case e : SelectItem =>
254- e.toString
254+ }
255255 }
256256 val groupMap = Map (" _id" -> groupId) ++ group.toMap ++ groupId.keys.map(s => s -> Map (" $first" -> (" $" + s))).toMap
257257 aggregatePipeline += Map (" $group" -> groupMap)
@@ -329,10 +329,10 @@ class MongoSqlQueryHolder {
329329 " $match" -> filterQuery
330330 )
331331 }
332- val hasAllColumns = selectItems.exists(_. isInstanceOf [ AllColumns ] )
332+ val hasAllColumns = selectItems.exists(i => i.toString.equalsIgnoreCase( " * " ) )
333333 if (selectItems.nonEmpty && ! hasAllColumns) {
334334 val addFields = selectItems.filter {
335- case e : SelectExpressionItem =>
335+ case e : SelectItem [ Expression ] =>
336336 e.getAlias match {
337337 case null => false
338338 case _ =>
@@ -341,17 +341,18 @@ class MongoSqlQueryHolder {
341341 case _ => false
342342 }
343343 val fields : Map [String , Any ] = addFields
344- .map(_.asInstanceOf [SelectExpressionItem ])
344+ .map(_.asInstanceOf [SelectItem [ Expression ] ])
345345 .map(e => e.getAlias.getName -> (" $" + e.getExpression.toString))
346346 .toMap
347+
347348 if (fields.nonEmpty) {
348349 aggregatePipeline += Map (" $addFields" -> fields)
349350 }
350351 aggregatePipeline += Map (
351352 " $project" -> selectItems
352353 .filterNot(s => s.toString.equalsIgnoreCase(" *" ))
353354 .map {
354- case e : SelectExpressionItem =>
355+ case e : SelectItem [ Expression ] =>
355356 e.getAlias match {
356357 case null =>
357358 e.getExpression.toString -> 1
@@ -362,6 +363,7 @@ class MongoSqlQueryHolder {
362363 }
363364 .toMap
364365 )
366+
365367 }
366368 if (aliasList.nonEmpty) {
367369 aliasList += " $$ROOT"
@@ -371,23 +373,20 @@ class MongoSqlQueryHolder {
371373 }
372374 Option (plainSelect.getDistinct).foreach { distinct =>
373375 val groupMap : mutable.Map [String , Any ] = mutable.Map ()
374- selectItems.foreach {
375- case e : SelectExpressionItem =>
376- val expressionName = e.getExpression.toString
377- if (expressionName.contains(" count" )) {
378- groupMap += expressionName -> Map (" $sum" -> 1 )
376+ selectItems.foreach { case e : SelectItem [Expression ] =>
377+ val expressionName = e.getExpression.toString
378+ if (expressionName.contains(" count" )) {
379+ groupMap += expressionName -> Map (" $sum" -> 1 )
380+ }
381+ else {
382+ val espr = expressionName.split('(' ).map(_.trim.replace(" )" , " " )).map(s => (" $" + s))
383+ if (espr.head.equalsIgnoreCase(espr.last)) {
384+ groupMap += expressionName -> Map (" $first" -> espr.last)
379385 }
380386 else {
381- val espr = expressionName.split('(' ).map(_.trim.replace(" )" , " " )).map(s => (" $" + s))
382- if (espr.head.equalsIgnoreCase(espr.last)) {
383- groupMap += expressionName -> Map (" $first" -> espr.last)
384- }
385- else {
386- groupMap += expressionName -> Map (espr.head -> espr.last)
387- }
387+ groupMap += expressionName -> Map (espr.head -> espr.last)
388388 }
389- case e : SelectItem =>
390- e.toString
389+ }
391390 }
392391 groupMap.put(" _id" , groupMap.keys.map(s => s -> (" $" + s)).toMap)
393392 aggregatePipeline += Map (" $group" -> groupMap.toMap)
@@ -408,37 +407,32 @@ class MongoSqlQueryHolder {
408407 }
409408
410409 private def convertInsertStatement (insert : Insert ): Unit = {
411- insert.getItemsList match {
412- case i : ExpressionList =>
413- val expressionList = i.getExpressions.asScala.toList
414- val document = mutable.Map [String , Any ]()
415- var index = 0
416- if (insert.getColumns == null ) {
417- throw new IllegalArgumentException (" column names must be specified" )
418- }
419- insert.getColumns.asScala
420- .map(_.getColumnName)
421- .foreach(colName => {
422- document += colName -> convertValue(expressionList(index))
423- index += 1
424- })
410+ val columns : List [String ] = Option (insert.getColumns).map(_.asScala).getOrElse(List .empty).map(_.getColumnName).toList
411+ if (columns.isEmpty) {
412+ throw new IllegalArgumentException (" column names must be specified" )
413+ }
414+ var singleDocumentCreated = false
415+ val baseExpressionList : ExpressionList [_] = insert.getSelect.getValues.getExpressions
416+ baseExpressionList.asScala.foreach {
417+ case e : ParenthesedExpressionList [Expression ] =>
418+ val document = mutable.Map [String , Any ]()
419+ columns.foreach(colName => document += colName -> convertValue(e.get(columns.indexOf(colName))))
425420 documentsToInsert += document.toMap
426- case i : MultiExpressionList =>
427- i.getExpressionLists.asScala.foreach { el =>
428- val expressionList = el.getExpressions.asScala.toList
429- val document = mutable.Map [String , Any ]()
430- var index = 0
431- insert.getColumns.asScala
432- .map(_.getColumnName)
433- .foreach(colName => {
434- document += colName -> convertValue(expressionList(index))
435- index += 1
436- })
437- documentsToInsert += document.toMap
421+ case _ =>
422+ try {
423+ if ( ! singleDocumentCreated) {
424+ val document = mutable.Map [String , Any ]()
425+ columns.foreach(colName => document += colName -> convertValue(baseExpressionList.get(columns.indexOf(colName)). asInstanceOf [ Expression ]))
426+ documentsToInsert += document.toMap
427+ }
428+ singleDocumentCreated = true
429+ }
430+ catch {
431+ case _ : Throwable =>
432+ throw new IllegalArgumentException ( " not supported expression list " )
438433 }
439- case i : ItemsList =>
440- throw new IllegalArgumentException (s " not supported items list of type ${i.getClass.getSimpleName}" )
441434 }
435+
442436 sqlCommandType = SQLCommandType .Insert
443437 sqlTable = insert.getTable
444438 }
@@ -458,18 +452,12 @@ class MongoSqlQueryHolder {
458452 .map(_.asScala)
459453 .getOrElse(List .empty)
460454 .foreach(set => {
461- val expressionList = set.getExpressions.asScala.toList
462- var index = 0
463- if (set.getColumns == null ) {
455+ val columns : List [String ] = Option (set.getColumns).map(_.asScala).getOrElse(List .empty).map(_.getColumnName).toList
456+ if (columns.isEmpty) {
464457 throw new IllegalArgumentException (" column names must be specified" )
465458 }
466- set.getColumns.asScala
467- .map(_.getColumnName)
468- .foreach(colName => {
469- updateSetElement += colName -> convertValue(expressionList(index))
470- index += 1
471- })
472-
459+ columns
460+ .foreach(colName => updateSetElement += colName -> convertValue(set.getValue(columns.indexOf(colName))))
473461 })
474462 if (updateSetElement.nonEmpty) {
475463 this .setElement = Some (updateSetElement.toMap)
0 commit comments