| 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 | class SC_Query { |
|---|
| 25 | var $option; |
|---|
| 26 | var $where; |
|---|
| 27 | var $conn; |
|---|
| 28 | var $groupby; |
|---|
| 29 | var $order; |
|---|
| 30 | |
|---|
| 31 | // コンストラクタ |
|---|
| 32 | /* |
|---|
| 33 | $err_disp:エラー表示を行うか |
|---|
| 34 | $new:新規に接続を行うか |
|---|
| 35 | */ |
|---|
| 36 | function SC_Query($dsn = "", $err_disp = true, $new = false) { |
|---|
| 37 | $this->conn = new SC_DBconn($dsn, $err_disp, $new); |
|---|
| 38 | $this->where = ""; |
|---|
| 39 | return $this->conn; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | // エラー判定 |
|---|
| 43 | function isError() { |
|---|
| 44 | if(PEAR::isError($this->conn->conn)) { |
|---|
| 45 | return true; |
|---|
| 46 | } |
|---|
| 47 | return false; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | // COUNT文の実行 |
|---|
| 51 | function count($table, $where = "", $arrval = array()) { |
|---|
| 52 | if(strlen($where) <= 0) { |
|---|
| 53 | $sqlse = "SELECT COUNT(*) FROM $table"; |
|---|
| 54 | } else { |
|---|
| 55 | $sqlse = "SELECT COUNT(*) FROM $table WHERE $where"; |
|---|
| 56 | } |
|---|
| 57 | // カウント文の実行 |
|---|
| 58 | $ret = $this->conn->getOne($sqlse, $arrval); |
|---|
| 59 | return $ret; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | function select($col, $table, $where = "", $arrval = array()){ |
|---|
| 63 | $sqlse = $this->getsql($col, $table, $where); |
|---|
| 64 | // DBに依存した SQL へ変換 |
|---|
| 65 | $dbFactory = SC_DB_DBFactory::getInstance(); |
|---|
| 66 | $sqlse = $dbFactory->sfChangeMySQL($sqlse); |
|---|
| 67 | $ret = $this->conn->getAll($sqlse, $arrval); |
|---|
| 68 | return $ret; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | function getLastQuery($disp = true) { |
|---|
| 72 | $sql = $this->conn->conn->last_query; |
|---|
| 73 | if($disp) { |
|---|
| 74 | print($sql.";<br />\n"); |
|---|
| 75 | } |
|---|
| 76 | return $sql; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | function commit() { |
|---|
| 80 | $this->conn->query("COMMIT"); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | function begin() { |
|---|
| 84 | $this->conn->query("BEGIN"); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | function rollback() { |
|---|
| 88 | $this->conn->query("ROLLBACK"); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | function exec($str, $arrval = array()) { |
|---|
| 92 | $this->conn->query($str, $arrval); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | function autoselect($col, $table, $arrwhere = array(), $arrcon = array()) { |
|---|
| 96 | $strw = ""; |
|---|
| 97 | $find = false; |
|---|
| 98 | foreach ($arrwhere as $key => $val) { |
|---|
| 99 | if(strlen($val) > 0) { |
|---|
| 100 | if(strlen($strw) <= 0) { |
|---|
| 101 | $strw .= $key ." LIKE ?"; |
|---|
| 102 | } else if(strlen($arrcon[$key]) > 0) { |
|---|
| 103 | $strw .= " ". $arrcon[$key]. " " . $key ." LIKE ?"; |
|---|
| 104 | } else { |
|---|
| 105 | $strw .= " AND " . $key ." LIKE ?"; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | $arrval[] = $val; |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | if(strlen($strw) > 0) { |
|---|
| 113 | $sqlse = "SELECT $col FROM $table WHERE $strw ".$this->option; |
|---|
| 114 | } else { |
|---|
| 115 | $sqlse = "SELECT $col FROM $table ".$this->option; |
|---|
| 116 | } |
|---|
| 117 | $ret = $this->conn->getAll($sqlse, $arrval); |
|---|
| 118 | return $ret; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | function getall($sql, $arrval = array()) { |
|---|
| 122 | $ret = $this->conn->getAll($sql, $arrval); |
|---|
| 123 | return $ret; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | function getsql($col, $table, $where) { |
|---|
| 127 | if($where != "") { |
|---|
| 128 | // 引数の$whereを優先して実行する。 |
|---|
| 129 | $sqlse = "SELECT $col FROM $table WHERE $where " . $this->groupby . " " . $this->order . " " . $this->option; |
|---|
| 130 | } else { |
|---|
| 131 | if($this->where != "") { |
|---|
| 132 | $sqlse = "SELECT $col FROM $table WHERE $this->where " . $this->groupby . " " . $this->order . " " . $this->option; |
|---|
| 133 | } else { |
|---|
| 134 | $sqlse = "SELECT $col FROM $table " . $this->groupby . " " . $this->order . " " . $this->option; |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | return $sqlse; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | function setoption($str) { |
|---|
| 141 | $this->option = $str; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | function setlimitoffset($limit, $offset = 0, $return = false) { |
|---|
| 145 | if (is_numeric($limit) && is_numeric($offset)){ |
|---|
| 146 | |
|---|
| 147 | $option = " LIMIT " . $limit; |
|---|
| 148 | $option.= " OFFSET " . $offset; |
|---|
| 149 | |
|---|
| 150 | if($return){ |
|---|
| 151 | return $option; |
|---|
| 152 | }else{ |
|---|
| 153 | $this->option.= $option; |
|---|
| 154 | } |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | function setgroupby($str) { |
|---|
| 159 | $this->groupby = "GROUP BY " . $str; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | function andwhere($str) { |
|---|
| 163 | if($this->where != "") { |
|---|
| 164 | $this->where .= " AND " . $str; |
|---|
| 165 | } else { |
|---|
| 166 | $this->where = $str; |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | function orwhere($str) { |
|---|
| 171 | if($this->where != "") { |
|---|
| 172 | $this->where .= " OR " . $str; |
|---|
| 173 | } else { |
|---|
| 174 | $this->where = $str; |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | function setwhere($str) { |
|---|
| 179 | $this->where = $str; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | function setorder($str) { |
|---|
| 183 | $this->order = "ORDER BY " . $str; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | function setlimit($limit){ |
|---|
| 188 | if ( is_numeric($limit)){ |
|---|
| 189 | $this->option = " LIMIT " .$limit; |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | function setoffset($offset) { |
|---|
| 194 | if ( is_numeric($offset)){ |
|---|
| 195 | $this->offset = " OFFSET " .$offset; |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | // INSERT文の生成・実行 |
|---|
| 201 | // $table :テーブル名 |
|---|
| 202 | // $sqlval :列名 => 値の格納されたハッシュ配列 |
|---|
| 203 | function insert($table, $sqlval) { |
|---|
| 204 | $strcol = ''; |
|---|
| 205 | $strval = ''; |
|---|
| 206 | $find = false; |
|---|
| 207 | |
|---|
| 208 | if(count($sqlval) <= 0 ) return false; |
|---|
| 209 | |
|---|
| 210 | foreach ($sqlval as $key => $val) { |
|---|
| 211 | $strcol .= $key . ','; |
|---|
| 212 | if(eregi("^Now\(\)$", $val)) { |
|---|
| 213 | $strval .= 'Now(),';
|
|---|
| 214 | // 先頭に~があるとプレースホルダーしない。 |
|---|
| 215 | } else if(ereg("^~", $val)) {
|
|---|
| 216 | $strval .= ereg_replace("^~", "", $val);
|
|---|
| 217 | } else { |
|---|
| 218 | $strval .= '?,'; |
|---|
| 219 | if($val != ""){ |
|---|
| 220 | $arrval[] = $val; |
|---|
| 221 | } else { |
|---|
| 222 | $arrval[] = NULL; |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | $find = true; |
|---|
| 226 | } |
|---|
| 227 | if(!$find) { |
|---|
| 228 | return false; |
|---|
| 229 | } |
|---|
| 230 | // 文末の","を削除 |
|---|
| 231 | $strcol = ereg_replace(",$","",$strcol); |
|---|
| 232 | // 文末の","を削除 |
|---|
| 233 | $strval = ereg_replace(",$","",$strval); |
|---|
| 234 | $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")"; |
|---|
| 235 | |
|---|
| 236 | // INSERT文の実行 |
|---|
| 237 | $ret = $this->conn->query($sqlin, $arrval); |
|---|
| 238 | |
|---|
| 239 | return $ret; |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | // INSERT文の生成・実行 |
|---|
| 243 | // $table :テーブル名 |
|---|
| 244 | // $sqlval :列名 => 値の格納されたハッシュ配列 |
|---|
| 245 | function fast_insert($table, $sqlval) { |
|---|
| 246 | $strcol = ''; |
|---|
| 247 | $strval = ''; |
|---|
| 248 | $find = false; |
|---|
| 249 | |
|---|
| 250 | foreach ($sqlval as $key => $val) { |
|---|
| 251 | $strcol .= $key . ','; |
|---|
| 252 | if($val != ""){ |
|---|
| 253 | $eval = pg_escape_string($val); |
|---|
| 254 | $strval .= "'$eval',"; |
|---|
| 255 | } else { |
|---|
| 256 | $strval .= "NULL,"; |
|---|
| 257 | } |
|---|
| 258 | $find = true; |
|---|
| 259 | } |
|---|
| 260 | if(!$find) { |
|---|
| 261 | return false; |
|---|
| 262 | } |
|---|
| 263 | // 文末の","を削除 |
|---|
| 264 | $strcol = ereg_replace(",$","",$strcol); |
|---|
| 265 | // 文末の","を削除 |
|---|
| 266 | $strval = ereg_replace(",$","",$strval); |
|---|
| 267 | $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")"; |
|---|
| 268 | |
|---|
| 269 | // INSERT文の実行 |
|---|
| 270 | $ret = $this->conn->query($sqlin); |
|---|
| 271 | |
|---|
| 272 | return $ret; |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | // UPDATE文の生成・実行 |
|---|
| 277 | // $table :テーブル名 |
|---|
| 278 | // $sqlval :列名 => 値の格納されたハッシュ配列 |
|---|
| 279 | // $where :WHERE文字列 |
|---|
| 280 | function update($table, $sqlval, $where = "", $arradd = "", $addcol = "") { |
|---|
| 281 | $strcol = ''; |
|---|
| 282 | $strval = ''; |
|---|
| 283 | $find = false; |
|---|
| 284 | foreach ($sqlval as $key => $val) { |
|---|
| 285 | if(eregi("^Now\(\)$", $val)) { |
|---|
| 286 | $strcol .= $key . '= Now(),';
|
|---|
| 287 | // 先頭に~があるとプレースホルダーしない。 |
|---|
| 288 | } else if(ereg("^~", $val)) {
|
|---|
| 289 | $strcol .= $key . "=" . ereg_replace("^~", "", $val) . ",";
|
|---|
| 290 | } else { |
|---|
| 291 | $strcol .= $key . '= ?,'; |
|---|
| 292 | if($val != ""){ |
|---|
| 293 | $arrval[] = $val; |
|---|
| 294 | } else { |
|---|
| 295 | $arrval[] = NULL; |
|---|
| 296 | } |
|---|
| 297 | } |
|---|
| 298 | $find = true; |
|---|
| 299 | } |
|---|
| 300 | if(!$find) { |
|---|
| 301 | return false; |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | if($addcol != "") { |
|---|
| 305 | foreach($addcol as $key => $val) { |
|---|
| 306 | $strcol .= "$key = $val,"; |
|---|
| 307 | } |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | // 文末の","を削除 |
|---|
| 311 | $strcol = ereg_replace(",$","",$strcol); |
|---|
| 312 | // 文末の","を削除 |
|---|
| 313 | $strval = ereg_replace(",$","",$strval); |
|---|
| 314 | |
|---|
| 315 | if($where != "") { |
|---|
| 316 | $sqlup = "UPDATE $table SET $strcol WHERE $where"; |
|---|
| 317 | } else { |
|---|
| 318 | $sqlup = "UPDATE $table SET $strcol"; |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | if(is_array($arradd)) { |
|---|
| 322 | // プレースホルダー用に配列を追加 |
|---|
| 323 | foreach($arradd as $val) { |
|---|
| 324 | $arrval[] = $val; |
|---|
| 325 | } |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | // INSERT文の実行 |
|---|
| 329 | $ret = $this->conn->query($sqlup, $arrval); |
|---|
| 330 | return $ret; |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | // MAX文の実行 |
|---|
| 334 | function max($table, $col, $where = "", $arrval = array()) { |
|---|
| 335 | if(strlen($where) <= 0) { |
|---|
| 336 | $sqlse = "SELECT MAX($col) FROM $table"; |
|---|
| 337 | } else { |
|---|
| 338 | $sqlse = "SELECT MAX($col) FROM $table WHERE $where"; |
|---|
| 339 | } |
|---|
| 340 | // MAX文の実行 |
|---|
| 341 | $ret = $this->conn->getOne($sqlse, $arrval); |
|---|
| 342 | return $ret; |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | // MIN文の実行 |
|---|
| 346 | function min($table, $col, $where = "", $arrval = array()) { |
|---|
| 347 | if(strlen($where) <= 0) { |
|---|
| 348 | $sqlse = "SELECT MIN($col) FROM $table"; |
|---|
| 349 | } else { |
|---|
| 350 | $sqlse = "SELECT MIN($col) FROM $table WHERE $where"; |
|---|
| 351 | } |
|---|
| 352 | // MIN文の実行 |
|---|
| 353 | $ret = $this->conn->getOne($sqlse, $arrval); |
|---|
| 354 | return $ret; |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | // 特定のカラムの値を取得 |
|---|
| 358 | function get($table, $col, $where = "", $arrval = array()) { |
|---|
| 359 | if(strlen($where) <= 0) { |
|---|
| 360 | $sqlse = "SELECT $col FROM $table"; |
|---|
| 361 | } else { |
|---|
| 362 | $sqlse = "SELECT $col FROM $table WHERE $where"; |
|---|
| 363 | } |
|---|
| 364 | // SQL文の実行 |
|---|
| 365 | $ret = $this->conn->getOne($sqlse, $arrval); |
|---|
| 366 | return $ret; |
|---|
| 367 | } |
|---|
| 368 | |
|---|
| 369 | function getone($sql, $arrval = array()) { |
|---|
| 370 | // SQL文の実行 |
|---|
| 371 | $ret = $this->conn->getOne($sql, $arrval); |
|---|
| 372 | return $ret; |
|---|
| 373 | |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | // 一行を取得 |
|---|
| 377 | function getrow($table, $col, $where = "", $arrval = array()) { |
|---|
| 378 | if(strlen($where) <= 0) { |
|---|
| 379 | $sqlse = "SELECT $col FROM $table"; |
|---|
| 380 | } else { |
|---|
| 381 | $sqlse = "SELECT $col FROM $table WHERE $where"; |
|---|
| 382 | } |
|---|
| 383 | // SQL文の実行 |
|---|
| 384 | $ret = $this->conn->getRow($sqlse, $arrval); |
|---|
| 385 | |
|---|
| 386 | return $ret; |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | // 1列取得 |
|---|
| 390 | function getCol($table, $col, $where = "", $arrval = array()) { |
|---|
| 391 | if (strlen($where) <= 0) { |
|---|
| 392 | $sqlse = "SELECT $col FROM $table"; |
|---|
| 393 | } else { |
|---|
| 394 | $sqlse = "SELECT $col FROM $table WHERE $where"; |
|---|
| 395 | } |
|---|
| 396 | // SQL文の実行 |
|---|
| 397 | return $this->conn->getCol($sqlse, $col, $arrval); |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | // レコードの削除 |
|---|
| 401 | function delete($table, $where = "", $arrval = array()) { |
|---|
| 402 | if(strlen($where) <= 0) { |
|---|
| 403 | $sqlde = "DELETE FROM $table"; |
|---|
| 404 | } else { |
|---|
| 405 | $sqlde = "DELETE FROM $table WHERE $where"; |
|---|
| 406 | } |
|---|
| 407 | $ret = $this->conn->query($sqlde, $arrval); |
|---|
| 408 | return $ret; |
|---|
| 409 | } |
|---|
| 410 | |
|---|
| 411 | function nextval($table, $colname) { |
|---|
| 412 | $sql = ""; |
|---|
| 413 | // postgresqlとmysqlとで処理を分ける |
|---|
| 414 | if (DB_TYPE == "pgsql") { |
|---|
| 415 | $seqtable = $table . "_" . $colname . "_seq"; |
|---|
| 416 | $sql = "SELECT NEXTVAL('$seqtable')"; |
|---|
| 417 | }else if (DB_TYPE == "mysql") { |
|---|
| 418 | $sql = "SELECT last_insert_id();"; |
|---|
| 419 | } |
|---|
| 420 | $ret = $this->conn->getOne($sql); |
|---|
| 421 | |
|---|
| 422 | return $ret; |
|---|
| 423 | } |
|---|
| 424 | |
|---|
| 425 | function currval($table, $colname) { |
|---|
| 426 | $sql = ""; |
|---|
| 427 | if (DB_TYPE == "pgsql") { |
|---|
| 428 | $seqtable = $table . "_" . $colname . "_seq"; |
|---|
| 429 | $sql = "SELECT CURRVAL('$seqtable')"; |
|---|
| 430 | }else if (DB_TYPE == "mysql") { |
|---|
| 431 | $sql = "SELECT last_insert_id();"; |
|---|
| 432 | } |
|---|
| 433 | $ret = $this->conn->getOne($sql); |
|---|
| 434 | |
|---|
| 435 | return $ret; |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | function setval($table, $colname, $data) { |
|---|
| 439 | $sql = ""; |
|---|
| 440 | if (DB_TYPE == "pgsql") { |
|---|
| 441 | $seqtable = $table . "_" . $colname . "_seq"; |
|---|
| 442 | $sql = "SELECT SETVAL('$seqtable', $data)"; |
|---|
| 443 | $ret = $this->conn->getOne($sql); |
|---|
| 444 | }else if (DB_TYPE == "mysql") { |
|---|
| 445 | $sql = "ALTER TABLE $table AUTO_INCREMENT=$data"; |
|---|
| 446 | $ret = $this->conn->query($sql); |
|---|
| 447 | } |
|---|
| 448 | |
|---|
| 449 | return $ret; |
|---|
| 450 | } |
|---|
| 451 | |
|---|
| 452 | function query($n ,$arr = "", $ignore_err = false){ |
|---|
| 453 | $result = $this->conn->query($n, $arr, $ignore_err); |
|---|
| 454 | return $result; |
|---|
| 455 | } |
|---|
| 456 | |
|---|
| 457 | // auto_incrementを取得する |
|---|
| 458 | function get_auto_increment($table_name){ |
|---|
| 459 | // ロックする |
|---|
| 460 | $this->query("LOCK TABLES $table_name WRITE"); |
|---|
| 461 | |
|---|
| 462 | // 次のIncrementを取得 |
|---|
| 463 | $arrRet = $this->getAll("SHOW TABLE STATUS LIKE ?", array($table_name)); |
|---|
| 464 | $auto_inc_no = $arrRet[0]["Auto_increment"]; |
|---|
| 465 | |
|---|
| 466 | // 値をカウントアップしておく |
|---|
| 467 | $this->conn->query("ALTER TABLE $table_name AUTO_INCREMENT=?" , $auto_inc_no + 1); |
|---|
| 468 | |
|---|
| 469 | // 解除する |
|---|
| 470 | $this->query('UNLOCK TABLES'); |
|---|
| 471 | |
|---|
| 472 | return $auto_inc_no; |
|---|
| 473 | } |
|---|
| 474 | } |
|---|
| 475 | |
|---|
| 476 | ?> |
|---|