From 7e91a7b73ffbbfbb2bc5ea0ed6d8e48225b1ee22 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Sun, 16 Mar 2025 17:29:52 -0400 Subject: [PATCH] Finish DocEx declarations --- .../src/main/kotlin/extensions/Connection.kt | 34 +++++++++++++ src/jvm/src/main/kotlin/jvm/Delete.kt | 13 ++++- src/jvm/src/main/kotlin/jvm/Document.kt | 15 ++++++ src/jvm/src/main/kotlin/jvm/Exists.kt | 13 ++++- src/jvm/src/main/kotlin/jvm/Find.kt | 31 ++++++++++-- src/jvm/src/main/kotlin/jvm/Patch.kt | 13 ++++- src/jvm/src/main/kotlin/jvm/RemoveFields.kt | 13 ++++- .../documents/java/ConfigurationTest.java | 48 +++++++++++++++++++ .../documents/java/support/JsonDocument.java | 10 +++- 9 files changed, 176 insertions(+), 14 deletions(-) create mode 100644 src/jvm/src/test/java/solutions/bitbadger/documents/java/ConfigurationTest.java diff --git a/src/jvm/src/main/kotlin/extensions/Connection.kt b/src/jvm/src/main/kotlin/extensions/Connection.kt index bdc56ff..9bdf51e 100644 --- a/src/jvm/src/main/kotlin/extensions/Connection.kt +++ b/src/jvm/src/main/kotlin/extensions/Connection.kt @@ -84,7 +84,9 @@ fun Connection.customScalar( * Create a document table if necessary * * @param tableName The table whose existence should be ensured (may include schema) + * @throws DocumentException If the dialect is not configured */ +@Throws(DocumentException::class) fun Connection.ensureTable(tableName: String) = Definition.ensureTable(tableName, this) @@ -94,7 +96,9 @@ fun Connection.ensureTable(tableName: String) = * @param tableName The table to be indexed (may include schema) * @param indexName The name of the index to create * @param fields One or more fields to be indexed< + * @throws DocumentException If any dependent process does */ +@Throws(DocumentException::class) fun Connection.ensureFieldIndex(tableName: String, indexName: String, fields: Collection) = Definition.ensureFieldIndex(tableName, indexName, fields, this) @@ -116,7 +120,9 @@ fun Connection.ensureDocumentIndex(tableName: String, indexType: DocumentIndex) * * @param tableName The table into which the document should be inserted (may include schema) * @param document The document to be inserted + * @throws DocumentException If IDs are misconfigured, or if the database command fails */ +@Throws(DocumentException::class) fun Connection.insert(tableName: String, document: TDoc) = Document.insert(tableName, document, this) @@ -125,7 +131,9 @@ fun Connection.insert(tableName: String, document: TDoc) = * * @param tableName The table in which the document should be saved (may include schema) * @param document The document to be saved + * @throws DocumentException If the database command fails */ +@Throws(DocumentException::class) fun Connection.save(tableName: String, document: TDoc) = Document.save(tableName, document, this) @@ -135,7 +143,9 @@ fun Connection.save(tableName: String, document: TDoc) = * @param tableName The table in which the document should be replaced (may include schema) * @param docId The ID of the document to be replaced * @param document The document to be replaced + * @throws DocumentException If no dialect has been configured, or if the database command fails */ +@Throws(DocumentException::class) fun Connection.update(tableName: String, docId: TKey, document: TDoc) = Document.update(tableName, docId, document, this) @@ -198,7 +208,9 @@ fun Connection.countByJsonPath(tableName: String, path: String) = * @param tableName The name of the table in which document existence should be checked * @param docId The ID of the document to be checked * @return True if the document exists, false if not + * @throws DocumentException If no dialect has been configured */ +@Throws(DocumentException::class) fun Connection.existsById(tableName: String, docId: TKey) = Exists.byId(tableName, docId, this) @@ -209,7 +221,9 @@ fun Connection.existsById(tableName: String, docId: TKey) = * @param fields The fields which should be compared * @param howMatched How the fields should be matched * @return True if any matching documents exist, false if not + * @throws DocumentException If no dialect has been configured, or if parameters are invalid */ +@Throws(DocumentException::class) @JvmOverloads fun Connection.existsByFields(tableName: String, fields: Collection>, howMatched: FieldMatch? = null) = Exists.byFields(tableName, fields, howMatched, this) @@ -247,7 +261,9 @@ fun Connection.existsByJsonPath(tableName: String, path: String) = * @param clazz The class of the document to be returned * @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering) * @return A list of documents from the given table + * @throws DocumentException If query execution fails */ +@Throws(DocumentException::class) @JvmOverloads fun Connection.findAll(tableName: String, clazz: Class, orderBy: Collection>? = null) = Find.all(tableName, clazz, orderBy, this) @@ -259,7 +275,9 @@ fun Connection.findAll(tableName: String, clazz: Class, orderBy: Co * @param docId The ID of the document to retrieve * @param clazz The class of the document to be returned * @return The document if it is found, `null` otherwise + * @throws DocumentException If no dialect has been configured */ +@Throws(DocumentException::class) fun Connection.findById(tableName: String, docId: TKey, clazz: Class) = Find.byId(tableName, docId, clazz, this) @@ -272,7 +290,9 @@ fun Connection.findById(tableName: String, docId: TKey, clazz: Clas * @param howMatched How the fields should be matched * @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering) * @return A list of documents matching the field comparison + * @throws DocumentException If no dialect has been configured, or if parameters are invalid */ +@Throws(DocumentException::class) @JvmOverloads fun Connection.findByFields( tableName: String, @@ -332,6 +352,7 @@ fun Connection.findByJsonPath( * @param howMatched How the fields should be matched (optional, defaults to `FieldMatch.ALL`) * @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering) * @return The first document matching the field comparison, or `null` if no matches are found + * @throws DocumentException If no dialect has been configured, or if parameters are invalid */ @Throws(DocumentException::class) @JvmOverloads @@ -392,7 +413,9 @@ fun Connection.findFirstByJsonPath( * @param tableName The name of the table in which a document should be patched * @param docId The ID of the document to be patched * @param patch The object whose properties should be replaced in the document + * @throws DocumentException If no dialect has been configured */ +@Throws(DocumentException::class) fun Connection.patchById(tableName: String, docId: TKey, patch: TPatch) = Patch.byId(tableName, docId, patch, this) @@ -403,7 +426,9 @@ fun Connection.patchById(tableName: String, docId: TKey, patch: T * @param fields The fields which should be compared * @param patch The object whose properties should be replaced in the document * @param howMatched How the fields should be matched + * @throws DocumentException If no dialect has been configured, or if parameters are invalid */ +@Throws(DocumentException::class) fun Connection.patchByFields( tableName: String, fields: Collection>, @@ -448,7 +473,9 @@ fun Connection.patchByJsonPath(tableName: String, path: String, patch: * @param tableName The name of the table in which the document's fields should be removed * @param docId The ID of the document to have fields removed * @param toRemove The names of the fields to be removed + * @throws DocumentException If no dialect has been configured */ +@Throws(DocumentException::class) fun Connection.removeFieldsById(tableName: String, docId: TKey, toRemove: Collection) = RemoveFields.byId(tableName, docId, toRemove, this) @@ -459,7 +486,9 @@ fun Connection.removeFieldsById(tableName: String, docId: TKey, toRemove: * @param fields The fields which should be compared * @param toRemove The names of the fields to be removed * @param howMatched How the fields should be matched + * @throws DocumentException If no dialect has been configured, or if parameters are invalid */ +@Throws(DocumentException::class) fun Connection.removeFieldsByFields( tableName: String, fields: Collection>, @@ -503,7 +532,9 @@ fun Connection.removeFieldsByJsonPath(tableName: String, path: String, toRemove: * * @param tableName The name of the table from which documents should be deleted * @param docId The ID of the document to be deleted + * @throws DocumentException If no dialect has been configured */ +@Throws(DocumentException::class) fun Connection.deleteById(tableName: String, docId: TKey) = Delete.byId(tableName, docId, this) @@ -513,7 +544,9 @@ fun Connection.deleteById(tableName: String, docId: TKey) = * @param tableName The name of the table from which documents should be deleted * @param fields The fields which should be compared * @param howMatched How the fields should be matched + * @throws DocumentException If no dialect has been configured, or if parameters are invalid */ +@Throws(DocumentException::class) @JvmOverloads fun Connection.deleteByFields(tableName: String, fields: Collection>, howMatched: FieldMatch? = null) = Delete.byFields(tableName, fields, howMatched, this) @@ -536,5 +569,6 @@ fun Connection.deleteByContains(tableName: String, criteria: TContai * @param path The JSON path comparison to match * @throws DocumentException If called on a SQLite connection */ +@Throws(DocumentException::class) fun Connection.deleteByJsonPath(tableName: String, path: String) = Delete.byJsonPath(tableName, path, this) diff --git a/src/jvm/src/main/kotlin/jvm/Delete.kt b/src/jvm/src/main/kotlin/jvm/Delete.kt index d442d7f..7cba82a 100644 --- a/src/jvm/src/main/kotlin/jvm/Delete.kt +++ b/src/jvm/src/main/kotlin/jvm/Delete.kt @@ -4,6 +4,7 @@ import solutions.bitbadger.documents.* import solutions.bitbadger.documents.extensions.customNonQuery import solutions.bitbadger.documents.query.DeleteQuery import java.sql.Connection +import kotlin.jvm.Throws /** * Functions to delete documents @@ -16,7 +17,9 @@ object Delete { * @param tableName The name of the table from which documents should be deleted * @param docId The ID of the document to be deleted * @param conn The connection on which the deletion should be executed + * @throws DocumentException If no dialect has been configured */ + @Throws(DocumentException::class) @JvmStatic fun byId(tableName: String, docId: TKey, conn: Connection) = conn.customNonQuery( @@ -29,7 +32,9 @@ object Delete { * * @param tableName The name of the table from which documents should be deleted * @param docId The ID of the document to be deleted + * @throws DocumentException If no connection string has been set */ + @Throws(DocumentException::class) @JvmStatic fun byId(tableName: String, docId: TKey) = Configuration.dbConn().use { byId(tableName, docId, it) } @@ -41,7 +46,9 @@ object Delete { * @param fields The fields which should be compared * @param howMatched How the fields should be matched * @param conn The connection on which the deletion should be executed + * @throws DocumentException If no dialect has been configured, or if parameters are invalid */ + @Throws(DocumentException::class) @JvmStatic @JvmOverloads fun byFields(tableName: String, fields: Collection>, howMatched: FieldMatch? = null, conn: Connection) { @@ -55,7 +62,9 @@ object Delete { * @param tableName The name of the table from which documents should be deleted * @param fields The fields which should be compared * @param howMatched How the fields should be matched + * @throws DocumentException If no connection string has been set, or if parameters are invalid */ + @Throws(DocumentException::class) @JvmStatic @JvmOverloads fun byFields(tableName: String, fields: Collection>, howMatched: FieldMatch? = null) = @@ -79,7 +88,7 @@ object Delete { * * @param tableName The name of the table from which documents should be deleted * @param criteria The object for which JSON containment should be checked - * @throws DocumentException If called on a SQLite connection + * @throws DocumentException If no connection string has been set, or if called on a SQLite connection */ @Throws(DocumentException::class) @JvmStatic @@ -104,7 +113,7 @@ object Delete { * * @param tableName The name of the table from which documents should be deleted * @param path The JSON path comparison to match - * @throws DocumentException If called on a SQLite connection + * @throws DocumentException If no connection string has been set, or if called on a SQLite connection */ @Throws(DocumentException::class) @JvmStatic diff --git a/src/jvm/src/main/kotlin/jvm/Document.kt b/src/jvm/src/main/kotlin/jvm/Document.kt index 7b2e0eb..69c4e64 100644 --- a/src/jvm/src/main/kotlin/jvm/Document.kt +++ b/src/jvm/src/main/kotlin/jvm/Document.kt @@ -2,12 +2,14 @@ package solutions.bitbadger.documents.jvm import solutions.bitbadger.documents.AutoId import solutions.bitbadger.documents.Configuration +import solutions.bitbadger.documents.DocumentException import solutions.bitbadger.documents.Field import solutions.bitbadger.documents.extensions.customNonQuery import solutions.bitbadger.documents.query.DocumentQuery import solutions.bitbadger.documents.query.Where import solutions.bitbadger.documents.query.statementWhere import java.sql.Connection +import kotlin.jvm.Throws /** * Functions for manipulating documents @@ -20,7 +22,9 @@ object Document { * @param tableName The table into which the document should be inserted (may include schema) * @param document The document to be inserted * @param conn The connection on which the query should be executed + * @throws DocumentException If IDs are misconfigured, or if the database command fails */ + @Throws(DocumentException::class) @JvmStatic fun insert(tableName: String, document: TDoc, conn: Connection) { val strategy = Configuration.autoIdStrategy @@ -37,7 +41,10 @@ object Document { * * @param tableName The table into which the document should be inserted (may include schema) * @param document The document to be inserted + * @throws DocumentException If no connection string has been set; if IDs are misconfigured; or if the database + * command fails */ + @Throws(DocumentException::class) @JvmStatic fun insert(tableName: String, document: TDoc) = Configuration.dbConn().use { insert(tableName, document, it) } @@ -48,7 +55,9 @@ object Document { * @param tableName The table in which the document should be saved (may include schema) * @param document The document to be saved * @param conn The connection on which the query should be executed + * @throws DocumentException If the database command fails */ + @Throws(DocumentException::class) @JvmStatic fun save(tableName: String, document: TDoc, conn: Connection) = conn.customNonQuery(DocumentQuery.save(tableName), listOf(Parameters.json(":data", document))) @@ -58,7 +67,9 @@ object Document { * * @param tableName The table in which the document should be saved (may include schema) * @param document The document to be saved + * @throws DocumentException If no connection string has been set, or if the database command fails */ + @Throws(DocumentException::class) @JvmStatic fun save(tableName: String, document: TDoc) = Configuration.dbConn().use { save(tableName, document, it) } @@ -70,7 +81,9 @@ object Document { * @param docId The ID of the document to be replaced * @param document The document to be replaced * @param conn The connection on which the query should be executed + * @throws DocumentException If no dialect has been configured, or if the database command fails */ + @Throws(DocumentException::class) @JvmStatic fun update(tableName: String, docId: TKey, document: TDoc, conn: Connection) = conn.customNonQuery( @@ -87,7 +100,9 @@ object Document { * @param tableName The table in which the document should be replaced (may include schema) * @param docId The ID of the document to be replaced * @param document The document to be replaced + * @throws DocumentException If no connection string has been set, or if the database command fails */ + @Throws(DocumentException::class) @JvmStatic fun update(tableName: String, docId: TKey, document: TDoc) = Configuration.dbConn().use { update(tableName, docId, document, it) } diff --git a/src/jvm/src/main/kotlin/jvm/Exists.kt b/src/jvm/src/main/kotlin/jvm/Exists.kt index 96d3cfb..990129e 100644 --- a/src/jvm/src/main/kotlin/jvm/Exists.kt +++ b/src/jvm/src/main/kotlin/jvm/Exists.kt @@ -4,6 +4,7 @@ import solutions.bitbadger.documents.* import solutions.bitbadger.documents.extensions.customScalar import solutions.bitbadger.documents.query.ExistsQuery import java.sql.Connection +import kotlin.jvm.Throws /** * Functions to determine whether documents exist @@ -17,7 +18,9 @@ object Exists { * @param docId The ID of the document to be checked * @param conn The connection on which the existence check should be executed * @return True if the document exists, false if not + * @throws DocumentException If no dialect has been configured */ + @Throws(DocumentException::class) @JvmStatic fun byId(tableName: String, docId: TKey, conn: Connection) = conn.customScalar( @@ -33,7 +36,9 @@ object Exists { * @param tableName The name of the table in which document existence should be checked * @param docId The ID of the document to be checked * @return True if the document exists, false if not + * @throws DocumentException If no connection string has been set */ + @Throws(DocumentException::class) @JvmStatic fun byId(tableName: String, docId: TKey) = Configuration.dbConn().use { byId(tableName, docId, it) } @@ -46,7 +51,9 @@ object Exists { * @param howMatched How the fields should be matched * @param conn The connection on which the existence check should be executed * @return True if any matching documents exist, false if not + * @throws DocumentException If no dialect has been configured, or if parameters are invalid */ + @Throws(DocumentException::class) @JvmStatic @JvmOverloads fun byFields( @@ -71,7 +78,9 @@ object Exists { * @param fields The fields which should be compared * @param howMatched How the fields should be matched * @return True if any matching documents exist, false if not + * @throws DocumentException If no connection string has been set, or if parameters are invalid */ + @Throws(DocumentException::class) @JvmStatic @JvmOverloads fun byFields(tableName: String, fields: Collection>, howMatched: FieldMatch? = null) = @@ -102,7 +111,7 @@ object Exists { * @param tableName The name of the table in which document existence should be checked * @param criteria The object for which JSON containment should be checked * @return True if any matching documents exist, false if not - * @throws DocumentException If called on a SQLite connection + * @throws DocumentException If no connection string has been set, or if called on a SQLite connection */ @Throws(DocumentException::class) @JvmStatic @@ -134,7 +143,7 @@ object Exists { * @param tableName The name of the table in which document existence should be checked * @param path The JSON path comparison to match * @return True if any matching documents exist, false if not - * @throws DocumentException If called on a SQLite connection + * @throws DocumentException If no connection string has been set, or if called on a SQLite connection */ @Throws(DocumentException::class) @JvmStatic diff --git a/src/jvm/src/main/kotlin/jvm/Find.kt b/src/jvm/src/main/kotlin/jvm/Find.kt index 59948cc..b63d52f 100644 --- a/src/jvm/src/main/kotlin/jvm/Find.kt +++ b/src/jvm/src/main/kotlin/jvm/Find.kt @@ -6,6 +6,7 @@ import solutions.bitbadger.documents.extensions.customSingle import solutions.bitbadger.documents.query.FindQuery import solutions.bitbadger.documents.query.orderBy import java.sql.Connection +import kotlin.jvm.Throws /** * Functions to find and retrieve documents @@ -20,7 +21,9 @@ object Find { * @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering) * @param conn The connection over which documents should be retrieved * @return A list of documents from the given table + * @throws DocumentException If query execution fails */ + @Throws(DocumentException::class) @JvmStatic fun all(tableName: String, clazz: Class, orderBy: Collection>? = null, conn: Connection) = conn.customList(FindQuery.all(tableName) + (orderBy?.let(::orderBy) ?: ""), listOf(), clazz, Results::fromData) @@ -32,7 +35,9 @@ object Find { * @param clazz The class of the document to be returned * @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering) * @return A list of documents from the given table + * @throws DocumentException If no connection string has been set, or if query execution fails */ + @Throws(DocumentException::class) @JvmStatic @JvmOverloads fun all(tableName: String, clazz: Class, orderBy: Collection>? = null) = @@ -45,7 +50,9 @@ object Find { * @param clazz The class of the document to be returned * @param conn The connection over which documents should be retrieved * @return A list of documents from the given table + * @throws DocumentException If query execution fails */ + @Throws(DocumentException::class) @JvmStatic fun all(tableName: String, clazz: Class, conn: Connection) = all(tableName, clazz, null, conn) @@ -58,7 +65,9 @@ object Find { * @param clazz The class of the document to be returned * @param conn The connection over which documents should be retrieved * @return The document if it is found, `null` otherwise + * @throws DocumentException If no dialect has been configured */ + @Throws(DocumentException::class) @JvmStatic fun byId(tableName: String, docId: TKey, clazz: Class, conn: Connection) = conn.customSingle( @@ -75,7 +84,9 @@ object Find { * @param docId The ID of the document to retrieve * @param clazz The class of the document to be returned * @return The document if it is found, `null` otherwise + * @throws DocumentException If no connection string has been set */ + @Throws(DocumentException::class) @JvmStatic fun byId(tableName: String, docId: TKey, clazz: Class) = Configuration.dbConn().use { byId(tableName, docId, clazz, it) } @@ -90,7 +101,9 @@ object Find { * @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering) * @param conn The connection over which documents should be retrieved * @return A list of documents matching the field comparison + * @throws DocumentException If no dialect has been configured, or if parameters are invalid */ + @Throws(DocumentException::class) @JvmStatic fun byFields( tableName: String, @@ -118,7 +131,9 @@ object Find { * @param howMatched How the fields should be matched * @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering) * @return A list of documents matching the field comparison + * @throws DocumentException If no connection string has been set, or if parameters are invalid */ + @Throws(DocumentException::class) @JvmStatic @JvmOverloads fun byFields( @@ -139,7 +154,9 @@ object Find { * @param howMatched How the fields should be matched * @param conn The connection over which documents should be retrieved * @return A list of documents matching the field comparison + * @throws DocumentException If no dialect has been configured, or if parameters are invalid */ + @Throws(DocumentException::class) @JvmStatic fun byFields( tableName: String, @@ -185,7 +202,7 @@ object Find { * @param clazz The class of the document to be returned * @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering) * @return A list of documents matching the JSON containment query - * @throws DocumentException If called on a SQLite connection + * @throws DocumentException If no connection string has been set, or if called on a SQLite connection */ @Throws(DocumentException::class) @JvmStatic @@ -248,7 +265,7 @@ object Find { * @param clazz The class of the document to be returned * @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering) * @return A list of documents matching the JSON Path match query - * @throws DocumentException If called on a SQLite connection + * @throws DocumentException If no connection string has been set, or if called on a SQLite connection */ @Throws(DocumentException::class) @JvmStatic @@ -281,7 +298,9 @@ object Find { * @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering) * @param conn The connection over which documents should be retrieved * @return The first document matching the field comparison, or `null` if no matches are found + * @throws DocumentException If no dialect has been configured, or if parameters are invalid */ + @Throws(DocumentException::class) @JvmStatic fun firstByFields( tableName: String, @@ -309,7 +328,9 @@ object Find { * @param howMatched How the fields should be matched (optional, defaults to `FieldMatch.ALL`) * @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering) * @return The first document matching the field comparison, or `null` if no matches are found + * @throws DocumentException If no connection string has been set, or if parameters are invalid */ + @Throws(DocumentException::class) @JvmStatic @JvmOverloads fun firstByFields( @@ -330,7 +351,9 @@ object Find { * @param howMatched How the fields should be matched (optional, defaults to `FieldMatch.ALL`) * @param conn The connection over which documents should be retrieved * @return The first document matching the field comparison, or `null` if no matches are found + * @throws DocumentException If no dialect has been configured, or if parameters are invalid */ + @Throws(DocumentException::class) @JvmStatic fun firstByFields( tableName: String, @@ -395,7 +418,7 @@ object Find { * @param clazz The class of the document to be returned * @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering) * @return The first document matching the JSON containment query, or `null` if no matches are found - * @throws DocumentException If called on a SQLite connection + * @throws DocumentException If no connection string has been set, or if called on a SQLite connection */ @Throws(DocumentException::class) @JvmStatic @@ -458,7 +481,7 @@ object Find { * @param clazz The class of the document to be returned * @param orderBy Fields by which the query should be ordered (optional, defaults to no ordering) * @return The first document matching the JSON Path match query, or `null` if no matches are found - * @throws DocumentException If called on a SQLite connection + * @throws DocumentException If no connection string has been set, or if called on a SQLite connection */ @Throws(DocumentException::class) @JvmStatic diff --git a/src/jvm/src/main/kotlin/jvm/Patch.kt b/src/jvm/src/main/kotlin/jvm/Patch.kt index 1b09991..1097d9c 100644 --- a/src/jvm/src/main/kotlin/jvm/Patch.kt +++ b/src/jvm/src/main/kotlin/jvm/Patch.kt @@ -4,6 +4,7 @@ import solutions.bitbadger.documents.* import solutions.bitbadger.documents.extensions.customNonQuery import solutions.bitbadger.documents.query.PatchQuery import java.sql.Connection +import kotlin.jvm.Throws /** * Functions to patch (partially update) documents @@ -17,7 +18,9 @@ object Patch { * @param docId The ID of the document to be patched * @param patch The object whose properties should be replaced in the document * @param conn The connection on which the update should be executed + * @throws DocumentException If no dialect has been configured */ + @Throws(DocumentException::class) @JvmStatic fun byId(tableName: String, docId: TKey, patch: TPatch, conn: Connection) = conn.customNonQuery( @@ -34,7 +37,9 @@ object Patch { * @param tableName The name of the table in which a document should be patched * @param docId The ID of the document to be patched * @param patch The object whose properties should be replaced in the document + * @throws DocumentException If no connection string has been set */ + @Throws(DocumentException::class) @JvmStatic fun byId(tableName: String, docId: TKey, patch: TPatch) = Configuration.dbConn().use { byId(tableName, docId, patch, it) } @@ -47,7 +52,9 @@ object Patch { * @param patch The object whose properties should be replaced in the document * @param howMatched How the fields should be matched * @param conn The connection on which the update should be executed + * @throws DocumentException If no dialect has been configured, or if parameters are invalid */ + @Throws(DocumentException::class) @JvmStatic fun byFields( tableName: String, @@ -72,7 +79,9 @@ object Patch { * @param fields The fields which should be compared * @param patch The object whose properties should be replaced in the document * @param howMatched How the fields should be matched + * @throws DocumentException If no connection string has been set, or if parameters are invalid */ + @Throws(DocumentException::class) @JvmStatic @JvmOverloads fun byFields( @@ -106,7 +115,7 @@ object Patch { * @param tableName The name of the table in which documents should be patched * @param criteria The object against which JSON containment should be checked * @param patch The object whose properties should be replaced in the document - * @throws DocumentException If called on a SQLite connection + * @throws DocumentException If no connection string has been set, or if called on a SQLite connection */ @Throws(DocumentException::class) @JvmStatic @@ -136,7 +145,7 @@ object Patch { * @param tableName The name of the table in which documents should be patched * @param path The JSON path comparison to match * @param patch The object whose properties should be replaced in the document - * @throws DocumentException If called on a SQLite connection + * @throws DocumentException If no connection string has been set, or if called on a SQLite connection */ @Throws(DocumentException::class) @JvmStatic diff --git a/src/jvm/src/main/kotlin/jvm/RemoveFields.kt b/src/jvm/src/main/kotlin/jvm/RemoveFields.kt index 0484b1e..b69fbf1 100644 --- a/src/jvm/src/main/kotlin/jvm/RemoveFields.kt +++ b/src/jvm/src/main/kotlin/jvm/RemoveFields.kt @@ -4,6 +4,7 @@ import solutions.bitbadger.documents.* import solutions.bitbadger.documents.extensions.customNonQuery import solutions.bitbadger.documents.query.RemoveFieldsQuery import java.sql.Connection +import kotlin.jvm.Throws /** * Functions to remove fields from documents @@ -31,7 +32,9 @@ object RemoveFields { * @param docId The ID of the document to have fields removed * @param toRemove The names of the fields to be removed * @param conn The connection on which the update should be executed + * @throws DocumentException If no dialect has been configured */ + @Throws(DocumentException::class) @JvmStatic fun byId(tableName: String, docId: TKey, toRemove: Collection, conn: Connection) { val nameParams = Parameters.fieldNames(toRemove) @@ -50,7 +53,9 @@ object RemoveFields { * @param tableName The name of the table in which the document's fields should be removed * @param docId The ID of the document to have fields removed * @param toRemove The names of the fields to be removed + * @throws DocumentException If no connection string has been set */ + @Throws(DocumentException::class) @JvmStatic fun byId(tableName: String, docId: TKey, toRemove: Collection) = Configuration.dbConn().use { byId(tableName, docId, toRemove, it) } @@ -63,7 +68,9 @@ object RemoveFields { * @param toRemove The names of the fields to be removed * @param howMatched How the fields should be matched * @param conn The connection on which the update should be executed + * @throws DocumentException If no dialect has been configured, or if parameters are invalid */ + @Throws(DocumentException::class) @JvmStatic fun byFields( tableName: String, @@ -87,7 +94,9 @@ object RemoveFields { * @param fields The fields which should be compared * @param toRemove The names of the fields to be removed * @param howMatched How the fields should be matched + * @throws DocumentException If no connection string has been set, or if parameters are invalid */ + @Throws(DocumentException::class) @JvmStatic @JvmOverloads fun byFields( @@ -128,7 +137,7 @@ object RemoveFields { * @param tableName The name of the table in which document fields should be removed * @param criteria The object against which JSON containment should be checked * @param toRemove The names of the fields to be removed - * @throws DocumentException If called on a SQLite connection + * @throws DocumentException If no connection string has been set, or if called on a SQLite connection */ @Throws(DocumentException::class) @JvmStatic @@ -160,7 +169,7 @@ object RemoveFields { * @param tableName The name of the table in which document fields should be removed * @param path The JSON path comparison to match * @param toRemove The names of the fields to be removed - * @throws DocumentException If called on a SQLite connection + * @throws DocumentException If no connection string has been set, or if called on a SQLite connection */ @Throws(DocumentException::class) @JvmStatic diff --git a/src/jvm/src/test/java/solutions/bitbadger/documents/java/ConfigurationTest.java b/src/jvm/src/test/java/solutions/bitbadger/documents/java/ConfigurationTest.java new file mode 100644 index 0000000..2a7ef00 --- /dev/null +++ b/src/jvm/src/test/java/solutions/bitbadger/documents/java/ConfigurationTest.java @@ -0,0 +1,48 @@ +package solutions.bitbadger.documents.java; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import solutions.bitbadger.documents.AutoId; +import solutions.bitbadger.documents.Configuration; +import solutions.bitbadger.documents.Dialect; +import solutions.bitbadger.documents.DocumentException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * Unit tests for the `Configuration` object + */ +@DisplayName("JVM | Java | Configuration") +final public class ConfigurationTest { + + @Test + @DisplayName("Default ID field is `id`") + public void defaultIdField() { + assertEquals("id", Configuration.idField, "Default ID field incorrect"); + } + + @Test + @DisplayName("Default Auto ID strategy is `DISABLED`") + public void defaultAutoId() { + assertEquals(AutoId.DISABLED, Configuration.autoIdStrategy, "Default Auto ID strategy should be `disabled`"); + } + + @Test + @DisplayName("Default ID string length should be 16") + public void defaultIdStringLength() { + assertEquals(16, Configuration.idStringLength, "Default ID string length should be 16"); + } + + @Test + @DisplayName("Dialect is derived from connection string") + public void dialectIsDerived() throws DocumentException { + try { + assertThrows(DocumentException.class, Configuration::dialect); + Configuration.setConnectionString("jdbc:postgresql:db"); + assertEquals(Dialect.POSTGRESQL, Configuration.dialect()); + } finally { + Configuration.setConnectionString(null); + } + } +} diff --git a/src/jvm/src/test/java/solutions/bitbadger/documents/java/support/JsonDocument.java b/src/jvm/src/test/java/solutions/bitbadger/documents/java/support/JsonDocument.java index 4d5b085..8b118cc 100644 --- a/src/jvm/src/test/java/solutions/bitbadger/documents/java/support/JsonDocument.java +++ b/src/jvm/src/test/java/solutions/bitbadger/documents/java/support/JsonDocument.java @@ -1,10 +1,12 @@ package solutions.bitbadger.documents.java.support; +import solutions.bitbadger.documents.DocumentException; import solutions.bitbadger.documents.jvm.Document; import solutions.bitbadger.documents.support.ThrowawayDatabase; import java.util.List; +import static org.junit.jupiter.api.Assertions.fail; import static solutions.bitbadger.documents.support.TypesKt.TEST_TABLE; public class JsonDocument { @@ -69,8 +71,12 @@ public class JsonDocument { new JsonDocument("five", "purple", 18)); public static void load(ThrowawayDatabase db, String tableName) { - for (JsonDocument doc : testDocuments) { - Document.insert(tableName, doc, db.getConn()); + try { + for (JsonDocument doc : testDocuments) { + Document.insert(tableName, doc, db.getConn()); + } + } catch (DocumentException ex) { + fail("Could not load test documents", ex); } }