| Conditions | 17 |
| Paths | 41 |
| Total Lines | 102 |
| Code Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 203 | public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
||
| 204 | { |
||
| 205 | // Get current db number |
||
| 206 | $db_conn=$this->db_connect_trap(); |
||
| 207 | $sql='SELECT id,value from '.$this->dbPrefix.'db_config WHERE name=\'db_version\' '; |
||
| 208 | $this->logging->log('SQL query : '.$sql,DEBUG ); |
||
| 209 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
| 210 | $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
||
| 211 | return 'ERROR'; |
||
| 212 | } |
||
| 213 | $version=$ret_code->fetchAll(); |
||
| 214 | $cur_version=$version[0]['value']; |
||
| 215 | $db_version_id=$version[0]['id']; |
||
| 216 | |||
| 217 | if ($this->trapDBType == 'pgsql') |
||
| 218 | { |
||
| 219 | $prefix .= 'update_pgsql/schema_'; |
||
| 220 | } |
||
| 221 | else |
||
| 222 | { |
||
| 223 | $prefix .= 'update_sql/schema_'; |
||
| 224 | } |
||
| 225 | //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
||
| 226 | if ($getmsg === true) |
||
| 227 | { |
||
| 228 | $message=''; |
||
| 229 | $this->logging->log('getting message for upgrade',DEBUG ); |
||
| 230 | while($cur_version<$target_version) |
||
| 231 | { |
||
| 232 | $cur_version++; |
||
| 233 | $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
||
| 234 | $input_stream=fopen($updateFile, 'r'); |
||
| 235 | if ($input_stream=== false) |
||
| 236 | { |
||
| 237 | $this->logging->log("Error reading update file ". $updateFile,2,''); |
||
| 238 | return 'ERROR'; |
||
| 239 | } |
||
| 240 | do { $line=fgets($input_stream); } |
||
| 241 | while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
||
| 242 | if ($line === false) |
||
| 243 | { |
||
| 244 | $this->logging->log("No message in file ". $updateFile,2,''); |
||
| 245 | return 'ERROR'; |
||
| 246 | } |
||
| 247 | $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
||
| 248 | } |
||
| 249 | return $message; |
||
| 250 | } |
||
| 251 | while($cur_version<$target_version) |
||
| 252 | { // tODO : execute pre & post scripts |
||
| 253 | $cur_version++; |
||
| 254 | $this->logging->log('Updating to version : ' .$cur_version ,INFO ); |
||
| 255 | $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
||
| 256 | $input_stream=fopen($updateFile, 'r'); |
||
| 257 | if ($input_stream=== false) |
||
| 258 | { |
||
| 259 | $this->logging->log("Error reading update file ". $updateFile,2,''); |
||
| 260 | return 'ERROR'; |
||
| 261 | } |
||
| 262 | $newline=''; |
||
| 263 | $db_conn=$this->db_connect_trap(); |
||
| 264 | $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||
| 265 | while (($line=fgets($input_stream)) !== false) |
||
| 266 | { |
||
| 267 | if (preg_match('/^#/', $line)) continue; // ignore comment lines |
||
| 268 | $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
||
| 269 | if (preg_match('/; *$/', $newline)) |
||
| 270 | { |
||
| 271 | $sql_req=$db_conn->prepare($newline); |
||
| 272 | if ($sql_req->execute() === false) { |
||
| 273 | $this->logging->log('Error create schema : '.$newline,1,''); |
||
| 274 | } |
||
| 275 | $cur_table_array=array(); |
||
| 276 | if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
||
| 277 | { |
||
| 278 | $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
||
| 279 | } |
||
| 280 | else |
||
| 281 | { |
||
| 282 | $cur_table='secret SQL stuff :-)'; |
||
| 283 | //$cur_table=$newline; |
||
| 284 | } |
||
| 285 | $this->logging->log('Doing : ' . $cur_table,INFO ); |
||
| 286 | |||
| 287 | $newline=''; |
||
| 288 | } |
||
| 289 | } |
||
| 290 | fclose($input_stream); |
||
| 291 | |||
| 292 | //$sql= $newline; |
||
| 293 | //if ($db_conn->query($sql) === false) { |
||
| 294 | // $this->logging->log('Error updating schema : '.$sql,1,''); |
||
| 295 | //} |
||
| 296 | |||
| 297 | $sql='UPDATE '.$this->dbPrefix.'db_config SET value='.$cur_version.' WHERE ( id = '.$db_version_id.' )'; |
||
| 298 | $this->logging->log('SQL query : '.$sql,DEBUG ); |
||
| 299 | if ($db_conn->query($sql) === false) { |
||
| 300 | $this->logging->log('Cannot update db version. Query : ' . $sql,2); |
||
| 301 | return 'ERROR'; |
||
| 302 | } |
||
| 303 | |||
| 304 | $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
||
| 305 | } |
||
| 309 | } |