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