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

/dbal/src/drivers/lmbDbTableInfo.class.php

http://github.com/limb-php-framework/limb
PHP | 163 lines | 123 code | 25 blank | 15 comment | 15 complexity | 3f76f8f1c85f860b3cdeabdb5183f2c1 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0
  1. <?php
  2. /*
  3. * Limb PHP Framework
  4. *
  5. * @link http://limb-project.com
  6. * @copyright Copyright &copy; 2004-2009 BIT(http://bit-creative.com)
  7. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  8. */
  9. lmb_require('limb/dbal/src/exception/lmbDbException.class.php');
  10. /**
  11. * abstract class lmbDbTableInfo.
  12. *
  13. * @package dbal
  14. * @version $Id: lmbDbTableInfo.class.php 7486 2009-01-26 19:13:20Z pachanga $
  15. */
  16. abstract class lmbDbTableInfo
  17. {
  18. protected $name;
  19. protected $columns = array();
  20. protected $indexes = array();
  21. protected $canonicalHandler;
  22. protected $cached_columns = array();
  23. function __construct($name, $canonicalHandler = null)
  24. {
  25. $this->name = $this->canonicalizeIdentifier($name);
  26. if($canonicalHandler !== null &&
  27. $canonicalHandler != 'strtolower' &&
  28. $canonicalHandler != 'strtoupper')
  29. {
  30. throw new lmbDbException("Invalid identifier compatability function '$canonicalHandler'");
  31. }
  32. $this->canonicalHandler = $canonicalHandler;
  33. }
  34. function canonicalizeIdentifier($id)
  35. {
  36. if(!is_null($this->canonicalHandler))
  37. return $this->canonicalHandler($id);
  38. return $id;
  39. }
  40. function getCanonicalColumnName($name)
  41. {
  42. $name = $this->canonicalizeIdentifier($name);
  43. // quick check if they happen to use the same case.
  44. if(array_key_exists($name, $this->columns))
  45. return $name;
  46. // slow check
  47. foreach(array_keys($this->columns) as $key)
  48. {
  49. if(strcasecmp($name, $key) == 0)
  50. return $key;
  51. }
  52. return $name;
  53. }
  54. function getName()
  55. {
  56. return $this->name;
  57. }
  58. abstract function loadColumns();
  59. abstract function loadIndexes();
  60. function hasColumn($name)
  61. {
  62. $old_name = $name;
  63. if(isset($this->cached_columns[$old_name]))
  64. return true;
  65. $this->loadColumns();
  66. $name = $this->getCanonicalColumnName($name);
  67. if(array_key_exists($name, $this->columns))
  68. {
  69. $this->cached_columns[$old_name] = $this->columns[$name];
  70. return true;
  71. }
  72. else
  73. return false;
  74. }
  75. function getColumn($name)
  76. {
  77. $old_name = $name;
  78. if(isset($this->cached_columns[$old_name]))
  79. return $this->cached_columns[$old_name];
  80. $this->loadColumns();
  81. $name = $this->getCanonicalColumnName($name);
  82. if(!array_key_exists($name, $this->columns))
  83. {
  84. throw new lmbDbException("Column '$name' does not exist");
  85. }
  86. $this->cached_columns[$old_name] = $this->columns[$name];
  87. return $this->cached_columns[$old_name];
  88. }
  89. function getColumnList()
  90. {
  91. $this->loadColumns();
  92. $result = array();
  93. foreach(array_keys($this->columns) as $name)
  94. $result[$name] = $name;
  95. return $result;
  96. }
  97. function getColumns()
  98. {
  99. $columns = array();
  100. foreach ($this->getColumnList() as $column_name)
  101. $columns[$column_name] = $this->getColumn($column_name);
  102. return $columns;
  103. }
  104. function hasIndex($name)
  105. {
  106. $this->loadIndexes();
  107. return array_key_exists($name, $this->indexes);
  108. }
  109. function getIndex($name)
  110. {
  111. $this->loadIndexes();
  112. if(!array_key_exists($name, $this->indexes))
  113. {
  114. throw new lmbDbException("Index '$name' does not exist");
  115. }
  116. return $this->indexes[$name];
  117. }
  118. function getIndexList()
  119. {
  120. $this->loadIndexes();
  121. $result = array();
  122. foreach(array_keys($this->indexes) as $name)
  123. $result[$name] = $name;
  124. return $result;
  125. }
  126. function getIndexForColumn($column)
  127. {
  128. if(is_object($column))
  129. $column = $column->getName();
  130. $this->loadIndexes();
  131. foreach ($this->indexes as $index)
  132. {
  133. if($column == $index->getColumnName())
  134. return $index;
  135. }
  136. return null;
  137. }
  138. }