Skip to content

Commit cf5aa46

Browse files
committed
feat: execute sql with database selection
1 parent 0a7a7de commit cf5aa46

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/main/scala/dev/mongocamp/driver/mongodb/sql/MongoSqlQueryHolder.scala

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@ import dev.mongocamp.driver.mongodb._
44
import dev.mongocamp.driver.mongodb.database.DatabaseProvider
55
import SQLCommandType.SQLCommandType
66
import com.mongodb.client.model.DropIndexOptions
7-
import net.sf.jsqlparser.statement.{ShowStatement, Statement, UnsupportedStatement}
8-
import net.sf.jsqlparser.expression.operators.conditional.{AndExpression, OrExpression}
7+
import dev.mongocamp.driver.mongodb.database.DatabaseProvider.CollectionSeparator
8+
import net.sf.jsqlparser.statement.{ ShowStatement, Statement, UnsupportedStatement }
9+
import net.sf.jsqlparser.expression.operators.conditional.{ AndExpression, OrExpression }
910
import 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}
11+
import net.sf.jsqlparser.expression.{ Expression, Parenthesis }
12+
import net.sf.jsqlparser.parser.{ CCJSqlParser, StreamProvider }
13+
import net.sf.jsqlparser.schema.{ Column, Table }
1314
import net.sf.jsqlparser.statement.create.index.CreateIndex
1415
import net.sf.jsqlparser.statement.delete.Delete
1516
import net.sf.jsqlparser.statement.drop.Drop
1617
import net.sf.jsqlparser.statement.insert.Insert
17-
import net.sf.jsqlparser.statement.select.{AllColumns, FromItem, PlainSelect, Select, SelectItem}
18+
import net.sf.jsqlparser.statement.select.{ AllColumns, FromItem, PlainSelect, Select, SelectItem }
1819
import net.sf.jsqlparser.statement.show.ShowTablesStatement
1920
import net.sf.jsqlparser.statement.truncate.Truncate
2021
import net.sf.jsqlparser.statement.update.Update
2122
import org.bson.conversions.Bson
2223
import org.mongodb.scala.model.IndexOptions
2324
import org.mongodb.scala.model.Sorts.ascending
24-
import org.mongodb.scala.{Document, Observable}
25+
import org.mongodb.scala.{ Document, Observable }
2526

2627
import java.util.concurrent.TimeUnit
2728
import scala.collection.mutable
@@ -90,11 +91,12 @@ class MongoSqlQueryHolder {
9091
}
9192
else if (classOf[UnsupportedStatement].isAssignableFrom(statement.getClass)) {
9293
val unsupportedStatement = statement.asInstanceOf[UnsupportedStatement]
93-
val isShowDatabases = unsupportedStatement.toString.toLowerCase.contains("show databases")
94-
val isShowSchemas = unsupportedStatement.toString.toLowerCase.contains("show schemas")
94+
val isShowDatabases = unsupportedStatement.toString.toLowerCase.contains("show databases")
95+
val isShowSchemas = unsupportedStatement.toString.toLowerCase.contains("show schemas")
9596
if (isShowDatabases | isShowSchemas) {
9697
sqlCommandType = SQLCommandType.ShowDatabases
97-
} else {
98+
}
99+
else {
98100
throw new IllegalArgumentException("not supported sql command type")
99101
}
100102
}
@@ -104,7 +106,9 @@ class MongoSqlQueryHolder {
104106
""
105107
}
106108

107-
def getCollection: String = sqlTable.getFullyQualifiedName
109+
def getCollection: String = {
110+
sqlTable.getFullyQualifiedName.replace(".", CollectionSeparator).replace("'", "").replace("`", "")
111+
}
108112

109113
def run(provider: DatabaseProvider, allowDiskUsage: Boolean = true): Observable[Document] = {
110114
sqlCommandType match {

src/test/scala/dev/mongocamp/driver/mongodb/sql/SelectSqlSpec.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ class SelectSqlSpec extends PersonSpecification {
1515
selectResponse.head.getString("guid") mustEqual "a17be99a-8913-4bb6-8f14-16d4fa1b3559"
1616
}
1717

18+
"simple sql with schema" in {
19+
val queryConverter = MongoSqlQueryHolder("select * from `mongocamp-unit-test`.`friend`")
20+
val selectResponse = queryConverter.run(TestDatabase.provider).resultList()
21+
selectResponse.size mustEqual 1327
22+
selectResponse.head.getString("name") mustEqual "Castaneda Mccullough"
23+
selectResponse.head.getLong("id") mustEqual 33
24+
}
25+
1826
"sql with in query" in {
1927
val queryConverter = MongoSqlQueryHolder("select id, guid, name, age, balance from people where age in (30, 18, 25, 22) order by id asc")
2028
val selectResponse = queryConverter.run(TestDatabase.provider).resultList()

0 commit comments

Comments
 (0)