diff options
author | wei <> | 2006-04-14 06:22:09 +0000 |
---|---|---|
committer | wei <> | 2006-04-14 06:22:09 +0000 |
commit | 3d3f8d3832921f99daf8ce1953304763c2e76c62 (patch) | |
tree | e1b0a9bc3a13fccd253770fb452ac96cc6315121 /framework/DataAccess/SQLMap/TSqlMapClient.php | |
parent | 373d8acc503b94ea09823f49e2ab5e395eccc584 (diff) |
Importing SQLMap + sample + docs.
Diffstat (limited to 'framework/DataAccess/SQLMap/TSqlMapClient.php')
-rw-r--r-- | framework/DataAccess/SQLMap/TSqlMapClient.php | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/framework/DataAccess/SQLMap/TSqlMapClient.php b/framework/DataAccess/SQLMap/TSqlMapClient.php new file mode 100644 index 00000000..7662d83e --- /dev/null +++ b/framework/DataAccess/SQLMap/TSqlMapClient.php @@ -0,0 +1,81 @@ +<?php
+
+require_once(dirname(__FILE__).'/TSqlMapper.php');
+
+/**
+ * A DataMapper client class that can load a cached SqlMapper. Give the configuration
+ * file, it looks for a .cache file containing serialized TSqlMapClient instance to
+ * load. Usage:
+ *
+ * <code>
+ * $client = new TSqlMapClient;
+ * $sqlmap = $client->configure($configFile, true); //load from cache.
+ * $products = $sqlMap->queryForList('statementName');
+ * </code>
+ *
+ * To save the TSqlMapper instance to cache for later usage, call
+ * cacheConfiguration().
+ *
+ * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
+ * @version $Revision: $ $Date: $
+ * @package System.DataAccess.SQLMap
+ * @since 3.0
+ */
+class TSqlMapClient
+{
+ private $_mapper;
+ private $_cache;
+
+ public function configure($configFile, $loadFromCache=false)
+ {
+ if(is_null($this->_mapper))
+ $this->initMapper($configFile, $loadFromCache);
+ return $this->_mapper;
+ }
+
+ public function getInstance()
+ {
+ return $this->_mapper;
+ }
+
+ public function cacheConfiguration()
+ {
+ if(!is_null($this->_mapper) && $this->_cache !== false)
+ {
+ if(!is_file($this->_cache))
+ {
+ var_dump('saving cache to file', $this->_cache);
+ file_put_contents($this->_cache,serialize($this->_mapper));
+ return true;
+ }
+ }
+ return false;
+ }
+
+ protected function initMapper($file=null,$loadFromCache=false)
+ {
+ $this->_cache = $this->getCacheFile($file);
+ if($loadFromCache && $this->_cache !== false && is_file($this->_cache))
+ {
+ var_dump('loading from cache: '.$this->_cache);
+ $this->_mapper = unserialize(file_get_contents($this->_cache));
+ }
+ else
+ {
+// var_dump('build from *.xml');
+ $builder = new TDomSqlMapBuilder();
+ $this->_mapper = $builder->configure($file);
+ }
+ }
+
+ protected function getCacheFile($file)
+ {
+ $path = realpath($file);
+ if($path !== false)
+ return substr($path,0, strrpos($path,'.')).'.cache';
+ else
+ return false;
+ }
+}
+
+?>
\ No newline at end of file |