11<?php namespace Illuminate \Database \Schema \Grammars ;
22
33use Illuminate \Support \Fluent ;
4+ use Doctrine \DBAL \Schema \Column ;
5+ use Doctrine \DBAL \Schema \TableDiff ;
6+ use Illuminate \Database \Connection ;
47use Illuminate \Database \Query \Expression ;
58use Illuminate \Database \Schema \Blueprint ;
69use Illuminate \Database \Grammar as BaseGrammar ;
10+ use Doctrine \DBAL \Schema \AbstractSchemaManager as SchemaManager ;
711
812abstract class Grammar extends BaseGrammar {
913
14+ /**
15+ * Compile a rename column command.
16+ *
17+ * @param \Illuminate\Database\Schema\Blueprint $blueprint
18+ * @param \Illuminate\Support\Fluent $command
19+ * @param \Illuminate\Database\Connection $connection
20+ * @return array
21+ */
22+ public function compileRenameColumn (Blueprint $ blueprint , Fluent $ command , Connection $ connection )
23+ {
24+ $ schema = $ connection ->getDoctrineSchemaManager ();
25+
26+ $ column = $ connection ->getDoctrineColumn ($ blueprint ->getTabel (), $ command ->from );
27+
28+ $ tableDiff = $ this ->getRenamedDiff ($ blueprint , $ command , $ column , $ schema );
29+
30+ return (array ) $ schema ->getDatabasePlatform ()->getAlterTableSQL ($ tableDiff );
31+ }
32+
33+ /**
34+ * Get a new column instance with the new column name.
35+ *
36+ * @param \Illuminate\Database\Schema\Blueprint $blueprint
37+ * @param \Illuminate\Support\Fluent $command
38+ * @param \Doctrine\DBAL\Schema\Column $column
39+ * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema
40+ * @return \Doctrine\DBAL\Schema\TableDiff
41+ */
42+ protected function getRenamedDiff (Blueprint $ blueprint , Fluent $ command , Column $ column , SchemaManager $ schema )
43+ {
44+ $ tableDiff = $ this ->getDoctrineTableDiff ($ blueprint , $ schema );
45+
46+ return $ this ->setRenamedColumns ($ tableDiff , $ command , $ column );
47+ }
48+
49+ /**
50+ * Set the renamed columns on the table diff.
51+ *
52+ * @param \Doctrine\DBAL\Schema\TableDiff $tableDiff
53+ * @param \Illuminate\Support\Fluent $command
54+ * @param \Doctrine\DBAL\Schema\Column $column
55+ * @return \Doctrine\DBAL\Schema\TableDiff
56+ */
57+ protected function setRenamedColumns (TableDiff $ tableDiff , Command $ command , Column $ column )
58+ {
59+ $ newColumn = new Column ($ command ->to , $ column ->getType (), $ column ->toArray ());
60+
61+ $ tableDiff ->renamedColumns = array ($ command ->from => $ newColumn );
62+
63+ return $ tableDiff ;
64+ }
65+
1066 /**
1167 * Compile a foreign key command.
1268 *
@@ -59,9 +115,9 @@ protected function getColumns(Blueprint $blueprint)
59115
60116 foreach ($ blueprint ->getColumns () as $ column )
61117 {
62- // Each of the column types have their own compiler functions which are
63- // responsible for turning the column definition into its SQL format
64- // for the platform. Then column modifiers are compiled and added.
118+ // Each of the column types have their own compiler functions which are tasked
119+ // with turning the column definition into its SQL format for this platform
120+ // used by the connection. The column's modifiers are compiled and added.
65121 $ sql = $ this ->wrap ($ column ).' ' .$ this ->getType ($ column );
66122
67123 $ columns [] = $ this ->addModifiers ($ sql , $ blueprint , $ column );
@@ -190,4 +246,20 @@ protected function getDefaultValue($value)
190246 return "' " .strval ($ value )."' " ;
191247 }
192248
249+ /**
250+ * Create an empty Doctrine DBAL TableDiff from the Blueprint.
251+ *
252+ * @param \Illuminate\Database\Schema\Blueprint $blueprint
253+ * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema
254+ * @return \Doctrine\DBAL\Schema\TableDiff
255+ */
256+ protected function getDoctrineTableDiff (Blueprint $ blueprint , SchemaManager $ schema )
257+ {
258+ $ tableDiff = new TableDiff ($ blueprint ->getTable ());
259+
260+ $ tableDiff ->fromTable = $ schema ->listTableDetails ($ blueprint ->getTable ());
261+
262+ return $ tableDiff ;
263+ }
264+
193265}
0 commit comments