本文整理匯總了PHP中PDOException::getCode方法的典型用法代碼示例。如果您正苦於以下問題:PHP PDOException::getCode方法的具體用法?PHP PDOException::getCode怎麽用?PHP PDOException::getCode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類PDOException
的用法示例。
在下文中一共展示了PDOException::getCode方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: handleError
public function handleError($model, $query, \PDOException $e)
{
// echo "*** Model in handleError: " . $model . "\n";
switch ($e->getCode()) {
// MySQL table missing
case '42S02':
// SQLite table missing
// SQLite table missing
case 'HY000' && stripos($e->getMessage(), "no such table") !== false:
if ($model != 'StdClass') {
$instance = new $model();
if ($instance instanceof ActiveRecord) {
$table_builder = new TableBuilder($instance);
$table_builder->build();
return $this->query($query, $model);
// Re-run the query
}
}
throw new DatabaseLayer\TableDoesntExistException($e->getCode() . ": " . $e->getMessage());
default:
// Write exception to log.
if (DatabaseLayer::getInstance()->getLogger()) {
DatabaseLayer::getInstance()->getLogger()->addError("Active Record Exception in " . $model . "\n\n" . $e->getCode() . ": " . $e->getMessage() . "\n\nrunning:\n\n{$query}");
}
throw new DatabaseLayer\Exception($e->getCode() . ": " . $e->getMessage() . ".\n\n" . $query);
}
}
示例2: __construct
/**
* Constructor.
*
* @param \PDOException $exception The PDO exception to wrap.
*/
public function __construct(\PDOException $exception)
{
parent::__construct($exception->getMessage(), 0, $exception->getPrevious());
$this->code = $exception->getCode();
$this->errorInfo = $exception->errorInfo;
$this->errorCode = isset($exception->errorInfo[1]) ? $exception->errorInfo[1] : $exception->getCode();
$this->sqlState = isset($exception->errorInfo[0]) ? $exception->errorInfo[0] : $exception->getCode();
}
示例3: createFromPDOException
/**
* @param \PDOException $exception
* @return MySqlException
*/
public static function createFromPDOException(\PDOException $exception)
{
$message = $exception->getMessage();
$codePosition = strpos($message, "[{$exception->getCode()}]");
if ($codePosition !== false) {
$message = trim(substr($message, $codePosition + strlen("[{$exception->getCode()}]") + 1));
}
return new MySqlException($message, $exception->getCode());
}
示例4: throwExceptionWithInfo
/**
* Throw DbException with query info
*
* @param string $sql
* @param array $params
* @param \PDOException $e
*
* @throws DbException
*/
protected function throwExceptionWithInfo($sql, array $params, \PDOException $e)
{
$exception = new DbException($e->getMessage(), (int) $e->getCode(), $e);
$exception->setSql($sql);
$exception->setParams($params);
throw $exception;
}
示例5: __construct
public function __construct(\PDOException $e, $extraMessage = '')
{
// Strip boring unnecessary info from error message
$strippedMsg = preg_replace('/SQLSTATE\\[[A-Za-z-0-9]+\\]( \\[[A-Za-z-0-9]+\\])?:?\\s?/', '', $e->getMessage());
// PDOExceptions' getCode() can return a code with letters, which normal
// exceptions won't accept. A converted code is better than no code at all though.
parent::__construct($strippedMsg . $extraMessage, (int) $e->getCode());
}
示例6: __construct
/**
* Create a new query excetion instance.
*
* @param string $sql
* @param array $bindings
* @param \PDOException $previous
* @return void
*/
public function __construct($sql, array $bindings, $previous)
{
$this->sql = $sql;
$this->bindings = $bindings;
$this->previous = $previous;
$this->code = $previous->getCode();
$this->errorInfo = $previous->errorInfo;
$this->message = $this->formatMessage($sql, $bindings, $previous);
}
示例7: throwException
/**
* @param PDOException $exception
*/
public function throwException(PDOException $exception)
{
if ($this->debug) {
echo '<pre>';
print_r(array($exception->getMessage(), $exception->getCode()));
echo '</pre>';
die;
}
}
示例8: testConstruct
/**
* @covers Veles\DataBase\Exceptions\DbException::__construct
*
* @param string $message
* @param string $ansi_code
* @param int $code
* @param \PDOException $exception
*
* @dataProvider constructProvider
*/
public function testConstruct($message, $ansi_code, $code, $exception)
{
$obj = new DbException($exception->getMessage(), (int) $exception->getCode(), $exception);
$result = $obj->getMessage();
$msg = 'Wrong DbException::__construct() behavior!';
$this->assertSame($message, $result, $msg);
$result = $obj->getAnsiCode();
$msg = 'Wrong DbException::__construct() behavior!';
$this->assertSame($ansi_code, $result, $msg);
$result = $obj->getCode();
$msg = 'Wrong DbException::__construct() behavior!';
$this->assertSame($code, $result, $msg);
}
示例9: raw_query
/**
* Run RAW Query
*
* @param string $sql
*
* @return Zend_Db_Statement_Interface
* @throws PDOException
*/
public function raw_query($sql)
{
try {
return $this->query($sql);
} catch (Zend_Db_Statement_Exception $e) {
// Convert to PDOException to maintain backwards compatibility with usage of MySQL adapter
$e = $e->getPrevious();
if (!$e instanceof PDOException) {
$e = new PDOException($e->getMessage(), $e->getCode());
}
throw $e;
}
}
示例10: __construct
/**
* Constructor.
*
* @param \PDOException $e
*/
public function __construct(\PDOException $e)
{
if (strstr($e->getMessage(), 'SQLSTATE[')) {
preg_match('/SQLSTATE\\[(\\w+)\\] \\[(\\w+)\\] (.*)/', $e->getMessage(), $matches);
if (count($matches)) {
$this->code = $matches[1] == 'HT000' ? $matches[2] : $matches[1];
$this->message = $matches[3];
} else {
$this->code = $e->getCode();
$this->message = $e->getMessage();
}
}
}
示例11: handlePDOException
public function handlePDOException(PDOException $e)
{
trigger_error('PHP PDO Error in ' . $e->getFile() . ' @' . strval($e->getLine()) . ' [' . strval($e->getCode()) . '] :: ' . $e->getMessage(), E_USER_WARNING);
foreach ($e->getTrace() as $a => $b) {
foreach ($b as $c => $d) {
if ($c == 'args') {
foreach ($d as $e => $f) {
trigger_error('PHP PDO Error trace: ' . strval($a) . '# args: ' . $e . ': ' . $f . '', E_USER_WARNING);
}
} else {
trigger_error('PHP PDO Error trace: ' . strval($a) . '# ' . $c . ': ' . $d . '', E_USER_WARNING);
}
}
}
}
示例12: __construct
/**
* Constructor
*
* @param \PDOException $e PDO exception
* @param string $query SQL query OPTIONAL
* @param array $params SQL query parameters OPTIONAL
*
* @return void
*/
public function __construct(\PDOException $e, $query = null, array $params = array())
{
$code = $e->getCode();
$message = $e->getMessage();
// Remove user credentials
if (strstr($message, 'SQLSTATE[') && preg_match('/SQLSTATE\\[(\\w+)\\] \\[(\\w+)\\] (.*)/', $message, $matches)) {
$code = 'HT000' == $matches[1] ? $matches[2] : $matches[1];
$message = $matches[3];
}
// Add additional information
if ($query) {
$message .= PHP_EOL . 'SQL query: ' . $query;
}
if ($params) {
$message .= PHP_EOL . 'SQL query parameters: ' . var_export($params, true);
}
$this->code = intval($code);
$this->message = $message;
}
示例13: generateError
/**
* Writes a ditailed error message to the log file, if specified.
*
* @param \PDOException $e
* @param string $strMessage
* @param string $strSql
* @return void
*/
private function generateError(\PDOException $e, $strMessage, $strSql = '')
{
$strError = PHP_EOL . "\t-- " . $strMessage . PHP_EOL . "\t-- PDOException code: " . $e->getCode() . PHP_EOL . "\t-- File: " . $e->getFile() . PHP_EOL . "\t-- Line: " . $e->getLine() . PHP_EOL . "\t-- Message: " . $e->getMessage() . (empty($strSql) ? '' : PHP_EOL . "\t-- SQL: " . $strSql . PHP_EOL) . PHP_EOL . "\t-------------------------------------------------------" . PHP_EOL . PHP_EOL;
$this->log($strError, true);
if (!empty($this->strWriteErrorLogTo)) {
if (is_resource($this->resourceErrorLog)) {
fwrite($this->resourceErrorLog, $strError);
} else {
$this->resourceErrorLog = fopen($this->strWriteErrorLogTo, 'a');
if (is_resource($this->resourceErrorLog)) {
fwrite($this->resourceErrorLog, $strError);
}
}
}
unset($strError);
}
示例14: convertPostgresException
/**
* @link http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html
*/
protected function convertPostgresException(\PDOException $e, \Exception $root)
{
$state = (string) isset($e->errorInfo[0]) ? $e->errorInfo[0] : $e->getCode();
switch ($state) {
case '0A000':
if (strpos($e->getMessage(), 'truncate') !== false) {
return new ForeignKeyConstraintViolationException($root->getMessage(), 0, $root);
}
break;
case '23503':
return new ForeignKeyConstraintViolationException($root->getMessage(), 0, $root);
case '23505':
return new UniqueConstraintViolationException($root->getMessage(), 0, $root);
}
return new DatabaseException($root->getMessage(), 0, $root);
}
示例15: db_error
/**
* 拋出錯誤信息
*
* @param PDOException $e
*/
private function db_error(PDOException $e)
{
echo '出錯了!!!<br />';
echo '錯誤文件:', $e->getFile(), '<br />';
echo '錯誤行號:', $e->getLine(), '<br />';
echo '錯誤編碼:', $e->getCode(), '<br />';
echo '錯誤詳情:', $e->getMessage(), '<br />';
exit('END...');
}