- Timestamp:
- Mar 9, 2020 3:13:45 AM (4 years ago)
- Location:
- trunk/lib
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/DB.inc.php
r712 r719 172 172 // Connect to database. Always create a new link to the server. 173 173 // Connection errors are suppressed so we can do our own error management below. 174 if ($this->dbh = @mysql_connect($this->getParam('db_server'), $this->getParam('db_user'), $this->getParam('db_pass'), true)) {174 if ($this->dbh = mysql_connect($this->getParam('db_server'), $this->getParam('db_user'), $this->getParam('db_pass'), true)) { 175 175 // Select database 176 176 mysql_select_db($this->getParam('db_name'), $this->dbh); -
trunk/lib/PDO.inc.php
r718 r719 242 242 243 243 if (!$this->_connected) { 244 $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__);245 return false;246 } 244 throw new \Exception(sprintf('No DB connection to run %s', __METHOD__)); 245 } 246 247 247 $this->_connected = false; 248 248 $this->dbh = null; 249 249 250 return true; 250 251 } … … 280 281 281 282 if (!$this->_connected) { 282 $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__); 283 return false; 283 throw new \Exception(sprintf('No DB connection to run %s', __METHOD__)); 284 284 } 285 285 … … 374 374 375 375 if (!$this->_connected) { 376 $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__); 377 return false; 376 throw new \Exception(sprintf('No DB connection to run %s', __METHOD__)); 378 377 } 379 378 … … 431 430 432 431 if (!$this->_connected) { 433 $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__); 434 return false; 432 throw new \Exception(sprintf('No DB connection to run %s', __METHOD__)); 435 433 } 436 434 … … 527 525 528 526 if (!$this->_connected) { 529 $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__); 530 return false; 527 throw new \Exception(sprintf('No DB connection to run %s', __METHOD__)); 531 528 } 532 529 … … 558 555 559 556 if (!$this->_connected) { 560 $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__); 561 return false; 557 throw new \Exception(sprintf('No DB connection to run %s', __METHOD__)); 562 558 } 563 559 -
trunk/lib/Prefs.inc.php
r669 r719 121 121 $this->setParam($params); 122 122 123 // Run Prefs->save() upon script completion if we're using the database storagetype. 124 // This only works if 'storagetype' is provided as a parameter to the constructor rather than via setParam() later. 125 if ('database' == $this->getParam('storagetype')) { 126 register_shutdown_function(array($this, 'save')); 123 if (isset($params['save_on_shutdown']) && $params['save_on_shutdown']) { 124 // Run Prefs->save() upon script completion if we're using the database storagetype. 125 // This only works if: 126 // - 'storagetype' is provided as a parameter to the constructor rather than via setParam() later. 127 // - $app->stop() is not called at the end of the script (which would close the PDO connection before the shutdown function runs). 128 if ('database' == $this->getParam('storagetype')) { 129 register_shutdown_function(array($this, 'save')); 130 } 127 131 } 128 132 } … … 138 142 { 139 143 $app =& App::getInstance(); 140 $ db =& DB::getInstance();144 $pdo =& \Strangecode\Codebase\PDO::getInstance(); 141 145 142 146 static $_db_tested = false; … … 144 148 if ($recreate_db || !$_db_tested && $this->getParam('create_table')) { 145 149 if ($recreate_db) { 146 $ db->query("DROP TABLE IF EXISTS " . $this->getParam('db_table'));150 $pdo->query(sprintf("DROP TABLE IF EXISTS `%s`", $pdo->sanitizeIdentifier($this->getParam('db_table')))); 147 151 $app->logMsg(sprintf('Dropping and recreating table %s.', $this->getParam('db_table')), LOG_INFO, __FILE__, __LINE__); 148 152 } 149 $db->query("CREATE TABLE IF NOT EXISTS " . $db->escapeString($this->getParam('db_table')) . " ( 150 user_id VARCHAR(32) NOT NULL DEFAULT '', 151 pref_namespace VARCHAR(32) NOT NULL DEFAULT '', 152 pref_key VARCHAR(64) NOT NULL DEFAULT '', 153 pref_value TEXT, 154 PRIMARY KEY (user_id, pref_namespace, pref_key) 155 )"); 156 157 if (!$db->columnExists($this->getParam('db_table'), array( 153 $stmt = $pdo->query(sprintf(" 154 CREATE TABLE IF NOT EXISTS `%s` ( 155 `user_id` VARCHAR(32) NOT NULL DEFAULT '', 156 `pref_namespace` VARCHAR(32) NOT NULL DEFAULT '', 157 `pref_key` VARCHAR(64) NOT NULL DEFAULT '', 158 `pref_value` TEXT, 159 PRIMARY KEY (`user_id`, `pref_namespace`, `pref_key`) 160 ) 161 ", $pdo->sanitizeIdentifier($this->getParam('db_table')))); 162 163 if (!$pdo->columnExists($this->getParam('db_table'), array( 158 164 'user_id', 159 165 'pref_namespace', … … 328 334 || (isset($_SESSION['_prefs'][$this->_ns]['saved']) && array_key_exists($key, $_SESSION['_prefs'][$this->_ns]['saved']))) { 329 335 $_SESSION['_prefs'][$this->_ns]['saved'][$key] = $val; 330 $app->logMsg(sprintf('Setting session preference %s => %s', $key, getDump($val, true)), LOG_DEBUG, __FILE__, __LINE__);336 $app->logMsg(sprintf('Setting session/database preference %s => %s', $key, getDump($val, true)), LOG_DEBUG, __FILE__, __LINE__); 331 337 } else { 332 $app->logMsg(sprintf('Not setting session preference %s => %s', $key, getDump($val, true)), LOG_DEBUG, __FILE__, __LINE__);338 $app->logMsg(sprintf('Not setting session/database preference %s => %s', $key, getDump($val, true)), LOG_DEBUG, __FILE__, __LINE__); 333 339 } 334 340 break; … … 499 505 { 500 506 $app =& App::getInstance(); 501 $ db =& DB::getInstance();507 $pdo =& \Strangecode\Codebase\PDO::getInstance(); 502 508 503 509 // Skip this method if not using the db. … … 526 532 527 533 // Retrieve all prefs for this user and namespace. 528 $qid = $db->query(" 529 SELECT pref_key, pref_value 530 FROM " . $db->escapeString($this->getParam('db_table')) . " 531 WHERE user_id = '" . $db->escapeString($this->getParam('user_id')) . "' 532 AND pref_namespace = '" . $db->escapeString($this->getParam('namespace')) . "' 533 LIMIT 10000 534 "); 535 while (list($key, $val) = mysql_fetch_row($qid)) { 534 $stmt = $pdo->prepare(sprintf("SELECT `pref_key`, `pref_value` FROM `%s` WHERE `user_id` = ? AND `pref_namespace` = ? LIMIT 100000", $pdo->sanitizeIdentifier($this->getParam('db_table')))); 535 $stmt->execute([$this->getParam('user_id'), $this->getParam('namespace')]); 536 while (list($key, $val) = $stmt->fetch(\PDO::FETCH_NUM)) { 536 537 $_SESSION['_prefs'][$this->_ns]['saved'][$key] = unserialize($val); 537 538 } 538 539 539 $app->logMsg(sprintf('Loaded %s prefs from database.', mysql_num_rows($qid)), LOG_DEBUG, __FILE__, __LINE__);540 $app->logMsg(sprintf('Loaded %s prefs from database.', sizeof($_SESSION['_prefs'][$this->_ns]['saved'])), LOG_DEBUG, __FILE__, __LINE__); 540 541 541 542 // Data loaded only once per session. … … 587 588 { 588 589 $app =& App::getInstance(); 589 $ db =& DB::getInstance();590 $pdo =& \Strangecode\Codebase\PDO::getInstance(); 590 591 591 592 // Skip this method if not using the db. … … 607 608 if (isset($_SESSION['_prefs'][$this->_ns]['saved']) && is_array($_SESSION['_prefs'][$this->_ns]['saved']) && ($allow_empty || !empty($_SESSION['_prefs'][$this->_ns]['saved']))) { 608 609 // Delete old prefs from database. 609 $db->query(" 610 DELETE FROM " . $db->escapeString($this->getParam('db_table')) . " 611 WHERE user_id = '" . $db->escapeString($this->getParam('user_id')) . "' 612 AND pref_namespace = '" . $db->escapeString($this->getParam('namespace')) . "' 613 "); 610 $stmt = $pdo->prepare(sprintf("DELETE FROM `%s` WHERE `user_id` = ? AND `pref_namespace` = ?", $pdo->sanitizeIdentifier($this->getParam('db_table')))); 611 $stmt->execute([$this->getParam('user_id'), $this->getParam('namespace')]); 614 612 615 613 // Insert new prefs. 616 614 $insert_values = array(); 617 615 foreach ($_SESSION['_prefs'][$this->_ns]['saved'] as $key => $val) { 618 $insert_values[] = sprintf("( '%s', '%s', '%s', '%s')",619 $ db->escapeString($this->getParam('user_id')),620 $ db->escapeString($this->getParam('namespace')),621 $ db->escapeString($key),622 $ db->escapeString(serialize($val))616 $insert_values[] = sprintf("(%s, %s, %s, %s)", 617 $pdo->quote($this->getParam('user_id')), 618 $pdo->quote($this->getParam('namespace')), 619 $pdo->quote($key), 620 $pdo->quote(serialize($val)) 623 621 ); 624 622 } 625 623 if (!empty($insert_values)) { 626 // TODO: after MySQL 5.0.23 is released this query could benefit from INSERT DELAYED.627 $db->query("628 INSERT INTO " . $db->escapeString($this->getParam('db_table')) . "629 (user_id, pref_namespace, pref_key, pref_value)630 VALUES " . join(', ', $insert_values) . "631 " );624 $stmt = $pdo->query(sprintf(" 625 INSERT INTO `%s` 626 (`user_id`, `pref_namespace`, `pref_key`, `pref_value`) 627 VALUES 628 %s 629 ", $pdo->sanitizeIdentifier($this->getParam('db_table')), join(', ', $insert_values))); 632 630 $app->logMsg(sprintf('Saved %s prefs to database for user_id %s.', sizeof($insert_values), $this->getParam('user_id')), LOG_DEBUG, __FILE__, __LINE__); 633 631 }
Note: See TracChangeset
for help on using the changeset viewer.