diff --git a/README.md b/README.md index 28e4cb88fc..9507bdba34 100644 --- a/README.md +++ b/README.md @@ -49,12 +49,22 @@ To fetch records from InfluxDB you can do a query directly on a database: It's also possible to use the QueryBuilder object. This is a class that simplifies the process of building queries. ```php + + // retrieve points with the query builder $result = $database->getQueryBuilder() ->select('cpucount') ->from('test_metric') ->limit(2) ->getResultSet() ->getPoints(); + + + // get the query from the QueryBuilder + $query = $database->getQueryBuilder() + ->select('cpucount') + ->from('test_metric') + ->getQuery(); + ``` ### Writing data diff --git a/composer.json b/composer.json index 73f3b4f755..6903916d63 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ ], "require": { "php": ">=5.3", - "guzzlehttp/guzzle": "~5.0" + "guzzlehttp/guzzle": "~3.0" }, "scripts": { "post-install-cmd": [ diff --git a/composer.lock b/composer.lock index 0a9d8afcdd..c5bb4ba854 100644 --- a/composer.lock +++ b/composer.lock @@ -4,34 +4,70 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "10abd66a90e1473453cbda87799ed079", + "hash": "bdd93bffe0aad03c6421d5d171d6b8c3", "packages": [ { "name": "guzzlehttp/guzzle", - "version": "5.3.x-dev", + "version": "v3.8.1", "source": { "type": "git", "url": "https://github.com/guzzle/guzzle.git", - "reference": "fd861570a9c3c28d98f418feea1f43f6268bdfa0" + "reference": "4de0618a01b34aa1c8c33a3f13f396dcd3882eba" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/guzzle/zipball/fd861570a9c3c28d98f418feea1f43f6268bdfa0", - "reference": "fd861570a9c3c28d98f418feea1f43f6268bdfa0", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/4de0618a01b34aa1c8c33a3f13f396dcd3882eba", + "reference": "4de0618a01b34aa1c8c33a3f13f396dcd3882eba", "shasum": "" }, "require": { - "guzzlehttp/ringphp": "^1.1", - "php": ">=5.4.0" + "ext-curl": "*", + "php": ">=5.3.3", + "symfony/event-dispatcher": ">=2.1" + }, + "replace": { + "guzzle/batch": "self.version", + "guzzle/cache": "self.version", + "guzzle/common": "self.version", + "guzzle/http": "self.version", + "guzzle/inflection": "self.version", + "guzzle/iterator": "self.version", + "guzzle/log": "self.version", + "guzzle/parser": "self.version", + "guzzle/plugin": "self.version", + "guzzle/plugin-async": "self.version", + "guzzle/plugin-backoff": "self.version", + "guzzle/plugin-cache": "self.version", + "guzzle/plugin-cookie": "self.version", + "guzzle/plugin-curlauth": "self.version", + "guzzle/plugin-error-response": "self.version", + "guzzle/plugin-history": "self.version", + "guzzle/plugin-log": "self.version", + "guzzle/plugin-md5": "self.version", + "guzzle/plugin-mock": "self.version", + "guzzle/plugin-oauth": "self.version", + "guzzle/service": "self.version", + "guzzle/stream": "self.version" }, "require-dev": { - "ext-curl": "*", - "phpunit/phpunit": "^4.0" + "doctrine/cache": "*", + "monolog/monolog": "1.*", + "phpunit/phpunit": "3.7.*", + "psr/log": "1.0.*", + "symfony/class-loader": "*", + "zendframework/zend-cache": "<2.3", + "zendframework/zend-log": "<2.3" }, "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.8-dev" + } + }, "autoload": { - "psr-4": { - "GuzzleHttp\\": "src/" + "psr-0": { + "Guzzle": "src/", + "Guzzle\\Tests": "tests/" } }, "notification-url": "https://packagist.org/downloads/", @@ -43,6 +79,10 @@ "name": "Michael Dowling", "email": "mtdowling@gmail.com", "homepage": "https://github.com/mtdowling" + }, + { + "name": "Guzzle Community", + "homepage": "https://github.com/guzzle/guzzle/contributors" } ], "description": "Guzzle is a PHP HTTP client library and framework for building RESTful web service clients", @@ -56,78 +96,36 @@ "rest", "web service" ], - "time": "2015-06-03 05:11:42" + "time": "2014-01-28 22:29:15" }, { - "name": "guzzlehttp/ringphp", + "name": "symfony/event-dispatcher", "version": "dev-master", "source": { "type": "git", - "url": "https://github.com/guzzle/RingPHP.git", - "reference": "9465032ac5d6beaa55f10923403e6e1c36018d9c" + "url": "https://github.com/symfony/EventDispatcher.git", + "reference": "f991afd968ea05582bae203754d761714598d018" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/RingPHP/zipball/9465032ac5d6beaa55f10923403e6e1c36018d9c", - "reference": "9465032ac5d6beaa55f10923403e6e1c36018d9c", + "url": "https://api.github.com/repos/symfony/EventDispatcher/zipball/f991afd968ea05582bae203754d761714598d018", + "reference": "f991afd968ea05582bae203754d761714598d018", "shasum": "" }, "require": { - "guzzlehttp/streams": "~3.0", - "php": ">=5.4.0", - "react/promise": "~2.0" + "php": ">=5.5.9" }, "require-dev": { - "ext-curl": "*", - "phpunit/phpunit": "~4.0" + "psr/log": "~1.0", + "symfony/config": "~2.8|~3.0", + "symfony/dependency-injection": "~2.8|~3.0", + "symfony/expression-language": "~2.8|~3.0", + "symfony/phpunit-bridge": "~2.8|~3.0", + "symfony/stopwatch": "~2.8|~3.0" }, "suggest": { - "ext-curl": "Guzzle will use specific adapters if cURL is present" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.1-dev" - } - }, - "autoload": { - "psr-4": { - "GuzzleHttp\\Ring\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Michael Dowling", - "email": "mtdowling@gmail.com", - "homepage": "https://github.com/mtdowling" - } - ], - "description": "Provides a simple API and specification that abstracts away the details of HTTP into a single PHP function.", - "time": "2015-05-21 17:23:02" - }, - { - "name": "guzzlehttp/streams", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/guzzle/streams.git", - "reference": "d1f8a6c55f0f753cfd6f6755856473eb02cedb19" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/guzzle/streams/zipball/d1f8a6c55f0f753cfd6f6755856473eb02cedb19", - "reference": "d1f8a6c55f0f753cfd6f6755856473eb02cedb19", - "shasum": "" - }, - "require": { - "php": ">=5.4.0" - }, - "require-dev": { - "phpunit/phpunit": "~4.0" + "symfony/dependency-injection": "", + "symfony/http-kernel": "" }, "type": "library", "extra": { @@ -137,7 +135,7 @@ }, "autoload": { "psr-4": { - "GuzzleHttp\\Stream\\": "src/" + "Symfony\\Component\\EventDispatcher\\": "" } }, "notification-url": "https://packagist.org/downloads/", @@ -146,62 +144,17 @@ ], "authors": [ { - "name": "Michael Dowling", - "email": "mtdowling@gmail.com", - "homepage": "https://github.com/mtdowling" - } - ], - "description": "Provides a simple abstraction over streams of data", - "homepage": "http://guzzlephp.org/", - "keywords": [ - "Guzzle", - "stream" - ], - "time": "2015-01-22 00:01:34" - }, - { - "name": "react/promise", - "version": "v2.2.0", - "source": { - "type": "git", - "url": "https://github.com/reactphp/promise.git", - "reference": "365fcee430dfa4ace1fbc75737ca60ceea7eeeef" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/reactphp/promise/zipball/365fcee430dfa4ace1fbc75737ca60ceea7eeeef", - "reference": "365fcee430dfa4ace1fbc75737ca60ceea7eeeef", - "shasum": "" - }, - "require": { - "php": ">=5.4.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.0-dev" - } - }, - "autoload": { - "psr-4": { - "React\\Promise\\": "src/" + "name": "Fabien Potencier", + "email": "fabien@symfony.com" }, - "files": [ - "src/functions_include.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ { - "name": "Jan Sorgalla", - "email": "jsorgalla@googlemail.com" + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" } ], - "description": "A lightweight implementation of CommonJS Promises/A for PHP", - "time": "2014-12-30 13:32:42" + "description": "Symfony EventDispatcher Component", + "homepage": "https://symfony.com", + "time": "2015-05-12 15:48:43" } ], "packages-dev": [], diff --git a/src/InfluxDB/Client.php b/src/InfluxDB/Client.php index 4306ffe7e6..6156c7d82d 100644 --- a/src/InfluxDB/Client.php +++ b/src/InfluxDB/Client.php @@ -6,7 +6,6 @@ namespace InfluxDB; -use GuzzleHttp\Client as httpClient; use InfluxDB\Client\Exception as ClientException; /** @@ -67,7 +66,7 @@ class Client protected $baseURI; /** - * @var \GuzzleHttp\Client + * @var \Guzzle\Http\Client */ protected $httpClient; @@ -114,12 +113,7 @@ class Client // the the base URI $this->setBaseURI(sprintf('%s://%s:%d', $this->scheme, $this->host, $this->port)); - - $this->httpClient = new httpClient(array( - 'base_url' => $this->getBaseURI(), - 'timeout' => $this->getTimeout() - ) - ); + $this->httpClient = new \Guzzle\Http\Client($this->getBaseURI()); } @@ -159,12 +153,12 @@ class Client $params = '?'.http_build_query(array_merge(array('q' => $query), $params)); - $options = array_merge($this->options, array('exceptions' => false)); + $options = array_merge($this->options, array('exceptions' => false, 'timeout' => $this->getTimeout())); try { - $response = $this->httpClient->get('query'.$params, $options); + $response = $this->httpClient->get('query'.$params, null, $options); - $raw = (string) $response->getBody(); + $raw = (string) $response->send()->getBody(); return new ResultSet($raw); @@ -174,24 +168,29 @@ class Client } /** - * @param $database - * @param $data + * Write points to the database + * + * @param string $database + * @param string $data + * * @return bool + * + * @internal Internal method, do not use directly * @throws Exception */ public function write($database, $data) { try { - $this->httpClient->post( - $this->getBaseURI() . '/write?db=' . $database, - array('body' => $data) - ); + $result = $this->httpClient->post($this->getBaseURI() . '/write?db=' . $database, null, $data, + array('timeout' => $this->getTimeout()) + )->send(); + + return $result->getStatusCode() == 204; + } catch (\Exception $e) { throw new Exception(sprintf('Writing has failed, exception: %s', $e->getMessage())); } - - return true; } /** diff --git a/src/InfluxDB/Query/Builder.php b/src/InfluxDB/Query/Builder.php index 4792f299e8..1709025f5f 100644 --- a/src/InfluxDB/Query/Builder.php +++ b/src/InfluxDB/Query/Builder.php @@ -207,7 +207,14 @@ class Builder return $this; } -// public function orderBy + + /** + * @return string + */ + public function getQuery() + { + return $this->parseQuery(); + } /** * Gets the result from the database (builds the query) @@ -215,6 +222,14 @@ class Builder * @return ResultSet */ public function getResultSet() + { + return $this->db->query($this->parseQuery()); + } + + /** + * @return string + */ + protected function parseQuery() { $query = sprintf("SELECT %s FROM %s", $this->selection, $this->metric); @@ -237,6 +252,6 @@ class Builder $query .= $this->limitClause; } - return $this->db->query($query); + return $query; } } \ No newline at end of file