Expand RQ_INTEGER type out to multiple types, one for each precision
authorTilghman Lesher <tilghman@meg.abyt.es>
Mon, 9 Jun 2008 22:51:59 +0000 (22:51 +0000)
committerTilghman Lesher <tilghman@meg.abyt.es>
Mon, 9 Jun 2008 22:51:59 +0000 (22:51 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@121367 65c4cc65-6c06-0410-ace0-fbb531ad65f3

apps/app_meetme.c
apps/app_queue.c
apps/app_voicemail.c
channels/chan_iax2.c
channels/chan_sip.c
include/asterisk/config.h
main/utils.c
res/res_config_curl.c
res/res_config_odbc.c
res/res_config_pgsql.c

index 14ba77b..c1acfc2 100644 (file)
@@ -1743,7 +1743,10 @@ static int conf_run(struct ast_channel *chan, struct ast_conference *conf, int c
        if (rt_log_members) {
                /* Update table */
                snprintf(members, sizeof(members), "%d", conf->users);
-               ast_realtime_require_field("meetme", "confno", RQ_INTEGER, strlen(conf->confno), "members", RQ_INTEGER, strlen(members), NULL);
+               ast_realtime_require_field("meetme",
+                       "confno", strlen(conf->confno) > 7 ? RQ_UINTEGER4 : strlen(conf->confno) > 4 ? RQ_UINTEGER3 : RQ_UINTEGER2, strlen(conf->confno),
+                       "members", RQ_UINTEGER1, strlen(members),
+                       NULL);
                ast_update_realtime("meetme", "confno", conf->confno, "members", members, NULL);
        }
        setusercount = 1;
@@ -2683,7 +2686,10 @@ bailoutandtrynormal:
                        if (rt_log_members) {
                                /* Update table */
                                snprintf(members, sizeof(members), "%d", conf->users);
-                               ast_realtime_require_field("meetme", "confno", RQ_INTEGER, strlen(conf->confno), "members", RQ_INTEGER, strlen(members), NULL);
+                               ast_realtime_require_field("meetme",
+                                       "confno", strlen(conf->confno) > 7 ? RQ_UINTEGER4 : strlen(conf->confno) > 4 ? RQ_UINTEGER3 : RQ_UINTEGER2, strlen(conf->confno),
+                                       "members", RQ_UINTEGER1, strlen(members),
+                                       NULL);
                                ast_update_realtime("meetme", "confno", conf->confno, "members", members, NULL);
                        }
                        if (confflags & CONFFLAG_MARKEDUSER) 
@@ -5710,7 +5716,7 @@ static int load_module(void)
        res |= ast_devstate_prov_add("SLA", sla_state);
 
        res |= ast_custom_function_register(&meetme_info_acf);
-       ast_realtime_require_field("meetme", "confno", RQ_INTEGER, 3, "members", RQ_INTEGER, 3, NULL);
+       ast_realtime_require_field("meetme", "confno", RQ_UINTEGER2, 3, "members", RQ_UINTEGER1, 3, NULL);
 
        return res;
 }
index 2a7f1e6..9d95797 100644 (file)
@@ -6336,7 +6336,7 @@ static int load_module(void)
 
        if (!(device_state_sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE, device_state_cb, NULL, AST_EVENT_IE_END)))
                res = -1;
-       ast_realtime_require_field("queue_members", "paused", RQ_INTEGER, 1, "uniqueid", RQ_INTEGER, 5, NULL);
+       ast_realtime_require_field("queue_members", "paused", RQ_INTEGER1, 1, "uniqueid", RQ_UINTEGER2, 5, NULL);
 
        return res ? AST_MODULE_LOAD_DECLINE : 0;
 }
index 3ff833e..24b7ee3 100644 (file)
@@ -10089,8 +10089,8 @@ static int load_module(void)
        ast_cli_register_multiple(cli_voicemail, sizeof(cli_voicemail) / sizeof(struct ast_cli_entry));
 
        ast_install_vm_functions(has_voicemail, inboxcount, messagecount, sayname);
