Exemplo n.º 1
0
/*
 * Reload domain table to new hash table and when done, make new hash table
 * current one.
 */
int reload_tables ( void )
{
    db_key_t cols[4];
    db1_res_t* res = NULL;
    db_row_t* row;
    struct domain_list **new_hash_table;
    int i;
    short type;
    str did, domain, name, value;
    int_str val;

    /* Choose new hash table and free its old contents */
    if (*hash_table == hash_table_1) {
	hash_table_free(hash_table_2);
	new_hash_table = hash_table_2;
    } else {
	hash_table_free(hash_table_1);
	new_hash_table = hash_table_1;
    }

    cols[0] = &did_col;
    cols[1] = &name_col;
    cols[2] = &type_col;
    cols[3] = &value_col;

    if (domain_db_init(&d_db_url) < 0) {
	LM_ERR("unable to open database connection\n");
	return -1;
    }

    if (domain_dbf.use_table(db_handle, &domain_attrs_table) < 0) {
	LM_ERR("error while trying to use domain_attrs table\n");
	goto err;
    }

    if (domain_dbf.query(db_handle, NULL, 0, NULL, cols, 0, 4, 0, &res) < 0) {
	LM_ERR("error while querying database\n");
	goto err;
    }

    row = RES_ROWS(res);

    LM_DBG("number of rows in domain_attrs table: %d\n", RES_ROW_N(res));
    
    for (i = 0; i < RES_ROW_N(res); i++) {

	row = RES_ROWS(res) + i;

	if ((VAL_NULL(ROW_VALUES(row)) == 1) ||
	    (VAL_TYPE(ROW_VALUES(row)) != DB1_STRING)) {
	    LM_ERR("did at row <%u> is null or not string\n", i);
	    goto err;
	}
	did.s = (char *)VAL_STRING(ROW_VALUES(row));
	did.len = strlen(did.s);
	if (did.len == 0) {
	    LM_ERR("did at row <%u> is empty string\n", i);
	    goto err;
	}

	if ((VAL_NULL(ROW_VALUES(row) + 1) == 1) ||
	    (VAL_TYPE(ROW_VALUES(row) + 1) != DB1_STRING)) {
	    LM_ERR("name at row <%u> is null or not string\n", i);
	    goto err;
	}
	name.s = (char *)VAL_STRING(ROW_VALUES(row) + 1);
	name.len = strlen(name.s);
	if (name.len == 0) {
	    LM_ERR("name at row <%u> is empty string\n", i);
	    goto err;
	}

	if ((VAL_NULL(ROW_VALUES(row) + 2) == 1) ||
	    ((VAL_TYPE(ROW_VALUES(row) + 2) != DB1_INT) &&
	     (VAL_TYPE(ROW_VALUES(row) + 2) != DB1_BIGINT))) {
	    LM_ERR("type at row <%u> is null or not int\n", i);
	    goto err;
	}
	if(VAL_TYPE(ROW_VALUES(row) + 2) == DB1_BIGINT) {
		type = (int)VAL_BIGINT(ROW_VALUES(row) + 2);
	} else {
		type = (int)VAL_INT(ROW_VALUES(row) + 2);
	}
	if ((type != 0) && (type != 2)) {
	    LM_ERR("unknown type <%d> at row <%u>\n", type, i);
	    goto err;
	}

	if ((VAL_NULL(ROW_VALUES(row) + 3) == 1) ||
	    (VAL_TYPE(ROW_VALUES(row) + 3) != DB1_STRING)) {
	    LM_ERR("value at row <%u> is null or not string\n", i);
	    goto err;
	}
	value.s = (char *)VAL_STRING(ROW_VALUES(row) + 3);
	value.len = strlen(value.s);

	if (type == 0) {
	    if (str2sint(&value, &(val.n)) == -1) {
		LM_ERR("value at row <%u> is invalid int\n", i);
		goto err;
	    }
	} else {
	    val.s = value;
	}

	if (type == 0) 
	    LM_DBG("inserting <did/name/type/value> = <%s/%s/%d/%d> into attribute list\n", did.s, name.s, type, val.n);
	else
	    LM_DBG("inserting <did/name/type/value> = <%s/%s/%d/%s> into attribute list\n", did.s, name.s, type, val.s.s);
		
	if (hash_table_attr_install(new_hash_table, &did, &name, type,
				    &val) == -1) {
	    LM_ERR("could not install attribute into hash table\n");
	    goto err;
	}
    }

    domain_dbf.free_result(db_handle, res);
    res = NULL;

    cols[0] = &domain_col;
    cols[1] = &did_col;
    
    if (domain_dbf.use_table(db_handle, &domain_table) < 0) {
	LM_ERR("error while trying to use domain table\n");
	goto err;
    }

    if (domain_dbf.query(db_handle, NULL, 0, NULL, cols, 0, 2, 0, &res) < 0) {
	LM_ERR("error while querying database\n");
	goto err;
    }

    row = RES_ROWS(res);

    LM_DBG("number of rows in domain table: %d\n", RES_ROW_N(res));
		
    for (i = 0; i < RES_ROW_N(res); i++) {
	
	row = RES_ROWS(res) + i;

	if ((VAL_NULL(ROW_VALUES(row)) == 1) ||
	    (VAL_TYPE(ROW_VALUES(row)) != DB1_STRING)) {
	    LM_ERR("domain at row <%u> is null or not string\n", i);
	    goto err;
	}
	domain.s = (char *)VAL_STRING(ROW_VALUES(row));
	domain.len = strlen(domain.s);
	if (domain.len == 0) {
	    LM_ERR("domain at row <%u> is empty string\n", i);
	    goto err;
	}

	if ((VAL_NULL(ROW_VALUES(row) + 1) != 1) &&
	    (VAL_TYPE(ROW_VALUES(row) + 1) != DB1_STRING)) {
	    LM_ERR("did at row <%u> is not null or string\n", i);
	    goto err;
	}
	if (VAL_NULL(ROW_VALUES(row) + 1) == 1) {
	    did.s = domain.s;
	    did.len = domain.len;
	} else {
	    did.s = (char *)VAL_STRING(ROW_VALUES(row) + 1);
	    did.len = strlen(did.s);
	    if (did.len == 0) {
		LM_ERR("did at row <%u> is empty string\n", i);
		goto err;
	    }
	}

	LM_DBG("inserting <did/domain> = <%s/%s> into hash table\n",
	       did.s, domain.s);

	if (hash_table_install(new_hash_table, &did, &domain) == -1) {
	    LM_ERR("could not install domain into hash table\n");
	    domain_dbf.free_result(db_handle, res);
	    goto err;
	}
    }

    domain_dbf.free_result(db_handle, res);
    res = NULL;
    
    *hash_table = new_hash_table;

    domain_db_close();
    return 1;

 err:
    domain_dbf.free_result(db_handle, res);
    res = NULL;
    domain_db_close();
    return -1;
}
Exemplo n.º 2
0
/*
 * Reload domain table to new hash table and when done, make new hash table
 * current one.
 */
