Skip to content

Commit fcffc39

Browse files
author
Wazabii
committed
Added functionality
1 parent 1a21e11 commit fcffc39

File tree

3 files changed

+111
-36
lines changed

3 files changed

+111
-36
lines changed

AbstractDB.php

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ abstract class AbstractDB implements DBInterface
2727
protected $mig;
2828
protected $compare = "=";
2929
protected $whereAnd = "AND";
30+
protected $whereNot = false;
3031
protected $whereIndex = 0;
3132
protected $whereProtocol = [];
3233
protected $fkData;
@@ -215,10 +216,49 @@ final protected function setWhereData(string|AttrInterface $key, string|int|floa
215216
throw new DBValidationException($this->mig->getMessage(), 1);
216217
}
217218

218-
$data[$this->whereIndex][$this->whereAnd][$this->compare][$key][] = $val;
219+
//$data[$this->whereIndex][$this->whereAnd][$this->compare][$key][] = $val;
220+
$data[$this->whereIndex][$this->whereAnd][$key][] = [
221+
"not" => $this->whereNot,
222+
"operator" => $this->compare,
223+
"value" => $val
224+
];
225+
219226
$this->whereProtocol[$key][] = $val;
220227
$this->resetWhere();
221228
}
229+
230+
/**
231+
* Build Where data
232+
* @param array $array
233+
* @return string
234+
*/
235+
final protected function whereArrToStr(array $array): string
236+
{
237+
$out = "";
238+
$count = 0;
239+
foreach ($array as $key => $arr) {
240+
foreach ($arr as $col => $a) {
241+
if (is_array($a)) {
242+
foreach ($a as $int => $row) {
243+
if ($count > 0) {
244+
$out .= "{$key} ";
245+
}
246+
if ($row['not'] === true) {
247+
$out .= "NOT ";
248+
}
249+
$out .= "{$col} {$row['operator']} {$row['value']} ";
250+
$count++;
251+
}
252+
253+
} else {
254+
$out .= "{$key} {$a} ";
255+
$count++;
256+
}
257+
}
258+
}
259+
260+
return $out;
261+
}
222262

