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