cvs: pear /DB_NestedSet NestedSet.php
| From: | Daniel Khan | Date: | Mon, 06 Oct 2003 15:16:24 +0000 |
| Subject: | cvs: pear /DB_NestedSet NestedSet.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-13429@lists.php.net to get a copy of this message | ||
datenpunk Mon Oct 6 11:16:24 2003 EDT
Modified files:
/pear/DB_NestedSet NestedSet.php
Log:
- Fixed MDB support
- Started to add selective quoting
- Renamed __convertTreeModel() to convertTreeModel()
- Fixed some comments
- Made error reporting more consistent
- got rid of some quote() calls which don't make sense
Index: pear/DB_NestedSet/NestedSet.php diff -u pear/DB_NestedSet/NestedSet.php:1.52 pear/DB_NestedSet/NestedSet.php:1.53 --- pear/DB_NestedSet/NestedSet.php:1.52 Fri Oct 3 19:28:47 2003 +++ pear/DB_NestedSet/NestedSet.php Mon Oct 6 11:16:24 2003 @@ -16,7 +16,7 @@ // | Authors: Daniel Khan <dk@webcluster.at> | // | Jason Rust <jason@rustyparts.com> | // +----------------------------------------------------------------------+ -// $Id: NestedSet.php,v 1.52 2003/10/03 23:28:47 datenpunk Exp $ +// $Id: NestedSet.php,v 1.53 2003/10/06 15:16:24 datenpunk Exp $ // // CREDITS: @@ -64,12 +64,12 @@ * * @author Daniel Khan <dk@webcluster.at> * @package DB_NestedSet -* @version $Revision: 1.52 $ +* @version $Revision: 1.53 $ * @access public */ // }}} -class DB_NestedSet extends PEAR { +class DB_NestedSet { // {{{ properties /** @@ -87,7 +87,7 @@ 'STRNA' => 'name' ); - + var $quotedParams = array('name'); /** * @var string The table with the actual tree data @@ -255,7 +255,7 @@ * @var array Map of error messages to their descriptions */ var $messages = array( - NESE_ERROR_RECURSION => 'This operation would lead to a recursion', + NESE_ERROR_RECURSION => '%s: This operation would lead to a recursion', NESE_ERROR_TBLOCKED => 'The structure Table is locked for another database operation, please retry.', NESE_ERROR_NODRIVER => 'The selected database driver %s wasn\'t found', NESE_ERROR_NOTSUPPORTED => 'Method not supported yet', @@ -292,7 +292,6 @@ if ($this->debug) { $this->_debugMessage('DB_NestedSet()'); } - $this->PEAR(); if (is_array($params) && count($params) > 0) { $this->params = $params; } @@ -497,7 +496,7 @@ $this->_addSQL($addSQL, 'join'), $this->node_table, $this->_flparams['rootid'], - $this->_quote($thisnode['rootid']), + $thisnode['rootid'], $this->_addSQL($addSQL, 'append'), $this->node_table, $firstsort, @@ -505,33 +504,20 @@ $this->secondarySort); } elseif($this->sortmode == NESE_SORT_PREORDER) { $firstsort = $this->_flparams['l']; - $sql = sprintf('SELECT %s %s FROM %s %s WHERE %s.%s=%s %s ORDER BY %s.%s', + $sql = sprintf('SELECT %s %s FROM %s %s WHERE %s.%s=%s %s ORDER BY %s.%s ASC', $this->_getSelectFields($aliasFields), $this->_addSQL($addSQL, 'cols'), $this->node_table, $this->_addSQL($addSQL, 'join'), $this->node_table, $this->_flparams['rootid'], - $this->_quote($thisnode['rootid']), + $thisnode['rootid'], $this->_addSQL($addSQL, 'append'), $this->node_table, $firstsort); } - $sql = sprintf('SELECT %s %s FROM %s %s WHERE %s.%s=%s %s ORDER BY %s.%s, %s.%s ASC', - $this->_getSelectFields($aliasFields), - $this->_addSQL($addSQL, 'cols'), - $this->node_table, - $this->_addSQL($addSQL, 'join'), - $this->node_table, - $this->_flparams['rootid'], - $this->_quote($thisnode['rootid']), - $this->_addSQL($addSQL, 'append'), - $this->node_table, - $firstsort, - $this->node_table, - $this->secondarySort); - + if (!$this->_caching) { $nodeSet = $this->_processResultSet($sql, $keepAsArray, $aliasFields); } else { @@ -741,7 +727,7 @@ $this->_addSQL($addSQL, 'join'), $this->node_table, $this->_flparams['rootid'], - $this->_quote($parent['rootid']), + $parent['rootid'], $this->node_table, $this->_flparams['level'], $parent['level'], @@ -809,11 +795,11 @@ $parent['r'], $this->node_table, $this->_flparams['rootid'], - $this->_quote($parent['rootid']), + $parent['rootid'], $this->node_table, $this->_flparams['id'], $this->_addSQL($addSQL, 'append'), - $this->_quote($id), + $id, $this->_flparams['l'] ); @@ -869,7 +855,7 @@ $this->_addSQL($addSQL, 'join'), $this->node_table, $this->_flparams[$idfield], - $this->_quote($id), + $id, $this->_addSQL($addSQL, 'append')); if (!$this->_caching) { @@ -982,7 +968,7 @@ * @param bool $first Danger: Deletes and (re)init's the hole tree - sequences are reset * * @access public - * @return int The node id + * @return mixed The node id or false on error */ function createRootNode($values, $id = false, $first = false, $_pos = 'AF') { @@ -996,10 +982,13 @@ $epr = array('createRootNode()', $id); return $this->_raiseError(NESE_ERROR_NOT_FOUND, E_USER_ERROR, $epr); } elseif($first && $id) { + + // No notice for now. + // But tehese 2 params don't make sense together $epr = array( 'createRootNode()', '[id] AND [first] were passed - that doesn\'t make sense'); - $this->_raiseError(NESE_ERROR_WRONG_MPARAM, E_USER_WARNING, $epr); + //$this->_raiseError(NESE_ERROR_WRONG_MPARAM, E_USER_WARNING, $epr); } // Try to aquire a table lock @@ -1067,14 +1056,12 @@ $sql[] = sprintf('INSERT INTO %s SET %s', $this->node_table, $qr); - - + for($i=0;$i<count($sql);$i++) { $res = $this->db->query($sql[$i]); $this->_testFatalAbort($res, __FILE__, __LINE__); } - - + // EVENT (nodeCreate) if (!$this->_skipCallbacks && isset($this->_hasListeners['nodeCreate'])) { @@ -1111,7 +1098,6 @@ if ($this->debug) { $this->_debugMessage('createSubNode($id, $values)'); } - // invalid parent id, bail out if (!($thisnode = $this->pickNode($id, true))) { @@ -1201,7 +1187,31 @@ // }}} // {{{ createLeftNode() - + /** + * Creates a node before a given node + * <pre> + * +-- root1 + * | + * +-\ root2 + * | | + * | |-- subnode2 [new] + * | |-- subnode1 [target] + * | |-- subnode3 + * | + * +-- root3 + * </pre> + * + * @param int $id Target node ID + * @param array $values Hash with param => value pairs of the node (see $this->params) + * @param bool $returnID Tell the method to return a node id instead of an object. + * ATTENTION: That the method defaults to return an object instead of the node id + * has been overseen and is basically a bug. We have to keep this to maintain BC. + * You will have to set $returnID to true to make it behave like the other creation methods. + * This flaw will get fixed with the next major version. + * + * @access public + * @return mixed The node id or false on error + */ function createLeftNode($id, $values) { if ($this->debug) { @@ -1333,7 +1343,7 @@ * This flaw will get fixed with the next major version. * * @access public - * @return object The new node object + * @return mixed The node id or false on error */ function createRightNode($id, $values) { @@ -1439,8 +1449,6 @@ if (!$this->_skipCallbacks && isset($this->_hasListeners['nodeCreate'])) { $this->triggerEvent('nodeCreate', $this->pickNode($id)); } - - // Dirty ugly hack to keep BC $this->_releaseLock(); return $node_id; } @@ -1462,8 +1470,10 @@ $this->_debugMessage("deleteNode($id)"); } + // invalid target node, bail out if (!($thisnode = $this->pickNode($id, true))) { - return false; + $epr = array('deleteNode()', $id); + return $this->_raiseError(NESE_ERROR_NOT_FOUND, E_USER_ERROR, $epr); } if (PEAR::isError($lock = $this->_setLock())) { @@ -1669,13 +1679,13 @@ // Get information about source and target if (!($source = $this->pickNode($id, true))) { - $this->raiseError("Node id: $id not found", NESE_ERROR_NOT_FOUND, PEAR_ERROR_TRIGGER, E_USER_ERROR); - return false; + $epr = array('moveTree()', $id); + return $this->_raiseError(NESE_ERROR_NOT_FOUND, E_USER_ERROR, $epr); } if (!($target = $this->pickNode($targetid, true))) { - $this->raiseError("Target id: $targetid not found", NESE_ERROR_NOT_FOUND, PEAR_ERROR_TRIGGER, E_USER_ERROR); - return false; + $epr = array('moveTree()', $targetid); + return $this->_raiseError(NESE_ERROR_NOT_FOUND, E_USER_ERROR, $epr); } if (PEAR::isError($lock = $this->_setLock(true))) { @@ -1693,7 +1703,8 @@ (($source['l'] <= $target['l']) && ($source['r'] >= $target['r']))) { $this->_releaseLock(true); - return new PEAR_Error($this->_getMessage(NESE_ERROR_RECURSION),NESE_ERROR_RECURSION); + $epr = array('moveTree()'); + return $this->_raiseError(NESE_ERROR_RECURSION, E_USER_NOTICE, $epr); } // Insert/move before or after @@ -1709,7 +1720,8 @@ (($source['l'] < $target['l']) && ($source['r'] > $target['r']))) { $this->_releaseLock(true); - return new PEAR_Error($this->_getMessage(NESE_ERROR_RECURSION),NESE_ERROR_RECURSION); + $epr = array('moveTree()'); + return $this->_raiseError(NESE_ERROR_RECURSION, E_USER_NOTICE, $epr); } @@ -1856,14 +1868,14 @@ } else { $sql = "UPDATE $tb SET - $fid=" . $this->_quote($key) . ", - $froot=" . $this->_quote($key) . " - WHERE $fid=" . $this->_quote($val); + $fid=" . $this->quote($key) . ", + $froot=" . $this->quote($key) . " + WHERE $fid=" . $this->quote($val); $updates[] = $sql; $orootid = $clone->rootid; $sql = "UPDATE $tb - SET $froot=" . $this->_quote($key) . " - WHERE $froot=" . $this->_quote($orootid); + SET $froot=" . $this->quote($key) . " + WHERE $froot=" . $this->quote($orootid); $updates[] = $sql; } $this->_skipCallbacks = false; @@ -2128,7 +2140,7 @@ * @access private */ function _testFatalAbort($errobj, $file, $line) { - if (!PEAR::isError($errobj)) { + if (!$this->_isDBError($errobj)) { return false; } @@ -2143,7 +2155,7 @@ $msg = $errobj->getMessage(); $code = $errobj->getCode(); } - $this->raiseError($msg, $code, PEAR_ERROR_TRIGGER, E_USER_ERROR); + PEAR::raiseError($msg, $code, PEAR_ERROR_TRIGGER, E_USER_ERROR); } // }}} @@ -2226,7 +2238,7 @@ 'majorversion'=>$this->_majorversion, 'minorversion'=>$this->_minorversion, 'version'=>sprintf('%s.%s',$this->_majorversion, $this->_minorversion), - 'revision'=>str_replace('$', '',"$Revision: 1.52 $") + 'revision'=>str_replace('$', '',"$Revision: 1.53 $") ); } @@ -2299,20 +2311,18 @@ $this->_debugMessage('testLock()'); } - - if($lockID = $this->_structureTableLock) { return $lockID; } - $this->_lockGC(); - $tb = $this->lock_table; - $stb = $this->node_table; + $this->_lockGC(); + $sql = sprintf('SELECT lockID FROM %s WHERE lockTable=%s', + $this->lock_table, + $this->quote($this->node_table)) ; - $sql = "SELECT lockID FROM $tb WHERE lockTable=" . $this->_quote($stb); $res = $this->db->query($sql); $this->_testFatalAbort($res, __FILE__, __LINE__); - - if ($res->numRows()) { + + if ($this->_numRows($res)) { return new PEAR_Error($this->_getMessage(NESE_ERROR_TBLOCKED),NESE_ERROR_TBLOCKED); } @@ -2339,22 +2349,23 @@ $this->_caching = false; $this->_restcache = true; } - $tb = $this->lock_table; - $stb = $this->node_table; - $stamp = time(); + if (!$lockID = $this->_structureTableLock) { $lockID = $this->_structureTableLock = uniqid('lck-'); - $sql = "INSERT INTO $tb SET - lockID=" . $this->_quote($lockID) . ", - lockTable=" . $this->_quote($stb) . ", - lockStamp=" . $this->_quote($stamp); + $sql = sprintf('INSERT INTO %s SET lockID=%s, lockTable=%s, lockStamp=%s', + $this->lock_table, + $this->quote($lockID), + $this->quote($this->node_table), + time()); + } else { - $sql = "UPDATE $tb SET lockStamp=" . $this->_quote($stamp) . " - WHERE lockID=" . $this->_quote($lockID) . " AND - lockTable=" . $this->_quote($stb); - } - + $sql = sprintf('UPDATE %s set lockStamp=%s WHERE lockID=%s AND lockTable=%s', + $this->lock_table, + time(), + $this->quote($lockID), + $this->quote($this->node_table)); + } if($exclusive) { $this->_lockExclusive = true; } @@ -2386,8 +2397,8 @@ $tb = $this->lock_table; $stb = $this->node_table; $sql = "DELETE FROM $tb - WHERE lockTable=" . $this->_quote($stb) . " AND - lockID=" . $this->_quote($lockID); + WHERE lockTable=" . $this->quote($stb) . " AND + lockID=" . $this->quote($lockID); $res = $this->db->query($sql); $this->_testFatalAbort($res, __FILE__, __LINE__); @@ -2413,7 +2424,7 @@ $stb = $this->node_table; $lockTTL = time() - $this->lockTTL; $sql = "DELETE FROM $tb - WHERE lockTable=" . $this->_quote($stb) . " AND + WHERE lockTable=" . $this->quote($stb) . " AND lockStamp < $lockTTL"; $res = $this->db->query($sql); @@ -2440,7 +2451,7 @@ $v = trim($val); if ($k) { - $arq[] = "$k=" . $this->_quote($v); + $arq[] = "$k=" . $this->quote($v); } } @@ -2533,26 +2544,7 @@ } // }}} - function _quote($str = null) - { - switch (strtolower(gettype($str))) { - case 'null': - return 'NULL'; - case 'integer': - case 'double': - return $str; - case 'string': - default: - if(function_exists('mysql_real_escape_string')) { - return "'".mysql_real_escape_string($str, $this->db->connection)."'"; - } else { - return "'".mysql_escape_string($str)."'"; - } - } - } - - - function __convertTreeModel(&$orig, &$copy, $parent=false) { + function convertTreeModel(&$orig, &$copy, $parent=false) { static $firstSet; @@ -2599,10 +2591,10 @@ $copy->createSubNode($parent, $values); } - DB_NestedSet::__convertTreeModel($orig, $copy, $sid); + DB_NestedSet::convertTreeModel($orig, $copy, $sid); } - } + } } // {{{ DB_NestedSet_Node:: class @@ -2611,7 +2603,7 @@ * Generic class for node objects * * @autor Daniel Khan <dk@webcluster.at>; -* @version $Revision: 1.52 $ +* @version $Revision: 1.53 $ * @package DB_NestedSet * * @access private
Index: pear/DB_NestedSet/NestedSet.php diff -u pear/DB_NestedSet/NestedSet.php:1.52 pear/DB_NestedSet/NestedSet.php:1.53 --- pear/DB_NestedSet/NestedSet.php:1.52 Fri Oct 3 19:28:47 2003 +++ pear/DB_NestedSet/NestedSet.php Mon Oct 6 11:16:24 2003 @@ -16,7 +16,7 @@ // | Authors: Daniel Khan <dk@webcluster.at> | // | Jason Rust <jason@rustyparts.com> | // +----------------------------------------------------------------------+ -// $Id: NestedSet.php,v 1.52 2003/10/03 23:28:47 datenpunk Exp $ +// $Id: NestedSet.php,v 1.53 2003/10/06 15:16:24 datenpunk Exp $ // // CREDITS: @@ -64,12 +64,12 @@ * * @author Daniel Khan <dk@webcluster.at> * @package DB_NestedSet -* @version $Revision: 1.52 $ +* @version $Revision: 1.53 $ * @access public */ // }}} -class DB_NestedSet extends PEAR { +class DB_NestedSet { // {{{ properties /** @@ -87,7 +87,7 @@ 'STRNA' => 'name' ); - + var $quotedParams = array('name'); /** * @var string The table with the actual tree data @@ -255,7 +255,7 @@ * @var array Map of error messages to their descriptions */ var $messages = array( - NESE_ERROR_RECURSION => 'This operation would lead to a recursion', + NESE_ERROR_RECURSION => '%s: This operation would lead to a recursion', NESE_ERROR_TBLOCKED => 'The structure Table is locked for another database operation, please retry.', NESE_ERROR_NODRIVER => 'The selected database driver %s wasn\'t found', NESE_ERROR_NOTSUPPORTED => 'Method not supported yet', @@ -292,7 +292,6 @@ if ($this->debug) { $this->_debugMessage('DB_NestedSet()'); } - $this->PEAR(); if (is_array($params) && count($params) > 0) { $this->params = $params; } @@ -497,7 +496,7 @@ $this->_addSQL($addSQL, 'join'), $this->node_table, $this->_flparams['rootid'], - $this->_quote($thisnode['rootid']), + $thisnode['rootid'], $this->_addSQL($addSQL, 'append'), $this->node_table, $firstsort, @@ -505,33 +504,20 @@ $this->secondarySort); } elseif($this->sortmode == NESE_SORT_PREORDER) { $firstsort = $this->_flparams['l']; - $sql = sprintf('SELECT %s %s FROM %s %s WHERE %s.%s=%s %s ORDER BY %s.%s', + $sql = sprintf('SELECT %s %s FROM %s %s WHERE %s.%s=%s %s ORDER BY %s.%s ASC', $this->_getSelectFields($aliasFields), $this->_addSQL($addSQL, 'cols'), $this->node_table, $this->_addSQL($addSQL, 'join'), $this->node_table, $this->_flparams['rootid'], - $this->_quote($thisnode['rootid']), + $thisnode['rootid'], $this->_addSQL($addSQL, 'append'), $this->node_table, $firstsort); } - $sql = sprintf('SELECT %s %s FROM %s %s WHERE %s.%s=%s %s ORDER BY %s.%s, %s.%s ASC', - $this->_getSelectFields($aliasFields), - $this->_addSQL($addSQL, 'cols'), - $this->node_table, - $this->_addSQL($addSQL, 'join'), - $this->node_table, - $this->_flparams['rootid'], - $this->_quote($thisnode['rootid']), - $this->_addSQL($addSQL, 'append'), - $this->node_table, - $firstsort, - $this->node_table, - $this->secondarySort); - + if (!$this->_caching) { $nodeSet = $this->_processResultSet($sql, $keepAsArray, $aliasFields); } else { @@ -741,7 +727,7 @@ $this->_addSQL($addSQL, 'join'), $this->node_table, $this->_flparams['rootid'], - $this->_quote($parent['rootid']), + $parent['rootid'], $this->node_table, $this->_flparams['level'], $parent['level'], @@ -809,11 +795,11 @@ $parent['r'], $this->node_table, $this->_flparams['rootid'], - $this->_quote($parent['rootid']), + $parent['rootid'], $this->node_table, $this->_flparams['id'], $this->_addSQL($addSQL, 'append'), - $this->_quote($id), + $id, $this->_flparams['l'] ); @@ -869,7 +855,7 @@ $this->_addSQL($addSQL, 'join'), $this->node_table, $this->_flparams[$idfield], - $this->_quote($id), + $id, $this->_addSQL($addSQL, 'append')); if (!$this->_caching) { @@ -982,7 +968,7 @@ * @param bool $first Danger: Deletes and (re)init's the hole tree - sequences are reset * * @access public - * @return int The node id + * @return mixed The node id or false on error */ function createRootNode($values, $id = false, $first = false, $_pos = 'AF') { @@ -996,10 +982,13 @@ $epr = array('createRootNode()', $id); return $this->_raiseError(NESE_ERROR_NOT_FOUND, E_USER_ERROR, $epr); } elseif($first && $id) { + + // No notice for now. + // But tehese 2 params don't make sense together $epr = array( 'createRootNode()', '[id] AND [first] were passed - that doesn\'t make sense'); - $this->_raiseError(NESE_ERROR_WRONG_MPARAM, E_USER_WARNING, $epr); + //$this->_raiseError(NESE_ERROR_WRONG_MPARAM, E_USER_WARNING, $epr); } // Try to aquire a table lock @@ -1067,14 +1056,12 @@ $sql[] = sprintf('INSERT INTO %s SET %s', $this->node_table, $qr); - - + for($i=0;$i<count($sql);$i++) { $res = $this->db->query($sql[$i]); $this->_testFatalAbort($res, __FILE__, __LINE__); } - - + // EVENT (nodeCreate) if (!$this->_skipCallbacks && isset($this->_hasListeners['nodeCreate'])) { @@ -1111,7 +1098,6 @@ if ($this->debug) { $this->_debugMessage('createSubNode($id, $values)'); } - // invalid parent id, bail out if (!($thisnode = $this->pickNode($id, true))) { @@ -1201,7 +1187,31 @@ // }}} // {{{ createLeftNode() - + /** + * Creates a node before a given node + * <pre> + * +-- root1 + * | + * +-\ root2 + * | | + * | |-- subnode2 [new] + * | |-- subnode1 [target] + * | |-- subnode3 + * | + * +-- root3 + * </pre> + * + * @param int $id Target node ID + * @param array $values Hash with param => value pairs of the node (see $this->params) + * @param bool $returnID Tell the method to return a node id instead of an object. + * ATTENTION: That the method defaults to return an object instead of the node id + * has been overseen and is basically a bug. We have to keep this to maintain BC. + * You will have to set $returnID to true to make it behave like the other creation methods. + * This flaw will get fixed with the next major version. + * + * @access public + * @return mixed The node id or false on error + */ function createLeftNode($id, $values) { if ($this->debug) { @@ -1333,7 +1343,7 @@ * This flaw will get fixed with the next major version. * * @access public - * @return object The new node object + * @return mixed The node id or false on error */ function createRightNode($id, $values) { @@ -1439,8 +1449,6 @@ if (!$this->_skipCallbacks && isset($this->_hasListeners['nodeCreate'])) { $this->triggerEvent('nodeCreate', $this->pickNode($id)); } - - // Dirty ugly hack to keep BC $this->_releaseLock(); return $node_id; } @@ -1462,8 +1470,10 @@ $this->_debugMessage("deleteNode($id)"); } + // invalid target node, bail out if (!($thisnode = $this->pickNode($id, true))) { - return false; + $epr = array('deleteNode()', $id); + return $this->_raiseError(NESE_ERROR_NOT_FOUND, E_USER_ERROR, $epr); } if (PEAR::isError($lock = $this->_setLock())) { @@ -1669,13 +1679,13 @@ // Get information about source and target if (!($source = $this->pickNode($id, true))) { - $this->raiseError("Node id: $id not found", NESE_ERROR_NOT_FOUND, PEAR_ERROR_TRIGGER, E_USER_ERROR); - return false; + $epr = array('moveTree()', $id); + return $this->_raiseError(NESE_ERROR_NOT_FOUND, E_USER_ERROR, $epr); } if (!($target = $this->pickNode($targetid, true))) { - $this->raiseError("Target id: $targetid not found", NESE_ERROR_NOT_FOUND, PEAR_ERROR_TRIGGER, E_USER_ERROR); - return false; + $epr = array('moveTree()', $targetid); + return $this->_raiseError(NESE_ERROR_NOT_FOUND, E_USER_ERROR, $epr); } if (PEAR::isError($lock = $this->_setLock(true))) { @@ -1693,7 +1703,8 @@ (($source['l'] <= $target['l']) && ($source['r'] >= $target['r']))) { $this->_releaseLock(true); - return new PEAR_Error($this->_getMessage(NESE_ERROR_RECURSION),NESE_ERROR_RECURSION); + $epr = array('moveTree()'); + return $this->_raiseError(NESE_ERROR_RECURSION, E_USER_NOTICE, $epr); } // Insert/move before or after @@ -1709,7 +1720,8 @@ (($source['l'] < $target['l']) && ($source['r'] > $target['r']))) { $this->_releaseLock(true); - return new PEAR_Error($this->_getMessage(NESE_ERROR_RECURSION),NESE_ERROR_RECURSION); + $epr = array('moveTree()'); + return $this->_raiseError(NESE_ERROR_RECURSION, E_USER_NOTICE, $epr); } @@ -1856,14 +1868,14 @@ } else { $sql = "UPDATE $tb SET - $fid=" . $this->_quote($key) . ", - $froot=" . $this->_quote($key) . " - WHERE $fid=" . $this->_quote($val); + $fid=" . $this->quote($key) . ", + $froot=" . $this->quote($key) . " + WHERE $fid=" . $this->quote($val); $updates[] = $sql; $orootid = $clone->rootid; $sql = "UPDATE $tb - SET $froot=" . $this->_quote($key) . " - WHERE $froot=" . $this->_quote($orootid); + SET $froot=" . $this->quote($key) . " + WHERE $froot=" . $this->quote($orootid); $updates[] = $sql; } $this->_skipCallbacks = false; @@ -2128,7 +2140,7 @@ * @access private */ function _testFatalAbort($errobj, $file, $line) { - if (!PEAR::isError($errobj)) { + if (!$this->_isDBError($errobj)) { return false; } @@ -2143,7 +2155,7 @@ $msg = $errobj->getMessage(); $code = $errobj->getCode(); } - $this->raiseError($msg, $code, PEAR_ERROR_TRIGGER, E_USER_ERROR); + PEAR::raiseError($msg, $code, PEAR_ERROR_TRIGGER, E_USER_ERROR); } // }}} @@ -2226,7 +2238,7 @@ 'majorversion'=>$this->_majorversion, 'minorversion'=>$this->_minorversion, 'version'=>sprintf('%s.%s',$this->_majorversion, $this->_minorversion), - 'revision'=>str_replace('$', '',"$Revision: 1.52 $") + 'revision'=>str_replace('$', '',"$Revision: 1.53 $") ); } @@ -2299,20 +2311,18 @@ $this->_debugMessage('testLock()'); } - - if($lockID = $this->_structureTableLock) { return $lockID; } - $this->_lockGC(); - $tb = $this->lock_table; - $stb = $this->node_table; + $this->_lockGC(); + $sql = sprintf('SELECT lockID FROM %s WHERE lockTable=%s', + $this->lock_table, + $this->quote($this->node_table)) ; - $sql = "SELECT lockID FROM $tb WHERE lockTable=" . $this->_quote($stb); $res = $this->db->query($sql); $this->_testFatalAbort($res, __FILE__, __LINE__); - - if ($res->numRows()) { + + if ($this->_numRows($res)) { return new PEAR_Error($this->_getMessage(NESE_ERROR_TBLOCKED),NESE_ERROR_TBLOCKED); } @@ -2339,22 +2349,23 @@ $this->_caching = false; $this->_restcache = true; } - $tb = $this->lock_table; - $stb = $this->node_table; - $stamp = time(); + if (!$lockID = $this->_structureTableLock) { $lockID = $this->_structureTableLock = uniqid('lck-'); - $sql = "INSERT INTO $tb SET - lockID=" . $this->_quote($lockID) . ", - lockTable=" . $this->_quote($stb) . ", - lockStamp=" . $this->_quote($stamp); + $sql = sprintf('INSERT INTO %s SET lockID=%s, lockTable=%s, lockStamp=%s', + $this->lock_table, + $this->quote($lockID), + $this->quote($this->node_table), + time()); + } else { - $sql = "UPDATE $tb SET lockStamp=" . $this->_quote($stamp) . " - WHERE lockID=" . $this->_quote($lockID) . " AND - lockTable=" . $this->_quote($stb); - } - + $sql = sprintf('UPDATE %s set lockStamp=%s WHERE lockID=%s AND lockTable=%s', + $this->lock_table, + time(), + $this->quote($lockID), + $this->quote($this->node_table)); + } if($exclusive) { $this->_lockExclusive = true; } @@ -2386,8 +2397,8 @@ $tb = $this->lock_table; $stb = $this->node_table; $sql = "DELETE FROM $tb - WHERE lockTable=" . $this->_quote($stb) . " AND - lockID=" . $this->_quote($lockID); + WHERE lockTable=" . $this->quote($stb) . " AND + lockID=" . $this->quote($lockID); $res = $this->db->query($sql); $this->_testFatalAbort($res, __FILE__, __LINE__); @@ -2413,7 +2424,7 @@ $stb = $this->node_table; $lockTTL = time() - $this->lockTTL; $sql = "DELETE FROM $tb - WHERE lockTable=" . $this->_quote($stb) . " AND + WHERE lockTable=" . $this->quote($stb) . " AND lockStamp < $lockTTL"; $res = $this->db->query($sql); @@ -2440,7 +2451,7 @@ $v = trim($val); if ($k) { - $arq[] = "$k=" . $this->_quote($v); + $arq[] = "$k=" . $this->quote($v); } } @@ -2533,26 +2544,7 @@ } // }}} - function _quote($str = null) - { - switch (strtolower(gettype($str))) { - case 'null': - return 'NULL'; - case 'integer': - case 'double': - return $str; - case 'string': - default: - if(function_exists('mysql_real_escape_string')) { - return "'".mysql_real_escape_string($str, $this->db->connection)."'"; - } else { - return "'".mysql_escape_string($str)."'"; - } - } - } - - - function __convertTreeModel(&$orig, &$copy, $parent=false) { + function convertTreeModel(&$orig, &$copy, $parent=false) { static $firstSet; @@ -2599,10 +2591,10 @@ $copy->createSubNode($parent, $values); } - DB_NestedSet::__convertTreeModel($orig, $copy, $sid); + DB_NestedSet::convertTreeModel($orig, $copy, $sid); } - } + } } // {{{ DB_NestedSet_Node:: class @@ -2611,7 +2603,7 @@ * Generic class for node objects * * @autor Daniel Khan <dk@webcluster.at>; -* @version $Revision: 1.52 $ +* @version $Revision: 1.53 $ * @package DB_NestedSet * * @access private