Files
librenms-librenms/src/Client.php
T
Walter Dal Mut c52377aad1 Allows non write adapters
* Renamed `AdapterInterface` to `WritableInterface`
 * Constructor now allows different adapter implementations
 * Check that adapter implements `WritableInterface` during data send
2015-06-13 12:04:04 +02:00

78 lines
2.1 KiB
PHP

<?php
namespace InfluxDB;
use InfluxDB\Adapter\WritableInterface;
use InfluxDb\Adapter\QueryableInterface;
/**
* Client to manage request at InfluxDB
*/
class Client
{
private $adapter;
public function __construct($adapter)
{
$this->adapter = $adapter;
return $this;
}
public function getAdapter()
{
return $this->adapter;
}
public function mark($name, array $values = [])
{
if (!($this->getAdapter() instanceOf WritableInterface)) {
throw new \BadMethodCallException("You can write data to database only if the adapter supports it!");
}
$data = $name;
if (!is_array($name)) {
$data =[];
$data['points'][0]['measurement'] = $name;
$data['points'][0]['fields'] = $values;
}
return $this->getAdapter()->send($data);
}
public function query($query)
{
if (!($this->getAdapter() instanceOf QueryableInterface)) {
throw new \BadMethodCallException("You can query the database only if the adapter supports it!");
}
$return = $this->getAdapter()->query($query);
return $return;
}
public function getDatabases()
{
if (!($this->getAdapter() instanceOf QueryableInterface)) {
throw new \BadMethodCallException("You can query the database only if the adapter supports it!");
}
return $this->getAdapter()->query("show databases");
}
public function createDatabase($name)
{
if (!($this->getAdapter() instanceOf QueryableInterface)) {
throw new \BadMethodCallException("You can query the database only if the adapter supports it!");
}
return $this->getAdapter()->query("create database \"{$name}\"");
}
public function deleteDatabase($name)
{
if (!($this->getAdapter() instanceOf QueryableInterface)) {
throw new \BadMethodCallException("You can query the database only if the adapter supports it!");
}
return $this->getAdapter()->query("drop database \"{$name}\"");
}
}