Piwik\DataAccess\LogQueryBuilder\JoinGenerator::generate PHP Method

generate() public method

Generate the join sql based on the needed tables
public generate ( ) : array
return array
    public function generate()
    {
        /** @var LogTable[] $availableLogTables */
        $availableLogTables = array();
        $this->tables->sort(array($this, 'sortTablesForJoin'));
        foreach ($this->tables as $i => $table) {
            if (is_array($table)) {
                // join condition provided
                $alias = isset($table['tableAlias']) ? $table['tableAlias'] : $table['table'];
                $this->joinString .= " LEFT JOIN " . Common::prefixTable($table['table']) . " AS " . $alias . " ON " . $table['joinOn'];
                continue;
            }
            $tableSql = Common::prefixTable($table) . " AS {$table}";
            $logTable = $this->tables->getLogTable($table);
            if ($i == 0) {
                // first table
                $this->joinString .= $tableSql;
            } else {
                $join = $this->findJoinCriteriasForTables($logTable, $availableLogTables);
                if ($join === null) {
                    $availableLogTables[$table] = $logTable;
                    continue;
                }
                // the join sql the default way
                $this->joinString .= " LEFT JOIN {$tableSql} ON " . $join;
            }
            $availableLogTables[$table] = $logTable;
        }
    }

Usage Example

Example #1
0
 public function getSelectQueryString(SegmentExpression $segmentExpression, $select, $from, $where, $bind, $groupBy, $orderBy, $limitAndOffset)
 {
     if (!is_array($from)) {
         $from = array($from);
     }
     $fromInitially = $from;
     if (!$segmentExpression->isEmpty()) {
         $segmentExpression->parseSubExpressionsIntoSqlExpressions($from);
         $segmentSql = $segmentExpression->getSql();
         $where = $this->getWhereMatchBoth($where, $segmentSql['where']);
         $bind = array_merge($bind, $segmentSql['bind']);
     }
     $tables = new JoinTables($this->logTableProvider, $from);
     $join = new JoinGenerator($tables);
     $join->generate();
     $from = $join->getJoinString();
     $joinWithSubSelect = $join->shouldJoinWithSelect();
     // hack for https://github.com/piwik/piwik/issues/9194#issuecomment-164321612
     $useSpecialConversionGroupBy = !empty($segmentSql) && strpos($groupBy, 'log_conversion.idgoal') !== false && $fromInitially == array('log_conversion') && strpos($from, 'log_link_visit_action') !== false;
     if ($useSpecialConversionGroupBy) {
         $innerGroupBy = "CONCAT(log_conversion.idvisit, '_' , log_conversion.idgoal, '_', log_conversion.buster)";
         $sql = $this->buildWrappedSelectQuery($select, $from, $where, $groupBy, $orderBy, $limitAndOffset, $innerGroupBy);
     } elseif ($joinWithSubSelect) {
         $sql = $this->buildWrappedSelectQuery($select, $from, $where, $groupBy, $orderBy, $limitAndOffset);
     } else {
         $sql = $this->buildSelectQuery($select, $from, $where, $groupBy, $orderBy, $limitAndOffset);
     }
     return array('sql' => $sql, 'bind' => $bind);
 }