223263
/**
224264
* Get the Main FK data protocol
@@ -308,37 +348,6 @@ final protected function extractCamelCase(string $value): array
308348
return $arr;
309349
}
310350

311-
/**
312-
* Build Where data
313-
* @param array $array
314-
* @return string
315-
*/
316-
final protected function whereArrToStr(array $array): string
317-
{
318-
$out = "";
319-
$count = 0;
320-
foreach ($array as $key => $arr) {
321-
foreach ($arr as $operator => $a) {
322-
if (is_array($a)) {
323-
foreach ($a as $col => $b) {
324-
foreach ($b as $val) {
325-
if ($count > 0) {
326-
$out .= "{$key} ";
327-
}
328-
$out .= "{$col} {$operator} {$val} ";
329-
$count++;
330-
}
331-
}
332-
} else {
333-
$out .= "{$key} {$a} ";
334-
$count++;
335-
}
336-
}
337-
}
338-
339-
return $out;
340-
}
341-
342351
/**
343352
* Build join data from Migrate data
344353
* @param MigrateInterface $mig

DB.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,16 @@ public function or(): self
313313
return $this;
314314
}
315315

316+
/**
317+
* Chaining with where "NOT"
318+
* @return self
319+
*/
320+
public function not(): self
321+
{
322+
$this->whereNot = true;
323+
return $this;
324+
}
325+
316326
/**
317327
* Raw Mysql Where input
318328
* Uses vsprintf to mysql prep/protect input in string. Prep string values needs to be eclosed manually

README.md

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
12
# MaplePHP - MySQL queries
23
MaplePHP - MySQL queries is a powerful yet **user-friendly** library for making **safe** database queries.
34

4-
The guide is not complete; more content will be added soon.
5-
5+
### Contents
6+
- [Connect to the database](#connect-to-the-database)
7+
- [Make queries](#make-queries)
8+
- [Attributes](#attributes)
9+
- *Migrations (Coming soon)*
610

711
## Connect to the database
812

@@ -41,11 +45,14 @@ $select->where("id", 1); // id = '1'
4145
$select->where("parent", 0, ">"); // parent > '1'
4246
```
4347
### Where 2
48+
"compare", "or"/"and" and "not".
4449
```php
4550
$select->whereRoleStatusParent(1, 1, 0);
4651
// role = '1' AND status = '1' AND Parent = 0
4752
$select->compare(">")->whereStatus(0)->or()->whereRole(1);
4853
// status > '0' OR role = '1'
54+
$select->not()->whereId(1)->whereEmail("john.doe@gmail.com");
55+
// NOT id = '1' AND email = 'john.doe@gmail.com'
4956
```
5057
### Where 3
5158
```php
@@ -63,7 +70,7 @@ $select->whereRaw("status = %d AND visible = %d", [1, 1]);
6370
// PROTECTED: status = 1 AND visible = 1
6471
```
6572
### Having
66-
Having command works the same as where command above with exception that you rename "where" in method to "having" and @havingBind do not exist.
73+
Having command works the same as where command above with exception that you rename "where" method to "having" and that the method "havingBind" do not exist.
6774
```php
6875
$select->having("id", 1); // id = '1'
6976
$select->having("parent", 0, ">"); // parent > '1'
@@ -88,7 +95,7 @@ $select->order("id", "ASC")->order("parent", "DESC");
8895
$select->orderRaw("id ASC, parent DESC");
8996
// ORDER BY id ASC, parent DESC
9097
```
91-
### Limit
98+
### Join
9299
```php
93100
$select->join("tableName", "b.user_id = a.id"); // Default INNER join
94101
$select->join("tableName", "b.user_id = '%d'", [872], "LEFT"); // PROTECTED INPUT
@@ -134,3 +141,52 @@ $select->setRaw("msg_id", "UUID()");
134141
```php
135142
echo $select->sql();
136143
```
144+
145+
## Attributes
146+
Each value is automatically escaped by default in the most effective manner to ensure consequential and secure data storage, guarding against SQL injection vulnerabilities. While it's possible to exert complete control over SQL input using various **Raw** methods, such an approach is not advisable due to the potential for mistakes that could introduce vulnerabilities. A safer alternative is to leverage the **Attr** class. The **Attr** class offers comprehensive configuration capabilities for nearly every value in the DB library, as illustrated below:
147+
```php
148+
$idValue = DB::withAttr("1")
149+
->prep(true)
150+
->enclose(true)
151+
->encode(true)
152+
->jsonEncode(true);
153+
154+
$select->where("id", $idValue);
155+
```
156+
#### Escape values and protect against SQL injections
157+
```php
158+
public function prep(bool $prep): self;
159+
```
160+
**Example:**
161+
- Input value: Lorem "ipsum" dolor
162+
- Output value: Lorem \\"ipsum\\" dolor
163+
164+
#### Enable/disable string enclose
165+
```php
166+
public function enclose(bool $enclose): self;
167+
```
168+
**Example:**
169+
- Input value: 1186
170+
- Output value: '1186'
171+
*E.g. will add or remove quotes to values*
172+
173+
#### Enable/disable XSS protection
174+
Some like to have the all the database data already HTML special character escaped.
175+
```php
176+
public function encode(bool $encode): self;
177+
```
178+
**Example:**
179+
- Input value: Lorem <strong>ipsum</strong> dolor
180+
- Output value: Lorem \<strong\>ipsum\</strong\> dolor
181+
182+
#### Automatically json encode array data
183+
A pragmatic function that will automatically encode all array input data to a json string
184+
```php
185+
public function jsonEncode(bool $jsonEncode): self;
186+
```
187+
**Example:**
188+
- Input value: array("firstname" => "John", "lastname" => "Doe");
189+
- Output value: {"firstname":"John","lastname":"Doe"}
190+
191+
The default values vary based on whether it is a table column, a condition in a WHERE clause, or a value to be set. For instance, columns default to enclose set to false, whereas for WHERE or SET inputs, it defaults to true. Regardless, every value defaults to **prep**, **encode** and **jsonEncode** being set to **true**.
192+

0 commit comments

Comments
 (0)