From cb15546e6c59cc54deaa4feb6b8475341c9018ac Mon Sep 17 00:00:00 2001 From: Martin Matous Date: Fri, 2 Aug 2019 10:20:09 +0200 Subject: [PATCH] lib: reduce prepare() calls in loops --- lib/Controller/DbController.php | 39 +++++------ lib/Service/StatementService.php | 115 +++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 20 deletions(-) create mode 100644 lib/Service/StatementService.php diff --git a/lib/Controller/DbController.php b/lib/Controller/DbController.php index d3a48120..cc97eb8e 100644 --- a/lib/Controller/DbController.php +++ b/lib/Controller/DbController.php @@ -22,6 +22,8 @@ use OCP\ILogger; use OCP\ITagManager; +use OCA\audioplayer\Service\StatementService; + /** * Controller class for main page. */ @@ -35,6 +37,7 @@ class DbController extends Controller private $shareManager; private $tagManager; private $logger; + private $statements; public function __construct( $appName, @@ -44,7 +47,8 @@ public function __construct( IDbConnection $db, ITagManager $tagManager, IManager $shareManager, - ILogger $logger + ILogger $logger, + StatementService $statements ) { parent::__construct($appName, $request); @@ -54,6 +58,7 @@ public function __construct( $this->shareManager = $shareManager; $this->tagManager = $tagManager; $this->logger = $logger; + $this->statements = $statements; } public function loadArtistsToAlbum($iAlbumId, $ARtistID) @@ -270,7 +275,7 @@ public function deleteFromDB($file_id, $userId = null) */ public function writeCoverToAlbum($userId, $iAlbumId, $sImage) { - $stmt = $this->db->prepare('UPDATE `*PREFIX*audioplayer_albums` SET `cover`= ?, `bgcolor`= ? WHERE `id` = ? AND `user_id` = ?'); + $stmt = $this->statements->updateAlbumCover(); $stmt->execute(array($sImage, '', $iAlbumId, $userId)); return true; } @@ -291,25 +296,25 @@ public function writeAlbumToDB($userId, $sAlbum, $sYear, $iArtistId, $parentId) $sYear = $this->normalizeInteger($sYear); $AlbumCount = 0; - $stmt = $this->db->prepare('SELECT `id`, `artist_id` FROM `*PREFIX*audioplayer_albums` WHERE `user_id` = ? AND `name` = ? AND `folder_id` = ?'); + $stmt = $this->statements->selectAlbumIdArtistId(); $stmt->execute(array($userId, $sAlbum, $parentId)); $row = $stmt->fetch(); if ($row) { if ((int)$row['artist_id'] !== (int)$iArtistId) { $various_id = $this->writeArtistToDB($userId, $this->l10n->t('Various Artists')); - $stmt = $this->db->prepare('UPDATE `*PREFIX*audioplayer_albums` SET `artist_id`= ? WHERE `id` = ? AND `user_id` = ?'); + $stmt = $this->statements->updateAlbumArtistId(); $stmt->execute(array($various_id, $row['id'], $userId)); } $insertid = $row['id']; } else { - $stmt = $this->db->prepare('INSERT INTO `*PREFIX*audioplayer_albums` (`user_id`,`name`,`folder_id`) VALUES(?,?,?)'); + $stmt = $this->statements->insertAlbum(); $stmt->execute(array($userId, $sAlbum, $parentId)); $insertid = $this->db->lastInsertId('*PREFIX*audioplayer_albums'); if ($iArtistId) { - $stmt = $this->db->prepare('UPDATE `*PREFIX*audioplayer_albums` SET `year`= ?, `artist_id`= ? WHERE `id` = ? AND `user_id` = ?'); + $stmt = $this->statements->updateAlbumYearArtistId(); $stmt->execute(array((int)$sYear, $iArtistId, $insertid, $userId)); } else { - $stmt = $this->db->prepare('UPDATE `*PREFIX*audioplayer_albums` SET `year`= ? WHERE `id` = ? AND `user_id` = ?'); + $stmt = $this->statements->updateAlbumYear(); $stmt->execute(array((int)$sYear, $insertid, $userId)); } $AlbumCount = 1; @@ -366,13 +371,13 @@ public function writeArtistToDB($userId, $sArtist) { $sArtist = $this->truncate($sArtist, '256'); - $stmt = $this->db->prepare('SELECT `id` FROM `*PREFIX*audioplayer_artists` WHERE `user_id` = ? AND `name` = ?'); + $stmt = $this->statements->selectArtistId(); $stmt->execute(array($userId, $sArtist)); $row = $stmt->fetch(); if ($row) { return $row['id']; } else { - $stmt = $this->db->prepare('INSERT INTO `*PREFIX*audioplayer_artists` (`user_id`,`name`) VALUES(?,?)'); + $stmt = $this->statements->insertArtist(); $stmt->execute(array($userId, $sArtist)); $insertid = $this->db->lastInsertId('*PREFIX*audioplayer_artists'); return $insertid; @@ -399,13 +404,13 @@ public function writeGenreToDB($userId, $sGenre) { $sGenre = $this->truncate($sGenre, '256'); - $stmt = $this->db->prepare('SELECT `id` FROM `*PREFIX*audioplayer_genre` WHERE `user_id` = ? AND `name` = ?'); + $stmt = $this->statements->selectGenreId(); $stmt->execute(array($userId, $sGenre)); $row = $stmt->fetch(); if ($row) { return $row['id']; } else { - $stmt = $this->db->prepare('INSERT INTO `*PREFIX*audioplayer_genre` (`user_id`,`name`) VALUES(?,?)'); + $stmt = $this->statements->insertGenre(); $stmt->execute(array($userId, $sGenre)); $insertid = $this->db->lastInsertId('*PREFIX*audioplayer_genre'); return $insertid; @@ -422,11 +427,7 @@ public function writeTrackToDB($userId, $aTrack) { $dublicate = 0; $insertid = 0; - $SQL = 'SELECT `id` FROM `*PREFIX*audioplayer_tracks` WHERE `user_id`= ? AND `title`= ? AND `number`= ? - AND `artist_id`= ? AND `album_id`= ? AND `length`= ? AND `bitrate`= ? - AND `mimetype`= ? AND `genre_id`= ? AND `year`= ? - AND `disc`= ? AND `composer`= ? AND `subtitle`= ?'; - $stmt = $this->db->prepare($SQL); + $stmt = $this->statements->selectTrackId(); $stmt->execute(array($userId, $aTrack['title'], $aTrack['number'], @@ -445,7 +446,7 @@ public function writeTrackToDB($userId, $aTrack) if (isset($row['id'])) { $dublicate = 1; } else { - $stmt = $this->db->prepare('INSERT INTO `*PREFIX*audioplayer_tracks` (`user_id`,`title`,`number`,`artist_id`,`album_id`,`length`,`file_id`,`bitrate`,`mimetype`,`genre_id`,`year`,`folder_id`,`disc`,`composer`,`subtitle`,`isrc`,`copyright`) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)'); + $stmt = $this->statements->insertTrack(); $stmt->execute(array($userId, $aTrack['title'], $aTrack['number'], @@ -656,9 +657,7 @@ public function setSessionValue($type, $value, $userId) */ public function getSessionValue($type) { - //return $this->session->get($type); - $SQL = 'SELECT `configvalue` FROM `*PREFIX*preferences` WHERE `userid`= ? AND `appid`= ? AND `configkey`= ?'; - $stmt = $this->db->prepare($SQL); + $stmt = $this->statements->getSessionValue($SQL); $stmt->execute(array($this->userId, 'audioplayer', $type)); $row = $stmt->fetch(); return $row['configvalue']; diff --git a/lib/Service/StatementService.php b/lib/Service/StatementService.php new file mode 100644 index 00000000..ac870b97 --- /dev/null +++ b/lib/Service/StatementService.php @@ -0,0 +1,115 @@ +db = $db; + $this->statements = array(); + $this->output = $output; + } + + public function selectTrackId() { + $key = 'selectTrackId'; + $SQL = 'SELECT `id` FROM `*PREFIX*audioplayer_tracks` WHERE `user_id`= ? AND `title`= ? AND `number`= ? + AND `artist_id`= ? AND `album_id`= ? AND `length`= ? AND `bitrate`= ? + AND `mimetype`= ? AND `genre_id`= ? AND `year`= ? + AND `disc`= ? AND `composer`= ? AND `subtitle`= ?'; + return $this->getStatement($key, $SQL); + } + + public function insertTrack() { + $key = 'insertTrack'; + $SQL = 'INSERT INTO `*PREFIX*audioplayer_tracks` + (`user_id`,`title`,`number`,`artist_id`, + `album_id`,`length`,`file_id`,`bitrate`, + `mimetype`,`genre_id`,`year`,`folder_id`, + `disc`,`composer`,`subtitle`,`isrc`,`copyright`) + VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)'; + return $this->getStatement($key, $SQL); + } + + public function getSessionValue() { + $key = 'getSessionValue'; + $SQL = 'SELECT `configvalue` FROM `*PREFIX*preferences` + WHERE `userid`= ? AND `appid`= ? AND `configkey`= ?'; + return $this->getStatement($key, $SQL); + } + + public function selectGenreId() { + $key = 'selectGenreId'; + $SQL = 'SELECT `id` FROM `*PREFIX*audioplayer_genre` WHERE `user_id` = ? AND `name` = ?'; + return $this->getStatement($key, $SQL); + } + + public function insertGenre() { + $key = 'insertGenre'; + $SQL = 'INSERT INTO `*PREFIX*audioplayer_genre` (`user_id`,`name`) VALUES(?,?)'; + return $this->getStatement($key, $SQL); + } + + public function selectArtistId() { + $key = 'selectArtistId'; + $SQL = 'SELECT `id` FROM `*PREFIX*audioplayer_artists` WHERE `user_id` = ? AND `name` = ?'; + return $this->getStatement($key, $SQL); + } + + public function insertArtist() { + $key = 'insertArtist'; + $SQL = 'INSERT INTO `*PREFIX*audioplayer_artists` (`user_id`,`name`) VALUES(?,?)'; + return $this->getStatement($key, $SQL); + } + + public function selectAlbumIdArtistId() { + $key = 'selectAlbumIdArtistId'; + $SQL = 'SELECT `id`, `artist_id` FROM `*PREFIX*audioplayer_albums` + WHERE `user_id` = ? AND `name` = ? AND `folder_id` = ?'; + return $this->getStatement($key, $SQL); + } + + public function updateAlbumArtistId() { + $key = 'updateAlbumArtistId'; + $SQL = 'UPDATE `*PREFIX*audioplayer_albums` SET `artist_id`= ? WHERE `id` = ? AND `user_id` = ?'; + return $this->getStatement($key, $SQL); + } + + public function updateAlbumYearArtistId() { + $key = 'updateAlbumYearArtistId'; + $SQL = 'UPDATE `*PREFIX*audioplayer_albums` SET `year`= ?, `artist_id`= ? + WHERE `id` = ? AND `user_id` = ?'; + return $this->getStatement($key, $SQL); + } + + public function updateAlbumYear() { + $key = 'updateAlbumYear'; + $SQL = 'UPDATE `*PREFIX*audioplayer_albums` SET `year`= ? WHERE `id` = ? AND `user_id` = ?'; + return $this->getStatement($key, $SQL); + } + + public function insertAlbum() { + $key = 'insertAlbum'; + $SQL = 'INSERT INTO `*PREFIX*audioplayer_albums` (`user_id`,`name`,`folder_id`) VALUES(?,?,?)'; + return $this->getStatement($key, $SQL); + } + + public function updateAlbumCover() { + $key = 'updateAlbumCover'; + $SQL = 'UPDATE `*PREFIX*audioplayer_albums` SET `cover`= ?, `bgcolor`= ? + WHERE `id` = ? AND `user_id` = ?'; + return $this->getStatement($key, $SQL); + } + + private function getStatement(string $key, string $SQL) { + if (isset($this->statements[$key])) { + return $this->statements[$key]; + } + $stmt = $this->db->prepare($SQL); + $this->statements[$key] = $stmt; + return $stmt; + } +}