source: branches/version-2_5-dev/data/class/SC_SelectSql.php @ 20541

Revision 20541, 7.4 KB checked in by Seasoft, 13 years ago (diff)

#627(ソース整形・ソースコメントの改善)

  • LF
  • 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-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文を作るクラス ---- */
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
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    /* 複数のカラムでORで優先検索する場合 例: AND ( item_flag1 = xxx OR item_flag2 = xxx OR item_flag3 = xxx  ) AND ...
174
175        配列の構造例 
176        if ( $_POST['show_site1'] ) $arrShowsite_1 = array( 'column' => "show_site1",
177                                                            'value'  => $_POST['show_site1'] );
178
179    */
180    function setWhereByOR( $arrWhere ){
181
182        $count = count( $arrWhere );
183
184        for( $i = 0; $i < $count; $i++ ) {
185
186            if( isset( $arrWhere[$i]['value'] ) ) $statement .= $arrWhere[$i]['column'] ." = " . SC_Utils_Ex::sfQuoteSmart($arrWhere[$i]['value']) ." OR "  ;
187        }
188
189        $statement = "( " . rtrim( $statement, " OR " ) . " )";
190
191        if( $this->where ) {
192
193            $this->where .= " AND " . $statement;
194
195        } else {
196
197            $this->where = "WHERE " . $statement;
198        }
199    }
200
201    function setWhere($where){
202        if ($where != "") {
203            if( $this->where ) {
204
205                $this->where .= " AND " . $where;
206
207            } else {
208
209                $this->where = "WHERE " . $where;
210            }
211        }
212    }
213
214    function setOrder($order){
215
216            $this->order =  "ORDER BY " . $order;
217
218    }
219
220    function setGroup( $group ) {
221
222        $this->group =  "GROUP BY " . $group;
223
224    }
225
226    function setLimitOffset( $limit, $offset ){
227
228        if ( is_numeric($limit) and is_numeric($offset) ){
229
230            $this->limit = " LIMIT " .$limit;
231            $this->offset = " OFFSET " .$offset;
232        }
233    }
234
235    function clearSql(){
236        $this->select = "";
237        $this->where = "";
238        $this->group = "";
239        $this->order = "";
240        $this->limit = "";
241        $this->offset = "";
242    }
243
244    function setSelect($sql) {
245        $this->select = $sql;
246    }
247}
248?>
Note: See TracBrowser for help on using the repository browser.