Skip to content

Commit

Permalink
finish version of Spring-Festival ,
Browse files Browse the repository at this point in the history
add Mysql::escape()
fix PoolPDOStatement sql error Exception,
fix PoolPDOStatement fetch() error,
change: yii2-swoole depend on swoole2.1 now.
  • Loading branch information
lizhenju committed Feb 12, 2018
1 parent f9be45e commit a0da017
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 45 deletions.
15 changes: 12 additions & 3 deletions db/mysql/PoolPDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public function doQuery($sql,$isExecute = false, $method, $fetchMode)
/**
* @param $data
* @param $fetchMode
* @deprecated it instead by PDOStatement
* @return bool
*/
protected function fetch($data,$fetchMode)
Expand All @@ -126,6 +127,7 @@ protected function fetch($data,$fetchMode)
/**
* @param $data
* @param $fetchMode
* @deprecated it instead by PDOStatement
* @return array
*/
protected function fetchAll($data,$fetchMode)
Expand All @@ -144,6 +146,7 @@ protected function fetchAll($data,$fetchMode)
/**
* @param $data
* @param $fetchMode
* @deprecated it instead by PDOStatement
* @return mixed|null
*/
protected function fetchColumn($data,$fetchMode)
Expand All @@ -163,6 +166,11 @@ public function lastInsertId($name = null)
return $this->_lastInsertId;
}

public function setLastInsertId($value)
{
return $this->_lastInsertId = $value;
}

