[icinga-checkins] icinga.org: icinga-api/mhein/default: * added interface from livestatus search to livestatus result

git at icinga.org git at icinga.org
Tue Jan 12 14:21:31 CET 2010


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

Author: Christian Doebler <christian.doebler at netways.de>
Date:   Tue Dec 22 14:20:47 2009 +0100

* added interface from livestatus search to livestatus result
* cleaned up a little

---

 objects/result/IcingaApiResultIdo.php        |   11 +--
 objects/result/IcingaApiResultLivestatus.php |  149 ++++++++++++++++++++++++++
 objects/search/IcingaApiSearchLivestatus.php |   22 ++--
 3 files changed, 160 insertions(+), 22 deletions(-)

diff --git a/objects/result/IcingaApiResultIdo.php b/objects/result/IcingaApiResultIdo.php
index 73e7852..8cadb96 100644
--- a/objects/result/IcingaApiResultIdo.php
+++ b/objects/result/IcingaApiResultIdo.php
@@ -37,7 +37,7 @@ class IcingaApiResultIdo
  		$this->numResults = $this->searchObject->rowCount();
 	}
 
-		/**
+	/**
 	 * sets next result row
 	 *
 	 * @param	void
@@ -48,7 +48,6 @@ class IcingaApiResultIdo
  		switch ($this->resultType) {
  			case self::RESULT_OBJECT:
 				$this->resultRow = $this->searchObject->fetchObject();
-				//$this->resultRow = $this->searchObject->fetch(PDO :: FETCH_CLASSTYPE);
 				if ($this->resultRow !== false) {
 					if ($this->offset === false) {
 						$this->offset = 0;
@@ -58,14 +57,6 @@ class IcingaApiResultIdo
 				} else {
 					$this->offset = false;
 				}
-//				if ($this->dbType == 'oci' && $this->resultRow !== false) {
-//					// if there's a better way to convert keys returned by
-//					// oracle to lower case, please let me know!
-//					foreach ($this->resultRow as $key => $value) {
-//						unset($this->resultRow->{$key});
-//						$this->resultRow->{strtolower($key)} = $value;
-//					}
-//				}
 				break;
  			case self::RESULT_ARRAY:
  				if ($this->resultArray === false) {
diff --git a/objects/result/IcingaApiResultLivestatus.php b/objects/result/IcingaApiResultLivestatus.php
new file mode 100644
index 0000000..cda126f
--- /dev/null
+++ b/objects/result/IcingaApiResultLivestatus.php
@@ -0,0 +1,149 @@
+<?php
+
+/**
+ * 
+ * @author Christian Doebler <christian.doebler at netways.de>
+ *
+ */
+class IcingaApiResultLivestatus
+	extends IcingaApiResult {
+
+	/*
+	 * VARIABLES
+	 */
+
+	private $searchColumns = array();
+
+	/*
+	 * METHODS
+	 */
+
+	/**
+	 * class constructor
+	 *
+	 * @param	void
+	 * @return	void
+	 * @author	Christian Doebler <christian.doebler at netways.de>
+	 */
+	public function __construct () {}
+
+	/**
+	 * sets search-column names
+	 *
+	 * @param	array		$columns			list of column names
+	 * @return	IcingaApiResultLivestatus		this object
+	 * @author	Christian Doebler <christian.doebler at netways.de>
+	 */
+	public function setSearchColumns (array $columns) {
+		$this->searchColumns = $columns;
+		$this->numResults = count($columns);
+ 		return $this;
+	}
+
+	/**
+	 * sets search result
+	 *
+	 * @param	array		$data				result of query
+	 * @return	IcingaApiResultLivestatus		this object
+	 * @author	Christian Doebler <christian.doebler at netways.de>
+	 */
+	public function setSearchResult (array $data) {
+		$this->resultArray = $data;
+ 		return $this;
+	}
+
+	/**
+	 * sets the search object
+	 *
+	 * @param	PDOStatement	$object			search object for further processing
+	 * @return	IcingaApiResultLivestatus		this object
+	 * @author	Christian Doebler <christian.doebler at netways.de>
+	 */
+	public function setSearchObject (&$object) {
+		$this->searchObject = $object;
+ 		return $this;
+	}
+
+	/**
+	 * sets next result row
+	 *
+	 * @param	void
+	 * @return	void
+	 * @author	Christian Doebler <christian.doebler at netways.de>
+	 */
+ 	public function next () {
+ 		switch ($this->resultType) {
+ 			case self::RESULT_OBJECT:
+				$this->resultRow = $this->searchObject->fetchObject();
+				if ($this->resultRow !== false) {
+					if ($this->offset === false) {
+						$this->offset = 0;
+					} else {
+						$this->offset++;
+					}
+				} else {
+					$this->offset = false;
+				}
+				break;
+ 			case self::RESULT_ARRAY:
+ 				if ($this->resultArray === false) {
+ 					$this->resultArray = $this->searchObject->fetchAll(PDO::FETCH_ASSOC);
+ 					if ($this->resultType == self::RESULT_ARRAY && $this->dbType == 'oci') {
+ 						$this->resultArray = array_change_key_case($this->resultArray, CASE_LOWER);
+ 					}
+ 				}
+				if ($this->offset === false) {
+					$this->offset = 0;
+				} else {
+					$this->offset++;
+				}
+				if ($this->offset >= $this->numResults) {
+					$this->offset = false;
+				}
+				if ($this->offset !== false) {
+					$this->resultRow = $this->resultArray[$this->offset];
+				} else {
+					$this->resultRow = false;
+				}
+ 				break;
+ 		}
+ 	}
+
+ 	/**
+ 	 * (non-PHPdoc)
+ 	 * @see objects/result/IcingaApiResult#rewind()
+ 	 */
+ 	public function rewind () {
+ 		switch ($this->resultType) {
+ 			case self::RESULT_OBJECT:
+ 				// TODO: implement rewind for objects
+		 		// throw new IcingaApiResultException('rewind() not supported by target type!');
+ 				break;
+ 			case self::RESULT_ARRAY:
+ 				$this->offset = false;
+ 				$this->next();
+ 				break;
+ 		}
+ 	}
+
+ 	/**
+ 	 * (non-PHPdoc)
+ 	 * @see objects/result/IcingaApiResult#getAll()
+ 	 */
+	public function getAll () {
+		$returnData = false;
+ 		switch ($this->resultType) {
+ 			case self::RESULT_OBJECT:
+ 				// TODO: implement getting complete result set for objects
+		 		throw new IcingaApiResultException('getAll() not supported by target type!');
+ 				break;
+ 			case self::RESULT_ARRAY:
+				$returnData = $this->resultArray;
+				break;
+ 		}
+ 		return $returnData;
+	}
+
+}
+
+?>
\ No newline at end of file
diff --git a/objects/search/IcingaApiSearchLivestatus.php b/objects/search/IcingaApiSearchLivestatus.php
index 673c676..8c280a4 100644
--- a/objects/search/IcingaApiSearchLivestatus.php
+++ b/objects/search/IcingaApiSearchLivestatus.php
@@ -81,7 +81,7 @@ class IcingaApiSearchLivestatus
 	}
 
 	/**
-	 * reads data from socket (inspired by NagVis' GlobalBackendmklivestatus and modified)
+	 * reads data from socket (inspired by NagVis' GlobalBackendmklivestatus)
 	 * @param	integer			$length			length of data to read
 	 * @return	string							read data; boolean false on error
 	 * @author	Christian Doebler <christian.doebler at netways.de>
@@ -250,7 +250,6 @@ class IcingaApiSearchLivestatus
 			$this->getQueryLimit()
 		);
 
-//var_dump($query);
 		return $query;
 	}
 
@@ -270,7 +269,7 @@ class IcingaApiSearchLivestatus
 	}
 
 	/**
-	 * queries livestatus socket and returns answer (inspired by NagVis' GlobalBackendmklivestatus and modified)
+	 * queries livestatus socket and returns answer (inspired by NagVis' GlobalBackendmklivestatus)
 	 * @param	string		$queryObject		livestatus object to query for
 	 * @return	
 	 * @author	Christian Doebler <christian.doebler at netways.de>
@@ -332,17 +331,16 @@ class IcingaApiSearchLivestatus
 	public function fetch () {
 		$object = false;
 
-//		$class = 'IcingaApiResultLivestatus';
-//		$object = new $class;
+		$class = 'IcingaApiResultLivestatus';
+		$object = new $class;
 
 		if (($data = $this->executeQuery()) !== false) {
-//var_dump($data);
-//			$object->setSearchObject($this->connectionObject->connectionStatement);
-//			$object->setDbType($this->connectionObject->getDbType());
-//			if ($this->resultType !== false) {
-//				$object->setResultType($this->resultType);
-//			}
-//			$object->next();
+			$object->setSearchColumns($this->resultColumnKeys);
+			$object->setSearchResult($data);
+			if ($this->resultType !== false) {
+				$object->setResultType($this->resultType);
+			}
+			$object->next();
 		}
 
 		parent::fetch();





More information about the icinga-checkins mailing list