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

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