Skip to content

Commit

Permalink
Merge pull request #71 from SkooppaOS/Action_methods
Browse files Browse the repository at this point in the history
Changed action methods to verb-like names.
  • Loading branch information
Michael Wilson committed Aug 31, 2015
2 parents 3e34572 + a39e690 commit 10422ec
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 84 deletions.
36 changes: 18 additions & 18 deletions docs/command-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ $person = $query
->select()
->from('person')
->where('name', 'vadas')
->first();
->getOne();

echo $people->name; // 'Vadas'
```
Expand Down Expand Up @@ -89,7 +89,7 @@ $builder->select()->from('person')
->limit(20)

$builder->select()->from('software')
->first();
->getOne();
```

Order the results.
Expand Down Expand Up @@ -221,16 +221,16 @@ $query
->andWhere('height', 5)
->limit(1)
->orderBy('name', 'asc') // order of orderBy is important
->all();
->getAll();
```

### Dispatching from the Query builder
What makes the Query Builder different is that you can interact with the database directly.

You can simply **dispatch** your query, or use **go**
You can simply **execute** your query, or use **go**
```php
$result = $query->select()->from('person')
->dispatch();
->execute();

$result = $query->select()->from('person')
->go();
Expand All @@ -239,10 +239,10 @@ Which will return a generic `Response`. Read [more about responses](responses.md

----

**all()** removes the limit and returns a an array or collections
**getAll()** removes the limit and returns an array of collections
```php
$result = $query->select()->from('person')
->all();
->getAll();
```
Read [more about responses](responses.md).

Expand All @@ -251,59 +251,59 @@ Read [more about responses](responses.md).
Or, we recommend **get()** for most cases.
```php
$result = $query->select()->from('person')
->get(); // alias of set()
->get(); // alias of getSet()
```
Which will return a Set (array of or single`Collection`).
Read [more about responses](responses.md).


----

**one()** and **first()** sets the limit to `1` before dispatching
**getOne()** sets the limit to `1` before executing
```php
$result = $query->select()->from('person')
->one(); // or first();
->getOne();
```
Returns a single `Collection`. Read [more about responses](responses.md).

----

**path()** dispatches and returns a Path.
**getPath()** executes and returns a Path.
```php
$result = $query->select()->from('person')
->path();
->getPath();
```
Read [more about responses](responses.md).

**Note: This is not implemented until traversals are finished**

----

**tree()** dispatches and returns a Tree.
**getTree()** executes and returns a Tree.
```php
$result = $query->select()->from('person')
->tree();
->getTree();
```
Read [more about responses](responses.md).

**Note: This is not implemented until traversals are finished**

----

**set()** dispatches and returns a Set (array or single collection).
**getSet()** executes and returns a Set (array or single collection).
This is really an alias for `get()`
```php
$result = $query->select()->from('person')
->set();
->getSet();
```
Read [more about responses](responses.md).

----

**scalar()** dispatches and returns a single, scalar value.
**getScalar()** executes and returns a single, scalar value.
```php
$result = $query->select('name')->from('person')->where('id', 5)
->scalar();
->getScalar();

echo $result; // 'Miranda'
```
Expand Down
41 changes: 22 additions & 19 deletions src/Commands/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Query extends Builder
/** @var ConnectionInterface Valid connection containing a driver */
protected $connection;


/**
* Creates a new instance of the Command Builder
* With a LanguageProcessor and Connection
Expand Down Expand Up @@ -53,11 +54,10 @@ public function fromDb($wanted = null)
*
* If no instance of CommandInterface is provided, then the
* current Command Bag is processed via the Command Processor
*
* @param CommandInterface|null $command
* @return Response the DB response in SpiderResponse format
*/
public function dispatch(CommandInterface $command = null)
private function dispatch($command = null)
{
$this->connection->open();

Expand All @@ -77,6 +77,7 @@ public function dispatch(CommandInterface $command = null)

// Reset query and return response
$this->bag = new Bag();

return $response;
}

Expand All @@ -89,16 +90,16 @@ public function go()
return $this->dispatch();
}

/* Dispatch with limits */
/* Dispatch without limits */
/**
* Dispatch a retrieve command with no limit.
* Return all the results
* @return array|Collection Results formatted as a Set
*/
public function all()
public function getAll()
{
$this->limit(false);
$response = $this->set();
$response = $this->getSet();

return (is_array($response)) ? $response : [$response];
}
Expand All @@ -109,34 +110,26 @@ public function all()
*/
public function get()
{
return $this->set();
return $this->getSet();
}

/**
* Retrieve the first result by dispatching the current Command Bag.
* @return Collection Results formatted as a set with single collection
*/
public function one()
public function getOne()
{
parent::first();
return $this->dispatch()->getSet();
}

/**
* Retrieve the first result by dispatching the current Command Bag.
* @return Collection Results formatted as a set with single collection
*/
public function first()
{
return $this->one();
}

/* Dispatch with Response formats */
/**
* Dispatches Command and formats results as a Set.
* @return array|Collection Results formatted as a set
*/
public function set()
public function getSet()
{
return $this->dispatch()->getSet();
}
Expand All @@ -145,7 +138,7 @@ public function set()
* Dispatches Command and formats results as a Tree.
* @return array|Collection Results formatted as a tree
*/
public function tree()
public function getTree()
{
parent::tree();
return $this->dispatch()->getTree();
Expand All @@ -155,7 +148,7 @@ public function tree()
* Dispatches Command and formats results as a Path.
* @return array|Collection Results formatted as a path
*/
public function path()
public function getPath()
{
parent::path();
return $this->dispatch()->getPath();
Expand All @@ -165,12 +158,22 @@ public function path()
* Dispatches Command and formats results as a scalar.
* @return string|bool|int Results formatted as a scalar
*/
public function scalar()
public function getScalar()
{
$this->limit(1);
return $this->dispatch()->getScalar();
}

/**
* Execute a command through dispatch
* @param CommandInterface|null $command
* @return Response
*/
public function execute(CommandInterface $command = null)
{
return $this->dispatch($command);
}

/* Manage the Builder itself */
/**
* Clear the current Command Bag
Expand Down
1 change: 1 addition & 0 deletions src/Drivers/OrientDB/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ protected function executeCommand($command, $method)

try {
$response = $this->client->$method($command->getScript());

} catch (ServerException $e) {
// Wrap a "class doesn't exist" exception
if (strpos($e->getMessage(), "not found in database")) {
Expand Down
Loading

0 comments on commit 10422ec

Please sign in to comment.