Skip to content

Commit

Permalink
put search code in a function
Browse files Browse the repository at this point in the history
  • Loading branch information
John Holt authored and John Holt committed Nov 3, 2023
1 parent 90bf652 commit 70d8345
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
12 changes: 12 additions & 0 deletions api/src/Database/DatabaseQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,16 @@ private function addBoundVariable($value): int
array_push($this->query_bound_values, $value);
return sizeof($this->query_bound_values);
}

public static function getWhereSearch($searchValue, $fields, &$query_bound_values)
{
$where = " AND (0=1";

foreach ($fields as $field) {
array_push($query_bound_values, $searchValue);
$where .= " OR lower(" . $field . ") LIKE lower(CONCAT('%', :" . sizeof($query_bound_values) . ", '%'))";
}
$where .= ")";
return $where;
}
}
11 changes: 3 additions & 8 deletions api/src/Page/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SynchWeb\Page;

use SynchWeb\Page;
use SynchWeb\Database\DatabaseQueryBuilder;

class Contact extends Page
{
Expand Down Expand Up @@ -48,16 +49,10 @@ function _get_contacts() {
}

if ($this->has_arg('s')) {
$st = sizeof($args) + 1;
$fields = array('pe.givenname', 'pe.familyname', 'c.cardname',
'l.name', 'l.address', 'l.city', 'l.country', 'pe.phonenumber', 'l.postcode');
$where .= " AND (0=1";

foreach ($fields as $i => $field) {
$where .= " OR lower(" . $field . ") LIKE lower(CONCAT('%', :" . ($st + $i) . ", '%'))";
array_push($args, $this->arg('s'));
}
$where .= ")";
$where = $where . DatabaseQueryBuilder::getWhereSearch($this->arg('s'), $fields, $args);
error_log($where);
}

$tot = $this->db->pq("SELECT count(c.labcontactid) as tot FROM labcontact c
Expand Down
20 changes: 18 additions & 2 deletions api/tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
use SynchWeb\Database\DatabaseParent;
use SynchWeb\Database\DatabaseQueryBuilder;

final class DatabaseHelperTest extends TestCase
use function PHPUnit\Framework\assertEquals;

final class DatabaseQueryBuilderTest extends TestCase
{
use \phpmock\phpunit\PHPMock;

private function create_database_helper($fields, $expected_sql, $expected_id_value = null)
private function create_database_helper($fields, $expected_sql=null, $expected_id_value = null)
{
$expected_values = [];
foreach ($fields as $field)
Expand Down Expand Up @@ -168,4 +170,18 @@ public function testWithTwoValuesWhenInsertingDBInsertIsCorrect()
}
$dbh->insert($expected_table);
}

public function testWithSingleIdWhereClauseGetWhereClauseReturnIt() {
$fields = ["a.field1", "b.field2"];
$expected_sql = " AND (0=1 OR lower({$fields[0]}) LIKE lower(CONCAT('%', :2, '%')) OR lower({$fields[1]}) LIKE lower(CONCAT('%', :3, '%')))";
$expectedSearchValue = "searchVal";

$element1 = "first";
$args=[$element1];
$sql = DatabaseQueryBuilder::getWhereSearch($expectedSearchValue, $fields, $args);

assertEquals($expected_sql, $sql);
assertEquals([$element1, $expectedSearchValue, $expectedSearchValue], $args);
}

}

0 comments on commit 70d8345

Please sign in to comment.