Changeset 42 for trunk/lib/DB.inc.php
- Timestamp:
- Dec 18, 2005 12:16:03 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/DB.inc.php
r41 r42 9 9 * @version 1.0.1 10 10 */ 11 11 12 12 class DB { 13 13 … … 35 35 'db_die_on_failure' => false, // TRUE = script stops on db error. 36 36 ); 37 37 38 38 // Translate between HTML and MySQL character set names. 39 39 var $mysql_character_sets = array( … … 41 41 'iso-8859-1' => 'latin1', 42 42 ); 43 43 44 44 // Caches. 45 45 var $existing_tables; 46 46 var $table_columns; 47 47 48 48 /** 49 49 * This method enforces the singleton pattern for this class. … … 63 63 return $instance; 64 64 } 65 65 66 66 /** 67 67 * Constructor. … … 119 119 } 120 120 } 121 121 122 122 /** 123 123 * Connect to database with credentials in params. … … 132 132 $this =& DB::getInstance(); 133 133 } 134 134 135 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; 138 138 } 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 143 mysql_select_db($this->getParam('db_name'), $this->dbh); 144 144 } 145 145 146 146 // Test for connection errors. 147 147 if (!$this->dbh || mysql_error($this->dbh)) { … … 164 164 } 165 165 } 166 166 167 167 // DB connection success! 168 168 $this->_connected = true; … … 173 173 return true; 174 174 } 175 175 176 176 /** 177 177 * Close db connection. … … 186 186 $this =& DB::getInstance(); 187 187 } 188 189 if (!$this->_connected) { 190 return false; 191 } 192 193 mysql_close($this->dbh); 194 } 195 188 189 if (!$this->_connected) { 190 return false; 191 } 192 193 mysql_close($this->dbh); 194 } 195 196 196 /** 197 197 * Return the current database handler. … … 207 207 $this =& DB::getInstance(); 208 208 } 209 209 210 210 if (!$this->_connected) { 211 211 return false; … … 214 214 return $this->dbh; 215 215 } 216 216 217 217 /** 218 218 * Returns connection status … … 226 226 return $this->_connected; 227 227 } 228 228 229 229 /** 230 230 * A wrapper for mysql_query. Allows us to set the database link_identifier, … … 238 238 { 239 239 static $_query_count = 0; 240 241 if (!isset($this) || !is_a($this, 'DB')) { 242 $this =& DB::getInstance(); 243 } 244 240 241 if (!isset($this) || !is_a($this, 'DB')) { 242 $this =& DB::getInstance(); 243 } 244 245 245 if (!$this->_connected) { 246 246 return false; … … 252 252 echo "<!-- ----------------- Query $_query_count ---------------------\n$debugqry\n-->\n"; 253 253 } 254 254 255 255 // Execute! 256 256 $qid = mysql_query($query, $this->dbh); 257 257 258 258 // Error checking. 259 259 if (!$qid || mysql_error($this->dbh)) { … … 269 269 } 270 270 } 271 271 272 272 return $qid; 273 273 } 274 274 275 275 /** 276 * Loads a list of tables in the current database into an array, and returns 276 * Loads a list of tables in the current database into an array, and returns 277 277 * true if the requested table is found. Use this function to enable/disable 278 * funtionality based upon the current available db tables or to dynamically 278 * funtionality based upon the current available db tables or to dynamically 279 279 * create tables if missing. 280 280 * … … 284 284 */ 285 285 function tableExists($table, $use_cached_results=true) 286 { 287 if (!isset($this) || !is_a($this, 'DB')) { 288 $this =& DB::getInstance(); 289 } 290 286 { 287 if (!isset($this) || !is_a($this, 'DB')) { 288 $this =& DB::getInstance(); 289 } 290 291 291 if (!$this->_connected) { 292 292 return false; … … 307 307 } 308 308 } 309 309 310 310 /** 311 311 * Tests if the given array of columns exists in the specified table. … … 318 318 */ 319 319 function columnExists($table, $columns, $strict=true, $use_cached_results=true) 320 { 321 if (!isset($this) || !is_a($this, 'DB')) { 322 $this =& DB::getInstance(); 323 } 324 320 { 321 if (!isset($this) || !is_a($this, 'DB')) { 322 $this =& DB::getInstance(); 323 } 324 325 325 if (!$this->_connected) { 326 326 return false; … … 331 331 return false; 332 332 } 333 333 334 334 // For single-value columns. 335 335 if (!is_array($columns)) { 336 336 $columns = array($columns); 337 337 } 338 338 339 339 if (!isset($this->table_columns[$table]) || !$use_cached_results) { 340 340 // Populate and cache array of current columns for this table. … … 345 345 } 346 346 } 347 347 348 348 if ($strict) { 349 349 // Do an exact comparison of table schemas. … … 359 359 } 360 360 } 361 361 362 362 /** 363 363 * Reset cached items. … … 372 372 $this->table_columns = null; 373 373 } 374 374 375 375 } // End. 376 376
Note: See TracChangeset
for help on using the changeset viewer.