Skip to content

Commit

Permalink
add more to the mysql persistence layer. still kinda sucks but its ok…
Browse files Browse the repository at this point in the history
…... people can write their own
  • Loading branch information
Arin Sarkissian authored and Arin Sarkissian committed Mar 25, 2009
1 parent fce9488 commit 0e10513
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 10 deletions.
7 changes: 7 additions & 0 deletions inc/Resqee/Persistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public abstract function completeJob($jobId, Resqee_Response $response);
*/
public abstract function findJobs(Resqee_Persistence_SearchParams $params);

/**
* Remove a job from the queue
*
* @param unknown_type $jobId
*/
public abstract function dequeue($jobId);

}

?>
64 changes: 54 additions & 10 deletions inc/Resqee/Persistence/MySQL.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

require_once 'Resqee/Persistence.php';
require_once 'Resqee/Persistence/Item.php';
require_once 'Resqee/Persistence/SearchParams.php';

class Resqee_Persistence_MySQL extends Resqee_Persistence_Item
{
Expand All @@ -11,14 +13,6 @@ class Resqee_Persistence_MySQL extends Resqee_Persistence_Item
*/
private static $db = null;

/**
* Constructor
*
*/
public function __construct()
{
}

/**
* Get a MySQL DB resource
*
Expand Down Expand Up @@ -146,8 +140,10 @@ public function getJobClassId($className)
public function dequeue($jobId)
{
$db = self::getDb();
$sql = 'DELETE FROM queuedJobs WHERE jobId = '
. mysql_real_escape_string($jobId, $db);
$sql = 'DELETE
FROM jobs
WHERE jobId = ' . mysql_real_escape_string($jobId, $db) .
' AND status = ' . Resqee_Persistence_Item::STATUS_QUEUED;

if (! mysql_query($sql, $db)) {
throw new Resqee_Exception_Persistence(
Expand All @@ -159,9 +155,57 @@ public function dequeue($jobId)
return true;
}

/**
* Find jobs based on the $params that where passed in.
*
* This is a pretty naive & crappy function... just like the rest of this class
*
* @param Resqee_Persistence_SearchParams $params
*
* @return whatever
*/
public function findJobs(Resqee_Persistence_SearchParams $params)
{
$db = self::getDb();
$where = array();
$fields = get_object_vars($params);

foreach ($fields as $field => $value) {
if ($value == null || $field == 'offset' || $field == 'limit') {
continue;
} else if (preg_match('/\W/', $field)) {
throw new Resqee_Exception_Persistence(
"Could not perform search: {$field} is not a value column name"
);
}

$matches = array();
if (preg_match('/^(min|max)(Request|Response)Time$/', $field, $matches)) {
$value = (int) $value;
if ($matches[1] == 'min') {
$where[] = strtolower($matches[2]) . " >= FROM_UNIXTIME($value) ";
} else {
$where[] = strtolower($matches[2]) . " <= FROM_UNIXTIME($value) ";
}
} else {
$where[] = (is_numeric($value))
? "$field = $value"
: "$field = " . mysql_real_escape_string($value, $db);
}
}

$sql = 'SELECT * FROM jobs where ' . implode(' AND ', $where);
$countSql = 'SELECT count(*) FROM jobs where ' . implode(' AND ', $where);

if (isset($params->limit, $params->offset)) {
$sql .= ' LIMIT ' . (int) $params->limit
. ' OFFSET ' . (int) $params->offset;
} else if (isset($params->limit)) {
$sql .= ' LIMIT ' . (int) $params->limit;
} else if (isset($params->offset)) {
$sql .= ' LIMIT ' . Resqee_Persistence_SearchParams::MAX_RESULTS
. ' OFFSET ' . (int) $params->offset;
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions inc/Resqee/Persistence/SearchParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

class Resqee_Persistence_SearchParams
{
/**
* Maximum # of results to fetch at a time
*
* Huge number
*
* @var int
*/
const MAX_RESULTS = 1000000;

/**
* Error state to query on.
*
Expand Down

0 comments on commit 0e10513

Please sign in to comment.