| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 3.1.9 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2026 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Analytics\QueryBuilders; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class MetricQueryBuilder |
| 20 |
{ |
| 21 |
private $select = ''; |
| 22 |
private $from = ''; |
| 23 |
private $joins = ''; |
| 24 |
private $where = []; |
| 25 |
private $groupBy = ''; |
| 26 |
private $having = ''; |
| 27 |
private $orderBy = ''; |
| 28 |
private $limit = ''; |
| 29 |
private $offset = ''; |
| 30 |
private $placeholders = []; |
| 31 |
|
| 32 |
public function select(string $select): self |
| 33 |
{ |
| 34 |
$this->select = $select; |
| 35 |
return $this; |
| 36 |
} |
| 37 |
|
| 38 |
public function from(string $from): self |
| 39 |
{ |
| 40 |
$this->from = $from; |
| 41 |
return $this; |
| 42 |
} |
| 43 |
|
| 44 |
public function join(string $join): self |
| 45 |
{ |
| 46 |
$this->joins .= ' ' . $join; |
| 47 |
return $this; |
| 48 |
} |
| 49 |
|
| 50 |
public function where(string $condition): self |
| 51 |
{ |
| 52 |
$this->where[] = $condition; |
| 53 |
return $this; |
| 54 |
} |
| 55 |
|
| 56 |
public function groupBy(string $groupBy): self |
| 57 |
{ |
| 58 |
$this->groupBy = $groupBy; |
| 59 |
return $this; |
| 60 |
} |
| 61 |
|
| 62 |
public function orderBy(string $orderBy): self |
| 63 |
{ |
| 64 |
$this->orderBy = $orderBy; |
| 65 |
return $this; |
| 66 |
} |
| 67 |
|
| 68 |
public function limit(int $limit): self |
| 69 |
{ |
| 70 |
$this->limit = "LIMIT {$limit}"; |
| 71 |
return $this; |
| 72 |
} |
| 73 |
|
| 74 |
public function offset(int $offset): self |
| 75 |
{ |
| 76 |
$this->offset = "OFFSET {$offset}"; |
| 77 |
return $this; |
| 78 |
} |
| 79 |
|
| 80 |
public function addPlaceholder($value): self |
| 81 |
{ |
| 82 |
$this->placeholders[] = $value; |
| 83 |
return $this; |
| 84 |
} |
| 85 |
|
| 86 |
public function toSQL(): string |
| 87 |
{ |
| 88 |
$sql = "SELECT {$this->select}"; |
| 89 |
$sql .= " FROM {$this->from}"; |
| 90 |
$sql .= $this->joins; |
| 91 |
|
| 92 |
if (!empty($this->where)) { |
| 93 |
$sql .= " WHERE " . implode(' AND ', $this->where); |
| 94 |
} |
| 95 |
|
| 96 |
if ($this->groupBy) { |
| 97 |
$sql .= " {$this->groupBy}"; |
| 98 |
} |
| 99 |
|
| 100 |
if ($this->having) { |
| 101 |
$sql .= " {$this->having}"; |
| 102 |
} |
| 103 |
|
| 104 |
if ($this->orderBy) { |
| 105 |
$sql .= " {$this->orderBy}"; |
| 106 |
} |
| 107 |
|
| 108 |
if ($this->limit) { |
| 109 |
$sql .= " {$this->limit}"; |
| 110 |
} |
| 111 |
|
| 112 |
if ($this->offset) { |
| 113 |
$sql .= " {$this->offset}"; |
| 114 |
} |
| 115 |
|
| 116 |
return $sql; |
| 117 |
} |
| 118 |
|
| 119 |
public function getPlaceholders(): array |
| 120 |
{ |
| 121 |
return $this->placeholders; |
| 122 |
} |
| 123 |
} |
| 124 |
|