1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135 |
- <?php
- // Copyright (C) 2010 Combodo SARL
- //
- // This program is free software; you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation; version 3 of the License.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program; if not, write to the Free Software
- // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- /**
- * General definition of an expression tree (could be OQL, SQL or whatever)
- *
- * @author Erwan Taloc <erwan.taloc@combodo.com>
- * @author Romain Quetiez <romain.quetiez@combodo.com>
- * @author Denis Flaven <denis.flaven@combodo.com>
- * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
- */
- class MissingQueryArgument extends CoreException
- {
- }
- abstract class Expression
- {
- // recursive translation of identifiers
- abstract public function GetUnresolvedFields($sAlias, &$aUnresolved);
- abstract public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true);
- // recursive rendering (aArgs used as input by default, or used as output if bRetrofitParams set to True
- abstract public function Render(&$aArgs = null, $bRetrofitParams = false);
- abstract public function ApplyParameters($aArgs);
-
- // recursively builds an array of class => fieldname
- abstract public function ListRequiredFields();
- abstract public function IsTrue();
-
- // recursively builds an array of [classAlias][fieldName] => value
- abstract public function ListConstantFields();
-
- public function RequiresField($sClass, $sFieldName)
- {
- // #@# todo - optimize : this is called quite often when building a single query !
- $aRequired = $this->ListRequiredFields();
- if (!in_array($sClass.'.'.$sFieldName, $aRequired)) return false;
- return true;
- }
- public function serialize()
- {
- return base64_encode($this->Render());
- }
-
- static public function unserialize($sValue)
- {
- return self::FromOQL(base64_decode($sValue));
- }
- static public function FromOQL($sConditionExpr)
- {
- $oOql = new OqlInterpreter($sConditionExpr);
- $oExpression = $oOql->ParseExpression();
-
- return $oExpression;
- }
- static public function FromSQL($sSQL)
- {
- $oSql = new SQLExpression($sSQL);
- return $oSql;
- }
- public function LogAnd($oExpr)
- {
- if ($this->IsTrue()) return clone $oExpr;
- if ($oExpr->IsTrue()) return clone $this;
- return new BinaryExpression($this, 'AND', $oExpr);
- }
- public function LogOr($oExpr)
- {
- return new BinaryExpression($this, 'OR', $oExpr);
- }
-
- abstract public function RenameParam($sOldName, $sNewName);
- /**
- * Make the most relevant label, given the value of the expression
- *
- * @param DBObjectSearch oFilter The context in which this expression has been used
- * @param string sValue The value returned by the query, for this expression
- * @param string sDefault The default value if no relevant label could be computed
- * @return The label
- */
- public function MakeValueLabel($oFilter, $sValue, $sDefault)
- {
- return $sDefault;
- }
- }
- class SQLExpression extends Expression
- {
- protected $m_sSQL;
- public function __construct($sSQL)
- {
- $this->m_sSQL = $sSQL;
- }
- public function IsTrue()
- {
- return false;
- }
- // recursive rendering
- public function Render(&$aArgs = null, $bRetrofitParams = false)
- {
- return $this->m_sSQL;
- }
- public function ApplyParameters($aArgs)
- {
- }
-
- public function GetUnresolvedFields($sAlias, &$aUnresolved)
- {
- }
- public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
- {
- return clone $this;
- }
- public function ListRequiredFields()
- {
- return array();
- }
-
- public function ListConstantFields()
- {
- return array();
- }
-
- public function RenameParam($sOldName, $sNewName)
- {
- // Do nothing, since there is nothing to rename
- }
- }
- class BinaryExpression extends Expression
- {
- protected $m_oLeftExpr; // filter code or an SQL expression (later?)
- protected $m_oRightExpr;
- protected $m_sOperator;
- public function __construct($oLeftExpr, $sOperator, $oRightExpr)
- {
- if (!is_object($oLeftExpr))
- {
- throw new CoreException('Expecting an Expression object on the left hand', array('found_type' => gettype($oLeftExpr)));
- }
- if (!is_object($oRightExpr))
- {
- throw new CoreException('Expecting an Expression object on the right hand', array('found_type' => gettype($oRightExpr)));
- }
- if (!$oLeftExpr instanceof Expression)
- {
- throw new CoreException('Expecting an Expression object on the left hand', array('found_class' => get_class($oLeftExpr)));
- }
- if (!$oRightExpr instanceof Expression)
- {
- throw new CoreException('Expecting an Expression object on the right hand', array('found_class' => get_class($oRightExpr)));
- }
- $this->m_oLeftExpr = $oLeftExpr;
- $this->m_oRightExpr = $oRightExpr;
- $this->m_sOperator = $sOperator;
- }
- public function IsTrue()
- {
- // return true if we are certain that it will be true
- if ($this->m_sOperator == 'AND')
- {
- if ($this->m_oLeftExpr->IsTrue() && $this->m_oRightExpr->IsTrue()) return true;
- }
- return false;
- }
-
- public function GetLeftExpr()
- {
- return $this->m_oLeftExpr;
- }
- public function GetRightExpr()
- {
- return $this->m_oRightExpr;
- }
- public function GetOperator()
- {
- return $this->m_sOperator;
- }
- // recursive rendering
- public function Render(&$aArgs = null, $bRetrofitParams = false)
- {
- $sOperator = $this->GetOperator();
- $sLeft = $this->GetLeftExpr()->Render($aArgs, $bRetrofitParams);
- $sRight = $this->GetRightExpr()->Render($aArgs, $bRetrofitParams);
- return "($sLeft $sOperator $sRight)";
- }
-
- public function ApplyParameters($aArgs)
- {
- if ($this->m_oLeftExpr instanceof VariableExpression)
- {
- $this->m_oLeftExpr = $this->m_oLeftExpr->GetAsScalar($aArgs);
- }
- else //if ($this->m_oLeftExpr instanceof Expression)
- {
- $this->m_oLeftExpr->ApplyParameters($aArgs);
- }
- if ($this->m_oRightExpr instanceof VariableExpression)
- {
- $this->m_oRightExpr = $this->m_oRightExpr->GetAsScalar($aArgs);
- }
- else //if ($this->m_oRightExpr instanceof Expression)
- {
- $this->m_oRightExpr->ApplyParameters($aArgs);
- }
- }
-
- public function GetUnresolvedFields($sAlias, &$aUnresolved)
- {
- $this->GetLeftExpr()->GetUnresolvedFields($sAlias, $aUnresolved);
- $this->GetRightExpr()->GetUnresolvedFields($sAlias, $aUnresolved);
- }
- public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
- {
- $oLeft = $this->GetLeftExpr()->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
- $oRight = $this->GetRightExpr()->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
- return new BinaryExpression($oLeft, $this->GetOperator(), $oRight);
- }
- public function ListRequiredFields()
- {
- $aLeft = $this->GetLeftExpr()->ListRequiredFields();
- $aRight = $this->GetRightExpr()->ListRequiredFields();
- return array_merge($aLeft, $aRight);
- }
-
-
- /**
- * List all constant expression of the form <field> = <scalar> or <field> = :<variable>
- * Could be extended to support <field> = <function><constant_expression>
- */
- public function ListConstantFields()
- {
- $aResult = array();
- if ($this->m_sOperator == '=')
- {
- if (($this->m_oLeftExpr instanceof FieldExpression) && ($this->m_oRightExpr instanceof ScalarExpression))
- {
- $aResult[$this->m_oLeftExpr->GetParent()][$this->m_oLeftExpr->GetName()] = $this->m_oRightExpr;
- }
- else if (($this->m_oRightExpr instanceof FieldExpression) && ($this->m_oLeftExpr instanceof ScalarExpression))
- {
- $aResult[$this->m_oRightExpr->GetParent()][$this->m_oRightExpr->GetName()] = $this->m_oLeftExpr;
- }
- else if (($this->m_oLeftExpr instanceof FieldExpression) && ($this->m_oRightExpr instanceof VariableExpression))
- {
- $aResult[$this->m_oLeftExpr->GetParent()][$this->m_oLeftExpr->GetName()] = $this->m_oRightExpr;
- }
- else if (($this->m_oRightExpr instanceof FieldExpression) && ($this->m_oLeftExpr instanceof VariableExpression))
- {
- $aResult[$this->m_oRightExpr->GetParent()][$this->m_oRightExpr->GetName()] = $this->m_oLeftExpr;
- }
- else
- {
- $aResult = array_merge($this->m_oRightExpr->ListConstantFields(), $this->m_oLeftExpr->ListConstantFields()) ;
- }
- }
- else
- {
- $aResult = array_merge($this->m_oRightExpr->ListConstantFields(), $this->m_oLeftExpr->ListConstantFields()) ;
- }
- return $aResult;
- }
-
- public function RenameParam($sOldName, $sNewName)
- {
- $this->GetLeftExpr()->RenameParam($sOldName, $sNewName);
- $this->GetRightExpr()->RenameParam($sOldName, $sNewName);
- }
- }
- class UnaryExpression extends Expression
- {
- protected $m_value;
- public function __construct($value)
- {
- $this->m_value = $value;
- }
- public function IsTrue()
- {
- // return true if we are certain that it will be true
- return ($this->m_value == 1);
- }
- public function GetValue()
- {
- return $this->m_value;
- }
- // recursive rendering
- public function Render(&$aArgs = null, $bRetrofitParams = false)
- {
- return CMDBSource::Quote($this->m_value);
- }
- public function ApplyParameters($aArgs)
- {
- }
-
- public function GetUnresolvedFields($sAlias, &$aUnresolved)
- {
- }
- public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
- {
- return clone $this;
- }
- public function ListRequiredFields()
- {
- return array();
- }
- public function ListConstantFields()
- {
- return array();
- }
-
- public function RenameParam($sOldName, $sNewName)
- {
- // Do nothing
- // really ? what about :param{$iParamIndex} ??
- }
- }
- class ScalarExpression extends UnaryExpression
- {
- public function __construct($value)
- {
- if (!is_scalar($value) && !is_null($value))
- {
- throw new CoreException('Attempt to create a scalar expression from a non scalar', array('var_type'=>gettype($value)));
- }
- parent::__construct($value);
- }
- // recursive rendering
- public function Render(&$aArgs = null, $bRetrofitParams = false)
- {
- if (is_null($this->m_value))
- {
- $sRet = 'NULL';
- }
- else
- {
- $sRet = CMDBSource::Quote($this->m_value);
- }
- return $sRet;
- }
- }
- class TrueExpression extends ScalarExpression
- {
- public function __construct()
- {
- parent::__construct(1);
- }
- public function IsTrue()
- {
- return true;
- }
- }
- class FalseExpression extends ScalarExpression
- {
- public function __construct()
- {
- parent::__construct(0);
- }
- public function IsTrue()
- {
- return false;
- }
- }
- class FieldExpression extends UnaryExpression
- {
- protected $m_sParent;
- protected $m_sName;
- public function __construct($sName, $sParent = '')
- {
- parent::__construct("$sParent.$sName");
- $this->m_sParent = $sParent;
- $this->m_sName = $sName;
- }
- public function IsTrue()
- {
- // return true if we are certain that it will be true
- return false;
- }
- public function GetParent() {return $this->m_sParent;}
- public function GetName() {return $this->m_sName;}
- // recursive rendering
- public function Render(&$aArgs = null, $bRetrofitParams = false)
- {
- if (empty($this->m_sParent))
- {
- return "`{$this->m_sName}`";
- }
- return "`{$this->m_sParent}`.`{$this->m_sName}`";
- }
- public function ListRequiredFields()
- {
- return array($this->m_sParent.'.'.$this->m_sName);
- }
- public function GetUnresolvedFields($sAlias, &$aUnresolved)
- {
- if ($this->m_sParent == $sAlias)
- {
- // Add a reference to the field
- $aUnresolved[$this->m_sName] = $this;
- }
- }
- public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
- {
- if (!array_key_exists($this->m_sParent, $aTranslationData))
- {
- if ($bMatchAll) throw new CoreException('Unknown parent id in translation table', array('parent_id' => $this->m_sParent, 'translation_table' => array_keys($aTranslationData)));
- return clone $this;
- }
- if (!array_key_exists($this->m_sName, $aTranslationData[$this->m_sParent]))
- {
- if (!array_key_exists('*', $aTranslationData[$this->m_sParent]))
- {
- // #@# debug - if ($bMatchAll) MyHelpers::var_dump_html($aTranslationData, true);
- if ($bMatchAll) throw new CoreException('Unknown name in translation table', array('name' => $this->m_sName, 'parent_id' => $this->m_sParent, 'translation_table' => array_keys($aTranslationData[$this->m_sParent])));
- return clone $this;
- }
- $sNewParent = $aTranslationData[$this->m_sParent]['*'];
- $sNewName = $this->m_sName;
- if ($bMarkFieldsAsResolved)
- {
- $oRet = new FieldExpressionResolved($sNewName, $sNewParent);
- }
- else
- {
- $oRet = new FieldExpression($sNewName, $sNewParent);
- }
- }
- else
- {
- $oRet = $aTranslationData[$this->m_sParent][$this->m_sName];
- }
- return $oRet;
- }
- /**
- * Make the most relevant label, given the value of the expression
- *
- * @param DBObjectSearch oFilter The context in which this expression has been used
- * @param string sValue The value returned by the query, for this expression
- * @param string sDefault The default value if no relevant label could be computed
- * @return The label
- */
- public function MakeValueLabel($oFilter, $sValue, $sDefault)
- {
- $sAttCode = $this->GetName();
- $sParentAlias = $this->GetParent();
- $aSelectedClasses = $oFilter->GetSelectedClasses();
- $sClass = $aSelectedClasses[$sParentAlias];
- $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
- // Set a default value for the general case
- $sRes = $oAttDef->GetAsHtml($sValue);
- // Exceptions...
- if ($oAttDef->IsExternalKey())
- {
- $sObjClass = $oAttDef->GetTargetClass();
- $iObjKey = (int)$sValue;
- if ($iObjKey > 0)
- {
- $oObject = MetaModel::GetObject($sObjClass, $iObjKey);
- $sRes = $oObject->GetHyperlink();
- }
- else
- {
- // Undefined
- $sRes = DBObject::MakeHyperLink($sObjClass, 0);
- }
- }
- elseif ($oAttDef->IsExternalField())
- {
- if (is_null($sValue))
- {
- $sRes = Dict::S('UI:UndefinedObject');
- }
- }
- return $sRes;
- }
- }
- // Has been resolved into an SQL expression
- class FieldExpressionResolved extends FieldExpression
- {
- public function GetUnresolvedFields($sAlias, &$aUnresolved)
- {
- }
- public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
- {
- return clone $this;
- }
- }
- class VariableExpression extends UnaryExpression
- {
- protected $m_sName;
- public function __construct($sName)
- {
- parent::__construct($sName);
- $this->m_sName = $sName;
- }
- public function IsTrue()
- {
- // return true if we are certain that it will be true
- return false;
- }
- public function GetName() {return $this->m_sName;}
- // recursive rendering
- public function Render(&$aArgs = null, $bRetrofitParams = false)
- {
- if (is_null($aArgs))
- {
- return ':'.$this->m_sName;
- }
- elseif (array_key_exists($this->m_sName, $aArgs))
- {
- return CMDBSource::Quote($aArgs[$this->m_sName]);
- }
- elseif ($bRetrofitParams)
- {
- $aArgs[$this->m_sName] = null;
- return ':'.$this->m_sName;
- }
- else
- {
- throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>$aArgs));
- }
- }
-
- public function RenameParam($sOldName, $sNewName)
- {
- if ($this->m_sName == $sOldName)
- {
- $this->m_sName = $sNewName;
- }
- }
-
- public function GetAsScalar($aArgs)
- {
- $value = '';
- if (array_key_exists($this->m_sName, $aArgs))
- {
- $value = $aArgs[$this->m_sName];
- }
- else
- {
- throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>array_keys($aArgs)));
- }
- return new ScalarExpression($value);
- }
- }
- // Temporary, until we implement functions and expression casting!
- // ... or until we implement a real full text search based in the MATCH() expression
- class ListExpression extends Expression
- {
- protected $m_aExpressions;
- public function __construct($aExpressions)
- {
- $this->m_aExpressions = $aExpressions;
- }
- public static function FromScalars($aScalars)
- {
- $aExpressions = array();
- foreach($aScalars as $value)
- {
- $aExpressions[] = new ScalarExpression($value);
- }
- return new ListExpression($aExpressions);
- }
- public function IsTrue()
- {
- // return true if we are certain that it will be true
- return false;
- }
- public function GetItems()
- {
- return $this->m_aExpressions;
- }
- // recursive rendering
- public function Render(&$aArgs = null, $bRetrofitParams = false)
- {
- $aRes = array();
- foreach ($this->m_aExpressions as $oExpr)
- {
- $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
- }
- return '('.implode(', ', $aRes).')';
- }
- public function ApplyParameters($aArgs)
- {
- $aRes = array();
- foreach ($this->m_aExpressions as $idx => $oExpr)
- {
- if ($oExpr instanceof VariableExpression)
- {
- $this->m_aExpressions[$idx] = $oExpr->GetAsScalar();
- }
- else
- {
- $oExpr->ApplyParameters($aArgs);
- }
- }
- }
-
- public function GetUnresolvedFields($sAlias, &$aUnresolved)
- {
- foreach ($this->m_aExpressions as $oExpr)
- {
- $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
- }
- }
- public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
- {
- $aRes = array();
- foreach ($this->m_aExpressions as $oExpr)
- {
- $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
- }
- return new ListExpression($aRes);
- }
- public function ListRequiredFields()
- {
- $aRes = array();
- foreach ($this->m_aExpressions as $oExpr)
- {
- $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
- }
- return $aRes;
- }
-
- public function ListConstantFields()
- {
- $aRes = array();
- foreach ($this->m_aExpressions as $oExpr)
- {
- $aRes = array_merge($aRes, $oExpr->ListConstantFields());
- }
- return $aRes;
- }
-
- public function RenameParam($sOldName, $sNewName)
- {
- $aRes = array();
- foreach ($this->m_aExpressions as $key => $oExpr)
- {
- $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
- }
- }
- }
- class FunctionExpression extends Expression
- {
- protected $m_sVerb;
- protected $m_aArgs; // array of expressions
- public function __construct($sVerb, $aArgExpressions)
- {
- $this->m_sVerb = $sVerb;
- $this->m_aArgs = $aArgExpressions;
- }
- public function IsTrue()
- {
- // return true if we are certain that it will be true
- return false;
- }
- public function GetVerb()
- {
- return $this->m_sVerb;
- }
- public function GetArgs()
- {
- return $this->m_aArgs;
- }
- // recursive rendering
- public function Render(&$aArgs = null, $bRetrofitParams = false)
- {
- $aRes = array();
- foreach ($this->m_aArgs as $oExpr)
- {
- $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
- }
- return $this->m_sVerb.'('.implode(', ', $aRes).')';
- }
- public function ApplyParameters($aArgs)
- {
- $aRes = array();
- foreach ($this->m_aArgs as $idx => $oExpr)
- {
- if ($oExpr instanceof VariableExpression)
- {
- $this->m_aArgs[$idx] = $oExpr->GetAsScalar($aArgs);
- }
- else
- {
- $oExpr->ApplyParameters($aArgs);
- }
- }
- }
- public function GetUnresolvedFields($sAlias, &$aUnresolved)
- {
- foreach ($this->m_aArgs as $oExpr)
- {
- $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
- }
- }
- public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
- {
- $aRes = array();
- foreach ($this->m_aArgs as $oExpr)
- {
- $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
- }
- return new FunctionExpression($this->m_sVerb, $aRes);
- }
- public function ListRequiredFields()
- {
- $aRes = array();
- foreach ($this->m_aArgs as $oExpr)
- {
- $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
- }
- return $aRes;
- }
-
- public function ListConstantFields()
- {
- $aRes = array();
- foreach ($this->m_aArgs as $oExpr)
- {
- $aRes = array_merge($aRes, $oExpr->ListConstantFields());
- }
- return $aRes;
- }
-
- public function RenameParam($sOldName, $sNewName)
- {
- foreach ($this->m_aArgs as $key => $oExpr)
- {
- $this->m_aArgs[$key] = $oExpr->RenameParam($sOldName, $sNewName);
- }
- }
- }
- class IntervalExpression extends Expression
- {
- protected $m_oValue; // expression
- protected $m_sUnit;
- public function __construct($oValue, $sUnit)
- {
- $this->m_oValue = $oValue;
- $this->m_sUnit = $sUnit;
- }
- public function IsTrue()
- {
- // return true if we are certain that it will be true
- return false;
- }
- public function GetValue()
- {
- return $this->m_oValue;
- }
- public function GetUnit()
- {
- return $this->m_sUnit;
- }
- // recursive rendering
- public function Render(&$aArgs = null, $bRetrofitParams = false)
- {
- return 'INTERVAL '.$this->m_oValue->Render($aArgs, $bRetrofitParams).' '.$this->m_sUnit;
- }
- public function ApplyParameters($aArgs)
- {
- if ($this->m_oValue instanceof VariableExpression)
- {
- $this->m_oValue = $this->m_oValue->GetAsScalar($aArgs);
- }
- else
- {
- $this->m_oValue->ApplyParameters($aArgs);
- }
- }
-
- public function GetUnresolvedFields($sAlias, &$aUnresolved)
- {
- $this->m_oValue->GetUnresolvedFields($sAlias, $aUnresolved);
- }
- public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
- {
- return new IntervalExpression($this->m_oValue->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved), $this->m_sUnit);
- }
- public function ListRequiredFields()
- {
- return array();
- }
- public function ListConstantFields()
- {
- return array();
- }
-
- public function RenameParam($sOldName, $sNewName)
- {
- $this->m_oValue->RenameParam($sOldName, $sNewName);
- }
- }
- class CharConcatExpression extends Expression
- {
- protected $m_aExpressions;
- public function __construct($aExpressions)
- {
- $this->m_aExpressions = $aExpressions;
- }
- public function IsTrue()
- {
- // return true if we are certain that it will be true
- return false;
- }
- public function GetItems()
- {
- return $this->m_aExpressions;
- }
- // recursive rendering
- public function Render(&$aArgs = null, $bRetrofitParams = false)
- {
- $aRes = array();
- foreach ($this->m_aExpressions as $oExpr)
- {
- $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
- // Concat will be globally NULL if one single argument is null !
- $aRes[] = "COALESCE($sCol, '')";
- }
- return "CAST(CONCAT(".implode(', ', $aRes).") AS CHAR)";
- }
- public function ApplyParameters($aArgs)
- {
- $aRes = array();
- foreach ($this->m_aExpressions as $idx => $oExpr)
- {
- if ($oExpr instanceof VariableExpression)
- {
- $this->m_aExpressions[$idx] = $oExpr->GetAsScalar();
- }
- else
- {
- $this->m_aExpressions->ApplyParameters($aArgs);
- }
- }
- }
- public function GetUnresolvedFields($sAlias, &$aUnresolved)
- {
- foreach ($this->m_aExpressions as $oExpr)
- {
- $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
- }
- }
- public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
- {
- $aRes = array();
- foreach ($this->m_aExpressions as $oExpr)
- {
- $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
- }
- return new CharConcatExpression($aRes);
- }
- public function ListRequiredFields()
- {
- $aRes = array();
- foreach ($this->m_aExpressions as $oExpr)
- {
- $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
- }
- return $aRes;
- }
- public function ListConstantFields()
- {
- $aRes = array();
- foreach ($this->m_aExpressions as $oExpr)
- {
- $aRes = array_merge($aRes, $oExpr->ListConstantFields());
- }
- return $aRes;
- }
- public function RenameParam($sOldName, $sNewName)
- {
- foreach ($this->m_aExpressions as $key => $oExpr)
- {
- $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
- }
- }
- }
- class CharConcatWSExpression extends CharConcatExpression
- {
- protected $m_separator;
- public function __construct($separator, $aExpressions)
- {
- $this->m_separator = $separator;
- parent::__construct($aExpressions);
- }
- // recursive rendering
- public function Render(&$aArgs = null, $bRetrofitParams = false)
- {
- $aRes = array();
- foreach ($this->m_aExpressions as $oExpr)
- {
- $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
- // Concat will be globally NULL if one single argument is null !
- $aRes[] = "COALESCE($sCol, '')";
- }
- $sSep = CMDBSource::Quote($this->m_separator);
- return "CAST(CONCAT_WS($sSep, ".implode(', ', $aRes).") AS CHAR)";
- }
- }
- class QueryBuilderExpressions
- {
- protected $m_oConditionExpr;
- protected $m_aSelectExpr;
- protected $m_aGroupByExpr;
- protected $m_aJoinFields;
- public function __construct($oCondition, $aGroupByExpr = null)
- {
- $this->m_oConditionExpr = $oCondition;
- $this->m_aSelectExpr = array();
- $this->m_aGroupByExpr = $aGroupByExpr;
- $this->m_aJoinFields = array();
- }
- public function GetSelect()
- {
- return $this->m_aSelectExpr;
- }
- public function GetGroupBy()
- {
- return $this->m_aGroupByExpr;
- }
- public function GetCondition()
- {
- return $this->m_oConditionExpr;
- }
- public function PopJoinField()
- {
- return array_pop($this->m_aJoinFields);
- }
- public function AddSelect($sAttAlias, $oExpression)
- {
- $this->m_aSelectExpr[$sAttAlias] = $oExpression;
- }
- //$oConditionTree = $oConditionTree->LogAnd($oFinalClassRestriction);
- public function AddCondition($oExpression)
- {
- $this->m_oConditionExpr = $this->m_oConditionExpr->LogAnd($oExpression);
- }
- public function PushJoinField($oExpression)
- {
- array_push($this->m_aJoinFields, $oExpression);
- }
- public function GetUnresolvedFields($sAlias, &$aUnresolved)
- {
- $this->m_oConditionExpr->GetUnresolvedFields($sAlias, $aUnresolved);
- foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
- {
- $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
- }
- if ($this->m_aGroupByExpr)
- {
- foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
- {
- $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
- }
- }
- foreach($this->m_aJoinFields as $oExpression)
- {
- $oExpression->GetUnresolvedFields($sAlias, $aUnresolved);
- }
- }
- public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
- {
- $this->m_oConditionExpr = $this->m_oConditionExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
- foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
- {
- $this->m_aSelectExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
- }
- if ($this->m_aGroupByExpr)
- {
- foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
- {
- $this->m_aGroupByExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
- }
- }
- foreach($this->m_aJoinFields as $index => $oExpression)
- {
- $this->m_aJoinFields[$index] = $oExpression->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
- }
- }
- public function RenameParam($sOldName, $sNewName)
- {
- $this->m_oConditionExpr->RenameParam($sOldName, $sNewName);
- foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
- {
- $this->m_aSelectExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
- }
- if ($this->m_aGroupByExpr)
- {
- foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
- {
- $this->m_aGroupByExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
- }
- }
- foreach($this->m_aJoinFields as $index => $oExpression)
- {
- $this->m_aJoinFields[$index] = $oExpression->RenameParam($sOldName, $sNewName);
- }
- }
- }
- ?>
|