From fe6e6eaaf43148c3785995bdd7ec3eb24defd18d Mon Sep 17 00:00:00 2001 From: stubbfel Date: Wed, 10 Jul 2013 11:34:07 +0200 Subject: [PATCH] finish #69 --- geoapi/api/PisApi.php | 34 +++++++++-- geoapi/database/PisSqlManager.php | 26 ++++++-- geoapi/database/SqlManager.php | 35 ++++++++--- geoapi/service/pis/index.php | 12 +++- geoapi/utility/ArrayManager.php | 17 +++++- geoapi/utility/StringManager.php | 8 ++- map/btucscampus.qgs | 98 +++++++++++++++---------------- 7 files changed, 163 insertions(+), 67 deletions(-) diff --git a/geoapi/api/PisApi.php b/geoapi/api/PisApi.php index 9ce27d5..2f81ff2 100644 --- a/geoapi/api/PisApi.php +++ b/geoapi/api/PisApi.php @@ -15,11 +15,29 @@ require_once PATH_API . "/Api.php"; class PisApi extends Api { /** - * Route string for the alias paramter + * Route string for the pids paramter * @var string */ public static $routeParameterPids = "/pid/:pid+"; + /** + * Route string for the iNames paramter + * @var string + */ + public static $routeParameterINames = "/iname/:iname+"; + + /** + * Keyword for pidList arguments + * @var string + */ + public static $keyPidList = "pidList"; + + /** + * Keyword for iNameList arguments + * @var string + */ + public static $keyINameList = "iNameList"; + /** * max number of pid for each query * @var int @@ -48,14 +66,22 @@ class PisApi extends Api { * @return query result as xml */ public function sendPisQuery($queryArgs) { - $pidList = \utiliy\ArrayManager::removeEmptyItmes($queryArgs); + $pidList = \utiliy\ArrayManager::removeEmptyItmes($queryArgs[self::$keyPidList]); + + if (array_key_exists(self::$keyINameList, $queryArgs)) { + $iNameList = \utiliy\ArrayManager::removeEmptyItmes($queryArgs[self::$keyINameList]); + } else { + $iNameList = array(); + } + if (count($pidList) < self::$maxPid) { - $result = $this->sqlManager->sendPisQuery($pidList); - + $result = $this->sqlManager->sendPisQuery($pidList, $iNameList); + return $this->serialManager->arrayToPis($result); } return NULL; } + } ?> diff --git a/geoapi/database/PisSqlManager.php b/geoapi/database/PisSqlManager.php index 9f7d3f5..0744bc1 100644 --- a/geoapi/database/PisSqlManager.php +++ b/geoapi/database/PisSqlManager.php @@ -3,7 +3,7 @@ namespace database; include_once "../../global.inc.php"; -include_once PATH_UTILITTY . "/StringManager.php"; +include_once PATH_UTILITTY . "/ArrayManager.php"; require_once PATH_DATABASE . "/SqlManager.php"; /** @@ -50,6 +50,12 @@ class PisSqlManager extends SQLManager { */ private static $pidTerm = "pid = "; + /** + * String for the iName part of the query + * @var string + */ + private static $iNameTerm = "iName = "; + /** * Default-Constructor */ @@ -69,19 +75,31 @@ class PisSqlManager extends SQLManager { * @param array $queryArgs * @return array [num][assoc] */ - public function sendPisQuery($queryArgs) { + public function sendPisQuery($pidList, $iNameList) { // build query string $query = self::$selectTerm; - if (\utiliy\ArrayManager::validIntList($queryArgs)) { - $query .= \utiliy\ArrayManager::toSqlWhereString($queryArgs, self::$orTerm, self::$pidTerm) . self::$orderByTerm; + if (\utiliy\ArrayManager::validIntList($pidList)) { + $query .= self::$openBracket; + $query .= \utiliy\ArrayManager::toSqlWhereString($pidList, self::$orTerm, self::$pidTerm); + $query .= self::$closeBracket; } else { return null; } + + if (count($iNameList) > 0 && \utiliy\ArrayManager::validAlphaNumList($iNameList)) { + $query .= self::$andTerm; + $query .= self::$openBracket; + $query .= \utiliy\ArrayManager::toSqlWhereString($iNameList, self::$orTerm, self::$iNameTerm); + $query .= self::$closeBracket; + } + + $query .= self::$orderByTerm; // send query return $this->query($query); } + } ?> diff --git a/geoapi/database/SqlManager.php b/geoapi/database/SqlManager.php index ddaec35..9119624 100644 --- a/geoapi/database/SqlManager.php +++ b/geoapi/database/SqlManager.php @@ -60,6 +60,18 @@ abstract class SqlManager { */ protected static $quoteTerm = "\""; + /** + * String for open Bracket in a query + * @var string + */ + protected static $openBracket = "("; + + /** + * String for close Bracket in a query + * @var string + */ + protected static $closeBracket = ")"; + /** * Default-Constructor */ @@ -67,7 +79,9 @@ abstract class SqlManager { $this->serverAddress = \config\DBConfig::$sqlServer; $this->dbName = \config\DBConfig::$sqlDBName; $this->userName = \config\DBConfig::$sqlDBUser; - $this->userPw = \config\DBConfig::$sqlDBUserPW; + $this->userPw = \config\DBConfig::$sqlDBUserPW + + ; } /** @@ -82,18 +96,20 @@ abstract class SqlManager { unset($this->serverAddress); unset($this->dbName); unset($this->userName); - unset($this->userPW); + unset($this->userPW + + ); } /** * Method setup the connection to the Database */ public function connect() { - $this->link = mysql_connect($this->serverAddress, $this->userName, $this->userPw); + $this->link = mysql_connect($this->serverAddress , $this->userName , $this->userPw); if (!$this->link) { exit("No Connection: " . mysql_error()); } - $selected = mysql_select_db($this->dbName, $this->link); + $selected = mysql_select_db($this->dbName , $this->link); if (!$selected) { exit("No DB: " . mysql_error()); } @@ -103,7 +119,9 @@ abstract class SqlManager { * Method close the connection */ public function closeConnection() { - if ($this->link) { + if ( + + $this->link) { mysql_close($this->link); unset($this->link); $this->link = null; @@ -113,20 +131,23 @@ abstract class SqlManager { /** * Method send a query to the Datebase and return the result * @param string $query + + * @return result[num][assoc] */ protected function query($query) { // send error - $mysqlResult = mysql_query($query, $this->link); + $mysqlResult = mysql_query($query, $this->link ); if (!$mysqlResult) { + // echo $query; exit("Query error: " . mysql_error()); } // fetch result $rowNums = mysql_num_rows($mysqlResult); $result = array(); - for ($i = 0; $i < $rowNums; $i++) { + for ($i = 0; $i < $rowNums; $i++) { $row = mysql_fetch_assoc($mysqlResult); $result[$i] = $row; } diff --git a/geoapi/service/pis/index.php b/geoapi/service/pis/index.php index bd7d7bc..d9e3e17 100644 --- a/geoapi/service/pis/index.php +++ b/geoapi/service/pis/index.php @@ -2,6 +2,7 @@ include_once "../../global.inc.php"; require_once PATH_API . "/PisApi.php"; +include_once PATH_UTILITTY . "/ArrayManager.php"; // get reguest header $headers = apache_request_headers(); @@ -9,8 +10,17 @@ $headers = apache_request_headers(); $app = new \api\PisApi($headers); // HTTP-Get-Method +$app->get(\api\PisApi::$routeParameterPids . \api\PisApi::$routeParameterINames, function ($pid, $iNames = array()) use ($app) { + $queryArgs = array(); + $queryArgs[\api\PisApi::$keyPidList] = $pid; + $queryArgs[\api\PisApi::$keyINameList] = $iNames; + echo $app->sendPisQuery($queryArgs); + }); + $app->get(\api\PisApi::$routeParameterPids, function ($pid) use ($app) { - echo $app->sendPisQuery($pid); + $queryArgs = array(); + $queryArgs[\api\PisApi::$keyPidList] = $pid; + echo $app->sendPisQuery($queryArgs); }); $app->run(); diff --git a/geoapi/utility/ArrayManager.php b/geoapi/utility/ArrayManager.php index dd25289..ff76b62 100644 --- a/geoapi/utility/ArrayManager.php +++ b/geoapi/utility/ArrayManager.php @@ -34,7 +34,7 @@ class ArrayManager { public static function toSqlWhereString($array, $operator = "", $fieldname = "") { $arrayStr = StringManager::$emptyString; foreach ($array as $value) { - $arrayStr .= $fieldname . $value . $operator; + $arrayStr .= $fieldname . StringManager::$quotes . $value . StringManager::$quotes . $operator; } $result = substr($arrayStr, 0, strlen($arrayStr) - strlen($operator)); return $result; @@ -55,6 +55,21 @@ class ArrayManager { return TRUE; } + /** + * Method check if all items of the array are only digits and < PHP_INT_MAX + * @param array $poly + * @return boolean + */ + public static function validAlphaNumList($list) { + foreach ($list as $value) { + + if (!ctype_alnum($value) || !\utiliy\StringManager::validSQLString($value)) { + return FALSE; + } + } + return TRUE; + } + } ?> diff --git a/geoapi/utility/StringManager.php b/geoapi/utility/StringManager.php index 1312e1c..a424cb8 100644 --- a/geoapi/utility/StringManager.php +++ b/geoapi/utility/StringManager.php @@ -12,10 +12,16 @@ include_once "../../global.inc.php"; class StringManager { /** - * A Constant vor an emptystring like "" + * A Constant for an emptystring like "" * @var string */ public static $emptyString = ""; + + /** + * A Constant for an quotechars like " + * @var string + */ + public static $quotes = "\""; /** * Method check if a certain string start with a certain substring diff --git a/map/btucscampus.qgs b/map/btucscampus.qgs index 9266baa..9a0a2bb 100644 --- a/map/btucscampus.qgs +++ b/map/btucscampus.qgs @@ -4,10 +4,10 @@ degrees - 14.101601 - 51.518800 - 14.676288 - 51.809110 + 14.191382 + 51.659713 + 14.478726 + 51.804868 0 @@ -49,29 +49,29 @@ - + - + - + - - - - + + + + @@ -119,7 +119,7 @@ - + name - + . - + . @@ -184,7 +184,7 @@ - + name - + . - + . @@ -251,7 +251,7 @@ - + name - + . - + . @@ -316,7 +316,7 @@ - + name - + . - + . @@ -383,7 +383,7 @@ - + name - + . - + . @@ -448,7 +448,7 @@ - + name - + . - + . @@ -515,7 +515,7 @@ - + name - + . - + . @@ -580,7 +580,7 @@ - + name - + . - + . @@ -647,7 +647,7 @@ - + name - + . - + . @@ -739,9 +739,9 @@ - + . - + . @@ -795,7 +795,7 @@ - + name - + . - + .