Source for file jDaoMethod.class.php

Documentation is available at jDaoMethod.class.php

  1. <?php
  2. /**
  3. @package     jelix
  4. @subpackage  dao
  5. @author      Croes GĂ©rald, Laurent Jouanneau
  6. @contributor Laurent Jouanneau
  7. @copyright   2001-2005 CopixTeam, 2005-2006 Laurent Jouanneau
  8. *  This class was get originally from the Copix project (CopixDAODefinitionV1, Copix 2.3dev20050901, http://www.copix.org)
  9. *  Few lines of code are still copyrighted 2001-2005 CopixTeam (LGPL licence).
  10. *  Initial authors of this Copix class are Gerald Croes and Laurent Jouanneau,
  11. *  and this class was adapted/improved for Jelix by Laurent Jouanneau
  12. *
  13. @link        http://www.jelix.org
  14. @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  15. */
  16.  
  17. /**
  18.  * containers for properties of dao method
  19.  * @package  jelix
  20.  * @subpackage dao
  21.  */
  22. class jDaoMethod {
  23.     public $name;
  24.     public $type;
  25.     public $distinct=false;
  26.     public $eventBeforeEnabled = false;
  27.     public $eventAfterEnabled = false;
  28.     private $_conditions null;
  29.     private $_parameters   array();
  30.     private $_parametersDefaultValues array();
  31.     private $_limit null;
  32.     private $_values array();
  33.     private $_def null;
  34.     private $_procstock=null;
  35.     private $_body=null;
  36.     private $_groupBy=null;
  37.  
  38.     function __construct ($method$def){
  39.         $this->_def $def;
  40.  
  41.         $params $def->getAttr($methodarray('name''type''call','distinct''eventbefore''eventafter''groupby'));
  42.  
  43.         if ($params['name']===null){
  44.             throw new jDaoXmlException ('missing.attr'array('name''method'));
  45.         }
  46.  
  47.         $this->name = $params['name'];
  48.         $this->type = $params['type'strtolower($params['type']'select';
  49.  
  50.         if (isset ($method->parameter)){
  51.             foreach ($method->parameter as $param){
  52.                 $attr $param->attributes();
  53.                 if (!isset ($attr['name'])){
  54.                     throw new jDaoXmlException ('method.parameter.unknowname'array($this->name));
  55.                 }
  56.                 $this->_parameters[]=(string)$attr['name'];
  57.                 if (isset ($attr['default'])){
  58.                     $this->_parametersDefaultValues[(string)$attr['name']]=(string)$attr['default'];
  59.                 }
  60.             }
  61.         }
  62.  
  63.         if($this->type == 'sql'){
  64.             if($params['call'=== null){
  65.                 throw new jDaoXmlException  ('method.procstock.name.missing');
  66.             }
  67.             $this->_procstock=$params['call'];
  68.             return;
  69.         }
  70.  
  71.         if($this->type == 'php'){
  72.             if (isset ($method->body)){
  73.                 $this->_body = (string)$method->body;
  74.             }else{
  75.                 throw new jDaoXmlException  ('method.body.missing');
  76.             }
  77.             return;
  78.         }
  79.  
  80.         $this->_conditions new jDaoConditions();
  81.         if (isset ($method->conditions)){
  82.             $this->_parseConditions($method->conditions[0],false);
  83.         }
  84.  
  85.         if ($this->type == 'update' || $this->type == 'delete'{
  86.             if ($params['eventbefore'== 'true')
  87.                 $this->eventBeforeEnabled = true;
  88.             if ($params['eventafter'== 'true')
  89.                 $this->eventAfterEnabled = true;
  90.         }
  91.  
  92.         if($this->type == 'update'){
  93.             if($this->_def->hasOnlyPrimaryKeys)
  94.                 throw new jDaoXmlException ('method.update.forbidden',array($this->name));
  95.  
  96.             if(isset($method->values&& isset($method->values[0]->value)){
  97.                 foreach ($method->values[0]->value as $val){
  98.                     $this->_addValue($val);
  99.                 }
  100.             }else{
  101.                 throw new jDaoXmlException ('method.values.undefine',array($this->name));
  102.             }
  103.             return;
  104.         }
  105.  
  106.         if(strlen($params['distinct'])){
  107.             if($this->type == 'select'){
  108.                 $this->distinct=$this->_def->getBool($params['distinct']);
  109.             }elseif($this->type == 'count'){
  110.                 $props $this->_def->getProperties();
  111.                 if (!isset ($props[$params['distinct']])){
  112.                     throw new jDaoXmlException ('method.property.unknown'array($this->name$params['distinct']));
  113.                 }
  114.                 $this->distinct=$params['distinct'];
  115.             }else{
  116.                 throw new jDaoXmlException ('forbidden.attr.context'array('distinct''<method name="'.$this->name.'"'));
  117.             }
  118.         }
  119.  
  120.         if($this->type == 'count')
  121.             return;
  122.  
  123.         if (isset ($method->order&& isset($method->order[0]->orderitem)){
  124.             foreach($method->order[0]->orderitem as $item){
  125.                 $this->_addOrder ($item);
  126.             }
  127.         }
  128.  
  129.         if(strlen($params['groupby'])){
  130.             if($this->type == 'select' || $this->type == 'selectfirst'){
  131.                 $this->_groupBy preg_split("/[\s,]+/"$params['groupby']);
  132.                 $props $this->_def->getProperties();
  133.                 foreach($this->_groupBy as $p){
  134.                     if (!isset ($props[$p])) {
  135.                         throw new jDaoXmlException ('method.property.unknown'array($this->name$p));
  136.                     }
  137.                 }
  138.             }else{
  139.                 throw new jDaoXmlException ('forbidden.attr.context'array('groupby''<method name="'.$this->name.'"'));
  140.             }
  141.         }
  142.  
  143.         if (isset($method->limit)){
  144.             if(isset($method->limit[1])){
  145.                 throw new jDaoXmlException ('tag.duplicate'array('limit'$this->name));
  146.             }
  147.             if($this->type == 'select'){
  148.                 $this->_addLimit($method->limit[0]);
  149.             }else{
  150.                 throw new jDaoXmlException ('method.limit.forbidden'$this->name);
  151.             }
  152.         }
  153.     }
  154.  
  155.     public function getConditions ()return $this->_conditions;}
  156.     public function getParameters ()return $this->_parameters;}
  157.     public function getParametersDefaultValues ()return $this->_parametersDefaultValues;}
  158.     public function getLimit ()return $this->_limit;}
  159.     public function getValues ()return $this->_values;}
  160.     public function getProcStock ()return $this->_procstock;}
  161.     public function getBody ()return $this->_body;}
  162.     public function getGroupBy(return $this->_groupBy;}
  163.  
  164.     private function _parseConditions($conditions$subcond=true){
  165.         if (isset ($conditions['logic'])){
  166.             $kind strtoupper((string)$conditions['logic']);
  167.         }else{
  168.             $kind 'AND';
  169.         }
  170.  
  171.         if ($subcond){
  172.             $this->_conditions->startGroup ($kind);
  173.         }else{
  174.             $this->_conditions->condition->glueOp =$kind;
  175.         }
  176.  
  177.         foreach($conditions->children(as $op=>$cond){
  178.             if($op !='conditions')
  179.                 $this->_addCondition ($op,$cond);
  180.             else
  181.                 $this->_parseConditions ($cond);
  182.         }
  183.  
  184.         if ($subcond{
  185.             $this->_conditions->endGroup();
  186.         }
  187.  
  188.     }
  189.  
  190.     private $_op array('eq'=>'=''neq'=>'<>''lt'=>'<''gt'=>'>''lteq'=>'<=''gteq'=>'>=',
  191.         'like'=>'LIKE''notlike'=>'NOT LIKE''isnull'=>'IS NULL''isnotnull'=>'IS NOT NULL','in'=>'IN''notin'=>'NOT IN',
  192.         'binary_op'=>'dummy');
  193.       // 'between'=>'BETWEEN',  'notbetween'=>'NOT BETWEEN',
  194.  
  195.     private $_attrcond array('property''expr''operator''driver')//, 'min', 'max', 'exprmin', 'exprmax'
  196.  
  197.     private function _addCondition($op$cond){
  198.  
  199.         $attr $this->_def->getAttr($cond$this->_attrcond);
  200.  
  201.         $field_id ($attr['property']!==null$attr['property']:'');
  202.  
  203.         if(!isset($this->_op[$op])){
  204.             throw new jDaoXmlException ('method.condition.unknown'array($this->name$op));
  205.         }
  206.  
  207.         $operator $this->_op[$op];
  208.  
  209.         $props $this->_def->getProperties();
  210.  
  211.         if (!isset ($props[$field_id])){
  212.             throw new jDaoXmlException ('method.property.unknown'array($this->name$field_id));
  213.         }
  214.  
  215.         if($this->type=='update'){
  216.             if($props[$field_id]->table != $this->_def->getPrimaryTable()){
  217.                 throw new jDaoXmlException ('method.property.forbidden'array($this->name$field_id));
  218.             }
  219.         }
  220.  
  221.         if(isset($cond['value']))
  222.             $value=(string)$cond['value'];
  223.         else
  224.             $value null;
  225.  
  226.         if($value!==null && $attr['expr']!==null){
  227.             throw new jDaoXmlException ('method.condition.valueexpr.together'array($this->name$op));
  228.         }else if($value!==null){
  229.             if($op == 'isnull' || $op =='isnotnull'){
  230.                 throw new jDaoXmlException ('method.condition.valueexpr.notallowed'array($this->name$op,$field_id));
  231.             }
  232.             if($op == 'binary_op'{
  233.                 if (!isset($attr['operator']|| empty($attr['operator'])) {
  234.                     throw new jDaoXmlException ('method.condition.operator.missing'array($this->name$op,$field_id));
  235.                 }
  236.                 if (isset($attr['driver']&& !empty($attr['driver'])) {
  237.                     if (jDaoCompiler::$dbType != $attr['driver']{
  238.                         throw new jDaoXmlException ('method.condition.driver.notallowed'array($this->name$op,$field_id));
  239.                     }
  240.                 }
  241.                 $operator $attr['operator'];
  242.             }
  243.             $this->_conditions->addCondition ($field_id$operator$value);
  244.         }else if($attr['expr']!==null){
  245.             if($op == 'isnull' || $op =='isnotnull'){
  246.                 throw new jDaoXmlException ('method.condition.valueexpr.notallowed'array($this->name$op$field_id));
  247.             }
  248.             if(($op == 'in' || $op =='notin')&& !preg_match('/^\$[a-zA-Z0-9_]+$/'$attr['expr'])){
  249.                 throw new jDaoXmlException ('method.condition.innotin.bad.expr'array($this->name$op$field_id));
  250.             }
  251.             if($op == 'binary_op'{
  252.                 if (!isset($attr['operator']|| empty($attr['operator'])) {
  253.                     throw new jDaoXmlException ('method.condition.operator.missing'array($this->name$op,$field_id));
  254.                 }
  255.                 if (isset($attr['driver']&& !empty($attr['driver'])) {
  256.                     if (jDaoCompiler::$dbType != $attr['driver']{
  257.                         throw new jDaoXmlException ('method.condition.driver.notallowed'array($this->name$op,$field_id));
  258.                     }
  259.                 }
  260.                 $operator $attr['operator'];
  261.             }
  262.             $this->_conditions->addCondition ($field_id$operator$attr['expr']true);
  263.         }else{
  264.             if($op != 'isnull' && $op !='isnotnull'){
  265.                 throw new jDaoXmlException ('method.condition.valueexpr.missing'array($this->name$op$field_id));
  266.             }
  267.             $this->_conditions->addCondition ($field_id$operator''false);
  268.         }
  269.     }
  270.  
  271.     private function _addOrder($order){
  272.         $attr $this->_def->getAttr($orderarray('property','way'));
  273.  
  274.         $way  ($attr['way'!== null $attr['way']:'ASC');
  275.  
  276.         if(substr ($way,0,1== '$'){
  277.             if(!in_array (substr ($way,1),$this->_parameters)){
  278.                 throw new jDaoXmlException ('method.orderitem.parameter.unknow'array($this->name$way));
  279.             }
  280.         }
  281.  
  282.         if ($attr['property'!= ''){
  283.             $prop =$this->_def->getProperties();
  284.             if(isset($prop[$attr['property']])){
  285.                 $this->_conditions->addItemOrder($attr['property']$way);
  286.             }elseif(substr ($attr['property'],0,1== '$'){
  287.                 if(!in_array (substr ($attr['property'],1),$this->_parameters)){
  288.                     throw new jDaoXmlException ('method.orderitem.parameter.unknow'array($this->name$way));
  289.                 }
  290.                 $this->_conditions->addItemOrder($attr['property']$way);
  291.             }else{
  292.                 throw new jDaoXmlException ('method.orderitem.bad'array($attr['property']$this->name));
  293.             }
  294.         }else{
  295.             throw new jDaoXmlException ('method.orderitem.property.missing'array($this->name));
  296.         }
  297.     }
  298.  
  299.     private function _addValue($attr){
  300.         if(isset($attr['value']))
  301.             $value=(string)$attr['value'];
  302.         else
  303.             $value null;
  304.  
  305.         $attr $this->_def->getAttr($attrarray('property','expr'));
  306.  
  307.         $prop $attr['property'];
  308.         $props =$this->_def->getProperties();
  309.  
  310.         if ($prop === null){
  311.             throw new jDaoXmlException ('method.values.property.unknow'array($this->name$prop));
  312.         }
  313.  
  314.         if(!isset($props[$prop])){
  315.             throw new jDaoXmlException ('method.values.property.unknow'array($this->name$prop));
  316.         }
  317.  
  318.         if($props[$prop]->table != $this->_def->getPrimaryTable()){
  319.             throw new jDaoXmlException ('method.values.property.bad'array($this->name,$prop ));
  320.         }
  321.  
  322.         if($props[$prop]->isPK){
  323.             throw new jDaoXmlException ('method.values.property.pkforbidden'array($this->name,$prop ));
  324.         }
  325.  
  326.         if($value!==null && $attr['expr']!==null){
  327.             throw new jDaoXmlException ('method.values.valueexpr'array($this->name$prop));
  328.         }else if($value!==null){
  329.             $this->_values [$prop]array$valuefalse);
  330.         }else if($attr['expr']!==null){
  331.             $this->_values [$prop]array$attr['expr']true);
  332.         }else{
  333.             $this->_values [$prop]array''false);
  334.         }
  335.     }
  336.  
  337.     private function _addLimit($limit){
  338.         $attr $this->_def->getAttr($limitarray('offset','count'));
  339.  
  340.         extract($attr);
  341.  
  342.         if$offset === null){
  343.             throw new jDaoXmlException ('missing.attr',array('offset','limit'));
  344.         }
  345.         if($count === null){
  346.             throw new jDaoXmlException ('missing.attr',array('count','limit'));
  347.         }
  348.  
  349.         if(substr ($offset,0,1== '$'){
  350.             if(in_array (substr ($offset,1),$this->_parameters)){
  351.                 $offsetparam=true;
  352.             }else{
  353.                 throw new jDaoXmlException ('method.limit.parameter.unknow'array($this->name$offset));
  354.             }
  355.         }else{
  356.             if(is_numeric ($offset)){
  357.                 $offsetparam=false;
  358.                 $offset intval ($offset);
  359.             }else{
  360.                 throw new jDaoXmlException ('method.limit.badvalue'array($this->name$offset));
  361.             }
  362.         }
  363.  
  364.         if(substr ($count,0,1== '$'){
  365.             if(in_array (substr ($count,1),$this->_parameters)){
  366.                 $countparam=true;
  367.             }else{
  368.                 throw new jDaoXmlException ('method.limit.parameter.unknow'array($this->name$count));
  369.             }
  370.         }else{
  371.             if(is_numeric($count)){
  372.                 $countparam=false;
  373.                 $count=intval($count);
  374.             }else{
  375.                 throw new jDaoXmlException ('method.limit.badvalue'array($this->name$count));
  376.             }
  377.         }
  378.         $this->_limitcompact('offset''count''offsetparam','countparam');
  379.     }
  380. }
  381. ?>

Documentation generated on Wed, 07 Sep 2011 13:47:01 +0200 by phpDocumentor 1.4.3