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