[icinga-checkins] icinga.org: icinga-api/mhein/default: * added base for processing queries w / default values

git at icinga.org git at icinga.org
Tue Oct 13 12:20:11 CEST 2009


Module: icinga-api
Branch: mhein/default
Commit: 7f15c4f550762ebd9c9787a6e10d4fbd61d691f2
URL:    https://git.icinga.org/?p=icinga-api.git;a=commit;h=7f15c4f550762ebd9c9787a6e10d4fbd61d691f2

Author: Christian Doebler <christian.doebler at netways.de>
Date:   Sat Oct  3 19:27:51 2009 +0200

* added base for processing queries w/ default values
* modified query fields for use of default values

---

 objects/search/IcingaApiSearch.php    |   12 +++++--
 objects/search/IcingaApiSearchIdo.php |   53 +++++++++++++++++++++++++++++++--
 2 files changed, 59 insertions(+), 6 deletions(-)

diff --git a/objects/search/IcingaApiSearch.php b/objects/search/IcingaApiSearch.php
index 08589b1..458085d 100644
--- a/objects/search/IcingaApiSearch.php
+++ b/objects/search/IcingaApiSearch.php
@@ -305,9 +305,15 @@ abstract class IcingaApiSearch
 			$valid = false;
 		}
 
-		if ($this->resultColumns === false) {
-			throw new IcingaApiSearchException('No result column defined!');
-			$valid = false;
+		if ($valid) {
+
+			$query = $this->queryMap[$this->searchTarget];
+
+			if ($this->resultColumns === false && strpos($query, '${FIELDS:') === false) {
+				throw new IcingaApiSearchException('No result column defined!');
+				$valid = false;
+			}
+
 		}
 
 		return $valid;
diff --git a/objects/search/IcingaApiSearchIdo.php b/objects/search/IcingaApiSearchIdo.php
index 3b5b3b3..71afd89 100644
--- a/objects/search/IcingaApiSearchIdo.php
+++ b/objects/search/IcingaApiSearchIdo.php
@@ -225,7 +225,7 @@ class IcingaApiSearchIdo
 				${LIMIT}',
 		self::TARGET_HOST_PARENTS =>
 			'select
-				ohp.object_id parent_object_id, ohp.name1 parent_name, oh.object_id child_object_id, oh.name1 child_name
+				${FIELDS:ohp.object_id parent_object_id, ohp.name1 parent_name, oh.object_id child_object_id, oh.name1 child_name}
 			from
 				${TABLE_PREFIX}objects ohp
 			inner join ${TABLE_PREFIX}host_parenthosts hph on hph.parent_host_object_id = ohp.object_id
@@ -344,6 +344,53 @@ class IcingaApiSearchIdo
 	}
 
 	/**
+	 * replaces query variables by corresponding data
+	 * @param	string			$query			query template
+	 * @param	string			$variableName	name of query variable to replace by defined values
+	 * @return	string							processed query
+	 * @author	Christian Doebler <christian.doebler at netways.de>
+	 */
+	private function replaceQueryVariables ($query, $variableName) {
+
+		$variableValues = false;
+		$variableNamePrefix = '${' . $variableName;
+
+		switch ($variableName) {
+			case 'FIELDS':
+				if (!empty($this->resultColumns)) {
+					$variableValues = $this->applySearchType(implode(',', $this->resultColumns));
+				}
+				break;
+			case 'FILTER':
+				break;
+			case 'GROUPBY':
+				break;
+			case 'ORDERBY':
+				break;
+			case 'LIMIT':
+				break;
+		}
+
+		if ($variableValues === false && strpos($query, $variableNamePrefix . ':') !== false) {
+			$fieldPattern = '/\\' . $variableNamePrefix . ':([^}]+)}/';
+			preg_match_all($fieldPattern, $query, $fieldDefaultMatches);
+			if (count($fieldDefaultMatches) == 2) {
+				$variableName = $fieldDefaultMatches[0][0];
+				$variableValues = $fieldDefaultMatches[1][0];
+			} else {
+				$variableValues = null;				
+			}
+		} else {
+			$variableName = variableNamePrefix . '}';
+		}
+
+		$query = str_replace($variableName, $variableValues, $query);
+
+		return $query;
+
+	}
+
+	/**
 	 * creates and executes database query
 	 *
 	 * @param	void
@@ -358,8 +405,8 @@ class IcingaApiSearchIdo
 
 			// create base for query
 			$query = $this->queryMap[$this->searchTarget];
-			$fields = $this->applySearchType(implode(',', $this->resultColumns));
-			$query = str_replace('${FIELDS}', $fields, $query);
+
+			$query = $this->replaceQueryVariables($query, 'FIELDS');
 			$query = str_replace('${TABLE_PREFIX}', $this->tablePrefix, $query);
 
 			// add filter





More information about the icinga-checkins mailing list