1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
5
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
6
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
7
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
8
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
9
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
10
|
|
|
* THE SOFTWARE. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Ytake\LaravelCouchbase\Schema; |
14
|
|
|
|
15
|
|
|
use Ytake\LaravelCouchbase\Database\CouchbaseConnection; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Blueprint |
19
|
|
|
* |
20
|
|
|
* @author Yuuki Takezawa<[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Blueprint extends \Illuminate\Database\Schema\Blueprint |
23
|
|
|
{ |
24
|
|
|
use NotSupportedTrait; |
25
|
|
|
|
26
|
|
|
/** @var CouchbaseConnection */ |
27
|
|
|
protected $connection; |
28
|
|
|
|
29
|
|
|
/** @var string[] */ |
30
|
|
|
protected $options = [ |
31
|
|
|
'bucketType' => 'couchbase', |
32
|
|
|
'saslPassword' => '', |
33
|
|
|
'flushEnabled' => true, |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param CouchbaseConnection $connection |
38
|
|
|
*/ |
39
|
2 |
|
public function connector(CouchbaseConnection $connection) |
40
|
|
|
{ |
41
|
2 |
|
$this->connection = $connection; |
42
|
2 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param array $options |
46
|
|
|
*/ |
47
|
|
|
public function setOptions(array $options) |
48
|
|
|
{ |
49
|
|
|
$this->options = array_merge($this->options, $options); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
2 |
|
public function create() |
56
|
|
|
{ |
57
|
2 |
|
return $this->connection->manager()->createBucket($this->table, $this->options); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function drop() |
64
|
|
|
{ |
65
|
|
|
return $this->connection->manager()->removeBucket($this->table); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* drop for N1QL primary index |
70
|
|
|
* |
71
|
|
|
* @param string $index |
72
|
|
|
* @param bool $ignoreIfNotExist |
73
|
|
|
* |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
1 |
|
public function dropPrimary($index = null, $ignoreIfNotExist = false) |
77
|
|
|
{ |
78
|
1 |
|
return $this->connection->openBucket($this->getTable()) |
79
|
1 |
|
->manager()->dropN1qlPrimaryIndex($this->detectIndexName($index), $ignoreIfNotExist); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* drop for N1QL secondary index |
84
|
|
|
* |
85
|
|
|
* @param string $index |
86
|
|
|
* @param bool $ignoreIfNotExist |
87
|
|
|
* |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
1 |
|
public function dropIndex($index, $ignoreIfNotExist = false) |
91
|
|
|
{ |
92
|
1 |
|
return $this->connection->openBucket($this->getTable()) |
93
|
1 |
|
->manager()->dropN1qlIndex($index, $ignoreIfNotExist); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Specify the primary index for the current bucket. |
98
|
|
|
* |
99
|
|
|
* @param string|null $name |
100
|
|
|
* @param boolean $ignoreIfExist if a primary index already exists, an exception will be thrown unless this is |
101
|
|
|
* set to true. |
102
|
|
|
* @param boolean $defer true to defer building of the index until buildN1qlDeferredIndexes()}is |
103
|
|
|
* called (or a direct call to the corresponding query service API). |
104
|
|
|
* |
105
|
|
|
* @return mixed |
106
|
|
|
*/ |
107
|
2 |
|
public function primaryIndex($name = null, $ignoreIfExist = false, $defer = false) |
108
|
|
|
{ |
109
|
2 |
|
return $this->connection->openBucket($this->getTable()) |
110
|
2 |
|
->manager()->createN1qlPrimaryIndex( |
111
|
2 |
|
$index = $this->detectIndexName($name), |
112
|
|
|
$ignoreIfExist, |
113
|
|
|
$defer |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Specify a secondary index for the current bucket. |
119
|
|
|
* |
120
|
|
|
* @param array $columns the JSON fields to index. |
121
|
|
|
* @param string $name the name of the index. |
122
|
|
|
* @param string $whereClause the WHERE clause of the index. |
123
|
|
|
* @param boolean $ignoreIfExist if a secondary index already exists with that name, an exception will be |
124
|
|
|
* thrown unless this is set to true. |
125
|
|
|
* @param boolean $defer true to defer building of the index until buildN1qlDeferredIndexes() is |
126
|
|
|
* called (or a direct call to the corresponding query service API). |
127
|
|
|
* |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
2 |
|
public function index($columns, $name = null, $whereClause = '', $ignoreIfExist = false, $defer = false) |
131
|
|
|
{ |
132
|
2 |
|
$name = (is_null($name)) ? $this->getTable() . "_secondary_index" : $name; |
133
|
|
|
|
134
|
2 |
|
return $this->connection->openBucket($this->getTable()) |
135
|
2 |
|
->manager()->createN1qlIndex( |
136
|
|
|
$name, |
137
|
|
|
$columns, |
138
|
|
|
$whereClause, |
139
|
|
|
$ignoreIfExist, |
140
|
|
|
$defer |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get the table the blueprint describes. |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
2 |
|
public function getTable() |
150
|
|
|
{ |
151
|
2 |
|
return $this->table; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param $index |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
2 |
|
protected function detectIndexName($index):string |
160
|
|
|
{ |
161
|
2 |
|
$index = (is_null($index)) ? "" : $index; |
162
|
|
|
|
163
|
2 |
|
return $index; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..