Tuang
12/23/2019, 4:16 AMjoinToString()
I means
val list = listOf(1,2,3)
// i want the result like this
// 1,2,3 <= each is int value
Timur Atakishiev
12/23/2019, 4:53 AMTuang
12/23/2019, 5:01 AMlist.joinToString()
but how to change each string to intTimur Atakishiev
12/23/2019, 5:03 AMTimur Atakishiev
12/23/2019, 5:04 AMTimur Atakishiev
12/23/2019, 5:04 AMTuang
12/23/2019, 5:05 AMTuang
12/23/2019, 5:05 AMTimur Atakishiev
12/23/2019, 5:06 AMSimon Kågedal Reimer
12/23/2019, 5:55 AMSimon Kågedal Reimer
12/23/2019, 5:58 AM"1,2,3"
to a list of ints?Tuang
12/23/2019, 6:03 AMselect name from someTable where id in (?, ?, ?)
and i have ids list val ids: MutableList<Int> = mutableListOf<Int>()
, i am going to pass the ids to queryRunner() parameter likes
QueryRunner().query(connection, query, ColumnListHandler<Int>(), 1,2,3
<= i need to pass like this, so i did ids.joinToString()
and
it is not int.
sorry for bad english.Simon Kågedal Reimer
12/23/2019, 6:11 AMTuang
12/23/2019, 6:12 AMSimon Kågedal Reimer
12/23/2019, 6:18 AM.query(connection, query, handler, firstInt, secondInt, thirdInt)
Tuang
12/23/2019, 6:19 AMSimon Kågedal Reimer
12/23/2019, 6:20 AMmyList.get(0), myList.get(1), myList.get(2)
– but that's not very prettySimon Kågedal Reimer
12/23/2019, 6:20 AMSimon Kågedal Reimer
12/23/2019, 6:21 AMTuang
12/23/2019, 6:21 AMTuang
12/23/2019, 6:21 AMSimon Kågedal Reimer
12/23/2019, 6:22 AMTuang
12/23/2019, 6:22 AMTuang
12/23/2019, 6:27 AMval ids = ArrayList<Any>()
// add ids
val relQuery = StringBuilder()
relQuery.append("SELECT name FROM table WHERE id IN (")
var separate = ""
repeat(domainIds.size) {
relQuery.append(separate)
separate = ","
relQuery.append("?")
}
relQuery.append(");")
val rel = QueryRunner().query(connection, relQuery.toString(), ColumnListHandler<Int>(), Ids)
Simon Kågedal Reimer
12/23/2019, 6:33 AM... where id in ${myList.joinToString()}
That's generally considered bad form because of the risk of SQL injections, but if you know it's a list of ints you should be safe I guess....Tuang
12/23/2019, 6:34 AMjimn
12/23/2019, 9:25 AMMatteo Mirk
12/23/2019, 1:29 PMfun query(conn: Connection, query: String, handler: ColumnListHandler<Int>, vararg ids: Int) { ... }
then you can invoke your method with any number of ids you like:
query(connection, relQuery.toString(), ColumnListHandler<Int>(), 1, 2, 3)
or if you have them in an array you can pass it using the spread operator:
val ids = arrayOf(1, 2, 3, 4)
//...
query(connection, relQuery.toString(), ColumnListHandler<Int>(), *ids)
Matteo Mirk
12/23/2019, 1:32 PMTuang
12/23/2019, 2:24 PM....
....
val paramIds = connection.createArrayOf("int", ids.toArray())
val query = "SELECT name FROM table WHERE id = ANY(?)"
val result = QueryRunner().query(connection, query, MapListHandler(), paramIds)
.....
.....
this worked perfectly.Matteo Mirk
12/23/2019, 2:25 PMTuang
12/23/2019, 2:34 PMTuang
12/23/2019, 2:34 PMMatteo Mirk
12/23/2019, 2:35 PMMatteo Mirk
12/23/2019, 2:35 PMTuang
12/23/2019, 2:35 PMMatteo Mirk
12/23/2019, 2:35 PMTuang
12/23/2019, 2:36 PMSimon Kågedal Reimer
12/23/2019, 2:36 PMTuang
12/23/2019, 2:37 PMval carMst: List<(Mutable)Map<String!, Any!>!>!
val carUserIds: List<(Mutable)Map<String!, Any!>!>!
....
....
val result = ArrayList<Car>()
for (mst in carMst) {
val userIds = ArrayList<Int>()
for (uids in carUserIds) {
if (carMst["car_id"] == uids["car_id"]) {
userIds.add(uids["user_id"] as Int)
}
}
val car = Car(
mst["car_id"] as Int,
mst["model"] as String,
userIds
)
result.add(car)
}
dont u think this is messy code?
i dont know too much about collection, but with collection this can be more simple and beautiful right? 😀Matteo Mirk
12/23/2019, 2:46 PMquery()
it expects a varargs as last arguments, so if you have your id list just call it like this
QueryRunner().query(connection, query, MapListHandler(), *ids.toArray())
For now, forget what we said about security since the lib will take care of that for you.Matteo Mirk
12/23/2019, 2:47 PMSimon Kågedal Reimer
12/23/2019, 2:54 PMwhere id in ?
– does that handle getting a list of values?Matteo Mirk
12/23/2019, 3:00 PMin
SQL clause, my bad! Well in that case we go back to your original suggestion, watching out for injections I guess…Simon Kågedal Reimer
12/23/2019, 3:07 PM?,?,?,?
with as many question marks as you have things in the list. And then use the trick with the * operatorSimon Kågedal Reimer
12/23/2019, 3:08 PMMatteo Mirk
12/23/2019, 3:09 PMMatteo Mirk
12/23/2019, 3:15 PM"SELECT name FROM table WHERE id IN (${"?,".repeat(ids.size).substringBeforeLast(",")})"
There’s only a problem if the id list gets too big we could get an error from the db, because many drivers or db engines put a limit on the query length.