source: branches/version-2_13-dev/data/class/SC_SelectSql.php @ 23124

Revision 23124, 7.8 KB checked in by kimoto, 11 years ago (diff)

#2043 typo修正・ソース整形・ソースコメントの改善 for 2.13.0
PHP4的な書き方の修正

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
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文を作るクラス ---- */
25class SC_SelectSql
26{
27    public $sql;
28
29    public $select;
30    public $where;
31    public $order;
32    public $group;
33    public $arrSql;
34    public $arrVal;
35
36    //-- コンストラクタ。
37    public function __construct($array = '')
38    {
39        if (is_array($array)) {
40            $this->arrSql = $array;
41        }
42    }
43
44    //-- SQL分生成
45    public function getSql($mode = '')
46    {
47        $this->sql = $this->select .' '. $this->where .' '. $this->group .' ';
48
49        if ($mode == 2) {
50            $this->sql .= $this->order;
51        }
52
53        return $this->sql;
54    }
55
56        // 検索用
57    public function addSearchStr($val)
58    {
59        $return = '%' .$val. '%';
60
61        return $return;
62    }
63
64    //-- 範囲検索(○ ~ ○ まで)
65    public function selectRange($from, $to, $column)
66    {
67        // ある単位のみ検索($from = $to)
68        if ($from == $to) {
69            $this->setWhere($column .' = ?');
70            $return = array($from);
71        // ~$toまで検索
72        } elseif (strlen($from) == 0 && strlen($to) > 0) {
73            $this->setWhere($column .' <= ? ');
74            $return = array($to);
75        // ~$from以上を検索
76        } elseif (strlen($from) > 0 && strlen($to) == 0) {
77            $this->setWhere($column .' >= ? ');
78            $return = array($from);
79        // $from~$toの検索
80        } else {
81            $this->setWhere($column .' BETWEEN ? AND ?');
82            $return = array($from, $to);
83        }
84
85        return $return;
86    }
87
88    //-- 期間検索(○年○月○日か~○年○月○日まで)
89    public 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    public function setItemTerm($arr, $ItemStr)
130    {
131        $return = array();
132        foreach ($arr as $data) {
133            if (count($arr) > 1) {
134                if (!is_null($data)) {
135                    $item .= $ItemStr . ' = ? OR ';
136                }
137            } else {
138                if (!is_null($data)) {
139                    $item = $ItemStr . ' = ?';
140                }
141            }
142            $return[] = $data;
143        }
144
145        if (count($arr) > 1) {
146            // FIXME 多分この rtrim の使い方は不適切(偶然動作しそうだが)
147            $item = '(' . rtrim($item, ' OR ') . ')';
148        }
149        $this->setWhere($item);
150
151        return $return;
152    }
153
154    // NULL値が必要な場合
155    public 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
172        return $return;
173    }
174    // NULLもしくは''で検索する場合
175    public function setItemTermWithNullAndSpace($arr, $ItemStr)
176    {
177        $return = array();
178        $count = count($arr);
179        $item = " {$ItemStr} IS NULL OR {$ItemStr} = '' ";
180        $i = 1;
181        if ($arr) {
182            foreach ($arr as $data) {
183                if ($i == $count) break;
184                $item .= " OR {$ItemStr} = ?";
185                $return[] = $data;
186                $i ++;
187            }
188        }
189        $item = "({$item}) ";
190        $this->setWhere($item);
191
192        return $return;
193    }
194
195    /* 複数のカラムでORで優先検索する場合 例: AND ( item_flag1 = xxx OR item_flag2 = xxx OR item_flag3 = xxx) AND ...
196
197        配列の構造例 
198        if ($_POST['show_site1']) $arrShowsite_1 = array('column' => 'show_site1',
199                                                            'value'  => $_POST['show_site1']);
200
201    */
202    public function setWhereByOR($arrWhere)
203    {
204        $count = count($arrWhere);
205
206        for ($i = 0; $i < $count; $i++) {
207            if (isset($arrWhere[$i]['value'])) {
208                $statement .= $arrWhere[$i]['column'] .' = ' . SC_Utils_Ex::sfQuoteSmart($arrWhere[$i]['value']) .' OR ';
209            }
210        }
211
212        $statement = '(' . rtrim($statement, ' OR ') . ')';
213
214        if ($this->where) {
215            $this->where .= ' AND ' . $statement;
216        } else {
217            $this->where = 'WHERE ' . $statement;
218        }
219    }
220
221    /**
222     * WHERE を取得する。
223     *
224     * ヘンテコセッター互換仕様。
225     * @param $with_where boolean 必要に応じて WHERE を前置するか
226     * @return string WHERE
227     */
228    public function getWhere($with_where = false)
229    {
230        $where = $this->where;
231
232        if (!$with_where) {
233            $where = preg_replace('/^\s*WHERE\s+/', '', $where);
234        }
235
236        return $where;
237    }
238
239    public function setWhere($where)
240    {
241        if ($where != '') {
242            if ($this->where) {
243                $this->where .= ' AND ' . $where;
244            } else {
245                $this->where = 'WHERE ' . $where;
246            }
247        }
248    }
249
250    public function setOrder($order)
251    {
252            $this->order =  'ORDER BY ' . $order;
253    }
254
255    public function setGroup($group)
256    {
257        $this->group =  'GROUP BY ' . $group;
258    }
259
260    public function clearSql()
261    {
262        $this->select = '';
263        $this->where = '';
264        $this->group = '';
265        $this->order = '';
266    }
267
268    public function setSelect($sql)
269    {
270        $this->select = $sql;
271    }
272}
Note: See TracBrowser for help on using the repository browser.