home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / classquery.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  7.5 KB  |  229 lines

  1. Class QueryGenerator 
  2.  
  3. Generates select query strings for a MySQL (may work on others!) database search.Currently supports case sensitive and fullword match. This class can be easily extended to make a search engine class. 
  4.  
  5.  
  6. <?php 
  7.  
  8. /***************************************************************************** 
  9.  Class Query v.1.1 
  10. ****************************************************************************** 
  11.  
  12.  Author: edward@planet-three.co.uk 
  13.  
  14.  Purpose: 
  15.   
  16.         To add a logical query to the SearchEngine class. The logic can be  
  17.         one of the following : AND , OR, NOT. 
  18.  
  19.  Usage:      
  20.         aquery = new Query("NOT", array("apple", "pear", "bannana")); 
  21.          
  22. *****************************************************************************/ 
  23.  
  24. class Query { 
  25.  
  26. //PRIVATE: 
  27.  
  28.     var $m_words; 
  29.     var $m_logic; 
  30.  
  31. //PUBLIC 
  32.  
  33.      // 
  34.      // Constructor   
  35.      
  36.      // logic: <AND | OR | NOT> 
  37.      // words: words to be searched for given logic 
  38.      
  39.     function Query($logic, $words) { 
  40.      
  41.         $this->m_words = $words; 
  42.         $this->m_logic = $logic; 
  43.     }    
  44.      
  45.      // print attributes of query 
  46.      
  47.     function p() { 
  48.      
  49.         echo  "$this->m_logic"; 
  50.         reset($this->m_words); 
  51.         while(list($i, $word) = each($this->m_words)) 
  52.             echo  ":$word"; 
  53.     } 
  54. }; 
  55.  
  56. /***************************************************************************** 
  57.  Class QueryGenerator v.1.2 
  58. ****************************************************************************** 
  59.   
  60.  Author: edward@planet-three.co.uk 
  61.   
  62.  purpose:  
  63.          
  64.         Generates selection query strings 
  65.  usage:      
  66.         query1 = new Query("AND", array("rabbit", "horse", "cow")); 
  67.         query2 = new Query("OR",  array("dogs", "cats", "kangaroos")); 
  68.         query3 = new Query("NOT", array("sheep")); 
  69.  
  70.         querygen = new QueryGenerator(array(query1, query2, query3)); 
  71.         query =  
  72.            querygen->GetQueryString("animal_table", "mamals_field", "ID_field"); 
  73.          
  74.         resultid = mysql_query(query);       
  75.         while(row = mysql_fetch_row(resultid)) 
  76.             echo "got row id ",row["ID_field"],"<br>"; 
  77.          
  78. *******************************************************************************/ 
  79.  
  80. class QueryGenerator { 
  81.  
  82.     var $m_Querys; 
  83.     var $m_start; 
  84.     var $m_offset;   
  85.      
  86.     var $m_wordmatch; 
  87.     var $m_casematch; 
  88.  
  89. //PUBLIC: 
  90.  
  91.      //  querys   : array of Query objects 
  92.      //  wordmatch: only match whole words 
  93.   
  94.     function QueryGenerator($Querys, $wordmatch=false, $casematch=false) { 
  95.      
  96.         $this->m_Querys = $Querys; 
  97.         $this->m_wordmatch = $wordmatch; 
  98.         $this->m_casematch = $casematch; 
  99.          
  100.         $this->Restrict(0,0); 
  101.     } 
  102.      
  103.      // start  : start searching data at this pos 
  104.      // offset : number of records to search after pos 
  105.      
  106.     function Restrict($start, $offset) { 
  107.      
  108.         $this->m_start  = $start; 
  109.         $this->m_offset = $offset;   
  110.     } 
  111.      
  112.     function GetRestrict(&$start, &$offset) { 
  113.      
  114.         $start = $this->m_start; 
  115.         $offset = $this->m_offset; 
  116.     } 
  117.      
  118.      // table : database table name 
  119.      // searchfield: field(s) to search in for resultfield(s) 
  120.      // resultfield: field(s) retrieved from search match 
  121.      // NOTE: field must be separated by "," eg, "field1,field2" 
  122.  
  123.      // RETURN: function returns resultfield 
  124.      
  125.     function GetQueryString($table, $searchfield, $resultfield= "ID") { 
  126.      
  127.          // begin query string 
  128.         $query_string =  "SELECT $resultfield FROM $table WHERE "; 
  129.      
  130.          // get the query string      
  131.         $query_string .= $this->BuildQueryString($table, $searchfield); 
  132.      
  133.          // append limit      
  134.         if ($this->m_start || $this->m_offset) { 
  135.             $query_string .=  "LIMIT $this->m_start";     
  136.             if($this->m_offset)           
  137.                 $query_string .=  ",$this->m_offset"; 
  138.         } 
  139.  
  140.         return $query_string; 
  141.     }    
  142.  
  143.      // build Query string that will return the number of rows selected 
  144.              
  145.     function GetCountQueryString($table, $searchfield) { 
  146.      
  147.          // begin query string 
  148.         $query_string =  "SELECT count(*) FROM $table WHERE "; 
  149.      
  150.          // get the query string      
  151.         $query_string .= $this->BuildQueryString($table, $searchfield); 
  152.                          
  153.         return $query_string; 
  154.     }        
  155.      
  156. //PRIVATE: 
  157.  
  158.      
  159.      // main function that generates the entire query 
  160.     function BuildQueryString($table, $searchfield) { 
  161.                          
  162.          // parse searchfields 
  163.         $searchfields = explode( ",", $searchfield);      
  164.         if(!is_array($searchfields))  
  165.             $searchfields=array($searchfield); 
  166.                      
  167.          // build query for each searchfield 
  168.         while ($searchfield = current($searchfields)) { 
  169.              
  170.              // uppercase do case insensitive man 
  171.             if (!$this->m_casematch) 
  172.                 $searchfield =  "UPPER(".$searchfield. ")"; 
  173.                      
  174.             $firstquery = true; 
  175.             $query_string .=  "("; 
  176.                                  
  177.              // build current query 
  178.             reset($this->m_Querys);                          
  179.             while($query = current($this->m_Querys)) {                           
  180.                  
  181.                  // append logic          
  182.                 if ($firstquery) 
  183.                     $firstquery=false; 
  184.                 else if ($query->m_logic ==  "NOT") 
  185.                     $query_string.= "AND"; 
  186.                 else 
  187.                     $query_string.=$query->m_logic;                                  
  188.                          
  189.                  // append where clause 
  190.                 $query_string .=  
  191.                 $this->BuildWhereClause($searchfield, $query);                                               
  192.                 next($this->m_Querys); 
  193.             } 
  194.             $query_string .= ")";                                 
  195.             if (next($searchfields)) 
  196.                 $query_string .=  "OR";   
  197.         }                                    
  198.         return $query_string; 
  199.     } 
  200.      
  201.      // build where clause 
  202.     function BuildWhereClause($searchfield, $query) { 
  203.                  
  204.         $query_string.= "(";                                      
  205.         while($word = current($query->m_words)) {                
  206.                                      
  207.             if (!$this->m_casematch) 
  208.                 $word = strtoupper($word);           
  209.                                      
  210.             if ($this->m_wordmatch) 
  211.                 $search_type =  "REGEXP '[[:<:]]".$word. "[[:>:]]'"; 
  212.             else 
  213.                 $search_type =  "REGEXP '$word'"; 
  214.                                                          
  215.             if ($query->m_logic ==  "NOT")  
  216.                 $query_string.= "$searchfield NOT "; 
  217.             else 
  218.                 $query_string.= "$searchfield "; 
  219.                  
  220.             $query_string.= $search_type;                    
  221.             if (next($query->m_words))               
  222.                 $query_string.= " OR "; 
  223.         }                                                                
  224.         $query_string.= ")";                                                      
  225.         return $query_string; 
  226.     }    
  227. };   
  228. ?> 
  229.