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

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