Fixed bug #6032: Freeing SQL result sets
authorMartin Kutschker <martin.t.kutschker@blackbox.net>
Sat, 6 Oct 2007 21:38:32 +0000 (21:38 +0000)
committerMartin Kutschker <martin.t.kutschker@blackbox.net>
Sat, 6 Oct 2007 21:38:32 +0000 (21:38 +0000)
git-svn-id: https://svn.typo3.org/TYPO3v4/Core/trunk@2533 709f56b5-9817-0410-a4d7-c38de5d9e867

ChangeLog
t3lib/class.t3lib_page.php
typo3/sysext/cms/tslib/class.tslib_content.php

index 26b290b..31da64e 100755 (executable)
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,7 @@
 2007-10-06  Martin Kutschker  <martin.t.kutschker@blackbox.net>
 
        * Fixed bug #6462: Wrong default locale charset on Windows
+       * Fixed bug #6032: Freeing SQL result sets
 
 2007-10-01  Bernhard Kraft  <kraftb@kraftb.at>
 
index 9d98b99..1e4d01e 100755 (executable)
@@ -2,7 +2,7 @@
 /***************************************************************
 *  Copyright notice
 *
-*  (c) 1999-2006 Kasper Skaarhoj (kasperYYYY@typo3.com)
+*  (c) 1999-2007 Kasper Skaarhoj (kasperYYYY@typo3.com)
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
@@ -185,9 +185,11 @@ class t3lib_pageSelect {
                $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'pages', 'uid='.intval($uid).$this->where_hid_del.($disableGroupAccessCheck ? '' : $this->where_groupAccess));
                $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
                $GLOBALS['TYPO3_DB']->sql_free_result($res);
-               if ($row) {
+               if ($row)       {
                        $this->versionOL('pages',$row);
-                       if (is_array($row))             return $this->getPageOverlay($row);
+                       if (is_array($row))     {
+                               return $this->getPageOverlay($row);
+                       }
                }
                return Array();
        }
@@ -201,9 +203,13 @@ class t3lib_pageSelect {
         */
        function getPage_noCheck($uid)  {
                $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'pages', 'uid='.intval($uid).$this->deleteClause('pages'));
-               if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+               $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+               $GLOBALS['TYPO3_DB']->sql_free_result($res);
+               if ($row)       {
                        $this->versionOL('pages',$row);
-                       if (is_array($row))             return $this->getPageOverlay($row);
+                       if (is_array($row))     {
+                               return $this->getPageOverlay($row);
+                       }
                }
                return Array();
        }
