From 2570226fbac3e26b1e94896b50d1db4bc1aa3308 Mon Sep 17 00:00:00 2001 From: wei <> Date: Sun, 17 Dec 2006 22:20:50 +0000 Subject: Add TDataSourceConfig, TSqlMapConfig, TActiveRecordConfig --- .../Data/ActiveRecord/TActiveRecordConfig.php | 108 ++++++++++++++ .../Data/ActiveRecord/TActiveRecordGateway.php | 62 ++++++-- .../Data/ActiveRecord/TActiveRecordManager.php | 19 +++ .../Data/SqlMap/Configuration/TSqlMapStatement.php | 28 ++-- .../Configuration/TSqlMapXmlConfiguration.php | 13 +- .../Data/SqlMap/Statements/TMappedStatement.php | 4 +- framework/Data/SqlMap/Statements/TSqlMapSelect.php | 29 ---- framework/Data/SqlMap/TSqlMapConfig.php | 163 +++++++++++++++++++++ framework/Data/SqlMap/TSqlMapGateway.php | 8 - framework/Data/SqlMap/TSqlMapManager.php | 25 ++-- framework/Data/TDataSourceConfig.php | 146 ++++++++++++++++++ 11 files changed, 527 insertions(+), 78 deletions(-) create mode 100644 framework/Data/ActiveRecord/TActiveRecordConfig.php delete mode 100644 framework/Data/SqlMap/Statements/TSqlMapSelect.php create mode 100644 framework/Data/SqlMap/TSqlMapConfig.php create mode 100644 framework/Data/TDataSourceConfig.php (limited to 'framework/Data') diff --git a/framework/Data/ActiveRecord/TActiveRecordConfig.php b/framework/Data/ActiveRecord/TActiveRecordConfig.php new file mode 100644 index 00000000..5670e64c --- /dev/null +++ b/framework/Data/ActiveRecord/TActiveRecordConfig.php @@ -0,0 +1,108 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2007 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Id$ + * @package System.Data.ActiveRecord + */ + +Prado::using('System.Data.TDataSourceConfig'); + +/** + * TActiveRecordConfig module configuration class. + * + * Database configuration for the default ActiveRecord manager instance. + * + * Example: application.xml configuration + * + * + * + * + * + * + * + * + * MySQL database definition: + * + * CREATE TABLE `blogs` ( + * `blog_id` int(10) unsigned NOT NULL auto_increment, + * `blog_name` varchar(255) NOT NULL, + * `blog_author` varchar(255) NOT NULL, + * PRIMARY KEY (`blog_id`) + * ) ENGINE=InnoDB DEFAULT CHARSET=utf8; + * + * + * Record php class: + * + * class Blogs extends TActiveRecord + * { + * public $blog_id; + * public $blog_name; + * public $blog_author; + * + * public static function finder() + * { + * return self::getRecordFinder('Blogs'); + * } + * } + * + * + * Usage example: + * + * class Home extends TPage + * { + * function onLoad($param) + * { + * $blogs = Blogs::finder()->findAll(); + * print_r($blogs); + * } + * } + * + * + * @author Wei Zhuo + * @version $Id$ + * @package System.Data.ActiveRecord + * @since 3.1 + */ +class TActiveRecordConfig extends TDataSourceConfig +{ + private $_enableCache=false; + + /** + * Initialize the active record manager. + * @param TXmlDocument xml configuration. + */ + public function init($xml) + { + parent::init($xml); + Prado::using('System.Data.ActiveRecord.TActiveRecordManager'); + $manager = TActiveRecordManager::getInstance(); + if($this->getEnableCache()) + $manager->setCache($this->getApplication()->getCache()); + $manager->setDbConnection($this->getDbConnection()); + } + + /** + * Set true to cache the table meta data. + * @param boolean true to cache sqlmap instance. + */ + public function setEnableCache($value) + { + $this->_enableCache = TPropertyValue::ensureBoolean($value, false); + } + + /** + * @return boolean true if table meta data should be cached, false otherwise. + */ + public function getEnableCache() + { + return $this->_enableCache; + } +} + +?> \ No newline at end of file diff --git a/framework/Data/ActiveRecord/TActiveRecordGateway.php b/framework/Data/ActiveRecord/TActiveRecordGateway.php index 19c8552f..e7ea5e46 100644 --- a/framework/Data/ActiveRecord/TActiveRecordGateway.php +++ b/framework/Data/ActiveRecord/TActiveRecordGateway.php @@ -22,7 +22,7 @@ class TActiveRecordGateway extends TComponent { private $_manager; - private $_tables=array(); + private $_tables=array(); //meta data cache. /** * Property name for optional table name in TActiveRecord. @@ -68,35 +68,67 @@ class TActiveRecordGateway extends TComponent } /** - * Gets the meta data for given database and table. + * @param TActiveRecord active record. + * @return TDbMetaData table meta data, null if not found. */ - public function getMetaData(TActiveRecord $record) + protected function getCachedMetaData($record) { $type=get_class($record); - if(!isset($this->_tables[$type])) + if(isset($this->_tables[$type])) + return $this->_tables[$type]; + if(($cache=$this->getManager()->getCache())!==null) { - $conn = $record->getDbConnection(); - $inspector = $this->getManager()->getTableInspector($conn); - $table = $this->getTableName($record); - $this->_tables[$type] = $inspector->getTableMetaData($table); + //force loading of the table inspector to load the required classes + // before unserializing cached meta data. + $this->getManager()->getTableInspector($record->getDbConnection()); + $data = $cache->get($this->getMetaDataCacheKey($record)); + if($data !== false && $data !== null) + { + $this->_tables[$type] = $data; + return $data; + } } - return $this->_tables[$type]; } /** - * @param array table meta data. + * @param TActiveRecord active record. + * @return string cache key, using connection string + record class name + */ + protected function getMetaDataCacheKey($record) + { + $conn = $record->getDbConnection()->getConnectionString(); + return $conn.':'.get_class($record); + } + + /** + * Cache the meta data, tries the application cache if applicable. + * @param TActiveRecord active record. + * @param TDbMetaData table meta data + * @return TDbMetaData table meta data. */ - public function setAllMetaData($data) + protected function cacheMetaData($record,$data) { - $this->_tables=$data; + $type = get_class($record); + if(($cache=$this->getManager()->getCache())!==null) + $cache->set($this->getMetaDataCacheKey($record), $data); + $this->_tables[$type] = $data; + return $data; } /** - * @return array all table meta data. + * Gets the meta data for given database and table. */ - public function getAllMetaData() + public function getMetaData(TActiveRecord $record) { - return $this->_tables; + $type=get_class($record); + if(!($data = $this->getCachedMetaData($record))) + { + $conn = $record->getDbConnection(); + $inspector = $this->getManager()->getTableInspector($conn); + $table = $this->getTableName($record); + $data = $this->cacheMetaData($record,$inspector->getTableMetaData($table)); + } + return $data; } /** diff --git a/framework/Data/ActiveRecord/TActiveRecordManager.php b/framework/Data/ActiveRecord/TActiveRecordManager.php index 883abbf2..7f239a34 100644 --- a/framework/Data/ActiveRecord/TActiveRecordManager.php +++ b/framework/Data/ActiveRecord/TActiveRecordManager.php @@ -11,6 +11,7 @@ */ Prado::using('System.Data.TDbConnection'); +Prado::using('System.Data.ActiveRecord.TActiveRecord'); Prado::using('System.Data.ActiveRecord.Exceptions.TActiveRecordException'); Prado::using('System.Data.ActiveRecord.TActiveRecordGateway'); Prado::using('System.Data.ActiveRecord.TActiveRecordStateRegistry'); @@ -46,6 +47,24 @@ class TActiveRecordManager extends TComponent private $_meta=array(); private $_connection; + private $_cache; + + /** + * @return ICache application cache. + */ + public function getCache() + { + return $this->_cache; + } + + /** + * @param ICache application cache + */ + public function setCache($value) + { + $this->_cache=$value; + } + /** * @param TDbConnection default database connection */ diff --git a/framework/Data/SqlMap/Configuration/TSqlMapStatement.php b/framework/Data/SqlMap/Configuration/TSqlMapStatement.php index 253d2090..5d2640e9 100644 --- a/framework/Data/SqlMap/Configuration/TSqlMapStatement.php +++ b/framework/Data/SqlMap/Configuration/TSqlMapStatement.php @@ -294,6 +294,22 @@ class TSqlMapStatement extends TComponent } } +/** + * TSqlMapSelect class file. + * + * @author Wei Zhuo + * @version $Id$ + * @package System.Data.SqlMap.Statements + * @since 3.1 + */ +class TSqlMapSelect extends TSqlMapStatement +{ + private $_generate; + + public function getGenerate(){ return $this->_generate; } + public function setGenerate($value){ $this->_generate = $value; } +} + /** * TSqlMapDelete class corresponds to the element. * @@ -338,18 +354,6 @@ class TSqlMapInsert extends TSqlMapStatement } } -/** - * TSqlMapSelect class corresponds to