-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from corley/feature/query-manager
Support for custom query manager
- Loading branch information
Showing
13 changed files
with
443 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
namespace InfluxDB; | ||
|
||
use InvalidArgumentException; | ||
use RuntimeException; | ||
use InfluxDB\Client; | ||
|
||
class Manager | ||
{ | ||
private $client; | ||
private $queries; | ||
|
||
public function __construct(Client $client) | ||
{ | ||
$this->client = $client; | ||
$this->queries = []; | ||
} | ||
|
||
public function addQuery($name, callable $query = null) | ||
{ | ||
if ($query === null) { | ||
list($name, $query) = $this->fromObjectToNameCallableList($name); | ||
} | ||
|
||
$this->queries[$name] = $query; | ||
} | ||
|
||
private function fromObjectToNameCallableList($name) | ||
{ | ||
if (is_object($name) && is_callable($name)) { | ||
if (method_exists($name, "__toString")) { | ||
return [(string)$name, $name]; | ||
} | ||
} | ||
|
||
throw new InvalidArgumentException("Your command should implements '__toString' method and should be a callable thing"); | ||
} | ||
|
||
public function __call($name, $args) | ||
{ | ||
if (method_exists($this->client, $name)) { | ||
return call_user_func_array([$this->client, $name], $args); | ||
} | ||
|
||
if (array_key_exists($name, $this->queries)) { | ||
$query = call_user_func_array($this->queries[$name], $args); | ||
return $this->client->query($query); | ||
} | ||
|
||
throw new RuntimeException("The method you are using is not allowed: '{$name}', do you have to add it with 'addQuery'"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
namespace InfluxDB\Query; | ||
|
||
class CreateDatabase | ||
{ | ||
public function __invoke($name) | ||
{ | ||
return "CREATE DATABASE \"" . addslashes($name) . "\""; | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return "createDatabase"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
namespace InfluxDB\Query; | ||
|
||
class DeleteDatabase | ||
{ | ||
public function __invoke($name) | ||
{ | ||
return "DROP DATABASE \"" . addslashes($name) . "\""; | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return "deleteDatabase"; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
namespace InfluxDB\Query; | ||
|
||
class GetDatabases | ||
{ | ||
public function __invoke() | ||
{ | ||
return "show databases"; | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return "getDatabases"; | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
namespace InfluxDB\Integration; | ||
|
||
use InfluxDB\Adapter\Http\Options; | ||
use InfluxDB\Adapter\Http\Writer; | ||
use InfluxDB\Adapter\Http\Reader; | ||
use InfluxDB\Client; | ||
use InfluxDB\Query\CreateDatabase; | ||
use InfluxDB\Query\DeleteDatabase; | ||
use InfluxDB\Query\GetDatabases; | ||
use GuzzleHttp\Client as GuzzleHttpClient; | ||
use InfluxDB\Manager; | ||
use InfluxDB\Integration\Framework\TestCase; | ||
|
||
class ManagerTest extends TestCase | ||
{ | ||
public function testCreateANewDatabase() | ||
{ | ||
$options = new Options(); | ||
$guzzleHttp = new GuzzleHttpClient(); | ||
$writer = new Writer($guzzleHttp, $options); | ||
$reader = new Reader($guzzleHttp, $options); | ||
$client = new Client($reader, $writer); | ||
$manager = new Manager($client); | ||
|
||
$manager->addQuery(new CreateDatabase()); | ||
$manager->addQuery(new DeleteDatabase()); | ||
$manager->addQuery(new GetDatabases()); | ||
|
||
$manager->createDatabase("one"); | ||
$manager->createDatabase("two"); | ||
$manager->createDatabase("walter"); | ||
|
||
$databases = $manager->getDatabases(); | ||
|
||
$this->assertCount(3, $databases["results"][0]["series"][0]["values"]); | ||
} | ||
|
||
public function testDropExistingDatabase() | ||
{ | ||
$options = new Options(); | ||
$guzzleHttp = new GuzzleHttpClient(); | ||
$writer = new Writer($guzzleHttp, $options); | ||
$reader = new Reader($guzzleHttp, $options); | ||
$client = new Client($reader, $writer); | ||
$manager = new Manager($client); | ||
|
||
$manager->addQuery(new CreateDatabase()); | ||
$manager->addQuery(new DeleteDatabase()); | ||
$manager->addQuery(new GetDatabases()); | ||
|
||
$manager->createDatabase("walter"); | ||
$this->assertDatabasesCount(1); | ||
|
||
$manager->deleteDatabase("walter"); | ||
$this->assertDatabasesCount(0); | ||
} | ||
|
||
public function testListActiveDatabases() | ||
{ | ||
$options = new Options(); | ||
$guzzleHttp = new GuzzleHttpClient(); | ||
$writer = new Writer($guzzleHttp, $options); | ||
$reader = new Reader($guzzleHttp, $options); | ||
$client = new Client($reader, $writer); | ||
$manager = new Manager($client); | ||
|
||
$manager->addQuery(new CreateDatabase()); | ||
$manager->addQuery(new DeleteDatabase()); | ||
$manager->addQuery(new GetDatabases()); | ||
|
||
$manager->createDatabase("one"); | ||
$manager->createDatabase("two"); | ||
|
||
$databases = $manager->getDatabases(); | ||
|
||
$this->assertCount(2, $databases["results"][0]["series"][0]["values"]); | ||
} | ||
} |
Oops, something went wrong.