Skip to content

Commit

Permalink
增加3种事务方法begin/commit/rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
hetao29 committed Nov 8, 2024
1 parent b33f6b3 commit 4335c0f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
13 changes: 11 additions & 2 deletions plugins/db/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,17 @@ private function __query($sql, $retry=false, $params=[]){
* @param string $sql
* @return boolean|int|array
*/
public function execute($sql){
return $this->__query($sql);
public function execute($sql,$params=[]){
return $this->__query($sql, false, $params);
}
public function begin(){
return $this->engine->begin();
}
public function commit(){
return $this->engine->commit();
}
public function rollback(){
return $this->engine->rollback();
}

private function __quote($condition,$split="AND",&$params=[]){
Expand Down
18 changes: 18 additions & 0 deletions plugins/db/DbMysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ public function error(){
if(!$this->_stmt)return false;
return $this->_stmt->error;
}
public function begin(){
if($this->_mysqli->connect_errno){
return false;
}
return $this->_mysqli->begin_transaction();
}
public function commit(){
if($this->_mysqli->connect_errno){
return false;
}
return $this->_mysqli->commit();
}
public function rollback(){
if($this->_mysqli->connect_errno){
return false;
}
return $this->_mysqli->rollback();
}
public function errno(){
$error=0;
if($this->_mysqli->connect_errno){
Expand Down
14 changes: 14 additions & 0 deletions plugins/db/DbPDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public function query($sql,$params=[]){
$this->_stmt = $this->_pdo->prepare($sql);
if($this->_stmt){
return $this->_stmt->execute($params);
}else{
trigger_error("CONNECT DATABASE ERROR ( ".implode("\n",$this->_pdo->errorInfo())." ) ",E_USER_WARNING);
}
return false;
}
Expand All @@ -101,6 +103,18 @@ public function count(){
if(!$this->_stmt)return false;
return $this->_stmt->rowCount();
}
public function begin(){
if(!$this->_pdo)return false;
return $this->_pdo->beginTransaction();
}
public function commit(){
if(!$this->_pdo)return false;
return $this->_pdo->commit();
}
public function rollback(){
if(!$this->_pdo)return false;
return $this->_pdo->rollBack();
}
public function escape($str){
if(!$this->_pdo)return false;
return trim($this->_pdo->quote($str),"'");
Expand Down

0 comments on commit 4335c0f

Please sign in to comment.