Commit 9ea7011c authored by Kyle Johnson's avatar Kyle Johnson 📚

Make MetaQueryBuilder more generic.

parent d398d287
Pipeline #764 passed with stage
in 46 seconds
...@@ -16,7 +16,6 @@ node_modules ...@@ -16,7 +16,6 @@ node_modules
# Project Stuff # Project Stuff
deploy.sh deploy.sh
deploy-old.sh deploy-old.sh
tests/
vendor/ vendor/
composer.lock composer.lock
package-lock.json package-lock.json
......
...@@ -11,10 +11,10 @@ class NF_Database_FieldsRepository ...@@ -11,10 +11,10 @@ class NF_Database_FieldsRepository
public function fetch( $form_id ) { public function fetch( $form_id ) {
$table_name = $this->db->prefix . 'nf3_fields'; $table_name = $this->db->prefix . 'nf3_fields';
$meta_table_name = $this->db->prefix . 'nf3_field_meta'; $meta_table_name = $this->db->prefix . 'nf3_field_meta';
$query_builder = new NF_Database_FieldsQueryBuilder( $table_name, $meta_table_name, $form_id ); $query_builder = new NF_Database_MetaQueryBuilder( $table_name, $meta_table_name, $form_id );
$fields_sql = $query_builder->get_fields_sql(); $fields_sql = $query_builder->get_sql();
$field_meta_sql = $query_builder->get_field_meta_sql(); $field_meta_sql = $query_builder->get_meta_sql();
$fields = array(); $fields = array();
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
/** /**
* Builds an SQL statement to fetch fields (with settings) for a given form ID. * Builds an SQL statement to fetch fields (with settings) for a given form ID.
*/ */
class NF_Database_FieldsQueryBuilder class NF_Database_MetaQueryBuilder
{ {
protected $table_name = ''; protected $table_name = '';
protected $meta_table_name = ''; protected $meta_table_name = '';
...@@ -23,7 +23,7 @@ class NF_Database_FieldsQueryBuilder ...@@ -23,7 +23,7 @@ class NF_Database_FieldsQueryBuilder
/** /**
* @return string * @return string
*/ */
public function get_field_ids_sql() public function get_ids_sql()
{ {
return "SELECT DISTINCT {$this->table_name}.id FROM {$this->table_name} WHERE {$this->table_name}.parent_id = {$this->parent_id}"; return "SELECT DISTINCT {$this->table_name}.id FROM {$this->table_name} WHERE {$this->table_name}.parent_id = {$this->parent_id}";
} }
...@@ -31,23 +31,23 @@ class NF_Database_FieldsQueryBuilder ...@@ -31,23 +31,23 @@ class NF_Database_FieldsQueryBuilder
/** /**
* @return string * @return string
*/ */
public function get_fields_sql() public function get_sql()
{ {
return " return "
SELECT * SELECT *
FROM $this->table_name FROM $this->table_name
WHERE id IN ( {$this->get_field_ids_sql()} ) WHERE id IN ( {$this->get_ids_sql()} )
"; ";
} }
public function get_field_meta_sql() public function get_meta_sql()
{ {
return " return "
SELECT Meta.parent_id, Meta.key, Meta.value SELECT Meta.parent_id, Meta.key, Meta.value
FROM $this->table_name as Object FROM $this->table_name as Object
JOIN $this->meta_table_name as Meta JOIN $this->meta_table_name as Meta
ON Object.id = Meta.parent_id ON Object.id = Meta.parent_id
WHERE Object.id IN ( {$this->get_field_ids_sql()} ) WHERE Object.id IN ( {$this->get_ids_sql()} )
"; ";
} }
} }
\ No newline at end of file
<?php
use PHPUnit\Framework\TestCase;
class fieldsCollection extends TestCase
{
public function testExample()
{
include '../ninja-forms/includes/database/FieldsCollection.php';
$fields = [ 1 => [ 'id' => 1 ] ];
$collection = new NF_Database_FieldsCollection( $fields );
$field = $collection->get_field_settings( 1 );
$this->assertTrue( 1 == $field[ 'id' ] );
}
}
\ No newline at end of file
<?php
use PHPUnit\Framework\TestCase;
class metaQueryBuilder extends TestCase
{
protected $query_builder;
protected function setUp()
{
include '../ninja-forms/includes/database/MetaQueryBuilder.php';
$this->query_builder = new NF_Database_MetaQueryBuilder( 'object', 'object_meta', 1 );
}
public function testMetaSql()
{
$expected = "
SELECT Meta.parent_id, Meta.key, Meta.value
FROM object as Object
JOIN object_meta as Meta
ON Object.id = Meta.parent_id
WHERE Object.id IN ( SELECT DISTINCT object.id FROM object WHERE object.parent_id = 1 )
";
$actual = $this->query_builder->get_meta_sql();
$this->assertEquals( trim($expected), trim($actual) );
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment