Skip to content

Commit

Permalink
Merge pull request #50 from sroehrl/sqlLite
Browse files Browse the repository at this point in the history
SqLiteAdapter.php equipped with $callfunction capability
  • Loading branch information
sroehrl authored Oct 28, 2022
2 parents 2f6570d + 3bef17d commit 7ef5b62
Show file tree
Hide file tree
Showing 5 changed files with 464 additions and 366 deletions.
29 changes: 28 additions & 1 deletion src/Database/SqLiteAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class SqLiteAdapter implements Adapter
private PDO $db;
private NeoanSQLTranslator $translator;
private array $rawSubstitutions;
private array $callFunctions = ['orderBy'=>[],'limit'=>[]];

public function __construct($credentials = ['location' => __DIR__ . '/database.db'])
{
Expand All @@ -27,12 +28,38 @@ public function easy(string $selectorString, array $conditions = [], mixed $extr
$sql .= " WHERE " . $this->translator->whereString;
}
$this->rawSubstitutions = array_values($conditions);
$result = $this->execute($sql, array_values($conditions));
$this->addCallFunctions($extra);
$result = $this->execute($sql);
return $result->fetchAll(PDO::FETCH_ASSOC);
}
private function addCallFunctions(?array $extra): void
{

foreach ($this->callFunctions as $key => $set){
if(isset($extra[$key])){
$this->callFunctions[$key] = $extra[$key];
}
}

}
private function orderBy($set): string
{
return " ORDER BY $set[0] $set[1]";
}
private function limit($set): string
{
return " LIMIT $set[0], $set[1]";
}

private function execute($sql): PDOStatement
{
foreach ($this->callFunctions as $key => $set){
if(!empty($set)){
$sql .= $this->{$key}($set);
}
}
// reset
$this->callFunctions = ['orderBy'=>[],'limit'=>[]];
$exec = $this->db->prepare($sql);
if (empty($this->rawSubstitutions)) {
$exec->execute();
Expand Down
15 changes: 15 additions & 0 deletions tests/Database/SqLiteAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,19 @@ function testMethods()
$this->assertEmpty($hard);

}
function testCallFunctions()
{
// clear db
Database::raw('DELETE FROM test_me');
// insert 3 rows
foreach(['one','two','three'] as $some){
Database::insert('test_me', ['some' => $some]);
}
// test limit
$call = Database::easy('test_me.*',[],['limit'=> [0,1]]);
$this->assertSame(1, count($call));
// test orderBy
$call = Database::easy('test_me.*',[],['orderBy'=> ['id','desc']]);
$this->assertTrue($call[0]['id'] > $call[1]['id']);
}
}
Binary file modified tests/Mocks/database.db
Binary file not shown.
41 changes: 41 additions & 0 deletions tests/Model/PaginateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Test\Model;

use Neoan\Database\Database;
use Neoan\Model\Collection;
use Neoan\Model\Paginate;
use PHPUnit\Framework\TestCase;
use Test\Mocks\DatabaseTestAdapter;
use Test\Mocks\MockModel;

class PaginateTest extends TestCase
{
protected function setUp(): void
{
Database::connect(new DatabaseTestAdapter());
$randomUser = 'heinz-' . microtime(true);
$model = new MockModel([
'userName' => $randomUser,
'email' => '[email protected]',
'password' => '123123'
]);
$model->store();

}
protected function tearDown(): void
{
Database::raw('DELETE FROM `mock`');
}
public function testPage()
{
$pagination = new Paginate(1,20, MockModel::class);
$test = $pagination->where(['id'=>1])
->descending('id')
->ascending('id')
->get();
$this->assertSame(1, $pagination->getPage());
$this->assertSame(20, $pagination->getPageSize());
$this->assertInstanceOf(Collection::class, $test['collection']);
}
}
Loading

0 comments on commit 7ef5b62

Please sign in to comment.