๋ช
๋ช
๋ ๋งค๊ฐ๋ณ์๊ฐ ์๋ SQL ์ฟผ๋ฆฌ๊ฐ ์์ต๋๋ค. :featureIds
const val selectGeoCoordinates = "SELECT gg.feature_id, gg.latitude, gg.longitude, gg.order, gg.group FROM geo.geo_coordinates as gg WHERE gg.feature_id IN (:featureIds);"
Int ๋ชฉ๋ก์ ๋ช ๋ช ๋ ๋งค๊ฐ๋ณ์์ ๋ฐ์ธ๋ฉํ๋ ค๊ณ ํ ๋
val values: List<Int> = listOf(4138, 1752, 1161, 371, 1860, 5498)
val geoCoordinatesList = geo.withHandle<List<GeoCoordinates>, Exception> { handle ->
handle.createQuery(selectGeoCoordinates).bindList("featureIds", values)
.mapTo<GeoCoordinates>().list()
}
์คํ ์ ๋ค์ ์์ธ๊ฐ ๋ฐ์ํฉ๋๋ค.
Caused by: org.jdbi.v3.core.statement.UnableToExecuteStatementException: Unable to execute, no named parameter matches 'featureIds'. [statement:"SELECT gg.feature_id, gg.latitude, gg.longitude, gg.order, gg.group FROM geo.geo_coordinates as gg WHERE gg.feature_id IN :featureIds ;", rewritten:"SELECT gg.feature_id, gg.latitude, gg.longitude, gg.order, gg.group FROM geo.geo_coordinates as gg WHERE gg.feature_id IN :featureIds ;", parsed:"ParsedSql{sql='SELECT gg.feature_id, gg.latitude, gg.longitude, gg.order, gg.group FROM geo.geo_coordinates as gg WHERE gg.feature_id IN ? ;', parameters=ParsedParameters{positional=false, parameterNames=[featureIds]}}", arguments:{ positional:{}, named:{__featureIds_0:4138,__featureIds_4:1860,__featureIds_3:371,__featureIds_2:1161,__featureIds_1:1752,__featureIds_5:5498}, finder:[]}]
at org.jdbi.v3.core.statement.ArgumentBinder.lambda$bindNamed$0(ArgumentBinder.java:58)
at java.util.Optional.orElseThrow(Optional.java:290)
at org.jdbi.v3.core.statement.ArgumentBinder.bindNamed(ArgumentBinder.java:57)
at org.jdbi.v3.core.statement.ArgumentBinder.bind(ArgumentBinder.java:27)
at org.jdbi.v3.core.statement.SqlStatement.internalExecute(SqlStatement.java:1443)
at org.jdbi.v3.core.result.ResultProducers.lambda$getResultSet$2(ResultProducers.java:59)
at org.jdbi.v3.core.result.ResultIterable.lambda$of$0(ResultIterable.java:53)
at org.jdbi.v3.core.result.ResultIterable.stream(ResultIterable.java:141)
at org.jdbi.v3.core.result.ResultIterable.collect(ResultIterable.java:197)
at org.jdbi.v3.core.result.ResultIterable.list(ResultIterable.java:186)
.bind("featureIds", 4138)
๋ฅผ ์ฌ์ฉํ๋ฉด ์๋ํ๋ฏ๋ก ์ด๋ฆ์ด ์ ํํ๋ค๊ณ ๊ฐ์ ํฉ๋๋ค.
๋ค๋ฅธ ์ฌ๋์ด ์ด๊ฒ์ ์ป์์ต๋๊น? ๋๋ ์ด๊ฒ์ ๋ํด ์์ ํ ์์ํดํ๋ฏ๋ก ์ด๋ค ๋์์ด๋ผ๋ ํ์ํฉ๋๋ค :(
์ข์... StringTemplateEngine์ ์๋ํ๋ ๋์ ์๋ฃจ์
์ ์ฐ์ฐํ ๋ฐ๊ฒฌํ์ต๋๋ค. ๋ชฉ๋ก์ ๋ํ ๋งค๊ฐ๋ณ์๋ฅผ <>
์ ๋ฃ์ผ๋ฉด ์๋ํฉ๋๋ค.
"SELECT gg.feature_id, gg.latitude, gg.longitude, gg.order, gg.group FROM geo.geo_coordinates as gg WHERE gg.feature_id IN (<featureIds>);"
.bindList("featureIds", values)
์๋ํฉ๋๋ค.
์ด๊ฒ์ ๋ฌธ์์ ์ถ๊ฐํ๊ณ bindList
์ ๋ํ ์๋ฅผ ์ ๊ณตํ ์ ์์ต๋๊น? :)
์, bindList๋ ์ค์ ๋ก ํ์ฅํ์ฌ <featureIds>
๋ฅผ :__featureIds_0, :__featureIds_1, ...etc
์ ๊ฐ์ด ์ ์ํ๊ณ ๋ชฉ๋ก ์์๋ฅผ ํด๋น ๋์ฒด ๋ณ์์ ๋ฐ์ธ๋ฉํฉ๋๋ค.
Postgres๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ์ฟผ๋ฆฌ์์ where featureIds = any(:featureIds)
๋ฅผ ์ฌ์ฉํ๊ณ bindByType("featureIds", featureIds, new GenericType<List<Long>>() {})
๋ฅผ ์ฌ์ฉํ์ฌ ๋ชฉ๋ก์ SQL ๋ฐฐ์ด๋ก ๋ฐ์ธ๋ฉํ ์ ์์ต๋๋ค.
์ข๋น๊ฐ ์๋๋ผ List
๊ฐ ํฌํจ๋ ๋จ์ผ ํค์ ๋ํด Map<String, Object>
bindMap
๋ฅผ ์ฌ์ฉํ๋ฉด ์๋ํ์ง ์๋ ๊ฒ ๊ฐ์ต๋๋ค. ์ด๋ฌํ ๋งค๊ฐ๋ณ์๋ bindList
๋ฅผ ์ฌ์ฉํ์ฌ ์ง๋์ ๋ณ๋๋ก ๋ฐ์ธ๋ฉํด์ผ ํฉ๋๋ค. ์ ํํฉ๋๊น?
์์:
a,b,c
from
table
where
a = :some_string
and b = ANY(<list_of_strings>)
๋ค์์ ๋ฐ์ธ๋ฉ์ผ๋ก:
.bindMap(Map.of(
'some_string', 'foobar',
'list_of_strings', Lists.newArrayList('q', 'r', 's')
))
ํธ๋ฆฌ๊ฑฐํฉ๋๋ค:
org.jdbi.v3.core.statement.UnableToCreateStatementException: Undefined attribute for token '<list_of_strings>'
๋ง์ต๋๋ค. bindMap
๋ ์ธ์๋ง ๋ฐ์ธ๋ฉํ๋ ๋ฐ๋ฉด bindList
๋ ํ
ํ๋ฆฟ์ด ์์๋๋ก ์๋ํ๋๋ก ์์ฑ๋ ์ ์ํด์ผ ํฉ๋๋ค. ๋์ค์ ๋ณ๊ฒฝ๋ ์ ์์ง๋ง ํจ๊ป ์๋ํ๋๋ก ์ค์ ๋์ง ์์์ต๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ข์... StringTemplateEngine์ ์๋ํ๋ ๋์ ์๋ฃจ์ ์ ์ฐ์ฐํ ๋ฐ๊ฒฌํ์ต๋๋ค. ๋ชฉ๋ก์ ๋ํ ๋งค๊ฐ๋ณ์๋ฅผ
<>
์ ๋ฃ์ผ๋ฉด ์๋ํฉ๋๋ค."SELECT gg.feature_id, gg.latitude, gg.longitude, gg.order, gg.group FROM geo.geo_coordinates as gg WHERE gg.feature_id IN (<featureIds>);"
.bindList("featureIds", values)
์๋ํฉ๋๋ค.์ด๊ฒ์ ๋ฌธ์์ ์ถ๊ฐํ๊ณ
bindList
์ ๋ํ ์๋ฅผ ์ ๊ณตํ ์ ์์ต๋๊น? :)