From 0d6841d106dcd12e357a6ef3d9ef3a51c07c4483 Mon Sep 17 00:00:00 2001 From: Gilles Crettenand Date: Thu, 21 May 2015 14:55:16 +0200 Subject: [PATCH] Improve speed of wish related function --- lib/User.php | 76 +++++++++++++++++++++----------------------- mobile.netbiblio.php | 6 ++-- 2 files changed, 38 insertions(+), 44 deletions(-) diff --git a/lib/User.php b/lib/User.php index a99542a..d27aa41 100644 --- a/lib/User.php +++ b/lib/User.php @@ -165,38 +165,29 @@ class User extends DbMapping * Add a book to the wish list if it is not already inside. * * delete the wishes cache for it to be reloaded the next time getWishes will be called. - * @param int $noticeId + * @param string $noticeNr * @return bool */ - public function addWish($noticeId) + public function addWish($noticeNr) { - $noticeId = str_replace("'", "''", $noticeId); - if (!$this->hasWish($noticeId)) { - // recover last id - $idSQL = "SELECT WishID from Counters"; - $idResult = Connection::execute($idSQL, true); - // return print_r($idResult, 1); - if ($row = $idResult->next()) { - // get new value - $newWishID = $row['WishID'] + 1; + if (! $this->hasWish($noticeNr)) { + $sql = "UPDATE Counters + SET WishID = WishID + 1 + OUTPUT INSERTED.WishID;"; + $result = Connection::execute($sql, true); - // update counter - $idSQL = "UPDATE Counters SET WishID=" . $newWishID; - Connection::execute($idSQL, true); - - $table = User::$wishTableName; + if ($row = $result->current()) { $employee_id = Configuration::get('www_employee_id'); $library_id = Configuration::get('www_library_id'); - $strSQL = "INSERT INTO $table (WishID, NoticeID, " . User::$idColumn . ", CreationDate, EmployeeID, BranchOfficeID, Remark, ModificationDate)"; - $strSQL .= " VALUES($newWishID, $noticeId, $this->id, GETDATE(), $employee_id, $library_id, '', GETDATE())"; - - // return $strSQL; - Connection::execute($strSQL); - - // $this->wishes = NULL; + $sql = sprintf("INSERT INTO %s + (WishID, NoticeID, %s, CreationDate, EmployeeID, BranchOfficeID, Remark, ModificationDate) + SELECT %s , NoticeID, %s, GETDATE() , %s , %s , '' , GETDATE() + FROM Notices + WHERE LTRIM(RTRIM(NoticeNr)) = '%s';", + User::$wishTableName, User::$idColumn, $row['WishID'], $this->id, $employee_id, $library_id, $noticeNr); + + Connection::execute($sql); return true; - } else { - return false; } } return false; @@ -204,17 +195,20 @@ class User extends DbMapping /** * Return true if the book is in the wish list - * @param int $noticeId + * @param string $noticeNr * @return bool */ - public function hasWish($noticeId) + private function hasWish($noticeNr) { - foreach ($this->getWishes() as $book) { - if ($book['id'] == $noticeId) { - return true; - } - } - return false; + $sql = sprintf("SELECT w.NoticeID + FROM Wishes AS w + INNER JOIN Notices AS n ON n.NoticeID = w.NoticeID + WHERE + LTRIM(RTRIM(n.NoticeNr)) = '%s' + AND w.UseraccountId = %s;", $noticeNr, $this->id); + $result = Connection::execute($sql); + + return $result->current() !== false; } /** @@ -243,14 +237,16 @@ class User extends DbMapping /** * Remove a book from the wish list - * @param int $noticeId + * @param string $noticeNr */ - public function deleteWish($noticeId) + public function deleteWish($noticeNr) { - $noticeId = str_replace("'", "''", $noticeId); - $table = User::$wishTableName; - $strSQL = "DELETE FROM $table"; - $strSQL .= " WHERE NoticeID = $noticeId AND " . User::$idColumn . " = $this->id;"; - Connection::execute($strSQL, true); + $sql = sprintf("DELETE w + FROM %s AS w + INNER JOIN Notices AS n ON n.NoticeID = w.NoticeID + WHERE + LTRIM(RTRIM(n.NoticeNr)) = '%s' + AND %s = %s;", User::$wishTableName, $noticeNr, User::$idColumn, $this->id); + Connection::execute($sql, true); } } \ No newline at end of file diff --git a/mobile.netbiblio.php b/mobile.netbiblio.php index a921356..356bc29 100644 --- a/mobile.netbiblio.php +++ b/mobile.netbiblio.php @@ -230,14 +230,12 @@ class NetBiblio extends WebService public function AddWish($bookNr) { - $id = AudioBook::findIdByCode(intval($bookNr)); - $this->data[] = $this->getUser()->addWish($id); + $this->data[] = $this->getUser()->addWish($bookNr); } public function DeleteWish($bookNr) { - $id = AudioBook::findIdByCode(intval($bookNr)); - $this->getUser()->deleteWish($id); + $this->getUser()->deleteWish($bookNr); } public function FindBooks($codes)