PageRenderTime 22ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Setuco/Db/Table/Abstract.php

https://github.com/atcorp/SetucoCMS
PHP | 213 lines | 108 code | 16 blank | 89 comment | 16 complexity | 2e5e77cc3499ae21dabef6faf95f0134 MD5 | raw file
  1. <?php
  2. /**
  3. * DbTableの抽象クラスです。
  4. *
  5. * LICENSE: ライセンスに関する情報
  6. *
  7. * @category Setuco
  8. * @package Setuco
  9. * @subpackage Db_Table
  10. * @copyright Copyright (c) 2010 SetucoCMS Project.(http://sourceforge.jp/projects/setucocms)
  11. * @license http://www.opensource.org/licenses/gpl-2.0.php GNU General Public License, version 2
  12. * @version
  13. * @link
  14. * @since File available since Release 0.1.0
  15. * @author charlesvineyard
  16. */
  17. /**
  18. * @package Setuco
  19. * @subpackage Db_Table
  20. * @author charlesvineyard
  21. */
  22. abstract class Setuco_Db_Table_Abstract extends Zend_Db_Table_Abstract
  23. {
  24. /**
  25. * SQL文中のバックスラッシュ(\)を置換する文字列
  26. *
  27. * @var string
  28. */
  29. const BACKSLASH_REPLACER = '__BS__';
  30. /**
  31. * 全部で何件あるのか取得する
  32. *
  33. * @return int 全てのデータ件数
  34. * @author suzuki-mar charlesvineyard
  35. */
  36. public function countAll()
  37. {
  38. $select = $this->select($this->_name);
  39. return $this->fetchAll($select)->count();
  40. }
  41. /**
  42. * IDからひとつの行を取得し、配列で返します。
  43. * 行が見つからなければ null を返します。
  44. *
  45. * このメソッドは複合キーに対応するため可変長引数です。
  46. *
  47. * 主キーが2つからなる複合キーの場合は、次のように1つ1つを引数に指定して呼び出してください。
  48. * それぞれが _primary に指定したカラムと順序に対応します。
  49. * loadByPrimary('キー1の値', 'キー2の値');
  50. *
  51. * このメソッドは親クラスのfindメソッドに委譲しています。
  52. * 呼び出しに関する制約はそちらのコメントも参照してください。
  53. *
  54. * @param mixed $key プライマリキーの値(複数の場合あり)
  55. * @return array|null 取得した行の配列。なければ null。
  56. * @see Zend_Db_Table_Abstract::find()
  57. * @author charlesvineyard
  58. */
  59. public function loadByPrimary()
  60. {
  61. foreach (func_get_args() as $i => $arg) {
  62. if (is_array($arg)) {
  63. throw new Zend_Db_Table_Exception("プライマリキーに対する値に配列は指定できません。(" . ($i + 1) . "番目のキー)");
  64. }
  65. if (is_null($arg)) {
  66. throw new Zend_Db_Table_Exception("プライマリキーに対する値に NULL は指定できません。(" . ($i + 1) . "番目のキー)");
  67. }
  68. }
  69. $rowset = call_user_func_array('parent::find', func_get_args());
  70. if ($rowset->count() == 0) {
  71. return null;
  72. }
  73. return $rowset->current()->toArray();
  74. }
  75. /**
  76. * 指定されたIDのレコードを削除します。
  77. *
  78. * このメソッドは複合キーに対応するため可変長引数です。
  79. * 主キーが2つからなる複合キーの場合は、次のように1つ1つを引数に指定して呼び出してください。
  80. * それぞれが _primary に指定したカラムと順序に対応します。
  81. * deleteByPrimary('キー1の値', 'キー2の値');
  82. *
  83. * @param mixed $key プライマリキーの値(複数の場合あり)
  84. * @return boolean 削除できたら true。該当レコードが存在しなければ false。
  85. * @author akitsukada charlesvineyard
  86. */
  87. public function deleteByPrimary()
  88. {
  89. $primary = (array) $this->_primary;
  90. if (count($primary) > func_num_args()) {
  91. throw new Zend_Db_Table_Exception("プライマリキーに対する値が少なすぎます。");
  92. }
  93. if (count($primary) < func_num_args()) {
  94. throw new Zend_Db_Table_Exception("プライマリキーに対する値が多すぎます。");
  95. }
  96. $where = array();
  97. $i = 0;
  98. foreach ($primary as $key) {
  99. if (is_array(func_get_arg($i))) {
  100. throw new Zend_Db_Table_Exception("プライマリキーに対する値に配列は指定できません。(" . ($i + 1) . "番目のキー)");
  101. }
  102. if (is_null(func_get_arg($i))) {
  103. throw new Zend_Db_Table_Exception("プライマリキーに対する値に NULL は指定できません。(" . ($i + 1) . "番目のキー)");
  104. }
  105. $where[] = $this->getAdapter()->quoteInto($key . ' = ?', func_get_arg($i));
  106. $i++;
  107. }
  108. $effectedRowCount = $this->delete($where);
  109. if ($effectedRowCount == 0) {
  110. return false;
  111. }
  112. return true;
  113. }
  114. /**
  115. * 指定されたIDのレコードを更新する。
  116. *
  117. * このメソッドは複合キーに対応するため可変長引数です。
  118. * 主キーが2つからなる複合キーの場合は、次のように1つ1つを引数に指定して呼び出してください。
  119. * それぞれが _primary に指定したカラムと順序に対応します。
  120. * deleteByPrimary('キー1の値', 'キー2の値');
  121. *
  122. * @param array $updateData キー:カラム名、値:更新する値の配列。
  123. * @param mixed $key プライマリキーの値(複数の場合あり)
  124. * @return boolean 更新できたら true。該当レコードが存在しなければ false。
  125. * @author charlesvineyard
  126. */
  127. public function updateByPrimary($updateData)
  128. {
  129. $primary = (array) $this->_primary;
  130. if (count($primary) > func_num_args() - 1) {
  131. throw new Zend_Db_Table_Exception("プライマリキーに対する値が少なすぎます。");
  132. }
  133. if (count($primary) < func_num_args() - 1) {
  134. throw new Zend_Db_Table_Exception("プライマリキーに対する値が多すぎます。");
  135. }
  136. $where = array();
  137. $i = 1;
  138. foreach ($primary as $key) {
  139. if (is_array(func_get_arg($i))) {
  140. throw new Zend_Db_Table_Exception("プライマリキーに対する値に配列は指定できません。(" . $i . "番目のキー)");
  141. }
  142. if (is_null(func_get_arg($i))) {
  143. throw new Zend_Db_Table_Exception("プライマリキーに対する値に NULL は指定できません。(" . $i . "番目のキー)");
  144. }
  145. $where[] = $this->getAdapter()->quoteInto($key . ' = ?', func_get_arg($i));
  146. $i++;
  147. }
  148. $effectedRowCount = $this->update($updateData, $where);
  149. if ($effectedRowCount == 0) {
  150. return false;
  151. }
  152. return true;
  153. }
  154. /**
  155. * WHERE句のLIKE演算子や正規表現に与える文字列を\(バックスラッシュ)でエスケープします。
  156. * エスケープされた文字が検索できるようになります。
  157. * バックスラッシュ自体を検索するときは、getBsReplacedExpressionとセットで使う必要があります。
  158. *
  159. * @param string $str LIKE検索を行う検索対象文字列
  160. * @return string エスケープ済みの検索対象文字列
  161. * @author akitsukada
  162. */
  163. public function escapeLikeString($str)
  164. {
  165. $str = str_replace('\\', self::BACKSLASH_REPLACER, $str);
  166. $str = addcslashes($str, '%_<>{}:[]+.*()|^$?');
  167. return $str;
  168. }
  169. /**
  170. * $columnNameにSQL文のカラム名、リテラルを受け取り、MySQL,PostgreSQLのreplace関数を
  171. * 適用した表現を返します。replace関数は'\'をBACKSLASH_REPLACERに置換します。
  172. * 例:"col" → "replace(col, '\\\\', '__BACKSLASH__')"
  173. * LIKE検索時には、escapeLikeStringとセットで使う必要があります。
  174. *
  175. * @param string $columnName
  176. * @return string 受け取ったカラム名にreplace関数を適用した表現
  177. * @author akitsukada
  178. */
  179. public function getBsReplacedExpression($columnName)
  180. {
  181. return "replace({$columnName}, '\\\\', '" . self::BACKSLASH_REPLACER . "')";
  182. }
  183. /**
  184. * 実行したSQLクエリーを取得する
  185. *
  186. * @return array 実行したSQLクエリー
  187. * @author suzuki-mar
  188. */
  189. public function getExecutSqls()
  190. {
  191. foreach ($this->_db->getProfiler()->getQueryProfiles() as $profiler) {
  192. $result[] = $profiler->getQuery();
  193. }
  194. return $result;
  195. }
  196. }