-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dries Samyn
committed
Oct 24, 2024
1 parent
8e9104e
commit a809d7f
Showing
2 changed files
with
60 additions
and
51 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
...lib-persistence/src/main/kotlin/net/corda/ledger/libs/persistence/util/NamedParamQuery.kt
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,52 @@ | ||
package net.corda.ledger.libs.persistence.util | ||
|
||
import net.corda.ledger.libs.persistence.utxo.impl.UtxoPersistenceServiceImpl | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
|
||
interface NamedParamQuery { | ||
companion object { | ||
fun from(sql: String): NamedParamQuery { | ||
val plainSql = StringBuilder() | ||
val fields = mutableMapOf<String, Int>() | ||
var marker: StringBuilder? = null | ||
var markerIndex = 0 | ||
for (i in sql.indices) { | ||
val c = sql[i] | ||
if (c == ':' && (i < sql.length - 1 && sql[i + 1].isValidTokenChar())) { | ||
marker = StringBuilder() | ||
markerIndex++ | ||
plainSql.append('?') | ||
continue | ||
} | ||
|
||
if (null != marker) { | ||
if (!c.isValidTokenChar()) { | ||
fields[marker.toString()] = markerIndex | ||
marker = null | ||
} else { | ||
marker.append(c) | ||
continue | ||
} | ||
} | ||
plainSql.append(c) | ||
} | ||
|
||
if (null != marker) { | ||
fields[marker.toString()] = markerIndex | ||
} | ||
|
||
return NamedParamQueryImpl(plainSql.toString(), fields) | ||
} | ||
|
||
private fun Char.isValidTokenChar(): Boolean = this.isLetterOrDigit() || this == '_' || this == '-' | ||
} | ||
|
||
val sql: String | ||
val fields: Map<String, Int> | ||
|
||
private class NamedParamQueryImpl( | ||
override val sql: String, | ||
override val fields: Map<String, Int>, | ||
) : NamedParamQuery | ||
} |
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