Skip to content

Commit

Permalink
lib: reduce prepare() calls in loops
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Matous committed Aug 3, 2019
1 parent 728672e commit cb15546
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 20 deletions.
39 changes: 19 additions & 20 deletions lib/Controller/DbController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use OCP\ILogger;
use OCP\ITagManager;

use OCA\audioplayer\Service\StatementService;

/**
* Controller class for main page.
*/
Expand All @@ -35,6 +37,7 @@ class DbController extends Controller
private $shareManager;
private $tagManager;
private $logger;
private $statements;

public function __construct(
$appName,
Expand All @@ -44,7 +47,8 @@ public function __construct(
IDbConnection $db,
ITagManager $tagManager,
IManager $shareManager,
ILogger $logger
ILogger $logger,
StatementService $statements
)
{
parent::__construct($appName, $request);
Expand All @@ -54,6 +58,7 @@ public function __construct(
$this->shareManager = $shareManager;
$this->tagManager = $tagManager;
$this->logger = $logger;
$this->statements = $statements;
}

public function loadArtistsToAlbum($iAlbumId, $ARtistID)
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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'],
Expand All @@ -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'],
Expand Down Expand Up @@ -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'];
Expand Down
115 changes: 115 additions & 0 deletions lib/Service/StatementService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
namespace OCA\audioplayer\Service;

use OCP\IDbConnection;

class StatementService {
private $db;
private $statements;
private $output;

public function __construct(IDbConnection $db) {
$this->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;
}
}

0 comments on commit cb15546

Please sign in to comment.