query() public method

Query influxDB
public query ( string $database, string $query, array $parameters = [] ) : influxdb\ResultSet
$database string
$query string
$parameters array
return influxdb\ResultSet
Example #1
0
 /**
  */
 public function testGuzzleQuery()
 {
     $client = new Client('localhost', 8086);
     $query = "some-bad-query";
     $bodyResponse = file_get_contents(dirname(__FILE__) . '/result.example.json');
     $httpMockClient = self::buildHttpMockClient($bodyResponse);
     $client->setDriver(new Guzzle($httpMockClient));
     /** @var \InfluxDB\ResultSet $result */
     $result = $client->query(null, $query);
     $this->assertInstanceOf('\\InfluxDB\\ResultSet', $result);
 }
 public function testIntegrationResponseHandler()
 {
     $options = $this->getOptions();
     $options->setDatabase("mydb");
     $client = $this->getClient();
     $client->createDatabase("mydb");
     $client->mark(["time" => "2015-09-10T23:20:35Z", "points" => [["measurement" => "cpu", "fields" => ["value" => "OK", "hello" => 2]], ["measurement" => "mem", "fields" => ["value" => "KO", "hello" => 4]]]]);
     $stack = new HandlerStack();
     $stack->setHandler(new CurlHandler());
     $stack->push(\InfluxDB\Handler\message_handler());
     // Push the response handler
     $http = new HttpClient(['handler' => $stack]);
     $options = new Http\Options();
     $options->setDatabase("mydb");
     $reader = new Http\Reader($http, $options);
     $writer = new Http\Writer($http, $options);
     $client = new Client($reader, $writer);
     $response = $client->query("SELECT * FROM cpu,mem");
     $this->assertEquals(["cpu" => [["value" => "OK", "hello" => 2, "time" => "2015-09-10T23:20:35Z"]], "mem" => [["value" => "KO", "hello" => 4, "time" => "2015-09-10T23:20:35Z"]]], $response);
 }
Example #3
0
 /**
  * @param string          $type
  * @param string          $privilege
  * @param string          $username
  * @param Database|string $database
  *
  * @throws \InfluxDB\Exception
  * @return \InfluxDB\ResultSet
  */
 private function executePrivilege($type, $privilege, $username, $database = null)
 {
     if (!in_array($privilege, [self::PRIVILEGE_READ, self::PRIVILEGE_WRITE, self::PRIVILEGE_ALL])) {
         throw new Exception($privilege . ' is not a valid privileges, allowed privileges: READ, WRITE, ALL');
     }
     if ($privilege != self::PRIVILEGE_ALL && !$database) {
         throw new Exception('Only grant ALL cluster-wide privileges are allowed');
     }
     $database = $database instanceof Database ? $database->getName() : (string) $database;
     $query = "{$type} {$privilege}";
     if ($database) {
         $query .= sprintf(' ON %s ', $database);
     } else {
         $query .= " PRIVILEGES ";
     }
     if ($username && $type == 'GRANT') {
         $query .= "TO {$username}";
     } elseif ($username && $type == 'REVOKE') {
         $query .= "FROM {$username}";
     }
     return $this->client->query(null, $query);
 }
Example #4
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->setHelperSet($this->getApplication()->getHelperSet());
     $db = $input->getOption("db");
     $query = $input->getOption("execute");
     $port = $input->getOption("port");
     $host = $input->getOption("host");
     $user = $input->getOption("user");
     $password = $input->getOption("password");
     $verbose = $input->getOption("verbose");
     $c = new Client(sprintf("http://%s:%s", $host, $port), $user, $password, $db);
     if ($verbose) {
         $c->setDebug(true);
     }
     $dialog = $this->getHelperSet()->get('dialog');
     try {
         ///$query = $dialog->ask($output, sprintf('influx: %s> ', $db));
         $begin = microtime(true);
         $result = $c->query($query);
         $end = microtime(true);
         if (empty($result)) {
             $output->writeln(sprintf("Empty set (%f sec)", round($end - $begin, 4)));
             return;
         }
         $total = 0;
         foreach ($result as $chunk) {
             $keys = $chunk->getColumns();
             $table = $this->getHelperSet()->get('table');
             $table->setHeaders($keys);
             $table->setRows($chunk->getPoints());
             $table->render($output);
             $total += count($chunk);
         }
         $output->writeln(sprintf("%d rows in set (%f sec)", $total, round($end - $begin, 4)));
     } catch (\Exception $e) {
         $output->writeln($e->getMessage());
     }
 }
Example #5
0
 /**
  * Query influxDB
  *
  * @param  string $query
  * @param  array  $params
  * @return ResultSet
  * @throws Exception
  */
 public function query($query, $params = [])
 {
     return $this->client->query($this->name, $query, $params);
 }
Example #6
0
 /**
  * @expectedException BadMethodCallException
  */
 public function testNeedQueryableInterfaceDuringQuery()
 {
     $client = new Client(new \stdClass());
     $client->query("OK", []);
 }