1
0
mirror of https://github.com/nerzhul/ocsms.git synced 2025-06-07 16:06:15 +00:00

Migrate more functions to the query builder

This commit is contained in:
Loic Blot 2018-04-10 08:38:27 +02:00
parent 25eb211440
commit 2b8e2ceb41
No known key found for this signature in database
GPG Key ID: EFAA458E8C153987

View File

@ -65,11 +65,14 @@ class SmsMapper extends Mapper {
} }
public function getLastTimestamp ($userId) { public function getLastTimestamp ($userId) {
$query = \OCP\DB::prepare('SELECT max(sms_date) as mx FROM ' . $qb = $this->db->getQueryBuilder();
'*PREFIX*ocsms_smsdatas WHERE user_id = ?');
$result = $query->execute(array($userId));
if ($row = $result->fetchRow()) { $qb->select('MAX(sms_date) as mx')
->from('ocsms_smsdatas')
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)));
$result = $qb->execute();
if ($row = $result->fetch()) {
return $row["mx"]; return $row["mx"];
} }
@ -77,17 +80,25 @@ class SmsMapper extends Mapper {
} }
public function getAllPhoneNumbers ($userId) { public function getAllPhoneNumbers ($userId) {
$query = \OCP\DB::prepare('SELECT sms_address FROM ' . $qb = $this->db->getQueryBuilder();
'*PREFIX*ocsms_smsdatas WHERE user_id = ? AND sms_mailbox IN (?,?,?)'); $qb->select('sms_address')
$result = $query->execute(array($userId, 0, 1, 3)); ->from('ocsms_smsdatas')
->where($qb->expr()->andX(
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId)),
$qb->expr()->in('sms_mailbox', array(0, 1, 3))
)
);
$result = $qb->execute();
$phoneList = array(); $phoneList = array();
while($row = $result->fetchRow()) { while($row = $result->fetch()) {
$pn = $row["sms_address"]; $pn = $row["sms_address"];
if (!in_array($pn, $phoneList)) { if (!in_array($pn, $phoneList)) {
array_push($phoneList, $pn); array_push($phoneList, $pn);
} }
} }
$result->closeCursor();
return $phoneList; return $phoneList;
} }
@ -95,11 +106,18 @@ class SmsMapper extends Mapper {
* get all possible SMS_adresses for a given formated phonenumber * get all possible SMS_adresses for a given formated phonenumber
*/ */
public function getAllPhoneNumbersForFPN ($userId, $phoneNumber, $country) { public function getAllPhoneNumbersForFPN ($userId, $phoneNumber, $country) {
$query = \OCP\DB::prepare('SELECT sms_address FROM ' . $qb = $this->db->getQueryBuilder();
'*PREFIX*ocsms_smsdatas WHERE user_id = ? AND sms_mailbox IN (?,?,?)'); $qb->select('sms_address')
$result = $query->execute(array($userId, 0, 1, 3)); ->from('ocsms_smsdatas')
->where($qb->expr()->andX(
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId)),
$qb->expr()->in('sms_mailbox', array(0, 1, 3))
)
);
$result = $qb->execute();
$phoneList = array(); $phoneList = array();
while($row = $result->fetchRow()) { while($row = $result->fetch()) {
$pn = $row["sms_address"]; $pn = $row["sms_address"];
$fmtPN = PhoneNumberFormatter::format($country, $pn); $fmtPN = PhoneNumberFormatter::format($country, $pn);
if (!isset($phoneList[$fmtPN])) { if (!isset($phoneList[$fmtPN])) {
@ -141,10 +159,14 @@ class SmsMapper extends Mapper {
} }
public function getMessageCount ($userId) { public function getMessageCount ($userId) {
$query = \OCP\DB::prepare('SELECT count(*) AS count FROM ' . $qb = $this->db->getQueryBuilder();
'*PREFIX*ocsms_smsdatas WHERE user_id = ?'); $qb->select('COUNT(*) AS count')
$result = $query->execute(array($userId)); ->from('ocsms_smsdatas')
if ($row = $result->fetchRow()) { ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId))
);
$result = $qb->execute();
if ($row = $result->fetch()) {
return $row["count"]; return $row["count"];
} }