forked from finos/vuu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finos#1296 use parameterized SQL query builder
- this would prevent SQL injection while adding better support for different types i.e. for instance before we had to manually convert values to strings (with or without quotes depending on the type) before using those in SQL query. Now, ignite ignite would do that for us out of the box.
- Loading branch information
1 parent
d618f9c
commit 4ee7c7a
Showing
13 changed files
with
264 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
plugin/ignite-plugin/src/main/scala/org/finos/vuu/feature/ignite/IgniteSqlQuery.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.finos.vuu.feature.ignite | ||
|
||
import org.apache.ignite.cache.query.SqlFieldsQuery | ||
import org.finos.vuu.feature.ignite.IgniteSqlQuery.QuerySeparator | ||
|
||
object IgniteSqlQuery { | ||
def apply(): IgniteSqlQuery = IgniteSqlQuery("") | ||
def apply(sqlTemplate: String): IgniteSqlQuery = new IgniteSqlQuery(sqlTemplate, List.empty) | ||
def empty: IgniteSqlQuery = IgniteSqlQuery() | ||
|
||
sealed abstract class QuerySeparator(val value: String) | ||
object QuerySeparator { | ||
final case object AND extends QuerySeparator(value = " AND ") | ||
final case object OR extends QuerySeparator(value = " OR ") | ||
final case object SPACE extends QuerySeparator(value = " ") | ||
final case object EMPTY extends QuerySeparator(value = "") | ||
} | ||
} | ||
|
||
case class IgniteSqlQuery(sqlTemplate: String, args: List[Any]) { | ||
|
||
def appendSql(sqlTemplate: String, sep: QuerySeparator = QuerySeparator.EMPTY): IgniteSqlQuery = { | ||
val newTemplate = if (sqlTemplate.isEmpty) this.sqlTemplate else Array(this.sqlTemplate, sqlTemplate).mkString(sep.value) | ||
this.copy(sqlTemplate = newTemplate) | ||
} | ||
|
||
def prependSql(sqlTemplate: String, sep: QuerySeparator = QuerySeparator.EMPTY): IgniteSqlQuery = { | ||
val newTemplate = if (sqlTemplate.isEmpty) this.sqlTemplate else Array(sqlTemplate, this.sqlTemplate).mkString(sep.value) | ||
this.copy(sqlTemplate = newTemplate) | ||
} | ||
|
||
def appendArgs(args: List[Any]): IgniteSqlQuery = { | ||
this.copy(args = this.args ++ args) | ||
} | ||
|
||
def appendQuery(query: IgniteSqlQuery, sep: QuerySeparator = QuerySeparator.EMPTY): IgniteSqlQuery = { | ||
this.appendSql(query.sqlTemplate, sep).appendArgs(query.args) | ||
} | ||
|
||
def isEmpty: Boolean = this.sqlTemplate.isEmpty && this.args.isEmpty | ||
|
||
def buildFieldsQuery(): SqlFieldsQuery = new SqlFieldsQuery(sqlTemplate).setArgs(args.toArray: _*) | ||
} |
Oops, something went wrong.