| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of EC-CUBE |
|---|
| 4 | * |
|---|
| 5 | * Copyright(c) 2000-2010 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 | /* ---- SQL文を作るクラス ---- */ |
|---|
| 25 | class SC_SelectSql { |
|---|
| 26 | |
|---|
| 27 | var $sql; |
|---|
| 28 | |
|---|
| 29 | var $select; |
|---|
| 30 | var $where; |
|---|
| 31 | var $order; |
|---|
| 32 | var $group; |
|---|
| 33 | var $limit; |
|---|
| 34 | var $offset; |
|---|
| 35 | var $arrSql; |
|---|
| 36 | var $arrVal; |
|---|
| 37 | |
|---|
| 38 | //-- コンストラクタ。 |
|---|
| 39 | function SC_SelectSql($array = "") { |
|---|
| 40 | if (is_array($array)) { |
|---|
| 41 | $this->arrSql = $array; |
|---|
| 42 | } |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | //-- SQL分生成 |
|---|
| 46 | function getSql( $mode = "" ){ |
|---|
| 47 | $this->sql = $this->select ." ". $this->where ." ". $this->group ." "; |
|---|
| 48 | |
|---|
| 49 | // $mode == 1 は limit & offset無し |
|---|
| 50 | if ($mode == 2) { |
|---|
| 51 | $this->sql .= $this->order; |
|---|
| 52 | }elseif ( $mode != 1 ){ |
|---|
| 53 | $this->sql .= $this->order . " " .$this->limit ." ". $this->offset; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | return $this->sql; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | // 検索用 |
|---|
| 60 | function addSearchStr($val) { |
|---|
| 61 | $return = "%" .$val. "%"; |
|---|
| 62 | return $return; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | //-- 範囲検索(○ ~ ○ まで) |
|---|
| 66 | function selectRange($from, $to, $column) { |
|---|
| 67 | |
|---|
| 68 | // ある単位のみ検索($from = $to) |
|---|
| 69 | if( $from == $to ) { |
|---|
| 70 | $this->setWhere( $column ." = ?" ); |
|---|
| 71 | $return = array($from); |
|---|
| 72 | // ~$toまで検索 |
|---|
| 73 | } elseif( strlen($from) == 0 && strlen($to) > 0 ) { |
|---|
| 74 | $this->setWhere( $column ." <= ? "); |
|---|
| 75 | $return = array($to); |
|---|
| 76 | // ~$from以上を検索 |
|---|
| 77 | } elseif( strlen($from) > 0 && strlen($to) == 0 ) { |
|---|
| 78 | $this->setWhere( $column ." >= ? "); |
|---|
| 79 | $return = array($from); |
|---|
| 80 | // $from~$toの検索 |
|---|
| 81 | } else { |
|---|
| 82 | $this->setWhere( $column ." BETWEEN ? AND ?" ); |
|---|
| 83 | $return = array($from, $to); |
|---|
| 84 | } |
|---|
| 85 | return $return; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | //-- 期間検索(○年○月○日か~○年○月○日まで) |
|---|
| 89 | function selectTermRange($from_year, $from_month, $from_day, $to_year, $to_month, $to_day, $column) { |
|---|
| 90 | |
|---|
| 91 | // 開始期間の構築 |
|---|
| 92 | $date1 = $from_year . "/" . $from_month . "/" . $from_day; |
|---|
| 93 | |
|---|
| 94 | // 終了期間の構築 |
|---|
| 95 | // @see http://svn.ec-cube.net/open_trac/ticket/328 |
|---|
| 96 | // FIXME とりあえずintvalで対策... |
|---|
| 97 | $date2 = mktime (0, 0, 0, intval($to_month), intval($to_day), intval($to_year)); |
|---|
| 98 | $date2 = $date2 + 86400; |
|---|
| 99 | // SQL文のdate関数に与えるフォーマットは、yyyy/mm/ddで指定する。 |
|---|
| 100 | $date2 = date('Y/m/d', $date2); |
|---|
| 101 | |
|---|
| 102 | // 開始期間だけ指定の場合 |
|---|
| 103 | if( ( $from_year != "" ) && ( $from_month != "" ) && ( $from_day != "" ) && ( $to_year == "" ) && ( $to_month == "" ) && ( $to_day == "" ) ) { |
|---|
| 104 | $this->setWhere( $column ." >= '" . $date1 . "'"); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | // 開始~終了 |
|---|
| 108 | if( ( $from_year != "" ) && ( $from_month != "" ) && ( $from_day != "" ) && |
|---|
| 109 | ( $to_year != "" ) && ( $to_month != "" ) && ( $to_day != "" ) ) { |
|---|
| 110 | $this->setWhere( $column ." >= '" . $date1 ."' AND ". $column . " < date('" . $date2 . "')" ); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | // 終了期間だけ指定の場合 |
|---|
| 114 | if( ( $from_year == "" ) && ( $from_month == "" ) && ( $from_day == "" ) && ( $to_year != "" ) && ( $to_month != "" ) && ( $to_day != "" ) ) { |
|---|
| 115 | $this->setWhere( $column ." < date('" . $date2 . "')"); |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | // checkboxなどで同一カラム内で単一、もしくは複数選択肢が有る場合 例: AND ( sex = xxx OR sex = xxx OR sex = xxx ) AND ... |
|---|
| 120 | function setItemTerm( $arr, $ItemStr ) { |
|---|
| 121 | |
|---|
| 122 | foreach( $arr as $data ) { |
|---|
| 123 | |
|---|
| 124 | if( count( $arr ) > 1 ) { |
|---|
| 125 | if( ! is_null( $data ) ) $item .= $ItemStr . " = ? OR "; |
|---|
| 126 | } else { |
|---|
| 127 | if( ! is_null( $data ) ) $item = $ItemStr . " = ?"; |
|---|
| 128 | } |
|---|
| 129 | $return[] = $data; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | if( count( $arr ) > 1 ) $item = "( " . rtrim( $item, " OR " ) . " )"; |
|---|
| 133 | $this->setWhere( $item ); |
|---|
| 134 | return $return; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | // NULL値が必要な場合 |
|---|
| 138 | function setItemTermWithNull( $arr, $ItemStr ) { |
|---|
| 139 | |
|---|
| 140 | $item = " ${ItemStr} IS NULL "; |
|---|
| 141 | |
|---|
| 142 | if ( $arr ){ |
|---|
| 143 | foreach( $arr as $data ) { |
|---|
| 144 | if ($data != "不明") { |
|---|
| 145 | $item .= " OR ${ItemStr} = ?"; |
|---|
| 146 | $return[] = $data; |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | $item = "( ${item} ) "; |
|---|
| 152 | $this->setWhere( $item ); |
|---|
| 153 | return $return; |
|---|
| 154 | } |
|---|
| 155 | // NULLもしくは''で検索する場合 |
|---|
| 156 | function setItemTermWithNullAndSpace( $arr, $ItemStr ) { |
|---|
| 157 | $count = count($arr); |
|---|
| 158 | $item = " ${ItemStr} IS NULL OR ${ItemStr} = '' "; |
|---|
| 159 | $i = 1; |
|---|
| 160 | if ( $arr ){ |
|---|
| 161 | foreach( $arr as $data ) { |
|---|
| 162 | if ($i == $count) break; |
|---|
| 163 | $item .= " OR ${ItemStr} = ?"; |
|---|
| 164 | $return[] = $data; |
|---|
| 165 | $i ++; |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | $item = "( ${item} ) "; |
|---|
| 169 | $this->setWhere( $item ); |
|---|
| 170 | return $return; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | /* 複数のカラムでORで優先検索する場合 例: AND ( item_flag1 = xxx OR item_flag2 = xxx OR item_flag3 = xxx ) AND ... |
|---|
| 176 | |
|---|
| 177 | 配列の構造例 |
|---|
| 178 | if ( $_POST['show_site1'] ) $arrShowsite_1 = array( "column" => "show_site1", |
|---|
| 179 | "value" => $_POST['show_site1'] ); |
|---|
| 180 | |
|---|
| 181 | */ |
|---|
| 182 | function setWhereByOR( $arrWhere ){ |
|---|
| 183 | |
|---|
| 184 | $count = count( $arrWhere ); |
|---|
| 185 | |
|---|
| 186 | for( $i = 0; $i < $count; $i++ ) { |
|---|
| 187 | |
|---|
| 188 | if( isset( $arrWhere[$i]["value"] ) ) $statement .= $arrWhere[$i]["column"] ." = " . SC_Utils_Ex::sfQuoteSmart($arrWhere[$i]["value"]) ." OR " ; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | $statement = "( " . rtrim( $statement, " OR " ) . " )"; |
|---|
| 192 | |
|---|
| 193 | if( $this->where ) { |
|---|
| 194 | |
|---|
| 195 | $this->where .= " AND " . $statement; |
|---|
| 196 | |
|---|
| 197 | } else { |
|---|
| 198 | |
|---|
| 199 | $this->where = "WHERE " . $statement; |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | function setWhere($where){ |
|---|
| 204 | if ($where != "") { |
|---|
| 205 | if( $this->where ) { |
|---|
| 206 | |
|---|
| 207 | $this->where .= " AND " . $where; |
|---|
| 208 | |
|---|
| 209 | } else { |
|---|
| 210 | |
|---|
| 211 | $this->where = "WHERE " . $where; |
|---|
| 212 | } |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | function setOrder($order){ |
|---|
| 217 | |
|---|
| 218 | $this->order = "ORDER BY " . $order; |
|---|
| 219 | |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | function setGroup( $group ) { |
|---|
| 223 | |
|---|
| 224 | $this->group = "GROUP BY " . $group; |
|---|
| 225 | |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | function setLimitOffset( $limit, $offset ){ |
|---|
| 230 | |
|---|
| 231 | if ( is_numeric($limit) and is_numeric($offset) ){ |
|---|
| 232 | |
|---|
| 233 | $this->limit = " LIMIT " .$limit; |
|---|
| 234 | $this->offset = " OFFSET " .$offset; |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | function clearSql(){ |
|---|
| 239 | $this->select = ""; |
|---|
| 240 | $this->where = ""; |
|---|
| 241 | $this->group = ""; |
|---|
| 242 | $this->order = ""; |
|---|
| 243 | $this->limit = ""; |
|---|
| 244 | $this->offset = ""; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | function setSelect($sql) { |
|---|
| 248 | $this->select = $sql; |
|---|
| 249 | } |
|---|
| 250 | } |
|---|
| 251 | ?> |
|---|