Initial Development #1

Merged
danieljsummers merged 88 commits from v1-rc into main 2025-04-16 01:29:20 +00:00
5 changed files with 191 additions and 11 deletions
Showing only changes of commit 93f4700613 - Show all commits

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/documents.iml" filepath="$PROJECT_DIR$/documents.iml" />
</modules>
</component>
</project>

8
documents.iml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="AdditionalModuleElements">
<content url="file://$MODULE_DIR$" dumb="true">
<sourceFolder url="file://$MODULE_DIR$/src/integration-test/kotlin" isTestSource="true" />
</content>
</component>
</module>

View File

@ -0,0 +1,43 @@
package solutions.bitbadger.documents
import kotlinx.serialization.Serializable
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import solutions.bitbadger.documents.query.Definition
import solutions.bitbadger.documents.query.Find
import kotlin.test.assertEquals
class CustomSQLiteIT {
private val tbl = "test_table";
@BeforeEach
fun setUp() {
Configuration.connectionString = "jdbc:sqlite:memory"
}
/**
* Reset the dialect
*/
@AfterEach
fun cleanUp() {
Configuration.dialectValue = null
}
@Test
@DisplayName("list succeeds with empty list")
fun listEmpty() {
Configuration.dbConn().use { conn ->
conn.customNonQuery(Definition.ensureTable(tbl), listOf())
conn.customNonQuery(Definition.ensureKey(tbl, Dialect.SQLITE), listOf())
val result = conn.customList<TestDocument>(Find.all(tbl), listOf(), Results::fromData)
assertEquals(0, result.size, "There should have been no results")
}
}
}
@Serializable
data class TestDocument(val id: String)

View File

@ -11,11 +11,9 @@ import java.sql.ResultSet
* @param mapFunc The mapping function between the document and the domain item * @param mapFunc The mapping function between the document and the domain item
* @return A list of results for the given query * @return A list of results for the given query
*/ */
inline fun <reified TDoc> Connection.customList(query: String, parameters: Collection<Parameter<*>>, inline fun <reified TDoc> Connection.customList(
mapFunc: (ResultSet) -> TDoc): List<TDoc> = query: String, parameters: Collection<Parameter<*>>, mapFunc: (ResultSet) -> TDoc
Parameters.apply(this, query, parameters).use { stmt -> ) = Custom.list(query, parameters, this, mapFunc)
Results.toCustomList(stmt, mapFunc)
}
/** /**
* Execute a query that returns one or no results * Execute a query that returns one or no results
@ -25,9 +23,9 @@ inline fun <reified TDoc> Connection.customList(query: String, parameters: Colle
* @param mapFunc The mapping function between the document and the domain item * @param mapFunc The mapping function between the document and the domain item
* @return The document if one matches the query, `null` otherwise * @return The document if one matches the query, `null` otherwise
*/ */
inline fun <reified TDoc> Connection.customSingle(query: String, parameters: Collection<Parameter<*>>, inline fun <reified TDoc> Connection.customSingle(
mapFunc: (ResultSet) -> TDoc): TDoc? = query: String, parameters: Collection<Parameter<*>>, mapFunc: (ResultSet) -> TDoc
this.customList("$query LIMIT 1", parameters, mapFunc).singleOrNull() ) = Custom.single(query, parameters, this, mapFunc)
/** /**
* Execute a query that returns no results * Execute a query that returns no results
@ -35,6 +33,19 @@ inline fun <reified TDoc> Connection.customSingle(query: String, parameters: Col
* @param query The query to retrieve the results * @param query The query to retrieve the results
* @param parameters Parameters to use for the query * @param parameters Parameters to use for the query
*/ */
fun Connection.customNonQuery(query: String, parameters: Collection<Parameter<*>>) { fun Connection.customNonQuery(query: String, parameters: Collection<Parameter<*>>) =
Parameters.apply(this, query, parameters).use { it.executeUpdate() } Custom.nonQuery(query, parameters, this)
}
/**
* 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 <reified T> Connection.customScalar(
query: String,
parameters: Collection<Parameter<*>>,
mapFunc: (ResultSet) -> T & Any
) = Custom.scalar(query, parameters, this, mapFunc)

110
src/main/kotlin/Custom.kt Normal file
View File

@ -0,0 +1,110 @@
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 <reified TDoc> list(
query: String, parameters: Collection<Parameter<*>>, 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 <reified TDoc> list(
query: String, parameters: Collection<Parameter<*>>, 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 <reified TDoc> single(
query: String, parameters: Collection<Parameter<*>>, 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 <reified TDoc> single(
query: String, parameters: Collection<Parameter<*>>, 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<Parameter<*>>, 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<Parameter<*>>) =
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 <reified T> scalar(
query: String, parameters: Collection<Parameter<*>>, 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 <reified T> scalar(
query: String, parameters: Collection<Parameter<*>>, mapFunc: (ResultSet) -> T & Any
) = Configuration.dbConn().use { scalar(query, parameters, it, mapFunc) }
}