- /**
- * Adds new column to table.
- *
- * Generally $defintion must be array with column data to keep this call cross-DB compatible.
- * Using string as $definition is allowed only for concrete DB adapter.
- * Adds primary key if needed
- *
- * @param string $tableName
- * @param string $columnName
- * @param array|string $definition string specific or universal array DB Server definition
- * @param string $schemaName
- * @return true|\Zend_Db_Statement_Pdo
- * @throws \Zend_Db_Exception
- */
- public function addColumn($tableName, $columnName, $definition, $schemaName = null)
- {
- if ($this->tableColumnExists($tableName, $columnName, $schemaName)) {
- return true;
- }
-
- $primaryKey = '';
- if (is_array($definition)) {
- $definition = array_change_key_case($definition, CASE_UPPER);
- if (empty($definition['COMMENT'])) {
- throw new \Zend_Db_Exception("Impossible to create a column without comment.");
- }
- if (!empty($definition['PRIMARY'])) {
- $primaryKey = sprintf(', ADD PRIMARY KEY (%s)', $this->quoteIdentifier($columnName));
- }
- $definition = $this->_getColumnDefinition($definition);
- }
-
- $sql = sprintf(
- 'ALTER TABLE %s ADD COLUMN %s %s %s',
- $this->quoteIdentifier($this->_getTableName($tableName, $schemaName)),
- $this->quoteIdentifier($columnName),
- $definition,
- $primaryKey
- );
-
- $result = $this->rawQuery($sql);
-
- $this->resetDdlCache($tableName, $schemaName);
-
- return $result;
- }