Changeset 149

Show
Ignore:
Timestamp:
04/27/06 22:40:01 (3 years ago)
Author:
laurentj
Message:

ajout d'un attribut 'distinct' sur les methodes dao

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/jelix/dao/jDAOGenerator.class.php

    r141 r149  
    266266 
    267267               case 'count': 
    268                   $src[] = '    $query = \'SELECT COUNT(*) as c \'.$this->_fromClause.$this->_whereClause;'; 
     268                  if($method->distinct !=''){ 
     269                    $prop=$this->_datasParser->getProperties (); 
     270                    $prop = $prop[$method->distinct]; 
     271                    $count=' DISTINCT '.$tables[$prop->table]['name'] .'.'.$prop->fieldName; 
     272                  }else{ 
     273                    $count='*'; 
     274                  } 
     275                  $src[] = '    $query = \'SELECT COUNT('.$count.') as c \'.$this->_fromClause.$this->_whereClause;'; 
    269276                  $glueCondition = ($sqlWhereClause !='' ? ' AND ':' WHERE '); 
    270277                  break; 
     
    272279               case 'select': 
    273280               default: 
    274                   $src[] = '    $query = $this->_selectClause.$this->_fromClause.$this->_whereClause;'; 
     281                  if($method->distinct !=''){ 
     282                    $select = '\''.$this->_getSelectClause($method->distinct).'\''; 
     283                  }else{ 
     284                     $select=' $this->_selectClause'; 
     285                  } 
     286                  $src[] = '    $query = '.$select.'.$this->_fromClause.$this->_whereClause;'; 
    275287                  $glueCondition = ($sqlWhereClause !='' ? ' AND ':' WHERE '); 
    276288                  if( ($lim = $method->getLimit ()) !==null){ 
     
    401413    * build SELECT clause for all SELECT queries 
    402414    */ 
    403    private function _getSelectClause (){ 
     415   private function _getSelectClause ($distinct=''){ 
    404416      $result = array(); 
    405417 
     
    416428                     //in oracle we must escape name 
    417429                  if ($driverName == 'oci8') { 
    418                      $result[] = $table.$prop->fieldName.' "'.$prop->name.'"'; 
     430                     $field = $table.$prop->fieldName.' "'.$prop->name.'"'; 
    419431                  }else{ 
    420                      $result[] = $table.$prop->fieldName.' as '.$prop->name; 
     432                     $field = $table.$prop->fieldName.' as '.$prop->name; 
    421433                  } 
    422434               }else{ 
    423                      $result[] = $table.$prop->fieldName; 
     435                     $field = $table.$prop->fieldName; 
    424436               } 
    425437            }else{ 
    426438               //in oracle we must escape name 
    427439               if ($driverName == 'oci8') { 
    428                   $result[] = sprintf ($prop->selectMotif, $table.$prop->fieldName).' "'.$prop->name.'"'; 
     440                  $field = sprintf ($prop->selectMotif, $table.$prop->fieldName).' "'.$prop->name.'"'; 
    429441               }else{ 
    430                   $result[] = sprintf ($prop->selectMotif, $table.$prop->fieldName).' as '.$prop->name; 
     442                  $field = sprintf ($prop->selectMotif, $table.$prop->fieldName).' as '.$prop->name; 
    431443               } 
    432444            } 
    433          } 
    434       } 
    435  
    436       return 'SELECT '.(implode (', ',$result)); 
     445 
     446            if($distinct == $prop->name) 
     447                array_unshift($result, $field); 
     448            else 
     449                $result[]=$field; 
     450         } 
     451      } 
     452 
     453      return 'SELECT '.($distinct!=''?'DISTINCT ':'').(implode (', ',$result)); 
    437454    } 
    438455 
  • trunk/lib/jelix/dao/jDAOParser.class.php

    r142 r149  
    332332   public $name; 
    333333   public $type; 
     334   public $distinct=''; 
    334335   private $_conditions = null; 
    335336   private $_parameters   = array(); 
     
    343344      $this->_def = $def; 
    344345 
    345       $params = $def->getAttr($method, array('name', 'type', 'call')); 
     346      $params = $def->getAttr($method, array('name', 'type', 'call','distinct')); 
    346347 
    347348      if ($params['name']===null){ 
     
    399400         } 
    400401         return; 
     402      } 
     403 
     404      if(($this->type == 'select' || $this->type == 'count') && strlen($params['distinct'])){ 
     405        $props = $this->_def->getProperties(); 
     406 
     407        if (!isset ($props[$params['distinct']])){ 
     408            $this->_def->_compiler->doDefError('method.property.unknown', array($this->name, $params['distinct'])); 
     409        } 
     410        $this->distinct=$params['distinct']; 
    401411      } 
    402412 
  • trunk/lib/jelix/docs/ns/dao.rng

    r135 r149  
    9898 
    9999         <element name="method"> 
    100             <attribute name="type"> 
    101                <choice> 
    102                   <value>count</value> 
    103                   <value>delete</value> 
    104                </choice> 
    105             </attribute> 
     100            <attribute name="type"><value>delete</value></attribute> 
    106101            <ref name="method.common" /> 
    107102            <ref name="method.conditions"/> 
     103         </element> 
     104 
     105         <element name="method"> 
     106             <attribute name="type"><value>count</value></attribute> 
     107             <optional> 
     108                 <attribute name="distinct" /> 
     109             </optional> 
     110             <ref name="method.common" /> 
     111             <ref name="method.conditions"/> 
    108112         </element> 
    109113 
     
    123127                  </choice> 
    124128               </attribute> 
     129            </optional> 
     130            <optional> 
     131                <attribute name="distinct" /> 
    125132            </optional> 
    126133            <ref name="method.common" /> 
  • trunk/testapp/modules/testapp/daos/testnews.dao.xml

    r131 r149  
    3232        <limit offset="0" count="$limit" /> 
    3333    </method> 
     34    <method name="selectdistinct" type="select" distinct="titre"> 
     35        <parameter name="limit" /> 
     36        <conditions> 
     37            <eq property="valider" value="3" /> 
     38        </conditions> 
     39        <order> 
     40            <orderitem property="date" way="desc" /> 
     41        </order> 
     42        <limit offset="0" count="$limit" /> 
     43    </method> 
     44    <method name="countdistinct" type="count" distinct="titre"> 
     45        <parameter name="limit" /> 
     46        <conditions> 
     47            <eq property="valider" value="3" /> 
     48        </conditions> 
     49    </method> 
     50 
     51 
    3452  </factory> 
    3553</dao> 
Download in other formats: Unified Diff Zip Archive