-       ast_realtime_require_field("voicemail", "uniqueid", RQ_INTEGER, 11, "password", RQ_CHAR, 10, NULL);
-       ast_realtime_require_field("voicemail_data", "filename", RQ_CHAR, 30, "duration", RQ_INTEGER, 5, NULL);
+       ast_realtime_require_field("voicemail", "uniqueid", RQ_UINTEGER3, 11, "password", RQ_CHAR, 10, NULL);
+       ast_realtime_require_field("voicemail_data", "filename", RQ_CHAR, 30, "duration", RQ_UINTEGER3, 5, NULL);
 
        return res;
 }
index e7b2d55..2886f35 100644 (file)
@@ -12188,7 +12188,7 @@ static int load_module(void)
        reload_firmware(0);
        iax_provision_reload(0);
 
-       ast_realtime_require_field("iaxpeers", "name", RQ_CHAR, 10, "ipaddr", RQ_CHAR, 15, "port", RQ_INTEGER, 5, "regseconds", RQ_INTEGER, 6, NULL);
+       ast_realtime_require_field("iaxpeers", "name", RQ_CHAR, 10, "ipaddr", RQ_CHAR, 15, "port", RQ_UINTEGER2, 5, "regseconds", RQ_UINTEGER2, 6, NULL);
 
        return AST_MODULE_LOAD_SUCCESS;
 }
