Added timePrecision

Client::setTimePrecision() affects all databases globally, and DB::setTimePrecision() only to the current database object
This commit is contained in:
César D. Rodas 2013-11-15 00:17:00 -03:00
parent b6d8a6fdbf
commit fd9667d928
3 changed files with 60 additions and 1 deletions

View File

@ -44,6 +44,15 @@ class BaseHTTP
protected $user;
protected $pass;
protected $base;
protected $timePrecision = 's';
protected $children = array();
const SECOND = 's';
const MILLISECOND = 'm';
const MICROSECOND = 'u';
const S = 's';
const MS = 'm';
const US = 'u';
protected function inherits(BaseHTTP $c)
{
@ -51,6 +60,8 @@ class BaseHTTP
$this->pass = $c->pass;
$this->port = $c->port;
$this->host = $c->host;
$this->timePrecision = $c->timePrecision;
$c->children[] = $this;
}
protected function getCurl($url, Array $args = [])
@ -85,6 +96,29 @@ class BaseHTTP
return $this->execCurl($ch);
}
public function getTimePrecision()
{
return $this->timePrecision;
}
public function setTimePrecision($p)
{
switch ($p) {
case 'm':
case 's':
case 'u':
$this->timePrecision = $p;
if ($this instanceof Client) {
foreach ($this->children as $children) {
$children->timePrecision = $p;
}
}
return $this;
}
throw new \InvalidArgumentException("Expecting m,s or u");
}
protected function get($url, Array $args = [])
{
$ch = $this->getCurl($url, $args);

View File

@ -82,7 +82,7 @@ class DB extends BaseHTTP
public function query($sql)
{
return new Cursor($this->get('series', ['q' => $sql]));
return new Cursor($this->get('series', ['q' => $sql, 'time_precision' => $this->timePrecision]));
}
public function createUser($username, $password)

View File

@ -47,6 +47,31 @@ class DBTest extends \phpunit_framework_testcase
$client->test_xxx->drop();
}
public function testTimePrecision()
{
$client = new Client;
$this->assertEquals('s', $client->getTimePrecision());
$db = $client->createDatabase("test_yyyy");
$this->assertEquals('s', $db->getTimePrecision());
$client->setTimePrecision('m');
$this->assertEquals('m', $client->getTimePrecision());
$this->assertEquals('m', $db->getTimePrecision());
$db1 = $client->createDatabase("test_yyyx");
$this->assertEquals('m', $db->getTimePrecision());
}
/**
* @expectedException InvalidArgumentException
*/
public function testInvalidTimePrecision()
{
$client = new Client;
$client->SetTimePrecision([]);
}
public function testQuery()
{
$client = new Client;