From 7fbcdc407e7f7c389cc9d0d16bbf7c9b5e97e626 Mon Sep 17 00:00:00 2001 From: Romain Quetiez Date: Wed, 24 Nov 2010 16:28:13 +0000 Subject: [PATCH] Added class DBProperty SVN:trunk[971] --- core/cmdbobject.class.inc.php | 2 + core/dbproperty.class.inc.php | 159 ++++++++++++++++++++++++++++++++++ test/testlist.inc.php | 3 +- 3 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 core/dbproperty.class.inc.php diff --git a/core/cmdbobject.class.inc.php b/core/cmdbobject.class.inc.php index b501468bf..6a583f971 100644 --- a/core/cmdbobject.class.inc.php +++ b/core/cmdbobject.class.inc.php @@ -59,6 +59,8 @@ require_once('dbobject.class.php'); require_once('dbobjectsearch.class.php'); require_once('dbobjectset.class.php'); +require_once('dbproperty.class.inc.php'); + // db change tracking data model require_once('cmdbchange.class.inc.php'); require_once('cmdbchangeop.class.inc.php'); diff --git a/core/dbproperty.class.inc.php b/core/dbproperty.class.inc.php new file mode 100644 index 000000000..994de667d --- /dev/null +++ b/core/dbproperty.class.inc.php @@ -0,0 +1,159 @@ + + * @author Romain Quetiez + * @author Denis Flaven + * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL + */ + + +/** + * A database property + * + * @package iTopORM + */ +class DBProperty extends DBObject +{ + public static function Init() + { + $aParams = array + ( + "category" => "cloud", + "key_type" => "autoincrement", + "name_attcode" => "name", + "state_attcode" => "", + "reconc_keys" => array(), + "db_table" => "priv_db_properties", + "db_key_field" => "id", + "db_finalclass_field" => "", + ); + MetaModel::Init_Params($aParams); + //MetaModel::Init_InheritAttributes(); + MetaModel::Init_AddAttribute(new AttributeString("name", array("allowed_values"=>null, "sql"=>"name", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array()))); + MetaModel::Init_AddAttribute(new AttributeString("description", array("allowed_values"=>null, "sql"=>"description", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array()))); + MetaModel::Init_AddAttribute(new AttributeString("value", array("allowed_values"=>null, "sql"=>"value", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array()))); + + MetaModel::Init_AddAttribute(new AttributeDateTime("change_date", array("allowed_values"=>null, "sql"=>"change_date", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array()))); + MetaModel::Init_AddAttribute(new AttributeString("change_comment", array("allowed_values"=>null, "sql"=>"change_comment", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array()))); + } + + /** + * Helper to check wether the table has been created into the DB + * (this table did not exist in 1.0.1 and older versions) + */ + public static function IsInstalled() + { + $sTable = MetaModel::DBGetTable(__CLASS__); + if (CMDBSource::IsTable($sTable)) + { + return true; + } + else + { + return false; + } + return false; + } + + public static function SetProperty($sName, $sValue, $sComment = '', $sDescription = null) + { + try + { + $oSearch = DBObjectSearch::FromOQL('SELECT DBProperty WHERE name = :name'); + $oSet = new DBObjectSet($oSearch, array(), array('name' => $sName)); + if ($oSet->Count() == 0) + { + $oProp = new DBProperty(); + $oProp->Set('name', $sName); + $oProp->Set('description', $sDescription); + $oProp->Set('value', $sValue); + $oProp->Set('change_date', time()); + $oProp->Set('change_comment', $sComment); + $oProp->DBInsert(); + } + elseif ($oSet->Count() == 1) + { + $oProp = $oSet->fetch(); + if (!is_null($sDescription)) + { + $oProp->Set('description', $sDescription); + } + $oProp->Set('value', $sValue); + $oProp->Set('change_date', time()); + $oProp->Set('change_comment', $sComment); + $oProp->DBUpdate(); + } + else + { + // Houston... + throw new CoreException('duplicate db property'); + } + } + catch (MySQLException $e) + { + // This might be because the table could not be found, + // let's check it and discard silently if this is really the case + if (self::IsInstalled()) + { + throw $e; + } + IssueLog::Error('Attempting to write a DBProperty while the module has not been installed'); + } + } + + public static function GetProperty($sName, $default = null) + { + try + { + $oSearch = DBObjectSearch::FromOQL('SELECT DBProperty WHERE name = :name'); + $oSet = new DBObjectSet($oSearch, array(), array('name' => $sName)); + $iCount = $oSet->Count(); + if ($iCount == 0) + { + //throw new CoreException('unknown db property', array('name' => $sName)); + $sValue = $default; + } + elseif ($iCount == 1) + { + $oProp = $oSet->fetch(); + $sValue = $oProp->Get('value'); + } + else + { + // $iCount > 1 + // Houston... + throw new CoreException('duplicate db property', array('name' => $sName, 'count' => $iCount)); + } + } + catch (MySQLException $e) + { + // This might be because the table could not be found, + // let's check it and discard silently if this is really the case + if (self::IsInstalled()) + { + throw $e; + } + $sValue = $default; + } + return $sValue; + } +} + +?> diff --git a/test/testlist.inc.php b/test/testlist.inc.php index a15ac5992..6a5a08f09 100644 --- a/test/testlist.inc.php +++ b/test/testlist.inc.php @@ -1904,6 +1904,7 @@ $aManageCloudUsersSpecs = array( 'args' => array( 'admin', /* sAdminLogin */ 'admin', /* sAdminPassword */ + 'http://myserver.mydomain.fr:8080', /* sCloudMgrUrl */ 'andros@combodo.com', /* sLogin */ 'André', /* sFirstName */ 'Dupont', /* sLastName */ @@ -2311,7 +2312,7 @@ class TestDBProperties extends TestBizModel { $sName = 'test'; DBProperty::SetProperty($sName, 'unix time:'.time(), 'means nothing', 'done with the automated test utility'); - $sValue = DBProperty::GetProperty($sName); + $sValue = DBProperty::GetProperty($sName, 'defaults to this because the table has not been created (1.0.1 install?)'); echo "

Write... then read property $sName, found: '$sValue'

\n"; } }