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 149 additions and 0 deletions
Showing only changes of commit b17ef73ff8 - Show all commits

View File

@ -15,6 +15,7 @@ object Count {
* @param conn The connection over which documents should be counted
* @return A count of the documents in the table
*/
@JvmStatic
fun all(tableName: String, conn: Connection) =
conn.customScalar(Count.all(tableName), mapFunc = Results::toCount)
@ -24,6 +25,7 @@ object Count {
* @param tableName The name of the table in which documents should be counted
* @return A count of the documents in the table
*/
@JvmStatic
fun all(tableName: String) =
Configuration.dbConn().use { all(tableName, it) }
@ -36,6 +38,8 @@ object Count {
* @param conn The connection on which the deletion should be executed
* @return A count of the matching documents in the table
*/
@JvmStatic
@JvmOverloads
fun byFields(
tableName: String,
fields: Collection<Field<*>>,
@ -58,6 +62,8 @@ object Count {
* @param howMatched How the fields should be matched
* @return A count of the matching documents in the table
*/
@JvmStatic
@JvmOverloads
fun byFields(tableName: String, fields: Collection<Field<*>>, howMatched: FieldMatch? = null) =
Configuration.dbConn().use { byFields(tableName, fields, howMatched, it) }
@ -70,6 +76,7 @@ object Count {
* @return A count of the matching documents in the table
* @throws DocumentException If called on a SQLite connection
*/
@JvmStatic
inline fun <reified TContains> byContains(tableName: String, criteria: TContains, conn: Connection) =
conn.customScalar(Count.byContains(tableName), listOf(Parameters.json(":criteria", criteria)), Results::toCount)
@ -81,6 +88,7 @@ object Count {
* @return A count of the matching documents in the table
* @throws DocumentException If called on a SQLite connection
*/
@JvmStatic
inline fun <reified TContains> byContains(tableName: String, criteria: TContains) =
Configuration.dbConn().use { byContains(tableName, criteria, it) }
@ -93,6 +101,7 @@ object Count {
* @return A count of the matching documents in the table
* @throws DocumentException If called on a SQLite connection
*/
@JvmStatic
fun byJsonPath(tableName: String, path: String, conn: Connection) =
conn.customScalar(
Count.byJsonPath(tableName),
@ -108,6 +117,7 @@ object Count {
* @return A count of the matching documents in the table
* @throws DocumentException If called on a SQLite connection
*/
@JvmStatic
fun byJsonPath(tableName: String, path: String) =
Configuration.dbConn().use { byJsonPath(tableName, path, it) }
}

View File

@ -17,6 +17,7 @@ object Document {
* @param document The document to be inserted
* @param conn The connection on which the query should be executed
*/
@JvmStatic
inline fun <reified TDoc> insert(tableName: String, document: TDoc, conn: Connection) {
val strategy = Configuration.autoIdStrategy
val query = if (strategy == AutoId.DISABLED) {
@ -58,6 +59,7 @@ object Document {
* @param tableName The table into which the document should be inserted (may include schema)
* @param document The document to be inserted
*/
@JvmStatic
inline fun <reified TDoc> insert(tableName: String, document: TDoc) =
Configuration.dbConn().use { insert(tableName, document, it) }
@ -68,6 +70,7 @@ object Document {
* @param document The document to be saved
* @param conn The connection on which the query should be executed
*/
@JvmStatic
inline fun <reified TDoc> save(tableName: String, document: TDoc, conn: Connection) =
conn.customNonQuery(Document.save(tableName), listOf(Parameters.json(":data", document)))
@ -77,6 +80,7 @@ object Document {
* @param tableName The table in which the document should be saved (may include schema)
* @param document The document to be saved
*/
@JvmStatic
inline fun <reified TDoc> save(tableName: String, document: TDoc) =
Configuration.dbConn().use { save(tableName, document, it) }
@ -88,6 +92,7 @@ object Document {
* @param document The document to be replaced
* @param conn The connection on which the query should be executed
*/
@JvmStatic
inline fun <TKey, reified TDoc> update(tableName: String, docId: TKey, document: TDoc, conn: Connection) =
conn.customNonQuery(
statementWhere(Document.update(tableName), Where.byId(":id", docId)),
@ -104,6 +109,7 @@ object Document {
* @param docId The ID of the document to be replaced
* @param document The document to be replaced
*/
@JvmStatic
inline fun <TKey, reified TDoc> update(tableName: String, docId: TKey, document: TDoc) =
Configuration.dbConn().use { update(tableName, docId, document, it) }
}

View File

@ -0,0 +1,20 @@
package solutions.bitbadger.documents.java.integration.common;
import solutions.bitbadger.documents.integration.ThrowawayDatabase;
import solutions.bitbadger.documents.java.testDocs.JsonDocument;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static solutions.bitbadger.documents.integration.TypesKt.TEST_TABLE;
final public class Count {
public static void all(ThrowawayDatabase db) {
JsonDocument.load(db);
assertEquals(5L, solutions.bitbadger.documents.Count.all(TEST_TABLE, db.getConn()),
"There should have been 5 documents in the table");
}
private Count() {
}
}

View File

@ -0,0 +1,81 @@
package solutions.bitbadger.documents.java.testDocs;
import solutions.bitbadger.documents.Document;
import solutions.bitbadger.documents.integration.ThrowawayDatabase;
import java.util.List;
import static solutions.bitbadger.documents.integration.TypesKt.TEST_TABLE;
public class JsonDocument {
private String id;
private String value;
private int numValue;
private SubDocument sub;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public int getNumValue() {
return numValue;
}
public void setNumValue(int numValue) {
this.numValue = numValue;
}
public SubDocument getSub() {
return sub;
}
public void setSub(SubDocument sub) {
this.sub = sub;
}
public JsonDocument(String id, String value, int numValue, SubDocument sub) {
this.id = id;
this.value = value;
this.numValue = numValue;
this.sub = sub;
}
public JsonDocument(String id, String value, int numValue) {
this(id, value, numValue, null);
}
public JsonDocument(String id) {
this(id, "", 0, null);
}
private static final List<JsonDocument> testDocuments = List.of(
new JsonDocument("one", "FIRST!", 0),
new JsonDocument("two", "another", 10, new SubDocument("green", "blue")),
new JsonDocument("three", "", 4),
new JsonDocument("four", "purple", 17, new SubDocument("green", "red")),
new JsonDocument("five", "purple", 18));
public static void load(ThrowawayDatabase db, String tableName) {
for (JsonDocument doc : testDocuments) {
// TODO: inline reified generics cannot be called from Java :(
// Document.insert(tableName, doc, db.getConn());
}
}
public static void load(ThrowawayDatabase db) {
load(db, TEST_TABLE);
}
}

View File

@ -0,0 +1,32 @@
package solutions.bitbadger.documents.java.testDocs;
public class SubDocument {
private String foo;
private String bar;
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
public String getBar() {
return bar;
}
public void setBar(String bar) {
this.bar = bar;
}
public SubDocument(String foo, String bar) {
this.foo = foo;
this.bar = bar;
}
public SubDocument() {
this("", "");
}
}