@@ -12,183 +12,183 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | trait MibDatabase |
| 14 | 14 | { |
| 15 | - /** @var array $oidDesc MUST be overide by calling class */ |
|
| 16 | - //protected $oidDesc; |
|
| 15 | + /** @var array $oidDesc MUST be overide by calling class */ |
|
| 16 | + //protected $oidDesc; |
|
| 17 | 17 | |
| 18 | - /** @var array $dbOidIndex MUST be overide by calling class */ |
|
| 19 | - //protected $dbOidIndex; |
|
| 18 | + /** @var array $dbOidIndex MUST be overide by calling class */ |
|
| 19 | + //protected $dbOidIndex; |
|
| 20 | 20 | |
| 21 | - /** @return \Trapdirector\Logging */ |
|
| 22 | - abstract public function getLogging(); |
|
| 21 | + /** @return \Trapdirector\Logging */ |
|
| 22 | + abstract public function getLogging(); |
|
| 23 | 23 | |
| 24 | - /** @return \Trapdirector\Database */ |
|
| 25 | - abstract public function getTrapsDB(); |
|
| 24 | + /** @return \Trapdirector\Database */ |
|
| 25 | + abstract public function getTrapsDB(); |
|
| 26 | 26 | |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Update or add an OID to database uses $this->dbOidIndex for mem cache |
|
| 30 | - * and $this->oidDesc doe data |
|
| 31 | - * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 32 | - */ |
|
| 33 | - public function update_oid() |
|
| 34 | - { |
|
| 35 | - $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 36 | - // Quote description. |
|
| 37 | - $this->oidDesc['description']=$db_conn->quote($this->oidDesc['description']); |
|
| 28 | + /** |
|
| 29 | + * Update or add an OID to database uses $this->dbOidIndex for mem cache |
|
| 30 | + * and $this->oidDesc doe data |
|
| 31 | + * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 32 | + */ |
|
| 33 | + public function update_oid() |
|
| 34 | + { |
|
| 35 | + $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 36 | + // Quote description. |
|
| 37 | + $this->oidDesc['description']=$db_conn->quote($this->oidDesc['description']); |
|
| 38 | 38 | |
| 39 | - if (isset($this->dbOidIndex[$this->oidDesc['oid']])) |
|
| 40 | - { // oid exists in db, so update |
|
| 41 | - return $this->update_oid_update(); |
|
| 42 | - } |
|
| 43 | - // create new OID. |
|
| 44 | - return $this->update_oid_create(); |
|
| 39 | + if (isset($this->dbOidIndex[$this->oidDesc['oid']])) |
|
| 40 | + { // oid exists in db, so update |
|
| 41 | + return $this->update_oid_update(); |
|
| 42 | + } |
|
| 43 | + // create new OID. |
|
| 44 | + return $this->update_oid_create(); |
|
| 45 | 45 | |
| 46 | - } |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Update object in DB with object in dbOidIndex if name/mib/type has changed. |
|
| 50 | - * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 51 | - */ |
|
| 52 | - private function update_oid_update() |
|
| 53 | - { |
|
| 48 | + /** |
|
| 49 | + * Update object in DB with object in dbOidIndex if name/mib/type has changed. |
|
| 50 | + * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 51 | + */ |
|
| 52 | + private function update_oid_update() |
|
| 53 | + { |
|
| 54 | 54 | |
| 55 | - $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 55 | + $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 56 | 56 | |
| 57 | - if ($this->dbOidIndex[$this->oidDesc['oid']]['key'] == -1) |
|
| 58 | - { // newly created. |
|
| 59 | - return 0; |
|
| 60 | - } |
|
| 61 | - $oidIndex=$this->dbOidIndex[$this->oidDesc['oid']]['key']; // Get index in dbOidAll |
|
| 62 | - $dbOid=$this->dbOidAll[$oidIndex]; // Get array of element |
|
| 63 | - if ( $this->oidDesc['name'] != $dbOid['name'] || |
|
| 64 | - $this->oidDesc['mib'] != $dbOid['mib'] || |
|
| 65 | - $this->oidDesc['type'] !=$dbOid['type'] |
|
| 66 | - ) |
|
| 67 | - { // Do update |
|
| 68 | - $sql='UPDATE '.$this->getTrapsDB()->dbPrefix.'mib_cache SET '. |
|
| 69 | - 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
|
| 70 | - ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
|
| 71 | - ' WHERE id= :id'; |
|
| 72 | - $sqlQuery=$db_conn->prepare($sql); |
|
| 57 | + if ($this->dbOidIndex[$this->oidDesc['oid']]['key'] == -1) |
|
| 58 | + { // newly created. |
|
| 59 | + return 0; |
|
| 60 | + } |
|
| 61 | + $oidIndex=$this->dbOidIndex[$this->oidDesc['oid']]['key']; // Get index in dbOidAll |
|
| 62 | + $dbOid=$this->dbOidAll[$oidIndex]; // Get array of element |
|
| 63 | + if ( $this->oidDesc['name'] != $dbOid['name'] || |
|
| 64 | + $this->oidDesc['mib'] != $dbOid['mib'] || |
|
| 65 | + $this->oidDesc['type'] !=$dbOid['type'] |
|
| 66 | + ) |
|
| 67 | + { // Do update |
|
| 68 | + $sql='UPDATE '.$this->getTrapsDB()->dbPrefix.'mib_cache SET '. |
|
| 69 | + 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
|
| 70 | + ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
|
| 71 | + ' WHERE id= :id'; |
|
| 72 | + $sqlQuery=$db_conn->prepare($sql); |
|
| 73 | 73 | |
| 74 | - $sqlParam=array( |
|
| 75 | - ':name' => $this->oidDesc['name'], |
|
| 76 | - ':type' => $this->oidDesc['type'], |
|
| 77 | - ':mib' => $this->oidDesc['mib'], |
|
| 78 | - ':tc' => $this->oidDesc['textconv']??'null', |
|
| 79 | - ':display_hint' => $this->oidDesc['dispHint']??'null' , |
|
| 80 | - ':syntax' => $this->oidDesc['syntax']==null??'null', |
|
| 81 | - ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
| 82 | - ':description' => $this->oidDesc['description']??'null', |
|
| 83 | - ':id' => $this->dbOidIndex[$this->oidDesc['oid']]['id'] |
|
| 84 | - ); |
|
| 74 | + $sqlParam=array( |
|
| 75 | + ':name' => $this->oidDesc['name'], |
|
| 76 | + ':type' => $this->oidDesc['type'], |
|
| 77 | + ':mib' => $this->oidDesc['mib'], |
|
| 78 | + ':tc' => $this->oidDesc['textconv']??'null', |
|
| 79 | + ':display_hint' => $this->oidDesc['dispHint']??'null' , |
|
| 80 | + ':syntax' => $this->oidDesc['syntax']==null??'null', |
|
| 81 | + ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
| 82 | + ':description' => $this->oidDesc['description']??'null', |
|
| 83 | + ':id' => $this->dbOidIndex[$this->oidDesc['oid']]['id'] |
|
| 84 | + ); |
|
| 85 | 85 | |
| 86 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
| 87 | - $this->getLogging()->log('Error in query : ' . $sql,ERROR,''); |
|
| 88 | - } |
|
| 89 | - $this->getLogging()->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 90 | - return 1; |
|
| 91 | - } |
|
| 92 | - else |
|
| 93 | - { |
|
| 94 | - $this->getLogging()->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 95 | - return 0; |
|
| 96 | - } |
|
| 97 | - } |
|
| 86 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
| 87 | + $this->getLogging()->log('Error in query : ' . $sql,ERROR,''); |
|
| 88 | + } |
|
| 89 | + $this->getLogging()->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 90 | + return 1; |
|
| 91 | + } |
|
| 92 | + else |
|
| 93 | + { |
|
| 94 | + $this->getLogging()->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 95 | + return 0; |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Create object in DB with object in dbOidIndex |
|
| 101 | - * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 102 | - */ |
|
| 103 | - private function update_oid_create() |
|
| 104 | - { |
|
| 105 | - // Insert data |
|
| 99 | + /** |
|
| 100 | + * Create object in DB with object in dbOidIndex |
|
| 101 | + * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 102 | + */ |
|
| 103 | + private function update_oid_create() |
|
| 104 | + { |
|
| 105 | + // Insert data |
|
| 106 | 106 | |
| 107 | - $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 108 | - $sql='INSERT INTO '.$this->getTrapsDB()->dbPrefix.'mib_cache '. |
|
| 109 | - '(oid, name, type , mib, textual_convention, display_hint '. |
|
| 110 | - ', syntax, type_enum , description ) ' . |
|
| 111 | - 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
|
| 112 | - ', :syntax, :type_enum, :description )'; |
|
| 107 | + $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 108 | + $sql='INSERT INTO '.$this->getTrapsDB()->dbPrefix.'mib_cache '. |
|
| 109 | + '(oid, name, type , mib, textual_convention, display_hint '. |
|
| 110 | + ', syntax, type_enum , description ) ' . |
|
| 111 | + 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
|
| 112 | + ', :syntax, :type_enum, :description )'; |
|
| 113 | 113 | |
| 114 | - if ($this->getTrapsDB()->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 114 | + if ($this->getTrapsDB()->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 115 | 115 | |
| 116 | - $sqlQuery=$db_conn->prepare($sql); |
|
| 116 | + $sqlQuery=$db_conn->prepare($sql); |
|
| 117 | 117 | |
| 118 | - $sqlParam=array( |
|
| 119 | - ':oid' => $this->oidDesc['oid'], |
|
| 120 | - ':name' => $this->oidDesc['name'], |
|
| 121 | - ':type' => $this->oidDesc['type'], |
|
| 122 | - ':mib' => $this->oidDesc['mib'], |
|
| 123 | - ':tc' => $this->oidDesc['textconv']??'null', |
|
| 124 | - ':display_hint' => $this->oidDesc['dispHint']??'null', |
|
| 125 | - ':syntax' => $this->oidDesc['syntax']??'null', |
|
| 126 | - ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
| 127 | - ':description' => $this->oidDesc['description']??'null' |
|
| 128 | - ); |
|
| 118 | + $sqlParam=array( |
|
| 119 | + ':oid' => $this->oidDesc['oid'], |
|
| 120 | + ':name' => $this->oidDesc['name'], |
|
| 121 | + ':type' => $this->oidDesc['type'], |
|
| 122 | + ':mib' => $this->oidDesc['mib'], |
|
| 123 | + ':tc' => $this->oidDesc['textconv']??'null', |
|
| 124 | + ':display_hint' => $this->oidDesc['dispHint']??'null', |
|
| 125 | + ':syntax' => $this->oidDesc['syntax']??'null', |
|
| 126 | + ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
| 127 | + ':description' => $this->oidDesc['description']??'null' |
|
| 128 | + ); |
|
| 129 | 129 | |
| 130 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
| 131 | - $this->getLogging()->log('Error in query : ' . $sql,1,''); |
|
| 132 | - } |
|
| 130 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
| 131 | + $this->getLogging()->log('Error in query : ' . $sql,1,''); |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - switch ($this->getTrapsDB()->trapDBType) |
|
| 135 | - { |
|
| 136 | - case 'pgsql': |
|
| 137 | - // Get last id to insert oid/values in secondary table |
|
| 138 | - if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 139 | - $this->getLogging()->log('Error getting id - pgsql - ',1,''); |
|
| 140 | - } |
|
| 141 | - if (! isset($inserted_id_ret['id'])) { |
|
| 142 | - $this->getLogging()->log('Error getting id - pgsql - empty.',ERROR); |
|
| 143 | - return 0; |
|
| 144 | - } |
|
| 145 | - $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id']; |
|
| 146 | - break; |
|
| 147 | - case 'mysql': |
|
| 148 | - // Get last id to insert oid/values in secondary table |
|
| 149 | - $sql='SELECT LAST_INSERT_ID();'; |
|
| 150 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 151 | - $this->getLogging()->log('Erreur getting id - mysql - ',ERROR); |
|
| 152 | - return 0; |
|
| 153 | - } |
|
| 134 | + switch ($this->getTrapsDB()->trapDBType) |
|
| 135 | + { |
|
| 136 | + case 'pgsql': |
|
| 137 | + // Get last id to insert oid/values in secondary table |
|
| 138 | + if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 139 | + $this->getLogging()->log('Error getting id - pgsql - ',1,''); |
|
| 140 | + } |
|
| 141 | + if (! isset($inserted_id_ret['id'])) { |
|
| 142 | + $this->getLogging()->log('Error getting id - pgsql - empty.',ERROR); |
|
| 143 | + return 0; |
|
| 144 | + } |
|
| 145 | + $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id']; |
|
| 146 | + break; |
|
| 147 | + case 'mysql': |
|
| 148 | + // Get last id to insert oid/values in secondary table |
|
| 149 | + $sql='SELECT LAST_INSERT_ID();'; |
|
| 150 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 151 | + $this->getLogging()->log('Erreur getting id - mysql - ',ERROR); |
|
| 152 | + return 0; |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 156 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 157 | - $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
|
| 158 | - break; |
|
| 159 | - default: |
|
| 160 | - $this->getLogging()->log('Error SQL type Unknown : '.$this->getTrapsDB()->trapDBType,ERROR); |
|
| 161 | - return 0; |
|
| 162 | - } |
|
| 155 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 156 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 157 | + $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
|
| 158 | + break; |
|
| 159 | + default: |
|
| 160 | + $this->getLogging()->log('Error SQL type Unknown : '.$this->getTrapsDB()->trapDBType,ERROR); |
|
| 161 | + return 0; |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - // Set as newly created. |
|
| 165 | - $this->dbOidIndex[$this->oidDesc['oid']]['key']=-1; |
|
| 166 | - return 2; |
|
| 167 | - } |
|
| 164 | + // Set as newly created. |
|
| 165 | + $this->dbOidIndex[$this->oidDesc['oid']]['key']=-1; |
|
| 166 | + return 2; |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * get all objects for a trap. |
|
| 171 | - * @param integer $trapId |
|
| 172 | - * @return array : array of cached objects |
|
| 173 | - */ |
|
| 174 | - private function cache_db_objects($trapId) |
|
| 175 | - { |
|
| 176 | - $dbObjects=array(); // cache of objects for trap in db |
|
| 177 | - $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 178 | - // Get all objects |
|
| 179 | - $sql='SELECT * FROM '.$this->getTrapsDB()->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
|
| 180 | - $this->getLogging()->log('SQL query get all traps: '.$sql,DEBUG ); |
|
| 181 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 182 | - $this->getLogging()->log('No result in query : ' . $sql,1,''); |
|
| 183 | - } |
|
| 184 | - $dbObjectsRaw=$ret_code->fetchAll(); |
|
| 169 | + /** |
|
| 170 | + * get all objects for a trap. |
|
| 171 | + * @param integer $trapId |
|
| 172 | + * @return array : array of cached objects |
|
| 173 | + */ |
|
| 174 | + private function cache_db_objects($trapId) |
|
| 175 | + { |
|
| 176 | + $dbObjects=array(); // cache of objects for trap in db |
|
| 177 | + $db_conn=$this->getTrapsDB()->db_connect_trap(); |
|
| 178 | + // Get all objects |
|
| 179 | + $sql='SELECT * FROM '.$this->getTrapsDB()->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
|
| 180 | + $this->getLogging()->log('SQL query get all traps: '.$sql,DEBUG ); |
|
| 181 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 182 | + $this->getLogging()->log('No result in query : ' . $sql,1,''); |
|
| 183 | + } |
|
| 184 | + $dbObjectsRaw=$ret_code->fetchAll(); |
|
| 185 | 185 | |
| 186 | - foreach ($dbObjectsRaw as $val) |
|
| 187 | - { |
|
| 188 | - $dbObjects[$val['object_id']]=1; |
|
| 189 | - } |
|
| 190 | - return $dbObjects; |
|
| 191 | - } |
|
| 186 | + foreach ($dbObjectsRaw as $val) |
|
| 187 | + { |
|
| 188 | + $dbObjects[$val['object_id']]=1; |
|
| 189 | + } |
|
| 190 | + return $dbObjects; |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | 193 | |
| 194 | 194 | } |