Changeset 18609 for tmp/version-2_5-test/data/class/SC_Query.php
- Timestamp:
- 2010/03/11 10:35:11 (16 years ago)
- File:
-
- 1 edited
-
tmp/version-2_5-test/data/class/SC_Query.php (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
tmp/version-2_5-test/data/class/SC_Query.php
r18562 r18609 86 86 * @param string $where WHERE句 87 87 * @param array $arrval プレースホルダ 88 * @param integer $fetchmode 使用するフェッチモード。デフォルトは DB_FETCHMODE_ASSOC。 88 89 * @return array|null 89 90 */ 90 function select($col, $table, $where = "", $arrval = array() ){91 function select($col, $table, $where = "", $arrval = array(), $fetchmode = DB_FETCHMODE_ASSOC) { 91 92 $sqlse = $this->getsql($col, $table, $where); 92 // DBに依存した SQL へ変換 93 $dbFactory = SC_DB_DBFactory_Ex::getInstance(); 94 $sqlse = $dbFactory->sfChangeMySQL($sqlse); 95 $ret = $this->conn->getAll($sqlse, $arrval); 93 $ret = $this->conn->getAll($sqlse, $arrval, $fetchmode); 96 94 return $ret; 97 95 } … … 99 97 /** 100 98 * 直前に実行されたSQL文を取得する. 99 * SC_DBconn::getLastQuery() を利用. 101 100 * 102 101 * @param boolean $disp trueの場合、画面出力を行う. … … 104 103 */ 105 104 function getLastQuery($disp = true) { 106 $sql = $this->conn->conn->last_query; 107 if($disp) { 108 print($sql.";<br />\n"); 109 } 110 return $sql; 105 return $this->conn->getLastQuery($disp); 111 106 } 112 107 … … 127 122 } 128 123 129 function autoselect($col, $table, $arrwhere = array(), $arrcon = array()) { 130 $strw = ""; 131 $find = false; 132 foreach ($arrwhere as $key => $val) { 133 if(strlen($val) > 0) { 134 if(strlen($strw) <= 0) { 135 $strw .= $key ." LIKE ?"; 136 } else if(strlen($arrcon[$key]) > 0) { 137 $strw .= " ". $arrcon[$key]. " " . $key ." LIKE ?"; 138 } else { 139 $strw .= " AND " . $key ." LIKE ?"; 140 } 141 142 $arrval[] = $val; 143 } 144 } 145 146 if(strlen($strw) > 0) { 147 $sqlse = "SELECT $col FROM $table WHERE $strw ".$this->option; 148 } else { 149 $sqlse = "SELECT $col FROM $table ".$this->option; 150 } 151 $ret = $this->conn->getAll($sqlse, $arrval); 152 return $ret; 153 } 154 155 function getall($sql, $arrval = array()) { 156 $ret = $this->conn->getAll($sql, $arrval); 157 return $ret; 158 } 159 160 function getsql($col, $table, $where) { 161 if($where != "") { 162 // 引数の$whereを優先して実行する。 163 $sqlse = "SELECT $col FROM $table WHERE $where " . $this->groupby . " " . $this->order . " " . $this->option; 164 } else { 165 if($this->where != "") { 166 $sqlse = "SELECT $col FROM $table WHERE $this->where " . $this->groupby . " " . $this->order . " " . $this->option; 167 } else { 168 $sqlse = "SELECT $col FROM $table " . $this->groupby . " " . $this->order . " " . $this->option; 169 } 170 } 124 /** 125 * クエリを実行し、全ての行を返す 126 * 127 * @param string $sql SQL クエリ 128 * @param array $arrVal プリペアドステートメントの実行時に使用される配列。配列の要素数は、クエリ内のプレースホルダの数と同じでなければなりません。 129 * @param integer $fetchmode 使用するフェッチモード。デフォルトは DB_FETCHMODE_ASSOC。 130 * @return array データを含む2次元配列。失敗した場合に 0 または DB_Error オブジェクトを返します。 131 */ 132 function getall($sql, $arrval = array(), $fetchmode = DB_FETCHMODE_ASSOC) { 133 $ret = $this->conn->getAll($sql, $arrval, $fetchmode); 134 return $ret; 135 } 136 137 function getsql($col, $table, $where = '') { 138 $sqlse = "SELECT $col FROM $table"; 139 140 // 引数の$whereを優先する。 141 if (strlen($where) >= 1) { 142 $sqlse .= " WHERE $where"; 143 } elseif (strlen($this->where) >= 1) { 144 $where = $this->where; 145 } 146 147 $sqlse .= ' ' . $this->groupby . ' ' . $this->order . ' ' . $this->option; 148 171 149 return $sqlse; 172 150 } … … 191 169 192 170 function setgroupby($str) { 193 $this->groupby = "GROUP BY " . $str; 171 if (strlen($str) == 0) { 172 $this->groupby = ''; 173 } else { 174 $this->groupby = "GROUP BY " . $str; 175 } 194 176 } 195 177 … … 215 197 216 198 function setorder($str) { 217 $this->order = "ORDER BY " . $str; 199 if (strlen($str) == 0) { 200 $this->order = ''; 201 } else { 202 $this->order = "ORDER BY " . $str; 203 } 218 204 } 219 205 … … 249 235 if(eregi("^Now\(\)$", $val)) { 250 236 $strval .= 'Now(),'; 251 // 先頭に~があるとプレースホルダーしない。252 237 } else { 253 238 $strval .= '?,'; 254 if($val != ""){ 255 $arrval[] = $val; 256 } else { 257 $arrval[] = NULL; 258 } 239 $arrval[] = $val; 259 240 } 260 241 $find = true; … … 274 255 } 275 256 276 // INSERT文の生成・実行277 // $table :テーブル名278 // $sqlval :列名 => 値の格納されたハッシュ配列279 function fast_insert($table, $sqlval) {280 $strcol = '';281 $strval = '';282 $find = false;283 284 foreach ($sqlval as $key => $val) {285 $strcol .= $key . ',';286 if($val != ""){287 $eval = pg_escape_string($val);288 $strval .= "'$eval',";289 } else {290 $strval .= "NULL,";291 }292 $find = true;293 }294 if(!$find) {295 return false;296 }297 // 文末の","を削除298 $strcol = ereg_replace(",$","",$strcol);299 // 文末の","を削除300 $strval = ereg_replace(",$","",$strval);301 $sqlin = "INSERT INTO $table(" . $strcol. ") VALUES (" . $strval . ")";302 303 // INSERT文の実行304 $ret = $this->conn->query($sqlin);305 306 return $ret;307 }308 309 257 /** 310 258 * UPDATE文を実行する. … … 313 261 * @param array $sqlval array('カラム名' => '値',...)の連想配列 314 262 * @param string $where WHERE句 315 * @param array $arradd $addcol用のプレースホルダ配列 316 * @param string $addcol 追加カラム 263 * @param array $arrValIn WHERE句用のプレースホルダ配列 (従来は追加カラム用も兼ねていた) 264 * @param array $arrRawSql 追加カラム 265 * @param array $arrRawSqlVal 追加カラム用のプレースホルダ配列 317 266 * @return 318 267 */ 319 function update($table, $sqlval, $where = "", $arr add = "", $addcol = "") {320 $ strcol = '';321 $ strval = '';268 function update($table, $sqlval, $where = "", $arrValIn = array(), $arrRawSql = array(), $arrRawSqlVal = array()) { 269 $arrCol = array(); 270 $arrVal = array(); 322 271 $find = false; 323 272 foreach ($sqlval as $key => $val) { 324 if(eregi("^Now\(\)$", $val)) { 325 $strcol .= $key . '= Now(),'; 326 // 先頭に~があるとプレースホルダーしない。 273 if (eregi("^Now\(\)$", $val)) { 274 $arrCol[] = $key . '= Now()'; 327 275 } else { 328 $strcol .= $key . '= ?,'; 329 if($val != ""){ 330 $arrval[] = $val; 331 } else { 332 $arrval[] = NULL; 333 } 276 $arrCol[] = $key . '= ?'; 277 $arrVal[] = $val; 334 278 } 335 279 $find = true; 336 280 } 337 if(!$find) { 281 282 if ($arrRawSql != "") { 283 foreach($arrRawSql as $key => $val) { 284 $arrCol[] = "$key = $val"; 285 } 286 } 287 288 $arrVal = array_merge($arrVal, $arrRawSqlVal); 289 290 if (empty($arrCol)) { 338 291 return false; 339 292 } 340 293 341 if($addcol != "") {342 foreach($addcol as $key => $val) {343 $strcol .= "$key = $val,";344 }345 }346 347 294 // 文末の","を削除 348 $strcol = ereg_replace(",$","",$strcol); 349 // 文末の","を削除 350 $strval = ereg_replace(",$","",$strval); 351 352 if($where != "") { 353 $sqlup = "UPDATE $table SET $strcol WHERE $where"; 354 } else { 355 $sqlup = "UPDATE $table SET $strcol"; 356 } 357 358 if(is_array($arradd)) { 295 $strcol = implode(', ', $arrCol); 296 297 if (is_array($arrValIn)) { // 旧版との互換用 359 298 // プレースホルダー用に配列を追加 360 foreach($arradd as $val) { 361 $arrval[] = $val; 362 } 363 } 364 365 // INSERT文の実行 366 $ret = $this->conn->query($sqlup, $arrval); 367 return $ret; 299 $arrVal = array_merge($arrVal, $arrValIn); 300 } 301 302 $sqlup = "UPDATE $table SET $strcol"; 303 if (strlen($where) >= 1) { 304 $sqlup .= " WHERE $where"; 305 } 306 307 // UPDATE文の実行 308 return $this->conn->query($sqlup, $arrVal); 368 309 } 369 310 370 311 // MAX文の実行 371 312 function max($table, $col, $where = "", $arrval = array()) { 372 if(strlen($where) <= 0) { 373 $sqlse = "SELECT MAX($col) FROM $table"; 374 } else { 375 $sqlse = "SELECT MAX($col) FROM $table WHERE $where"; 376 } 377 // MAX文の実行 378 $ret = $this->conn->getOne($sqlse, $arrval); 313 $ret = $this->get($table, "MAX($col)", $where); 379 314 return $ret; 380 315 } … … 382 317 // MIN文の実行 383 318 function min($table, $col, $where = "", $arrval = array()) { 384 if(strlen($where) <= 0) { 385 $sqlse = "SELECT MIN($col) FROM $table"; 386 } else { 387 $sqlse = "SELECT MIN($col) FROM $table WHERE $where"; 388 } 389 // MIN文の実行 390 $ret = $this->conn->getOne($sqlse, $arrval); 319 $ret = $this->get($table, "MIN($col)", $where); 391 320 return $ret; 392 321 } … … 394 323 // 特定のカラムの値を取得 395 324 function get($table, $col, $where = "", $arrval = array()) { 396 if(strlen($where) <= 0) { 397 $sqlse = "SELECT $col FROM $table"; 398 } else { 399 $sqlse = "SELECT $col FROM $table WHERE $where"; 400 } 325 $sqlse = $this->getsql($col, $table, $where); 401 326 // SQL文の実行 402 $ret = $this-> conn->getOne($sqlse, $arrval);327 $ret = $this->getone($sqlse, $arrval); 403 328 return $ret; 404 329 } … … 408 333 $ret = $this->conn->getOne($sql, $arrval); 409 334 return $ret; 410 411 } 412 413 // 一行を取得 414 function getrow($table, $col, $where = "", $arrval = array()) { 415 if(strlen($where) <= 0) { 416 $sqlse = "SELECT $col FROM $table"; 417 } else { 418 $sqlse = "SELECT $col FROM $table WHERE $where"; 419 } 335 } 336 337 /** 338 * 一行をカラム名をキーとした連想配列として取得 339 * 340 * @param string $table テーブル名 341 * @param string $col カラム名 342 * @param string $where WHERE句 343 * @param array $arrVal プレースホルダ配列 344 * @param integer $fetchmode 使用するフェッチモード。デフォルトは DB_FETCHMODE_ASSOC。 345 * @return array array('カラム名' => '値', ...)の連想配列 346 */ 347 function getRow($table, $col, $where = "", $arrVal = array(), $fetchmode = DB_FETCHMODE_ASSOC) { 348 $sqlse = $this->getsql($col, $table, $where); 420 349 // SQL文の実行 421 $ret = $this->conn->getRow($sqlse, $arrval); 422 423 return $ret; 350 return $this->conn->getRow($sqlse, $arrVal ,$fetchmode); 424 351 } 425 352 426 353 // 1列取得 427 354 function getCol($table, $col, $where = "", $arrval = array()) { 428 if (strlen($where) <= 0) { 429 $sqlse = "SELECT $col FROM $table"; 430 } else { 431 $sqlse = "SELECT $col FROM $table WHERE $where"; 432 } 355 $sqlse = $this->getsql($col, $table, $where); 433 356 // SQL文の実行 434 357 return $this->conn->getCol($sqlse, 0, $arrval);
Note: See TracChangeset
for help on using the changeset viewer.
