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          ':lat' => null,
00167          ':long' => null,
00168          ':lat_n' => null,
00169          ':lat_s' => null,
00170          ':long_w' => null,
00171          ':long_e' => null
00172       );
00173       switch ($obj->getSpatialModelClassName()) {
00174       case "UNLVSpatialPointObject":
00175          $params[':lat'] = $obj->getSpatialLat();
00176          $params[':long'] = $obj->getSpatialLong();
00177          break;
00178       case "UNLVSpatialRectObject":
00179          $params[':lat_n'] = $obj->getSpatialNorthLat();
00180          $params[':lat_s'] = $obj->getSpatialSouthLat();
00181          $params[':long_w'] = $obj->getSpatialWestLong();
00182          $params[':long_e'] = $obj->getSpatialEastLong();
00183          break;
00184       }
00185 
00186       $this->write($sql, $params);
00187    }
00188 
00193    public function getNumObjectsMatchingQuery(UNLVSpatialQuery $query) {
00194       $sql = UNLVSpatialGenericDataStore::getCountSQL($query);
00195       $result = $this->read($sql, array());
00196       return $result[0]['count'];
00197    }
00198 
00203    public function getObjectsMatchingQuery(UNLVSpatialQuery $query) {
00204       $sql = UNLVSpatialGenericDataStore::getSQL($query);
00205       $objects = array();
00206       foreach ($this->read($sql, array()) as $result) {
00207          $collection = DMCollectionFactory::getCollection($result['alias']);
00208          $obj = DMObjectFactory::getObject($collection, $result['ptr']);
00209 
00210          if ($result['lat_n']) { // it's a rect
00211             $sp_obj = new UNLVSpatialRectObject();
00212             $sp_obj->setSpatialNorthLat($result['lat_n']);
00213             $sp_obj->setSpatialSouthLat($result['lat_s']);
00214             $sp_obj->setSpatialWestLong($result['long_w']);
00215             $sp_obj->setSpatialEastLong($result['long_e']);
00216             $obj->addAssociatedModel($sp_obj);
00217          } else { // it's a point
00218             $sp_obj = new UNLVSpatialPointObject();
00219             $sp_obj->setSpatialLat($result['lat']);
00220             $sp_obj->setSpatialLong($result['long']);
00221             $obj->addAssociatedModel($sp_obj);
00222          }
00223 
00224          $objects[] = $obj;
00225       }
00226       return $objects;
00227    }
00228 
00229 }
 All Data Structures Functions Variables