2014-09-10 12:00:46 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace InfluxDB;
|
|
|
|
|
|
2014-09-12 20:20:23 +02:00
|
|
|
use InfluxDb\Adapter\QueryableInterface;
|
|
|
|
|
|
2015-01-15 00:46:01 +01:00
|
|
|
/**
|
|
|
|
|
* Client to manage request at InfluxDB
|
|
|
|
|
*/
|
2014-09-11 09:59:33 +02:00
|
|
|
class Client
|
2014-09-10 12:00:46 +02:00
|
|
|
{
|
|
|
|
|
private $adapter;
|
2015-01-15 00:46:01 +01:00
|
|
|
|
2015-06-10 15:13:19 +02:00
|
|
|
public function __construct(Adapter\AdapterInterface $adapter)
|
2014-09-10 12:00:46 +02:00
|
|
|
{
|
|
|
|
|
$this->adapter = $adapter;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getAdapter()
|
|
|
|
|
{
|
|
|
|
|
return $this->adapter;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-07 12:37:48 +02:00
|
|
|
public function mark($name, array $values = [])
|
2014-09-10 12:00:46 +02:00
|
|
|
{
|
2015-06-07 12:31:11 +02:00
|
|
|
$data = $name;
|
|
|
|
|
if (!is_array($name)) {
|
|
|
|
|
$data =[];
|
2014-09-13 09:17:34 +02:00
|
|
|
|
2015-06-10 11:40:43 +02:00
|
|
|
$data['points'][0]['measurement'] = $name;
|
2015-06-07 12:31:11 +02:00
|
|
|
$data['points'][0]['fields'] = $values;
|
|
|
|
|
}
|
2014-09-10 12:00:46 +02:00
|
|
|
|
2015-06-07 12:37:48 +02:00
|
|
|
return $this->getAdapter()->send($data);
|
2014-09-10 12:00:46 +02:00
|
|
|
}
|
2014-09-12 20:20:23 +02:00
|
|
|
|
2015-06-07 12:37:48 +02:00
|
|
|
public function query($query)
|
2014-09-12 20:20:23 +02:00
|
|
|
{
|
|
|
|
|
if (!($this->getAdapter() instanceOf QueryableInterface)) {
|
|
|
|
|
throw new \BadMethodCallException("You can query the database only if the adapter supports it!");
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-07 12:37:48 +02:00
|
|
|
$return = $this->getAdapter()->query($query);
|
2014-09-14 17:36:29 +02:00
|
|
|
|
|
|
|
|
return $return;
|
2014-09-12 20:20:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-13 08:19:51 +02:00
|
|
|
public function getDatabases()
|
|
|
|
|
{
|
|
|
|
|
if (!($this->getAdapter() instanceOf QueryableInterface)) {
|
|
|
|
|
throw new \BadMethodCallException("You can query the database only if the adapter supports it!");
|
|
|
|
|
}
|
2015-06-07 12:37:48 +02:00
|
|
|
return $this->getAdapter()->query("show databases");
|
2014-09-13 08:19:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function createDatabase($name)
|
|
|
|
|
{
|
|
|
|
|
if (!($this->getAdapter() instanceOf QueryableInterface)) {
|
|
|
|
|
throw new \BadMethodCallException("You can query the database only if the adapter supports it!");
|
|
|
|
|
}
|
2015-06-07 12:37:48 +02:00
|
|
|
return $this->getAdapter()->query("create database \"{$name}\"");
|
2014-09-13 08:19:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function deleteDatabase($name)
|
|
|
|
|
{
|
|
|
|
|
if (!($this->getAdapter() instanceOf QueryableInterface)) {
|
|
|
|
|
throw new \BadMethodCallException("You can query the database only if the adapter supports it!");
|
|
|
|
|
}
|
2015-06-07 12:37:48 +02:00
|
|
|
return $this->getAdapter()->query("drop database \"{$name}\"");
|
2014-09-13 08:19:51 +02:00
|
|
|
}
|
2014-09-10 12:00:46 +02:00
|
|
|
}
|