188865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar/*
288865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar * Copyright (C) 2017 The Android Open Source Project
388865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar *
488865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar * Licensed under the Apache License, Version 2.0 (the "License");
588865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar * you may not use this file except in compliance with the License.
688865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar * You may obtain a copy of the License at
788865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar *
888865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar *      http://www.apache.org/licenses/LICENSE-2.0
988865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar *
1088865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar * Unless required by applicable law or agreed to in writing, software
1188865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar * distributed under the License is distributed on an "AS IS" BASIS,
1288865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1388865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar * See the License for the specific language governing permissions and
1488865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar * limitations under the License.
1588865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar */
1688865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar
17bdc4c86d3dff74f6634a38e2f7b316b0e823a2c8Alan Viveretteimport androidx.room.parser.SQLTypeAffinity
18bdc4c86d3dff74f6634a38e2f7b316b0e823a2c8Alan Viveretteimport androidx.room.verifier.ColumnInfo
1988865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyarimport java.sql.PreparedStatement
2088865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyarimport java.sql.ResultSet
2188865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyarimport java.sql.ResultSetMetaData
2288865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyarimport java.sql.SQLException
2388865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar
2488865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyarinternal fun <T> ResultSet.collect(f: (ResultSet) -> T): List<T> {
2588865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar    val result = arrayListOf<T>()
2688865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar    try {
2788865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar        while (next()) {
2888865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar            result.add(f.invoke(this))
2988865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar        }
3088865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar    } finally {
3188865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar        close()
3288865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar    }
3388865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar    return result
3488865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar}
3588865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar
366f1f5567abe765d30fda9c8fedce5617ecdeda9cAurimas Liutikasprivate fun <T> PreparedStatement.map(f: (Int, ResultSetMetaData) -> T): List<T> {
3788865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar    val columnCount = try {
3888865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar        metaData.columnCount
396f1f5567abe765d30fda9c8fedce5617ecdeda9cAurimas Liutikas    } catch (ex: SQLException) {
4088865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar        // ignore, no-result query
4188865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar        0
4288865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar    }
4388865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar    // return is separate than data creation because we want to know who throws the exception
4488865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar    return (1.rangeTo(columnCount)).map { f(it, metaData) }
4588865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar}
4688865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar
4788865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyarinternal fun PreparedStatement.columnNames(): List<String> {
4888865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar    return map { index, data -> data.getColumnName(index) }
4988865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar}
5088865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar
516f1f5567abe765d30fda9c8fedce5617ecdeda9cAurimas Liutikasprivate fun PreparedStatement.tryGetAffinity(columnIndex: Int): SQLTypeAffinity {
5288865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar    return try {
5388865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar        SQLTypeAffinity.valueOf(metaData.getColumnTypeName(columnIndex).capitalize())
546f1f5567abe765d30fda9c8fedce5617ecdeda9cAurimas Liutikas    } catch (ex: IllegalArgumentException) {
5588865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar        SQLTypeAffinity.NULL
5688865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar    }
5788865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar}
5888865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar
5988865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyarinternal fun PreparedStatement.columnInfo(): List<ColumnInfo> {
6096cc740203eaa752fc85ca7ca722a8de550ae88cYigit Boyar    //see: http://sqlite.1065341.n5.nabble.com/Column-order-in-resultset-td23127.html
6188865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar    return map { index, data -> ColumnInfo(data.getColumnName(index), tryGetAffinity(index)) }
6288865f77c35657a2bc545a718ca16a648fc8b62eYigit Boyar}
63