/**
* @param int $attribute
* @return mixed|null
Expand Down Expand Up @@ -194,7 +202,7 @@ public function quote($string, $parameter_type = PDO::PARAM_STR)
if ($parameter_type !== PDO::PARAM_STR) {
throw new PDOException('Only PDO::PARAM_STR is currently implemented for the $parameter_type of MysqlPoolPdo::quote()');
}
return "'" . str_replace("'", "''", $string) . "'";
return $this->pool->escape($string);
}

/**
Expand Down Expand Up @@ -242,6 +250,7 @@ public function commit()
}
$ret = $this->pool->commit($this->_bingId);
$this->_bingId = null;
$this->_isTransaction = false;
return $ret;
}
/**
Expand All @@ -256,6 +265,7 @@ public function rollBack()
}
$ret = $this->pool->rollBack($this->_bingId);
$this->_bingId = null;
$this->_isTransaction = false;
return $ret;
}

Expand All @@ -264,8 +274,7 @@ public function onError($event)
if($this->_bingId === null){
return ;
}
$this->pool->rollBack($this->_bingId);
$this->_bingId = null;
$this->rollBack();
}

/**
Expand Down
116 changes: 78 additions & 38 deletions db/mysql/PoolPDOStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,37 @@
*/
class PoolPDOStatement extends PDOStatement
{
protected $_sth;
protected $statement;

/**
* PDO Oci8 driver
*
* @var PoolPDO
*/
protected $_pdo;
protected $pdo;

/**
* Statement options
*
* @var array
*/
protected $_options = array();
protected $options = array();

/**
* Default fetch mode for this statement
* @var integer
*/
protected $_fetchMode = null;
private $_fetchMode = null;

/**
* @var ResultData
*/
protected $_result = null;
private $_resultData = null;


protected $_boundColumns = [];
private $_boundColumns = [];

private $_index = 0;

/**
* PoolPDOStatement constructor.
Expand All @@ -54,11 +56,20 @@ class PoolPDOStatement extends PDOStatement
*/
public function __construct(string $statement,PoolPDO $pdo, $options)
{
$this->_sth = $statement;
$this->_pdo = $pdo;
$this->_options = $options;
$this->statement = $statement;
$this->pdo = $pdo;
$this->options = $options;
}

/**
* @inheritdoc
*/
function __destruct()
{
unset($this->_resultData,$this->_boundColumns,$this->statement);
}


/**
* Executes a prepared statement
*
Expand All @@ -68,26 +79,33 @@ public function __construct(string $statement,PoolPDO $pdo, $options)
*/
public function execute($inputParams = [])
{
$bingID = $this->_pdo->getBingId();
$pool = $this->_pdo->pool;
$bingID = $this->pdo->getBingId();
$pool = $this->pdo->pool;
if(!empty($inputParams)){
$this->_boundColumns = $inputParams;
}
if(!empty($this->_boundColumns)){
$this->prepareParamName();
$this->_result = $pool->prepareAndExecute($this->_sth,$this->_boundColumns,$bingID);
$this->_resultData = $pool->prepareAndExecute($this->statement,$this->_boundColumns,$bingID);
$this->pdo->setLastInsertId($this->_resultData->insert_id);
}else{
$this->_result = $pool->doQuery($this->_sth,$bingID);
$this->_resultData = $pool->doQuery($this->statement,$bingID);
}
if($this->_resultData->result === false){
throw new PDOException($this->_resultData->error);
}
return true;
}

public function prepareParamName()
{
$statement = $this->_sth;
$statement = $this->statement;
if (strpos($statement, ':') !== false) {
$data = [];
$this->_sth = preg_replace_callback('/:\w+\b/u', function ($matches) use (&$data) {
$this->statement = preg_replace_callback('/:\w+\b/u', function ($matches) use (&$data) {
if(!isset($this->_boundColumns[$matches[0]])){
return $matches[0];
}
$data[] = $this->_boundColumns[$matches[0]];
return '?';
}, $statement);
Expand All @@ -112,17 +130,37 @@ public function fetch(
$cursor_offset = 0
)
{
static $styleUnsupport = [
'PDO::FETCH_BOUND' => PDO::FETCH_BOUND,
'PDO::FETCH_CLASS' => PDO::FETCH_CLASS,
'PDO::FETCH_INTO' => PDO::FETCH_INTO,
'PDO::FETCH_LAZY' => PDO::FETCH_LAZY,
'PDO::FETCH_NAMED' => PDO::FETCH_NAMED,
'PDO::FETCH_OBJ' => PDO::FETCH_OBJ,
];
if ($cursor_orientation !== PDO::FETCH_ORI_NEXT || $cursor_offset !== 0) {
throw new PDOException('$cursor_orientation that is not PDO::FETCH_ORI_NEXT is not implemented for PoolPDOStatement::fetch()');
}

if($fetch_style == PDO::FETCH_CLASS) {
throw new PDOException('PDO::FETCH_CLASS is not implemented for PoolPDOStatement::fetch()');
if(in_array($fetch_style,$styleUnsupport)) {
throw new PDOException(array_search($fetch_style,$styleUnsupport).'is not implemented for PoolPDOStatement::fetch()');
}
if(empty($this->_result)){

if(
empty($this->_resultData)
|| empty($result = $this->_resultData->result)
|| empty($data = $result[$this->_index++] ?? [])
){
return false;
}
return $this->_result->result[0];

if($fetch_style == PDO::FETCH_NUM){
$data = array_values($data);
}elseif ($fetch_style == PDO::FETCH_BOTH){
$dataRows = array_values($data);
$data = array_merge($data,$dataRows);
}

return $data;
}

/**
Expand Down Expand Up @@ -199,10 +237,10 @@ public function bindValue(
*/
public function rowCount()
{
if(empty($this->_result)){
if(empty($this->_resultData)){
return 0;
}
return $this->_result->affected_rows;
return $this->_resultData->affected_rows;
}

/**
Expand All @@ -214,10 +252,11 @@ public function rowCount()
*/
public function fetchColumn($colNumber = 0)
{
if( empty($this->_result->result[$colNumber]) ){
$result = $this->fetch(PDO::FETCH_NUM);
if ($result === false) {
return false;
}
return array_shift($this->_result->result[$colNumber]);
return $result[$colNumber] ?? false;
}

/**
Expand All @@ -235,15 +274,16 @@ public function fetchAll(
$ctor_args = null
)
{
if(empty($this->_result)){
if(empty($this->_resultData) || empty($this->_resultData->result)){
return [];
}
if($fetch_style == PDO::FETCH_COLUMN){
$keys = array_keys($this->_result->result[0]);
$keys = array_keys($this->_resultData->result[0]);
$key = array_shift($keys);
return ArrayHelper::getColumn((array)$this->_result->result,$key);
unset($keys);
return ArrayHelper::getColumn((array)$this->_resultData->result,$key);
}
return $this->_result->result;
return $this->_resultData->result;
}

/**
Expand Down Expand Up @@ -271,7 +311,7 @@ public function fetchObject($className = 'stdClass', $ctor_args = null)
*/
public function errorCode()
{
return $this->_result->errno;
return $this->_resultData->errno;
}

/**
Expand All @@ -281,11 +321,11 @@ public function errorCode()
*/
public function errorInfo()
{
if ($this->_result->errno) {
if ($this->_resultData->errno) {
return array(
'HY000',
$this->_result->errno,
$this->_result->error
$this->_resultData->errno,
$this->_resultData->error
);
}

Expand All @@ -302,7 +342,7 @@ public function errorInfo()
*/
public function setAttribute($attribute, $value)
{
$this->_options[$attribute] = $value;
$this->options[$attribute] = $value;
return true;
}

Expand All @@ -315,8 +355,8 @@ public function setAttribute($attribute, $value)
*/
public function getAttribute($attribute)
{
if (isset($this->_options[$attribute])) {
return $this->_options[$attribute];
if (isset($this->options[$attribute])) {
return $this->options[$attribute];
}
return null;
}
Expand All @@ -328,11 +368,11 @@ public function getAttribute($attribute)
*/
public function columnCount()
{
if(empty($this->_result) || empty($this->_result->result)){
if(empty($this->_resultData) || empty($this->_resultData->result)){
return 0;
}

return count(@$this->_result->result[0]);
return count(@$this->_resultData->result[0]);
}

public function getColumnMeta($column)
Expand Down Expand Up @@ -382,7 +422,7 @@ public function nextRowset()
*/
public function closeCursor()
{
unset($this->_result);
unset($this->_resultData);
return true;
}

Expand Down
Loading

0 comments on commit a0da017

Please sign in to comment.