[icinga-checkins] icinga.org: icinga-api/cdoebler/default: * modified base for pgsql

git at icinga.org git at icinga.org
Fri Jan 22 13:56:27 CET 2010


Module: icinga-api
Branch: cdoebler/default
Commit: 3e21e445b4e726a061827e21a718fbd9f9d4da68
URL:    https://git.icinga.org/?p=icinga-api.git;a=commit;h=3e21e445b4e726a061827e21a718fbd9f9d4da68

Author: Christian Doebler <christian.doebler at netways.de>
Date:   Fri Jan 22 13:55:40 2010 +0100

* modified base for pgsql
* altered limit statement for pgsql

---

 objects/search/IcingaApiSearch.php                 |    9 +++
 objects/search/IcingaApiSearchIdo.php              |   52 ++++++++++++++-----
 .../data_interfaces/IcingaApiSearchIdoPgsql.php    |   27 ++++++-----
 3 files changed, 62 insertions(+), 26 deletions(-)

diff --git a/objects/search/IcingaApiSearch.php b/objects/search/IcingaApiSearch.php
index f51fb50..7ac3c8d 100644
--- a/objects/search/IcingaApiSearch.php
+++ b/objects/search/IcingaApiSearch.php
@@ -34,6 +34,8 @@ abstract class IcingaApiSearch
 	protected $icingaType = null;
 	protected $columns = false;
 
+	protected $columnWrap = false;
+
 	/*
 	 * METHODS
 	 */
@@ -319,6 +321,13 @@ abstract class IcingaApiSearch
 			$classSuffix = ucfirst(strtolower($dbType));
 			$class = 'IcingaApiSearch' . $sourceType . $classSuffix;
 			$this->ifSettings = new $class;
+
+			if (array_key_exists('columnWrap', $this->ifSettings->statements)) {
+				$this->columnWrap = $this->ifSettings->statements['columnWrap'];
+			} else {
+				$this->columnWrap = false;
+			}
+
 			$success = true;
 		}
 
diff --git a/objects/search/IcingaApiSearchIdo.php b/objects/search/IcingaApiSearchIdo.php
index 8b7ff53..c6e0612 100644
--- a/objects/search/IcingaApiSearchIdo.php
+++ b/objects/search/IcingaApiSearchIdo.php
@@ -12,7 +12,7 @@ class IcingaApiSearchIdo
 	 * VARIABLES
 	 */
 
