Skip to content

Commit 7ea56e1

Browse files
authored
Merge pull request #74 from Zwartpet/patch/line-protocol
Implemented comma, equal sign and space escaping
2 parents ee65b9a + e90e2bd commit 7ea56e1

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

src/Adapter/WriterTrait.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ protected function tagsToString(array $tags)
5252
{
5353
$tagLine = "";
5454
if (count($tags) > 0) {
55-
array_walk($tags, function(&$value, $key) {
56-
$value = "{$key}={$value}";
55+
array_walk($tags, function (&$value, $key) {
56+
$value = "{$this->addSlashes($key)}={$this->addSlashes($value)}";
5757
});
5858
$tagLine = sprintf(",%s", implode(",", $tags));
5959
}
@@ -63,15 +63,15 @@ protected function tagsToString(array $tags)
6363

6464
protected function pointsToString(array $elements)
6565
{
66-
array_walk($elements, function(&$value, $key) {
66+
array_walk($elements, function (&$value, $key) {
6767
$dataType = gettype($value);
6868
if (!in_array($dataType, ["string", "double", "boolean", "integer"])) {
6969
$dataType = "serializable";
7070
}
7171
$dataType = ucfirst($dataType);
7272
if ($dataType!='Null') {
7373
$value = call_user_func([$this, "convert{$dataType}"], $value);
74-
$value = "{$key}={$value}";
74+
$value = "{$this->addSlashes($key)}={$value}";
7575
}
7676
});
7777
$elements = array_filter($elements);
@@ -102,4 +102,23 @@ protected function convertBoolean($value)
102102
{
103103
return (($value) ? "true" : "false");
104104
}
105+
106+
/**
107+
* Returns strings with space, comma, or equals sign characters backslashed per Influx write protocol syntax
108+
*
109+
* @param string $value
110+
* @return string
111+
*/
112+
private function addSlashes($value)
113+
{
114+
return str_replace([
115+
' ',
116+
',',
117+
'='
118+
], [
119+
'\ ',
120+
'\,',
121+
'\='
122+
], $value);
123+
}
105124
}

src/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ public function query($query)
4545
{
4646
return $this->getReader()->query($query);
4747
}
48-
}
48+
}

tests/unit/Adapter/WriterTraitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public function getElements()
2525
{
2626
return [
2727
[["one" => "two"], "one=\"two\""],
28+
[["one with space" => "two"], "one\\ with\\ space=\"two\""],
29+
[["one with,comma" => "two"], "one\\ with\\,comma=\"two\""],
2830
[["one" => "two", "three" => "four"], "one=\"two\",three=\"four\""],
2931
[["one" => true, "three" => false], "one=true,three=false"],
3032
[["one" => true, "three" => 0., "four" => 1.], "one=true,three=0,four=1"],

0 commit comments

Comments
 (0)