Total Complexity | 55 |
Total Lines | 380 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Database often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Database, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Database |
||
10 | { |
||
11 | |||
12 | // Databases |
||
13 | /** @var \PDO $trapDB trap database */ |
||
14 | protected $trapDB=null; |
||
15 | protected $idoDB=null; //< ido database |
||
16 | public $trapDBType; //< Type of database for traps (mysql, pgsql) |
||
17 | public $idoDBType; //< Type of database for ido (mysql, pgsql) |
||
18 | |||
19 | protected $trapDSN; //< trap database connection params |
||
20 | protected $trapUsername; //< trap database connection params |
||
21 | protected $trapPass; //< trap database connection params |
||
22 | public $dbPrefix; //< database tables prefix |
||
23 | |||
24 | protected $idoSet; //< bool true is ido database set |
||
25 | protected $idoDSN; //< trap database connection params |
||
26 | protected $idoUsername; //< trap database connection params |
||
27 | protected $idoPass; //< trap database connection params |
||
28 | |||
29 | // Logging function |
||
30 | |||
31 | protected $logging; //< logging class |
||
32 | |||
33 | /** |
||
34 | * @param Logging $logClass : where to log |
||
35 | * @param array $dbParam : array of named params type,host,dbname,username,[port],[password] |
||
36 | */ |
||
37 | function __construct($logClass,$dbParam,$dbPrefix) |
||
38 | { |
||
39 | $this->logging=$logClass; |
||
40 | $this->dbPrefix=$dbPrefix; |
||
41 | |||
42 | $this->trapDSN=$this->setupDSN($dbParam); |
||
43 | $this->trapUsername = $dbParam['username']; |
||
44 | $this->trapPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
||
45 | $this->trapDBType=$dbParam['db']; |
||
46 | $this->logging->log('DSN : '.$this->trapDSN. ';user '.$this->trapUsername.' / prefix : '. $this->dbPrefix,INFO); |
||
47 | $this->db_connect_trap(); |
||
48 | |||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Setup and connect to IDO database |
||
53 | * @param array $dbParam : array of named params |
||
54 | */ |
||
55 | public function setupIDO($dbParam) |
||
56 | { |
||
57 | $this->idoDSN=$this->setupDSN($dbParam); |
||
58 | $this->idoUsername = $dbParam['username']; |
||
59 | $this->idoPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
||
60 | $this->logging->log('DSN : '.$this->idoDSN. ';user '.$this->idoUsername,INFO); |
||
61 | $this->idoDBType=$dbParam['db']; |
||
62 | $this->db_connect_ido(); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Connect to IDO database |
||
67 | * @return \PDO |
||
68 | */ |
||
69 | public function db_connect_ido() |
||
70 | { |
||
71 | if ($this->idoDB != null) { |
||
72 | // Check if connection is still alive |
||
73 | try { |
||
74 | $this->idoDB->query('select 1')->fetchColumn(); |
||
75 | return $this->idoDB; |
||
76 | } catch (Exception $e) { |
||
77 | // select 1 failed, try to reconnect. |
||
78 | $this->logging->log('Database IDO connection lost, reconnecting',WARN); |
||
79 | } |
||
80 | } |
||
81 | try { |
||
82 | $this->idoDB = new PDO($this->idoDSN,$this->idoUsername,$this->idoPass); |
||
83 | } catch (PDOException $e) { |
||
84 | $this->logging->log('Connection failed to IDO : ' . $e->getMessage(),ERROR,''); |
||
85 | } |
||
86 | return $this->idoDB; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Connect to Trap database |
||
91 | * @return \PDO |
||
92 | */ |
||
93 | public function db_connect_trap() |
||
94 | { |
||
95 | if ($this->trapDB != null) { |
||
96 | // Check if connection is still alive |
||
97 | try { |
||
98 | $this->trapDB->query('select 1')->fetchColumn(); |
||
99 | return $this->trapDB; |
||
100 | } catch (Exception $e) { |
||
101 | // select 1 failed, try to reconnect. |
||
102 | $this->logging->log('Database connection lost, reconnecting',WARN); |
||
103 | } |
||
104 | } |
||
105 | try { |
||
106 | $this->trapDB = new PDO($this->trapDSN,$this->trapUsername,$this->trapPass); |
||
107 | } catch (PDOException $e) { |
||
108 | $this->logging->log('Connection failed : ' . $e->getMessage(),ERROR,''); |
||
109 | } |
||
110 | return $this->trapDB; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Setup dsn and check parameters |
||
115 | * @param array $configElmt |
||
116 | * @return string |
||
117 | */ |
||
118 | protected function setupDSN($configElmt) |
||
137 | } |
||
138 | |||
139 | /** Set name=element in database config table |
||
140 | * @param string $name |
||
141 | * @param string $element |
||
142 | * @return boolean true on success, else false (error logged) |
||
143 | */ |
||
144 | public function setDBConfig($name,$element) |
||
145 | { |
||
146 | $db_conn=$this->db_connect_trap(); |
||
147 | $sql='SELECT id from '.$this->dbPrefix.'db_config WHERE ( name=\''.$name.'\' )'; |
||
148 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
149 | $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
||
150 | return false; |
||
151 | } |
||
152 | $value=$ret_code->fetch(); |
||
153 | if ($value != null && isset($value['id'])) |
||
154 | { // Entry exists -> update |
||
155 | $sql='UPDATE '.$this->dbPrefix.'db_config SET value = \''.$element.'\' WHERE (id = '.$value['id'].')'; |
||
156 | } |
||
157 | else |
||
158 | { // Entry does no exists -> create |
||
159 | $sql='INSERT INTO '.$this->dbPrefix.'db_config (name,value) VALUES (\''.$name.'\' , \''.$element.'\' )'; |
||
160 | } |
||
161 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
|
|||
162 | $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
||
163 | return false; |
||
164 | } |
||
165 | $this->logging->log('Setting config '.$name.' = '.$element.' in database',INFO); |
||
166 | return true; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Get data from db_config |
||
171 | * @param $element string name of param |
||
172 | * @return mixed : value (or null) |
||
173 | */ |
||
174 | public function getDBConfig($element) |
||
175 | { |
||
176 | $db_conn=$this->db_connect_trap(); |
||
177 | $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
||
178 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
179 | $this->logging->log('No result in query : ' . $sql,WARN,''); |
||
180 | return null; |
||
181 | } |
||
182 | $value=$ret_code->fetch(); |
||
183 | if ($value != null && isset($value['value'])) |
||
184 | { |
||
185 | return $value['value']; |
||
186 | } |
||
187 | return null; |
||
188 | } |
||
189 | |||
190 | |||
191 | //********* Schema Management *********************/ |
||
192 | |||
193 | /** Create database schema |
||
194 | * @param $schema_file string File to read schema from |
||
195 | * @param $table_prefix string to replace #PREFIX# in schema file by this |
||
196 | */ |
||
197 | public function create_schema($schema_file,$table_prefix) |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Update database schema from current (as set in db) to $target_version |
||
248 | * @param $prefix string file prefix of sql update File |
||
249 | * @param $target_version int target db version number |
||
250 | * @param $table_prefix string to replace #PREFIX# in schema file by this |
||
251 | * @param bool $getmsg : only get messages from version upgrades |
||
252 | * @return string : if $getmsg=true, return messages or 'ERROR' on error. |
||
253 | */ |
||
254 | public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
||
255 | { |
||
256 | // Get current db number |
||
257 | $db_conn=$this->db_connect_trap(); |
||
258 | $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE name=\'db_version\' '; |
||
259 | $this->logging->log('SQL query : '.$sql,DEBUG ); |
||
260 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
261 | $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
||
262 | return 'ERROR'; |
||
263 | } |
||
264 | $version=$ret_code->fetchAll(); |
||
265 | $cur_version=$version[0]['value']; |
||
266 | |||
267 | if ($this->trapDBType == 'pgsql') |
||
268 | { |
||
269 | $prefix .= 'update_pgsql/schema_'; |
||
270 | } |
||
271 | else |
||
272 | { |
||
273 | $prefix .= 'update_sql/schema_'; |
||
274 | } |
||
275 | //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
||
276 | if ($getmsg === true) |
||
277 | { |
||
278 | return $this->update_schema_message($prefix, $cur_version, $target_version); |
||
279 | } |
||
280 | |||
281 | if ($this->update_schema_do($prefix, $cur_version, $target_version, $table_prefix) === true) |
||
282 | { |
||
283 | return 'ERROR'; |
||
284 | } |
||
285 | return ''; |
||
286 | |||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Update database schema from current (as set in db) to $target_version |
||
291 | * @param string $prefix file prefix of sql update File |
||
292 | * @param int $cur_version current db version number |
||
293 | * @param int $target_version target db version number |
||
294 | * @param string $table_prefix to replace #PREFIX# in schema file by this |
||
295 | * @return bool : true on error |
||
296 | */ |
||
297 | public function update_schema_do($prefix,$cur_version,$target_version,$table_prefix) |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Get database message for update to $target_version |
||
355 | * @param string $prefix file prefix of sql update File |
||
356 | * @param int $cur_version current db version number |
||
357 | * @param int $target_version target db version number |
||
358 | * @return string : return messages or 'ERROR'. |
||
359 | */ |
||
360 | private function update_schema_message($prefix,$cur_version,$target_version) |
||
389 | } |
||
390 | |||
391 | } |