Use the query builder for the joblist queries

remotes/origin/appinfo-object
Joas Schilling 2015-12-18 09:47:54 +07:00
parent 1456e910ad
commit 345e68cafa
2 changed files with 92 additions and 44 deletions

@ -28,22 +28,20 @@ use OCP\BackgroundJob\IJobList;
use OCP\AutoloadNotAllowedException; use OCP\AutoloadNotAllowedException;
class JobList implements IJobList { class JobList implements IJobList {
/** /** @var \OCP\IDBConnection */
* @var \OCP\IDBConnection protected $connection;
*/
private $conn;
/** /**
* @var \OCP\IConfig $config * @var \OCP\IConfig $config
*/ */
private $config; protected $config;
/** /**
* @param \OCP\IDBConnection $conn * @param \OCP\IDBConnection $connection
* @param \OCP\IConfig $config * @param \OCP\IConfig $config
*/ */
public function __construct($conn, $config) { public function __construct($connection, $config) {
$this->conn = $conn; $this->connection = $connection;
$this->config = $config; $this->config = $config;
} }
@ -58,12 +56,20 @@ class JobList implements IJobList {
} else { } else {
$class = $job; $class = $job;
} }
$argument = json_encode($argument); $argument = json_encode($argument);
if (strlen($argument) > 4000) { if (strlen($argument) > 4000) {
throw new \InvalidArgumentException('Background job arguments can\'t exceed 4000 characters (json encoded)'); throw new \InvalidArgumentException('Background job arguments can\'t exceed 4000 characters (json encoded)');
} }
$query = $this->conn->prepare('INSERT INTO `*PREFIX*jobs`(`class`, `argument`, `last_run`) VALUES(?, ?, 0)');
$query->execute(array($class, $argument)); $query = $this->connection->getQueryBuilder();
$query->insert('jobs')
->values([
'class' => $query->createNamedParameter($class),
'argument' => $query->createNamedParameter($argument),
'last_run' => $query->createNamedParameter(0, \PDO::PARAM_INT),
]);
$query->execute();
} }
} }
@ -77,19 +83,25 @@ class JobList implements IJobList {
} else { } else {
$class = $job; $class = $job;
} }
$query = $this->connection->getQueryBuilder();
$query->delete('jobs')
->where($query->expr()->eq('class', $query->createNamedParameter($class)));
if (!is_null($argument)) { if (!is_null($argument)) {
$argument = json_encode($argument); $argument = json_encode($argument);
$query = $this->conn->prepare('DELETE FROM `*PREFIX*jobs` WHERE `class` = ? AND `argument` = ?'); $query->andWhere($query->expr()->eq('argument', $query->createNamedParameter($argument)));
$query->execute(array($class, $argument));
} else {
$query = $this->conn->prepare('DELETE FROM `*PREFIX*jobs` WHERE `class` = ?');
$query->execute(array($class));
} }
$query->execute();
} }
/**
* @param int $id
*/
protected function removeById($id) { protected function removeById($id) {
$query = $this->conn->prepare('DELETE FROM `*PREFIX*jobs` WHERE `id` = ?'); $query = $this->connection->getQueryBuilder();
$query->execute([$id]); $query->delete('jobs')
->where($query->expr()->eq('id', $query->createNamedParameter($id, \PDO::PARAM_INT)));
$query->execute();
} }
/** /**
@ -106,9 +118,19 @@ class JobList implements IJobList {
$class = $job; $class = $job;
} }
$argument = json_encode($argument); $argument = json_encode($argument);
$query = $this->conn->prepare('SELECT `id` FROM `*PREFIX*jobs` WHERE `class` = ? AND `argument` = ?');
$query->execute(array($class, $argument)); $query = $this->connection->getQueryBuilder();
return (bool)$query->fetch(); $query->select('id')
->from('jobs')
->where($query->expr()->eq('class', $query->createNamedParameter($class)))
->andWhere($query->expr()->eq('argument', $query->createNamedParameter($argument)))
->setMaxResults(1);
$result = $query->execute();
$row = $result->fetch();
$result->closeCursor();
return (bool) $row;
} }
/** /**
@ -117,15 +139,20 @@ class JobList implements IJobList {
* @return Job[] * @return Job[]
*/ */
public function getAll() { public function getAll() {
$query = $this->conn->prepare('SELECT `id`, `class`, `last_run`, `argument` FROM `*PREFIX*jobs`'); $query = $this->connection->getQueryBuilder();
$query->execute(); $query->select('*')
$jobs = array(); ->from('jobs');
while ($row = $query->fetch()) { $result = $query->execute();
$jobs = [];
while ($row = $result->fetch()) {
$job = $this->buildJob($row); $job = $this->buildJob($row);
if ($job) { if ($job) {
$jobs[] = $job; $jobs[] = $job;
} }
} }
$result->closeCursor();
return $jobs; return $jobs;
} }
@ -136,22 +163,39 @@ class JobList implements IJobList {
*/ */
public function getNext() { public function getNext() {
$lastId = $this->getLastJob(); $lastId = $this->getLastJob();
$query = $this->conn->prepare('SELECT `id`, `class`, `last_run`, `argument` FROM `*PREFIX*jobs` WHERE `id` > ? ORDER BY `id` ASC', 1);
$query->execute(array($lastId)); $query = $this->connection->getQueryBuilder();
if ($row = $query->fetch()) { $query->select('*')
->from('jobs')
->where($query->expr()->gt('id', $query->createNamedParameter($lastId, \PDO::PARAM_INT)))
->orderBy('id', 'ASC')
->setMaxResults(1);
$result = $query->execute();
$row = $result->fetch();
$result->closeCursor();
if ($row) {
$jobId = $row['id']; $jobId = $row['id'];
$job = $this->buildJob($row); $job = $this->buildJob($row);
} else { } else {
//begin at the start of the queue //begin at the start of the queue
$query = $this->conn->prepare('SELECT `id`, `class`, `last_run`, `argument` FROM `*PREFIX*jobs` ORDER BY `id` ASC', 1); $query = $this->connection->getQueryBuilder();
$query->execute(); $query->select('*')
if ($row = $query->fetch()) { ->from('jobs')
->orderBy('id', 'ASC')
->setMaxResults(1);
$result = $query->execute();
$row = $result->fetch();
$result->closeCursor();
if ($row) {
$jobId = $row['id']; $jobId = $row['id'];
$job = $this->buildJob($row); $job = $this->buildJob($row);
} else { } else {
return null; //empty job list return null; //empty job list
} }
} }
if (is_null($job)) { if (is_null($job)) {
$this->removeById($jobId); $this->removeById($jobId);
return $this->getNext(); return $this->getNext();
@ -165,9 +209,15 @@ class JobList implements IJobList {
* @return Job|null * @return Job|null
*/ */
public function getById($id) { public function getById($id) {
$query = $this->conn->prepare('SELECT `id`, `class`, `last_run`, `argument` FROM `*PREFIX*jobs` WHERE `id` = ?'); $query = $this->connection->getQueryBuilder();
$query->execute(array($id)); $query->select('*')
if ($row = $query->fetch()) { ->from('jobs')
->where($query->expr()->eq('id', $query->createNamedParameter($id, \PDO::PARAM_INT)));
$result = $query->execute();
$row = $result->fetch();
$result->closeCursor();
if ($row) {
return $this->buildJob($row); return $this->buildJob($row);
} else { } else {
return null; return null;
@ -225,7 +275,10 @@ class JobList implements IJobList {
* @param Job $job * @param Job $job
*/ */
public function setLastRun($job) { public function setLastRun($job) {
$query = $this->conn->prepare('UPDATE `*PREFIX*jobs` SET `last_run` = ? WHERE `id` = ?'); $query = $this->connection->getQueryBuilder();
$query->execute(array(time(), $job->getId())); $query->update('jobs')
->set('last_run', $query->createNamedParameter(time(), \PDO::PARAM_INT))
->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), \PDO::PARAM_INT)));
$query->execute();
} }
} }

@ -15,26 +15,21 @@ use Test\TestCase;
* Class JobList * Class JobList
* *
* @group DB * @group DB
*
* @package Test\BackgroundJob * @package Test\BackgroundJob
*/ */
class JobList extends TestCase { class JobList extends TestCase {
/** /** @var \OC\BackgroundJob\JobList */
* @var \OC\BackgroundJob\JobList
*/
protected $instance; protected $instance;
/** /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
* @var \OCP\IConfig | \PHPUnit_Framework_MockObject_MockObject $config
*/
protected $config; protected $config;
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$conn = \OC::$server->getDatabaseConnection(); $connection = \OC::$server->getDatabaseConnection();
$this->config = $this->getMock('\OCP\IConfig'); $this->config = $this->getMock('\OCP\IConfig');
$this->instance = new \OC\BackgroundJob\JobList($conn, $this->config); $this->instance = new \OC\BackgroundJob\JobList($connection, $this->config);
} }
protected function getAllSorted() { protected function getAllSorted() {