source: branches/version-2_12-dev/data/class/SC_SelectSql.php @ 21684

Revision 21684, 7.6 KB checked in by Seasoft, 12 years ago (diff)

#1613 (typo修正・ソース整形・ソースコメントの改善)

  • 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-2011 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    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        $return = array();
91
92        // 開始期間の構築
93        $date1 = $from_year . '/' . $from_month . '/' . $from_day;
94
95        // 終了期間の構築
96        // @see http://svn.ec-cube.net/open_trac/ticket/328
97        // FIXME とりあえずintvalで対策...
98        $date2 = mktime (0, 0, 0, intval($to_month), intval($to_day), intval($to_year));
99        $date2 = $date2 + 86400;
100        // SQL文のdate関数に与えるフォーマットは、yyyy/mm/ddで指定する。
101        $date2 = date('Y/m/d', $date2);
102
103        // 開始期間だけ指定の場合
104        if (($from_year != '') && ($from_month != '') && ($from_day != '') && ($to_year == '') && ($to_month == '') && ($to_day == '')) {
105            $this->setWhere($column .' >= ?');
106            $return[] = $date1;
107        }
108
109        // 開始~終了
110        if (($from_year != '') && ($from_month != '') && ($from_day != '')
111            && ($to_year != '') && ($to_month != '') && ($to_day != '')
112        ) {
113            $this->setWhere($column . ' >= ? AND ' . $column . ' < date(?)');
114            $return[] = $date1;
115            $return[] = $date2;
116        }
117
118        // 終了期間だけ指定の場合
119        if (($from_year == '') && ($from_month == '') && ($from_day == '') && ($to_year != '') && ($to_month != '') && ($to_day != '')) {
120            $this->setWhere($column . ' < date(?)');
121            $return[] = $date2;
122        }
123
124        return $return;
125    }
126
127    // checkboxなどで同一カラム内で単一、もしくは複数選択肢が有る場合 例: AND ( sex = xxx OR sex = xxx OR sex = xxx) AND ...
128    function setItemTerm($arr, $ItemStr) {
129
130        foreach ($arr as $data) {
131
132            if (count($arr) > 1) {
133                if (!is_null($data)) {
134                    $item .= $ItemStr . ' = ? OR ';
135                }
136            } else {
137                if (!is_null($data)) {
138                    $item = $ItemStr . ' = ?';
139                }
140            }
141            $return[] = $data;
142        }
143
144        if (count($arr) > 1) {
145            // FIXME 多分この rtrim の使い方は不適切(偶然動作しそうだが)
146            $item = '(' . rtrim($item, ' OR ') . ')';
147        }
148        $this->setWhere($item);
149        return $return;
150    }
151
152    // NULL値が必要な場合
153    function setItemTermWithNull($arr, $ItemStr) {
154
155        $item = " ${ItemStr} IS NULL ";
156
157        if ($arr) {
158            foreach ($arr as $data) {
159                if ($data != '不明') {
160                    $item .= " OR ${ItemStr} = ?";
161                    $return[] = $data;
162                }
163            }
164        }
165
166        $item = "(${item}) ";
167        $this->setWhere($item);
168        return $return;
169    }
170    // NULLもしくは''で検索する場合
171    function setItemTermWithNullAndSpace($arr, $ItemStr) {
172        $count = count($arr);
173        $item = " ${ItemStr} IS NULL OR ${ItemStr} = '' ";
174        $i = 1;
175        if ($arr) {
176            foreach ($arr as $data) {
177                if ($i == $count) break;
178                $item .= " OR ${ItemStr} = ?";
179                $return[] = $data;
180                $i ++;
181            }
182        }
183        $item = "(${item}) ";
184        $this->setWhere($item);
185        return $return;
186    }
187
188    /* 複数のカラムでORで優先検索する場合 例: AND ( item_flag1 = xxx OR item_flag2 = xxx OR item_flag3 = xxx) AND ...
189
190        配列の構造例 
191        if ($_POST['show_site1']) $arrShowsite_1 = array('column' => 'show_site1',
192                                                            'value'  => $_POST['show_site1']);
193
194    */
195    function setWhereByOR($arrWhere) {
196
197        $count = count($arrWhere);
198
199        for ($i = 0; $i < $count; $i++) {
200            if (isset($arrWhere[$i]['value'])) {
201                $statement .= $arrWhere[$i]['column'] .' = ' . SC_Utils_Ex::sfQuoteSmart($arrWhere[$i]['value']) .' OR ';
202            }
203        }
204
205        $statement = '(' . rtrim($statement, ' OR ') . ')';
206
207        if ($this->where) {
208
209            $this->where .= ' AND ' . $statement;
210
211        } else {
212
213            $this->where = 'WHERE ' . $statement;
214        }
215    }
216
217    function setWhere($where) {
218        if ($where != '') {
219            if ($this->where) {
220
221                $this->where .= ' AND ' . $where;
222
223            } else {
224
225                $this->where = 'WHERE ' . $where;
226            }
227        }
228    }
229
230    function setOrder($order) {
231
232            $this->order =  'ORDER BY ' . $order;
233
234    }
235
236    function setGroup($group) {
237
238        $this->group =  'GROUP BY ' . $group;
239
240    }
241
242    function setLimitOffset($limit, $offset) {
243
244        if (is_numeric($limit) and is_numeric($offset)) {
245
246            $this->limit = ' LIMIT ' .$limit;
247            $this->offset = ' OFFSET ' .$offset;
248        }
249    }
250
251    function clearSql() {
252        $this->select = '';
253        $this->where = '';
254        $this->group = '';
255        $this->order = '';
256        $this->limit = '';
257        $this->offset = '';
258    }
259
260    function setSelect($sql) {
261        $this->select = $sql;
262    }
263}
Note: See TracBrowser for help on using the repository browser.