Code

parser: Let the TIMESERIES command accept optional data-source names.
[sysdb.git] / src / utils / unixsock.c
index da992c94b51601024bc03e2988ca9ff8d7a1e9d8..75221716e2479585f310c0c1dd263321dbd2735f 100644 (file)
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#if HAVE_CONFIG_H
+#      include "config.h"
+#endif /* HAVE_CONFIG_H */
+
 #include "utils/error.h"
 #include "utils/unixsock.h"
 
@@ -87,59 +91,6 @@ sdb_unixsock_get_column_count(const char *string, const char *delim)
        return count;
 } /* sdb_unixsock_get_column_count */
 
-static int
-sdb_unixsock_parse_cell(char *string, int type, sdb_data_t *data)
-{
-       char *endptr = NULL;
-
-       switch (type) {
-               case SDB_TYPE_INTEGER:
-                       errno = 0;
-                       data->data.integer = strtoll(string, &endptr, 0);
-                       break;
-               case SDB_TYPE_DECIMAL:
-                       errno = 0;
-                       data->data.decimal = strtod(string, &endptr);
-                       break;
-               case SDB_TYPE_STRING:
-                       data->data.string = string;
-                       break;
-               case SDB_TYPE_DATETIME:
-                       {
-                               double datetime = strtod(string, &endptr);
-                               data->data.datetime = DOUBLE_TO_SDB_TIME(datetime);
-                       }
-                       break;
-               case SDB_TYPE_BINARY:
-                       /* we don't support any binary information containing 0-bytes */
-                       data->data.binary.length = strlen(string);
-                       data->data.binary.datum = (const unsigned char *)string;
-                       break;
-               default:
-                       sdb_log(SDB_LOG_ERR, "unixsock: Unexpected type %i while "
-                                       "parsing query result.", type);
-                       return -1;
-       }
-
-       if ((type == SDB_TYPE_INTEGER) || (type == SDB_TYPE_DECIMAL)
-                       || (type == SDB_TYPE_DATETIME)) {
-               if (errno || (string == endptr)) {
-                       char errbuf[1024];
-                       sdb_log(SDB_LOG_ERR, "unixsock: Failed to parse string "
-                                       "'%s' as numeric value (type %i): %s", string, type,
-                                       sdb_strerror(errno, errbuf, sizeof(errbuf)));
-                       return -1;
-               }
-               else if (endptr && (*endptr != '\0'))
-                       sdb_log(SDB_LOG_WARNING, "unixsock: Ignoring garbage after "
-                                       "number while parsing numeric value (type %i): %s.",
-                                       type, endptr);
-       }
-
-       data->type = type;
-       return 0;
-} /* sdb_unixsock_parse_cell */
-
 static int
 sdb_unixsock_client_process_one_line(sdb_unixsock_client_t *client,
                char *line, sdb_unixsock_client_data_cb callback,
@@ -156,7 +107,7 @@ sdb_unixsock_client_process_one_line(sdb_unixsock_client_t *client,
        for (i = 0; i < column_count; ++i) {
                char *next;
 
-               if (! line) { /* this must no happen */
+               if (! line) { /* this must not happen */
                        sdb_log(SDB_LOG_ERR, "unixsock: Unexpected EOL while "
                                        "parsing line (expected %i columns delimited by '%s'; "
                                        "got %i): %s", column_count, delim,
@@ -174,15 +125,25 @@ sdb_unixsock_client_process_one_line(sdb_unixsock_client_t *client,
                        ++next;
                }
 
-               if (sdb_unixsock_parse_cell(line,
-                                       types ? types[i] : SDB_TYPE_STRING, &data[i]))
-                       return -1;
+               if (types && (types[i] != SDB_TYPE_STRING)) {
+                       if (sdb_data_parse(line, types[i], &data[i]))
+                               return -1;
+               }
+               else {
+                       /* Avoid a copy in common cases */
+                       data[i].type = SDB_TYPE_STRING;
+                       data[i].data.string = line;
+               }
 
                line = next;
        }
 
        if (callback(client, (size_t)column_count, data, user_data))
                return -1;
+
+       for (i = 0; i < column_count; ++i)
+               if (types && (types[i] != SDB_TYPE_STRING))
+                       sdb_data_free_datum(&data[i]);
        return 0;
 } /* sdb_unixsock_client_process_one_line */
 
@@ -320,7 +281,7 @@ sdb_unixsock_client_recv(sdb_unixsock_client_t *client,
        buffer[buflen - 1] = '\0';
 
        buflen = strlen(buffer);
-       while ((buffer[buflen - 1] == '\n') || (buffer[buflen - 1] == '\r')) {
+       while (buflen && ((buffer[buflen - 1] == '\n') || (buffer[buflen - 1] == '\r'))) {
                buffer[buflen - 1] = '\0';
                --buflen;
        }