int reload_domain_table ( void )
{
	db_key_t cols[2];
	db_res_t* res = NULL;
	db_row_t* row;
	db_val_t* val;

	struct domain_list **new_hash_table;
	int i;

	str domain, attrs;

	cols[0] = &domain_col;
	cols[1] = &domain_attrs_col;

	if (domain_dbf.use_table(db_handle, &domain_table) < 0) {
		LM_ERR("Error while trying to use domain table\n");
		return -3;
	}

	if (domain_dbf.query(db_handle, NULL, 0, NULL, cols, 0, 2, 0, &res) < 0) {
		LM_ERR("Error while querying database\n");
		return -3;
	}

	/* Choose new hash table and free its old contents */
	if (*hash_table == hash_table_1) {
		hash_table_free(hash_table_2);
		new_hash_table = hash_table_2;
	} else {
		hash_table_free(hash_table_1);
		new_hash_table = hash_table_1;
	}

	row = RES_ROWS(res);

	LM_DBG("Number of rows in domain table: %d\n", RES_ROW_N(res));

	for (i = 0; i < RES_ROW_N(res); i++) {
		val = ROW_VALUES(row + i);
		if (VAL_TYPE(val) == DB_STRING) {
			domain.s = (char *)VAL_STRING(val);
			domain.len = strlen(domain.s);
		} else if (VAL_TYPE(val) == DB_STR) {
			domain = VAL_STR(val);
		} else {
			LM_ERR("Database problem on domain column\n");
			domain_dbf.free_result(db_handle, res);
			return -3;
		}
		if (VAL_NULL(val + 1)) {
			/* add a marker to determine whether the attributes exist or not */
			attrs.len = 0;
			attrs.s = NULL;
		} else if (VAL_TYPE(val + 1) == DB_STRING) {
			attrs.s = (char *)VAL_STRING(val + 1);
			attrs.len = strlen(attrs.s);
		} else if (VAL_TYPE(val + 1) == DB_STR) {
			attrs = VAL_STR(val + 1);
		} else {
			LM_ERR("Database problem on attrs column\n");
			domain_dbf.free_result(db_handle, res);
			return -3;
		}
		LM_DBG("Value: %s inserted into domain hash table\n",VAL_STRING(val));

		if (hash_table_install(new_hash_table, &domain, &attrs)==-1){
			LM_ERR("Hash table problem\n");
			domain_dbf.free_result(db_handle, res);
			return -3;
		}
	}
	domain_dbf.free_result(db_handle, res);

	*hash_table = new_hash_table;

	return 1;
}
Exemplo n.º 3
0
/*
 * Reload domain table to new hash table and when done, make new hash table
 * current one.
 */
