1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\LaravelAdjacencyList\Eloquent; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder as Base; |
6
|
|
|
use Illuminate\Database\PostgresConnection; |
7
|
|
|
use RuntimeException; |
8
|
|
|
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\MySqlGrammar; |
9
|
|
|
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\PostgresGrammar; |
10
|
|
|
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\SQLiteGrammar; |
11
|
|
|
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\SqlServerGrammar; |
12
|
|
|
|
13
|
|
|
class Builder extends Base |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Get the hydrated models without eager loading. |
17
|
|
|
* |
18
|
|
|
* @param array $columns |
19
|
|
|
* @return \Illuminate\Database\Eloquent\Model[] |
20
|
|
|
*/ |
21
|
278 |
|
public function getModels($columns = ['*']) |
22
|
|
|
{ |
23
|
278 |
|
$items = $this->query->get($columns)->all(); |
24
|
|
|
|
25
|
278 |
|
if ($this->getConnection() instanceof PostgresConnection) { |
|
|
|
|
26
|
73 |
|
$path = $this->model->getPathName(); |
27
|
|
|
|
28
|
73 |
|
if (isset($items[0]->$path)) { |
29
|
35 |
|
$this->replacePathSeparator( |
30
|
35 |
|
$items, |
31
|
|
|
$path, |
|
|
|
|
32
|
35 |
|
$this->model->getPathSeparator() |
|
|
|
|
33
|
|
|
); |
34
|
|
|
|
35
|
35 |
|
foreach ($this->model->getCustomPaths() as $path) { |
36
|
35 |
|
$this->replacePathSeparator( |
37
|
35 |
|
$items, |
38
|
35 |
|
$path['name'], |
39
|
35 |
|
$path['separator'] |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
278 |
|
$table = (new $this->model)->getTable(); |
46
|
|
|
|
47
|
278 |
|
$models = $this->model->hydrate($items)->each->setTable($table); |
48
|
|
|
|
49
|
278 |
|
return $models->all(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Replace the separator in a PostgreSQL path column. |
54
|
|
|
* |
55
|
|
|
* @param array $items |
56
|
|
|
* @param string $path |
57
|
|
|
* @param string $separator |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
35 |
|
protected function replacePathSeparator(array $items, $path, $separator) |
61
|
|
|
{ |
62
|
35 |
|
foreach ($items as $item) { |
63
|
35 |
|
$item->$path = str_replace( |
64
|
35 |
|
',', |
65
|
|
|
$separator, |
66
|
35 |
|
substr($item->$path, 1, -1) |
67
|
|
|
); |
68
|
|
|
} |
69
|
35 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the expression grammar. |
73
|
|
|
* |
74
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Query\Grammars\ExpressionGrammar |
75
|
|
|
*/ |
76
|
214 |
|
public function getExpressionGrammar() |
77
|
|
|
{ |
78
|
214 |
|
$driver = $this->query->getConnection()->getDriverName(); |
79
|
|
|
|
80
|
214 |
|
switch ($driver) { |
81
|
214 |
|
case 'mysql': |
82
|
57 |
|
return $this->query->getConnection()->withTablePrefix(new MySqlGrammar); |
83
|
157 |
|
case 'pgsql': |
84
|
57 |
|
return $this->query->getConnection()->withTablePrefix(new PostgresGrammar); |
85
|
100 |
|
case 'sqlite': |
86
|
57 |
|
return $this->query->getConnection()->withTablePrefix(new SQLiteGrammar); |
87
|
43 |
|
case 'sqlsrv': |
88
|
43 |
|
return $this->query->getConnection()->withTablePrefix(new SqlServerGrammar); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
throw new RuntimeException('This database is not supported.'); // @codeCoverageIgnore |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|