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

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