Skip to content

Commit

Permalink
lib: refactor query builder handling
Browse files Browse the repository at this point in the history
Rather than having to remember to reset the query builder in each method
that uses it, these are now created on-demand from the db connection.

Signed-off-by: Maximilian Bosch <[email protected]>
  • Loading branch information
Ma27 authored and AndyScherzinger committed Oct 10, 2024
1 parent cf2b9b2 commit 176491b
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 106 deletions.
5 changes: 1 addition & 4 deletions lib/Controller/ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class ContactsController extends Controller {
private $contactsManager;
private $addressService;
private $dbconnection;
private $qb;
private $cdBackend;
private $avatarManager;
private $root;
Expand Down Expand Up @@ -69,7 +68,6 @@ public function __construct(
$this->contactsManager = $contactsManager;
$this->addressService = $addressService;
$this->dbconnection = $dbconnection;
$this->qb = $dbconnection->getQueryBuilder();
$this->cdBackend = $cdBackend;
$this->root = $root;
$this->urlGenerator = $urlGenerator;
Expand Down Expand Up @@ -669,7 +667,7 @@ private function getAddressBooksReadOnly(): array {
* @throws \OCP\DB\Exception
*/
private function setAddressCoordinates(float $lat, float $lng, string $adr, string $uri): void {
$qb = $this->qb;
$qb = $this->dbconnection->getQueryBuilder();
$adr_norm = strtolower(preg_replace('/\s+/', '', $adr));

$qb->select('id')
Expand Down Expand Up @@ -703,7 +701,6 @@ private function setAddressCoordinates(float $lat, float $lng, string $adr, stri
$req = $qb->execute();
$id = $qb->getLastInsertId();
}
$qb = $this->dbconnection->getQueryBuilder();
}


Expand Down
2 changes: 0 additions & 2 deletions lib/Controller/PublicContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
class PublicContactsController extends PublicPageController {
protected IManager $contactsManager;
protected AddressService $addressService;
protected IQueryBuilder $qb;
protected CardDavBackend $cdBackend;
protected IAvatarManager $avatarManager;
protected IRootFolder $root;
Expand All @@ -64,7 +63,6 @@ public function __construct(
$this->avatarManager = $avatarManager;
$this->contactsManager = $contactsManager;
$this->addressService = $addressService;
$this->qb = $dbconnection->getQueryBuilder();
$this->cdBackend = $cdBackend;
$this->root = $root;
}
Expand Down
67 changes: 32 additions & 35 deletions lib/Service/AddressService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
* @package OCA\Maps\Service
*/
class AddressService {
private $qb;
private $dbconnection;
private $jobList;
private $appData;
Expand All @@ -55,7 +54,6 @@ public function __construct(
IDBConnection $dbconnection,
) {
$this->dbconnection = $dbconnection;
$this->qb = $dbconnection->getQueryBuilder();
$this->memcache = $cacheFactory->createLocal('maps');
$this->jobList = $jobList;
$this->appData = $appData;
Expand All @@ -78,11 +76,12 @@ public function addressToGeo($adr, $uri): string {
*/
public function lookupAddress($adr, $uri): array {
$adr_norm = strtolower(preg_replace('/\s+/', '', $adr));
$this->qb->select('id', 'lat', 'lng', 'looked_up')
$qb = $this->dbconnection->getQueryBuilder();
$qb->select('id', 'lat', 'lng', 'looked_up')
->from('maps_address_geo')
->where($this->qb->expr()->eq('object_uri', $this->qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR)))
->andWhere($this->qb->expr()->eq('adr_norm', $this->qb->createNamedParameter($adr_norm, IQueryBuilder::PARAM_STR)));
$req = $this->qb->execute();
->where($qb->expr()->eq('object_uri', $qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR)))
->andWhere($qb->expr()->eq('adr_norm', $qb->createNamedParameter($adr_norm, IQueryBuilder::PARAM_STR)));
$req = $qb->execute();
$lat = null;
$lng = null;
$inDb = false;
Expand Down Expand Up @@ -122,14 +121,13 @@ public function lookupAddress($adr, $uri): array {

} else {
if ($lookedUp) {
$this->qb->update('maps_address_geo')
$qb->update('maps_address_geo')
->set('lat', $qb->createNamedParameter($lat, IQueryBuilder::PARAM_STR))
->set('lng', $qb->createNamedParameter($lng, IQueryBuilder::PARAM_STR))
->set('object_uri', $qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR))
->set('looked_up', $qb->createNamedParameter($lookedUp, IQueryBuilder::PARAM_BOOL))
->where($this->qb->expr()->eq('id', $this->qb->createNamedParameter($id, IQueryBuilder::PARAM_STR)));
$req = $this->qb->execute();
$qb = $this->dbconnection->getQueryBuilder();
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_STR)));
$req = $qb->execute();
}
}

Expand All @@ -149,11 +147,11 @@ private function lookupAddressInternal($adr): array {

$adr_norm = strtolower(preg_replace('/\s+/', '', $adr));

$this->qb->select('lat', 'lng')
$qb->select('lat', 'lng')

Check failure on line 150 in lib/Service/AddressService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedVariable

lib/Service/AddressService.php:150:3: UndefinedVariable: Cannot find referenced variable $qb (see https://psalm.dev/024)
->from('maps_address_geo')
->where($this->qb->expr()->eq('looked_up', $this->qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL)))
->andWhere($this->qb->expr()->eq('adr_norm', $this->qb->createNamedParameter($adr_norm, IQueryBuilder::PARAM_STR)));
$req = $this->qb->execute();
->where($qb->expr()->eq('looked_up', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL)))

Check failure on line 152 in lib/Service/AddressService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedVariable

lib/Service/AddressService.php:152:12: UndefinedVariable: Cannot find referenced variable $qb (see https://psalm.dev/024)

Check failure on line 152 in lib/Service/AddressService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedVariable

lib/Service/AddressService.php:152:41: UndefinedVariable: Cannot find referenced variable $qb (see https://psalm.dev/024)
->andWhere($qb->expr()->eq('adr_norm', $qb->createNamedParameter($adr_norm, IQueryBuilder::PARAM_STR)));

Check failure on line 153 in lib/Service/AddressService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedVariable

lib/Service/AddressService.php:153:15: UndefinedVariable: Cannot find referenced variable $qb (see https://psalm.dev/024)

Check failure on line 153 in lib/Service/AddressService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedVariable

lib/Service/AddressService.php:153:43: UndefinedVariable: Cannot find referenced variable $qb (see https://psalm.dev/024)
$req = $qb->execute();

Check failure on line 154 in lib/Service/AddressService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedVariable

lib/Service/AddressService.php:154:10: UndefinedVariable: Cannot find referenced variable $qb (see https://psalm.dev/024)
while ($row = $req->fetch()) {
$res[0] = $row['lat'];
$res[1] = $row['lng'];
Expand Down Expand Up @@ -236,7 +234,7 @@ public function scheduleVCardForLookup($cardData, $cardUri) {
}

private function cleanUpDBContactAddresses($vCard, $uri) {
$qb = $this->qb;
$qb = $this->dbconnection->getQueryBuilder();
// get all vcard addresses
$vCardAddresses = [];
foreach ($vCard->children() as $property) {
Expand All @@ -247,36 +245,34 @@ private function cleanUpDBContactAddresses($vCard, $uri) {
}
// check which addresses from DB is not in the vCard anymore
$adrIdToDelete = [];
$this->qb->select('id', 'adr')
$qb->select('id', 'adr')
->from('maps_address_geo')
->where($this->qb->expr()->eq('object_uri', $this->qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR)));
$req = $this->qb->execute();
->where($qb->expr()->eq('object_uri', $qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR)));
$req = $qb->execute();
while ($row = $req->fetch()) {
if (!in_array($row['adr'], $vCardAddresses)) {
array_push($adrIdToDelete, $row['id']);
}
}
$req->closeCursor();
$qb = $this->dbconnection->getQueryBuilder();

foreach ($adrIdToDelete as $id) {
$qb = $this->dbconnection->getQueryBuilder();
$qb->delete('maps_address_geo')
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
);
$req = $qb->execute();
$qb = $this->dbconnection->getQueryBuilder();
}
}

public function deleteDBContactAddresses($uri) {
$qb = $this->qb;
$qb = $this->dbconnection->getQueryBuilder();
$qb->delete('maps_address_geo')
->where(
$qb->expr()->eq('object_uri', $qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR))
);
$req = $qb->execute();
$qb = $this->dbconnection->getQueryBuilder();
}

// schedules the address for an external lookup
Expand All @@ -287,18 +283,18 @@ private function scheduleForLookup($adr, $uri): array {
$geo = $this->lookupAddressExternal($adr);
}
$adr_norm = strtolower(preg_replace('/\s+/', '', $adr));
$this->qb->insert('maps_address_geo')
$qb = $this->dbconnection->getQueryBuilder();
$qb->insert('maps_address_geo')
->values([
'adr' => $this->qb->createNamedParameter($adr, IQueryBuilder::PARAM_STR),
'adr_norm' => $this->qb->createNamedParameter($adr_norm, IQueryBuilder::PARAM_STR),
'object_uri' => $this->qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR),
'lat' => $this->qb->createNamedParameter($geo[0], IQueryBuilder::PARAM_STR),
'lng' => $this->qb->createNamedParameter($geo[1], IQueryBuilder::PARAM_STR),
'looked_up' => $this->qb->createNamedParameter($geo[2], IQueryBuilder::PARAM_BOOL),
'adr' => $qb->createNamedParameter($adr, IQueryBuilder::PARAM_STR),
'adr_norm' => $qb->createNamedParameter($adr_norm, IQueryBuilder::PARAM_STR),
'object_uri' => $qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR),
'lat' => $qb->createNamedParameter($geo[0], IQueryBuilder::PARAM_STR),
'lng' => $qb->createNamedParameter($geo[1], IQueryBuilder::PARAM_STR),
'looked_up' => $qb->createNamedParameter($geo[2], IQueryBuilder::PARAM_BOOL),
]);
$req = $this->qb->execute();
$id = $this->qb->getLastInsertId();
$qb = $this->dbconnection->getQueryBuilder();
$req = $qb->execute();
$id = $qb->getLastInsertId();
if (!$geo[2]) {
$this->jobList->add(LookupMissingGeoJob::class, []);
}
Expand All @@ -310,11 +306,12 @@ private function scheduleForLookup($adr, $uri): array {
public function lookupMissingGeo($max = 200):bool {
// stores if all addresses where looked up
$lookedUpAll = true;
$this->qb->select('adr', 'object_uri')
$qb = $this->dbconnection->getQueryBuilder();
$qb->select('adr', 'object_uri')
->from('maps_address_geo')
->where($this->qb->expr()->eq('looked_up', $this->qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)))
->where($qb->expr()->eq('looked_up', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)))
->setMaxResults($max);
$req = $this->qb->execute();
$req = $qb->execute();
$result = $req->fetchAll();
$req->closeCursor();
$i = 0;
Expand Down
Loading

0 comments on commit 176491b

Please sign in to comment.