Skip to content

Commit 0032caf

Browse files
committed
v1.3.0 - Add Limit in in Select Class
1 parent 53741aa commit 0032caf

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/classes/Select.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
2+
23
namespace DinoDev\MySql\Classes;
4+
35
use DinoDev\MySql\Classes\MySql;
46

57
class Select extends MySql
@@ -11,28 +13,37 @@ public function __construct(MySql $_MySql)
1113
$this->MySql = $_MySql;
1214
}
1315

14-
public function All(string $table)
16+
public function All(string $table, int $limit = null)
1517
{
16-
return $this->MySql->queryAndFetch("SELECT * FROM $table");
18+
$sql = "SELECT * FROM $table";
19+
$sql .= $limit ? " LIMIT $limit" : "";
20+
21+
return $this->MySql->queryAndFetch($sql);
1722
}
1823

19-
public function Where(string $table, string $field, $value)
24+
public function Where(string $table, string $field, $value, int $limit = null)
2025
{
2126
$valueCorrection = gettype($value) == "string" ? "'$value'" : $value;
2227

23-
return $this->MySql->queryAndFetch("SELECT * FROM $table WHERE $field = $valueCorrection");
28+
$sql = "SELECT * FROM $table WHERE $field = $valueCorrection";
29+
$sql .= $limit ? " LIMIT $limit" : "";
30+
31+
return $this->MySql->queryAndFetch($sql);
2432
}
2533

26-
public function Like(string $table, string $field, $value)
34+
public function Like(string $table, string $field, $value, int $limit = null)
2735
{
2836
$valueType = gettype($value);
2937

3038
if ($valueType == "string") {
31-
return $this->MySql->queryAndFetch("SELECT * FROM $table WHERE $field LIKE '%$value%'");
39+
$sql = "SELECT * FROM $table WHERE $field LIKE '%$value%'";
40+
$sql .= $limit ? " LIMIT $limit" : "";
41+
42+
return $this->MySql->queryAndFetch($sql);
3243
} else {
3344
$message = "Value type is incorrect. Value has to be an String. Value type: " . $valueType;
3445

3546
throw new \RuntimeException($message);
3647
}
3748
}
38-
}
49+
}

tests/SelectTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public function testAll()
2323
{
2424
$this->assertIsArray($this->Select->All("country"));
2525
$this->assertFalse($this->Select->All("NoTable"));
26+
27+
$this->assertCount(5, $this->Select->All("country", 5));
2628
}
2729

2830
public function testWhere()
@@ -31,13 +33,17 @@ public function testWhere()
3133
$this->assertEmpty($this->Select->Where("country", "Name", "Brasil"));
3234

3335
$this->assertFalse($this->Select->Where("NoTable", "Name", "Brazil"));
36+
37+
$this->assertCount(5, $this->Select->Where("country", "Region", "South America", 5));
3438
}
3539

3640
public function testLike()
3741
{
3842
$this->assertIsArray($this->Select->Like("country", "Name", "Braz"));
3943
$this->assertEmpty($this->Select->Like("country", "Name", "Bras"));
4044

41-
$this->assertFalse($this->Select->Like("NoTable", "Name", "Braz"));
45+
$this->assertFalse($this->Select->Like("NoTable", "Name", "Braz"));
46+
47+
$this->assertCount(5, $this->Select->Like("country", "Region", "th Ame", 5));
4248
}
4349
}

0 commit comments

Comments
 (0)