preparing storing of new contact

This commit is contained in:
2025-10-09 20:34:38 +02:00
parent b0364ec77e
commit e0c05506c3
7 changed files with 129 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ package de.srsoftware.umbrella.contact;
import static de.srsoftware.tools.jdbc.Condition.equal;
import static de.srsoftware.tools.jdbc.Query.SelectQuery.ALL;
import static de.srsoftware.tools.jdbc.Query.insertInto;
import static de.srsoftware.tools.jdbc.Query.select;
import static de.srsoftware.umbrella.contact.Constants.*;
import static de.srsoftware.umbrella.core.Constants.*;
@@ -92,7 +93,16 @@ public class SqliteDb extends BaseDb implements ContactDb{
@Override
public Contact save(Contact contact) {
if (contact.id() == 0){ // new contact
throw UmbrellaException.unprocessable("Storing of new contacts not implemented");
try {
var rs = insertInto(TABLE_CONTACTS,DATA).values(contact.vcard()).execute(db).getGeneratedKeys();
Long id = null;
if (rs.next()) id = rs.getLong(1);
rs.close();
if (id != null) return new Contact(id,contact.vcard());
throw databaseException("Failed to store new vcard");
} catch (SQLException e) {
throw databaseException("Failed to store new vcard",e);
}
} else try { // update
Query.update(TABLE_CONTACTS).set(DATA).where(ID,equal(contact.id())).prepare(db).apply(contact.vcard()).execute();
} catch (SQLException e) {