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

Revision 1884, 5.4 KB checked in by kakinaka, 18 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       
72        // FROM
73        $date1 = date("Y/m/d", mktime(0,0,0,$from_month,$from_day,$from_year));
74       
75        // TO(TO¤Ï+1Æü)
76        $date2 = date("Y/m/d", strtotime(date('Y/m/d', mktime(0,0,0,$to_month,$to_day,$to_year)) . " + day"));
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            $this->setWhere( $column ." >= ? AND ". $column . " < ?" );
89            $return = array($date1, $date2);
90        }
91
92        // ½ªÎ»´ü´Ö¤À¤±»ØÄê¤Î¾ì¹ç
93        if( ( $from_year == "" ) && ( $from_month == "" ) && ( $from_day == "" ) && ( $to_year != "" ) && ( $to_month != "" ) && ( $to_day != "" ) ) {
94            $this->setWhere( $column ." < ?" );
95            $return = array($date2);
96        }
97        return $return;
98    }   
99
100    // checkbox¤Ê¤É¤ÇƱ°ì¥«¥é¥àÆâ¤Çñ°ì¡¢¤â¤·¤¯¤ÏÊ£¿ôÁªÂò»è¤¬Í­¤ë¾ì¹ç¡¡Îã: AND ( sex = xxx OR sex = xxx OR sex = xxx  ) AND ...
101    function setItemTerm( $arr, $ItemStr ) {
102
103        foreach( $arr as $data ) {
104   
105            if( count( $arr ) > 1 ) {
106                if( ! is_null( $data ) ) $item .= $ItemStr . " = ? OR ";   
107            } else {
108                if( ! is_null( $data ) ) $item = $ItemStr . " = ?";
109            }
110            $return[] = $data;
111        }
112
113        if( count( $arr ) > 1 )  $item = "( " . rtrim( $item, " OR " ) . " )";
114        $this->setWhere( $item );
115        return $return;
116    }
117
118    //¡¡NULLÃͤ¬É¬Íפʾì¹ç
119    function setItemTermWithNull( $arr, $ItemStr ) {
120
121        $item = " ${ItemStr} IS NULL ";
122       
123        if ( $arr ){
124            foreach( $arr as $data ) { 
125                if ($data != "ÉÔÌÀ") {
126                    $item .= " OR ${ItemStr} = ?";
127                    $return[] = $data;
128                }
129            }
130        }
131       
132        $item = "( ${item} ) ";
133        $this->setWhere( $item );
134        return $return;
135    }
136    // NULL¤â¤·¤¯¤Ï''¤Ç¸¡º÷¤¹¤ë¾ì¹ç
137    function setItemTermWithNullAndSpace( $arr, $ItemStr ) {
138        $count = count($arr);
139        $item = " ${ItemStr} IS NULL OR ${ItemStr} = '' ";
140        $i = 1;
141        if ( $arr ){
142            foreach( $arr as $data ) { 
143                if ($i == $count) break;
144                $item .= " OR ${ItemStr} = ?"; 
145                $return[] = $data;
146                $i ++;
147            }
148        }
149        $item = "( ${item} ) ";
150        $this->setWhere( $item );
151        return $return;
152    }
153   
154
155
156    /* Ê£¿ô¤Î¥«¥é¥à¤ÇOR¤ÇÍ¥À踡º÷¤¹¤ë¾ì¹ç¡¡Î㡧¡¡AND ( item_flag1 = xxx OR item_flag2 = xxx OR item_flag3 = xxx  ) AND ...
157
158        ÇÛÎó¤Î¹½Â¤Îã¡¡
159        if ( $_POST['show_site1'] ) $arrShowsite_1 = array( "column" => "show_site1",
160                                                            "value"  => $_POST['show_site1'] );
161
162    */
163    function setWhereByOR( $arrWhere ){
164
165        $count = count( $arrWhere );
166
167        for( $i = 0; $i < $count; $i++ ) {
168
169            if( isset( $arrWhere[$i]["value"] ) ) $statement .= $arrWhere[$i]["column"] ." = '" . addslashes( $arrWhere[$i]["value"] ) ."' OR "  ;
170        }
171
172        $statement = "( " . rtrim( $statement, " OR " ) . " )";
173
174        if( $this->where ) {
175
176            $this->where .= " AND " . $statement;
177
178        } else {
179
180            $this->where = "WHERE " . $statement;
181        }
182    }
183
184    function setWhere($where){
185        if ($where != "") {     
186            if( $this->where ) {
187   
188                $this->where .= " AND " . $where;
189   
190            } else {
191   
192                $this->where = "WHERE " . $where;
193            }
194        }
195    }
196
197    function setOrder($order){
198       
199            $this->order =  "ORDER BY " . $order;
200       
201    }
202
203    function setGroup( $group ) {
204       
205        $this->group =  "GROUP BY " . $group;
206       
207    }
208
209   
210    function setLimitOffset( $limit, $offset ){
211
212        if ( is_numeric($limit) and is_numeric($offset) ){
213
214            $this->limit = " LIMIT " .$limit;
215            $this->offset = " OFFSET " .$offset;
216        }   
217    }
218   
219    function clearSql(){
220        $this->select = "";
221        $this->where = "";
222        $this->group = "";
223        $this->order = "";
224        $this->limit = "";
225        $this->offset = "";
226    }
227   
228    function setSelect($sql) {
229        $this->select = $sql;
230    }
231}
232?>
Note: See TracBrowser for help on using the repository browser.