-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind.php
158 lines (135 loc) · 4.55 KB
/
Find.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
class Find extends Record {
private array $where = [];
private array $having = [];
private array $join = [];
private string $group = '';
private array $order = [];
private string $table;
private bool $isHaving = false;
private bool $isJoin = false;
private string $limit = '';
private string $groupCondition = 'AND';
private ?string $className = null;
public function __construct(string $table, string $className = null) {
$this->table = $table;
$this->className = $className;
parent::__construct();
}
public function getClass(): ?string {
return $this->className;
}
public function or(): self {
$this->groupCondition();
$this->groupCondition = 'OR';
return $this;
}
public function and(): self {
$this->groupCondition();
$this->groupCondition = 'AND';
return $this;
}
public function eq(string $field, $value): self {
return $this->addCondition($field, '=', $value);
}
public function lt(string $field, $value): self {
return $this->addCondition($field, '<', $value);
}
public function lte(string $field, $value): self {
return $this->addCondition($field, '<=', $value);
}
public function gt(string $field, $value): self {
return $this->addCondition($field, '>', $value);
}
public function gte(string $field, $value): self {
return $this->addCondition($field, '>=', $value);
}
public function llike(string $field, $value): self {
return $this->addCondition($field, ' LIKE ', '%' . $value);
}
public function rlike(string $field, $value): self {
return $this->addCondition($field, ' LIKE ', $value . '%');
}
public function like(string $field, $value): self {
return $this->addCondition($field, ' LIKE ', '%' . $value . '%');
}
public function limit(int $limit, int $offset = 0): self {
$this->limit = ' LIMIT ' . $offset . ', ' . $limit;
return $this;
}
public function having(): self {
if (!$this->isHaving) {
$this->groupCondition();
$this->isHaving = true;
}
return $this;
}
public function join(): self {
if (!$this->isJoin) {
$this->groupCondition();
$this->isJoin = true;
}
return $this;
}
public function findAndReplaceTableName($value) {
return preg_replace('/\*\.[`]?([A-Za-z_0-9]+)/', "`{$this->table}`.`$1`", $value);
}
public function group(array $groups): self {
if ($groups) {
$res = [];
foreach($groups as $group) {
$res[] = $this->findAndReplaceTableName($group);
}
$this->group = ' GROUP BY '.implode(', ', $res) .' ';
}
return $this;
}
/**
* @param string $field
* @return $this
*/
public function asc(string $field): self {
$this->order[] = $this->findAndReplaceTableName($field) . ' ASC';
return $this;
}
/**
* @param string $field
* @return $this
*/
public function desc(string $field): self {
$this->order[] = $this->findAndReplaceTableName($field) . ' ASC';
return $this;
}
public function __toString(): string
{
$this->groupCondition();
return ($this->join[0] ?? '')
. (isset($this->where[0]) ? " WHERE {$this->where[0]}" : '')
. $this->group
. ($this->having[0] ?? '')
. ($this->order ? ' ORDER BY ' . implode(', ', $this->order) : '')
. $this->limit;
}
private function addCondition(string $field, string $formula, $value): self {
$cond = $this->findAndReplaceTableName($field)
. $formula
. (is_numeric($value) ? $value : '"' . Record::escape($value) . '"');
if ($this->isHaving) {
$this->having[] = $cond;
} else if ($this->isJoin) {
$this->join[] = $cond;
} else {
$this->where[] = $cond;
}
return $this;
}
private function groupCondition(): void {
if ($this->isHaving && $this->having) {
$this->having = [ '(' . implode(" $this->groupCondition ", $this->having) . ')'];
} else if ($this->isJoin && $this->join) {
$this->join = [ '(' . implode(" $this->groupCondition ", $this->join) . ')'];
} else if ($this->where) {
$this->where = [ '(' . implode(" $this->groupCondition ", $this->where) . ')'];
}
}
}