1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Join a table.
|
|
|
19 |
*
|
|
|
20 |
* @package block_dash
|
|
|
21 |
* @copyright 2020 bdecent gmbh <https://bdecent.de>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace block_dash\local\dash_framework\query_builder;
|
|
|
26 |
|
|
|
27 |
use block_dash\local\dash_framework\query_builder\exception\invalid_operator_exception;
|
|
|
28 |
use block_dash\local\dash_framework\query_builder\exception\invalid_where_clause_exception;
|
|
|
29 |
use coding_exception;
|
|
|
30 |
use dml_exception;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Join a table.
|
|
|
34 |
*
|
|
|
35 |
* @package block_dash
|
|
|
36 |
*/
|
|
|
37 |
class join {
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Inner JOIN query.
|
|
|
41 |
*/
|
|
|
42 |
const TYPE_INNER_JOIN = 'JOIN';
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* SQL Left Join.
|
|
|
46 |
*/
|
|
|
47 |
const TYPE_LEFT_JOIN = 'LEFT JOIN';
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* @var string Table name of joined table.
|
|
|
51 |
*/
|
|
|
52 |
private $table;
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* @var string Joined table alias.
|
|
|
56 |
*/
|
|
|
57 |
private $alias;
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* @var array
|
|
|
61 |
*/
|
|
|
62 |
private $joinconditions = [];
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* SQL join type. See self::TYPE_*
|
|
|
66 |
*
|
|
|
67 |
* @var string
|
|
|
68 |
*/
|
|
|
69 |
private $jointype;
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* @var array Extra paramters used in query build.
|
|
|
73 |
*/
|
|
|
74 |
private $extraparameters;
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Constructors
|
|
|
78 |
* @param string $table Table name of joined table.
|
|
|
79 |
* @param string $alias Joined table alias.
|
|
|
80 |
* @param string $jointablefield Field of joined table to reference in join condition.
|
|
|
81 |
* @param string $origintablefield Field of origin table to join to.
|
|
|
82 |
* @param string $jointype SQL join type. See self::TYPE_*
|
|
|
83 |
* @param array $extraparameters Extra parameters used in join SQL.
|
|
|
84 |
*/
|
|
|
85 |
public function __construct(string $table, string $alias, string $jointablefield, string $origintablefield,
|
|
|
86 |
$jointype = self::TYPE_INNER_JOIN, array $extraparameters = []) {
|
|
|
87 |
$this->table = $table;
|
|
|
88 |
$this->alias = $alias;
|
|
|
89 |
// Join table field.
|
|
|
90 |
if (!empty($jointablefield)) {
|
|
|
91 |
$this->joinconditions[] = sprintf('%s.%s = %s', $alias, $jointablefield, $origintablefield);
|
|
|
92 |
}
|
|
|
93 |
$this->jointype = $jointype;
|
|
|
94 |
$this->extraparameters = $extraparameters;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Get alias mentioned in query.
|
|
|
99 |
*
|
|
|
100 |
* @return string
|
|
|
101 |
*/
|
|
|
102 |
public function get_alias(): string {
|
|
|
103 |
return $this->alias;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Add additional raw join condition.
|
|
|
108 |
*
|
|
|
109 |
* @param string $condition
|
|
|
110 |
*/
|
|
|
111 |
public function add_join_condition(string $condition): void {
|
|
|
112 |
$this->joinconditions[] = $condition;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Get SQL and params for join.
|
|
|
117 |
*
|
|
|
118 |
* @return array<string, array>
|
|
|
119 |
*/
|
|
|
120 |
public function get_sql_and_params(): array {
|
|
|
121 |
$sql = sprintf('%s {%s} %s ON ', $this->jointype, $this->table, $this->alias);
|
|
|
122 |
$sql .= implode(' AND ', $this->joinconditions);
|
|
|
123 |
|
|
|
124 |
return [$sql, $this->extraparameters];
|
|
|
125 |
}
|
|
|
126 |
}
|