PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/XoopsEngine/lib/Xoops/Zend/Db/Table.php

https://github.com/xoops-pi/engine
PHP | 266 lines | 112 code | 24 blank | 130 comment | 23 complexity | 60f135ad71942062494dbfa925706faf MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework for Xoops Engine
  4. *
  5. * You may not change or alter any portion of this comment or credits
  6. * of supporting developers from this source code or any supporting source code
  7. * which is considered copyrighted (c) material of the original comment or credit authors.
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * @copyright Xoops Engine http://www.xoopsengine.org
  13. * @license http://www.fsf.org/copyleft/gpl.html GNU public license
  14. * @author Taiwen Jiang <phppp@users.sourceforge.net>
  15. * @since 3.0
  16. * @category Xoops_Zend
  17. * @package Db
  18. * @version $Id$
  19. */
  20. abstract class Xoops_Zend_Db_Table extends Zend_Db_Table_Abstract
  21. {
  22. /**
  23. * Table specific prefix, by module
  24. *
  25. * @var string
  26. */
  27. protected $prefix = '';
  28. /**
  29. * Classname for row
  30. *
  31. * @var string
  32. */
  33. protected $_rowClass = 'Xoops_Zend_Db_Table_Row';
  34. /**
  35. * Classname for rowset
  36. *
  37. * @var string
  38. */
  39. protected $_rowsetClass = 'Xoops_Zend_Db_Table_Rowset';
  40. /**
  41. * __construct() - For concrete implementation of Xoops_Zend_Db_Table
  42. *
  43. * @param string|array $config string can reference a Zend_Registry key for a db adapter
  44. * OR it can reference the name of a table
  45. * @param array|Zend_Db_Table_Definition $definition
  46. */
  47. public function __construct($config = array(), $definition = null)
  48. {
  49. if ($definition !== null && is_array($definition)) {
  50. $definition = new Zend_Db_Table_Definition($definition);
  51. }
  52. if (is_string($config)) {
  53. if (Zend_Registry::isRegistered($config)) {
  54. trigger_error(__CLASS__ . '::' . __METHOD__ . '(\'registryName\') is not valid usage of Zend_Db_Table, '
  55. . 'try extending Zend_Db_Table_Abstract in your extending classes.',
  56. E_USER_NOTICE
  57. );
  58. $config = array(self::ADAPTER => $config);
  59. } else {
  60. // process this as table with or without a definition
  61. if ($definition instanceof Zend_Db_Table_Definition
  62. && $definition->hasTableConfig($config)) {
  63. // this will have DEFINITION_CONFIG_NAME & DEFINITION
  64. $config = $definition->getTableConfig($config);
  65. } else {
  66. $config = array(self::NAME => $config);
  67. }
  68. }
  69. }
  70. parent::__construct($config);
  71. }
  72. /**
  73. * setOptions()
  74. *
  75. * @param array $options
  76. * @return Zend_Db_Table_Abstract
  77. */
  78. public function setOptions(Array $options)
  79. {
  80. if (isset($options["prefix"])) {
  81. $this->setPrefix($options["prefix"]);
  82. unset($options["prefix"]);
  83. }
  84. parent::setOptions($options);
  85. return $this;
  86. }
  87. /**
  88. * @param mixed $db Either an Adapter object, or a string naming a Registry key
  89. * @return Zend_Db_Adapter_Abstract
  90. * @throws Zend_Db_Table_Exception
  91. */
  92. protected static function _setupAdapter($db)
  93. {
  94. if ($db === null) {
  95. return null;
  96. }
  97. if (is_string($db)) {
  98. $db = XOOPS::registry($db);
  99. }
  100. if (!$db instanceof Zend_Db_Adapter_Abstract) {
  101. require_once 'Zend/Db/Table/Exception.php';
  102. throw new Zend_Db_Table_Exception('Argument must be of type Zend_Db_Adapter_Abstract, or a Registry key where a Zend_Db_Adapter_Abstract object is stored');
  103. }
  104. return $db;
  105. }
  106. /**
  107. * Sets the default metadata cache for information returned by Zend_Db_Adapter_Abstract::describeTable().
  108. *
  109. * If $defaultMetadataCache is null, then no metadata cache is used by default.
  110. *
  111. * @param mixed $metadataCache Either a Cache object, or a string naming a Registry key
  112. * @return void
  113. */
  114. public static function setDefaultMetadataCache($metadataCache = null)
  115. {
  116. self::$_defaultMetadataCache = self::_setupMetadataCache($metadataCache);
  117. }
  118. /**
  119. * Gets the default metadata cache for information returned by Zend_Db_Adapter_Abstract::describeTable().
  120. *
  121. * @return Zend_Cache_Core or null
  122. */
  123. public static function getDefaultMetadataCache()
  124. {
  125. return self::$_defaultMetadataCache;
  126. }
  127. /**
  128. * Sets the metadata cache for information returned by Zend_Db_Adapter_Abstract::describeTable().
  129. *
  130. * If $metadataCache is null, then no metadata cache is used. Since there is no opportunity to reload metadata
  131. * after instantiation, this method need not be public, particularly because that it would have no effect
  132. * results in unnecessary API complexity. To configure the metadata cache, use the metadataCache configuration
  133. * option for the class constructor upon instantiation.
  134. *
  135. * @param mixed $metadataCache Either a Cache object, or a string naming a Registry key
  136. * @return Zend_Db_Table_Abstract Provides a fluent interface
  137. */
  138. protected function _setMetadataCache($metadataCache)
  139. {
  140. $this->_metadataCache = self::_setupMetadataCache($metadataCache);
  141. return $this;
  142. }
  143. /**
  144. * @param mixed $metadataCache Either a Cache object, or a string naming a Registry key
  145. * @return Zend_Cache_Core
  146. * @throws Zend_Db_Table_Exception
  147. */
  148. protected static function _setupMetadataCache($metadataCache)
  149. {
  150. if ($metadataCache === null) {
  151. return null;
  152. }
  153. if (is_string($metadataCache)) {
  154. $metadataCache = Zend_Registry::get($metadataCache);
  155. }
  156. return $metadataCache;
  157. }
  158. /**
  159. * Initializes metadata.
  160. *
  161. * If metadata cannot be loaded from cache, adapter's describeTable() method is called to discover metadata
  162. * information. Returns true if and only if the metadata are loaded from cache.
  163. *
  164. * @return boolean
  165. * @throws Zend_Db_Table_Exception
  166. */
  167. protected function _setupMetadata()
  168. {
  169. if ($this->metadataCacheInClass() && (count($this->_metadata) > 0)) {
  170. return true;
  171. }
  172. // Assume that metadata will be loaded from cache
  173. $isMetadataFromCache = true;
  174. // If $this has no metadata cache but the class has a default metadata cache
  175. //if (null === $this->_metadataCache && null !== self::$_defaultMetadataCache) {
  176. if (null === $this->_metadataCache && null !== static::getDefaultMetadataCache()) {
  177. // Make $this use the default metadata cache of the class
  178. $this->_setMetadataCache(static::getDefaultMetadataCache());
  179. }
  180. // If $this has a metadata cache
  181. if (null !== $this->_metadataCache) {
  182. // Define the cache identifier where the metadata are saved
  183. //get db configuration
  184. $dbConfig = $this->_db->getConfig();
  185. // Define the cache identifier where the metadata are saved
  186. $cacheId = md5( // port:host/dbname:schema.table (based on availabilty)
  187. (isset($dbConfig['options']['port']) ? ':'.$dbConfig['options']['port'] : null)
  188. . (isset($dbConfig['options']['host']) ? ':'.$dbConfig['options']['host'] : null)
  189. . '/'.$dbConfig['dbname'].':'.$this->_schema.'.'.$this->_name
  190. );
  191. }
  192. // If $this has no metadata cache or metadata cache misses
  193. if (null === $this->_metadataCache || !($metadata = $this->_metadataCache->load($cacheId))) {
  194. // Metadata are not loaded from cache
  195. $isMetadataFromCache = false;
  196. // Fetch metadata from the adapter's describeTable() method
  197. $metadata = $this->_db->describeTable($this->_name, $this->_schema);
  198. // If $this has a metadata cache, then cache the metadata
  199. //if (null !== $this->_metadataCache && !$this->_metadataCache->save($metadata, $cacheId, array('model'))) {
  200. if (null !== $this->_metadataCache && !$this->_metadataCache->save($metadata, $cacheId)) {
  201. trigger_error('Failed saving metadata to metadataCache', E_USER_NOTICE);
  202. }
  203. }
  204. // Assign the metadata to $this
  205. $this->_metadata = $metadata;
  206. // Return whether the metadata were loaded from cache
  207. return $isMetadataFromCache;
  208. }
  209. /**
  210. * Set table specific prefix
  211. *
  212. * @param string $prefix
  213. * @return {@Xoops_Zend_Db_Table}
  214. */
  215. public function setPrefix($prefix)
  216. {
  217. $this->prefix = $prefix;
  218. return $this;
  219. }
  220. /**
  221. * Prefix a table name
  222. *
  223. * if tablename is empty, only prefix will be returned
  224. *
  225. * @param string $table tablename
  226. * @return string prefixed tablename, just prefix if tablename is empty
  227. */
  228. public function prefix($table = '')
  229. {
  230. /*
  231. $segs = array();
  232. if (!empty($this->prefix)) {
  233. $segs[] = $this->prefix;
  234. }
  235. if (!empty($table)) {
  236. $segs[] = $table;
  237. }
  238. $table = implode("_", $segs);
  239. */
  240. return $this->getAdapter()->prefix($table, $this->prefix);
  241. }
  242. }