public function writeToDB($userId, $smsList, $purgeAllSmsBeforeInsert = false)
{
\OCP\DB::beginTransaction();
if ($purgeAllSmsBeforeInsert === true) {
$query = \OCP\DB::prepare('DELETE FROM *PREFIX*ocsms_smsdatas ' . 'WHERE user_id = ?');
$result = $query->execute(array($userId));
}
foreach ($smsList as $sms) {
$smsFlags = sprintf("%s%s", $sms["read"] === "true" ? "1" : "0", $sms["seen"] === "true" ? "1" : "0");
// Only delete if we haven't purged the DB
if ($purgeAllSmsBeforeInsert === false) {
// Remove previous record
// @ TODO: only update the required fields, getAllIds can be useful
$query = \OCP\DB::prepare('DELETE FROM *PREFIX*ocsms_smsdatas ' . 'WHERE user_id = ? AND sms_id = ?');
$result = $query->execute(array($userId, (int) $sms["_id"]));
}
$now = date("Y-m-d H:i:s");
$query = \OCP\DB::prepare('INSERT INTO *PREFIX*ocsms_smsdatas ' . '(user_id, added, lastmodified, sms_flags, sms_date, sms_id,' . 'sms_address, sms_msg, sms_mailbox, sms_type) VALUES ' . '(?,?,?,?,?,?,?,?,?,?)');
$result = $query->execute(array($userId, $now, $now, $smsFlags, $sms["date"], (int) $sms["_id"], $sms["address"], $sms["body"], (int) $sms["mbox"], (int) $sms["type"]));
}
\OCP\DB::commit();
}