Changeset 22796 for branches/version-2_12-dev/data/class/SC_Query.php
- Timestamp:
- 2013/05/02 18:11:36 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/version-2_12-dev/data/class/SC_Query.php
r22567 r22796 30 30 * @version $Id$ 31 31 */ 32 class SC_Query 33 { 32 class SC_Query { 34 33 35 34 var $option = ''; … … 50 49 * @param boolean $new 新規に接続を行うかどうか 51 50 */ 52 function __construct($dsn = '', $force_run = false, $new = false) 53 { 51 function __construct($dsn = '', $force_run = false, $new = false) { 54 52 55 53 if ($dsn == '') { … … 101 99 * @return SC_Query シングルトンの SC_Query インスタンス 102 100 */ 103 static function getSingletonInstance($dsn = '', $force_run = false, $new = false) 104 { 101 static function getSingletonInstance($dsn = '', $force_run = false, $new = false) { 105 102 $objThis = SC_Query_Ex::getPoolInstance($dsn); 106 103 if (is_null($objThis)) { … … 122 119 * @return boolean 123 120 */ 124 function isError() 125 { 121 function isError() { 126 122 if (PEAR::isError($this->conn)) { 127 123 return true; … … 138 134 * @return integer 件数 139 135 */ 140 function count($table, $where = '', $arrWhereVal = array()) 141 { 136 function count($table, $where = '', $arrWhereVal = array()) { 142 137 return $this->get('COUNT(*)', $table, $where, $arrWhereVal); 143 138 } … … 151 146 * @return boolean 有無 152 147 */ 153 function exists($table, $where = '', $arrWhereVal = array()) 154 { 148 function exists($table, $where = '', $arrWhereVal = array()) { 155 149 $sql_inner = $this->getSql('*', $table, $where, $arrWhereVal); 156 150 $sql = "SELECT CASE WHEN EXISTS($sql_inner) THEN 1 ELSE 0 END"; … … 169 163 * @return array|null 170 164 */ 171 function select($cols, $from = '', $where = '', $arrWhereVal = array(), $fetchmode = MDB2_FETCHMODE_ASSOC) 172 { 165 function select($cols, $from = '', $where = '', $arrWhereVal = array(), $fetchmode = MDB2_FETCHMODE_ASSOC) { 173 166 $sqlse = $this->getSql($cols, $from, $where, $arrWhereVal); 174 167 return $this->getAll($sqlse, $arrWhereVal, $fetchmode); … … 181 174 * @return string SQL文 182 175 */ 183 function getLastQuery($disp = true) 184 { 176 function getLastQuery($disp = true) { 185 177 $sql = $this->conn->last_query; 186 178 if ($disp) { … … 196 188 * 失敗した場合は PEAR::Error オブジェクト 197 189 */ 198 function commit() 199 { 190 function commit() { 200 191 return $this->conn->commit(); 201 192 } … … 207 198 * 失敗した場合は PEAR::Error オブジェクト 208 199 */ 209 function begin() 210 { 200 function begin() { 211 201 return $this->conn->beginTransaction(); 212 202 } … … 218 208 * 失敗した場合は PEAR::Error オブジェクト 219 209 */ 220 function rollback() 221 { 210 function rollback() { 222 211 return $this->conn->rollback(); 223 212 } … … 228 217 * @return boolean トランザクションが開始されている場合 true 229 218 */ 230 function inTransaction() 231 { 219 function inTransaction() { 232 220 return $this->conn->inTransaction(); 233 221 } … … 240 228 * FIXME MDB2::exec() の実装であるべき 241 229 */ 242 function exec($str, $arrVal = array()) 243 { 230 function exec($str, $arrVal = array()) { 244 231 return $this->query($str, $arrVal); 245 232 } … … 254 241 * @return boolean 結果 255 242 */ 256 function doCallbackAll($cbFunc, $sql, $arrVal = array(), $fetchmode = MDB2_FETCHMODE_ASSOC) 257 { 243 function doCallbackAll($cbFunc, $sql, $arrVal = array(), $fetchmode = MDB2_FETCHMODE_ASSOC) { 258 244 259 245 $sql = $this->dbFactory->sfChangeMySQL($sql); … … 287 273 * @return array データを含む2次元配列。失敗した場合に 0 または DB_Error オブジェクトを返します。 288 274 */ 289 function getAll($sql, $arrVal = array(), $fetchmode = MDB2_FETCHMODE_ASSOC) 290 { 275 function getAll($sql, $arrVal = array(), $fetchmode = MDB2_FETCHMODE_ASSOC) { 291 276 292 277 $sql = $this->dbFactory->sfChangeMySQL($sql); … … 321 306 * @return string 構築済みの SELECT 文 322 307 */ 323 function getSql($cols, $from = '', $where = '', &$arrWhereVal = null) 324 { 308 function getSql($cols, $from = '', $where = '', &$arrWhereVal = null) { 325 309 $dbFactory = SC_DB_DBFactory_Ex::getInstance(); 326 310 … … 358 342 * @return SC_Query 自分自身のインスタンス 359 343 */ 360 function setOption($str) 361 { 344 function setOption($str) { 362 345 $this->option = $str; 363 346 return $this; … … 373 356 * @return SC_Query 自分自身のインスタンス 374 357 */ 375 function setLimitOffset($limit, $offset = 0) 376 { 358 function setLimitOffset($limit, $offset = 0) { 377 359 if (is_numeric($limit) && is_numeric($offset)) { 378 360 $this->conn->setLimit($limit, $offset); … … 389 371 * @return SC_Query 自分自身のインスタンス 390 372 */ 391 function setGroupBy($str) 392 { 373 function setGroupBy($str) { 393 374 if (strlen($str) == 0) { 394 375 $this->groupby = ''; … … 407 388 * @return SC_Query 自分自身のインスタンス 408 389 */ 409 function andWhere($str) 410 { 390 function andWhere($str) { 411 391 if ($this->where != '') { 412 392 $this->where .= ' AND ' . $str; … … 425 405 * @return SC_Query 自分自身のインスタンス 426 406 */ 427 function orWhere($str) 428 { 407 function orWhere($str) { 429 408 if ($this->where != '') { 430 409 $this->where .= ' OR ' . $str; … … 444 423 * @return SC_Query 自分自身のインスタンス 445 424 */ 446 function setWhere($where = '', $arrWhereVal = array()) 447 { 425 function setWhere($where = '', $arrWhereVal = array()) { 448 426 $this->where = $where; 449 427 $this->arrWhereVal = $arrWhereVal; … … 459 437 * @return SC_Query 自分自身のインスタンス 460 438 */ 461 function setOrder($str) 462 { 439 function setOrder($str) { 463 440 if (strlen($str) == 0) { 464 441 $this->order = ''; … … 477 454 * @return SC_Query 自分自身のインスタンス 478 455 */ 479 function setLimit($limit) 480 { 456 function setLimit($limit) { 481 457 if (is_numeric($limit)) { 482 458 $this->conn->setLimit($limit); … … 493 469 * @return SC_Query 自分自身のインスタンス 494 470 */ 495 function setOffset($offset) 496 { 471 function setOffset($offset) { 497 472 if (is_numeric($offset)) { 498 473 $this->conn->setLimit($this->conn->limit, $offset); … … 512 487 * @return integer|DB_Error|boolean 挿入件数またはエラー(DB_Error, false) 513 488 */ 514 function insert($table, $arrVal, $arrSql = array(), $arrSqlVal = array(), $from = '', $arrFromVal = array()) 515 { 489 function insert($table, $arrVal, $arrSql = array(), $arrSqlVal = array(), $from = '', $arrFromVal = array()) { 516 490 $strcol = ''; 517 491 $strval = ''; … … 570 544 * @return 571 545 */ 572 function update($table, $arrVal, $where = '', $arrWhereVal = array(), $arrRawSql = array(), $arrRawSqlVal = array()) 573 { 546 function update($table, $arrVal, $where = '', $arrWhereVal = array(), $arrRawSql = array(), $arrRawSqlVal = array()) { 574 547 $arrCol = array(); 575 548 $arrValForQuery = array(); … … 626 599 * @return integer MAX文の実行結果 627 600 */ 628 function max($col, $table, $where = '', $arrWhereVal = array()) 629 { 601 function max($col, $table, $where = '', $arrWhereVal = array()) { 630 602 $ret = $this->get("MAX($col)", $table, $where, $arrWhereVal); 631 603 return $ret; … … 641 613 * @return integer MIN文の実行結果 642 614 */ 643 function min($col, $table, $where = '', $arrWhereVal = array()) 644 { 615 function min($col, $table, $where = '', $arrWhereVal = array()) { 645 616 $ret = $this->get("MIN($col)", $table, $where, $arrWhereVal); 646 617 return $ret; … … 656 627 * @return mixed SQL の実行結果 657 628 */ 658 function get($col, $table = '', $where = '', $arrWhereVal = array()) 659 { 629 function get($col, $table = '', $where = '', $arrWhereVal = array()) { 660 630 $sqlse = $this->getSql($col, $table, $where, $arrWhereVal); 661 631 // SQL文の実行 … … 671 641 * @return mixed SQL の実行結果 672 642 */ 673 function getOne($sql, $arrVal = array()) 674 { 643 function getOne($sql, $arrVal = array()) { 675 644 676 645 $sql = $this->dbFactory->sfChangeMySQL($sql); … … 705 674 * @return array array('カラム名' => '値', ...)の連想配列 706 675 */ 707 function getRow($col, $table = '', $where = '', $arrWhereVal = array(), $fetchmode = MDB2_FETCHMODE_ASSOC) 708 { 676 function getRow($col, $table = '', $where = '', $arrWhereVal = array(), $fetchmode = MDB2_FETCHMODE_ASSOC) { 709 677 710 678 $sql = $this->getSql($col, $table, $where, $arrWhereVal); … … 739 707 * @return array SQL の実行結果の配列 740 708 */ 741 function getCol($col, $table = '', $where = '', $arrWhereVal = array()) 742 { 709 function getCol($col, $table = '', $where = '', $arrWhereVal = array()) { 743 710 $sql = $this->getSql($col, $table, $where, $arrWhereVal); 744 711 $sql = $this->dbFactory->sfChangeMySQL($sql); … … 771 738 * @return 772 739 */ 773 function delete($table, $where = '', $arrWhereVal = array()) 774 { 740 function delete($table, $where = '', $arrWhereVal = array()) { 775 741 if (strlen($where) <= 0) { 776 742 $sqlde = "DELETE FROM $table"; … … 788 754 * @param integer 次のシーケンス値 789 755 */ 790 function nextVal($seq_name) 791 { 756 function nextVal($seq_name) { 792 757 return $this->conn->nextID($seq_name); 793 758 } … … 799 764 * @return integer 現在のシーケンス値 800 765 */ 801 function currVal($seq_name) 802 { 766 function currVal($seq_name) { 803 767 return $this->conn->currID($seq_name); 804 768 } … … 811 775 * @return MDB2_OK 812 776 */ 813 function setVal($seq_name, $start) 814 { 777 function setVal($seq_name, $start) { 815 778 $objManager =& $this->conn->loadModule('Manager'); 816 779 … … 840 803 * @return array SQL の実行結果の配列 841 804 */ 842 function query($n ,$arr = array(), $ignore_err = false, $types = null, $result_types = MDB2_PREPARE_RESULT) 843 { 805 function query($n ,$arr = array(), $ignore_err = false, $types = null, $result_types = MDB2_PREPARE_RESULT) { 844 806 845 807 $n = $this->dbFactory->sfChangeMySQL($n); … … 866 828 * @return array シーケンス名の配列 867 829 */ 868 function listSequences() 869 { 830 function listSequences() { 870 831 $objManager =& $this->conn->loadModule('Manager'); 871 832 return $objManager->listSequences(); … … 877 838 * @return array テーブル名の配列 878 839 */ 879 function listTables() 880 { 840 function listTables() { 881 841 return $this->dbFactory->listTables($this); 882 842 } … … 888 848 * @return array 指定のテーブルのカラム名の配列 889 849 */ 890 function listTableFields($table) 891 { 850 function listTableFields($table) { 892 851 $objManager =& $this->conn->loadModule('Manager'); 893 852 return $objManager->listTableFields($table); … … 900 859 * @return array 指定のテーブルのインデックス一覧 901 860 */ 902 function listTableIndexes($table) 903 { 861 function listTableIndexes($table) { 904 862 $objManager =& $this->conn->loadModule('Manager'); 905 863 return $objManager->listTableIndexes($table); … … 914 872 * MySQLのtext型フィールドを指定する場合は $definition['length'] = 'text_field(NNN)' が必要 915 873 */ 916 function createIndex($table, $name, $definition) 917 { 874 function createIndex($table, $name, $definition) { 918 875 $definition = $this->dbFactory->sfGetCreateIndexDefinition($table, $name, $definition); 919 876 $objManager =& $this->conn->loadModule('Manager'); … … 927 884 * @param string $name インデックス名 928 885 */ 929 function dropIndex($table, $name) 930 { 886 function dropIndex($table, $name) { 931 887 $objManager =& $this->conn->loadModule('Manager'); 932 888 return $objManager->dropIndex($table, $name); … … 939 895 * @return array テーブル情報の配列 940 896 */ 941 function getTableInfo($table) 942 { 897 function getTableInfo($table) { 943 898 $objManager =& $this->conn->loadModule('Reverse'); 944 899 return $objManager->tableInfo($table, NULL); … … 956 911 * @return string クォートされた文字列 957 912 */ 958 function quote($val) 959 { 913 function quote($val) { 960 914 return $this->conn->quote($val); 961 915 } … … 968 922 * @return array テーブルに存在する列のみ抽出した連想配列 969 923 */ 970 function extractOnlyColsOf($table, $arrParams) 971 { 924 function extractOnlyColsOf($table, $arrParams) { 972 925 $arrCols = $this->listTableFields($table); 973 926 $arrResults = array(); … … 989 942 * @return MDB2_Statement_Common プリペアドステートメントインスタンス 990 943 */ 991 function prepare($sql, $types = null, $result_types = MDB2_PREPARE_RESULT) 992 { 944 function prepare($sql, $types = null, $result_types = MDB2_PREPARE_RESULT) { 993 945 $sth =& $this->conn->prepare($sql, $types, $result_types); 994 946 if (PEAR::isError($sth)) { … … 1007 959 * @return MDB2_Result 結果セットのインスタンス 1008 960 */ 1009 function execute(&$sth, $arrVal = array()) 1010 { 961 function execute(&$sth, $arrVal = array()) { 1011 962 1012 963 $arrStartInfo =& $this->lfStartDbTraceLog($sth, $arrVal); … … 1034 985 * @return string トレースしたエラー文字列 1035 986 */ 1036 function traceError($error, $sql = '', $arrVal = false) 1037 { 987 function traceError($error, $sql = '', $arrVal = false) { 1038 988 $err = "SQL: [$sql]\n"; 1039 989 if ($arrVal !== false) { … … 1052 1002 * エラー処理 1053 1003 */ 1054 function error($msg) 1055 { 1004 function error($msg) { 1056 1005 $msg = "DB処理でエラーが発生しました。\n" . $msg; 1057 1006 if (!$this->force_run) { … … 1072 1021 * @return array 実行結果の配列 1073 1022 */ 1074 function getQueryDefsFields($n ,$arr = array(), $ignore_err = false, $types = null, $result_types = MDB2_PREPARE_RESULT) 1075 { 1023 function getQueryDefsFields($n ,$arr = array(), $ignore_err = false, $types = null, $result_types = MDB2_PREPARE_RESULT) { 1076 1024 1077 1025 $n = $this->dbFactory->sfChangeMySQL($n); … … 1100 1048 * @return void 1101 1049 */ 1102 private function lfStartDbTraceLog(&$objSth, &$arrVal) 1103 { 1050 private function lfStartDbTraceLog(&$objSth, &$arrVal) { 1104 1051 if (!defined('SQL_QUERY_LOG_MODE') || SQL_QUERY_LOG_MODE === 0) { 1105 1052 return; … … 1136 1083 * @return void 1137 1084 */ 1138 private function lfEndDbTraceLog(&$arrStartInfo, &$objSth, &$arrVal) 1139 { 1085 private function lfEndDbTraceLog(&$arrStartInfo, &$objSth, &$arrVal) { 1140 1086 if (!defined('SQL_QUERY_LOG_MODE') || SQL_QUERY_LOG_MODE === 0) { 1141 1087 return; … … 1168 1114 * @return SC_Query プールしたインスタンス 1169 1115 */ 1170 static function setPoolInstance(&$objThis, $dsn = '') 1171 { 1116 static function setPoolInstance(&$objThis, $dsn = '') { 1172 1117 $key_str = serialize($dsn); 1173 1118 return SC_Query_Ex::$arrPoolInstance[$key_str] = $objThis; … … 1180 1125 * @return SC_Query|null 1181 1126 */ 1182 static function getPoolInstance($dsn = '') 1183 { 1127 static function getPoolInstance($dsn = '') { 1184 1128 $key_str = serialize($dsn); 1185 1129 if (isset(SC_Query_Ex::$arrPoolInstance[$key_str])) {
Note: See TracChangeset
for help on using the changeset viewer.