-	
+
 
 	/*
 	 * METHODS
@@ -138,13 +138,22 @@ class IcingaApiSearchIdo
 	 */
 	private function applySearchType ($fields) {
 		switch ($this->searchType) {
-			case IcingaApi::SEARCH_TYPE_COUNT:
+			case self::SEARCH_TYPE_COUNT:
 				$fieldsArray = explode(' ', $fields);
 				$countFieldsArray = count($fieldsArray);
 				if ($countFieldsArray > 2) {
 					throw new IcingaApiSearchException('applySearchType(): invalid number of columns for use of \'count\'!');
 				} else {
-					$suffix = 'COUNT';
+					preg_match('/^([^A-Z]+)/', $fieldsArray[1], $matches);
+					if (count($matches) && count($matches[0])) {
+						$suffix = $matches[0][0];
+						$fieldsArray[1] = substr($fieldsArray[1], strlen($suffix));
+					} else {
+						$suffix = null;
+					}
+
+					$suffix .= 'COUNT';
+
 					if ($countFieldsArray == 2) {
 						$suffix .= '_' . $fieldsArray[1];
 					}
@@ -208,19 +217,15 @@ class IcingaApiSearchIdo
 			$columns = array($columns);
 		}
 
-		if (array_key_exists('columnWrap', $this->ifSettings->statements)) {
-			$columnWrap = $this->ifSettings->statements['columnWrap'];
-		} else {
-			$columnWrap = false;
-		}
-
 		foreach ($columns as $currentColumn) {
 			if (($processedColumn = $this->getColumn($currentColumn)) !== false) {
 				array_push($this->resultColumnKeys, $currentColumn);
 				array_push($this->resultColumnsNoAliases, $processedColumn);
 
-				if ($columnWrap !== false) {
-					$currentColumn = sprintf($columnWrap, $currentColumn);
+				if ($this->columnWrap !== false) {
+					$currentColumn = sprintf(
+						$this->columnWrap, $currentColumn
+					);
 				}
 
 				$processedColumn .= ' ' . $currentColumn;
@@ -252,7 +257,7 @@ class IcingaApiSearchIdo
 		}
 
 		$loopCounter = 0;
-		while ($loopCounter < 2 && $variableValues == false) {
+		while ($loopCounter < 2 && $variableValues === false) {
 			if ($loopCounter && count($fieldDefaultMatches)) {
 				$variableValues = $fieldDefaultMatches[1][0];
 			}
@@ -268,6 +273,7 @@ class IcingaApiSearchIdo
 						}
 					} else {
 						// add default values to joins
+						$variableValuesArr = array();
 						$valuesLong = explode(',', $variableValues);
 						foreach ($valuesLong as $currentValueLong) {
 							$valueShort = explode(' ', trim($currentValueLong));
@@ -275,6 +281,17 @@ class IcingaApiSearchIdo
 							if (!in_array($table, $this->joinTables)) {
 								array_push($this->joinTables, $table);
 							}
+
+							if ($this->columnWrap !== false) {
+								$alias = sprintf($this->columnWrap, $valueShort[1]);
+								$columnStr = sprintf (
+									'%s.%s %s', $table, $column, $alias
+								);
+								array_push($variableValuesArr, $columnStr);
+							}
+						}
+						if ($this->columnWrap !== false) {
+							$variableValues = implode(',', $variableValuesArr);
 						}
 					}
 					break;
@@ -310,7 +327,11 @@ class IcingaApiSearchIdo
 						list($variableValuesTemplate, $variableValues) =
 							$this->ifSettings->createQueryLimit($this->searchLimit);
 					} elseif ($variableValues !== false) {
-						$this->searchLimit = explode(',', $variableValues);
+						if (!is_array($variableValues)) {
+							$this->searchLimit = explode(',', $variableValues);
+						} else {
+							$this->searchLimit = $variableValues;
+						}
 					}
 					break;
 			}
@@ -326,7 +347,10 @@ class IcingaApiSearchIdo
 					$variableValues = $variableValuesPrefix . $variableValues;
 				}
 				if ($variableValuesTemplate !== false) {
-					$variableValues = sprintf (
+					if (!is_array($variableValues)) {
+						$variableValues = array($variableValues);
+					}
+					$variableValues = vsprintf (
 						$variableValuesTemplate,
 						$variableValues
 					);
diff --git a/objects/search/data_interfaces/IcingaApiSearchIdoPgsql.php b/objects/search/data_interfaces/IcingaApiSearchIdoPgsql.php
index 9a07a19..880c0be 100644
--- a/objects/search/data_interfaces/IcingaApiSearchIdoPgsql.php
+++ b/objects/search/data_interfaces/IcingaApiSearchIdoPgsql.php
@@ -23,7 +23,7 @@ class IcingaApiSearchIdoPgsql
 		'fieldsSuffix'	=> false,
 		'group'			=> ' group by %s ',
 		'order'			=> ' order by ',
-		'limit'			=> ' limit %s '
+		'limit'			=> ' offset %s limit %s '
 	);
 
 	public $queryMap = array (
@@ -143,7 +143,7 @@ class IcingaApiSearchIdoPgsql
 			from 
 				${TABLE_PREFIX}customvariables cv
 			${if_table:cvs:inner join ${TABLE_PREFIX}customvariablestatus cvs on cvs.object_id = cv.object_id}
-			where 1
+			where true
 			${FILTER}
 			${GROUPBY}
 			${ORDERBY}
@@ -153,7 +153,7 @@ class IcingaApiSearchIdoPgsql
 				${FIELDS}
 			from
 				${TABLE_PREFIX}configfilevariables cfv
-			where 1
+			where true
 			${FILTER}
 			${GROUPBY}
 			${ORDERBY}
@@ -174,7 +174,7 @@ class IcingaApiSearchIdoPgsql
 				${FIELDS}
 			from
 				${TABLE_PREFIX}logentries le
-			where 1 
+			where true 
 			${FILTER}
 			${GROUPBY}
 			${ORDERBY}
@@ -197,7 +197,7 @@ class IcingaApiSearchIdoPgsql
 			${if_table:ohg,hg,hgm,oh:inner join ${TABLE_PREFIX}objects ohg on ohg.object_id = hg.hostgroup_object_id}
 			${if_table:cvsh,oh:inner join ${TABLE_PREFIX}customvariablestatus cvsh on oh.object_id = cvsh.object_id}
 			${if_table:cvsc,oc,cgm,cg,hcg,h,oh:-- inner join ${TABLE_PREFIX}customvariablestatus cvsc on oc.object_id = cvsc.object_id}
-			where 1
+			where true
 			${FILTER}
 			group by
 				hs.current_state
@@ -224,7 +224,7 @@ class IcingaApiSearchIdoPgsql
 			${if_table:cvsh,oh,s,os:inner join ${TABLE_PREFIX}customvariablestatus cvsh on oh.object_id = cvsh.object_id}
 			${if_table:cvss,os:inner join ${TABLE_PREFIX}customvariablestatus cvss on os.object_id = cvss.object_id}
 			${if_table:cvsc,oc,cgm,cg,scg,s,os:-- inner join ${TABLE_PREFIX}customvariablestatus cvsc on oc.object_id = cvsc.object_id}
-			where 1
+			where true
 			${FILTER}
 			group by
 				ss.current_state
@@ -241,7 +241,7 @@ class IcingaApiSearchIdoPgsql
 			${if_table:hg,hgm,oh:inner join ${TABLE_PREFIX}hostgroups hg on hg.hostgroup_id = hgm.hostgroup_id}
 			${if_table:ohg,hg,hgm,oh:inner join ${TABLE_PREFIX}objects ohg on ohg.object_id = hg.hostgroup_object_id}
 			${if_table:cvsh,oh:inner join ${TABLE_PREFIX}customvariablestatus cvsh on oh.object_id = cvsh.object_id}
-			where 1
+			where true
 			${FILTER}
 			${GROUPBY}
 			${ORDERBY}
@@ -260,7 +260,7 @@ class IcingaApiSearchIdoPgsql
 			${if_table:ohg,hg,hgm,oh,s,os:inner join ${TABLE_PREFIX}objects ohg on ohg.object_id = hg.hostgroup_object_id}
 			${if_table:cvsh,oh,s,os:inner join ${TABLE_PREFIX}customvariablestatus cvsh on oh.object_id = cvsh.object_id}
 			${if_table:cvss,os:inner join ${TABLE_PREFIX}customvariablestatus cvss on os.object_id = cvss.object_id}
-			where 1
+			where true
 			${FILTER}
 			${GROUPBY}
 			${ORDERBY}
@@ -289,7 +289,7 @@ class IcingaApiSearchIdoPgsql
 			${if_table:h,s,on:left join ${TABLE_PREFIX}hosts h on h.host_object_id = on.object_id or h.host_object_id = s.host_object_id}
 			${if_table:oh,h,s,on:left join ${TABLE_PREFIX}objects oh on oh.object_id = h.host_object_id}
 			${if_table:os,s,on:left join ${TABLE_PREFIX}objects os on os.object_id = s.service_object_id}
-			where 1
+			where true
 			${FILTER}
 			${GROUPBY}
 			${ORDERBY:n.start_time asc}
@@ -303,7 +303,7 @@ class IcingaApiSearchIdoPgsql
 			${if_table:hgm:inner join ${TABLE_PREFIX}hostgroup_members hgm on hgm.hostgroup_id = hg.hostgroup_id}
 			${if_table:oh,hgm:inner join ${TABLE_PREFIX}objects oh on oh.object_id = hgm.host_object_id}
 			${if_table:hs,oh,hgm:inner join ${TABLE_PREFIX}hoststatus hs on hs.host_object_id = oh.object_id}
-			where 1
+			where true
 			${FILTER}
 			${GROUPBY}
 			${ORDERBY:hs.current_state asc}
@@ -317,7 +317,7 @@ class IcingaApiSearchIdoPgsql
 			${if_table:sgm:inner join ${TABLE_PREFIX}servicegroup_members sgm on sgm.servicegroup_id = sg.servicegroup_id}
 			${if_table:os,sgm:inner join ${TABLE_PREFIX}objects os on os.object_id = sgm.service_object_id}
 			${if_table:ss,os,sgm:inner join ${TABLE_PREFIX}servicestatus ss on ss.service_object_id = os.object_id}
-			where 1
+			where true
 			${FILTER}
 			${GROUPBY}
 			${ORDERBY:ss.current_state asc}
@@ -561,7 +561,10 @@ class IcingaApiSearchIdoPgsql
 		$returnValue = array($this->statements['limit']);
 
 		if ($searchLimit !== false) {
-			array_push($returnValue, implode(',', $searchLimit));
+			if (count($searchLimit) == 1) {
+				$searchLimit = array(0, $searchLimit[0]);
+			}
+			array_push($returnValue, $searchLimit);
 		} else {
 			array_push($returnValue, false);
 		}





More information about the icinga-checkins mailing list