dmBridge PHP API
UNLVSpatialMySQLDataStore.php
00001 <?php
00002 #
00003 # UNLVSpatial: a dmBridge module for spatial content
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 UNLVSpatialMySQLDataStore extends DMMySQLDataStore {
00014 
00015    private static $instance;
00016 
00022    public static function getSetupSQL() {
00023       return array(
00024          "CREATE TABLE IF NOT EXISTS spatial_object (
00025             id int(10) unsigned NOT NULL auto_increment,
00026             alias varchar(30) NOT NULL,
00027             ptr int(10) unsigned NOT NULL,
00028             lat float default NULL,
00029             `long` float default NULL,
00030             lat_s float default NULL,
00031             lat_n float default NULL,
00032             long_w float default NULL,
00033             long_e float default NULL,
00034             timestamp timestamp default CURRENT_TIMESTAMP,
00035             PRIMARY KEY (id)
00036          );"
00037       );
00038    }
00039 
00045    public static function getUpgradeSQL() {
00046       return array(
00047          // currently empty because there is no previous version from which
00048          // to upgrade.
00049       );
00050    }
00051 
00056    public static function getInstance() {
00057       if (!self::$instance instanceof self) {
00058          self::$instance = new self;
00059       }
00060       return self::$instance;
00061    }
00062 
00067    public function getNumObjectsInCollection($col) {
00068       $sql = UNLVSpatialGenericDataStore::getNumObjectsInCollectionSQL();
00069       $params = array(':alias' => $col->getAlias());
00070       $result = $this->read($sql, $params);
00071       return (int) $result[0]['count'];
00072    }
00073 
00083    public function getObject(DMCollection $col, $ptr) {
00084       $select = "SELECT lat, `long`, lat_n, lat_s, long_w, long_e ";
00085       $from = "FROM spatial_object ";
00086       $where = "WHERE alias = :alias AND ptr = :ptr ";
00087       $limit = "LIMIT 1";
00088       $sql = $select . $from . $where . $limit . ";";
00089 
00090       $params = array(
00091          ':alias' => $col->getAlias(),
00092          ':ptr' => $ptr
00093       );
00094 
00095       $result = $this->read($sql, $params);
00096 
00097       if (count($result)) {
00098          if ($result[0]['lat_n']) { // it's a box
00099             $sp_obj = new UNLVSpatialRectObject();
00100             $sp_obj->setSpatialDataSource("local");
00101             $sp_obj->setSpatialNorthLat($result[0]['lat_n']);
00102             $sp_obj->setSpatialSouthLat($result[0]['lat_s']);
00103             $sp_obj->setSpatialWestLong($result[0]['long_w']);
00104             $sp_obj->setSpatialEastLong($result[0]['long_e']);
00105             return $sp_obj;
00106          } else if ($result[0]['lat']) { // it's a point
00107             $sp_obj = new UNLVSpatialPointObject();
00108             $sp_obj->setSpatialDataSource("local");
00109             $sp_obj->setSpatialLat($result[0]['lat']);
00110             $sp_obj->setSpatialLong($result[0]['long']);
00111             return $sp_obj;
00112          }
00113       }
00114       return null;
00115    }
00116 
00120    public function deleteAllObjectsInCollection(DMCollection $col) {
00121       $params = array(
00122          ':alias' => $col->getAlias()
00123       );
00124       $sql = "DELETE FROM spatial_object WHERE alias = :alias";
00125       $this->write($sql, $params);
00126    }
00127 
00132    public function deleteObject(DMObject $obj) {
00133       $sql = "DELETE FROM spatial_object
00134          WHERE alias = :alias AND ptr = :ptr";
00135       $params = array(
00136          ':alias' => $obj->getCollection()->getAlias(),
00137          ':ptr' => $obj->getPtr()
00138       );
00139       $this->write($sql, $params);
00140    }
00141 
00146    public function updateObject(DMObject $obj) {
00147       // decide whether to insert or to update
00148       $sql = "SELECT COUNT(id) AS count
00149          FROM spatial_object
00150          WHERE alias = :alias AND ptr = :ptr";
00151       $params = array(
00152          ':alias' => $obj->getCollection()->getAlias(),
00153          ':ptr' => $obj->getPtr()
00154       );
00155       $result = $this->read($sql, $params);
00156 
00157       if ($result[0]['count'] > 0) { // update
00158          $sql = UNLVSpatialGenericDataStore::getUpdateObjectSQL();
00159       } else { // insert
00160          $sql = UNLVSpatialGenericDataStore::getInsertObjectSQL();
00161       }
00162 
00163       $params = array(
00164          ':alias' => $obj->getCollection()->getAlias(),
00165          ':ptr' => $obj->getPtr()
00166       );
00167       switch ($obj->getSpatialModelClassName()) {
00168       case "UNLVSpatialPointObject":
00169          $params[':lat'] = $obj->getSpatialLat();
00170          $params[':long'] = $obj->getSpatialLong();
00171          break;
00172       case "UNLVSpatialRectObject":
00173          $params[':lat_n'] = $obj->getSpatialNorthLat();
00174          $params[':lat_s'] = $obj->getSpatialSouthLat();
00175          $params[':long_w'] = $obj->getSpatialWestLong();
00176          $params[':long_e'] = $obj->getSpatialEastLong();
00177          break;
00178       }
00179 
00180       $this->write($sql, $params);
00181    }
00182 
00187    public function getNumObjectsMatchingQuery(UNLVSpatialQuery $query) {
00188       $sql = UNLVSpatialGenericDataStore::getCountSQL($query);
00189       $result = $this->read($sql, array());
00190       return $result[0]['count'];
00191    }
00192 
00197    public function getObjectsMatchingQuery(UNLVSpatialQuery $query) {
00198       $sql = UNLVSpatialGenericDataStore::getSQL($query);
00199       $objects = array();
00200       foreach ($this->read($sql, array()) as $result) {
00201          $collection = DMCollectionFactory::getCollection($result['alias']);
00202          $obj = DMObjectFactory::getObject($collection, $result['ptr']);
00203 
00204          if ($result['lat_n']) { // it's a rect
00205             $sp_obj = new UNLVSpatialRectObject();
00206             $sp_obj->setSpatialNorthLat($result['lat_n']);
00207             $sp_obj->setSpatialSouthLat($result['lat_s']);
00208             $sp_obj->setSpatialWestLong($result['long_w']);
00209             $sp_obj->setSpatialEastLong($result['long_e']);
00210             $obj->addAssociatedModel($sp_obj);
00211          } else { // it's a point
00212             $sp_obj = new UNLVSpatialPointObject();
00213             $sp_obj->setSpatialLat($result['lat']);
00214             $sp_obj->setSpatialLong($result['long']);
00215             $obj->addAssociatedModel($sp_obj);
00216          }
00217 
00218          $objects[] = $obj;
00219       }
00220       return $objects;
00221    }
00222 
00223 }
 All Data Structures Functions Variables