1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212:
<?php
namespace MvcCore\Ext\Models\Db;
class Reader implements \MvcCore\Ext\Models\Db\IReader {
protected $statement;
protected $providerExecResult = NULL;
protected $rawData = NULL;
protected $stmntOpenedProp = NULL;
public function __construct (\MvcCore\Ext\Models\Db\Statement $statement) {
$this->statement = $statement;
$this->stmntOpenedProp = new \ReflectionProperty($statement, 'opened');
$this->stmntOpenedProp->setAccessible(TRUE);
}
public function GetExecResult () {
if ($this->providerExecResult === NULL)
$this->providerInvokeExecute();
return $this->providerExecResult;
}
public function GetRawData () {
return $this->rawData;
}
public function GetStatement () {
return $this->statement;
}
protected function & fetchRawData ($singleRow, $fetchMode = \PDO::FETCH_ASSOC) {
if ($this->providerExecResult === NULL && $this->statement->IsOpened() !== FALSE)
$this->providerInvokeExecute();
$statement = $this->statement->GetProviderStatement();
if ($singleRow) {
$this->rawData = $statement->fetch($fetchMode);
} else {
$this->rawData = $statement->fetchAll($fetchMode);
}
$autoCloseStatement = in_array(
\MvcCore\Ext\Models\Db\IStatement::AUTO_CLOSE,
$this->statement->GetDriverOptions()
);
if ($autoCloseStatement)
$this->statement->Close();
return $this->rawData;
}
protected function providerInvokeExecute () {
if ($this->providerExecResult !== NULL) return $this;
$params = $this->statement->GetParams();
$providerStatement = $this->statement->GetProviderStatement();
$exception = NULL;
$dbErrorMsg = NULL;
try {
set_error_handler(function ($phpErrLevel, $errMessage) use (& $dbErrorMsg) {
$dbErrorMsg = $errMessage;
});
$this->providerExecResult = $providerStatement->execute($params);
$this->stmntOpenedProp->setValue($this->statement, TRUE);
restore_error_handler();
if (!$this->providerExecResult) {
$errInfo = $providerStatement->errorInfo();
throw new \Exception($errInfo[2] ?: $dbErrorMsg, intval($errInfo[0]));
}
} catch (\Exception $e) {
$exception = \MvcCore\Ext\Models\Db\Exception::Create($e)
->setQuery($providerStatement->queryString)
->setParams($params);
$this->providerExecResult = FALSE;
} catch (\Throwable $e) {
$exception = \MvcCore\Ext\Models\Db\Exception::Create($e)
->setQuery($providerStatement->queryString)
->setParams($params);
$this->providerExecResult = FALSE;
}
if ($this->providerExecResult === FALSE) {
$connection = $this->statement->GetConnection();
$testConnType = new \ReflectionClass($connection);
$retryConnectMethod = $testConnType->getMethod('reConnectIfNecessaryOrThrownError');
$retryConnectMethod->setAccessible(TRUE);
$providerConn = $retryConnectMethod->invokeArgs(
$connection, [$exception]
);
$firstWord = $this->getSqlCommandFirstWord($providerStatement->queryString);
if ($firstWord === 'select') {
$providerStatement = $providerConn->prepare($providerStatement->queryString . ';');
$statementType = new \ReflectionClass($this->statement);
$provStatementProp = $statementType->getProperty('providerStatement');
$provStatementProp->setAccessible(TRUE);
$provStatementProp->setValue($this->statement, $providerStatement);
return $this->providerInvokeExecute();
}
}
return $this;
}
protected function cleanUpData () {
$this->rawData = NULL;
}
protected function getSqlCommandFirstWord ($sql) {
$sqlTrimmed = trim($sql, "; \t\n\r\0\x0B");
preg_match("#\s#", $sqlTrimmed, $matches, PREG_OFFSET_CAPTURE);
if ($matches && $matches[0]) {
$firstWhiteSpacePos = $matches[0][1];
$firstWord = mb_strtolower(mb_substr($sqlTrimmed, 0, $firstWhiteSpacePos));
return $firstWord;
}
return $sql;
}
}