preparing storing of new contact
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user