dmBridge PHP API
UNLVRandomMySQLDataStore.php
00001 <?php
00002 #
00003 # UNLVRandom: a dmBridge module for random objects
00004 #
00005 # Copyright © 2011 Board of Regents of the Nevada System of Higher
00006 # Education, on behalf of the University of Nevada, Las Vegas
00007 #
00008 
00013 class UNLVRandomMySQLDataStore extends DMMySQLDataStore
00014 implements UNLVRandomDataStore {
00015 
00016    private static $instance;
00017 
00023    public static function getSetupSQL() {
00024       return array(
00025          "CREATE TABLE IF NOT EXISTS random_object (
00026             id int(10) unsigned NOT NULL auto_increment,
00027             alias varchar(30) NOT NULL,
00028             ptr int(10) unsigned NOT NULL,
00029             PRIMARY KEY (id)
00030          );"
00031       );
00032    }
00033 
00039    public static function getUpgradeSQL() {
00040       return array(
00041          // currently empty because there is no previous version from which
00042          // to upgrade.
00043       );
00044    }
00045 
00050    public static function getInstance() {
00051       if (!self::$instance instanceof self) {
00052          self::$instance = new self;
00053       }
00054       return self::$instance;
00055    }
00056 
00060    public function addRandomObject(DMObject $obj) {
00061       $sql = "INSERT INTO random_object(alias, ptr) VALUES(:alias, :ptr);";
00062       $params = array(
00063          ':alias' => $obj->getCollection()->getAlias(),
00064          ':ptr' => $obj->getPtr()
00065       );
00066       $this->write($sql, $params);
00067    }
00068 
00072    public function getRandomObject() {
00073       $sql = "SELECT alias, ptr FROM random_object ORDER BY RAND() LIMIT 1";
00074       $result = $this->read($sql, array());
00075 
00076       if (count($result)) {
00077          $collection = DMCollectionFactory::getCollection(
00078                $result[0]['alias']);
00079          $obj = DMObjectFactory::getObject($collection, $result[0]['ptr']);
00080          return $obj;
00081       }
00082       return null;
00083    }
00084 
00089    public function isRandomObject(DMObject $obj) {
00090       foreach ($this->getRandomObjects() as $ro) {
00091          if ($obj->equals($ro)) {
00092             return true;
00093          }
00094       }
00095       return false;
00096    }
00097 
00101    public function getRandomObjects() {
00102       $sql = "SELECT alias, ptr FROM random_object ORDER BY RAND()";
00103       $result = $this->read($sql, array());
00104 
00105       $objects = array();
00106       foreach ($result as $row) {
00107          try {
00108             $collection = DMCollectionFactory::getCollection($row['alias']);
00109             $objects[] = DMObjectFactory::getObject($collection, $row['ptr']);
00110          } catch (DMUnavailableModelException $e) {
00111             // nothing we can do; skip
00112          }
00113       }
00114       return $objects;
00115    }
00116 
00117 }
 All Data Structures Functions Variables