1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\PostgresConnection; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use PDO; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\MariaDbGrammar; |
10
|
|
|
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\MySqlGrammar; |
11
|
|
|
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\PostgresGrammar; |
12
|
|
|
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\SingleStoreGrammar; |
13
|
|
|
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\SQLiteGrammar; |
14
|
|
|
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\SqlServerGrammar; |
15
|
|
|
|
16
|
|
|
trait BuildsAdjacencyListQueries |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Get the hydrated models without eager loading. |
20
|
|
|
* |
21
|
|
|
* @param array $columns |
22
|
|
|
* @return \Illuminate\Database\Eloquent\Model[] |
23
|
|
|
*/ |
24
|
1196 |
|
public function getModels($columns = ['*']) |
25
|
|
|
{ |
26
|
1196 |
|
$items = $this->query->get($columns)->all(); |
27
|
|
|
|
28
|
1196 |
|
if ($this->getConnection() instanceof PostgresConnection) { |
|
|
|
|
29
|
256 |
|
$path = $this->model->getPathName(); |
30
|
|
|
|
31
|
256 |
|
if (isset($items[0]->$path)) { |
32
|
116 |
|
$this->replacePathSeparator( |
33
|
116 |
|
$items, |
34
|
116 |
|
$path, |
35
|
116 |
|
$this->model->getPathSeparator() |
36
|
116 |
|
); |
37
|
|
|
|
38
|
116 |
|
foreach ($this->model->getCustomPaths() as $path) { |
39
|
115 |
|
$this->replacePathSeparator( |
40
|
115 |
|
$items, |
41
|
115 |
|
$path['name'], |
42
|
115 |
|
$path['separator'] |
43
|
115 |
|
); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
1196 |
|
$table = (new $this->model())->getTable(); |
49
|
|
|
|
50
|
1196 |
|
$models = $this->model->hydrate($items)->each->setTable($table); |
51
|
|
|
|
52
|
1196 |
|
return $models->all(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Replace the separator in a PostgreSQL path column. |
57
|
|
|
* |
58
|
|
|
* @param array $items |
59
|
|
|
* @param string $path |
60
|
|
|
* @param string $separator |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
116 |
|
protected function replacePathSeparator(array $items, $path, $separator) |
64
|
|
|
{ |
65
|
116 |
|
foreach ($items as $item) { |
66
|
116 |
|
$item->$path = str_replace( |
67
|
116 |
|
',', |
68
|
116 |
|
$separator, |
69
|
116 |
|
substr($item->$path, 1, -1) |
70
|
116 |
|
); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the expression grammar. |
76
|
|
|
* |
77
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Query\Grammars\ExpressionGrammar |
78
|
|
|
*/ |
79
|
1119 |
|
public function getExpressionGrammar() |
80
|
|
|
{ |
81
|
1119 |
|
$driver = $this->query->getConnection()->getDriverName(); |
82
|
|
|
|
83
|
|
|
switch ($driver) { |
84
|
1119 |
|
case 'mysql': |
85
|
412 |
|
$version = $this->query->getConnection()->getReadPdo()->getAttribute(PDO::ATTR_SERVER_VERSION); |
86
|
|
|
|
87
|
412 |
|
$grammar = Str::contains($version, 'MariaDB') |
88
|
169 |
|
? new MariaDbGrammar($this->model) |
89
|
243 |
|
: new MySqlGrammar($this->model); |
90
|
|
|
|
91
|
412 |
|
return $this->query->getConnection()->withTablePrefix($grammar); |
92
|
707 |
|
case 'pgsql': |
93
|
243 |
|
return $this->query->getConnection()->withTablePrefix( |
94
|
243 |
|
new PostgresGrammar($this->model) |
95
|
243 |
|
); |
96
|
464 |
|
case 'sqlite': |
97
|
242 |
|
return $this->query->getConnection()->withTablePrefix( |
98
|
242 |
|
new SQLiteGrammar($this->model) |
99
|
242 |
|
); |
100
|
222 |
|
case 'sqlsrv': |
101
|
158 |
|
return $this->query->getConnection()->withTablePrefix( |
102
|
158 |
|
new SqlServerGrammar($this->model) |
103
|
158 |
|
); |
104
|
64 |
|
case 'singlestore': |
105
|
64 |
|
return $this->query->getConnection()->withTablePrefix( |
106
|
64 |
|
new SingleStoreGrammar($this->model) |
107
|
64 |
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
throw new RuntimeException('This database is not supported.'); // @codeCoverageIgnore |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Register all passed global scopes. |
115
|
|
|
* |
116
|
|
|
* @param array $scopes |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
310 |
|
public function withGlobalScopes(array $scopes) |
120
|
|
|
{ |
121
|
310 |
|
foreach ($scopes as $identifier => $scope) { |
122
|
20 |
|
$this->withGlobalScope($identifier, $scope); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
310 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|