From 883f8f6d0c8420b2ddae46b2345faa86e90a0230 Mon Sep 17 00:00:00 2001 From: Konrad Rosenbaum Date: Tue, 10 Jul 2012 19:38:04 +0200 Subject: [PATCH] allow debugging in PHP transactions: outputs XML comments --- phpbase/db.php | 10 +++++-- phpbase/db_mysql.php | 19 +++++++++++++-- phpbase/exception.php | 4 ++- phpbase/transaction.php | 58 ++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 83 insertions(+), 8 deletions(-) diff --git a/phpbase/db.php b/phpbase/db.php index fc6d2c4..3587a8c 100644 --- a/phpbase/db.php +++ b/phpbase/db.php @@ -163,6 +163,7 @@ abstract class DbEngine } $ret.=$this->createTableExtras($tablename,$table); $ret.=")"; + WobTransaction::debug("Create Table Stmt: ".$ret,WobTransaction::DebugDbStatement); return $ret; } @@ -190,6 +191,7 @@ abstract class DbEngine $query="SELECT $cols FROM ".$this->tableName($table); if($where!="")$query.=" WHERE ".$where; if($orderby!="")$query.=" ".$orderby; + WobTransaction::debug("DB Select Stmt: ".$query,WobTransaction::DebugDbStatement); return $query; } @@ -224,14 +226,16 @@ abstract class DbEngine $val.="NULL"; } $ret.=$val.")"; -// print $ret; + WobTransaction::debug("DB Insert Stmt: ".$ret,WobTransaction::DebugDbStatement); return $ret; } /**creates a SQL92 statement for deletes; overwrite this to implement DB specific syntax*/ protected function sqlDelete($table,$where) { - return "DELETE FROM ".$this->tableName($table)." WHERE ".$where; + $ret="DELETE FROM ".$this->tableName($table)." WHERE ".$where; + WobTransaction::debug("DB Delete Stmt: ".$ret,WobTransaction::DebugDbStatement); + return $ret; } /**creates a SQL92 statement for updates; overwrite this to implement DB specific syntax*/ @@ -264,7 +268,7 @@ abstract class DbEngine $ret.="NULL"; } $ret.=" WHERE ".$where; - //print $ret; + WobTransaction::debug("DB Update Stmt: ".$ret,WobTransaction::DebugDbStatement); return $ret; } diff --git a/phpbase/db_mysql.php b/phpbase/db_mysql.php index a36a6a4..a182b4e 100644 --- a/phpbase/db_mysql.php +++ b/phpbase/db_mysql.php @@ -94,6 +94,7 @@ class MysqlEngine extends DbEngine } public function beginTransaction() { + WobTransaction::debug("DB Begin Transaction",WobTransaction::DebugDbTransaction); $this->intrans=mysqli_query($this->dbhdl,$this->sqlBeginTransaction()); return $this->intrans; } @@ -101,6 +102,7 @@ class MysqlEngine extends DbEngine public function commitTransaction() { if(!$this->intrans)return false; + WobTransaction::debug("DB Commit Transaction",WobTransaction::DebugDbTransaction); //otherwise commit $this->intrans=false; return mysqli_query($this->dbhdl,$this->sqlCommitTransaction()); @@ -109,6 +111,7 @@ class MysqlEngine extends DbEngine public function rollbackTransaction() { $this->intrans=false; + WobTransaction::debug("DB RollBack Transaction",WobTransaction::DebugDbTransaction); //don't ask, just do (also catches implicit transactions) return mysqli_query($this->dbhdl,$this->sqlRollbackTransaction()); } @@ -125,11 +128,13 @@ class MysqlEngine extends DbEngine if($wl)$req.=" WRITE"; else $req.=" READ"; } + WobTransaction::debug("DB Lock: ".$req,WobTransaction::DebugDbQuery); mysqli_query($this->dbhdl,$req); } protected function unlockDB() { + WobTransaction::debug("DB Full Unlock",WobTransaction::DebugDbQuery); mysqli_query($this->dbhdl,"UNLOCK TABLES"); } @@ -145,6 +150,7 @@ class MysqlEngine extends DbEngine $query=$this->sqlSelect($table,$cols,$where,$orderby); if($this->transmode)$query.=" FOR UPDATE"; $res=mysqli_query($this->dbhdl,$query); + WobTransaction::debug("DB SELECT: ".$query."\n".($res===false?"failed":"successful"),WobTransaction::DebugDbQuery); if($res===false)return false; $nr=mysqli_num_rows($res); $ret=array(); @@ -158,6 +164,7 @@ class MysqlEngine extends DbEngine protected function createTable($tn,$t) { $sql=$this->sqlCreateTable($tn,$t); + WobTransaction::debug("DB create table: ".$sql,WobTransaction::DebugDbQuery); return mysqli_query($this->dbhdl,$sql); } @@ -223,7 +230,9 @@ class MysqlEngine extends DbEngine public function insert($table,array $values) { $this->transmode=true; - $res=mysqli_query($this->dbhdl,$this->sqlInsert($table,$values)); + $sql=$this->sqlInsert($table,$values); + $res=mysqli_query($this->dbhdl,$sql); + WobTransaction::debug("DB Insert: ".$sql."\n".($res===false?"failed":"successful"),WobTransaction::DebugDbQuery); if($res===false)return false; global $dbScheme; $seq=$dbScheme->hasSequence($table); @@ -246,7 +255,9 @@ class MysqlEngine extends DbEngine public function update($table,array $values,$where) { $this->transmode=true; - $res=mysqli_query($this->dbhdl,$this->sqlUpdate($table,$values,$where)); + $sql=$this->sqlUpdate($table,$values,$where); + $res=mysqli_query($this->dbhdl,$sql); + WobTransaction::debug("DB Update: ".$sql."\n".($res===false?"failed":"successful"),WobTransaction::DebugDbQuery); if($res!==false)return mysqli_affected_rows($this->dbhdl); else return false; } @@ -254,7 +265,9 @@ class MysqlEngine extends DbEngine public function deleteRows($table,$where) { $this->transmode=true; - $b=mysqli_query($this->dbhdl,$this->sqlDelete($table,$where)); + $sql=$this->sqlDelete($table,$where); + $b=mysqli_query($this->dbhdl,$sql); + WobTransaction::debug("DB Delete Rows: ".$sql."\n".($b===false?"failed":"successful"),WobTransaction::DebugDbQuery); // echo mysqli_error($this->dbhdl); if($b!==false)return mysqli_affected_rows($this->dbhdl); else return false; diff --git a/phpbase/exception.php b/phpbase/exception.php index 2b880a4..36653ba 100644 --- a/phpbase/exception.php +++ b/phpbase/exception.php @@ -28,10 +28,12 @@ class TransactionError extends Exception case WobTransactionBase::Soap12Encoding:$this->printXmlSchema();break; default: $this->printXmlWob();break; } + //print debug stuff (if anything left) + WobTransaction::printDebug(); } /**used by machine oriented transaction to print the proper XML representation of the error in Wob Encoding*/ - public function printXmlWob() + protected function printXmlWob() { header("X-WobResponse-Status: Error"); $xml=new DOMDocument; diff --git a/phpbase/transaction.php b/phpbase/transaction.php index bbfbf98..3ee5001 100644 --- a/phpbase/transaction.php +++ b/phpbase/transaction.php @@ -13,6 +13,29 @@ class WobTransactionBase { protected $aoutput; protected $toutput; protected $headers=array(); + static protected $debugstr=""; + static protected $debuglev=0; + + ///no debugging (used only in setDebugLevel()) + const DebugNone = 0; + ///activate all debug (used only in setDebugLevel()) + const DebugAll = 0x0ffffff; + ///flag: output the debug string immediately (risks losing headers, used only in debug()) + const DebugUrgent = 0x1000000; + ///debug database errors + const DebugDbError = 0x0000001; + ///debug database queries when they are executed + const DebugDbQuery = 0x0000002; + ///debug database statements when they are constructed + const DebugDbStatement = 0x0000004; + ///debug database transaction start/end + const DebugDbTransaction= 0x0000008; + ///output all database related actions (used only in setDebugLevel()) + const DebugDbAll = 0x00000ff; + ///debug transaction related actions + const DebugTransactions = 0x0000100; + ///miscellaneous debug output (default level) + const DebugMisc = 0x0800000; //these encoding constants must match the WocProcessor::MessageEncoding enum /**encode messages according to PACK standard - with minimal XML levels*/ @@ -34,6 +57,39 @@ class WobTransactionBase { ///returns the currently running instance of a transaction static public function getInstance(){return self::$instance;} + ///set the debug level of the server + static public function setDebugLevel($level) + { + self::$debuglev=$level & self::DebugAll; + } + + ///inserts a comment as debug statement + static public function debug($str,$level=self::DebugMisc) + { + if((self::$debuglev & $level) == 0)return; + + if(self::$debugstr!="")self::$debugstr.="\n\n"; + self::$debugstr.=$str; + + if($level & self::DebugUrgent)self::printDebug(); + } + + ///print debug comment now (called from xmlToString() and TransactionError + static public function printDebug() + { + if(self::$debugstr == "")return; + print(self::getDebug()); + } + + ///return debug comment now (called from xmlToString() and TransactionError + static public function getDebug() + { + if(self::$debugstr == "")return ""; + $ret="\n\n"; + self::$debugstr=""; + return $ret; + } + /**Wob message encoding: called to determine the correct transaction, aborts the script if there is none.*/ static public function getTransactionNameWob() { @@ -197,7 +253,7 @@ class WobTransactionBase { //put envelope into document $xml["doc"]->appendChild($env); } - return $xml["doc"]->saveXml(); + return $xml["doc"]->saveXml() . self::getDebug(); } }; -- 1.7.2.5