| 1 | <?php |
---|
| 2 | /* |
---|
| 3 | * This file is part of EC-CUBE |
---|
| 4 | * |
---|
| 5 | * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved. |
---|
| 6 | * |
---|
| 7 | * http://www.lockon.co.jp/ |
---|
| 8 | * |
---|
| 9 | * This program is free software; you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License |
---|
| 11 | * as published by the Free Software Foundation; either version 2 |
---|
| 12 | * of the License, or (at your option) any later version. |
---|
| 13 | * |
---|
| 14 | * This program is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this program; if not, write to the Free Software |
---|
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | /** |
---|
| 25 | * SQLの構築・実行を行う |
---|
| 26 | * |
---|
| 27 | * @author LOCKON CO.,LTD. |
---|
| 28 | * @version $Id$ |
---|
| 29 | */ |
---|
| 30 | class SC_Query { |
---|
| 31 | var $option; |
---|
| 32 | var $where; |
---|
| 33 | var $conn; |
---|
| 34 | var $groupby; |
---|
| 35 | var $order; |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * コンストラクタ. |
---|
| 39 | * |
---|
| 40 | * @param $dsn |
---|
| 41 | * @param boolean $err_disp エラー表示を行うかどうか |
---|
| 42 | * @param boolean $new 新規に接続を行うかどうか |
---|
| 43 | * @return SC_Query |
---|
| 44 | */ |
---|
| 45 | function SC_Query($dsn = "", $err_disp = true, $new = false) { |
---|
| 46 | $this->conn = new SC_DBconn($dsn, $err_disp, $new); |
---|
| 47 | $this->where = ""; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * エラー判定を行う. |
---|
| 52 | * |
---|
| 53 | * @return boolean |
---|
| 54 | */ |
---|
| 55 | function isError() { |
---|
| 56 | if(PEAR::isError($this->conn->conn)) { |
---|
| 57 | return true; |
---|
| 58 | } |
---|
| 59 | return false; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * COUNT文を実行する. |
---|
| 64 | * |
---|
| 65 | * @param string $table テーブル名 |
---|
| 66 | * @param string $where where句 |
---|
| 67 | * @param array $arrval プレースホルダ |
---|
| 68 | * @return integer 件数 |
---|
| 69 | */ |
---|
| 70 | function count($table, $where = "", $arrval = array()) { |
---|
| 71 | if(strlen($where) <= 0) { |
---|
| 72 | $sqlse = "SELECT COUNT(*) FROM $table"; |
---|
| 73 | } else { |
---|
| 74 | $sqlse = "SELECT COUNT(*) FROM $table WHERE $where"; |
---|
| 75 | } |
---|
| 76 | // カウント文の実行 |
---|
| 77 | $ret = $this->conn->getOne($sqlse, $arrval); |
---|
| 78 | return $ret; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | /** |
---|
| 82 | * SELECT文を実行する. |
---|
| 83 | * |
---|
| 84 | * @param string $col カラム名. 複数カラムの場合はカンマ区切りで書く |
---|
| 85 | * @param string $table テーブル名 |
---|
| 86 | * @param string $where WHERE句 |
---|
| 87 | * @param array $arrval プレースホルダ |
---|
| 88 | * @param integer $fetchmode 使用するフェッチモード。デフォルトは DB_FETCHMODE_ASSOC。 |
---|
| 89 | * @return array|null |
---|
| 90 | */ |
---|
| 91 | function select($col, $table, $where = "", $arrval = array(), $fetchmode = DB_FETCHMODE_ASSOC) { |
---|
| 92 | $sqlse = $this->getsql($col, $table, $where); |
---|
| 93 | $ret = $this->conn->getAll($sqlse, $arrval, $fetchmode); |
---|
| 94 | return $ret; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * 直前に実行されたSQL文を取得する. |
---|
| 99 | * SC_DBconn::getLastQuery() を利用. |
---|
| 100 | * |
---|
| 101 | * @param boolean $disp trueの場合、画面出力を行う. |
---|
| 102 | * @return string SQL文 |
---|
| 103 | */ |
---|
| 104 | function getLastQuery($disp = true) { |
---|
| 105 | return $this->conn->getLastQuery($disp); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | function commit() { |
---|
| 109 | $this->conn->query("COMMIT"); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | function begin() { |
---|
| 113 | $this->conn->query("BEGIN"); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | function rollback() { |
---|
| 117 | $this->conn->query("ROLLBACK"); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | function exec($str, $arrval = array()) { |
---|
| 121 | $this->conn->query($str, $arrval); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | function autoselect($col, $table, $arrwhere = array(), $arrcon = array()) { |
---|
| 125 | $strw = ""; |
---|
| 126 | $find = false; |
---|
| 127 | foreach ($arrwhere as $key => $val) { |
---|
| 128 | if(strlen($val) > 0) { |
---|
| 129 | if(strlen($strw) <= 0) { |
---|
| 130 | $strw .= $key ." LIKE ?"; |
---|
| 131 | } else if(strlen($arrcon[$key]) > 0) { |
---|
| 132 | $strw .= " ". $arrcon[$key]. " " . $key ." LIKE ?"; |
---|
| 133 | } else { |
---|
| 134 | $strw .= " AND " . $key ." LIKE ?"; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | $arrval[] = $val; |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | if(strlen($strw) > 0) { |
---|
| 142 | $sqlse = "SELECT $col FROM $table WHERE $strw ".$this->option; |
---|
| 143 | } else { |
---|
| 144 | $sqlse = "SELECT $col FROM $table ".$this->option; |
---|
| 145 | } |
---|
| 146 | $ret = $this->conn->getAll($sqlse, $arrval); |
---|
| 147 | return $ret; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /** |
---|
| 151 | * クエリを実行し、全ての行を返す |
---|
| 152 | * |
---|
| 153 | * @param string $sql SQL クエリ |
---|
| 154 | * @param array $arrVal プリペアドステートメントの実行時に使用される配列。配列の要素数は、クエリ内のプレースホルダの数と同じでなければなりません。 |
---|
| 155 | * @param integer $fetchmode 使用するフェッチモード。デフォルトは DB_FETCHMODE_ASSOC。 |
---|
| 156 | * @return array データを含む2次元配列。失敗した場合に 0 または DB_Error オブジェクトを返します。 |
---|
| 157 | */ |
---|
| 158 | function getall($sql, $arrval = array(), $fetchmode = DB_FETCHMODE_ASSOC) { |
---|
| 159 | $ret = $this->conn->getAll($sql, $arrval, $fetchmode); |
---|
| 160 | return $ret; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | function getsql($col, $table, $where) { |
---|
| 164 | if($where != "") { |
---|
| 165 | // 引数の$whereを優先して実行する。 |
---|
| 166 | $sqlse = "SELECT $col FROM $table WHERE $where " . $this->groupby . " " . $this->order . " " . $this->option; |
---|
| 167 | } else { |
---|
| 168 | if($this->where != "") { |
---|
| 169 | $sqlse = "SELECT $col FROM $table WHERE $this->where " . $this->groupby . " " . $this->order . " " . $this->option; |
---|
| 170 | } else { |
---|
| 171 | $sqlse = "SELECT $col FROM $table " . $this->groupby . " " . $this->order . " " . $this->option; |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | return $sqlse; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | function setoption($str) { |
---|
| 178 | $this->option = $str; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | function setlimitoffset($limit, $offset = 0, $return = false) { |
---|
| 182 | if (is_numeric($limit) && is_numeric($offset)){ |
---|
| 183 | |
---|
| 184 | $option = " LIMIT " . $limit; |
---|
| 185 | $option.= " OFFSET " . $offset; |
---|
| 186 | |
---|
| 187 | if($return){ |
---|
| 188 | return $option; |
---|
| 189 | }else{ |
---|
| 190 | $this->option.= $option; |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | function setgroupby($str) { |
---|
| 196 | if (strlen($str) == 0) { |
---|
| 197 | $this->groupby = ''; |
---|
| 198 | } else { |
---|
| 199 | $this->groupby = "GROUP BY " . $str; |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | function andwhere($str) { |
---|
| 204 | if($this->where != "") { |
---|
| 205 | $this->where .= " AND " . $str; |
---|
| 206 | } else { |
---|
| 207 | $this->where = $str; |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | function orwhere($str) { |
---|
| 212 | if($this->where != "") { |
---|
| 213 | $this->where .= " OR " . $str; |
---|
| 214 | } else { |
---|
| 215 | $this->where = $str; |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | function setwhere($str) { |
---|
| 220 | $this->where = $str; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | function setorder($str) { |
---|
| 224 | if (strlen($str) == 0) { |
---|
| 225 | $this->order = ''; |
---|
| 226 | } else { |
---|
| 227 | $this->order = "ORDER BY " . $str; |
---|
| 228 | } |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | |
---|
| 232 | function setlimit($limit){ |
---|
| 233 | if ( is_numeric($limit)){ |
---|
| 234 | $this->option = " LIMIT " .$limit; |
---|
| 235 | } |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | function setoffset($offset) { |
---|
| 239 | if ( is_numeric($offset)){ |
---|
| 240 | $this->offset = " OFFSET " .$offset; |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | /** |
---|
| 245 | * INSERT文を実行する. |
---|
| 246 | * |
---|
| 247 | * @param string $table テーブル名 |
---|
| 248 | * @param array $sqlval array('カラム名' => '値',...)の連想配列 |
---|
| 249 | * @return |
---|
| 250 | */ |
---|
| 251 | function insert($table, $sqlval) { |
---|
| 252 | $strcol = ''; |
---|
| 253 | $strval = ''; |
---|
| 254 | $find = false; |
---|
| 255 | |
---|
| 256 | if(count($sqlval) <= 0 ) return false; |
---|
| 257 | |
---|
| 258 | foreach ($sqlval as $key => $val) { |
---|
| 259 | $strcol .= $key . ','; |
---|
| 260 | if(eregi("^Now\(\)$", $val)) { |
---|
| 261 | $strval .= 'Now(),'; |
---|
| 262 | } else { |
---|
| 263 | $strval .= '?,'; |
---|
| 264 | $arrval[] = $val; |
---|
| 265 | } |
---|
| 266 | $find = true; |
---|
| 267 | } |
---|
| 268 | if(!$find) { |
---|
| 269 | return false; |
---|
| 270 | } |
---|
| 271 | // 文末の","を削除 |
---|
| 272 | $strcol = ereg_replace(",$","",$strcol); |
---|
| 273 | // 文末の","を削除 |
---|
| 274 | $strval = ereg_replace(",$","",$strval); |
---|
| 275 | $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")"; |
---|
| 276 | // INSERT文の実行 |
---|
| 277 | $ret = $this->conn->query($sqlin, $arrval); |
---|
| 278 | |
---|
| 279 | return $ret; |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | /** |
---|
| 283 | * UPDATE文を実行する. |
---|
| 284 | * |
---|
| 285 | * @param string $table テーブル名 |
---|
| 286 | * @param array $sqlval array('カラム名' => '値',...)の連想配列 |
---|
| 287 | * @param string $where WHERE句 |
---|
| 288 | * @param array $arrValIn WHERE句用のプレースホルダ配列 (従来は追加カラム用も兼ねていた) |
---|
| 289 | * @param array $arrRawSql 追加カラム |
---|
| 290 | * @param array $arrRawSqlVal 追加カラム用のプレースホルダ配列 |
---|
| 291 | * @return |
---|
| 292 | */ |
---|
| 293 | function update($table, $sqlval, $where = "", $arrValIn = array(), $arrRawSql = array(), $arrRawSqlVal = array()) { |
---|
| 294 | $arrCol = array(); |
---|
| 295 | $arrVal = array(); |
---|
| 296 | $find = false; |
---|
| 297 | foreach ($sqlval as $key => $val) { |
---|
| 298 | if (eregi("^Now\(\)$", $val)) { |
---|
| 299 | $arrCol[] = $key . '= Now()'; |
---|
| 300 | } else { |
---|
| 301 | $arrCol[] = $key . '= ?'; |
---|
| 302 | $arrVal[] = $val; |
---|
| 303 | } |
---|
| 304 | $find = true; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | if ($arrRawSql != "") { |
---|
| 308 | foreach($arrRawSql as $key => $val) { |
---|
| 309 | $arrCol[] = "$key = $val"; |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | $arrVal = array_merge($arrVal, $arrRawSqlVal); |
---|
| 314 | |
---|
| 315 | if (empty($arrCol)) { |
---|
| 316 | return false; |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | // 文末の","を削除 |
---|
| 320 | $strcol = implode(', ', $arrCol); |
---|
| 321 | |
---|
| 322 | if (is_array($arrValIn)) { // 旧版との互換用 |
---|
| 323 | // プレースホルダー用に配列を追加 |
---|
| 324 | $arrVal = array_merge($arrVal, $arrValIn); |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | $sqlup = "UPDATE $table SET $strcol"; |
---|
| 328 | if (strlen($where) >= 1) { |
---|
| 329 | $sqlup .= " WHERE $where"; |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | // UPDATE文の実行 |
---|
| 333 | return $this->conn->query($sqlup, $arrVal); |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | // MAX文の実行 |
---|
| 337 | function max($table, $col, $where = "", $arrval = array()) { |
---|
| 338 | if(strlen($where) <= 0) { |
---|
| 339 | $sqlse = "SELECT MAX($col) FROM $table"; |
---|
| 340 | } else { |
---|
| 341 | $sqlse = "SELECT MAX($col) FROM $table WHERE $where"; |
---|
| 342 | } |
---|
| 343 | // MAX文の実行 |
---|
| 344 | $ret = $this->conn->getOne($sqlse, $arrval); |
---|
| 345 | return $ret; |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | // MIN文の実行 |
---|
| 349 | function min($table, $col, $where = "", $arrval = array()) { |
---|
| 350 | if(strlen($where) <= 0) { |
---|
| 351 | $sqlse = "SELECT MIN($col) FROM $table"; |
---|
| 352 | } else { |
---|
| 353 | $sqlse = "SELECT MIN($col) FROM $table WHERE $where"; |
---|
| 354 | } |
---|
| 355 | // MIN文の実行 |
---|
| 356 | $ret = $this->conn->getOne($sqlse, $arrval); |
---|
| 357 | return $ret; |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | // 特定のカラムの値を取得 |
---|
| 361 | function get($table, $col, $where = "", $arrval = array()) { |
---|
| 362 | if(strlen($where) <= 0) { |
---|
| 363 | $sqlse = "SELECT $col FROM $table"; |
---|
| 364 | } else { |
---|
| 365 | $sqlse = "SELECT $col FROM $table WHERE $where"; |
---|
| 366 | } |
---|
| 367 | // SQL文の実行 |
---|
| 368 | $ret = $this->conn->getOne($sqlse, $arrval); |
---|
| 369 | return $ret; |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | function getone($sql, $arrval = array()) { |
---|
| 373 | // SQL文の実行 |
---|
| 374 | $ret = $this->conn->getOne($sql, $arrval); |
---|
| 375 | return $ret; |
---|
| 376 | |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | /** |
---|
| 380 | * 一行をカラム名をキーとした連想配列として取得 |
---|
| 381 | * |
---|
| 382 | * @param string $table テーブル名 |
---|
| 383 | * @param string $col カラム名 |
---|
| 384 | * @param string $where WHERE句 |
---|
| 385 | * @param array $arrVal プレースホルダ配列 |
---|
| 386 | * @param integer $fetchmode 使用するフェッチモード。デフォルトは DB_FETCHMODE_ASSOC。 |
---|
| 387 | * @return array array('カラム名' => '値', ...)の連想配列 |
---|
| 388 | */ |
---|
| 389 | function getRow($table, $col, $where = "", $arrVal = array(), $fetchmode = DB_FETCHMODE_ASSOC) { |
---|
| 390 | $sqlse = "SELECT $col FROM $table"; |
---|
| 391 | |
---|
| 392 | if (strlen($where) >= 1) { |
---|
| 393 | $sqlse .= " WHERE $where"; |
---|
| 394 | } |
---|
| 395 | // SQL文の実行 |
---|
| 396 | return $this->conn->getRow($sqlse, $arrVal ,$fetchmode); |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | // 1列取得 |
---|
| 400 | function getCol($table, $col, $where = "", $arrval = array()) { |
---|
| 401 | if (strlen($where) <= 0) { |
---|
| 402 | $sqlse = "SELECT $col FROM $table"; |
---|
| 403 | } else { |
---|
| 404 | $sqlse = "SELECT $col FROM $table WHERE $where"; |
---|
| 405 | } |
---|
| 406 | // SQL文の実行 |
---|
| 407 | return $this->conn->getCol($sqlse, 0, $arrval); |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | /** |
---|
| 411 | * レコードの削除 |
---|
| 412 | * |
---|
| 413 | * @param string $table テーブル名 |
---|
| 414 | * @param string $where WHERE句 |
---|
| 415 | * @param array $arrval プレースホルダ |
---|
| 416 | * @return |
---|
| 417 | */ |
---|
| 418 | function delete($table, $where = "", $arrval = array()) { |
---|
| 419 | if(strlen($where) <= 0) { |
---|
| 420 | $sqlde = "DELETE FROM $table"; |
---|
| 421 | } else { |
---|
| 422 | $sqlde = "DELETE FROM $table WHERE $where"; |
---|
| 423 | } |
---|
| 424 | $ret = $this->conn->query($sqlde, $arrval); |
---|
| 425 | return $ret; |
---|
| 426 | } |
---|
| 427 | |
---|
| 428 | function nextval($table, $colname) { |
---|
| 429 | $sql = ""; |
---|
| 430 | // postgresqlとmysqlとで処理を分ける |
---|
| 431 | if (DB_TYPE == "pgsql") { |
---|
| 432 | $seqtable = $table . "_" . $colname . "_seq"; |
---|
| 433 | $sql = "SELECT NEXTVAL('$seqtable')"; |
---|
| 434 | }else if (DB_TYPE == "mysql") { |
---|
| 435 | $sql = "SELECT last_insert_id();"; |
---|
| 436 | } |
---|
| 437 | $ret = $this->conn->getOne($sql); |
---|
| 438 | |
---|
| 439 | return $ret; |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | function currval($table, $colname) { |
---|
| 443 | $sql = ""; |
---|
| 444 | if (DB_TYPE == "pgsql") { |
---|
| 445 | $seqtable = $table . "_" . $colname . "_seq"; |
---|
| 446 | $sql = "SELECT CURRVAL('$seqtable')"; |
---|
| 447 | }else if (DB_TYPE == "mysql") { |
---|
| 448 | $sql = "SELECT last_insert_id();"; |
---|
| 449 | } |
---|
| 450 | $ret = $this->conn->getOne($sql); |
---|
| 451 | |
---|
| 452 | return $ret; |
---|
| 453 | } |
---|
| 454 | |
---|
| 455 | function setval($table, $colname, $data) { |
---|
| 456 | $sql = ""; |
---|
| 457 | if (DB_TYPE == "pgsql") { |
---|
| 458 | $seqtable = $table . "_" . $colname . "_seq"; |
---|
| 459 | $sql = "SELECT SETVAL('$seqtable', $data)"; |
---|
| 460 | $ret = $this->conn->getOne($sql); |
---|
| 461 | }else if (DB_TYPE == "mysql") { |
---|
| 462 | $sql = "ALTER TABLE $table AUTO_INCREMENT=$data"; |
---|
| 463 | $ret = $this->conn->query($sql); |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | return $ret; |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | function query($n ,$arr = "", $ignore_err = false){ |
---|
| 470 | $result = $this->conn->query($n, $arr, $ignore_err); |
---|
| 471 | return $result; |
---|
| 472 | } |
---|
| 473 | |
---|
| 474 | /** |
---|
| 475 | * auto_incrementを取得する. |
---|
| 476 | * |
---|
| 477 | * @param string $table_name テーブル名 |
---|
| 478 | * @return integer |
---|
| 479 | */ |
---|
| 480 | function get_auto_increment($table_name){ |
---|
| 481 | // ロックする |
---|
| 482 | $this->query("LOCK TABLES $table_name WRITE"); |
---|
| 483 | |
---|
| 484 | // 次のIncrementを取得 |
---|
| 485 | $arrRet = $this->getAll("SHOW TABLE STATUS LIKE ?", array($table_name)); |
---|
| 486 | $auto_inc_no = $arrRet[0]["Auto_increment"]; |
---|
| 487 | |
---|
| 488 | // 値をカウントアップしておく |
---|
| 489 | $this->conn->query("ALTER TABLE $table_name AUTO_INCREMENT=?" , $auto_inc_no + 1); |
---|
| 490 | |
---|
| 491 | // 解除する |
---|
| 492 | $this->query('UNLOCK TABLES'); |
---|
| 493 | |
---|
| 494 | return $auto_inc_no; |
---|
| 495 | } |
---|
| 496 | } |
---|
| 497 | |
---|
| 498 | ?> |
---|