1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of EC-CUBE |
---|
4 | * |
---|
5 | * Copyright(c) 2000-2013 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 | |
---|
28 | var $sql; |
---|
29 | |
---|
30 | var $select; |
---|
31 | var $where; |
---|
32 | var $order; |
---|
33 | var $group; |
---|
34 | var $arrSql; |
---|
35 | var $arrVal; |
---|
36 | |
---|
37 | //-- コンストラクタ。 |
---|
38 | function __construct($array = '') |
---|
39 | { |
---|
40 | if (is_array($array)) { |
---|
41 | $this->arrSql = $array; |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | //-- SQL分生成 |
---|
46 | function getSql($mode = '') |
---|
47 | { |
---|
48 | $this->sql = $this->select .' '. $this->where .' '. $this->group .' '; |
---|
49 | |
---|
50 | if ($mode == 2) { |
---|
51 | $this->sql .= $this->order; |
---|
52 | } |
---|
53 | |
---|
54 | return $this->sql; |
---|
55 | } |
---|
56 | |
---|
57 | // 検索用 |
---|
58 | function addSearchStr($val) |
---|
59 | { |
---|
60 | $return = '%' .$val. '%'; |
---|
61 | return $return; |
---|
62 | } |
---|
63 | |
---|
64 | //-- 範囲検索(○ ~ ○ まで) |
---|
65 | function selectRange($from, $to, $column) |
---|
66 | { |
---|
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 | $return = array(); |
---|
92 | |
---|
93 | // 開始期間の構築 |
---|
94 | $date1 = $from_year . '/' . $from_month . '/' . $from_day; |
---|
95 | |
---|
96 | // 終了期間の構築 |
---|
97 | // @see http://svn.ec-cube.net/open_trac/ticket/328 |
---|
98 | // FIXME とりあえずintvalで対策... |
---|
99 | $date2 = mktime (0, 0, 0, intval($to_month), intval($to_day), intval($to_year)); |
---|
100 | $date2 = $date2 + 86400; |
---|
101 | // SQL文のdate関数に与えるフォーマットは、yyyy/mm/ddで指定する。 |
---|
102 | $date2 = date('Y/m/d', $date2); |
---|
103 | |
---|
104 | // 開始期間だけ指定の場合 |
---|
105 | if (($from_year != '') && ($from_month != '') && ($from_day != '') && ($to_year == '') && ($to_month == '') && ($to_day == '')) { |
---|
106 | $this->setWhere($column .' >= ?'); |
---|
107 | $return[] = $date1; |
---|
108 | } |
---|
109 | |
---|
110 | // 開始~終了 |
---|
111 | if (($from_year != '') && ($from_month != '') && ($from_day != '') |
---|
112 | && ($to_year != '') && ($to_month != '') && ($to_day != '') |
---|
113 | ) { |
---|
114 | $this->setWhere($column . ' >= ? AND ' . $column . ' < date(?)'); |
---|
115 | $return[] = $date1; |
---|
116 | $return[] = $date2; |
---|
117 | } |
---|
118 | |
---|
119 | // 終了期間だけ指定の場合 |
---|
120 | if (($from_year == '') && ($from_month == '') && ($from_day == '') && ($to_year != '') && ($to_month != '') && ($to_day != '')) { |
---|
121 | $this->setWhere($column . ' < date(?)'); |
---|
122 | $return[] = $date2; |
---|
123 | } |
---|
124 | |
---|
125 | return $return; |
---|
126 | } |
---|
127 | |
---|
128 | // checkboxなどで同一カラム内で単一、もしくは複数選択肢が有る場合 例: AND ( sex = xxx OR sex = xxx OR sex = xxx) AND ... |
---|
129 | function setItemTerm($arr, $ItemStr) |
---|
130 | { |
---|
131 | $return = array(); |
---|
132 | foreach ($arr as $data) { |
---|
133 | |
---|
134 | if (count($arr) > 1) { |
---|
135 | if (!is_null($data)) { |
---|
136 | $item .= $ItemStr . ' = ? OR '; |
---|
137 | } |
---|
138 | } else { |
---|
139 | if (!is_null($data)) { |
---|
140 | $item = $ItemStr . ' = ?'; |
---|
141 | } |
---|
142 | } |
---|
143 | $return[] = $data; |
---|
144 | } |
---|
145 | |
---|
146 | if (count($arr) > 1) { |
---|
147 | // FIXME 多分この rtrim の使い方は不適切(偶然動作しそうだが) |
---|
148 | $item = '(' . rtrim($item, ' OR ') . ')'; |
---|
149 | } |
---|
150 | $this->setWhere($item); |
---|
151 | return $return; |
---|
152 | } |
---|
153 | |
---|
154 | // NULL値が必要な場合 |
---|
155 | function setItemTermWithNull($arr, $ItemStr) |
---|
156 | { |
---|
157 | $return = array(); |
---|
158 | $item = " {$ItemStr} IS NULL "; |
---|
159 | |
---|
160 | if ($arr) { |
---|
161 | foreach ($arr as $data) { |
---|
162 | if ($data != '不明') { |
---|
163 | $item .= " OR {$ItemStr} = ?"; |
---|
164 | $return[] = $data; |
---|
165 | } |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | $item = "({$item}) "; |
---|
170 | $this->setWhere($item); |
---|
171 | return $return; |
---|
172 | } |
---|
173 | // NULLもしくは''で検索する場合 |
---|
174 | function setItemTermWithNullAndSpace($arr, $ItemStr) |
---|
175 | { |
---|
176 | $return = array(); |
---|
177 | $count = count($arr); |
---|
178 | $item = " {$ItemStr} IS NULL OR {$ItemStr} = '' "; |
---|
179 | $i = 1; |
---|
180 | if ($arr) { |
---|
181 | foreach ($arr as $data) { |
---|
182 | if ($i == $count) break; |
---|
183 | $item .= " OR {$ItemStr} = ?"; |
---|
184 | $return[] = $data; |
---|
185 | $i ++; |
---|
186 | } |
---|
187 | } |
---|
188 | $item = "({$item}) "; |
---|
189 | $this->setWhere($item); |
---|
190 | return $return; |
---|
191 | } |
---|
192 | |
---|
193 | /* 複数のカラムでORで優先検索する場合 例: AND ( item_flag1 = xxx OR item_flag2 = xxx OR item_flag3 = xxx) AND ... |
---|
194 | |
---|
195 | 配列の構造例 |
---|
196 | if ($_POST['show_site1']) $arrShowsite_1 = array('column' => 'show_site1', |
---|
197 | 'value' => $_POST['show_site1']); |
---|
198 | |
---|
199 | */ |
---|
200 | function setWhereByOR($arrWhere) |
---|
201 | { |
---|
202 | |
---|
203 | $count = count($arrWhere); |
---|
204 | |
---|
205 | for ($i = 0; $i < $count; $i++) { |
---|
206 | if (isset($arrWhere[$i]['value'])) { |
---|
207 | $statement .= $arrWhere[$i]['column'] .' = ' . SC_Utils_Ex::sfQuoteSmart($arrWhere[$i]['value']) .' OR '; |
---|
208 | } |
---|
209 | } |
---|
210 | |
---|
211 | $statement = '(' . rtrim($statement, ' OR ') . ')'; |
---|
212 | |
---|
213 | if ($this->where) { |
---|
214 | |
---|
215 | $this->where .= ' AND ' . $statement; |
---|
216 | |
---|
217 | } else { |
---|
218 | |
---|
219 | $this->where = 'WHERE ' . $statement; |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | /** |
---|
224 | * WHERE を取得する。 |
---|
225 | * |
---|
226 | * ヘンテコセッター互換仕様。 |
---|
227 | * @param $with_where boolean 必要に応じて WHERE を前置するか |
---|
228 | * @return string WHERE |
---|
229 | */ |
---|
230 | function getWhere($with_where = false) |
---|
231 | { |
---|
232 | $where = $this->where; |
---|
233 | |
---|
234 | if (!$with_where) { |
---|
235 | $where = preg_replace('/^\s*WHERE\s+/', '', $where); |
---|
236 | } |
---|
237 | |
---|
238 | return $where; |
---|
239 | } |
---|
240 | |
---|
241 | function setWhere($where) |
---|
242 | { |
---|
243 | if ($where != '') { |
---|
244 | if ($this->where) { |
---|
245 | |
---|
246 | $this->where .= ' AND ' . $where; |
---|
247 | |
---|
248 | } else { |
---|
249 | |
---|
250 | $this->where = 'WHERE ' . $where; |
---|
251 | } |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | function setOrder($order) |
---|
256 | { |
---|
257 | |
---|
258 | $this->order = 'ORDER BY ' . $order; |
---|
259 | |
---|
260 | } |
---|
261 | |
---|
262 | function setGroup($group) |
---|
263 | { |
---|
264 | |
---|
265 | $this->group = 'GROUP BY ' . $group; |
---|
266 | |
---|
267 | } |
---|
268 | |
---|
269 | function clearSql() |
---|
270 | { |
---|
271 | $this->select = ''; |
---|
272 | $this->where = ''; |
---|
273 | $this->group = ''; |
---|
274 | $this->order = ''; |
---|
275 | } |
---|
276 | |
---|
277 | function setSelect($sql) |
---|
278 | { |
---|
279 | $this->select = $sql; |
---|
280 | } |
---|
281 | } |
---|