Example #1
0
 /**
  * Treats a <data type="sql:count"> node and its children:
  * Count of related records in other table.
  *
  * @access private
  * @param  SimpleXmlElement  $data
  * @return string
  */
 protected function process_sql_count($data)
 {
     if ($data->getElementByPath('joinkeys')) {
         return $this->process_sql_count_join($data);
     }
     $cnt_name = $data->attributes('name');
     $cnt_table = $data->attributes('table');
     $cnt_key = $data->attributes('key');
     $cnt_val = $data->attributes('value');
     $cnt_valtype = $data->attributes('valuetype');
     $cnt_distinct = $data->attributes('distinct');
     if (!$cnt_table) {
         if ($cnt_distinct) {
             $formula = 'COUNT( DISTINCT ' . $this->_currentTableAs . '.`' . $cnt_distinct . '` )';
         } else {
             $formula = 'COUNT(*)';
         }
         if ($cnt_name) {
             $formula .= ' AS `' . $cnt_name . '`';
         }
         if ($cnt_key && $cnt_val && $cnt_valtype) {
             $this->addWhere($cnt_key, '=', $cnt_val, $cnt_valtype);
         }
     } elseif ($cnt_table == $this->_table || !$this->_table) {
         if ($cnt_distinct) {
             $formula = 'COUNT( DISTINCT ' . $this->_currentTableAs . '.`' . $cnt_distinct . '` )';
         } else {
             $formula = 'COUNT(*)';
         }
         if ($cnt_name) {
             $formula .= ' AS `' . $cnt_name . '`';
         }
         if ($cnt_table && !$this->_table) {
             $this->_table = $cnt_table;
         }
         // collect implicit HAVING statement with <field name="xxx" ... key="id" value="1 valuetype="const:int"> :
         if (!$cnt_valtype) {
             $cnt_valtype = 'sql:parentfield';
         }
         if ($cnt_key && $cnt_val) {
             $this->addWhere($cnt_key, '=', $cnt_val, $cnt_valtype);
         }
     } else {
         //		return $this->process_subquery( $data, false );
         $subqueryTable = $data->attributes('table');
         $subqueryName = $data->attributes('name');
         $this->_levelPush();
         $this->incrementTableAs();
         $xmlsql = new self($this->_db, $subqueryTable, $this->_pluginParams);
         $xmlsql->syncSubQueryTablesIndexes($this);
         $xmlsql->maintableAs = $this->tableAs;
         $xmlsql->process_data($data);
         $formula = '( ' . $xmlsql->_buildSQLquery() . ' )' . ($subqueryName ? ' AS ' . $this->_db->NameQuote($subqueryName) : '');
         $this->_levelPop();
     }
     return $formula;
 }