package solutions.bitbadger.documents import java.sql.Connection import java.sql.ResultSet /** * Custom query execution functions */ object Custom { /** * Execute a query that returns a list of results * * @param query The query to retrieve the results * @param parameters Parameters to use for the query * @param conn The connection over which the query should be executed * @param mapFunc The mapping function between the document and the domain item * @return A list of results for the given query */ inline fun list( query: String, parameters: Collection> = listOf(), conn: Connection, mapFunc: (ResultSet) -> TDoc ) = Parameters.apply(conn, query, parameters).use { Results.toCustomList(it, mapFunc) } /** * Execute a query that returns a list of results (creates connection) * * @param query The query to retrieve the results * @param parameters Parameters to use for the query * @param mapFunc The mapping function between the document and the domain item * @return A list of results for the given query */ inline fun list( query: String, parameters: Collection> = listOf(), mapFunc: (ResultSet) -> TDoc ) = Configuration.dbConn().use { list(query, parameters, it, mapFunc) } /** * Execute a query that returns one or no results * * @param query The query to retrieve the results * @param parameters Parameters to use for the query * @param conn The connection over which the query should be executed * @param mapFunc The mapping function between the document and the domain item * @return The document if one matches the query, `null` otherwise */ inline fun single( query: String, parameters: Collection> = listOf(), conn: Connection, mapFunc: (ResultSet) -> TDoc ) = list("$query LIMIT 1", parameters, conn, mapFunc).singleOrNull() /** * Execute a query that returns one or no results * * @param query The query to retrieve the results * @param parameters Parameters to use for the query * @param mapFunc The mapping function between the document and the domain item * @return The document if one matches the query, `null` otherwise */ inline fun single( query: String, parameters: Collection> = listOf(), mapFunc: (ResultSet) -> TDoc ) = Configuration.dbConn().use { single(query, parameters, it, mapFunc) } /** * Execute a query that returns no results * * @param query The query to retrieve the results * @param conn The connection over which the query should be executed * @param parameters Parameters to use for the query */ fun nonQuery(query: String, parameters: Collection> = listOf(), conn: Connection) { Parameters.apply(conn, query, parameters).use { it.executeUpdate() } } /** * Execute a query that returns no results * * @param query The query to retrieve the results * @param parameters Parameters to use for the query */ fun nonQuery(query: String, parameters: Collection> = listOf()) = Configuration.dbConn().use { nonQuery(query, parameters, it) } /** * Execute a query that returns a scalar result * * @param query The query to retrieve the result * @param parameters Parameters to use for the query * @param conn The connection over which the query should be executed * @param mapFunc The mapping function between the document and the domain item * @return The scalar value from the query */ inline fun scalar( query: String, parameters: Collection> = listOf(), conn: Connection, mapFunc: (ResultSet) -> T & Any ) = Parameters.apply(conn, query, parameters).use { stmt -> stmt.executeQuery().use { rs -> rs.next() mapFunc(rs) } } /** * Execute a query that returns a scalar result * * @param query The query to retrieve the result * @param parameters Parameters to use for the query * @param mapFunc The mapping function between the document and the domain item * @return The scalar value from the query */ inline fun scalar( query: String, parameters: Collection> = listOf(), mapFunc: (ResultSet) -> T & Any ) = Configuration.dbConn().use { scalar(query, parameters, it, mapFunc) } }