Changeset 157 for tags/2.0.2/lib/DB.inc.php
- Timestamp:
- Jun 9, 2006 9:25:54 PM (18 years ago)
- Location:
- tags/2.0.2
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
tags/2.0.2/lib/DB.inc.php
r71 r157 85 85 function setParam($params) 86 86 { 87 if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) { 88 $this =& DB::getInstance(); 89 } 87 $_this =& DB::getInstance(); 90 88 91 89 if (isset($params) && is_array($params)) { 92 90 // Merge new parameters with old overriding only those passed. 93 $ this->_params = array_merge($this->_params, $params);91 $_this->_params = array_merge($_this->_params, $params); 94 92 } else { 95 93 App::logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); … … 108 106 function getParam($param) 109 107 { 110 if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) { 111 $this =& DB::getInstance(); 112 } 113 114 if (isset($this->_params[$param])) { 115 return $this->_params[$param]; 108 $_this =& DB::getInstance(); 109 110 if (isset($_this->_params[$param])) { 111 return $_this->_params[$param]; 116 112 } else { 117 113 App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); … … 129 125 function connect() 130 126 { 131 if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) { 132 $this =& DB::getInstance(); 133 } 134 135 if (!$this->getParam('db_name') || !$this->getParam('db_user') || !$this->getParam('db_pass')) { 127 $_this =& DB::getInstance(); 128 129 if (!$_this->getParam('db_name') || !$_this->getParam('db_user') || !$_this->getParam('db_pass')) { 136 130 App::logMsg('Database credentials missing.', LOG_EMERG, __FILE__, __LINE__); 137 131 return false; … … 139 133 140 134 // Connect to database. Always create a new link to the server. 141 if ($ this->dbh = mysql_connect($this->getParam('db_server'), $this->getParam('db_user'), $this->getParam('db_pass'), true)) {135 if ($_this->dbh = mysql_connect($_this->getParam('db_server'), $_this->getParam('db_user'), $_this->getParam('db_pass'), true)) { 142 136 // Select database 143 mysql_select_db($ this->getParam('db_name'), $this->dbh);137 mysql_select_db($_this->getParam('db_name'), $_this->dbh); 144 138 } 145 139 146 140 // Test for connection errors. 147 if (!$ this->dbh || mysql_error($this->dbh)) {148 $mysql_error_msg = $ this->dbh ? 'Codebase MySQL error: (' . mysql_errno($this->dbh) . ') ' . mysql_error($this->dbh) : 'Codebase MySQL error: Could not connect to server.';141 if (!$_this->dbh || mysql_error($_this->dbh)) { 142 $mysql_error_msg = $_this->dbh ? 'Codebase MySQL error: (' . mysql_errno($_this->dbh) . ') ' . mysql_error($_this->dbh) : 'Codebase MySQL error: Could not connect to server.'; 149 143 App::logMsg($mysql_error_msg, LOG_EMERG, __FILE__, __LINE__); 150 144 151 145 // Print helpful or pretty error? 152 if ($ this->getParam('db_debug')) {146 if ($_this->getParam('db_debug')) { 153 147 echo $mysql_error_msg . "\n"; 154 148 } else { … … 157 151 158 152 // Die or continue without connection? 159 if ($ this->getParam('db_die_on_failure')) {153 if ($_this->getParam('db_die_on_failure')) { 160 154 echo "\n\n<!-- Script execution stopped out of embarrassment. -->"; 161 155 die; … … 166 160 167 161 // DB connection success! 168 $ this->_connected = true;162 $_this->_connected = true; 169 163 170 164 // Tell MySQL what character set we're useing. Available only on MySQL verions > 4.01.01. 171 $ this->query("/*!40101 SET NAMES '" . $this->mysql_character_sets[strtolower(App::getParam('character_set'))] . "' */");165 $_this->query("/*!40101 SET NAMES '" . $_this->mysql_character_sets[strtolower(App::getParam('character_set'))] . "' */"); 172 166 173 167 return true; … … 183 177 function close() 184 178 { 185 if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) { 186 $this =& DB::getInstance(); 187 } 188 189 if (!$this->_connected) { 190 return false; 191 } 192 193 mysql_close($this->dbh); 179 $_this =& DB::getInstance(); 180 181 if (!$_this->_connected) { 182 return false; 183 } 184 185 mysql_close($_this->dbh); 194 186 } 195 187 … … 198 190 * 199 191 * @access public 200 * @return resource Current value of $ this->dbh.192 * @return resource Current value of $_this->dbh. 201 193 * @author Quinn Comendant <quinn@strangecode.com> 202 194 * @since 20 Aug 2005 13:50:36 … … 204 196 function getDBH() 205 197 { 206 if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) { 207 $this =& DB::getInstance(); 208 } 209 210 if (!$this->_connected) { 211 return false; 212 } 213 214 return $this->dbh; 198 $_this =& DB::getInstance(); 199 200 if (!$_this->_connected) { 201 return false; 202 } 203 204 return $_this->dbh; 215 205 } 216 206 … … 224 214 function isConnected() 225 215 { 226 return $this->_connected; 216 $_this =& DB::getInstance(); 217 218 return $_this->_connected; 227 219 } 228 220 … … 238 230 function escapeString($string) 239 231 { 240 if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) { 241 $this =& DB::getInstance(); 242 } 243 return mysql_real_escape_string($string, $this->dbh); 232 $_this =& DB::getInstance(); 233 234 return mysql_real_escape_string($string, $_this->dbh); 244 235 } 245 236 … … 256 247 static $_query_count = 0; 257 248 258 if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) { 259 $this =& DB::getInstance(); 260 } 261 262 if (!$this->_connected) { 249 $_this =& DB::getInstance(); 250 251 if (!$_this->_connected) { 263 252 return false; 264 253 } … … 266 255 $_query_count++; 267 256 $debugqry = preg_replace("/\n[\t ]+/", "\n", $query); 268 if ($ this->getParam('db_always_debug') || $debug) {257 if ($_this->getParam('db_always_debug') || $debug) { 269 258 echo "<!-- ----------------- Query $_query_count ---------------------\n$debugqry\n-->\n"; 270 259 } 271 260 272 261 // Execute! 273 $qid = mysql_query($query, $ this->dbh);262 $qid = mysql_query($query, $_this->dbh); 274 263 275 264 // Error checking. 276 if (!$qid || mysql_error($ this->dbh)) {277 if ($ this->getParam('db_debug')) {278 echo '<pre style="padding:2em; background:#ddd; font:9px monaco;">' . wordwrap(mysql_error($ this->dbh)) . '<hr>' . htmlspecialchars($debugqry) . '</pre>';265 if (!$qid || mysql_error($_this->dbh)) { 266 if ($_this->getParam('db_debug')) { 267 echo '<pre style="padding:2em; background:#ddd; font:9px monaco;">' . wordwrap(mysql_error($_this->dbh)) . '<hr>' . htmlspecialchars($debugqry) . '</pre>'; 279 268 } else { 280 269 echo _("This page is temporarily unavailable. It should be back up in a few minutes."); 281 270 } 282 App::logMsg(sprintf('MySQL error %s: %s in query: %s', mysql_errno($ this->dbh), mysql_error($this->dbh), $debugqry), LOG_EMERG, __FILE__, __LINE__);283 if ($ this->getParam('db_die_on_failure')) {271 App::logMsg(sprintf('MySQL error %s: %s in query: %s', mysql_errno($_this->dbh), mysql_error($_this->dbh), $debugqry), LOG_EMERG, __FILE__, __LINE__); 272 if ($_this->getParam('db_die_on_failure')) { 284 273 echo "\n\n<!-- Script execution stopped out of embarrassment. -->"; 285 274 die; … … 302 291 function tableExists($table, $use_cached_results=true) 303 292 { 304 if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) { 305 $this =& DB::getInstance(); 306 } 307 308 if (!$this->_connected) { 309 return false; 310 } 311 312 if (!isset($this->existing_tables) || !$use_cached_results) { 313 $this->existing_tables = array(); 314 $qid = $this->query("SHOW TABLES"); 293 $_this =& DB::getInstance(); 294 295 if (!$_this->_connected) { 296 return false; 297 } 298 299 if (!isset($_this->existing_tables) || !$use_cached_results) { 300 $_this->existing_tables = array(); 301 $qid = $_this->query("SHOW TABLES"); 315 302 while (list($row) = mysql_fetch_row($qid)) { 316 $ this->existing_tables[] = $row;317 } 318 } 319 if (in_array($table, $ this->existing_tables)) {303 $_this->existing_tables[] = $row; 304 } 305 } 306 if (in_array($table, $_this->existing_tables)) { 320 307 return true; 321 308 } else { 322 App::logMsg(sprintf('nonexistent DB table: %s.%s', $ this->getParam('db_name'), $table), LOG_ALERT, __FILE__, __LINE__);309 App::logMsg(sprintf('nonexistent DB table: %s.%s', $_this->getParam('db_name'), $table), LOG_ALERT, __FILE__, __LINE__); 323 310 return false; 324 311 } … … 336 323 function columnExists($table, $columns, $strict=true, $use_cached_results=true) 337 324 { 338 if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) { 339 $this =& DB::getInstance(); 340 } 341 342 if (!$this->_connected) { 325 $_this =& DB::getInstance(); 326 327 if (!$_this->_connected) { 343 328 return false; 344 329 } 345 330 346 331 // Ensure the table exists. 347 if (!$ this->tableExists($table, $use_cached_results)) {332 if (!$_this->tableExists($table, $use_cached_results)) { 348 333 return false; 349 334 } … … 354 339 } 355 340 356 if (!isset($ this->table_columns[$table]) || !$use_cached_results) {341 if (!isset($_this->table_columns[$table]) || !$use_cached_results) { 357 342 // Populate and cache array of current columns for this table. 358 $ this->table_columns[$table] = array();359 $qid = $ this->query("DESCRIBE $table");343 $_this->table_columns[$table] = array(); 344 $qid = $_this->query("DESCRIBE $table"); 360 345 while ($row = mysql_fetch_row($qid)) { 361 $ this->table_columns[$table][] = $row[0];346 $_this->table_columns[$table][] = $row[0]; 362 347 } 363 348 } … … 366 351 // Do an exact comparison of table schemas. 367 352 sort($columns); 368 sort($ this->table_columns[$table]);369 return $ this->table_columns[$table] == $columns;353 sort($_this->table_columns[$table]); 354 return $_this->table_columns[$table] == $columns; 370 355 } else { 371 356 // Only check that the specified columns are available in the table. 372 $match_columns = array_intersect($ this->table_columns[$table], $columns);357 $match_columns = array_intersect($_this->table_columns[$table], $columns); 373 358 sort($columns); 374 359 sort($match_columns); … … 386 371 function resetCache() 387 372 { 388 $ this->existing_tables = null;389 $ this->table_columns = null;373 $_this->existing_tables = null; 374 $_this->table_columns = null; 390 375 } 391 376
Note: See TracChangeset
for help on using the changeset viewer.