/** * Sets or adds a new index definition * @param Index $index Definition of the index to set or add * @return null * @throws zibo\library\database\exception\DatabaseException when the index contains a field which is not in this table */ public function setIndex(Index $index) { $fields = $index->getFields(); foreach ($fields as $field) { if (!$this->hasField($field->getName())) { throw new DatabaseException('Cannot add the index: the field ' . $field->getName() . ' is not set in this table'); } } $this->indexes[$index->getName()] = $index; }
/** * Sets an index to the table * @param zibo\library\database\definition\Index $index index to add to the table * @return null * @throws zibo\library\orm\exception\ModelException when a field of the index is not in this table * @throws zibo\library\orm\exception\ModelException when a field of the index is not a property or a belongs to field * @throws zibo\library\orm\exception\ModelException when the index contains out of localized and unlocalized fields */ public function setIndex(Index $index) { $isLocalized = null; $fields = $index->getFields(); foreach ($fields as $fieldName => $field) { if (!$this->hasField($fieldName)) { throw new ModelException('Cannot add the index: the field ' . $fieldName . ' is not set in this table'); } if ($this->fields[$fieldName] instanceof HasField) { throw new ModelException('Cannot add the index: the field ' . $fieldName . ' is not a property or a belongs to field'); } if ($isLocalized === null) { $isLocalized = $field->isLocalized(); } elseif ($field->isLocalized() != $isLocalized) { throw new ModelException('Cannot combine localized and unlocalized fields in 1 index'); } } $this->indexes[$index->getName()] = $index; }
/** * Gets the index element for the provided index * @param zibo\library\xml\dom\Document $dom * @param zibo\library\database\definition\Index $index Index to get the element from * @return DOMElement */ protected function getElementFromIndex(Document $dom, Index $index) { $indexName = $index->getName(); $indexElement = $dom->createElement(self::TAG_INDEX); $indexElement->setAttribute(self::ATTRIBUTE_NAME, $indexName); $fields = $index->getFields(); foreach ($fields as $field) { $fieldElement = $dom->createElement(self::TAG_INDEX_FIELD); $fieldElement->setAttribute(self::ATTRIBUTE_NAME, $field->getName()); $indexElement->appendChild($fieldElement); } return $indexElement; }
/** * Adds a index to the provided table * @param string $tableName Quoted name of the table * @param zibo\library\database\definition\Index $index Index to add * @return null */ private function addIndex($tableName, Index $index) { $fields = $index->getFields(); foreach ($fields as $fieldName => $field) { $fields[$fieldName] = $this->connection->quoteIdentifier($fieldName); } $sql = 'ALTER TABLE ' . $tableName . ' ADD INDEX ' . $this->connection->quoteIdentifier($index->getName()) . ' ('; $sql .= implode(', ', $fields) . ')'; $this->connection->execute($sql); }
/** * Adds a index to the provided table * @param string $tableName Plain name of the table * @param zibo\library\database\definition\Index $index Index to add * @return null */ private function addIndex($tableName, Index $index) { $fields = $index->getFields(); foreach ($fields as $fieldName => $field) { $fields[$fieldName] = $this->connection->quoteIdentifier($fieldName); } $sql = 'CREATE INDEX ' . $this->connection->quoteIdentifier('index' . ucFirst($tableName) . ucfirst($index->getName())) . ' ON ' . $this->connection->quoteIdentifier($tableName) . ' ('; $sql .= implode(', ', $fields) . ')'; $this->connection->execute($sql); }