/**
  * Get last used increment part of rewrite request path
  *
  * @param string $urlPath
  * @param Mage_Core_Model_Store $store
  * @return int
  */
 protected function _getRewriteRequestIncrement($urlPath, Mage_Core_Model_Store $store)
 {
     // match request_url abcdef1234(-12)(.html) pattern
     $match = array();
     $regularExpression = '#^([0-9a-z/-]+)(-([0-9]+))?$#i';
     preg_match($regularExpression, $urlPath, $match);
     $match[1] = $match[1] . '-';
     $match[4] = isset($match[4]) ? $match[4] : '';
     $prefix = $match[1];
     $requestPathField = new Zend_Db_Expr($this->_connection->quoteIdentifier('request_path'));
     //select increment part of request path and cast expression to integer
     $urlIncrementPartExpression = $this->_eavHelper->getCastToIntExpression($this->_connection->getSubstringSql($requestPathField, strlen($prefix) + 1, $this->_connection->getLengthSql($requestPathField) . ' - ' . strlen($prefix)));
     $select = $this->_connection->select()->from($this->_getTable('enterprise_urlrewrite/url_rewrite'), new Zend_Db_Expr('MAX(' . $urlIncrementPartExpression . ')'))->where('entity_type = :entity_type')->where('store_id = :store_id')->where('request_path LIKE :request_path')->where($this->_connection->prepareSqlCondition('request_path', array('regexp' => '^' . preg_quote($prefix) . '[0-9]*$')));
     $bind = array('store_id' => (int) $store->getId(), 'request_path' => $prefix . '%', 'entity_type' => Enterprise_Catalog_Model_Category::URL_REWRITE_ENTITY_TYPE);
     return (int) $this->_connection->fetchOne($select, $bind);
 }
 /**
  * Generate unique url key if current url key already occupied
  *
  * @param Mage_Catalog_Model_Abstract $object
  * @return Mage_Catalog_Model_Abstract
  */
 protected function _generateNextUrlKeySuffix(Mage_Catalog_Model_Abstract $object)
 {
     $prefixValue = $object->getData($this->getAttribute()->getAttributeCode());
     $requestPathField = new Zend_Db_Expr($this->_connection->quoteIdentifier('value'));
     //select increment part of request path and cast expression to integer
     $urlIncrementPartExpression = $this->_eavHelper->getCastToIntExpression($this->_connection->getSubstringSql($requestPathField, strlen($prefixValue) + 1, $this->_connection->getLengthSql($requestPathField) . ' - ' . strlen($prefixValue)));
     $prefixRegexp = preg_quote($prefixValue);
     $orCondition = $this->_connection->select()->orWhere($this->_connection->prepareSqlCondition('value', array('regexp' => '^' . $prefixRegexp . '$')))->orWhere($this->_connection->prepareSqlCondition('value', array('regexp' => '^' . $prefixRegexp . '-[0-9]*$')))->getPart(Zend_Db_Select::WHERE);
     $select = $this->_connection->select();
     $select->from($this->getAttribute()->getBackendTable(), new Zend_Db_Expr('MAX(ABS(' . $urlIncrementPartExpression . '))'))->where('value LIKE :url_key')->where('entity_id <> :entity_id')->where(implode('', $orCondition));
     $bind = array('url_key' => $prefixValue . '%', 'entity_id' => (int) $object->getId());
     $suffix = $this->_connection->fetchOne($select, $bind);
     if (!is_null($suffix)) {
         $suffix = (int) $suffix;
         $object->setData($this->getAttribute()->getAttributeCode(), sprintf('%s-%s', $prefixValue, ++$suffix));
     }
     return $object;
 }