Passed
Push — master ( 7048d4...c63076 )
by Patrick
02:08
created

MibDatabase::cache_db_objects()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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