index 9d9770f..6da662f 100644 (file)
@@ -22863,8 +22863,8 @@ static int load_module(void)
        ast_realtime_require_field(ast_check_realtime("sipregs") ? "sipregs" : "sippeers",
                "name", RQ_CHAR, 10,
                "ipaddr", RQ_CHAR, 15,
-               "port", RQ_INTEGER, 5,
-               "regseconds", RQ_INTEGER, 5,
+               "port", RQ_UINTEGER2, 5,
+               "regseconds", RQ_UINTEGER2, 5,   /* Max of 18 hours */
                "defaultuser", RQ_CHAR, 10,
                "fullcontact", RQ_CHAR, 20,
                "regserver", RQ_CHAR, 20,
index e1f983d..fdcd28a 100644 (file)
@@ -28,6 +28,7 @@ extern "C" {
 #endif
 
 #include "asterisk/utils.h"
+#include "asterisk/inline_api.h"
 
 struct ast_config;
 
@@ -50,7 +51,16 @@ enum {
  * \brief Types used in ast_realtime_require_field
  */
 typedef enum {
-       RQ_INTEGER,
+       RQ_INTEGER1,
+       RQ_UINTEGER1,
+       RQ_INTEGER2,
+       RQ_UINTEGER2,
+       RQ_INTEGER3,
+       RQ_UINTEGER3,
+       RQ_INTEGER4,
+       RQ_UINTEGER4,
+       RQ_INTEGER8,
+       RQ_UINTEGER8,
        RQ_CHAR,
        RQ_FLOAT,
        RQ_DATE,
@@ -451,6 +461,27 @@ int ast_parse_arg(const char *arg, enum ast_parse_flags flags,
 #define CV_DSTR(__x, __dst)    CV_F(__x, if (__dst) ast_free(__dst); __dst = ast_strdup(__val))
 #define CV_STRFIELD(__x, __obj, __field) CV_F(__x, ast_string_field_set(__obj, __field, __val))
 
+AST_INLINE_API(
+int ast_rq_is_int(require_type type),
+{
+       switch (type) {
+       case RQ_INTEGER1:
+       case RQ_UINTEGER1:
+       case RQ_INTEGER2:
+       case RQ_UINTEGER2:
+       case RQ_INTEGER3:
+       case RQ_UINTEGER3:
+       case RQ_INTEGER4:
+       case RQ_UINTEGER4:
+       case RQ_INTEGER8:
+       case RQ_UINTEGER8:
+               return 1;
+       default:
+               return 0;
+       }
+}
+)
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif
index c1a2970..b043b61 100644 (file)
@@ -59,6 +59,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #define AST_API_MODULE
 #include "asterisk/threadstorage.h"
 
+#define AST_API_MODULE
+#include "asterisk/config.h"
+
 static char base64[64];
 static char b2a[256];
 
index e0e1b06..722b08e 100644 (file)
@@ -430,7 +430,16 @@ static int require_curl(const char *url, const char *unused, va_list ap)
                ast_uri_encode(elm, field, sizeof(field), EncodeSpecialChars);
                ast_str_append(&query, 0, "%s=%s%%3A%d", field,
                        type == RQ_CHAR ? "char" :
-                       type == RQ_INTEGER ? "integer" :
+                       type == RQ_INTEGER1 ? "integer1" :
+                       type == RQ_UINTEGER1 ? "uinteger1" :
+                       type == RQ_INTEGER2 ? "integer2" :
+                       type == RQ_UINTEGER2 ? "uinteger2" :
+                       type == RQ_INTEGER3 ? "integer3" :
+                       type == RQ_UINTEGER3 ? "uinteger3" :
+                       type == RQ_INTEGER4 ? "integer4" :
+                       type == RQ_UINTEGER4 ? "uinteger4" :
+                       type == RQ_INTEGER8 ? "integer8" :
+                       type == RQ_UINTEGER8 ? "uinteger8" :
                        type == RQ_DATE ? "date" :
                        type == RQ_DATETIME ? "datetime" :
                        type == RQ_FLOAT ? "float" :
index 9173cd7..4bd6f20 100644 (file)
@@ -878,8 +878,8 @@ static struct ast_config *config_odbc(const char *database, const char *table, c
        return cfg;
 }
 
-#define warn_length(col, size) ast_log(LOG_WARNING, "Column %s is not long enough to contain realtime data (needs %d)\n", col->name, size)
-#define warn_type(col, type)   ast_log(LOG_WARNING, "Column %s is of the incorrect type to contain realtime data\n", col->name)
+#define warn_length(col, size) ast_log(LOG_WARNING, "Realtime table %s@%s: column '%s' is not long enough to contain realtime data (needs %d)\n", table, database, col->name, size)
+#define warn_type(col, type)   ast_log(LOG_WARNING, "Realtime table %s@%s: column '%s' is of the incorrect type (%d) to contain the required realtime data\n", table, database, col->name, col->type)
 
 static int require_odbc(const char *database, const char *table, va_list ap)
 {
@@ -907,15 +907,28 @@ static int require_odbc(const char *database, const char *table, va_list ap)
                                case SQL_VARBINARY:
                                case SQL_LONGVARBINARY:
                                case SQL_GUID:
-                                       if ((type == RQ_INTEGER && size > 10) || (type == RQ_CHAR && col->size < size)) {
-                                               warn_length(col, size);
-                                       } else if (type == RQ_DATE && col->size < 10) {
-                                               warn_length(col, 10);
-                                       } else if (type == RQ_DATETIME && col->size < 19) {
-                                               warn_length(col, 19);
-                                       } else if (type == RQ_FLOAT && col->size < 10) {
-                                               warn_length(col, 10);
+#define CHECK_SIZE(n) \
+                                               if (col->size < n) {      \
+                                                       warn_length(col, n);  \
+                                               }                         \
+                                               break;
+                                       switch (type) {
+                                       case RQ_UINTEGER1: CHECK_SIZE(3)  /*         255 */
+                                       case RQ_INTEGER1:  CHECK_SIZE(4)  /*        -128 */
+                                       case RQ_UINTEGER2: CHECK_SIZE(5)  /*       65535 */
+                                       case RQ_INTEGER2:  CHECK_SIZE(6)  /*      -32768 */
+                                       case RQ_UINTEGER3:                /*    16777215 */
+                                       case RQ_INTEGER3:  CHECK_SIZE(8)  /*    -8388608 */
+                                       case RQ_DATE:                     /*  2008-06-09 */
+                                       case RQ_UINTEGER4: CHECK_SIZE(10) /*  4200000000 */
+                                       case RQ_INTEGER4:  CHECK_SIZE(11) /* -2100000000 */
+                                       case RQ_DATETIME:                 /* 2008-06-09 16:03:47 */
+                                       case RQ_UINTEGER8: CHECK_SIZE(19) /* trust me    */
+                                       case RQ_INTEGER8:  CHECK_SIZE(20) /* ditto       */
+                                       case RQ_FLOAT:
+                                       case RQ_CHAR:      CHECK_SIZE(size)
                                        }
+#undef CHECK_SIZE
                                        break;
                                case SQL_TYPE_DATE:
                                        if (type != RQ_DATE) {
@@ -928,44 +941,98 @@ static int require_odbc(const char *database, const char *table, va_list ap)
                                                warn_type(col, type);
                                        }
                                        break;
-                               case SQL_INTEGER:
-                               case SQL_BIGINT:
-                               case SQL_SMALLINT:
-                               case SQL_TINYINT:
                                case SQL_BIT:
-                                       if (type != RQ_INTEGER) {
-                                               warn_type(col, type);
+                                       warn_length(col, size);
+                                       break;
+#define WARN_TYPE_OR_LENGTH(n) \
+                                               if (!ast_rq_is_int(type)) {  \
+                                                       warn_type(col, type);    \
+                                               } else {                     \
+                                                       warn_length(col, n);  \
+                                               }
+                               case SQL_TINYINT:
+                                       if (type != RQ_UINTEGER1) {
+                                               WARN_TYPE_OR_LENGTH(size)
+                                       }
+                                       break;
+                               case SQL_C_STINYINT:
+                                       if (type != RQ_INTEGER1) {
+                                               WARN_TYPE_OR_LENGTH(size)
                                        }
-                                       if ((col->type == SQL_BIT && size > 1) ||
-                                               (col->type == SQL_TINYINT && size > 2) ||
-                                               (col->type == SQL_SMALLINT && size > 4) ||
-                                               (col->type == SQL_INTEGER && size > 10)) {
-                                               warn_length(col, size);
+                                       break;
+                               case SQL_C_USHORT:
+                                       if (type != RQ_UINTEGER1 && type != RQ_INTEGER1 && type != RQ_UINTEGER2) {
+                                               WARN_TYPE_OR_LENGTH(size)
+                                       }
+                                       break;
+                               case SQL_SMALLINT:
+                               case SQL_C_SSHORT:
+                                       if (type != RQ_UINTEGER1 && type != RQ_INTEGER1 && type != RQ_INTEGER2) {
+                                               WARN_TYPE_OR_LENGTH(size)
+                                       }
+                                       break;
+                               case SQL_C_ULONG:
+                                       if (type != RQ_UINTEGER1 && type != RQ_INTEGER1 &&
+                                               type != RQ_UINTEGER2 && type != RQ_INTEGER2 &&
+                                               type != RQ_UINTEGER3 && type != RQ_INTEGER3 &&
+                                               type != RQ_INTEGER4) {
+                                               WARN_TYPE_OR_LENGTH(size)
+                                       }
+                                       break;
+                               case SQL_INTEGER:
+                               case SQL_C_SLONG:
+                                       if (type != RQ_UINTEGER1 && type != RQ_INTEGER1 &&
+                                               type != RQ_UINTEGER2 && type != RQ_INTEGER2 &&
+                                               type != RQ_UINTEGER3 && type != RQ_INTEGER3 &&
+                                               type != RQ_UINTEGER4) {
+                                               WARN_TYPE_OR_LENGTH(size)
+                                       }
+                                       break;
+                               case SQL_C_UBIGINT:
+                                       if (type != RQ_UINTEGER1 && type != RQ_INTEGER1 &&
+                                               type != RQ_UINTEGER2 && type != RQ_INTEGER2 &&
+                                               type != RQ_UINTEGER3 && type != RQ_INTEGER3 &&
+                                               type != RQ_UINTEGER4 && type != RQ_INTEGER4 &&
+                                               type != RQ_INTEGER8) {
+                                               WARN_TYPE_OR_LENGTH(size)
+                                       }
+                                       break;
+                               case SQL_BIGINT:
+                               case SQL_C_SBIGINT:
+                                       if (type != RQ_UINTEGER1 && type != RQ_INTEGER1 &&
+                                               type != RQ_UINTEGER2 && type != RQ_INTEGER2 &&
+                                               type != RQ_UINTEGER3 && type != RQ_INTEGER3 &&
+                                               type != RQ_UINTEGER4 && type != RQ_INTEGER4 &&
+                                               type != RQ_UINTEGER8) {
+                                               WARN_TYPE_OR_LENGTH(size)
                                        }
                                        break;
+#undef WARN_TYPE_OR_LENGTH
                                case SQL_NUMERIC:
                                case SQL_DECIMAL:
                                case SQL_FLOAT:
                                case SQL_REAL:
                                case SQL_DOUBLE:
-                                       if (type != RQ_INTEGER && type != RQ_FLOAT) {
+                                       if (!ast_rq_is_int(type) && type != RQ_FLOAT) {
                                                warn_type(col, type);
                                        }
                                        break;
                                default:
-                                       ast_log(LOG_WARNING, "Column type (%d) unrecognized for field '%s' in %s@%s\n", col->type, elm, table, database);
+                                       ast_log(LOG_WARNING, "Realtime table %s@%s: column type (%d) unrecognized for column '%s'\n", table, database, col->type, elm);
                                }
                                break;
                        }
                }
                if (!col) {
-                       ast_log(LOG_WARNING, "Table %s@%s requires column '%s', but that column does not exist!\n", table, database, elm);
+                       ast_log(LOG_WARNING, "Realtime table %s@%s requires column '%s', but that column does not exist!\n", table, database, elm);
                }
        }
        va_end(ap);
        AST_RWLIST_UNLOCK(&tableptr->columns);
        return 0;
 }
+#undef warn_length
+#undef warn_type
 
 static int unload_odbc(const char *database, const char *tablename)
 {
index 0afcf07..79c9c19 100644 (file)
@@ -943,14 +943,21 @@ static int require_pgsql(const char *database, const char *tablename, va_list ap
                                } else if (strncmp(column->type, "int", 3) == 0) {
                                        int typesize = atoi(column->type + 3);
                                        /* Integers can hold only other integers */
-                                       if (type == RQ_INTEGER && ((typesize == 2 && size > 4) || (typesize == 4 && size > 10))) {
+                                       if ((type == RQ_INTEGER8 || type == RQ_UINTEGER8 ||
+                                               type == RQ_INTEGER4 || type == RQ_UINTEGER4 ||
+                                               type == RQ_INTEGER3 || type == RQ_UINTEGER3 ||
+                                               type == RQ_UINTEGER2) && typesize == 2) {
                                                ast_log(LOG_WARNING, "Column '%s' may not be large enough for the required data length: %d\n", column->name, size);
                                                res = -1;
-                                       } else if (type != RQ_INTEGER) {
-                                               ast_log(LOG_WARNING, "Column '%s' is of the incorrect type: (need %s(%d) but saw %s)\n", column->name, type == RQ_CHAR ? "char" : "something else ", size, column->type);
+                                       } else if ((type == RQ_INTEGER8 || type == RQ_UINTEGER8 ||
+                                               type == RQ_UINTEGER4) && typesize == 4) {
+                                               ast_log(LOG_WARNING, "Column '%s' may not be large enough for the required data length: %d\n", column->name, size);
+                                               res = -1;
+                                       } else if (type == RQ_CHAR || type == RQ_DATETIME || type == RQ_FLOAT || type == RQ_DATE) {
+                                               ast_log(LOG_WARNING, "Column '%s' is of the incorrect type: (need %s(%d) but saw %s)\n", column->name, type == RQ_CHAR ? "char" : type == RQ_DATETIME ? "datetime" : type == RQ_DATE ? "date" : type == RQ_FLOAT ? "float" : "a rather stiff drink ", size, column->type);
                                                res = -1;
                                        }
-                               } else if (strncmp(column->type, "float", 5) == 0 && type != RQ_INTEGER && type != RQ_FLOAT) {
+                               } else if (strncmp(column->type, "float", 5) == 0 && !ast_rq_is_int(type) && type != RQ_FLOAT) {
                                        ast_log(LOG_WARNING, "Column %s cannot be a %s\n", column->name, column->type);
                                        res = -1;
                                } else { /* There are other types that no module implements yet */
@@ -965,25 +972,32 @@ static int require_pgsql(const char *database, const char *tablename, va_list ap
                        if (requirements == RQ_WARN) {
                                ast_log(LOG_WARNING, "Table %s requires a column '%s' of size '%d', but no such column exists.\n", tablename, elm, size);
                        } else {
-                               struct ast_str *sql = ast_str_create(100), *fieldtype = ast_str_create(16);
+                               struct ast_str *sql = ast_str_create(100);
+                               char fieldtype[15];
                                PGresult *res;
 
                                if (requirements == RQ_CREATECHAR || type == RQ_CHAR) {
-                                       ast_str_set(&fieldtype, 0, "CHAR(%d)", size);
-                               } else if (type == RQ_INTEGER) {
-                                       ast_str_set(&fieldtype, 0, "INT%d", size < 5 ? 2 : (size < 11 ? 4 : 8));
+                                       snprintf(fieldtype, sizeof(fieldtype), "CHAR(%d)", size);
+                               } else if (type == RQ_INTEGER1 || type == RQ_UINTEGER1 || type == RQ_INTEGER2) {
+                                       snprintf(fieldtype, sizeof(fieldtype), "INT2");
+                               } else if (type == RQ_UINTEGER2 || type == RQ_INTEGER3 || type == RQ_UINTEGER3 || type == RQ_INTEGER4) {
+                                       snprintf(fieldtype, sizeof(fieldtype), "INT4");
+                               } else if (type == RQ_UINTEGER4 || type == RQ_INTEGER8) {
+                                       snprintf(fieldtype, sizeof(fieldtype), "INT8");
+                               } else if (type == RQ_UINTEGER8) {
+                                       /* No such type on PostgreSQL */
+                                       snprintf(fieldtype, sizeof(fieldtype), "CHAR(20)");
                                } else if (type == RQ_FLOAT) {
-                                       ast_str_set(&fieldtype, 0, "FLOAT8");
+                                       snprintf(fieldtype, sizeof(fieldtype), "FLOAT8");
                                } else if (type == RQ_DATE) {
-                                       ast_str_set(&fieldtype, 0, "DATE");
+                                       snprintf(fieldtype, sizeof(fieldtype), "DATE");
                                } else if (type == RQ_DATETIME) {
-                                       ast_str_set(&fieldtype, 0, "TIMESTAMP");
+                                       snprintf(fieldtype, sizeof(fieldtype), "TIMESTAMP");
                                } else {
                                        ast_free(sql);
-                                       ast_free(fieldtype);
                                        continue;
                                }
-                               ast_str_set(&sql, 0, "ALTER TABLE %s ADD COLUMN %s %s", tablename, elm, fieldtype->str);
+                               ast_str_set(&sql, 0, "ALTER TABLE %s ADD COLUMN %s %s", tablename, elm, fieldtype);
                                ast_debug(1, "About to lock pgsql_lock (running alter on table '%s' to add column '%s')\n", tablename, elm);
 
                                ast_mutex_lock(&pgsql_lock);
@@ -991,7 +1005,6 @@ static int require_pgsql(const char *database, const char *tablename, va_list ap
                                        ast_mutex_unlock(&pgsql_lock);
                                        ast_log(LOG_ERROR, "Unable to add column: %s\n", sql->str);
                                        ast_free(sql);
-                                       ast_free(fieldtype);
                                        continue;
                                }
 
@@ -1005,7 +1018,6 @@ static int require_pgsql(const char *database, const char *tablename, va_list ap
                                ast_mutex_unlock(&pgsql_lock);
 
                                ast_free(sql);
-                               ast_free(fieldtype);
                        }
                }
        }