]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
dict-sql: Add signed "int" type
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Tue, 15 Aug 2017 12:37:01 +0000 (15:37 +0300)
committerAki Tuomi <aki.tuomi@dovecot.fi>
Fri, 8 Sep 2017 10:18:32 +0000 (13:18 +0300)
src/lib-dict/dict-sql-settings.c
src/lib-dict/dict-sql-settings.h
src/lib-dict/dict-sql.c

index ba6e247d68f0df0f0843470025cc0e520475bc7f..9741adce020ba2746b72a6ebb786444f38449c27 100644 (file)
@@ -138,6 +138,8 @@ dict_sql_value_type_parse(const char *value_type, enum dict_sql_type *type_r)
                *type_r = DICT_SQL_TYPE_STRING;
        else if (strcmp(value_type, "hexblob") == 0)
                *type_r = DICT_SQL_TYPE_HEXBLOB;
+       else if (strcmp(value_type, "int") == 0)
+               *type_r = DICT_SQL_TYPE_INT;
        else if (strcmp(value_type, "uint") == 0)
                *type_r = DICT_SQL_TYPE_UINT;
        else
@@ -226,6 +228,11 @@ parse_setting(const char *key, const char *value,
                        field->variable = p_strndup(ctx->pool, value + 10,
                                                    value_len-10-1);
                        field->sql_field.value_type = DICT_SQL_TYPE_HEXBLOB;
+               } else if (strncmp(value, "${int:", 6) == 0 &&
+                          value[value_len-1] == '}') {
+                       field->variable = p_strndup(ctx->pool, value + 6,
+                                                   value_len-6-1);
+                       field->sql_field.value_type = DICT_SQL_TYPE_INT;
                } else if (strncmp(value, "${uint:", 7) == 0 &&
                           value[value_len-1] == '}') {
                        field->variable = p_strndup(ctx->pool, value + 7,
index 5ee0977fe46557bd352edaa1132fcc085cf3d46c..0f0a3aff785e2ca08814aad5b4fad333cf639b46 100644 (file)
@@ -3,6 +3,7 @@
 
 enum dict_sql_type {
        DICT_SQL_TYPE_STRING = 0,
+       DICT_SQL_TYPE_INT,
        DICT_SQL_TYPE_UINT,
        DICT_SQL_TYPE_HEXBLOB
 };
index 5adb6500ed8be8fe9f98948d005a4ed9cb66ee4e..f4a690bfff07d0873368101ae82a188f4de970ff 100644 (file)
@@ -217,6 +217,7 @@ sql_dict_value_escape(string_t *str, struct sql_dict *dict,
                      const char **error_r)
 {
        buffer_t *buf;
+       int64_t snum;
        uint64_t num;
 
        switch (value_type) {
@@ -224,6 +225,15 @@ sql_dict_value_escape(string_t *str, struct sql_dict *dict,
                str_printfa(str, "'%s%s'", sql_escape_string(dict->db, value),
                            value_suffix);
                return 0;
+       case DICT_SQL_TYPE_INT:
+               if (value_suffix[0] != '\0' || str_to_int64(value, &snum) < 0) {
+                       *error_r = t_strdup_printf(
+                               "%s field's value isn't 64bit signed integer: %s%s (in pattern: %s)",
+                               field_name, value, value_suffix, map->pattern);
+                       return -1;
+               }
+               str_printfa(str, "%"PRId64, snum);
+               return 0;
        case DICT_SQL_TYPE_UINT:
                if (value_suffix[0] != '\0' || str_to_uint64(value, &num) < 0) {
                        *error_r = t_strdup_printf(
@@ -371,6 +381,7 @@ sql_dict_result_unescape(enum dict_sql_type type, pool_t pool,
 
        switch (type) {
        case DICT_SQL_TYPE_STRING:
+       case DICT_SQL_TYPE_INT:
        case DICT_SQL_TYPE_UINT:
                return p_strdup(pool, sql_result_get_field_value(result, result_idx));
        case DICT_SQL_TYPE_HEXBLOB: