Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ForceIndex function to DB Select.php #350

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions library/Zend/Db/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Zend_Db_Select
const DISTINCT = 'distinct';
const COLUMNS = 'columns';
const FROM = 'from';
const FORCE_INDEX = 'forceindex';
const UNION = 'union';
const WHERE = 'where';
const GROUP = 'group';
Expand All @@ -73,6 +74,7 @@ class Zend_Db_Select
const SQL_UNION = 'UNION';
const SQL_UNION_ALL = 'UNION ALL';
const SQL_FROM = 'FROM';
const SQL_FORCE_INDEX = 'FORCE INDEX';
const SQL_WHERE = 'WHERE';
const SQL_DISTINCT = 'DISTINCT';
const SQL_GROUP_BY = 'GROUP BY';
Expand Down Expand Up @@ -466,6 +468,19 @@ public function joinNatural($name, $cols = self::SQL_WILDCARD, $schema = null)
{
return $this->_join(self::NATURAL_JOIN, $name, null, $cols, $schema);
}

/**
* Adds an index hint for a given schema.index
* @param $tableIndex
* @throws Zend_Db_Select_Exception
*/
public function forceIndex($tableIndex)
{
list($table,$index) = explode('.',$tableIndex);
if (empty($table) || empty($index)) {
throw new Zend_Db_Select_Exception("Format of forceIndex is table_name.index_name");
}
}

/**
* Adds a WHERE condition to the query by AND.
Expand Down Expand Up @@ -1089,6 +1104,19 @@ protected function _getQuotedTable($tableName, $correlationName = null)
{
return $this->_adapter->quoteTableAs($tableName, $correlationName, true);
}

/**
* Returns force index statement for _renderFrom processing
* @param $tableName
* @return string
*/
protected function _getForceIndexForTable($tableName)
{
if ($this->_parts[self::FORCE_INDEX][$tableName]) {
return ' ' . self::SQL_FORCE_INDEX . ' ('.$this->_parts[self::FORCE_INDEX][$tableName].')';
}
return '';
}

/**
* Render DISTINCT clause
Expand Down Expand Up @@ -1168,6 +1196,7 @@ protected function _renderFrom($sql)

$tmp .= $this->_getQuotedSchema($table['schema']);
$tmp .= $this->_getQuotedTable($table['tableName'], $correlationName);
$tmp .= $this->_getForceIndexForTable($table['tableName']);

// Add join conditions (if applicable)
if (!empty($from) && ! empty($table['joinCondition'])) {
Expand All @@ -1185,6 +1214,19 @@ protected function _renderFrom($sql)

return $sql;
}

/**
* Render FORCE INDEX option
*
* @param $sql
* @see Zend_Db_Select::_getForceIndexForTable
* @return string
*/
protected function _renderForceIndex($sql)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this is here, could you please remove that? I would like to merge this one, thanks.

{
// rendered in _renderFrom block via _getForceIndexForTable
return $sql;
}

/**
* Render UNION query
Expand Down