22
33namespace SingleStore \Laravel \Schema \Blueprint ;
44
5+ use Illuminate \Database \Connection ;
56use Illuminate \Database \Schema \Grammars \Grammar ;
7+ use Illuminate \Foundation \Application ;
68use Illuminate \Support \Arr ;
79use SingleStore \Laravel \Schema \Blueprint as SingleStoreBlueprint ;
810
@@ -32,30 +34,23 @@ trait InlinesIndexes
3234 'spatialIndex ' ,
3335 ];
3436
35- /**
36- * The keys of the commands that are indexes
37- *
38- * @var int[]
39- */
40- protected $ indexCommandKeys = [];
41-
4237 /**
4338 * Given a set of statements from the `toSQL` method, inline all
4439 * of the indexes into the CREATE TABLE statement.
4540 *
4641 * @param array $statements
4742 * @return array
4843 */
49- protected function inlineCreateIndexStatements ($ statements )
44+ protected function inlineCreateIndexStatements ($ statements, $ indexStatementKeys )
5045 {
5146 // In the `addImpliedCommands` method we gathered up the keys of all the commands
5247 // that are index commands. Now that we're ready to compile the SQL we'll pull
5348 // all those statements out to sneak them into the CREATE TABLE statement.
54- $ indexStatements = Arr::only ($ statements , $ this -> indexCommandKeys );
49+ $ indexStatements = Arr::only ($ statements , $ indexStatementKeys );
5550
5651 // Since we're putting the index statements inside the CREATE TABLE statement,
5752 // we pull them out of the statement list so that they don't run as ALTERs.
58- Arr::forget ($ statements , $ this -> indexCommandKeys );
53+ Arr::forget ($ statements , $ indexStatementKeys );
5954
6055 $ search = SingleStoreBlueprint::INDEX_PLACEHOLDER ;
6156
@@ -72,37 +67,18 @@ protected function inlineCreateIndexStatements($statements)
7267 }
7368
7469 /**
75- * Get all of the index commands out of the blueprint's command queue .
70+ * Check if the command is index .
7671 *
7772 * @return \Illuminate\Support\Collection
7873 */
79- protected function indexCommands ( )
74+ protected function isIndexCommand ( $ command )
8075 {
81- return $ this -> commandsNamed ( array_merge (
76+ return in_array ( $ command -> name , array_merge (
8277 $ this ->singleStoreIndexes ,
8378 $ this ->mysqlIndexes
8479 ));
8580 }
8681
87- /**
88- * @param Grammar $grammar
89- * @return void
90- */
91- protected function addImpliedCommands (Grammar $ grammar )
92- {
93- parent ::addImpliedCommands ($ grammar );
94-
95- $ this ->addFluentSingleStoreIndexes ();
96-
97- if ($ this ->creating ()) {
98- // We have to pull the keys for the indexes during this method because once
99- // compiled, the primary key's `name` attribute is set to null, meaning
100- // that we can no longer tell what type of key it is. By hooking into
101- // the `addImpliedCommands` method, we access it before compilation.
102- $ this ->indexCommandKeys = $ this ->indexCommands ()->keys ()->toArray ();
103- }
104- }
105-
10682 /**
10783 * @return void
10884 */
@@ -132,4 +108,33 @@ protected function addFluentSingleStoreIndexes()
132108 }
133109 }
134110 }
111+
112+ public function toSql (Connection $ connection , Grammar $ grammar )
113+ {
114+ if (version_compare (Application::VERSION , '10.0.0 ' , '>= ' )) {
115+ $ this ->addImpliedCommands ($ connection , $ grammar );
116+ } else {
117+ $ this ->addImpliedCommands ($ grammar );
118+ }
119+ $ this ->addFluentSingleStoreIndexes ();
120+
121+ $ statements = [];
122+ $ indexStatementKeys = [];
123+
124+ foreach ($ this ->commands as $ command ) {
125+ $ method = 'compile ' .ucfirst ($ command ->name );
126+ $ isIndex = $ this ->isIndexCommand ($ command );
127+
128+ if (method_exists ($ grammar , $ method ) || $ grammar ::hasMacro ($ method )) {
129+ if (! is_null ($ sql = $ grammar ->$ method ($ this , $ command , $ connection ))) {
130+ $ statements = array_merge ($ statements , (array ) $ sql );
131+ if ($ isIndex ) {
132+ array_push ($ indexStatementKeys , count ($ statements ) - 1 );
133+ }
134+ }
135+ }
136+ }
137+
138+ return $ this ->creating () ? $ this ->inlineCreateIndexStatements ($ statements , $ indexStatementKeys ) : $ statements ;
139+ }
135140}
0 commit comments