Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,23 @@ $client->mark("serie", [
]);
```

**Pay attention! This option: `setForceIntegers` will be removed when InfluxDB reaches a stable release `1.*` and we will enable the data type detection by default (BC BREAK)**
**Pay attention! This option: `setForceIntegers` will be removed when InfluxDB
reaches a stable release `1.*` and we will enable the data type detection by
default (BC BREAK)**

### Force data type

If you want to ensure that a type is effectively parsed correctly you can force
it directly during the send operation

```php
$client->mark("serie", [
"value" => new IntType(12), // Marked as int64
"elem" => new FloatType(12.4), // Marked as float64
"status" => new BoolType(true), // Marked as boolean
"line" => new StringType("12w"), // Marked as string
]);
```

### Query Builder

Expand Down
46 changes: 31 additions & 15 deletions src/Adapter/AdapterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,41 @@ protected function tagsToString(array $tags)

protected function pointsToString(array $elements)
{
$options = $this->getOptions();
array_walk($elements, function(&$value, $key) use ($options) {
switch(gettype($value)) {
case "string":
$value = "\"{$value}\"";
break;
case "boolean":
$value = ($value) ? "true" : "false";
break;
case "integer":
$value = ($options->getForceIntegers()) ? "{$value}i" : $value;
break;
default:
break;
array_walk($elements, function(&$value, $key) {
$dataType = gettype($value);
if (!in_array($dataType, ["string", "double", "boolean", "integer"])) {
$dataType = "serializable";
}

$dataType = ucfirst($dataType);
$value = call_user_func([$this, "convert{$dataType}"], $value);
$value = "{$key}={$value}";
});

return implode(",", $elements);
}

protected function convertSerializable($value)
{
return "{$value}";
}

protected function convertString($value)
{
return "\"{$value}\"";
}

protected function convertInteger($value)
{
return (($this->getOptions()->getForceIntegers()) ? "{$value}i" : $value);
}

protected function convertDouble($value)
{
return $value;
}

protected function convertBoolean($value)
{
return (($value) ? "true" : "false");
}
}
18 changes: 18 additions & 0 deletions src/Type/BoolType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace InfluxDB\Type;

class BoolType
{
private $num;

public function __construct($value)
{
$this->num = boolval((string)$value);
}

public function __toString()
{
return ($this->num) ? "true" : "false";
}
}

18 changes: 18 additions & 0 deletions src/Type/FloatType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace InfluxDB\Type;

class FloatType
{
private $num;

public function __construct($value)
{
$this->num = floatval((string)$value);
}

public function __toString()
{
return (string)$this->num;
}
}

17 changes: 17 additions & 0 deletions src/Type/IntType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace InfluxDB\Type;

class IntType
{
private $num;

public function __construct($value)
{
$this->num = intval((string)$value);
}

public function __toString()
{
return $this->num . "i";
}
}
18 changes: 18 additions & 0 deletions src/Type/StringType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace InfluxDB\Type;

class StringType
{
private $num;

public function __construct($value)
{
$this->num = strval($value);
}

public function __toString()
{
return "\"" . $this->num . "\"";
}
}

8 changes: 8 additions & 0 deletions tests/unit/Adapter/AdapterAbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

use ReflectionMethod;
use InfluxDB\Options;
use InfluxDB\Type\IntType;
use InfluxDB\Type\FloatType;

class AdapterAbstractTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -36,6 +38,12 @@ public function getElements()
[["one" => (double)12, "three" => 14], "one=12,three=14i", (new Options())->setForceIntegers(true)],
[["one" => (double)12, "three" => (double)14], "one=12,three=14", (new Options())->setForceIntegers(true)],
[["one" => (double)"12", "three" => (int)"14"], "one=12,three=14i", (new Options())->setForceIntegers(true)],
[["one" => (double)"12", "three" => new IntType("14")], "one=12,three=14i", (new Options())->setForceIntegers(true)],
[["one" => (double)"12", "three" => new IntType(14.12)], "one=12,three=14i", (new Options())->setForceIntegers(true)],
[["one" => (double)"12", "three" => new IntType(14)], "one=12,three=14i", (new Options())->setForceIntegers(true)],
[["one" => (double)"12", "three" => new FloatType(14)], "one=12,three=14", (new Options())->setForceIntegers(true)],
[["one" => (double)"12", "three" => new FloatType("14")], "one=12,three=14", (new Options())->setForceIntegers(true)],
[["one" => (double)"12", "three" => new FloatType("14.123")], "one=12,three=14.123", (new Options())->setForceIntegers(true)],
];
}
}
30 changes: 30 additions & 0 deletions tests/unit/Type/BoolTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace InfluxDB\Type;

class BoolTypeTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider boolProvider
*/
public function testConversions($in, $out)
{
$this->assertEquals($out, new BoolType($in));
}

public function boolProvider()
{
return [
[true, "true"],
["1", "true"],
[1, "true"],
["something", "true"],
[12, "true"],

[false, "false"],
["0", "false"],
[0, "false"],
[null, "false"],
];
}
}

32 changes: 32 additions & 0 deletions tests/unit/Type/FloatTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace InfluxDB\Type;

class FloatTypeTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider floatProvider
*/
public function testConversion($in, $out)
{
$in = new FloatType($in);
$this->assertSame($out, (string)$in);
}

public function floatProvider()
{
return [
[12.12, "12.12"],
["12.12", "12.12"],
["12", "12"],
[12, "12"],
[new FloatType("12.12"), "12.12"],
[new FloatType(12.12), "12.12"],
["invalid", "0"],
[null, "0"],
[new FloatType("invalid"), "0"],
[new FloatType(null), "0"],
[true, "1"],
[false, "0"],
];
}
}
30 changes: 30 additions & 0 deletions tests/unit/Type/IntTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace InfluxDB\Type;

class IntTypeTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider intProvider
*/
public function testConversions($in, $out)
{
$this->assertEquals($out, new IntType($in));
}

public function intProvider()
{
return [
["12", "12i"],
[12, "12i"],
[12.12, "12i"],
[new IntType(12.12), "12i"],
[new IntType("12.12"), "12i"],
["invalid", "0i"],
[null, "0i"],
[new IntType("invalid"), "0i"],
[new IntType(null), "0i"],
[true, "1i"],
[false, "0i"],
];
}
}
27 changes: 27 additions & 0 deletions tests/unit/Type/StringTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace InfluxDB\Type;

class StringTypeTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider stringProvider
*/
public function testConversions($in, $out)
{
$this->assertEquals($out, new StringType($in));
}

public function stringProvider()
{
return [
[true, '"1"'],
["walter", '"walter"'],
["12", '"12"'],
["12.153", '"12.153"'],
[12.153, '"12.153"'],
[12, '"12"'],
];
}
}