source: temp/trunk/data/class/SC_SelectSql.php @ 1881

Revision 1881, 5.7 KB checked in by kakinaka, 20 years ago (diff)

blank

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2
3/* ---- SQLʸ¤òºî¤ë¥¯¥é¥¹ ---- */
4class SC_SelectSql {
5   
6    var $sql;
7   
8    var $select;   
9    var $where;
10    var $order;
11    var $group;
12    var $limit;
13    var $offset;
14    var $arrSql;
15    var $arrVal;
16
17    //--¡¡¥³¥ó¥¹¥È¥é¥¯¥¿¡£
18    function SC_SelectSql($array = "") {
19        if (is_array($array)) {
20            $this->arrSql = $array;
21        }
22    }
23
24    //-- SQLʬÀ¸À®
25    function getSql( $mode = "" ){
26        $this->sql = $this->select ." ". $this->where ." ". $this->group ." ";
27                       
28        // $mode == 1 ¤Ï limit & offset̵¤·                     
29        if ( $mode != 1 ){
30            $this->sql .= $this->order . " " .$this->limit ." ". $this->offset;
31        } elseif ($mode == 2) {
32            $this->sql .= $this->order;
33        }
34        return $this->sql; 
35    }
36
37        // ¸¡º÷ÍÑ
38    function addSearchStr($val) {
39        $return = sfManualEscape($val);
40        $return = "%" .$return. "%";
41        return $return;
42    }
43   
44    //-- Èϰϸ¡º÷¡Ê¡û¡¡¢·¡¡¡û¡¡¤Þ¤Ç¡Ë
45    function selectRange($from, $to, $column) {
46
47        // ¤¢¤ëñ°Ì¤Î¤ß¸¡º÷($from = $to)
48        if(  $from == $to ) {
49            $this->setWhere( $column ." = ?" );
50            $return = array($from);
51        //¡¡¢·$to¤Þ¤Ç¸¡º÷
52        } elseif(  strlen($from) == 0 && strlen($to) > 0 ) {
53            $this->setWhere( $column ." <= ? ");
54            $return = array($to);
55        //¡¡¢·$from°Ê¾å¤ò¸¡º÷
56        } elseif(  strlen($from) > 0 && strlen($to) == 0 ) {
57            $this->setWhere( $column ." >= ? ");
58            $return = array($from);
59        //¡¡$from¢·$to¤Î¸¡º÷
60        } else {
61            $this->setWhere( $column ." BETWEEN ? AND ?" );
62            $return = array($from, $to);
63        }
64        return $return;
65    }
66
67    //--¡¡´ü´Ö¸¡º÷¡Ê¡ûǯ¡û·î¡ûÆü¤«¢·¡ûǯ¡û·î¡ûÆü¤Þ¤Ç¡Ë
68    function selectTermRange($from_year, $from_month, $from_day, $to_year, $to_month, $to_day, $column) {
69
70        sfprintr($from_year .":". $from_month.":". $from_day.":". $to_year.":". $to_month.":". $to_day);
71        $date1 = date("Y/m/d", mktime(0,0,0,$from_month,$from_day,$from_year));
72        $date2 = date("Y/m/d", mktime(0,0,0,$to_month,$to_day,$to_year));
73       
74        $date3 = strtotime(date('Y/m/d', mktime(0,0,0,$to_month,$to_day,$to_year) . " + day");
75       
76        sfprintr($date3);
77       
78        // ³«»Ï´ü´Ö¤À¤±»ØÄê¤Î¾ì¹ç
79        if( ( $from_year != "" ) && ( $from_month != "" ) && ( $from_day != "" ) && ( $to_year == "" ) && ( $to_month == "" ) && ( $to_day == "" ) ) {
80            $date1 = date("Y/m/d", mktime(0,0,0,$from_month,$from_day,$from_year));
81            $this->setWhere( $column ." >= ?" );
82            $return = array($date1);
83        }
84
85        //¡¡³«»Ï¢·½ªÎ»
86        if( ( $from_year != "" ) && ( $from_month != "" ) && ( $from_day != "" ) &&
87            ( $to_year != "" ) && ( $to_month != "" ) && ( $to_day != "" ) ) {
88
89            list( $date1, $date2, $err ) = sfCheckSetTerm( $from_year, $from_month, $from_day, $to_year, $to_month, $to_day );
90            if ( ! $err ) {
91                $this->setWhere( $column ." BETWEEN ? AND ?" );
92                $return = array($date1, $date2);
93            }
94        }
95
96        // ½ªÎ»´ü´Ö¤À¤±»ØÄê¤Î¾ì¹ç
97        if( ( $from_year == "" ) && ( $from_month == "" ) && ( $from_day == "" ) && ( $to_year != "" ) && ( $to_month != "" ) && ( $to_day != "" ) ) {
98   
99            list( $date1, $date2, $err ) = sfCheckSetTerm( 1900, 1, 2, $to_year, $to_month, $to_day );
100            if ( ! $err ) {
101                $this->setWhere( $column ." < ?" );
102                $return = array($date2);
103            }
104        }
105        return $return;
106    }   
107
108    // checkbox¤Ê¤É¤ÇƱ°ì¥«¥é¥àÆâ¤Çñ°ì¡¢¤â¤·¤¯¤ÏÊ£¿ôÁªÂò»è¤¬Í­¤ë¾ì¹ç¡¡Îã: AND ( sex = xxx OR sex = xxx OR sex = xxx  ) AND ...
109    function setItemTerm( $arr, $ItemStr ) {
110
111        foreach( $arr as $data ) {
112   
113            if( count( $arr ) > 1 ) {
114                if( ! is_null( $data ) ) $item .= $ItemStr . " = ? OR ";   
115            } else {
116                if( ! is_null( $data ) ) $item = $ItemStr . " = ?";
117            }
118            $return[] = $data;
119        }
120
121        if( count( $arr ) > 1 )  $item = "( " . rtrim( $item, " OR " ) . " )";
122        $this->setWhere( $item );
123        return $return;
124    }
125
126    //¡¡NULLÃͤ¬É¬Íפʾì¹ç
127    function setItemTermWithNull( $arr, $ItemStr ) {
128
129        $item = " ${ItemStr} IS NULL ";
130       
131        if ( $arr ){
132            foreach( $arr as $data ) { 
133                if ($data != "ÉÔÌÀ") {
134                    $item .= " OR ${ItemStr} = ?";
135                    $return[] = $data;
136                }
137            }
138        }
139       
140        $item = "( ${item} ) ";
141        $this->setWhere( $item );
142        return $return;
143    }
144    // NULL¤â¤·¤¯¤Ï''¤Ç¸¡º÷¤¹¤ë¾ì¹ç
145    function setItemTermWithNullAndSpace( $arr, $ItemStr ) {
146        $count = count($arr);
147        $item = " ${ItemStr} IS NULL OR ${ItemStr} = '' ";
148        $i = 1;
149        if ( $arr ){
150            foreach( $arr as $data ) { 
151                if ($i == $count) break;
152                $item .= " OR ${ItemStr} = ?"; 
153                $return[] = $data;
154                $i ++;
155            }
156        }
157        $item = "( ${item} ) ";
158        $this->setWhere( $item );
159        return $return;
160    }
161   
162
163
164    /* Ê£¿ô¤Î¥«¥é¥à¤ÇOR¤ÇÍ¥À踡º÷¤¹¤ë¾ì¹ç¡¡Îã¡§¡¡AND ( item_flag1 = xxx OR item_flag2 = xxx OR item_flag3 = xxx  ) AND ...
165
166        ÇÛÎó¤Î¹½Â¤Îã¡¡
167        if ( $_POST['show_site1'] ) $arrShowsite_1 = array( "column" => "show_site1",
168                                                            "value"  => $_POST['show_site1'] );
169
170    */
171    function setWhereByOR( $arrWhere ){
172
173        $count = count( $arrWhere );
174
175        for( $i = 0; $i < $count; $i++ ) {
176
177            if( isset( $arrWhere[$i]["value"] ) ) $statement .= $arrWhere[$i]["column"] ." = '" . addslashes( $arrWhere[$i]["value"] ) ."' OR "  ;
178        }
179
180        $statement = "( " . rtrim( $statement, " OR " ) . " )";
181
182        if( $this->where ) {
183
184            $this->where .= " AND " . $statement;
185
186        } else {
187
188            $this->where = "WHERE " . $statement;
189        }
190    }
191
192    function setWhere($where){
193        if ($where != "") {     
194            if( $this->where ) {
195   
196                $this->where .= " AND " . $where;
197   
198            } else {
199   
200                $this->where = "WHERE " . $where;
201            }
202        }
203    }
204
205    function setOrder($order){
206       
207            $this->order =  "ORDER BY " . $order;
208       
209    }
210
211    function setGroup( $group ) {
212       
213        $this->group =  "GROUP BY " . $group;
214       
215    }
216
217   
218    function setLimitOffset( $limit, $offset ){
219
220        if ( is_numeric($limit) and is_numeric($offset) ){
221
222            $this->limit = " LIMIT " .$limit;
223            $this->offset = " OFFSET " .$offset;
224        }   
225    }
226   
227    function clearSql(){
228        $this->select = "";
229        $this->where = "";
230        $this->group = "";
231        $this->order = "";
232        $this->limit = "";
233        $this->offset = "";
234    }
235   
236    function setSelect($sql) {
237        $this->select = $sql;
238    }
239}
240?>
Note: See TracBrowser for help on using the repository browser.