Skip to content

Commit 1350fda

Browse files
authored
Merge pull request #174 from tobiasschuerg/fix/string_ref
feat: all strings by ref
2 parents 1211a8e + 0748806 commit 1350fda

20 files changed

+208
-126
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
# Changelog
2+
## unreleased
3+
### Features
4+
- [174](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/pull/174) - All API methods with a string param allow specifying string by all basic types of :
5+
- Arduino `String` class
6+
- C `char *` or `char[]`
7+
- Flash string using `F`,`PSTR` or `FPSTR` macros
8+
29
## 3.10.0 [2022-01-20]
310
### Features
411
- [167](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/pull/167) - Added `InfluxDBClient::writeRecord(const char *record)`.

src/InfluxDbClient.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
static const char TooEarlyMessage[] PROGMEM = "Cannot send request yet because of applied retry strategy. Remaining ";
3636

37-
static String escapeJSONString(String &value);
37+
static String escapeJSONString(const String &value);
3838
static String precisionToString(WritePrecision precision, uint8_t version = 2) {
3939
switch(precision) {
4040
case WritePrecision::US:
@@ -352,7 +352,7 @@ char * InfluxDBClient::Batch::createData() {
352352
return buff;
353353
}
354354

355-
bool InfluxDBClient::writeRecord(String &record) {
355+
bool InfluxDBClient::writeRecord(const String &record) {
356356
return writeRecord(record.c_str());
357357
}
358358

@@ -577,11 +577,11 @@ static const char QueryDialect[] PROGMEM = "\
577577
static const char Params[] PROGMEM = ",\
578578
\"params\": {";
579579

580-
FluxQueryResult InfluxDBClient::query(String fluxQuery) {
580+
FluxQueryResult InfluxDBClient::query(const String &fluxQuery) {
581581
return query(fluxQuery, QueryParams());
582582
}
583583

584-
FluxQueryResult InfluxDBClient::query(String fluxQuery, QueryParams params) {
584+
FluxQueryResult InfluxDBClient::query(const String &fluxQuery, QueryParams params) {
585585
uint32_t rwt = getRemainingRetryTime();
586586
if(rwt > 0) {
587587
INFLUXDB_CLIENT_DEBUG("[W] Cannot query yet, pause %ds, %ds yet\n", _retryTime, rwt);
@@ -638,7 +638,7 @@ FluxQueryResult InfluxDBClient::query(String fluxQuery, QueryParams params) {
638638
}
639639

640640

641-
static String escapeJSONString(String &value) {
641+
static String escapeJSONString(const String &value) {
642642
String ret;
643643
int d = 0;
644644
int i,from = 0;

src/InfluxDbClient.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,19 @@ class InfluxDBClient {
122122
bool validateConnection();
123123
// Writes record in InfluxDB line protocol format to write buffer.
124124
// Returns true if successful, false in case of any error
125-
bool writeRecord(String &record);
125+
bool writeRecord(const String &record);
126126
bool writeRecord(const char *record);
127127
// Writes record represented by Point to buffer
128128
// Returns true if successful, false in case of any error
129129
bool writePoint(Point& point);
130130
// Sends Flux query and returns FluxQueryResult object for subsequently reading flux query response.
131131
// Use FluxQueryResult::next() method to iterate over lines of the query result.
132132
// Always call of FluxQueryResult::close() when reading is finished. Check FluxQueryResult doc for more info.
133-
FluxQueryResult query(String fluxQuery);
133+
FluxQueryResult query(const String &fluxQuery);
134134
// Sends Flux query with params and returns FluxQueryResult object for subsequently reading flux query response.
135135
// Use FluxQueryResult::next() method to iterate over lines of the query result.
136136
// Always call of FluxQueryResult::close() when reading is finished. Check FluxQueryResult doc for more info.
137-
FluxQueryResult query(String fluxQuery, QueryParams params);
137+
FluxQueryResult query(const String &fluxQuery, QueryParams params);
138138
// Forces writing of all points in buffer, even the batch is not full.
139139
// Returns true if successful, false in case of any error
140140
bool flushBuffer();

src/Options.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include "Options.h"
2929
#include "util/helpers.h"
3030

31-
WriteOptions& WriteOptions::addDefaultTag(String name, String value) {
31+
WriteOptions& WriteOptions::addDefaultTag(const String &name, const String &value) {
3232
if(_defaultTags.length() > 0) {
3333
_defaultTags += ',';
3434
}

src/Options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class WriteOptions {
8181
WriteOptions& retryInterval(uint16_t retryIntervalSec) { _retryInterval = retryIntervalSec; return *this; }
8282
WriteOptions& maxRetryInterval(uint16_t maxRetryIntervalSec) { _maxRetryInterval = maxRetryIntervalSec; return *this; }
8383
WriteOptions& maxRetryAttempts(uint16_t maxRetryAttempts) { _maxRetryAttempts = maxRetryAttempts; return *this; }
84-
WriteOptions& addDefaultTag(String name, String value);
84+
WriteOptions& addDefaultTag(const String &name, const String &value);
8585
WriteOptions& clearDefaultTags() { _defaultTags = (char *)nullptr; return *this; }
8686
};
8787

src/Point.cpp

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include "Point.h"
2929
#include "util/helpers.h"
3030

31-
Point::Point(String measurement):
31+
Point::Point(const String & measurement):
3232
_measurement(escapeKey(measurement, false)),
3333
_tags(""),
3434
_fields(""),
@@ -37,7 +37,7 @@ Point::Point(String measurement):
3737

3838
}
3939

40-
void Point::addTag(String name, String value) {
40+
void Point::addTag(const String &name, String value) {
4141
if(_tags.length() > 0) {
4242
_tags += ',';
4343
}
@@ -46,23 +46,67 @@ void Point::addTag(String name, String value) {
4646
_tags += escapeKey(value);
4747
}
4848

49-
void Point::addField(String name, long long value) {
49+
void Point::addField(const String &name, long long value) {
5050
char buff[50];
5151
snprintf(buff, 50, "%lld", value);
5252
putField(name, String(buff)+"i");
5353
}
5454

55-
void Point::addField(String name, unsigned long long value) {
55+
void Point::addField(const String &name, unsigned long long value) {
5656
char buff[50];
5757
snprintf(buff, 50, "%llu", value);
5858
putField(name, String(buff)+"i");
5959
}
6060

61-
void Point::addField(String name, const char *value) {
62-
putField(name, "\"" + escapeValue(value) + "\"");
61+
void Point::addField(const String &name, const char *value) {
62+
putField(name, escapeValue(value));
6363
}
6464

65-
void Point::putField(String name, String value) {
65+
void Point::addField(const String &name, const __FlashStringHelper *pstr) {
66+
addField(name, String(pstr));
67+
}
68+
69+
void Point::addField(const String &name, float value, int decimalPlaces) {
70+
if(!isnan(value)) putField(name, String(value, decimalPlaces));
71+
}
72+
73+
void Point::addField(const String &name, double value, int decimalPlaces) {
74+
if(!isnan(value)) putField(name, String(value, decimalPlaces));
75+
}
76+
77+
void Point::addField(const String &name, char value) {
78+
addField(name, String(value).c_str());
79+
}
80+
81+
void Point::addField(const String &name, unsigned char value) {
82+
putField(name, String(value)+"i");
83+
}
84+
85+
void Point::addField(const String &name, int value) {
86+
putField(name, String(value)+"i");
87+
}
88+
89+
void Point::addField(const String &name, unsigned int value) {
90+
putField(name, String(value)+"i");
91+
}
92+
93+
void Point::addField(const String &name, long value) {
94+
putField(name, String(value)+"i");
95+
}
96+
97+
void Point::addField(const String &name, unsigned long value) {
98+
putField(name, String(value)+"i");
99+
}
100+
101+
void Point::addField(const String &name, bool value) {
102+
putField(name, bool2string(value));
103+
}
104+
105+
void Point::addField(const String &name, const String &value) {
106+
addField(name, value.c_str());
107+
}
108+
109+
void Point::putField(const String &name, const String &value) {
66110
if(_fields.length() > 0) {
67111
_fields += ',';
68112
}
@@ -71,11 +115,11 @@ void Point::putField(String name, String value) {
71115
_fields += value;
72116
}
73117

74-
String Point::toLineProtocol(String includeTags) const {
118+
String Point::toLineProtocol(const String &includeTags) const {
75119
return createLineProtocol(includeTags);
76120
}
77121

78-
String Point::createLineProtocol(String &incTags) const {
122+
String Point::createLineProtocol(const String &incTags) const {
79123
String line;
80124
line.reserve(_measurement.length() + 1 + incTags.length() + 1 + _tags.length() + 1 + _fields.length() + 1 + _timestamp.length());
81125
line += _measurement;
@@ -125,7 +169,7 @@ void Point::setTime(unsigned long long timestamp) {
125169
_timestamp = timeStampToString(timestamp);
126170
}
127171

128-
void Point::setTime(String timestamp) {
172+
void Point::setTime(const String &timestamp) {
129173
_timestamp = timestamp;
130174
}
131175

src/Point.h

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,30 @@
3838
class Point {
3939
friend class InfluxDBClient;
4040
public:
41-
Point(String measurement);
41+
Point(const String &measurement);
4242
// Adds string tag
43-
void addTag(String name, String value);
43+
void addTag(const String &name, String value);
4444
// Add field with various types
45-
void addField(String name, float value, int decimalPlaces = 2) { if(!isnan(value)) putField(name, String(value, decimalPlaces)); }
46-
void addField(String name, double value, int decimalPlaces = 2) { if(!isnan(value)) putField(name, String(value, decimalPlaces)); }
47-
void addField(String name, char value) { addField(name, String(value).c_str()); }
48-
void addField(String name, unsigned char value) { putField(name, String(value)+"i"); }
49-
void addField(String name, int value) { putField(name, String(value)+"i"); }
50-
void addField(String name, unsigned int value) { putField(name, String(value)+"i"); }
51-
void addField(String name, long value) { putField(name, String(value)+"i"); }
52-
void addField(String name, unsigned long value) { putField(name, String(value)+"i"); }
53-
void addField(String name, bool value) { putField(name, bool2string(value)); }
54-
void addField(String name, String value) { addField(name, value.c_str()); }
55-
void addField(String name, long long value);
56-
void addField(String name, unsigned long long value);
57-
void addField(String name, const char *value);
45+
void addField(const String &name, float value, int decimalPlaces = 2);
46+
void addField(const String &name, double value, int decimalPlaces = 2);
47+
void addField(const String &name, char value);
48+
void addField(const String &name, unsigned char value);
49+
void addField(const String &name, int value);
50+
void addField(const String &name, unsigned int value);
51+
void addField(const String &name, long value);
52+
void addField(const String &name, unsigned long value);
53+
void addField(const String &name, bool value);
54+
void addField(const String &name, const String &value);
55+
void addField(const String &name, const __FlashStringHelper *pstr);
56+
void addField(const String &name, long long value);
57+
void addField(const String &name, unsigned long long value);
58+
void addField(const String &name, const char *value);
5859
// Set timestamp to `now()` and store it in specified precision, nanoseconds by default. Date and time must be already set. See `configTime` in the device API
5960
void setTime(WritePrecision writePrecision = WritePrecision::NS);
6061
// Set timestamp in offset since epoch (1.1.1970). Correct precision must be set InfluxDBClient::setWriteOptions.
6162
void setTime(unsigned long long timestamp);
6263
// Set timestamp in offset since epoch (1.1.1970 00:00:00). Correct precision must be set InfluxDBClient::setWriteOptions.
63-
void setTime(String timestamp);
64+
void setTime(const String &timestamp);
6465
// Clear all fields. Usefull for reusing point
6566
void clearFields();
6667
// Clear tags
@@ -72,7 +73,7 @@ friend class InfluxDBClient;
7273
// True if a point contains timestamp
7374
bool hasTime() const { return _timestamp.length() > 0; }
7475
// Creates line protocol with optionally added tags
75-
String toLineProtocol(String includeTags = "") const;
76+
String toLineProtocol(const String &includeTags = "") const;
7677
// returns current timestamp
7778
String getTime() const { return _timestamp; }
7879
protected:
@@ -82,8 +83,8 @@ friend class InfluxDBClient;
8283
String _timestamp;
8384
protected:
8485
// method for formating field into line protocol
85-
void putField(String name, String value);
86+
void putField(const String &name, const String &value);
8687
// Creates line protocol string
87-
String createLineProtocol(String &incTags) const;
88+
String createLineProtocol(const String &incTags) const;
8889
};
8990
#endif //_POINT_H_

src/query/FluxParser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ FluxQueryResult::FluxQueryResult(CsvReader *reader) {
3434
_data = std::make_shared<Data>(reader);
3535
}
3636

37-
FluxQueryResult::FluxQueryResult(String error):FluxQueryResult((CsvReader *)nullptr) {
37+
FluxQueryResult::FluxQueryResult(const String &error):FluxQueryResult((CsvReader *)nullptr) {
3838
_data->_error = error;
3939
}
4040

@@ -51,7 +51,7 @@ FluxQueryResult &FluxQueryResult::operator=(const FluxQueryResult &other) {
5151
FluxQueryResult::~FluxQueryResult() {
5252
}
5353

54-
int FluxQueryResult::getColumnIndex(String columnName) {
54+
int FluxQueryResult::getColumnIndex(const String &columnName) {
5555
int i = -1;
5656
std::vector<String>::iterator it = find(_data->_columnNames.begin(), _data->_columnNames.end(), columnName);
5757
if (it != _data->_columnNames.end()) {
@@ -68,7 +68,7 @@ FluxValue FluxQueryResult::getValueByIndex(int index) {
6868
return ret;
6969
}
7070

71-
FluxValue FluxQueryResult::getValueByName(String columnName) {
71+
FluxValue FluxQueryResult::getValueByName(const String &columnName) {
7272
FluxValue ret;
7373
int i = getColumnIndex(columnName);
7474
if(i > -1) {

src/query/FluxParser.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class FluxQueryResult {
5454
// Constructor for reading result
5555
FluxQueryResult(CsvReader *reader);
5656
// Constructor for error result
57-
FluxQueryResult(String error);
57+
FluxQueryResult(const String &error);
5858
// Copy constructor
5959
FluxQueryResult(const FluxQueryResult &other);
6060
// Assignment operator
@@ -64,11 +64,11 @@ class FluxQueryResult {
6464
// or an error. Call getError() and check non empty value
6565
bool next();
6666
// Returns index of the column, or -1 if not found
67-
int getColumnIndex(String columnName);
67+
int getColumnIndex(const String &columnName);
6868
// Returns a converted value by index, or nullptr in case of missing value or wrong index
6969
FluxValue getValueByIndex(int index);
7070
// Returns a result value by column name, or nullptr in case of missing value or wrong column name
71-
FluxValue getValueByName(String columnName);
71+
FluxValue getValueByName(const String &columnName);
7272
// Returns flux datatypes of all columns
7373
std::vector<String> getColumnsDatatype() { return _data->_columnDatatypes; }
7474
// Returns names of all columns

0 commit comments

Comments
 (0)