Skip to content

Commit

Permalink
Merge branch 'feature/binding'
Browse files Browse the repository at this point in the history
  • Loading branch information
cambell-prince committed Jul 28, 2020
2 parents f8de009 + bf05b8d commit 4cb65e8
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 18 deletions.
11 changes: 7 additions & 4 deletions src/DataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,16 @@ private function dynamicWrapper(callable $fn, $model = null)
}

/**
* @var $sql string SQL query
* @var string $sql string SQL query
* @var array $data array of values for bound parameters
* @return \PDOStatement
*/
public function query($sql)
public function query($sql, $data = null)
{
return $this->dynamicWrapper(function () use ($sql) {
return $this->pdo->query($sql);
return $this->dynamicWrapper(function () use ($sql, $data) {
$statement = $this->pdo->prepare($sql);
$statement->execute($data);
return $statement;
});
}

Expand Down
10 changes: 7 additions & 3 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

class QueryBuilder
{
public $boundData = null;

private $method;

private $instance;

private $sql;


public function __construct($creatable, \PDO $pdo = null)
{
if (is_string($creatable) && \class_exists($creatable)) {
Expand Down Expand Up @@ -47,10 +50,11 @@ private function ensureFrom()
}
}

public function where($sql)
public function where($sql, $data)
{
$this->ensureFrom();
$this->sql .= ' WHERE ' . $sql;
$this->boundData = $data;
return $this;
}

Expand All @@ -66,7 +70,7 @@ public function some()
$this->ensureFrom();
/** @var DataMapper */
$mapper = $this->instance->_mapper;
$result = $mapper->query($this->sql);
$result = $mapper->query($this->sql, $this->boundData);
while ($mapper->readRow($this->instance, $result)) {
yield $this->instance;
}
Expand All @@ -86,7 +90,7 @@ public function one()
/** @var DataMapper */
$mapper = $this->instance->_mapper;
$this->limit(1);
$result = $mapper->query($this->sql);
$result = $mapper->query($this->sql, $this->boundData);
$couldRead = $mapper->readRow($this->instance, $result);
if ($couldRead === false) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion test/anorm/DataMapper_Dynamic_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testFindOne_OK()
{
/** @var NotYetModel */
$model = DataMapper::find('NotYetModel', $this->pdo)
->where("`name`='Name 1'")
->where("`name`=:name", [':name' => 'Name 1'])
->one();
$this->assertTrue(true); // Just testing that we haven't yet thrown.
}
Expand Down
26 changes: 18 additions & 8 deletions test/anorm/DataMapper_Find_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_once(__DIR__ . '/../../vendor/autoload.php');

require_once(__DIR__ . '/SomeTableModel.php');
require_once(__DIR__ . '/TestEnvironment.php');

use PHPUnit\Framework\TestCase;

Expand All @@ -17,12 +18,12 @@ class DataMapperFindTest extends TestCase
public function __construct()
{
parent::__construct();
$this->pdo = new \PDO('mysql:host=localhost;dbname=anorm_test', 'travis', '');
$this->pdo = TestEnvironment::pdo();
}

public static function setUpBeforeClass()
{
$pdo = new \PDO('mysql:host=localhost;dbname=anorm_test', 'travis', '');
$pdo = TestEnvironment::pdo();
$pdo->query('DROP TABLE IF EXISTS `some_table`');
$sql = file_get_contents(__DIR__ . '/TestSchema.sql');
$pdo->query($sql);
Expand All @@ -36,11 +37,20 @@ public static function setUpBeforeClass()
}
}

// public function testFindOne_OK()
// {
// /** @var SomeTableModel */
// $model = DataMapper::find('SomeTableModel', $this->pdo)
// ->where("`name`='Name 1'")
// ->one();
// $this->assertEquals('Name 1', $model->name);
// }

public function testFindOne_OK()
{
/** @var SomeTableModel */
$model = DataMapper::find('SomeTableModel', $this->pdo)
->where("`name`='Name 1'")
->where("`name`=:name", [':name' => 'Name 1'])
->one();
$this->assertEquals('Name 1', $model->name);
}
Expand All @@ -49,8 +59,8 @@ public function testFindOneOrThrow_OK()
{
/** @var SomeTableModel */
$model = DataMapper::find('SomeTableModel', $this->pdo)
->where("`name`='Name 1'")
->oneOrThrow();
->where("`name`=:name", [':name' => 'Name 1'])
->oneOrThrow();
$this->assertEquals('Name 1', $model->name);
}

Expand All @@ -72,20 +82,20 @@ public function testFindOne_NotPresent_False()
{
/** @var SomeTableModel */
$model = DataMapper::find('SomeTableModel', $this->pdo)
->where("`name`='Bogus Name'")
->where("`name`=:name", [':name' => 'Bogus Name'])
->one();
$this->assertEquals(false, $model);
}

/**
* @expectedException \Exception
* @expectedExceptionMessage QueryBuilder: Expected one not found from 'SELECT * FROM `some_table` WHERE `name`='Bogus Name' LIMIT 1'
* @expectedExceptionMessage QueryBuilder: Expected one not found from 'SELECT * FROM `some_table` WHERE `name`=:name LIMIT 1'
*/
public function testFindOneOrThrow_NotPresent_Throws()
{
/** @var SomeTableModel */
$model = DataMapper::find('SomeTableModel', $this->pdo)
->where("`name`='Bogus Name'")
->where("`name`=:name", [':name' => 'Bogus Name'])
->oneOrThrow();
}

Expand Down
2 changes: 1 addition & 1 deletion test/anorm/QueryBuilder_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testFunctions_ReturnThis()
$this->assertSame($o, $result);
$result = $o->from('');
$this->assertSame($o, $result);
$result = $o->where('');
$result = $o->where('', []);
$this->assertSame($o, $result);
$result = $o->orderBy('');
$this->assertSame($o, $result);
Expand Down
2 changes: 2 additions & 0 deletions test/anorm/ReplaceTableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public function __construct(\PDO $pdo)
parent::__construct($pdo, DataMapper::createByClass($pdo, $this));
$this->_mapper->modelPrimaryKey = 'replaceId';
$this->_mapper->useReplace = true;
$this->dtc = null;
}

public function countRows()
Expand All @@ -19,5 +20,6 @@ public function countRows()

public $replaceId;
public $name;
public $dtc;
}

3 changes: 2 additions & 1 deletion test/anorm/TestReplaceSchema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# DROP TABLE `replace_table` IF EXISTS;
CREATE TABLE `replace_table` (
`replace_id` varchar(32) NOT NULL,
`name` varchar(128) NOT NULL
`name` varchar(128) NOT NULL,
`dtc` date NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `replace_table`
ADD UNIQUE KEY `replace_id` (`replace_id`);

0 comments on commit 4cb65e8

Please sign in to comment.