Skip to content

Instantly share code, notes, and snippets.

@sousk
Created February 17, 2010 08:12
Show Gist options
  • Save sousk/306424 to your computer and use it in GitHub Desktop.
Save sousk/306424 to your computer and use it in GitHub Desktop.
a simple Q4M wrapper for Propel/PHP. note that this scripts has no care about query injection
<?php
/**
*
*/
class Q4M
{
public static $owned_tables = array();
public static $con = null;
const Q4M_SELECT = "select * from %s where queue_wait('%s')";
const Q4M_ABORT = "select queue_abort()";
const Q4M_END = "select queue_end()";
const Q4M_INSERT = "insert into %s (%s) values (%s)";
/**
* owner mode にスイッチし、job をフェッチする
*/
public static function select($table, $opt="")
{
$rs = self::execute(sprintf(self::Q4M_SELECT, $table, $table.$opt));
$rs->next();
return $rs->getRow();
}
public static function queue($table, $values)
{
$keys = array();
$vals = array();
foreach ($values as $k => $v) {
$keys[] = "`{$k}`";
$vals[] = "'{$v}'";
}
self::execute(sprintf(self::Q4M_INSERT,
$table, implode(",", $keys), implode(",", $vals)));
}
public static function abort()
{
if (! empty(self::$owned_tables)) {
self::execute(self::Q4M_ABORT);
return true;
}
return false;
}
public static function end()
{
if (! empty(self::$owned_tables)) {
self::execute(self::Q4M_END);
return true;
}
return false;
}
private static function execute($sql)
{
if (is_null(self::$con)) {
self::$con = Propel::getConnection();
}
return self::$con->executeQuery($sql, ResultSet::FETCHMODE_ASSOC);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment