Skip to content

Commit

Permalink
revising query to "bind" property to dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
soletan committed Nov 1, 2016
1 parent a307b27 commit 86f3bf7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
16 changes: 12 additions & 4 deletions extensions/datasource/classes/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function __construct( connection $connection, $table );
* @return $this fluent interface
*/
public function distinct( $enabled = true );

/**
* Joins selected dataset to include (some of) its columns.
*
Expand All @@ -105,12 +105,20 @@ public function addDataset( $dataset, $condition, $parameters = null );
* Adds property to fetch of all matching records in (one of the joined)
* dataset(s).
*
* This property may be using alias. $name may contain full term. In that
* This property may be using alias. `$name` may contain full term. In that
* case $parameters may include some parameters replacing markers in term.
*
* The number of parameters must match number of markers in term.
*
* @param string $name property to fetch or term to evaluate on all matches
* `$name` may be array consisting of explicitly selected dataset and actual
* name of property to fetch from selected dataset. Dataset may be selected
* by name as used on adding dataset using @see addDataset() or by index.
* On using index this is either 0-based index of dataset according to order
* of adding datasets **previously**. Negative index is counting in reverse
* order, thus `-1` is selecting most recently added dataset whereas `0` is
* selecting initial dataset of query and `1` is selecting first actually
* added dataset.
*
* @param string[]|string $name property to fetch or term to evaluate on all matches, array of dataset name (or index in current query) and name of dataset's property
* @param string $alias alias to assign for addressing fetched value in filter
* @param mixed $parameters array of parameters or first of additional arguments providing one parameter each
* @return $this
Expand Down
20 changes: 20 additions & 0 deletions extensions/datasource/classes/sql_query.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ public function addDataset( $dataset, $condition, $parameters = null )

public function addProperty( $name, $alias = null, $parameters = null )
{
if ( is_array( $name ) ) {
$bindToDataSet = array_shift( $name );
$name = array_shift( $name );
} else
$bindToDataSet = null;

if ( !is_string( $name ) || !( $name = trim( $name ) ) )
throw new \InvalidArgumentException( 'bad column name' );

Expand All @@ -371,6 +377,20 @@ public function addProperty( $name, $alias = null, $parameters = null )
else if ( !is_string( $alias ) || !( $alias = trim( $alias ) ) )
throw new \InvalidArgumentException( 'bad column alias' );


if ( is_integer( $bindToDataSet ) ) {
$sets = array_keys( $this->tables );
$sets = array_slice( $sets, $bindToDataSet, 1 );

if ( !count( $sets ) )
throw new \OutOfRangeException( 'invalid index of data set to bind property' );

$bindToDataSet = array_shift( $sets );
}

if ( trim( $bindToDataSet ) )
$name = $this->datasource()->qualifyPropertyNames( $bindToDataSet, $name );

$this->columns[$alias] = $name;

$this->collectParameters( func_get_args(), $this->columnParameters, 2 );
Expand Down

0 comments on commit 86f3bf7

Please sign in to comment.