]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
dict-sql: Use array for type names
authorAki Tuomi <aki.tuomi@open-xchange.com>
Tue, 25 Apr 2023 12:01:29 +0000 (15:01 +0300)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Tue, 2 May 2023 09:55:12 +0000 (09:55 +0000)
Ensures that type names match with enum.

src/lib-dict-backend/dict-sql-settings.c
src/lib-dict-backend/dict-sql-settings.h
src/lib-dict-backend/dict-sql.c

index 71e2f101e382ca7761920e08e84cc375609d580a..fb626e0b4f56cceec76095efbb6780a28193e5fa 100644 (file)
@@ -52,6 +52,16 @@ struct dict_sql_settings_cache {
 
 static HASH_TABLE(const char *, struct dict_sql_settings_cache *) dict_sql_settings_cache;
 
+static const char *dict_sql_type_names[] = {
+       "string",
+       "int",
+       "uint",
+       "double",
+       "hexblob",
+       "uuid",
+};
+static_assert_array_size(dict_sql_type_names, DICT_SQL_TYPE_COUNT);
+
 static const char *pattern_read_name(const char **pattern)
 {
        const char *p = *pattern, *name;
@@ -135,21 +145,14 @@ static const char *dict_sql_fields_map(struct setting_parser_ctx *ctx)
 static bool
 dict_sql_value_type_parse(const char *value_type, enum dict_sql_type *type_r)
 {
-       if (strcmp(value_type, "string") == 0)
-               *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 if (strcmp(value_type, "double") == 0)
-               *type_r = DICT_SQL_TYPE_DOUBLE;
-       else if (strcmp(value_type, "uuid") == 0)
-               *type_r = DICT_SQL_TYPE_UUID;
-       else
-               return FALSE;
-       return TRUE;
+       for (enum dict_sql_type type = DICT_SQL_TYPE_STRING;
+           type < DICT_SQL_TYPE_COUNT; type++) {
+               if (strcmp(value_type, dict_sql_type_names[type]) == 0) {
+                       *type_r = type;
+                       return TRUE;
+               }
+       }
+       return FALSE;
 }
 
 static const char *dict_sql_map_finish(struct setting_parser_ctx *ctx)
index 98437166ec8fcbe262a836962f197e59b8a47ef1..cff5a44b37ad7e878521c0d569ab92a38b1273c8 100644 (file)
@@ -8,6 +8,7 @@ enum dict_sql_type {
        DICT_SQL_TYPE_DOUBLE,
        DICT_SQL_TYPE_HEXBLOB,
        DICT_SQL_TYPE_UUID,
+       DICT_SQL_TYPE_COUNT
 };
 
 struct dict_sql_field {
index 94062a2cd2a2f20a7a7de759d475e03f27b6954a..302f47f2b6d419e8b2839339ee82b6f82da9f7b4 100644 (file)
@@ -263,6 +263,8 @@ sql_dict_statement_bind(struct sql_statement *stmt, unsigned int column_idx,
        case DICT_SQL_TYPE_UUID:
                sql_statement_bind_uuid(stmt, column_idx, param->value_uuid);
                break;
+       case DICT_SQL_TYPE_COUNT:
+               i_unreached();
        }
 }
 
@@ -347,6 +349,8 @@ sql_dict_value_get(const struct dict_sql_map *map,
                return 0;
        case DICT_SQL_TYPE_HEXBLOB:
                break;
+       case DICT_SQL_TYPE_COUNT:
+               i_unreached();
        }
 
        buf = t_buffer_create(strlen(value)/2);
@@ -525,6 +529,8 @@ sql_dict_result_unescape(enum dict_sql_type type, pool_t pool,
                return guid_128_to_uuid_string(guid, FORMAT_RECORD);
        case DICT_SQL_TYPE_HEXBLOB:
                break;
+       case DICT_SQL_TYPE_COUNT:
+               i_unreached();
        }
 
        data = sql_result_get_field_value_binary(result, result_idx, &size);