Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Db / Sql / Ddl / AlterTable.php
blob60fb4b08217d31bb387cf42db335b7637d73e912
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Db\Sql\Ddl;
12 use Zend\Db\Adapter\Platform\PlatformInterface;
13 use Zend\Db\Adapter\Platform\Sql92 as AdapterSql92Platform;
14 use Zend\Db\Sql\AbstractSql;
16 class AlterTable extends AbstractSql implements SqlInterface
18 const ADD_COLUMNS = 'addColumns';
19 const ADD_CONSTRAINTS = 'addConstraints';
20 const CHANGE_COLUMNS = 'changeColumns';
21 const DROP_COLUMNS = 'dropColumns';
22 const DROP_CONSTRAINTS = 'dropConstraints';
23 const TABLE = 'table';
25 /**
26 * @var array
28 protected $addColumns = array();
30 /**
31 * @var array
33 protected $addConstraints = array();
35 /**
36 * @var array
38 protected $changeColumns = array();
40 /**
41 * @var array
43 protected $dropColumns = array();
45 /**
46 * @var array
48 protected $dropConstraints = array();
50 /**
51 * Specifications for Sql String generation
52 * @var array
54 protected $specifications = array(
55 self::TABLE => "ALTER TABLE %1\$s\n",
56 self::ADD_COLUMNS => array(
57 "%1\$s" => array(
58 array(1 => 'ADD COLUMN %1$s', 'combinedby' => ",\n")
61 self::CHANGE_COLUMNS => array(
62 "%1\$s" => array(
63 array(2 => 'CHANGE COLUMN %1$s %2$s', 'combinedby' => ",\n"),
66 self::DROP_COLUMNS => array(
67 "%1\$s" => array(
68 array(1 => 'DROP COLUMN %1$s', 'combinedby' => ",\n"),
71 self::ADD_CONSTRAINTS => array(
72 "%1\$s" => array(
73 array(1 => 'ADD %1$s', 'combinedby' => ",\n"),
76 self::DROP_CONSTRAINTS => array(
77 "%1\$s" => array(
78 array(1 => 'DROP CONSTRAINT %1$s', 'combinedby' => ",\n"),
83 /**
84 * @var string
86 protected $table = '';
88 /**
89 * @param string $table
91 public function __construct($table = '')
93 ($table) ? $this->setTable($table) : null;
96 /**
97 * @param string $name
98 * @return self
100 public function setTable($name)
102 $this->table = $name;
104 return $this;
108 * @param Column\ColumnInterface $column
109 * @return self
111 public function addColumn(Column\ColumnInterface $column)
113 $this->addColumns[] = $column;
115 return $this;
119 * @param string $name
120 * @param Column\ColumnInterface $column
121 * @return self
123 public function changeColumn($name, Column\ColumnInterface $column)
125 $this->changeColumns[$name] = $column;
127 return $this;
131 * @param string $name
132 * @return self
134 public function dropColumn($name)
136 $this->dropColumns[] = $name;
138 return $this;
142 * @param string $name
143 * @return self
145 public function dropConstraint($name)
147 $this->dropConstraints[] = $name;
149 return $this;
153 * @param Constraint\ConstraintInterface $constraint
154 * @return self
156 public function addConstraint(Constraint\ConstraintInterface $constraint)
158 $this->addConstraints[] = $constraint;
160 return $this;
164 * @param string|null $key
165 * @return array
167 public function getRawState($key = null)
169 $rawState = array(
170 self::TABLE => $this->table,
171 self::ADD_COLUMNS => $this->addColumns,
172 self::DROP_COLUMNS => $this->dropColumns,
173 self::CHANGE_COLUMNS => $this->changeColumns,
174 self::ADD_CONSTRAINTS => $this->addConstraints,
175 self::DROP_CONSTRAINTS => $this->dropConstraints,
178 return (isset($key) && array_key_exists($key, $rawState)) ? $rawState[$key] : $rawState;
182 * @param PlatformInterface $adapterPlatform
183 * @return string
185 public function getSqlString(PlatformInterface $adapterPlatform = null)
187 // get platform, or create default
188 $adapterPlatform = ($adapterPlatform) ?: new AdapterSql92Platform;
190 $sqls = array();
191 $parameters = array();
193 foreach ($this->specifications as $name => $specification) {
194 $parameters[$name] = $this->{'process' . $name}($adapterPlatform, null, null, $sqls, $parameters);
195 if ($specification && is_array($parameters[$name]) && ($parameters[$name] != array(array()))) {
196 $sqls[$name] = $this->createSqlFromSpecificationAndParameters($specification, $parameters[$name]);
198 if (stripos($name, 'table') === false && $parameters[$name] !== array(array())) {
199 $sqls[] = ",\n";
203 // remove last ,\n
204 array_pop($sqls);
206 $sql = implode('', $sqls);
208 return $sql;
211 protected function processTable(PlatformInterface $adapterPlatform = null)
213 return array($adapterPlatform->quoteIdentifier($this->table));
216 protected function processAddColumns(PlatformInterface $adapterPlatform = null)
218 $sqls = array();
219 foreach ($this->addColumns as $column) {
220 $sqls[] = $this->processExpression($column, $adapterPlatform)->getSql();
223 return array($sqls);
226 protected function processChangeColumns(PlatformInterface $adapterPlatform = null)
228 $sqls = array();
229 foreach ($this->changeColumns as $name => $column) {
230 $sqls[] = array(
231 $adapterPlatform->quoteIdentifier($name),
232 $this->processExpression($column, $adapterPlatform)->getSql()
236 return array($sqls);
239 protected function processDropColumns(PlatformInterface $adapterPlatform = null)
241 $sqls = array();
242 foreach ($this->dropColumns as $column) {
243 $sqls[] = $adapterPlatform->quoteIdentifier($column);
246 return array($sqls);
249 protected function processAddConstraints(PlatformInterface $adapterPlatform = null)
251 $sqls = array();
252 foreach ($this->addConstraints as $constraint) {
253 $sqls[] = $this->processExpression($constraint, $adapterPlatform);
256 return array($sqls);
259 protected function processDropConstraints(PlatformInterface $adapterPlatform = null)
261 $sqls = array();
262 foreach ($this->dropConstraints as $constraint) {
263 $sqls[] = $adapterPlatform->quoteIdentifier($constraint);
266 return array($sqls);