Skip to content

Instantly share code, notes, and snippets.

@jtnix
Last active April 2, 2018 19:28
Show Gist options
  • Save jtnix/7f66f510e70f712625ff2cd10624faa5 to your computer and use it in GitHub Desktop.
Save jtnix/7f66f510e70f712625ff2cd10624faa5 to your computer and use it in GitHub Desktop.
Patch for Zend Framework 1.14 to allow use of FORCE INDEX MySQL hinting command in SELECT queries
Index: library/Zend/Db/Select.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- library/Zend/Db/Select.php (revision 4524)
+++ library/Zend/Db/Select.php (revision )
@@ -47,6 +47,7 @@
const DISTINCT = 'distinct';
const COLUMNS = 'columns';
const FROM = 'from';
+ const FORCE_INDEX = 'forceindex';
const UNION = 'union';
const WHERE = 'where';
const GROUP = 'group';
@@ -68,6 +69,7 @@
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';
@@ -439,6 +441,19 @@
}
/**
+ * 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.
*
* If a value is passed as the second param, it will be quoted
@@ -1052,6 +1067,19 @@
}
/**
+ * 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
*
* @param string $sql SQL query
@@ -1129,6 +1157,7 @@
$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'])) {
@@ -1147,6 +1176,18 @@
return $sql;
}
+ /**
+ * Render FORCE INDEX option
+ *
+ * @param $sql
+ * @see Zend_Db_Select::_getForceIndexForTable
+ * @return string
+ */
+ protected function _renderForceIndex($sql)
+ {
+ // rendered in _renderFrom block via _getForceIndexForTable
+ return $sql;
+ }
/**
* Render UNION query
*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment