source: branches/comu-ver2/data/class/SC_SelectSql.php @ 17546

Revision 17546, 7.3 KB checked in by Seasoft, 13 years ago (diff)

波ダッシュ(〜)を全角チルダ(~)に統一

  • 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-2007 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 = SC_Utils_Ex::sfManualEscape($val);
62        $return = "%" .$return. "%";
63        return $return;
64    }
65
66    //-- 範囲検索(○ ~ ○ まで)
67    function selectRange($from, $to, $column) {
68
69        // ある単位のみ検索($from = $to)
70        if(  $from == $to ) {
71            $this->setWhere( $column ." = ?" );
72            $return = array($from);
73        // ~$toまで検索
74        } elseif(  strlen($from) == 0 && strlen($to) > 0 ) {
75            $this->setWhere( $column ." <= ? ");
76            $return = array($to);
77        // ~$from以上を検索
78        } elseif(  strlen($from) > 0 && strlen($to) == 0 ) {
79            $this->setWhere( $column ." >= ? ");
80            $return = array($from);
81        // $from~$toの検索
82        } else {
83            $this->setWhere( $column ." BETWEEN ? AND ?" );
84            $return = array($from, $to);
85        }
86        return $return;
87    }
88
89    //-- 期間検索(○年○月○日か~○年○月○日まで)
90    function selectTermRange($from_year, $from_month, $from_day, $to_year, $to_month, $to_day, $column) {
91
92        // FROM
93        $date1 = $from_year . "/" . $from_month . "/" . $from_day;
94
95        // TO
96        $date2 = @mktime (0, 0, 0, $to_month, $to_day,  $to_year);
97        $date2 = $date2 + 86400;
98        // SQL文のdate関数に与えるフォーマットは、yyyy/mm/ddで指定する。
99        $date2 = date('Y/m/d', $date2);
100
101        // 開始期間だけ指定の場合
102        if( ( $from_year != "" ) && ( $from_month != "" ) && ( $from_day != "" ) && ( $to_year == "" ) && ( $to_month == "" ) && ( $to_day == "" ) ) {
103            $this->setWhere( $column ." >= '" . $date1 . "'");
104        }
105
106        // 開始~終了
107        if( ( $from_year != "" ) && ( $from_month != "" ) && ( $from_day != "" ) &&
108            ( $to_year != "" ) && ( $to_month != "" ) && ( $to_day != "" ) ) {
109            $this->setWhere( $column ." >= '" . $date1 ."' AND ". $column . " < date('" . $date2 . "')" );
110        }
111
112        // 終了期間だけ指定の場合
113        if( ( $from_year == "" ) && ( $from_month == "" ) && ( $from_day == "" ) && ( $to_year != "" ) && ( $to_month != "" ) && ( $to_day != "" ) ) {
114            $this->setWhere( $column ." < date('" . $date2 . "')");
115        }
116    }
117
118    // checkboxなどで同一カラム内で単一、もしくは複数選択肢が有る場合 例: AND ( sex = xxx OR sex = xxx OR sex = xxx  ) AND ...
119    function setItemTerm( $arr, $ItemStr ) {
120
121        foreach( $arr as $data ) {
122
123            if( count( $arr ) > 1 ) {
124                if( ! is_null( $data ) ) $item .= $ItemStr . " = ? OR ";
125            } else {
126                if( ! is_null( $data ) ) $item = $ItemStr . " = ?";
127            }
128            $return[] = $data;
129        }
130
131        if( count( $arr ) > 1 )  $item = "( " . rtrim( $item, " OR " ) . " )";
132        $this->setWhere( $item );
133        return $return;
134    }
135
136    // NULL値が必要な場合
137    function setItemTermWithNull( $arr, $ItemStr ) {
138
139        $item = " ${ItemStr} IS NULL ";
140
141        if ( $arr ){
142            foreach( $arr as $data ) {
143                if ($data != "不明") {
144                    $item .= " OR ${ItemStr} = ?";
145                    $return[] = $data;
146                }
147            }
148        }
149
150        $item = "( ${item} ) ";
151        $this->setWhere( $item );
152        return $return;
153    }
154    // NULLもしくは''で検索する場合
155    function setItemTermWithNullAndSpace( $arr, $ItemStr ) {
156        $count = count($arr);
157        $item = " ${ItemStr} IS NULL OR ${ItemStr} = '' ";
158        $i = 1;
159        if ( $arr ){
160            foreach( $arr as $data ) {
161                if ($i == $count) break;
162                $item .= " OR ${ItemStr} = ?";
163                $return[] = $data;
164                $i ++;
165            }
166        }
167        $item = "( ${item} ) ";
168        $this->setWhere( $item );
169        return $return;
170    }
171
172
173
174    /* 複数のカラムでORで優先検索する場合 例: AND ( item_flag1 = xxx OR item_flag2 = xxx OR item_flag3 = xxx  ) AND ...
175
176        配列の構造例 
177        if ( $_POST['show_site1'] ) $arrShowsite_1 = array( "column" => "show_site1",
178                                                            "value"  => $_POST['show_site1'] );
179
180    */
181    function setWhereByOR( $arrWhere ){
182
183        $count = count( $arrWhere );
184
185        for( $i = 0; $i < $count; $i++ ) {
186
187            if( isset( $arrWhere[$i]["value"] ) ) $statement .= $arrWhere[$i]["column"] ." = " . SC_Utils_Ex::sfQuoteSmart($arrWhere[$i]["value"]) ." OR "  ;
188        }
189
190        $statement = "( " . rtrim( $statement, " OR " ) . " )";
191
192        if( $this->where ) {
193
194            $this->where .= " AND " . $statement;
195
196        } else {
197
198            $this->where = "WHERE " . $statement;
199        }
200    }
201
202    function setWhere($where){
203        if ($where != "") {
204            if( $this->where ) {
205
206                $this->where .= " AND " . $where;
207
208            } else {
209
210                $this->where = "WHERE " . $where;
211            }
212        }
213    }
214
215    function setOrder($order){
216
217            $this->order =  "ORDER BY " . $order;
218
219    }
220
221    function setGroup( $group ) {
222
223        $this->group =  "GROUP BY " . $group;
224
225    }
226
227
228    function setLimitOffset( $limit, $offset ){
229
230        if ( is_numeric($limit) and is_numeric($offset) ){
231
232            $this->limit = " LIMIT " .$limit;
233            $this->offset = " OFFSET " .$offset;
234        }
235    }
236
237    function clearSql(){
238        $this->select = "";
239        $this->where = "";
240        $this->group = "";
241        $this->order = "";
242        $this->limit = "";
243        $this->offset = "";
244    }
245
246    function setSelect($sql) {
247        $this->select = $sql;
248    }
249}
250?>
Note: See TracBrowser for help on using the repository browser.