/**
     * @param $query
     * @return Tx_PtExtlist_Domain_DataBackend_DataSource_Typo3DataSource
     * @throws Exception
     */
    public function executeQuery($query)
    {
        try {
            $this->startTimeMeasure();
            $this->resource = $this->connection->sql_query($query);
            Tx_PtExtbase_Assertions_Assert::isMySQLRessource($this->resource, $this->dbObj);
            $this->stopTimeMeasure();
        } catch (Exception $e) {
            throw new Exception('Error while retrieving data from database using typo3 db object.<br> 
							     Error: ' . $e->getMessage() . ' sql_error says: ' . $this->connection->sql_error() . ' 1280400023<br><br>
							     SQL QUERY: <br>
							     </strong><hr>' . nl2br($query) . '<hr><strong>', 1280400023);
        }
        return $this;
    }
 /**
  * @test
  */
 public function importingExtension()
 {
     $this->importExtensions(array('extbase'));
     /** @var $res mysqli_result|resource */
     $res = $this->db->sql_query('show tables');
     $rows = $this->db->sql_num_rows($res);
     $this->assertNotSame(0, $rows);
 }
 /**
  * Executes a query
  * EXPERIMENTAL - This method will make its best to handle the query correctly
  * but if it cannot, it will simply pass the query to DEFAULT handler.
  *
  * You should use exec_* function from this class instead!
  * If you don't, anything that does not use the _DEFAULT handler will probably break!
  *
  * This method was deprecated in TYPO3 4.1 but is considered experimental since TYPO3 4.4
  * as it tries to handle the query correctly anyway.
  *
  * @param	string		Query to execute
  * @return	pointer		Result pointer / DBAL object
  */
 public function sql_query($query)
 {
     $globalConfig = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['dbal']);
     if ($globalConfig['sql_query.passthrough']) {
         return parent::sql_query($query);
     }
     // This method is heavily used by Extbase, try to handle it with DBAL-native methods
     $queryParts = $this->SQLparser->parseSQL($query);
     if (is_array($queryParts) && t3lib_div::inList('SELECT,UPDATE,INSERT,DELETE', $queryParts['type'])) {
         return $this->exec_query($queryParts);
     }
     switch ($this->handlerCfg['_DEFAULT']['type']) {
         case 'native':
             $sqlResult = mysql_query($query, $this->handlerInstance['_DEFAULT']['link']);
             break;
         case 'adodb':
             $sqlResult = $this->handlerInstance['_DEFAULT']->Execute($query);
             $sqlResult->TYPO3_DBAL_handlerType = 'adodb';
             break;
         case 'userdefined':
             $sqlResult = $this->handlerInstance['_DEFAULT']->sql_query($query);
             $sqlResult->TYPO3_DBAL_handlerType = 'userdefined';
             break;
     }
     if ($this->printErrors && $this->sql_error()) {
         debug(array($this->lastQuery, $this->sql_error()));
     }
     return $sqlResult;
 }
    /**
     * Get tree data
     *
     * @param integer $uid
     * @param string $subLevelId
     *
     * @return array
     */
    protected function getTreeData($uid, $subLevelId)
    {
        // Filter the results by preference and access
        $clauseExludePidList = '';
        if ($pidList = $GLOBALS['BE_USER']->getTSConfigVal('options.hideRecords.pages')) {
            if ($pidList = $this->databaseHandle->cleanIntList($pidList)) {
                $clauseExludePidList = ' AND pages.uid NOT IN (' . $pidList . ')';
            }
        }
        $clause = ' AND ' . $GLOBALS['BE_USER']->getPagePermsClause(1) . ' ' . $clauseExludePidList;
        /**
         * We want a page tree with all the excluded pages in there. This means
         * all pages that have the exclude flag set and also all pages that have the
         * excludeBranch flag set, including their children.
         *
         * 1). First fetch the page id's that have any exclusion options set
         */
        $result = $this->databaseHandle->sql_query('
			SELECT GROUP_CONCAT(uid) AS uids
			FROM pages
			WHERE
				tx_tablecleaner_exclude = 1 AND
				deleted = 0 ' . $clause . ';
		');
        $row = $this->databaseHandle->sql_fetch_assoc($result);
        $excludePages = array();
        if ($row['uids'] !== NULL) {
            $excludePages = explode(',', $row['uids']);
        }
        $this->databaseHandle->sql_free_result($result);
        $result = $this->databaseHandle->sql_query('
			SELECT GROUP_CONCAT(uid) AS uids
			FROM pages
			WHERE
				tx_tablecleaner_exclude_branch = 1 AND
				deleted = 0 ' . $clause . ';
		');
        $row = $this->databaseHandle->sql_fetch_assoc($result);
        $excludeBranchPages = array();
        if ($row['uids'] !== NULL) {
            $excludeBranchPages = explode(',', $row['uids']);
        }
        $this->databaseHandle->sql_free_result($result);
        /**
         * 2). Fetch the id's up to the 'current root' page.
         * To build a complete page tree, we also need the parents of the
         * excluded pages. So we merge the found pages and fetch the rootlines for
         * all those pages.
         */
        $allExcludedPages = array_merge($excludePages, $excludeBranchPages);
        $allExcludedPages = array_unique($allExcludedPages);
        $allUids = array();
        foreach ($allExcludedPages as $pageId) {
            // Don't fetch the rootline if the pageId is already in the list
            if (!in_array($pageId, $allUids)) {
                // Get the rootline up to the starting uid
                $rootLine = t3lib_BEfunc::BEgetRootLine($pageId, ' AND NOT uid = ' . $uid . $clause);
                foreach ($rootLine as $record) {
                    $allUids[] = $record['uid'];
                }
            }
        }
        /**
         * 3). Include self
         */
        $allUids[] = $uid;
        /**
         * 4). Fetch all the children of the pages that have exclude_branch set.
         */
        foreach ($excludeBranchPages as $pageId) {
            $allUids = array_merge($allUids, Tx_Tablecleaner_Utility_Base::fetchChildPages($pageId));
        }
        $allUids = array_unique($allUids);
        $foundPages = $this->pageRepository->findByUids($allUids);
        $allPages = array();
        foreach ($foundPages as $page) {
            $allPages[$page['uid']] = $page;
        }
        $tree = $this->reassembleTree($allPages, $uid, $subLevelId);
        $rootElement[$uid] = $allPages[$uid];
        $rootElement[$uid][$subLevelId] = $tree;
        return $rootElement;
    }