| Conditions | 17 |
| Paths | 41 |
| Total Lines | 102 |
| Code Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 87 | public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
||
| 88 | { |
||
| 89 | // Get current db number |
||
| 90 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 91 | $sql='SELECT id,value from '.$this->db_prefix.'db_config WHERE name=\'db_version\' '; |
||
|
|
|||
| 92 | $this->logging->log('SQL query : '.$sql,DEBUG ); |
||
| 93 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
| 94 | $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
||
| 95 | return; |
||
| 96 | } |
||
| 97 | $version=$ret_code->fetchAll(); |
||
| 98 | $cur_version=$version[0]['value']; |
||
| 99 | $db_version_id=$version[0]['id']; |
||
| 100 | |||
| 101 | if ($this->trapsDB->trapDBType == 'pgsql') |
||
| 102 | { |
||
| 103 | $prefix .= 'update_pgsql/schema_'; |
||
| 104 | } |
||
| 105 | else |
||
| 106 | { |
||
| 107 | $prefix .= 'update_sql/schema_'; |
||
| 108 | } |
||
| 109 | //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
||
| 110 | if ($getmsg === true) |
||
| 111 | { |
||
| 112 | $message=''; |
||
| 113 | $this->logging->log('getting message for upgrade',DEBUG ); |
||
| 114 | while($cur_version<$target_version) |
||
| 115 | { |
||
| 116 | $cur_version++; |
||
| 117 | $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
||
| 118 | $input_stream=fopen($updateFile, 'r'); |
||
| 119 | if ($input_stream=== false) |
||
| 120 | { |
||
| 121 | $this->logging->log("Error reading update file ". $updateFile,2,''); |
||
| 122 | return; |
||
| 123 | } |
||
| 124 | do { $line=fgets($input_stream); } |
||
| 125 | while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
||
| 126 | if ($line === false) |
||
| 127 | { |
||
| 128 | $this->logging->log("No message in file ". $updateFile,2,''); |
||
| 129 | return; |
||
| 130 | } |
||
| 131 | $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
||
| 132 | } |
||
| 133 | return $message; |
||
| 134 | } |
||
| 135 | while($cur_version<$target_version) |
||
| 136 | { // tODO : execute pre & post scripts |
||
| 137 | $cur_version++; |
||
| 138 | $this->logging->log('Updating to version : ' .$cur_version ,INFO ); |
||
| 139 | $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
||
| 140 | $input_stream=fopen($updateFile, 'r'); |
||
| 141 | if ($input_stream=== false) |
||
| 142 | { |
||
| 143 | $this->logging->log("Error reading update file ". $updateFile,2,''); |
||
| 144 | return; |
||
| 145 | } |
||
| 146 | $newline=''; |
||
| 147 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 148 | $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||
| 149 | while (($line=fgets($input_stream)) !== false) |
||
| 150 | { |
||
| 151 | if (preg_match('/^#/', $line)) continue; // ignore comment lines |
||
| 152 | $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
||
| 153 | if (preg_match('/; *$/', $newline)) |
||
| 154 | { |
||
| 155 | $sql_req=$db_conn->prepare($newline); |
||
| 156 | if ($sql_req->execute() === false) { |
||
| 157 | $this->logging->log('Error create schema : '.$newline,1,''); |
||
| 158 | } |
||
| 159 | $cur_table_array=array(); |
||
| 160 | if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
||
| 161 | { |
||
| 162 | $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
||
| 163 | } |
||
| 164 | else |
||
| 165 | { |
||
| 166 | $cur_table='secret SQL stuff :-)'; |
||
| 167 | //$cur_table=$newline; |
||
| 168 | } |
||
| 169 | $this->logging->log('Doing : ' . $cur_table,INFO ); |
||
| 170 | |||
| 171 | $newline=''; |
||
| 172 | } |
||
| 173 | } |
||
| 174 | fclose($input_stream); |
||
| 175 | |||
| 176 | //$sql= $newline; |
||
| 177 | //if ($db_conn->query($sql) === false) { |
||
| 178 | // $this->logging->log('Error updating schema : '.$sql,1,''); |
||
| 179 | //} |
||
| 180 | |||
| 181 | $sql='UPDATE '.$this->db_prefix.'db_config SET value='.$cur_version.' WHERE ( id = '.$db_version_id.' )'; |
||
| 182 | $this->logging->log('SQL query : '.$sql,DEBUG ); |
||
| 183 | if ($db_conn->query($sql) === false) { |
||
| 184 | $this->logging->log('Cannot update db version. Query : ' . $sql,2); |
||
| 185 | return; |
||
| 186 | } |
||
| 187 | |||
| 188 | $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
||
| 189 | } |
||
| 194 | } |