Initial Development #1
@ -15,6 +15,7 @@ object Count {
|
|||||||
* @param conn The connection over which documents should be counted
|
* @param conn The connection over which documents should be counted
|
||||||
* @return A count of the documents in the table
|
* @return A count of the documents in the table
|
||||||
*/
|
*/
|
||||||
|
@JvmStatic
|
||||||
fun all(tableName: String, conn: Connection) =
|
fun all(tableName: String, conn: Connection) =
|
||||||
conn.customScalar(Count.all(tableName), mapFunc = Results::toCount)
|
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
|
* @param tableName The name of the table in which documents should be counted
|
||||||
* @return A count of the documents in the table
|
* @return A count of the documents in the table
|
||||||
*/
|
*/
|
||||||
|
@JvmStatic
|
||||||
fun all(tableName: String) =
|
fun all(tableName: String) =
|
||||||
Configuration.dbConn().use { all(tableName, it) }
|
Configuration.dbConn().use { all(tableName, it) }
|
||||||
|
|
||||||
@ -36,6 +38,8 @@ object Count {
|
|||||||
* @param conn The connection on which the deletion should be executed
|
* @param conn The connection on which the deletion should be executed
|
||||||
* @return A count of the matching documents in the table
|
* @return A count of the matching documents in the table
|
||||||
*/
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
@JvmOverloads
|
||||||
fun byFields(
|
fun byFields(
|
||||||
tableName: String,
|
tableName: String,
|
||||||
fields: Collection<Field<*>>,
|
fields: Collection<Field<*>>,
|
||||||
@ -58,6 +62,8 @@ object Count {
|
|||||||
* @param howMatched How the fields should be matched
|
* @param howMatched How the fields should be matched
|
||||||
* @return A count of the matching documents in the table
|
* @return A count of the matching documents in the table
|
||||||
*/
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
@JvmOverloads
|
||||||
fun byFields(tableName: String, fields: Collection<Field<*>>, howMatched: FieldMatch? = null) =
|
fun byFields(tableName: String, fields: Collection<Field<*>>, howMatched: FieldMatch? = null) =
|
||||||
Configuration.dbConn().use { byFields(tableName, fields, howMatched, it) }
|
Configuration.dbConn().use { byFields(tableName, fields, howMatched, it) }
|
||||||
|
|
||||||
@ -70,6 +76,7 @@ object Count {
|
|||||||
* @return A count of the matching documents in the table
|
* @return A count of the matching documents in the table
|
||||||
* @throws DocumentException If called on a SQLite connection
|
* @throws DocumentException If called on a SQLite connection
|
||||||
*/
|
*/
|
||||||
|
@JvmStatic
|
||||||
inline fun <reified TContains> byContains(tableName: String, criteria: TContains, conn: Connection) =
|
inline fun <reified TContains> byContains(tableName: String, criteria: TContains, conn: Connection) =
|
||||||
conn.customScalar(Count.byContains(tableName), listOf(Parameters.json(":criteria", criteria)), Results::toCount)
|
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
|
* @return A count of the matching documents in the table
|
||||||
* @throws DocumentException If called on a SQLite connection
|
* @throws DocumentException If called on a SQLite connection
|
||||||
*/
|
*/
|
||||||
|
@JvmStatic
|
||||||
inline fun <reified TContains> byContains(tableName: String, criteria: TContains) =
|
inline fun <reified TContains> byContains(tableName: String, criteria: TContains) =
|
||||||
Configuration.dbConn().use { byContains(tableName, criteria, it) }
|
Configuration.dbConn().use { byContains(tableName, criteria, it) }
|
||||||
|
|
||||||
@ -93,6 +101,7 @@ object Count {
|
|||||||
* @return A count of the matching documents in the table
|
* @return A count of the matching documents in the table
|
||||||
* @throws DocumentException If called on a SQLite connection
|
* @throws DocumentException If called on a SQLite connection
|
||||||
*/
|
*/
|
||||||
|
@JvmStatic
|
||||||
fun byJsonPath(tableName: String, path: String, conn: Connection) =
|
fun byJsonPath(tableName: String, path: String, conn: Connection) =
|
||||||
conn.customScalar(
|
conn.customScalar(
|
||||||
Count.byJsonPath(tableName),
|
Count.byJsonPath(tableName),
|
||||||
@ -108,6 +117,7 @@ object Count {
|
|||||||
* @return A count of the matching documents in the table
|
* @return A count of the matching documents in the table
|
||||||
* @throws DocumentException If called on a SQLite connection
|
* @throws DocumentException If called on a SQLite connection
|
||||||
*/
|
*/
|
||||||
|
@JvmStatic
|
||||||
fun byJsonPath(tableName: String, path: String) =
|
fun byJsonPath(tableName: String, path: String) =
|
||||||
Configuration.dbConn().use { byJsonPath(tableName, path, it) }
|
Configuration.dbConn().use { byJsonPath(tableName, path, it) }
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ object Document {
|
|||||||
* @param document The document to be inserted
|
* @param document The document to be inserted
|
||||||
* @param conn The connection on which the query should be executed
|
* @param conn The connection on which the query should be executed
|
||||||
*/
|
*/
|
||||||
|
@JvmStatic
|
||||||
inline fun <reified TDoc> insert(tableName: String, document: TDoc, conn: Connection) {
|
inline fun <reified TDoc> insert(tableName: String, document: TDoc, conn: Connection) {
|
||||||
val strategy = Configuration.autoIdStrategy
|
val strategy = Configuration.autoIdStrategy
|
||||||
val query = if (strategy == AutoId.DISABLED) {
|
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 tableName The table into which the document should be inserted (may include schema)
|
||||||
* @param document The document to be inserted
|
* @param document The document to be inserted
|
||||||
*/
|
*/
|
||||||
|
@JvmStatic
|
||||||
inline fun <reified TDoc> insert(tableName: String, document: TDoc) =
|
inline fun <reified TDoc> insert(tableName: String, document: TDoc) =
|
||||||
Configuration.dbConn().use { insert(tableName, document, it) }
|
Configuration.dbConn().use { insert(tableName, document, it) }
|
||||||
|
|
||||||
@ -68,6 +70,7 @@ object Document {
|
|||||||
* @param document The document to be saved
|
* @param document The document to be saved
|
||||||
* @param conn The connection on which the query should be executed
|
* @param conn The connection on which the query should be executed
|
||||||
*/
|
*/
|
||||||
|
@JvmStatic
|
||||||
inline fun <reified TDoc> save(tableName: String, document: TDoc, conn: Connection) =
|
inline fun <reified TDoc> save(tableName: String, document: TDoc, conn: Connection) =
|
||||||
conn.customNonQuery(Document.save(tableName), listOf(Parameters.json(":data", document)))
|
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 tableName The table in which the document should be saved (may include schema)
|
||||||
* @param document The document to be saved
|
* @param document The document to be saved
|
||||||
*/
|
*/
|
||||||
|
@JvmStatic
|
||||||
inline fun <reified TDoc> save(tableName: String, document: TDoc) =
|
inline fun <reified TDoc> save(tableName: String, document: TDoc) =
|
||||||
Configuration.dbConn().use { save(tableName, document, it) }
|
Configuration.dbConn().use { save(tableName, document, it) }
|
||||||
|
|
||||||
@ -88,6 +92,7 @@ object Document {
|
|||||||
* @param document The document to be replaced
|
* @param document The document to be replaced
|
||||||
* @param conn The connection on which the query should be executed
|
* @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) =
|
inline fun <TKey, reified TDoc> update(tableName: String, docId: TKey, document: TDoc, conn: Connection) =
|
||||||
conn.customNonQuery(
|
conn.customNonQuery(
|
||||||
statementWhere(Document.update(tableName), Where.byId(":id", docId)),
|
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 docId The ID of the document to be replaced
|
||||||
* @param document 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) =
|
inline fun <TKey, reified TDoc> update(tableName: String, docId: TKey, document: TDoc) =
|
||||||
Configuration.dbConn().use { update(tableName, docId, document, it) }
|
Configuration.dbConn().use { update(tableName, docId, document, it) }
|
||||||
}
|
}
|
||||||
|
@ -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() {
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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("", "");
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user