Source for file jDaoGenerator.class.php

Documentation is available at jDaoGenerator.class.php

  1. <?php
  2. /**
  3. * @package    jelix
  4. * @subpackage dao
  5. * @author     GĂ©rald Croes, Laurent Jouanneau
  6. * @contributor Laurent Jouanneau
  7. * @contributor Bastien Jaillot (bug fix)
  8. * @contributor Julien Issler
  9. * @copyright  2001-2005 CopixTeam, 2005-2011 Laurent Jouanneau
  10. * @copyright  2007-2008 Julien Issler
  11. *  This class was get originally from the Copix project (CopixDAOGeneratorV1, Copix 2.3dev20050901, http://www.copix.org)
  12. *  Few lines of code are still copyrighted 2001-2005 CopixTeam (LGPL licence).
  13. *  Initial authors of this Copix class are Gerald Croes and Laurent Jouanneau,
  14. *  and this class was rewrited for Jelix by Laurent Jouanneau
  15. *
  16. * @link        http://www.jelix.org
  17. * @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  18. */
  19.  
  20. /**
  21. * This is a generator which creates php class from dao xml file.
  22. *
  23. * It is called by jDaoCompiler
  24. * @package  jelix
  25. * @subpackage dao
  26. * @see jDaoCompiler
  27. */
  28. class jDaoGenerator {
  29.  
  30.     /**
  31.     * the dao definition.
  32.     * @var jDaoParser 
  33.     */
  34.     protected $_dataParser = null;
  35.  
  36.     /**
  37.     * The DaoRecord ClassName
  38.     * @var string 
  39.     */
  40.     protected $_DaoRecordClassName = null;
  41.  
  42.     /**
  43.     * the DAO classname
  44.     * @var string 
  45.     */
  46.     protected $_DaoClassName = null;
  47.  
  48.     protected $propertiesListForInsert = 'PrimaryTable';
  49.  
  50.     protected $aliasWord = ' AS ';
  51.  
  52.     /**
  53.      * @var jDbTools 
  54.     */
  55.     protected $tools;
  56.  
  57.     protected $_daoId;
  58.     protected $_daoPath;
  59.     protected $_dbType;
  60.  
  61.     /**
  62.      * the real name of the main table
  63.      */
  64.     protected $tableRealName = '';
  65.  
  66.     /**
  67.      * the real name of the main table, escaped in SQL
  68.      * so it is ready to include into a SQL query.
  69.      */
  70.     protected $tableRealNameEsc = '';
  71.     
  72.     protected $sqlWhereClause = '';
  73.     
  74.     protected $sqlFromClause = '';
  75.     
  76.     protected $sqlSelectClause = '';
  77.  
  78.     /**
  79.     * constructor
  80.     * @param jDaoParser $daoDefinition 
  81.     */
  82.     function __construct($selector, $tools, $daoParser){
  83.         $this->_daoId = $selector->toString();
  84.         $this->_daoPath = $selector->getPath();
  85.         $this->_dbType = $selector->driver;
  86.         $this->_dataParser = $daoParser;
  87.         $this->_DaoClassName = $selector->getDaoClass();
  88.         $this->_DaoRecordClassName = $selector->getDaoRecordClass();
  89.         $this->tools = $tools;
  90.     }
  91.  
  92.     /**
  93.     * build all classes
  94.     */
  95.     public function buildClasses () {
  96.  
  97.         $src = array();
  98.         $src[] = ' require_once ( JELIX_LIB_PATH .\'dao/jDaoRecordBase.class.php\');';
  99.         $src[] = ' require_once ( JELIX_LIB_PATH .\'dao/jDaoFactoryBase.class.php\');';
  100.  
  101.         // prepare some values to generate properties and methods
  102.  
  103.         $this->buildFromWhereClause();
  104.         $this->sqlSelectClause   = $this->buildSelectClause();
  105.  
  106.         $tables            = $this->_dataParser->getTables();
  107.         $pkFields          = $this->_getPrimaryFieldsList();
  108.         $this->tableRealName    = $tables[$this->_dataParser->getPrimaryTable()]['realname'];
  109.         $this->tableRealNameEsc = $this->_encloseName('\'.$this->_conn->prefixTable(\''.$this->tableRealName.'\').\'');
  110.  
  111.         $sqlPkCondition    = $this->buildSimpleConditions($pkFields);
  112.         if ($sqlPkCondition != '') {
  113.             $sqlPkCondition= ($this->sqlWhereClause !='' ? ' AND ':' WHERE ').$sqlPkCondition;
  114.         }
  115.  
  116.         //-----------------------
  117.         // Build the record class
  118.         //-----------------------
  119.  
  120.         $src[] = "\nclass ".$this->_DaoRecordClassName.' extends jDaoRecordBase {';
  121.  
  122.         $properties=array();
  123.  
  124.         foreach ($this->_dataParser->getProperties() as $id=>$field) {
  125.             $properties[$id] = get_object_vars($field);
  126.             if ($field->defaultValue !== null) {
  127.                 $src[] =' public $'.$id.'='.var_export($field->defaultValue, true).';';
  128.             }
  129.             else
  130.                 $src[] =' public $'.$id.';';
  131.         }
  132.  
  133.         // TODO PHP 5.3 : we could remove that
  134.         $src[] = '   public function getProperties() { return '.$this->_DaoClassName.'::$_properties; }';
  135.         $src[] = '   public function getPrimaryKeyNames() { return '.$this->_DaoClassName.'::$_pkFields; }';
  136.         $src[] = '}';
  137.  
  138.         //--------------------
  139.         // Build the dao class
  140.         //--------------------
  141.  
  142.         $src[] = "\nclass ".$this->_DaoClassName.' extends jDaoFactoryBase {';
  143.         $src[] = '   protected $_tables = '.var_export($tables, true).';';
  144.         $src[] = '   protected $_primaryTable = \''.$this->_dataParser->getPrimaryTable().'\';';
  145.         $src[] = '   protected $_selectClause=\''.$this->sqlSelectClause.'\';';
  146.         $src[] = '   protected $_fromClause;';
  147.         $src[] = '   protected $_whereClause=\''.$this->sqlWhereClause.'\';';
  148.         $src[] = '   protected $_DaoRecordClassName=\''.$this->_DaoRecordClassName.'\';';
  149.         $src[] = '   protected $_daoSelector = \''.$this->_daoId.'\';';
  150.  
  151.         if($this->tools->trueValue != '1'){
  152.             $src[]='   protected $trueValue ='.var_export($this->tools->trueValue, true).';';
  153.             $src[]='   protected $falseValue ='.var_export($this->tools->falseValue, true).';';
  154.         }
  155.  
  156.         if($this->_dataParser->hasEvent('deletebefore') || $this->_dataParser->hasEvent('delete'))
  157.             $src[] = '   protected $_deleteBeforeEvent = true;';
  158.         if ($this->_dataParser->hasEvent('deleteafter') || $this->_dataParser->hasEvent('delete'))
  159.             $src[] = '   protected $_deleteAfterEvent = true;';
  160.         if ($this->_dataParser->hasEvent('deletebybefore') || $this->_dataParser->hasEvent('deleteby'))
  161.             $src[] = '   protected $_deleteByBeforeEvent = true;';
  162.         if ($this->_dataParser->hasEvent('deletebyafter') || $this->_dataParser->hasEvent('deleteby'))
  163.             $src[] = '   protected $_deleteByAfterEvent = true;';
  164.  
  165.         $src[] = '   public static $_properties = '.var_export($properties, true).';';
  166.         $src[] = '   public static $_pkFields = array('.$this->_writeFieldNamesWith ($start = '\'', $end='\'', $beetween = ',', $pkFields).');';
  167.  
  168.         $src[] = ' ';
  169.         $src[] = 'public function __construct($conn){';
  170.         $src[] = '   parent::__construct($conn);';
  171.         $src[] = '   $this->_fromClause = \''.$this->sqlFromClause.'\';';
  172.         $src[] = '}';
  173.  
  174.         // cannot put this methods directly into jDaoBase because self cannot refer to a child class
  175.         // FIXME PHP53, we could use the static keyword instead of self
  176.         $src[] = '   public function getProperties() { return self::$_properties; }';
  177.         $src[] = '   public function getPrimaryKeyNames() { return self::$_pkFields;}';
  178.  
  179.         $src[] = ' ';
  180.         $src[] = ' protected function _getPkWhereClauseForSelect($pk){';
  181.         $src[] = '   extract($pk);';
  182.         $src[] = ' return \''.$sqlPkCondition.'\';';
  183.         $src[] = '}';
  184.  
  185.         $src[] = ' ';
  186.         $src[] = 'protected function _getPkWhereClauseForNonSelect($pk){';
  187.         $src[] = '   extract($pk);';
  188.         $src[] = '   return \' where '.$this->buildSimpleConditions($pkFields,'',false).'\';';
  189.         $src[] = '}';
  190.  
  191.         //----- Insert method
  192.  
  193.         $src[] = $this->buildInsertMethod($pkFields);
  194.  
  195.         //-----  update method
  196.  
  197.         $src[] = $this->buildUpdateMethod($pkFields);
  198.  
  199.         //----- other user methods
  200.  
  201.         $src[] = $this->buildUserMethods();
  202.  
  203.         $src[] = $this->buildEndOfClass();
  204.  
  205.         $src[] = '}';//end of class
  206.  
  207.         return implode("\n",$src);
  208.     }
  209.  
  210.     /**
  211.      * build the insert() method in the final class
  212.      * @return string the source of the method
  213.      */
  214.     protected function buildInsertMethod($pkFields) {
  215.         $pkai = $this->getAutoIncrementPKField();
  216.         $src = array();
  217.         $src[] = 'public function insert ($record){';
  218.  
  219.         if($pkai !== null){
  220.             // if there is an autoincrement field as primary key
  221.  
  222.             // if a value is given for the autoincrement field, then with do a full insert
  223.             $src[]=' if($record->'.$pkai->name.' > 0 ){';
  224.             $src[] = '    $query = \'INSERT INTO '.$this->tableRealNameEsc.' (';
  225.             $fields = $this->_getPropertiesBy('PrimaryTable');
  226.             list($fields, $values) = $this->_prepareValues($fields,'insertPattern', 'record->');
  227.  
  228.             $src[] = implode(',',$fields);
  229.             $src[] = ') VALUES (';
  230.             $src[] = implode(', ',$values);
  231.             $src[] = ")';";
  232.  
  233.             $src[] = '}else{';
  234.  
  235.             $fields = $this->_getPropertiesBy($this->propertiesListForInsert);
  236.         }else{
  237.             $fields = $this->_getPropertiesBy('PrimaryTable');
  238.         }
  239.  
  240.         if($this->_dataParser->hasEvent('insertbefore') || $this->_dataParser->hasEvent('insert')){
  241.             $src[] = '   jEvent::notify("daoInsertBefore", array(\'dao\'=>$this->_daoSelector, \'record\'=>$record));';
  242.         }
  243.         
  244.         // if there isn't a autoincrement as primary key, then we do a full insert.
  245.         // if there isn't a value for the autoincrement field and if this is a mysql/sqlserver and pgsql,
  246.         // we do an insert without given primary key. In other case, we do a full insert.
  247.  
  248.         $src[] = '    $query = \'INSERT INTO '.$this->tableRealNameEsc.' (';
  249.  
  250.         list($fields, $values) = $this->_prepareValues($fields,'insertPattern', 'record->');
  251.  
  252.         $src[] = implode(',',$fields);
  253.         $src[] = ') VALUES (';
  254.         $src[] = implode(', ',$values);
  255.         $src[] = ")';";
  256.  
  257.         if($pkai !== null)
  258.             $src[] = '}';
  259.  
  260.         $src[] = '   $result = $this->_conn->exec ($query);';
  261.  
  262.         if($pkai !== null){
  263.             $src[] = '   if(!$result)';
  264.             $src[] = '       return false;';
  265.  
  266.             $src[] = '   if($record->'.$pkai->name.' < 1 ) ';
  267.             $src[] = $this->buildUpdateAutoIncrementPK($pkai);
  268.         }
  269.  
  270.         // we generate a SELECT query to update field on the record object, which are autoincrement or calculated
  271.         $fields = $this->_getPropertiesBy('FieldToUpdate');
  272.         if (count($fields)) {
  273.             $result = array();
  274.             foreach ($fields as $id=>$prop){
  275.                 $result[]= $this->buildSelectPattern($prop->selectPattern, '', $prop->fieldName, $prop->name);
  276.             }
  277.  
  278.             $sql = 'SELECT '.(implode (', ',$result)). ' FROM '.$this->tableRealNameEsc.' WHERE ';
  279.             $sql.= $this->buildSimpleConditions($pkFields, 'record->', false);
  280.  
  281.             $src[] = '  $query =\''.$sql.'\';';
  282.             $src[] = '  $rs  =  $this->_conn->query ($query);';
  283.             $src[] = '  $newrecord =  $rs->fetch ();';
  284.             foreach ($fields as $id=>$prop){
  285.                 $src[] = '  $record->'.$prop->name.' = $newrecord->'.$prop->name.';';
  286.             }
  287.         }
  288.  
  289.         if($this->_dataParser->hasEvent('insertafter') || $this->_dataParser->hasEvent('insert')){
  290.             $src[] = '   jEvent::notify("daoInsertAfter", array(\'dao\'=>$this->_daoSelector, \'record\'=>$record));';
  291.         }
  292.  
  293.         $src[] = '    return $result;';
  294.         $src[] = '}';
  295.  
  296.         return implode("\n",$src);
  297.     }
  298.  
  299.     /**
  300.      * build the update() method for the final class
  301.      * @return string the source of the method
  302.      */
  303.     protected function buildUpdateMethod($pkFields) {
  304.         $src = array();
  305.         
  306.         $src[] = 'public function update ($record){';
  307.         list($fields, $values) = $this->_prepareValues($this->_getPropertiesBy('PrimaryFieldsExcludePk'),'updatePattern', 'record->');
  308.         
  309.         if(count($fields)){
  310.             
  311.             if($this->_dataParser->hasEvent('updatebefore') || $this->_dataParser->hasEvent('update')){
  312.                 $src[] = '   jEvent::notify("daoUpdateBefore", array(\'dao\'=>$this->_daoSelector, \'record\'=>$record));';
  313.             }
  314.             
  315.             $src[] = '   $query = \'UPDATE '.$this->tableRealNameEsc.' SET ';
  316.             $sqlSet='';
  317.             foreach($fields as $k=> $fname){
  318.                 $sqlSet.= ', '.$fname. '= '. $values[$k];
  319.             }
  320.             $src[] = substr($sqlSet,1);
  321.  
  322.             $sqlCondition = $this->buildSimpleConditions($pkFields, 'record->', false);
  323.             if($sqlCondition!='')
  324.                 $src[] = ' where '.$sqlCondition;
  325.  
  326.             $src[] = "';";
  327.  
  328.             $src[] = '   $result = $this->_conn->exec ($query);';
  329.  
  330.             // we generate a SELECT query to update field on the record object, which are autoincrement or calculated
  331.             $fields = $this->_getPropertiesBy('FieldToUpdateOnUpdate');
  332.             if (count($fields)) {
  333.                 $result = array();
  334.                 foreach ($fields as $id=>$prop){
  335.                     $result[]= $this->buildSelectPattern($prop->selectPattern, '', $prop->fieldName, $prop->name);
  336.                 }
  337.  
  338.                 $sql = 'SELECT '.(implode (', ',$result)). ' FROM '.$this->tableRealNameEsc.' WHERE ';
  339.                 $sql.= $this->buildSimpleConditions($pkFields, 'record->', false);
  340.  
  341.                 $src[] = '  $query =\''.$sql.'\';';
  342.                 $src[] = '  $rs  =  $this->_conn->query ($query, jDbConnection::FETCH_INTO, $record);';
  343.                 $src[] = '  $record =  $rs->fetch ();';
  344.             }
  345.  
  346.             if($this->_dataParser->hasEvent('updateafter') || $this->_dataParser->hasEvent('update'))
  347.                 $src[] = '   jEvent::notify("daoUpdateAfter", array(\'dao\'=>$this->_daoSelector, \'record\'=>$record));';
  348.  
  349.             $src[] = '   return $result;';
  350.         }else{
  351.             //the dao is mapped on a table which contains only primary key : update is impossible
  352.             // so we will generate an error on update
  353.             $src[] = "     throw new jException('jelix~dao.error.update.impossible',array('".$this->_daoId."','".$this->_daoPath."'));";
  354.         }
  355.  
  356.         $src[] = ' }';//ends the update function
  357.         return implode("\n",$src);
  358.     }
  359.  
  360.     /**
  361.      * build all methods defined by the developer in the dao file
  362.      * @return string the source of the methods
  363.      */
  364.     protected function buildUserMethods() {
  365.         
  366.         $allField = $this->_getPropertiesBy('All');
  367.         $primaryFields = $this->_getPropertiesBy('PrimaryTable');
  368.         $src = array();
  369.  
  370.         foreach($this->_dataParser->getMethods() as $name=>$method){
  371.  
  372.             $defval = $method->getParametersDefaultValues();
  373.             if(count($defval)){
  374.                 $mparam='';
  375.                 foreach($method->getParameters() as $param){
  376.                     $mparam.=', $'.$param;
  377.                     if(isset($defval[$param]))
  378.                         $mparam.='=\''.str_replace("'","\'",$defval[$param]).'\'';
  379.                 }
  380.                 $mparam = substr($mparam,1);
  381.             }else{
  382.                 $mparam=implode(', $',$method->getParameters());
  383.                 if($mparam != '') $mparam ='$'.$mparam;
  384.             }
  385.  
  386.             $src[] = ' function '.$method->name.' ('. $mparam.'){';
  387.  
  388.             $limit='';
  389.  
  390.             $glueCondition =' WHERE ';
  391.             switch($method->type){
  392.                 case 'delete':
  393.                     $this->buildDeleteUserQuery($method, $src, $primaryFields);
  394.                     break;
  395.                 case 'update':
  396.                     $this->buildUpdateUserQuery($method, $src, $primaryFields);
  397.                     break;
  398.                 case 'php':
  399.                     $src[] = $method->getBody();
  400.                     $src[] = '}';
  401.                     break;
  402.  
  403.                 case 'count':
  404.                     $this->buildCountUserQuery($method, $src, $allField);
  405.                     break;
  406.                 case 'selectfirst':
  407.                 case 'select':
  408.                 default:
  409.                     $limit = $this->buildSelectUserQuery($method, $src, $allField);
  410.             }
  411.  
  412.             if($method->type == 'php')
  413.                 continue;
  414.  
  415.  
  416.             switch($method->type){
  417.                 case 'delete':
  418.                 case 'update' :
  419.                     if ($method->eventBeforeEnabled || $method->eventAfterEnabled) {
  420.                         $src[] = '   $args = func_get_args();';
  421.                         $methname = ($method->type == 'update'?'Update':'Delete');
  422.                         if ($method->eventBeforeEnabled) {
  423.                             $src[] = '   jEvent::notify("daoSpecific'.$methname.'Before", array(\'dao\'=>$this->_daoSelector,\'method\'=>\''.
  424.                             $method->name.'\', \'params\'=>$args));';
  425.                         }
  426.                         if ($method->eventAfterEnabled) {
  427.                             $src[] = '   $result = $this->_conn->exec ($__query);';
  428.                             $src[] = '   jEvent::notify("daoSpecific'.$methname.'After", array(\'dao\'=>$this->_daoSelector,\'method\'=>\''.
  429.                                 $method->name.'\', \'params\'=>$args));';
  430.                             $src[] = '   return $result;';
  431.                         } else {
  432.                             $src[] = '    return $this->_conn->exec ($__query);';
  433.                         }
  434.                     } else {
  435.                         $src[] = '    return $this->_conn->exec ($__query);';
  436.                     }
  437.                     break;
  438.                 case 'count':
  439.                     $src[] = '    $__rs = $this->_conn->query($__query);';
  440.                     $src[] = '    $__res = $__rs->fetch();';
  441.                     $src[] = '    return intval($__res->c);';
  442.                     break;
  443.                 case 'selectfirst':
  444.                     $src[] = '    $__rs = $this->_conn->limitQuery($__query,0,1);';
  445.                     $src[] = '    $this->finishInitResultSet($__rs);';
  446.                     $src[] = '    return $__rs->fetch();';
  447.                     break;
  448.                 case 'select':
  449.                 default:
  450.                     if($limit)
  451.                         $src[] = '    $__rs = $this->_conn->limitQuery($__query'.$limit.');';
  452.                     else
  453.                         $src[] = '    $__rs = $this->_conn->query($__query);';
  454.                     $src[] = '    $this->finishInitResultSet($__rs);';
  455.                     $src[] = '    return $__rs;';
  456.             }
  457.             $src[] = '}';
  458.         }
  459.         return implode("\n",$src);
  460.     }
  461.  
  462.     /**
  463.      *
  464.      */
  465.     protected function buildDeleteUserQuery($method, &$src, &$primaryFields) {
  466.         $src[] = '    $__query = \'DELETE FROM '.$this->tableRealNameEsc.' \';';
  467.         $cond = $method->getConditions();
  468.         if($cond !== null) {
  469.             $sqlCond = $this->buildConditions($cond, $primaryFields, $method->getParameters(), false);
  470.             if(trim($sqlCond) != '')
  471.                 $src[] = '$__query .=\' WHERE '.$sqlCond."';";
  472.         }
  473.     }
  474.  
  475.     /**
  476.      *
  477.      */
  478.     protected function buildUpdateUserQuery($method, &$src, &$primaryFields) {
  479.         $src[] = '    $__query = \'UPDATE '.$this->tableRealNameEsc.' SET ';
  480.         $updatefields = $this->_getPropertiesBy('PrimaryFieldsExcludePk');
  481.         $sqlSet='';
  482.  
  483.         foreach($method->getValues() as $propname=>$value){
  484.             if($value[1]){
  485.                 preg_match_all('/\$([a-zA-Z0-9_]+)/', $value[0], $varMatches, PREG_OFFSET_CAPTURE );
  486.                 $parameters = $method->getParameters();
  487.                 if (count($varMatches[0])) {
  488.                     $result = '';
  489.                     $len = 0;
  490.                     foreach($varMatches[1] as $k=>$var) {
  491.                         $result .= substr($value[0], $len, $len+$varMatches[0][$k][1]);
  492.                         $len += $varMatches[0][$k][1] + strlen($varMatches[0][$k][0]);
  493.                         if (in_array($var[0], $parameters)) {
  494.                             $result .= '\'.'.$this->_preparePHPExpr($varMatches[0][$k][0], $updatefields[$propname],true).'.\'';
  495.                         }
  496.                         else {
  497.                             $result .= $varMatches[0][$k][0];
  498.                         }
  499.                     }
  500.                     $value[0] = $result;
  501.                 }
  502.                 $sqlSet.= ', '.$this->_encloseName($updatefields[$propname]->fieldName). '= '. $value[0];
  503.             }else{
  504.                 $sqlSet.= ', '.$this->_encloseName($updatefields[$propname]->fieldName). '= '.
  505.                     $this->tools->escapeValue($updatefields[$propname]->unifiedType, $value[0], false, true);
  506.             }
  507.         }
  508.         $src[] = substr($sqlSet,1).'\';';
  509.         $cond = $method->getConditions();
  510.         if($cond !== null) {
  511.             $sqlCond = $this->buildConditions($cond, $primaryFields, $method->getParameters(), false);
  512.             if(trim($sqlCond) != '')
  513.                 $src[] = '$__query .=\' WHERE '.$sqlCond."';";
  514.         }
  515.     }
  516.  
  517.     /**
  518.      *
  519.      */
  520.     protected function buildCountUserQuery($method, &$src, &$allField) {
  521.         if ($method->distinct !='') {
  522.             $properties = $this->_dataParser->getProperties ();
  523.             $tables = $this->_dataParser->getTables();
  524.             $prop = $properties[$method->distinct];
  525.             $count=' DISTINCT '.$this->_encloseName($tables[$prop->table]['name']) .'.'.$this->_encloseName($prop->fieldName);
  526.         }
  527.         else {
  528.             $count='*';
  529.         }
  530.         $src[] = '    $__query = \'SELECT COUNT('.$count.') as c \'.$this->_fromClause.$this->_whereClause;';
  531.         $glueCondition = ($this->sqlWhereClause !='' ? ' AND ':' WHERE ');
  532.  
  533.         $cond = $method->getConditions();
  534.         if($cond !== null) {
  535.             $sqlCond = $this->buildConditions($cond, $allField, $method->getParameters(), true);
  536.             if(trim($sqlCond) != '')
  537.                 $src[] = '$__query .=\''.$glueCondition.$sqlCond."';";
  538.         }
  539.     }
  540.  
  541.     /**
  542.      *
  543.      */
  544.     protected function buildSelectUserQuery($method, &$src, &$allField) {
  545.         $limit = '';
  546.         if($method->distinct !=''){
  547.             $select = '\''.$this->buildSelectClause($method->distinct).'\'';
  548.         }
  549.         else{
  550.             $select=' $this->_selectClause';
  551.         }
  552.         $src[] = '    $__query = '.$select.'.$this->_fromClause.$this->_whereClause;';
  553.         $glueCondition = ($this->sqlWhereClause !='' ? ' AND ':' WHERE ');
  554.         if( $method->type == 'select' && ($lim = $method->getLimit ()) !==null){
  555.             $limit =', '.$lim['offset'].', '.$lim['count'];
  556.         }
  557.  
  558.         $sqlCond = $this->buildConditions($method->getConditions(), $allField, $method->getParameters(), true, $method->getGroupBy());
  559.  
  560.         if(trim($sqlCond) != '')
  561.             $src[] = '$__query .=\''.$glueCondition.$sqlCond."';";
  562.  
  563.         return $limit;
  564.     }
  565.  
  566.  
  567.     /**
  568.     * create FROM clause and WHERE clause for all SELECT query
  569.     */
  570.     protected function buildFromWhereClause(){
  571.  
  572.         $tables = $this->_dataParser->getTables();
  573.  
  574.         foreach($tables as $table_name => $table){
  575.             $tables[$table_name]['realname'] = '\'.$this->_conn->prefixTable(\''.$table['realname'].'\').\'';
  576.         }
  577.  
  578.         $primarytable = $tables[$this->_dataParser->getPrimaryTable()];
  579.         $ptrealname = $this->_encloseName($primarytable['realname']);
  580.         $ptname = $this->_encloseName($primarytable['name']);
  581.  
  582.         list($sqlFrom, $sqlWhere) = $this->buildOuterJoins($tables, $ptname);
  583.  
  584.         $sqlFrom =$ptrealname.$this->aliasWord.$ptname.$sqlFrom;
  585.  
  586.         foreach($this->_dataParser->getInnerJoins() as $tablejoin){
  587.             $table= $tables[$tablejoin];
  588.             $tablename = $this->_encloseName($table['name']);
  589.             $sqlFrom .=', '.$this->_encloseName($table['realname']).$this->aliasWord.$tablename;
  590.  
  591.             foreach($table['fk'] as $k => $fk){
  592.                 $sqlWhere.=' AND '.$ptname.'.'.$this->_encloseName($fk).'='.$tablename.'.'.$this->_encloseName($table['pk'][$k]);
  593.             }
  594.         }
  595.  
  596.         $this->sqlWhereClause = ($sqlWhere !='' ? ' WHERE '.substr($sqlWhere,4) :'');
  597.         $this->sqlFromClause = ' FROM '.$sqlFrom;
  598.     }
  599.  
  600.     /**
  601.      * generates the part of the FROM clause for outer joins
  602.      * @return array  [0]=> the part of the FROM clause, [1]=> the part to add to the WHERE clause when needed
  603.      */
  604.     protected function buildOuterJoins(&$tables, $primaryTableName){
  605.         $sqlFrom = '';
  606.         foreach($this->_dataParser->getOuterJoins() as $tablejoin){
  607.             $table= $tables[$tablejoin[0]];
  608.             $tablename = $this->_encloseName($table['name']);
  609.  
  610.             $r =$this->_encloseName($table['realname']).$this->aliasWord.$tablename;
  611.  
  612.             $fieldjoin='';
  613.             foreach($table['fk'] as $k => $fk){
  614.                 $fieldjoin.=' AND '.$primaryTableName.'.'.$this->_encloseName($fk).'='.$tablename.'.'.$this->_encloseName($table['pk'][$k]);
  615.             }
  616.             $fieldjoin=substr($fieldjoin,4);
  617.  
  618.             if($tablejoin[1] == 0){
  619.                 $sqlFrom.=' LEFT JOIN '.$r.' ON ('.$fieldjoin.')';
  620.             }elseif($tablejoin[1] == 1){
  621.                 $sqlFrom.=' RIGHT JOIN '.$r.' ON ('.$fieldjoin.')';
  622.             }
  623.         }
  624.         return array($sqlFrom, '');
  625.     }
  626.  
  627.     /**
  628.     * build a SELECT clause for all SELECT queries
  629.     * @return string the select clause.
  630.     */
  631.     protected function buildSelectClause ($distinct=false){
  632.         $result = array();
  633.  
  634.         $tables = $this->_dataParser->getTables();
  635.         foreach ($this->_dataParser->getProperties () as $id=>$prop){
  636.  
  637.             $table = $this->_encloseName($tables[$prop->table]['name']) .'.';
  638.  
  639.             if ($prop->selectPattern !=''){
  640.                 $result[]= $this->buildSelectPattern($prop->selectPattern, $table, $prop->fieldName, $prop->name);
  641.             }
  642.         }
  643.  
  644.         return 'SELECT '.($distinct?'DISTINCT ':'').(implode (', ',$result));
  645.     }
  646.  
  647.     /**
  648.      * build an item for the select clause
  649.     */
  650.     protected function buildSelectPattern ($pattern, $table, $fieldname, $propname ){
  651.         if ($pattern =='%s'){
  652.             $field = $table.$this->_encloseName($fieldname);
  653.             if ($fieldname != $propname){
  654.                 $field .= ' as '.$this->_encloseName($propname);    
  655.             }
  656.         }else{
  657.             $field = str_replace(array("'", "%s"), array("\\'",$table.$this->_encloseName($fieldname)),$pattern).' as '.$this->_encloseName($propname);
  658.         }
  659.         return $field;
  660.     }
  661.  
  662.     protected function buildEndOfClass() {
  663.         return '';
  664.     }
  665.  
  666.     /**
  667.     * format field names with start, end and between strings.
  668.     *   will write the field named info.
  669.     *   eg info == name
  670.     *   echo $field->name
  671.     * @param string   $info    property to get from objects in $using
  672.     * @param string   $start   string to add before the info
  673.     * @param string   $end     string to add after the info
  674.     * @param string   $beetween string to add between each info
  675.     * @param array    $using     list of CopixPropertiesForDAO object. if null, get default fields list
  676.     * @see  jDaoProperty
  677.     */
  678.     protected function _writeFieldsInfoWith ($info, $start = '', $end='', $beetween = '', $using = null){
  679.         $result = array();
  680.         if ($using === null){
  681.             //if no fields are provided, using _dataParser's as default.
  682.             $using = $this->_dataParser->getProperties ();
  683.         }
  684.  
  685.         foreach ($using as $id=>$field){
  686.             $result[] = $start . $field->$info . $end;
  687.         }
  688.  
  689.         return implode ($beetween,$result);
  690.     }
  691.  
  692.     /**
  693.     * format field names with start, end and between strings.
  694.     */
  695.     protected function _writeFieldNamesWith ($start = '', $end='', $beetween = '', $using = null){
  696.         return $this->_writeFieldsInfoWith ('name', $start, $end, $beetween, $using);
  697.     }
  698.  
  699.     protected function _getPrimaryFieldsList() {
  700.         $tables            = $this->_dataParser->getTables();
  701.         $pkFields          = array();
  702.  
  703.         $primTable = $tables[$this->_dataParser->getPrimaryTable()];
  704.         $props  = $this->_dataParser->getProperties();
  705.         // we want to have primary keys as the same order indicated into primarykey attr
  706.         foreach($primTable['pk'] as $pkname) {
  707.             foreach($primTable['fields'] as $f){
  708.                 if ($props[$f]->fieldName == $pkname) {
  709.                     $pkFields[$props[$f]->name] = $props[$f];
  710.                     break;
  711.                 }
  712.             }
  713.         }
  714.         return $pkFields;
  715.     }
  716.  
  717.     /**
  718.     * gets fields that match a condition returned by the $captureMethod
  719.     * @internal
  720.     */
  721.     protected function _getPropertiesBy ($captureMethod){
  722.         $captureMethod = '_capture'.$captureMethod;
  723.         $result = array ();
  724.  
  725.         foreach ($this->_dataParser->getProperties() as $field){
  726.             if ( $this->$captureMethod($field)){
  727.                 $result[$field->name] = $field;
  728.             }
  729.         }
  730.         return $result;
  731.     }
  732.  
  733.     protected function _capturePrimaryFieldsExcludeAutoIncrement(&$field){
  734.         return ($field->table == $this->_dataParser->getPrimaryTable() && !$field->autoIncrement);
  735.     }
  736.  
  737.     protected function _capturePrimaryFieldsExcludePk(&$field){
  738.         return ($field->table == $this->_dataParser->getPrimaryTable()) && !$field->isPK;
  739.     }
  740.  
  741.     protected function _capturePrimaryTable(&$field){
  742.         return ($field->table == $this->_dataParser->getPrimaryTable());
  743.     }
  744.  
  745.     protected function _captureAll(&$field){
  746.         return true;
  747.     }
  748.  
  749.     protected function _captureFieldToUpdate(&$field){
  750.         return ($field->table == $this->_dataParser->getPrimaryTable()
  751.                 && !$field->isPK
  752.                 && !$field->isFK
  753.                 && ( $field->autoIncrement || ($field->insertPattern != '%s' && $field->selectPattern != '')));
  754.     }
  755.  
  756.     protected function _captureFieldToUpdateOnUpdate(&$field){
  757.         return ($field->table == $this->_dataParser->getPrimaryTable()
  758.                 && !$field->isPK
  759.                 && !$field->isFK
  760.                 && ( $field->autoIncrement || ($field->updatePattern != '%s' && $field->selectPattern != '')));
  761.     }
  762.  
  763.     protected function _captureBinaryField(&$field) {
  764.         return ($field->unifiedType == 'binary' || $field->unifiedType == 'varbinary');
  765.     }
  766.  
  767.     /**
  768.     * get autoincrement PK field
  769.     */
  770.     protected function getAutoIncrementPKField ($using = null){
  771.         if ($using === null){
  772.             $using = $this->_dataParser->getProperties ();
  773.         }
  774.  
  775.         $tb = $this->_dataParser->getTables();
  776.         $tb = $tb[$this->_dataParser->getPrimaryTable()]['realname'];
  777.  
  778.         foreach ($using as $id=>$field) {
  779.             if(!$field->isPK)
  780.                 continue;
  781.             if ($field->autoIncrement) {
  782.                 return $field;
  783.             }
  784.         }
  785.         return null;
  786.     }
  787.  
  788.     /**
  789.      * build a WHERE clause with conditions on given properties : conditions are
  790.      * equality between a variable and the field.
  791.      * the variable name is the name of the property, made with an optional prefix
  792.      * given in $fieldPrefix parameter.
  793.      * This method is called to generate WHERE clause for primary keys.
  794.      * @param array $fields  list of jDaoPropery objects
  795.      * @param string $fieldPrefix  an optional prefix to prefix variable names
  796.      * @param boolean $forSelect  if true, the table name or table alias will prefix
  797.      *                             the field name in the query
  798.      * @return string the WHERE clause (without the WHERE keyword)
  799.      * @internal
  800.      */
  801.     protected function buildSimpleConditions (&$fields, $fieldPrefix='', $forSelect=true){
  802.         $r = ' ';
  803.  
  804.         $first = true;
  805.         foreach($fields as $field){
  806.             if (!$first){
  807.                 $r .= ' AND ';
  808.             }else{
  809.                 $first = false;
  810.             }
  811.  
  812.             if($forSelect){
  813.                 $condition = $this->_encloseName($field->table).'.'.$this->_encloseName($field->fieldName);
  814.             }else{
  815.                 $condition = $this->_encloseName($field->fieldName);
  816.             }
  817.  
  818.             $var = '$'.$fieldPrefix.$field->name;
  819.             $value = $this->_preparePHPExpr($var, $field, !$field->requiredInConditions, '=');
  820.  
  821.             $r .= $condition.'\'.'.$value.'.\'';
  822.         }
  823.  
  824.         return $r;
  825.     }
  826.  
  827.  
  828.     protected function _prepareValues ($fieldList, $pattern='', $prefixfield=''){
  829.         $values = $fields = array();
  830.  
  831.         foreach ((array)$fieldList as $fieldName=>$field) {
  832.             if ($pattern != '' && $field->$pattern == '') {
  833.                 continue;
  834.             }
  835.  
  836.             $value = $this->_preparePHPExpr('$'.$prefixfield.$fieldName, $field, true);
  837.  
  838.             if($pattern != ''){
  839.                 if(strpos($field->$pattern, "'") !== false && strpos($field->$pattern, "\\'") === false) {
  840.                     $values[$field->name] = sprintf(str_replace("'", "\\'", $field->$pattern),'\'.'.$value.'.\'');
  841.                 } else {
  842.                     $values[$field->name] = sprintf($field->$pattern,'\'.'.$value.'.\'');
  843.                 }
  844.             }else{
  845.                 $values[$field->name] = '\'.'.$value.'.\'';
  846.             }
  847.  
  848.             $fields[$field->name] = $this->_encloseName($field->fieldName);
  849.         }
  850.         return array($fields, $values);
  851.     }
  852.  
  853.  
  854.     /**
  855.      * build 'where' clause from conditions declared with condition tag in a user method
  856.      * @param jDaoConditions $cond the condition object which contains conditions data
  857.      * @param array $fields  array of jDaoProperty
  858.      * @param array $params  list of parameters name of the method
  859.      * @param boolean $withPrefix true if the field name should be preceded by the table name/table alias
  860.      * @param array $groupby  list of properties to use in a groupby
  861.      * @return string a WHERE clause (without the WHERE keyword) with eventually an ORDER clause
  862.      * @internal
  863.      */
  864.     protected function buildConditions ($cond, $fields, $params=array(), $withPrefix=true, $groupby=null){
  865.         if($cond)
  866.             $sql = $this->buildOneSQLCondition ($cond->condition, $fields, $params, $withPrefix, true);
  867.         else
  868.             $sql = '';
  869.  
  870.         if($groupby && count($groupby)) {
  871.             if(trim($sql) =='') {
  872.                 $sql = ' 1=1 ';
  873.             }
  874.             foreach($groupby as $k=>$f) {
  875.                 if ($withPrefix)
  876.                     $groupby[$k]= $this->_encloseName($fields[$f]->table).'.'.$this->_encloseName($fields[$f]->fieldName);
  877.                 else
  878.                     $groupby[$k]= $this->_encloseName($fields[$f]->fieldName);
  879.             }
  880.             $sql .= ' GROUP BY '.implode (', ', $groupby);
  881.         }
  882.  
  883.         $order = array ();
  884.         foreach ($cond->order as $name => $way){
  885.             $ord='';
  886.             if (isset($fields[$name])){
  887.                 if ($withPrefix)
  888.                     $ord = $this->_encloseName($fields[$name]->table).'.'.$this->_encloseName($fields[$name]->fieldName);
  889.                 else
  890.                     $ord = $this->_encloseName($fields[$name]->fieldName);
  891.             }elseif($name[0] == '$'){
  892.                 $ord = '\'.'.$name.'.\'';
  893.             }else{
  894.                 continue;
  895.             }
  896.             if($way[0] == '$'){
  897.                 $order[]=$ord.' \'.( strtolower('.$way.') ==\'asc\'?\'asc\':\'desc\').\'';
  898.             }else{
  899.                 $order[]=$ord.' '.$way;
  900.             }
  901.         }
  902.         if(count ($order) > 0){
  903.             if(trim($sql) =='') {
  904.                 $sql = ' 1=1 ';
  905.             }
  906.             $sql.=' ORDER BY '.implode (', ', $order);
  907.         }
  908.         return $sql;
  909.     }
  910.  
  911.     /**
  912.      * build a condition for the SQL WHERE clause.
  913.      * this method call itself recursively.
  914.      * @param jDaoCondition $cond a condition object which contains conditions data
  915.      * @param array $fields  array of jDaoProperty
  916.      * @param array $params  list of parameters name of the method
  917.      * @param boolean $withPrefix true if the field name should be preceded by the table name/table alias
  918.      * @param boolean $principal  should be true for the first call, and false for recursive call
  919.      * @return string a WHERE clause (without the WHERE keyword)
  920.      * @see jDaoGenerator::buildConditions
  921.      * @internal
  922.      */
  923.     protected function buildOneSQLCondition ($condition, $fields, $params, $withPrefix, $principal=false){
  924.  
  925.         $r = ' ';
  926.  
  927.         //direct conditions for the group
  928.         $first = true;
  929.         foreach ($condition->conditions as $cond){
  930.             if (!$first){
  931.                 $r .= ' '.$condition->glueOp.' ';
  932.             }
  933.             $first = false;
  934.  
  935.             $prop = $fields[$cond['field_id']];
  936.  
  937.             if($withPrefix){
  938.                 $f = $this->_encloseName($prop->table).'.'.$this->_encloseName($prop->fieldName);
  939.             }else{
  940.                 $f = $this->_encloseName($prop->fieldName);
  941.             }
  942.  
  943.             $r .= $f.' ';
  944.  
  945.             if($cond['operator'] == 'IN' || $cond['operator'] == 'NOT IN'){
  946.                 if($cond['isExpr']){
  947.                     $phpexpr = $this->_preparePHPCallbackExpr($prop);
  948.                     $phpvalue = 'implode(\',\', array_map( '.$phpexpr.', '.$cond['value'].'))';
  949.                     $value= '(\'.'.$phpvalue.'.\')';
  950.                 }else{
  951.                     $value= '('.str_replace("'", "\\'", $cond['value']).')';
  952.                 }
  953.                 $r.=$cond['operator'].' '.$value;
  954.             }elseif($cond['operator'] == 'IS NULL' || $cond['operator'] == 'IS NOT NULL'){
  955.                 $r.=$cond['operator'].' ';
  956.             }else{
  957.                 if($cond['isExpr']){
  958.                     $value=str_replace("'","\\'",$cond['value']);
  959.                     // we need to know if the expression is like "$foo" (1) or a thing like "concat($foo,'bla')" (2)
  960.                     // because of the nullability of the parameter. If the value of the parameter is null and the operator
  961.                     // is = or <>, then we need to generate a thing like :
  962.                     // - in case 1: ($foo === null ? 'IS NULL' : '='.$this->_conn->quote($foo))
  963.                     // - in case 2: '= concat('.($foo === null ? 'NULL' : $this->_conn->quote($foo)).' ,\'bla\')'
  964.                     if($value[0] == '$'){
  965.                         $value = '\'.'.$this->_preparePHPExpr($value, $prop, !$prop->requiredInConditions,$cond['operator']).'.\'';
  966.                     }else{
  967.                         foreach($params as $param){
  968.                             $value = str_replace('$'.$param, '\'.'.$this->_preparePHPExpr('$'.$param, $prop, !$prop->requiredInConditions).'.\'',$value);
  969.                         }
  970.                         $value = $cond['operator'].' '.$value;
  971.                     }
  972.                 } else {
  973.                     $value = $cond['operator'].' ';
  974.                     if ($cond['operator'] == 'LIKE' || $cond['operator'] == 'NOT LIKE') {
  975.                         $value .= $this->tools->escapeValue('varchar', $cond['value'], false, true);
  976.                     } else {
  977.                         $value .= $this->tools->escapeValue($prop->unifiedType, $cond['value'], false, true);
  978.                     }
  979.                 }
  980.                 $r.=$value;
  981.             }
  982.         }
  983.         //sub conditions
  984.         foreach ($condition->group as $conditionDetail){
  985.             if (!$first){
  986.                 $r .= ' '.$condition->glueOp.' ';
  987.             }
  988.             $r .= $this->buildOneSQLCondition($conditionDetail, $fields, $params, $withPrefix);
  989.             $first=false;
  990.         }
  991.  
  992.         //adds parenthesis around the sql if needed (non empty)
  993.         if (strlen (trim ($r)) > 0 && (!$principal ||($principal && $condition->glueOp != 'AND'))){
  994.             $r = '('.$r.')';
  995.         }
  996.         return $r;
  997.     }
  998.  
  999.     protected function _preparePHPExpr($expr, $field, $checknull=true, $forCondition=''){
  1000.         $opnull = $opval = '';
  1001.         if($checknull && $forCondition != ''){
  1002.             if($forCondition == '=')
  1003.                 $opnull = 'IS ';
  1004.             elseif($forCondition == '<>')
  1005.                 $opnull = 'IS NOT ';
  1006.             else
  1007.                 $checknull=false;
  1008.         }
  1009.         $type = '';
  1010.         if ($forCondition != 'LIKE' && $forCondition != 'NOT LIKE')
  1011.             $type = strtolower($field->unifiedType);
  1012.  
  1013.         if ($forCondition != '') {
  1014.             $forCondition = '\' '.$forCondition.' \'.'; // spaces for operators like LIKE
  1015.         }
  1016.  
  1017.         switch($type){
  1018.             case 'integer':
  1019.                 if($checknull){
  1020.                     $expr= '('.$expr.' === null ? \''.$opnull.'NULL\' : '.$forCondition.'intval('.$expr.'))';
  1021.                 }else{
  1022.                     $expr= $forCondition.'intval('.$expr.')';
  1023.                 }
  1024.                 break;
  1025.             case 'double':
  1026.             case 'float':
  1027.             case 'numeric':
  1028.             case 'decimal':
  1029.                 if($checknull){
  1030.                     $expr='('.$expr.' === null ? \''.$opnull.'NULL\' : '.$forCondition.'jDb::floatToStr('.$expr.'))';
  1031.                 }else{
  1032.                     $expr=$forCondition.'jDb::floatToStr('.$expr.')';
  1033.                 }
  1034.                 break;
  1035.             case 'boolean':
  1036.                 if($checknull){
  1037.                     $expr= '('.$expr.' === null ? \''.$opnull.'NULL\' : '.$forCondition.'$this->_prepareValue('.$expr.', "boolean", true))';
  1038.                 }else{
  1039.                     $expr= $forCondition.'$this->_prepareValue('.$expr.', "boolean", true)';
  1040.                 }
  1041.                 break;
  1042.             default:
  1043.                 if ($type=='varbinary' || $type=='binary')
  1044.                     $qparam = ',true';
  1045.                 else
  1046.                     $qparam = '';
  1047.  
  1048.                 if ($checknull) {
  1049.                    $expr = '('.$expr.' === null ? \''.$opnull.'NULL\' : '.$forCondition.'$this->_conn->quote2('.$expr.',false'.$qparam.'))';
  1050.                 }
  1051.                 else {
  1052.                    $expr = $forCondition.'$this->_conn->quote'.($qparam?'2('.$expr.',true,true)':'('.$expr.')');
  1053.                 }
  1054.         }
  1055.         return $expr;
  1056.     }
  1057.  
  1058.     protected function _preparePHPCallbackExpr($field){
  1059.         $type = strtolower($field->unifiedType);
  1060.         // TODO PHP53: generate a closure instead of create_function
  1061.         switch($type){
  1062.             case 'integer':
  1063.                 return 'create_function(\'$__e\',\'return intval($__e);\')';
  1064.             case 'double':
  1065.             case 'float':
  1066.             case 'numeric':
  1067.             case 'decimal':
  1068.                 return 'create_function(\'$__e\',\'return jDb::floatToStr($__e);\')';
  1069.             case 'boolean':
  1070.                 return 'array($this, \'_callbackBool\')';
  1071.             default:
  1072.                 if ($type=='varbinary' || $type=='binary')
  1073.                     return 'array($this, \'_callbackQuoteBin\')';
  1074.                 else
  1075.                     return 'array($this, \'_callbackQuote\')';
  1076.         }
  1077.     }
  1078.  
  1079.     protected function _encloseName($name){
  1080.         return $this->tools->encloseName($name);
  1081.     }
  1082.  
  1083.     protected function buildUpdateAutoIncrementPK($pkai) {
  1084.         return '       $record->'.$pkai->name.'= $this->_conn->lastInsertId();';
  1085.     }
  1086. }

Documentation generated on Wed, 24 Sep 2014 21:57:19 +0200 by phpDocumentor 1.4.3