Forráskód Böngészése

add sql sort by param for Data_Source, used in WfsDataServer

Piotr Labudda 10 éve
szülő
commit
155b83ce82
2 módosított fájl, 63 hozzáadás és 29 törlés
  1. 12 5
      SE/se-lib/Api/WfsDataServer.php
  2. 51 24
      SE/se-lib/Data_Source.php

+ 12 - 5
SE/se-lib/Api/WfsDataServer.php

@@ -44,11 +44,14 @@ class Api_WfsDataServer extends Api_WfsServerBase {
 	public function getFeatureAction() {
 		$type = V::get('TYPENAME', '', $_REQUEST);
 		$typeEx = explode(':', $type);
-		$maxFeatures = V::get('MAXFEATURES', '10000', $_REQUEST, 'int');// TODO: Set Deafult Limit
+		$maxFeatures = '10000';// TODO: Set Deafult Limit
+		$maxFeatures = V::get('MAXFEATURES', $maxFeatures, $_REQUEST, 'int');
+		$maxFeatures = V::get('maxFeatures', $maxFeatures, $_REQUEST, 'int');
 		$ogcFilter = V::get('Filter', '', $_REQUEST);
+		$sortBy = V::get('sortBy', '', $_REQUEST);
 		$srsname = V::get('SRSNAME', '', $_REQUEST);// eg. EPSG:4326
 		if (count($typeEx) == 2) {
-			return $this->getFeatures($typeEx[0], $typeEx[1], $maxFeatures, $srsname, $ogcFilter);
+			return $this->getFeatures($typeEx[0], $typeEx[1], $maxFeatures, $srsname, $ogcFilter, $sortBy);
 		} else {
 			throw new HttpException("Wrong param TYPENAME", 400);
 		}
@@ -71,7 +74,7 @@ class Api_WfsDataServer extends Api_WfsServerBase {
 		}
 	}
 
-	public function getFeatures($nsPrefix, $type, $maxFeatures, $srsname, $ogcFilter = '') {
+	public function getFeatures($nsPrefix, $type, $maxFeatures, $srsname, $ogcFilter = '', $sortBy = '') {
 		$DBG = (V::get('DBG_GEO', '', $_GET) > 0);// TODO: Profiler
 		$typeName = "{$nsPrefix}:{$type}";
 		$acl = $this->getAclFromTypeName($typeName);
@@ -94,8 +97,12 @@ class Api_WfsDataServer extends Api_WfsServerBase {
 if($DBG){echo "ogcFilter(" . strlen($ogcFilter) . "): {$ogcFilter}\n";}
 		$searchParams = array();
 		$searchParams['limit'] = $maxFeatures;
-		$searchParams['order_by'] = $acl->getPrimaryKeyField();
-		$searchParams['order_dir'] = 'DESC';
+		if (!empty($sortBy)) {
+			$searchParams['sortBy'] = $sortBy;
+		} else {
+			$searchParams['order_by'] = $acl->getPrimaryKeyField();
+			$searchParams['order_dir'] = 'DESC';
+		}
 		if (strlen($ogcFilter) > 0) $searchParams['ogc:Filter'] = $ogcFilter;
 if($DBG){echo 'getItems:';print_r($searchParams);echo "\n";}
 		$items = $acl->getItems($searchParams);

+ 51 - 24
SE/se-lib/Data_Source.php

@@ -584,34 +584,61 @@ if(V::get('DBG_DS', 0, $_GET) > 0){echo'<pre style="max-height:200px;overflow:au
 	public function getItems($params = array()) {
 		$primaryKeyField = $this->getPrimaryKeyField();
 		$items = array();
-		$sql_limit = V::get('limit', $this->_default_sql_limit, $params, 'int');
-		$sql_offset = V::get('limitstart', 0, $params, 'int');
-		$sql_order_by = V::get('order_by', '', $params);
-		if ($sql_order_by) {
-			$sql_order_dir = V::get('order_dir', '', $params);
-
-			// prevent from sorting by special columns
-			if (!array_key_exists($sql_order_by, $this->_cols)) {
-				$sql_order_by = null;
-				$sql_order_dir = null;
+		$sql = new stdClass();
+		$sql->limit = V::get('limit', $this->_default_sql_limit, $params, 'int');
+		$sql->offset = V::get('limitstart', 0, $params, 'int');
+		$sql->orderBy = '';
+		$sql->_orderBy = V::get('order_by', '', $params);
+		$sql->_sortBy = V::get('sortBy', '', $params);
+		if ($sql->_sortBy) {// ID A,COL_X D,COL_Y A,...
+			$sql->_sortByList = array();
+			$sortByEx = explode(',', $sql->_sortBy);
+			foreach ($sortByEx as $sortPart) {
+				$sortPart = trim($sortPart);
+				if (empty($sortPart)) continue;
+				$sortPartEx = explode(' ', $sortPart);
+				if (count($sortPartEx) > 2) throw new Exception("SortBy parse error #" . __LINE__);
+				$sortColName = trim($sortPartEx[0]);
+				if (!array_key_exists($sortColName, $this->_cols)) throw new Exception("SortBy parse error - no column name '{$sortColName}' #" . __LINE__);
+				$colSortDir = 'ASC';
+				if (count($sortPartEx) == 2) {
+					if ('A' == $sortPartEx[1] || 'ASC' == $sortPartEx[1]) {
+					} else if ('D' == $sortPartEx[1] || 'DESC' == $sortPartEx[1]) {
+						$colSortDir = 'DESC';
+					} else throw new Exception("SortBy parse error - unknown sort order '{$sortPartEx[1]}' #" . __LINE__);
+				}
+				$sql->_sortByList[] = "t.`{$sortColName}` {$colSortDir}";
 			}
-		}
-		if ($sql_order_by) {
-			$sql_order_by = "order by t.`{$sql_order_by}`";
-			if ($sql_order_dir) {
-				$sql_order_by = "{$sql_order_by} {$sql_order_dir}";
+			if (!empty($sql->_sortByList)) {
+				$sql->orderBy = "order by " . implode(", ", $sql->_sortByList);
+			}
+		} else {
+			if ($sql->_orderBy) {
+				$sql->_orderDir = V::get('order_dir', '', $params);
+
+				// prevent from sorting by special columns
+				if (!array_key_exists($sql->_orderBy, $this->_cols)) {
+					$sql->_orderBy = null;
+					$sql->_orderDir = null;
+				}
+			}
+			if ($sql->_orderBy) {
+				$sql->orderBy = "order by t.`{$sql->_orderBy}`";
+				if ($sql->_orderDir) {
+					$sql->orderBy = "{$sql->orderBy} {$sql->_orderDir}";
+				}
 			}
 		}
-		$sql_cols = $this->_get_sql_cols();
-		$sql_where = $this->_parseSqlWhere($params);
-		$sql = "select {$sql_cols}
-			from {$this->_tbl} as t
-			where {$sql_where}
-			{$sql_order_by}
-			limit {$sql_limit} offset {$sql_offset}
+		$sql->cols = $this->_get_sql_cols();
+		$sql->where = $this->_parseSqlWhere($params);
+		$sql->query = "select {$sql->cols}
+			from `{$this->_tbl}` t
+			where {$sql->where}
+			{$sql->orderBy}
+			limit {$sql->limit} offset {$sql->offset}
 		";
-		if(V::get('DBG_DS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>'."\n\n";}
-		$res = $this->_db->query($sql);
+		DBG::_('DBG_DS', '>2', "sql", $sql, __CLASS__, __FUNCTION__, __LINE__);
+		$res = $this->_db->query($sql->query);
 		while ($r = $this->_db->fetch($res)) {
 			$items[$r->{$primaryKeyField}] = $r;
 		}