2
2
3
3
namespace SingleStore \Laravel \Schema \Blueprint ;
4
4
5
+ use Illuminate \Database \Connection ;
5
6
use Illuminate \Database \Schema \Grammars \Grammar ;
7
+ use Illuminate \Foundation \Application ;
6
8
use Illuminate \Support \Arr ;
7
9
use SingleStore \Laravel \Schema \Blueprint as SingleStoreBlueprint ;
8
10
@@ -32,34 +34,27 @@ trait InlinesIndexes
32
34
'spatialIndex ' ,
33
35
];
34
36
35
- /**
36
- * The keys of the commands that are indexes
37
- *
38
- * @var int[]
39
- */
40
- protected $ indexCommandKeys = [];
41
-
42
37
/**
43
38
* Given a set of statements from the `toSQL` method, inline all
44
39
* of the indexes into the CREATE TABLE statement.
45
40
*
46
41
* @param array $statements
47
42
* @return array
48
43
*/
49
- protected function inlineCreateIndexStatements ($ statements )
44
+ protected function inlineCreateIndexStatements ($ statements, $ indexStatementKeys )
50
45
{
51
46
// In the `addImpliedCommands` method we gathered up the keys of all the commands
52
47
// that are index commands. Now that we're ready to compile the SQL we'll pull
53
48
// 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 );
55
50
56
51
// Since we're putting the index statements inside the CREATE TABLE statement,
57
52
// 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 );
59
54
60
55
$ search = SingleStoreBlueprint::INDEX_PLACEHOLDER ;
61
56
62
- if (! $ indexStatements ) {
57
+ if (!$ indexStatements ) {
63
58
// If there are no index statements at all, we need to replace the preceding comma as well.
64
59
$ search = ", $ search " ;
65
60
}
@@ -72,37 +67,18 @@ protected function inlineCreateIndexStatements($statements)
72
67
}
73
68
74
69
/**
75
- * Get all of the index commands out of the blueprint's command queue .
70
+ * Check if the command is index .
76
71
*
77
72
* @return \Illuminate\Support\Collection
78
73
*/
79
- protected function indexCommands ( )
74
+ protected function isIndexCommand ( $ command )
80
75
{
81
- return $ this -> commandsNamed ( array_merge (
76
+ return in_array ( $ command -> name , array_merge (
82
77
$ this ->singleStoreIndexes ,
83
78
$ this ->mysqlIndexes
84
79
));
85
80
}
86
81
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
-
106
82
/**
107
83
* @return void
108
84
*/
@@ -132,4 +108,33 @@ protected function addFluentSingleStoreIndexes()
132
108
}
133
109
}
134
110
}
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
+ }
135
140
}
0 commit comments