@@ -218,9 +224,13 @@ class t3lib_pageSelect {
        function getFirstWebPage($uid)  {
                $output = '';
                $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'pages', 'pid='.intval($uid).$this->where_hid_del.$this->where_groupAccess, '', 'sorting', '1');
-               if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+               $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+               $GLOBALS['TYPO3_DB']->sql_free_result($res);
+               if ($row)       {
                        $this->versionOL('pages',$row);
-                       if (is_array($row))             $output = $this->getPageOverlay($row);
+                       if (is_array($row))     {
+                               $output = $this->getPageOverlay($row);
+                       }
                }
                $GLOBALS['TYPO3_DB']->sql_free_result($res);
                return $output;
@@ -236,7 +246,9 @@ class t3lib_pageSelect {
        function getPageIdFromAlias($alias)     {
                $alias = strtolower($alias);
                $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', 'pages', 'alias='.$GLOBALS['TYPO3_DB']->fullQuoteStr($alias, 'pages').' AND pid>=0 AND pages.deleted=0');  // "AND pid>=0" because of versioning (means that aliases sent MUST be online!)
-               if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+               $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+               $GLOBALS['TYPO3_DB']->sql_free_result($res);
+               if ($row)       {
                        return $row['uid'];
                }
                return 0;
@@ -284,6 +296,7 @@ class t3lib_pageSelect {
                                                        '1'
                                                );
                                $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                               $GLOBALS['TYPO3_DB']->sql_free_result($res);
                                $this->versionOL('pages_language_overlay',$row);
 
                                if (is_array($row))     {
@@ -339,9 +352,9 @@ class t3lib_pageSelect {
                                                                '1'
                                                        );
                                                        $olrow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                                                       $GLOBALS['TYPO3_DB']->sql_free_result($res);
                                                        $this->versionOL($table,$olrow);
-       #debug($row);
-       #debug($olrow);
+
                                                                // Merge record content by traversing all fields:
                                                        if (is_array($olrow))   {
                                                                foreach($row as $fN => $fV)     {
@@ -456,6 +469,7 @@ class t3lib_pageSelect {
                                }
                        }
                }
+               $GLOBALS['TYPO3_DB']->sql_free_result($res);
                return $output;
        }
 
@@ -489,7 +503,9 @@ class t3lib_pageSelect {
                                        '',
                                        1
                                );
-               if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+               $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+               $GLOBALS['TYPO3_DB']->sql_free_result($res);
+               if ($row)       {
                        if ($row['redirectTo']) {
                                $rURL = $row['redirectTo'];
                                if ($row['prepend_params'])     {
@@ -540,7 +556,9 @@ class t3lib_pageSelect {
 
                while ($uid!=0 && $loopCheck<20)        {       // Max 20 levels in the page tree.
                        $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($selFields, 'pages', 'uid='.intval($uid).' AND pages.deleted=0 AND pages.doktype!=255');
-                       if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+                       $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                       $GLOBALS['TYPO3_DB']->sql_free_result($res);
+                       if ($row)       {
                                $this->versionOL('pages',$row);
                                $this->fixVersioningPid('pages',$row);
 
@@ -563,6 +581,7 @@ class t3lib_pageSelect {
                                                        array_pop($MPA);
                                                        $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($selFields, 'pages', 'uid='.intval($curMP[1]).' AND pages.deleted=0 AND pages.doktype!=255');
                                                        $mp_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                                                       $GLOBALS['TYPO3_DB']->sql_free_result($res);
 
                                                        $this->versionOL('pages',$mp_row);
                                                        $this->fixVersioningPid('pages',$mp_row);
@@ -690,11 +709,14 @@ class t3lib_pageSelect {
                        if (!is_array($pageRec))        {
                                $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,pid,doktype,mount_pid,mount_pid_ol,t3ver_state', 'pages', 'uid='.intval($pageId).' AND pages.deleted=0 AND pages.doktype!=255');
                                $pageRec = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                               $GLOBALS['TYPO3_DB']->sql_free_result($res);
                                $this->versionOL('pages',$pageRec);             // Only look for version overlay if page record is not supplied; This assumes that the input record is overlaid with preview version, if any!
                        }
 
                                // Set first Page uid:
-                       if (!$firstPageUid)     $firstPageUid = $pageRec['uid'];
+                       if (!$firstPageUid)     {
+                               $firstPageUid = $pageRec['uid'];
+                       }
 
                                // Look for mount pid value plus other required circumstances:
                        $mount_pid = intval($pageRec['mount_pid']);
@@ -702,13 +724,14 @@ class t3lib_pageSelect {
 
                                        // Get the mount point record (to verify its general existence):
                                $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,pid,doktype,mount_pid,mount_pid_ol,t3ver_state', 'pages', 'uid='.$mount_pid.' AND pages.deleted=0 AND pages.doktype!=255');
-                               $mount_rec = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
-                               $this->versionOL('pages',$mount_rec);
+                               $mountRec = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                               $GLOBALS['TYPO3_DB']->sql_free_result($res);
+                               $this->versionOL('pages',$mountRec);
 
-                               if (is_array($mount_rec))       {
+                               if (is_array($mountRec))        {
                                                // Look for recursive mount point:
                                        $prevMountPids[] = $mount_pid;
-                                       $recursiveMountPid = $this->getMountPointInfo($mount_pid, $mount_rec, $prevMountPids, $firstPageUid);
+                                       $recursiveMountPid = $this->getMountPointInfo($mount_pid, $mountRec, $prevMountPids, $firstPageUid);
 
                                                // Return mount point information:
                                        return $recursiveMountPid ?
@@ -718,7 +741,7 @@ class t3lib_pageSelect {
                                                                        'overlay' => $pageRec['mount_pid_ol'],
                                                                        'MPvar' => $mount_pid.'-'.$firstPageUid,
                                                                        'mount_point_rec' => $pageRec,
-                                                                       'mount_pid_rec' => $mount_rec,
+                                                                       'mount_pid_rec' => $mountRec,
                                                                );
                                } else {
                                        return -1;      // Means, there SHOULD have been a mount point, but there was none!
@@ -765,14 +788,18 @@ class t3lib_pageSelect {
                $uid = intval($uid);
                if (is_array($TCA[$table]))     {
                        $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $table, 'uid='.intval($uid).$this->enableFields($table));
-                       if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+                       $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                       $GLOBALS['TYPO3_DB']->sql_free_result($res);
+                       if ($row)       {
                                $this->versionOL($table,$row);
                                $GLOBALS['TYPO3_DB']->sql_free_result($res);
 
                                if (is_array($row))     {
                                        if ($checkPage) {
                                                $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', 'pages', 'uid='.intval($row['pid']).$this->enableFields('pages'));
-                                               if ($GLOBALS['TYPO3_DB']->sql_num_rows($res))   {
+                                               $numRows = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
+                                               $GLOBALS['TYPO3_DB']->sql_free_result($res);
+                                               if ($numRows>0) {
                                                        return $row;
                                                } else {
                                                        return 0;
@@ -806,7 +833,9 @@ class t3lib_pageSelect {
                                if (!$noWSOL)   {
                                        $this->versionOL($table,$row);
                                }
-                               if (is_array($row))     return $row;
+                               if (is_array($row))     {
+                                       return $row;
+                               }
                        }
                }
        }
@@ -821,7 +850,7 @@ class t3lib_pageSelect {
         * @param       string          Optional GROUP BY field(s), if none, supply blank string.
         * @param       string          Optional ORDER BY field(s), if none, supply blank string.
         * @param       string          Optional LIMIT value ([begin,]max), if none, supply blank string.
-        * @return      mixed           Returns array (the record) if found, otherwise blank/0 (zero)
+        * @return      mixed           Returns array (the record) if found, otherwise nothing (void)
         */
        function getRecordsByField($theTable,$theField,$theValue,$whereClause='',$groupBy='',$orderBy='',$limit='')     {
                global $TCA;
@@ -842,7 +871,9 @@ class t3lib_pageSelect {
                                if (is_array($row)) $rows[] = $row;
                        }
                        $GLOBALS['TYPO3_DB']->sql_free_result($res);
-                       if (count($rows))       return $rows;
+                       if (count($rows))       {
+                               return $rows;
+                       }
                }
        }
 
@@ -882,8 +913,9 @@ class t3lib_pageSelect {
                        $whereAdd = ' AND tstamp > '.(time()-$expTime);
                }
                $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('content', 'cache_hash', 'hash='.$GLOBALS['TYPO3_DB']->fullQuoteStr($hash, 'cache_hash').$whereAdd);
-               if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
-                       $GLOBALS['TYPO3_DB']->sql_free_result($res);
+               $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+               $GLOBALS['TYPO3_DB']->sql_free_result($res);
+               if ($row)       {
                        return $row['content'];
                }
        }
@@ -1178,7 +1210,9 @@ class t3lib_pageSelect {
                                // Find pointed-to record.
                        if ($moveID)    {
                                $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(implode(',',array_keys($row)), $table, 'uid='.intval($moveID).$this->enableFields($table));
-                               if ($origRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))     {
+                               $origRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                               $GLOBALS['TYPO3_DB']->sql_free_result($res);
+                               if ($origRow)   {
                                        $row = $origRow;
                                        return TRUE;
                                }
@@ -1211,10 +1245,12 @@ class t3lib_pageSelect {
                                         t3ver_state=3 AND
                                         t3ver_move_id='.intval($uid).' AND
                                         t3ver_wsid='.intval($workspace).
-                                               $this->deleteClause($table)
+                                       $this->deleteClause($table)
                                );
 
-                               if (is_array($rows[0])) return $rows[0];
+                               if (is_array($rows[0])) {
+                                       return $rows[0];
+                               }
                        }
                }
                return FALSE;
index 426fe42..f0b2bfb 100755 (executable)
@@ -1219,7 +1219,7 @@ class tslib_cObj {
                                                                $cObj->start($row,$conf['table']);
                                                                $tmpValue = $cObj->cObjGetSingle($renderObjName, $renderObjConf, $renderObjKey);
                                                                $cobjValue .= $tmpValue;
-                                                       }# else debug($GLOBALS['TSFE']->recordRegister,'CONTENT');
+                                                       }
                                                }
                                        }
                                        $GLOBALS['TYPO3_DB']->sql_free_result($res);
@@ -3299,13 +3299,14 @@ class tslib_cObj {
         * Implements the stdWrap "numRows" property
         *
         * @param       array           TypoScript properties for the property (see link to "numRows")
-        * @return      integer         The number of rows found by the select
+        * @return      integer         The number of rows found by the select (FALSE on error)
         * @access private
         * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=317&cHash=e28e53e634
         * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=318&cHash=a98cb4e7e6
         * @see stdWrap()
         */
        function numRows($conf) {
+               $result = FALSE;
                $conf['select.']['selectFields'] = 'count(*)';
 
                $res = $this->exec_getQuery($conf['table'],$conf['select.']);
@@ -3314,8 +3315,10 @@ class tslib_cObj {
                        $GLOBALS['TT']->setTSlogMessage($error,3);
                } else {
                        $row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
-                       return intval($row[0]);
+                       $result = intval($row[0]);
                }
+               $GLOBALS['TYPO3_DB']->sql_free_result($res);
+               return $result;
        }
 
        /**
@@ -5232,8 +5235,10 @@ class tslib_cObj {
                                                        }
                                                        if (!$tCR_flag) {
                                                                foreach ($tCR_rootline as $tCR_data)    {
-                                                                       $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'sys_domain', 'pid='.intval($tCR_data['uid']).' AND redirectTo=\'\''.$this->enableFields('sys_domain'), '', 'sorting');
-                                                                       if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+                                                                       $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', 'sys_domain', 'pid='.intval($tCR_data['uid']).' AND redirectTo=\'\''.$this->enableFields('sys_domain'), '', 'sorting');
+                                                                       $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                                                                       $GLOBALS['TYPO3_DB']->sql_free_result($res);
+                                                                       if ($row)       {
                                                                                $tCR_domain = preg_replace('/\/$/','',$row['domainName']);
                                                                                break;
                                                                        }
@@ -6402,6 +6407,7 @@ class tslib_cObj {
                                                        $next_id = $mount_info['mount_pid'];
                                                        $res2 = $GLOBALS['TYPO3_DB']->exec_SELECTquery($allFields, 'pages', 'uid='.intval($next_id).' AND deleted=0 '.$moreWhereClauses, '' ,'sorting');
                                                        $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res2);
+                                                       $GLOBALS['TYPO3_DB']->sql_free_result($res2);
                                                        $GLOBALS['TSFE']->sys_page->versionOL('pages',$row);
 
                                                        if ($row['doktype']==255 || $row['doktype']==6 || $row['t3ver_state']>0)        { unset($row); }        // Doing this after the overlay to make sure changes in the overlay are respected.
@@ -6428,6 +6434,7 @@ class tslib_cObj {
                                                }
                                        }
                                }
+                               $GLOBALS['TYPO3_DB']->sql_free_result($res);
                        }
                }
                        // If first run, check if the ID should be returned:
@@ -6591,14 +6598,15 @@ class tslib_cObj {
 
                                // Finding the total number of records, if used:
                        if (strstr(strtolower($conf['begin'].$conf['max']),'total'))    {
-                               $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', $table, $queryParts['WHERE'], $queryParts['GROUPBY']);
+                               $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*)', $table, $queryParts['WHERE'], $queryParts['GROUPBY']);
                                if ($error = $GLOBALS['TYPO3_DB']->sql_error()) {
                                        $GLOBALS['TT']->setTSlogMessage($error);
                                } else {
-                                       $total = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
-                                       $conf['max'] = eregi_replace('total', (string)$total, $conf['max']);
-                                       $conf['begin'] = eregi_replace('total', (string)$total, $conf['begin']);
+                                       $row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
+                                       $conf['max'] = eregi_replace('total', $row[0], $conf['max']);
+                                       $conf['begin'] = eregi_replace('total', $row[0], $conf['begin']);
                                }
+                               $GLOBALS['TYPO3_DB']->sql_free_result($res);
                        }
                        if (!$error)    {
                                $conf['begin'] = t3lib_div::intInRange(ceil($this->calc($conf['begin'])),0);
@@ -6758,6 +6766,7 @@ class tslib_cObj {
                                        $outArr[] = $row['uid'];
                                }
                        }
+                       $GLOBALS['TYPO3_DB']->sql_free_result($res);
                }
                return $outArr;
        }
@@ -6779,6 +6788,7 @@ class tslib_cObj {
                        } else {
                                $this->checkPid_cache[$uid] = 0;
                        }
+                       $GLOBALS['TYPO3_DB']->sql_free_result($res);
                }
                return $this->checkPid_cache[$uid];
        }