Skip to content
Snippets Groups Projects
Commit 2374ba3c authored by Josh Pollock's avatar Josh Pollock
Browse files

delete by entry ids

parent 2409bd5d
No related branches found
No related tags found
No related merge requests found
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
namespace calderawp\CalderaFormsQuery\Tests\Integration\Delete; namespace calderawp\CalderaFormsQuery\Tests\Integration\Delete;
use calderawp\CalderaFormsQuery\Delete\Entry;
use calderawp\CalderaFormsQuery\Tests\Integration\IntegrationTestCase; use calderawp\CalderaFormsQuery\Tests\Integration\IntegrationTestCase;
class EntryTest extends IntegrationTestCase class EntryTest extends IntegrationTestCase
...@@ -159,7 +160,41 @@ class EntryTest extends IntegrationTestCase ...@@ -159,7 +160,41 @@ class EntryTest extends IntegrationTestCase
) )
)); ));
}
/**
* Test querying by IDs
*
* @covers Entry::deleteByEntryIds()
*/
public function testByEntryIds()
{
$entryIdOne = $this->createEntryWithMockFormAndGetEntryId();
$entryIdTwo = $this->createEntryWithMockFormAndGetEntryId();
$entryIdThree = $this->createEntryWithMockFormAndGetEntryId();
//Delete results IN One and Three
$entryGenerator = $this->entryDeleteGeneratorFactory();
$sql = $entryGenerator->deleteByEntryIds( [
$entryIdOne,
$entryIdThree
])
->getPreparedSql();
$this->queryWithWPDB($sql);
//Query for entry Two expect 1 result
$entryGenerator = $this->entryGeneratorFactory();
$sql = $entryGenerator->queryByEntryId($entryIdTwo)
->getPreparedSql();
$results = $this->queryWithWPDB( $sql );
$this->assertSame( 1, count( $results ) );
//Query for entry One expect 0 result
$entryGenerator = $this->entryGeneratorFactory();
$sql = $entryGenerator->queryByEntryId($entryIdOne)
->getPreparedSql();
$results = $this->queryWithWPDB( $sql );
$this->assertSame( 0, count( $results ) );
} }
......
...@@ -28,6 +28,17 @@ class Entry extends DeleteQueryBuilder ...@@ -28,6 +28,17 @@ class Entry extends DeleteQueryBuilder
return $this->is('id', $entryId); return $this->is('id', $entryId);
} }
/**
* Delete an array of entries
*
* @param array $entryIds
* @return $this
*/
public function deleteByEntryIds(array $entryIds)
{
return $this->in($entryIds);
}
/** /**
* Delete entries belonging to a specific user ID * Delete entries belonging to a specific user ID
* *
......
<?php
namespace calderawp\CalderaFormsQuery\Exceptions;
class Exception extends \Exception
{
}
\ No newline at end of file
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
namespace calderawp\CalderaFormsQuery; namespace calderawp\CalderaFormsQuery;
use calderawp\CalderaFormsQuery\Select\DoesSelectQuery; use calderawp\CalderaFormsQuery\Exceptions\Exception;
use NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery; use NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery;
/** /**
...@@ -94,12 +94,17 @@ abstract class QueryBuilder implements CreatesSqlQueries ...@@ -94,12 +94,17 @@ abstract class QueryBuilder implements CreatesSqlQueries
* *
* @param string $sql SQL query with substitutions * @param string $sql SQL query with substitutions
* @return string * @return string
* @throws Exception
*/ */
protected function substituteValues($sql) protected function substituteValues($sql)
{ {
$values = $this->getBuilder()->getValues(); $values = $this->getBuilder()->getValues();
foreach ($values as $identifier => $value) { foreach ($values as $identifier => $value) {
$values[$identifier] = $this->surroundValue($value); if (is_array( $value ) || is_object( $value ) ) {
continue;
} else {
$values[$identifier] = $this->surroundValue($value);
}
} }
return str_replace(array_keys($values), array_values($values), $sql); return str_replace(array_keys($values), array_values($values), $sql);
} }
...@@ -153,4 +158,19 @@ abstract class QueryBuilder implements CreatesSqlQueries ...@@ -153,4 +158,19 @@ abstract class QueryBuilder implements CreatesSqlQueries
{ {
return $this->substituteValues($this->getBuilder()->write($this->getCurrentQuery())); return $this->substituteValues($this->getBuilder()->write($this->getCurrentQuery()));
} }
/**
* Add a WHERE IN()
*
* @param array $entryIds
* @return $this
*/
protected function in(array $entryIds)
{
$this
->getCurrentQuery()
->where()
->in('id', $entryIds);
return $this;
}
} }
...@@ -42,11 +42,7 @@ class Entry extends SelectQueryBuilder ...@@ -42,11 +42,7 @@ class Entry extends SelectQueryBuilder
*/ */
public function queryByEntryIds(array $entryIds) public function queryByEntryIds(array $entryIds)
{ {
$this return $this->in($entryIds);
->getSelectQuery()
->where()
->in( 'id', $entryIds );
return $this;
} }
/** /**
...@@ -59,4 +55,6 @@ class Entry extends SelectQueryBuilder ...@@ -59,4 +55,6 @@ class Entry extends SelectQueryBuilder
{ {
return $this->is('user_id', $userId); return $this->is('user_id', $userId);
} }
} }
...@@ -3,11 +3,7 @@ ...@@ -3,11 +3,7 @@
namespace calderawp\CalderaFormsQuery\Select; namespace calderawp\CalderaFormsQuery\Select;
use calderawp\CalderaFormsQuery\CreatesSqlQueries;
use calderawp\CalderaFormsQuery\MySqlBuilder;
use calderawp\CalderaFormsQuery\QueryBuilder; use calderawp\CalderaFormsQuery\QueryBuilder;
use NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery;
use NilPortugues\Sql\QueryBuilder\Manipulation\Delete;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select; use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
abstract class SelectQueryBuilder extends QueryBuilder implements DoesSelectQuery abstract class SelectQueryBuilder extends QueryBuilder implements DoesSelectQuery
...@@ -56,8 +52,12 @@ abstract class SelectQueryBuilder extends QueryBuilder implements DoesSelectQuer ...@@ -56,8 +52,12 @@ abstract class SelectQueryBuilder extends QueryBuilder implements DoesSelectQuer
return $this; return $this;
} }
/**
* Set new query in selectQuery prop
*/
private function setNewQuery() private function setNewQuery()
{ {
$this->selectQuery = new \NilPortugues\Sql\QueryBuilder\Manipulation\Select($this->getTableName()); $this->selectQuery = new \NilPortugues\Sql\QueryBuilder\Manipulation\Select($this->getTableName());
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment