InfluxDB\Database::create PHP Method

create() public method

Create this database
public create ( InfluxDB\Database\RetentionPolicy $retentionPolicy = null, boolean $createIfNotExists = true ) : influxdb\ResultSet
$retentionPolicy InfluxDB\Database\RetentionPolicy
$createIfNotExists boolean Only create the database if it does not yet exist
return influxdb\ResultSet
    public function create(RetentionPolicy $retentionPolicy = null, $createIfNotExists = true)
    {
        try {
            $query = sprintf('CREATE DATABASE %s"%s"', $createIfNotExists ? 'IF NOT EXISTS ' : '', $this->name);
            $this->query($query);
            if ($retentionPolicy) {
                $this->createRetentionPolicy($retentionPolicy);
            }
        } catch (Exception $e) {
            throw new DatabaseException(sprintf('Failed to created database %s', $this->name), $e->getCode(), $e);
        }
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * @see https://github.com/influxdata/influxdb-php/pull/36
  */
 public function testReservedNames()
 {
     $database = new Database('stats', $this->mockClient);
     // test handling of reserved keywords in database name
     $database->create();
     $this->assertEquals('CREATE DATABASE IF NOT EXISTS "stats"', Client::$lastQuery);
     $database->listRetentionPolicies();
     $this->assertEquals('SHOW RETENTION POLICIES ON "stats"', Client::$lastQuery);
     // test handling of reserved keywords in retention policy names
     $database->create($this->getTestRetentionPolicy('default'));
     $this->assertEquals('CREATE RETENTION POLICY "default" ON "stats" DURATION 1d REPLICATION 1 DEFAULT', Client::$lastQuery);
     // test handling of reserved keywords in measurement names
     $this->assertEquals($database->getQueryBuilder()->from('server')->getQuery(), 'SELECT * FROM "server"');
 }