summaryrefslogtreecommitdiff
path: root/framework/Testing/Data/Analysis
diff options
context:
space:
mode:
Diffstat (limited to 'framework/Testing/Data/Analysis')
-rw-r--r--framework/Testing/Data/Analysis/TDbStatementAnalysis.php238
-rw-r--r--framework/Testing/Data/Analysis/TSimpleDbStatementAnalysis.php134
2 files changed, 372 insertions, 0 deletions
diff --git a/framework/Testing/Data/Analysis/TDbStatementAnalysis.php b/framework/Testing/Data/Analysis/TDbStatementAnalysis.php
new file mode 100644
index 00000000..46f9b745
--- /dev/null
+++ b/framework/Testing/Data/Analysis/TDbStatementAnalysis.php
@@ -0,0 +1,238 @@
+<?php
+/**
+ * IDbStatementAnalysis, TDbStatementAnalysisParameter,
+ * TDbStatementAnalysis, TDbStatementClassification file.
+ *
+ * @author Yves Berkholz <godzilla80[at]gmx[dot]net>
+ * @link http://www.pradosoft.com/
+ * @copyright Copyright &copy; 2005-2010 PradoSoft
+ * @license http://www.pradosoft.com/license/
+ * @version $Id$
+ * @package System.Testing.Data.Analysis
+ */
+
+ /**
+ * IDbStatementAnalysis interface
+ *
+ * @author Yves Berkholz <godzilla80[at]gmx[dot]net>
+ * @license http://www.pradosoft.com/license/
+ * @version $Id$
+ * @package System.Testing.Data.Analysis
+ * @since 4.0
+ */
+ interface IDbStatementAnalysis
+ {
+ /**
+ * @param TDbStatementAnalysisParamete
+ * @return TDbStatementClassification
+ */
+ public static function doClassificationAnalysis(TDbStatementAnalysisParameter $param);
+
+ /**
+ * @param TDbStatementAnalysisParameter
+ * @return TDbStatementClassification
+ */
+ public function getClassificationAnalysis(TDbStatementAnalysisParameter $param);
+
+ /**
+ * @param string PDO drivername of connection
+ */
+ public function setDriverName($value);
+
+ /**
+ * @return string PDO drivername of connection
+ */
+ public function getDriverName();
+ }
+
+ /**
+ * TDbStatementAnalysisParameter class
+ *
+ *
+ * @author Yves Berkholz <godzilla80[at]gmx[dot]net>
+ * @license http://www.pradosoft.com/license/
+ * @version $Id$
+ * @package System.Testing.Data.Analysis
+ * @since 4.0
+ */
+ class TDbStatementAnalysisParameter
+ {
+ /**
+ * @var string The SQL statement that should be analysed
+ */
+ protected $_statement = null;
+
+ /**
+ * TDbStatementClassification Defaults to 'UNKNOWN'
+ */
+ protected $_defaultClassification = TDbStatementClassification::UNKNOWN;
+
+ /**
+ * string|null PDO drivername of connection
+ */
+ protected $_drivername = null;
+
+ /**
+ * @param string The SQL statement that should be analysed
+ * @param TDbStatementClassification
+ * @param string|null PDO drivername of connection
+ */
+ public function __construct($statement='', $defaultClassification=null, $drivername=null)
+ {
+ $this->setStatement($statement);
+ $this->setDefaultClassification($defaultClassification);
+ $this->setDriverName($drivername);
+ }
+
+ /**
+ * @param string The SQL statement that should be analysed
+ */
+ public function setStatement($value)
+ {
+ $this->_statement = (string)$value;
+ }
+
+ /**
+ * @return string The SQL statement that should be analysed
+ */
+ public function getStatement()
+ {
+ return $this->_statement;
+ }
+
+ /**
+ * @param string|null PDO drivername of connection
+ */
+ public function setDriverName($value)
+ {
+ $this->_drivername = ($value===null) ? null : (string)$value;
+ }
+
+ /**
+ * @return string|null PDO drivername of connection
+ */
+ public function getDriverName()
+ {
+ return $this->_drivername;
+ }
+
+ /**
+ * @param TDbStatementClassification Defaults to 'UNKNOWN'
+ */
+ public function setDefaultClassification($value)
+ {
+ if($value!==null)
+ $this->_defaultClassification = (string)$value;
+ else
+ $this->_defaultClassification = TDbStatementClassification::UNKNOWN;
+ }
+
+ /**
+ * @return TDbStatementClassification
+ */
+ public function getDefaultClassification()
+ {
+ return $this->_defaultClassification;
+ }
+ }
+
+ /**
+ * TDbStatementAnalysis class
+ *
+ * Basic "dummy" implementation allways return {@link TDbStatementAnalysisParameter::getDefaultClassification DefaultClassification}
+ *
+ * @author Yves Berkholz <godzilla80[at]gmx[dot]net>
+ * @license http://www.pradosoft.com/license/
+ * @version $Id$
+ * @package System.Testing.Data.Analysis
+ * @since 4.0
+ */
+ class TDbStatementAnalysis implements IDbStatementAnalysis
+ {
+ /**
+ * @var string|null PDO drivername of connection
+ */
+ protected $_drivername = null;
+
+ /**
+ * @param string|null PDO drivername of connection
+ */
+ public function setDriverName($value)
+ {
+ $this->_drivername = ($value===null) ? null : (string)$value;
+ }
+
+ /**
+ * @return string|null PDO drivername of connection
+ */
+ public function getDriverName()
+ {
+ return $this->_drivername;
+ }
+
+ /**
+ * @param TDbStatementAnalysisParamete
+ * @return TDbStatementClassification
+ */
+ public static function doClassificationAnalysis(TDbStatementAnalysisParameter $param)
+ {
+ return $param->getDefaultClassification();
+ }
+
+ /**
+ * @param TDbStatementAnalysisParameter
+ * @return TDbStatementClassification
+ */
+ public function getClassificationAnalysis(TDbStatementAnalysisParameter $param)
+ {
+ return $param->getDefaultClassification();
+ }
+ }
+
+ /**
+ * TDbStatementClassification
+ *
+ * @author Yves Berkholz <godzilla80[at]gmx[dot]net>
+ * @license http://www.pradosoft.com/license/
+ * @version $Id$
+ * @package System.Testing.Data.Analysis
+ * @since 4.0
+ */
+ class TDbStatementClassification extends TEnumerable
+ {
+ /**
+ * Structured Query Language
+ */
+ const SQL = 'SQL';
+
+ /**
+ * Data Definition Language
+ */
+ const DDL = 'DDL';
+
+ /**
+ * Data Manipulation Language
+ */
+ const DML = 'DML';
+
+ /**
+ * Data Control Language
+ */
+ const DCL = 'DCL';
+
+ /**
+ * Transaction Control Language
+ */
+ const TCL = 'TCL';
+
+ /**
+ * classification depends on subsequent statement(s)
+ */
+ const CONTEXT = 'CONTEXT';
+
+ /**
+ * unable to detect real classification or multiple possibilities
+ */
+ const UNKNOWN = 'UNKNOWN';
+ }
+?>
diff --git a/framework/Testing/Data/Analysis/TSimpleDbStatementAnalysis.php b/framework/Testing/Data/Analysis/TSimpleDbStatementAnalysis.php
new file mode 100644
index 00000000..e4ab3391
--- /dev/null
+++ b/framework/Testing/Data/Analysis/TSimpleDbStatementAnalysis.php
@@ -0,0 +1,134 @@
+<?php
+/**
+ * TSimpleDbStatementAnalysis file.
+ *
+ * @author Yves Berkholz <godzilla80[at]gmx[dot]net>
+ * @link http://www.pradosoft.com/
+ * @copyright Copyright &copy; 2005-2010 PradoSoft
+ * @license http://www.pradosoft.com/license/
+ * @version $Id$
+ * @package System.Testing.Data.Analysis
+ */
+
+ Prado::using('System.Testing.Data.Analysis.TDbStatementAnalysis');
+
+ /**
+ * TSimpleDbStatementAnalysis class
+ *
+ * IMPORTANT!!!
+ * BETA Version - Use with care and NOT in production environment (only tested with MySql)
+ *
+ * @author Yves Berkholz <godzilla80[at]gmx[dot]net>
+ * @license http://www.pradosoft.com/license/
+ * @version $Id$
+ * @package System.Testing.Data.Analysis
+ * @since 4.0
+ * @todo SELECT * FOR UPDATE (row lock)
+ * @todo SELECT * INTO [ TEMPORARY | TEMP ] [ TABLE ] new_table (PostgreSQL)
+ * @todo mysql conditional commands in multiline comments e.g. / *! MySQL specific code * /
+ */
+ class TSimpleDbStatementAnalysis extends TDbStatementAnalysis
+ {
+ /**
+ * @var array mapping of commands to classification
+ */
+ protected static $mappingClassificationAnalysis = array(
+ 'CREATE' => TDbStatementClassification::DDL,
+ 'DROP' => TDbStatementClassification::DDL,
+ 'ALTER' => TDbStatementClassification::DDL,
+ 'RENAME' => TDbStatementClassification::DDL,
+
+ 'INSERT' => TDbStatementClassification::DML,
+ 'UPDATE' => TDbStatementClassification::DML,
+ 'DELETE' => TDbStatementClassification::DML,
+ 'REPLACE' => TDbStatementClassification::DML,
+ 'TRUNCATE' => TDbStatementClassification::DML,
+ 'LOAD' => TDbStatementClassification::DML,
+
+ 'GRANT' => TDbStatementClassification::DCL,
+ 'REVOKE' => TDbStatementClassification::DCL,
+
+ 'XA' => TDbStatementClassification::TCL,
+ 'SAVEPOINT' => TDbStatementClassification::TCL,
+ 'CHECKPOINT' => TDbStatementClassification::TCL,
+ 'RELEASE SAVEPOINT' => TDbStatementClassification::TCL,
+ 'START TRANSACTION' => TDbStatementClassification::TCL,
+ 'BEGIN' => TDbStatementClassification::TCL,
+ 'COMMIT' => TDbStatementClassification::TCL,
+ 'ROLLBACK' => TDbStatementClassification::TCL,
+ 'LOCK' => TDbStatementClassification::TCL,
+ 'UNLOCK' => TDbStatementClassification::TCL,
+ 'ABORT' => TDbStatementClassification::TCL,
+ 'END' => TDbStatementClassification::TCL,
+
+ 'SELECT' => TDbStatementClassification::SQL,
+
+ 'SHOW' => TDbStatementClassification::SQL,
+ 'DESCRIBE' => TDbStatementClassification::SQL,
+ 'EXPLAIN' => TDbStatementClassification::SQL,
+ 'PRAGMA' => TDbStatementClassification::SQL,
+
+ 'SET' => TDbStatementClassification::CONTEXT,
+ 'USE' => TDbStatementClassification::CONTEXT,
+
+ 'CALL' => TDbStatementClassification::UNKNOWN,
+ 'EXEC' => TDbStatementClassification::UNKNOWN,
+ 'PREPARE' => TDbStatementClassification::UNKNOWN,
+ 'EXECUTE' => TDbStatementClassification::UNKNOWN,
+ 'DEALLOCATE' => TDbStatementClassification::UNKNOWN,
+ );
+
+ /**
+ * @var array
+ */
+ protected static $cacheClassificationAnalysis = array();
+
+ /**
+ * @var string
+ */
+ protected static $regExpClassificationAnalysis = null;
+
+ /**
+ * @param TDbStatementAnalysisParamete
+ * @return TDbStatementClassification
+ */
+ public static function doClassificationAnalysis(TDbStatementAnalysisParameter $param)
+ {
+ $statement = $param->getStatement();
+ $default = $param->getDefaultClassification();
+
+ $hash = md5($statement . '-' . $default);
+
+ if( isset(self::$cacheClassificationAnalysis[$hash]) )
+ return self::$cacheClassificationAnalysis[$hash];
+
+ self::$cacheClassificationAnalysis[$hash] = $default;
+
+ $statement = preg_replace('/(?:--|\\#)[\x20\\t\\S]*\s+|\/\\*[\x20\\t\\n\\r\\S]*?\\*\//Ssmux', '', $statement);
+ $statement = preg_replace('/[\s]+/Smu', ' ', $statement);
+ $statement = trim($statement);
+
+ if(self::$regExpClassificationAnalysis===null)
+ self::$regExpClassificationAnalysis = '/^(' . str_replace(' ', '\x20', implode('|', array_keys(self::$mappingClassificationAnalysis))) . ')+[\s]+.*|\k1/Siu';
+
+ $cmd = strToUpper(preg_replace(self::$regExpClassificationAnalysis, '\1', $statement));
+
+ if( isset(self::$mappingClassificationAnalysis[$cmd]) )
+ self::$cacheClassificationAnalysis[$hash] = self::$mappingClassificationAnalysis[$cmd];
+
+ return self::$cacheClassificationAnalysis[$hash];
+ }
+
+ /**
+ * @param TDbStatementAnalysisParameter
+ * @return TDbStatementClassification
+ */
+ public function getClassificationAnalysis(TDbStatementAnalysisParameter $param)
+ {
+ if( ($drivername = $this->getDriverName())!== null )
+ $param->setDriverName($drivername);
+
+ return self::doClassificationAnalysis($param);
+ }
+ }
+?> \ No newline at end of file