int reload_domain_table ( void )
{
/*	db_key_t keys[] = {domain_col}; */
	db_val_t vals[1];
	db_key_t cols[1];
	db_res_t* res;
	db_row_t* row;
	db_val_t* val;

	struct domain_list **new_hash_table;
	int i;

	cols[0] = domain_col.s;

	if (domain_dbf.use_table(db_handle, domain_table.s) < 0) {
		LOG(L_ERR, "reload_domain_table(): Error while trying to use domain table\n");
		return -1;
	}

	VAL_TYPE(vals) = DB_STR;
	VAL_NULL(vals) = 0;
    
	if (domain_dbf.query(db_handle, NULL, 0, NULL, cols, 0, 1, 0, &res) < 0) {
		LOG(L_ERR, "reload_domain_table(): Error while querying database\n");
		return -1;
	}

	/* Choose new hash table and free its old contents */
	if (*hash_table == hash_table_1) {
		hash_table_free(hash_table_2);
		new_hash_table = hash_table_2;
	} else {
		hash_table_free(hash_table_1);
		new_hash_table = hash_table_1;
	}

	row = RES_ROWS(res);

	DBG("Number of rows in domain table: %d\n", RES_ROW_N(res));
		
	for (i = 0; i < RES_ROW_N(res); i++) {
		val = ROW_VALUES(row + i);
		if ((ROW_N(row) == 1) && (VAL_TYPE(val) == DB_STRING)) {
			
			DBG("Value: %s inserted into domain hash table\n", VAL_STRING(val));

			if (hash_table_install(new_hash_table, (char *)(VAL_STRING(val))) == -1) {
				LOG(L_ERR, "domain_reload(): Hash table problem\n");
				domain_dbf.free_result(db_handle, res);
				return -1;
			}
		} else {
			LOG(L_ERR, "domain_reload(): Database problem\n");
			domain_dbf.free_result(db_handle, res);
			return -1;
		}
	}
	domain_dbf.free_result(db_handle, res);

	*hash_table = new_hash_table;
	
	return 1;
}