Changeset 119 for trunk/lib/DB.inc.php
- Timestamp:
- May 20, 2006 2:12:12 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/DB.inc.php
r71 r119 85 85 function setParam($params) 86 86 { 87 if (!isset($ this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {88 $ this =& DB::getInstance();87 if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) { 88 $_this =& DB::getInstance(); 89 89 } 90 90 91 91 if (isset($params) && is_array($params)) { 92 92 // Merge new parameters with old overriding only those passed. 93 $ this->_params = array_merge($this->_params, $params);93 $_this->_params = array_merge($_this->_params, $params); 94 94 } else { 95 95 App::logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); … … 108 108 function getParam($param) 109 109 { 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];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]; 116 116 } else { 117 117 App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); … … 129 129 function connect() 130 130 { 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')) {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')) { 136 136 App::logMsg('Database credentials missing.', LOG_EMERG, __FILE__, __LINE__); 137 137 return false; … … 139 139 140 140 // 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)) {141 if ($_this->dbh = mysql_connect($_this->getParam('db_server'), $_this->getParam('db_user'), $_this->getParam('db_pass'), true)) { 142 142 // Select database 143 mysql_select_db($ this->getParam('db_name'), $this->dbh);143 mysql_select_db($_this->getParam('db_name'), $_this->dbh); 144 144 } 145 145 146 146 // 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.';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.'; 149 149 App::logMsg($mysql_error_msg, LOG_EMERG, __FILE__, __LINE__); 150 150 151 151 // Print helpful or pretty error? 152 if ($ this->getParam('db_debug')) {152 if ($_this->getParam('db_debug')) { 153 153 echo $mysql_error_msg . "\n"; 154 154 } else { … … 157 157 158 158 // Die or continue without connection? 159 if ($ this->getParam('db_die_on_failure')) {159 if ($_this->getParam('db_die_on_failure')) { 160 160 echo "\n\n<!-- Script execution stopped out of embarrassment. -->"; 161 161 die; … … 166 166 167 167 // DB connection success! 168 $ this->_connected = true;168 $_this->_connected = true; 169 169 170 170 // 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'))] . "' */");171 $_this->query("/*!40101 SET NAMES '" . $_this->mysql_character_sets[strtolower(App::getParam('character_set'))] . "' */"); 172 172 173 173 return true; … … 183 183 function close() 184 184 { 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);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); 194 194 } 195 195 … … 204 204 function getDBH() 205 205 { 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;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; 215 215 } 216 216 … … 238 238 function escapeString($string) 239 239 { 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);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); 244 244 } 245 245 … … 256 256 static $_query_count = 0; 257 257 258 if (!isset($ this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {259 $ this =& DB::getInstance();260 } 261 262 if (!$ this->_connected) {258 if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) { 259 $_this =& DB::getInstance(); 260 } 261 262 if (!$_this->_connected) { 263 263 return false; 264 264 } … … 266 266 $_query_count++; 267 267 $debugqry = preg_replace("/\n[\t ]+/", "\n", $query); 268 if ($ this->getParam('db_always_debug') || $debug) {268 if ($_this->getParam('db_always_debug') || $debug) { 269 269 echo "<!-- ----------------- Query $_query_count ---------------------\n$debugqry\n-->\n"; 270 270 } 271 271 272 272 // Execute! 273 $qid = mysql_query($query, $ this->dbh);273 $qid = mysql_query($query, $_this->dbh); 274 274 275 275 // 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>';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>'; 279 279 } else { 280 280 echo _("This page is temporarily unavailable. It should be back up in a few minutes."); 281 281 } 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')) {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')) { 284 284 echo "\n\n<!-- Script execution stopped out of embarrassment. -->"; 285 285 die; … … 302 302 function tableExists($table, $use_cached_results=true) 303 303 { 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");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"); 315 315 while (list($row) = mysql_fetch_row($qid)) { 316 $ this->existing_tables[] = $row;317 } 318 } 319 if (in_array($table, $ this->existing_tables)) {316 $_this->existing_tables[] = $row; 317 } 318 } 319 if (in_array($table, $_this->existing_tables)) { 320 320 return true; 321 321 } else { 322 App::logMsg(sprintf('nonexistent DB table: %s.%s', $ this->getParam('db_name'), $table), LOG_ALERT, __FILE__, __LINE__);322 App::logMsg(sprintf('nonexistent DB table: %s.%s', $_this->getParam('db_name'), $table), LOG_ALERT, __FILE__, __LINE__); 323 323 return false; 324 324 } … … 336 336 function columnExists($table, $columns, $strict=true, $use_cached_results=true) 337 337 { 338 if (!isset($ this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {339 $ this =& DB::getInstance();340 } 341 342 if (!$ this->_connected) {338 if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) { 339 $_this =& DB::getInstance(); 340 } 341 342 if (!$_this->_connected) { 343 343 return false; 344 344 } 345 345 346 346 // Ensure the table exists. 347 if (!$ this->tableExists($table, $use_cached_results)) {347 if (!$_this->tableExists($table, $use_cached_results)) { 348 348 return false; 349 349 } … … 354 354 } 355 355 356 if (!isset($ this->table_columns[$table]) || !$use_cached_results) {356 if (!isset($_this->table_columns[$table]) || !$use_cached_results) { 357 357 // Populate and cache array of current columns for this table. 358 $ this->table_columns[$table] = array();359 $qid = $ this->query("DESCRIBE $table");358 $_this->table_columns[$table] = array(); 359 $qid = $_this->query("DESCRIBE $table"); 360 360 while ($row = mysql_fetch_row($qid)) { 361 $ this->table_columns[$table][] = $row[0];361 $_this->table_columns[$table][] = $row[0]; 362 362 } 363 363 } … … 366 366 // Do an exact comparison of table schemas. 367 367 sort($columns); 368 sort($ this->table_columns[$table]);369 return $ this->table_columns[$table] == $columns;368 sort($_this->table_columns[$table]); 369 return $_this->table_columns[$table] == $columns; 370 370 } else { 371 371 // Only check that the specified columns are available in the table. 372 $match_columns = array_intersect($ this->table_columns[$table], $columns);372 $match_columns = array_intersect($_this->table_columns[$table], $columns); 373 373 sort($columns); 374 374 sort($match_columns);
Note: See TracChangeset
for help on using the changeset viewer.