Skip to content

Commit

Permalink
fixed PR: Add ForceIndex function to DB Select.php Shardj#350
Browse files Browse the repository at this point in the history
  • Loading branch information
develart-projects committed Aug 10, 2023
1 parent 8d040e6 commit e41414c
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion 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,7 +1214,7 @@ protected function _renderFrom($sql)

return $sql;
}

/**
* Render UNION query
*
Expand Down

0 comments on commit e41414c

Please sign in to comment.