Example #1
0
/*
 * Initialize database module
 * No function should be called before this
 */
db_con_t* db_init(const char* _url)
{
	struct db_id* id;
	struct my_con* con;
	db_con_t* res;

	id = 0;
	res = 0;

	/* if called from PROC_MAIN, allow it only from mod_init( when pt==0)*/
	if (is_main && fixup_complete){
		LOG(L_ERR, "BUG: mysql: db_init: called from the main process,"
					" ignoring...\n");
	}

	if (!_url) {
		LOG(L_ERR, "db_init: Invalid parameter value\n");
		return 0;
	}

	res = pkg_malloc(sizeof(db_con_t) + sizeof(struct my_con*));
	if (!res) {
		LOG(L_ERR, "db_init: No memory left\n");
		return 0;
	}
	memset(res, 0, sizeof(db_con_t) + sizeof(struct my_con*));

	id = new_db_id(_url);
	if (!id) {
		LOG(L_ERR, "db_init: Cannot parse URL '%s'\n", _url);
		goto err;
	}

	     /* Find the connection in the pool */
	con = (struct my_con*)pool_get(id);
	if (!con) {
		DBG("db_init: Connection '%s' not found in pool\n", _url);
		     /* Not in the pool yet */
		con = new_connection(id);
		if (!con) {
			goto err;
		}
		pool_insert((struct pool_con*)con);
	} else {
		DBG("db_init: Connection '%s' found in pool\n", _url);
	}

	res->tail = (unsigned long)con;
	return res;

 err:
	if (id) free_db_id(id);
	if (res) pkg_free(res);
	return 0;
}
Example #2
0
/*
 * Initialize database module
 * No function should be called before this
 */
db_con_t* db_mysql_init(const char* _url)
{
    struct db_id* id;
    struct my_con* con;
    db_con_t* res;

    id = 0;
    res = 0;

    if (!_url) {
        LM_ERR("invalid parameter value\n");
        return 0;
    }

    res = pkg_malloc(sizeof(db_con_t) + sizeof(struct my_con*));
    if (!res) {
        LM_ERR("no private memory left\n");
        return 0;
    }
    memset(res, 0, sizeof(db_con_t) + sizeof(struct my_con*));

    id = new_db_id(_url);
    if (!id) {
        LM_ERR("cannot parse URL '%s'\n", _url);
        goto err;
    }

    /* Find the connection in the pool */
    con = (struct my_con*)pool_get(id);
    if (!con) {
        LM_DBG("connection '%s' not found in pool\n", _url);
        /* Not in the pool yet */
        con = db_mysql_new_connection(id);
        if (!con) {
            goto err;
        }
        pool_insert((struct pool_con*)con);
    } else {
        LM_DBG("connection '%s' found in pool\n", _url);
    }

    res->tail = (unsigned long)con;
    return res;

err:
    if (id) free_db_id(id);
    if (res) pkg_free(res);
    return 0;
}
Example #3
0
/* creates an url string without password field*/
static void db_get_url(const str* url){
	struct db_id* id = new_db_id(url);
	static str scheme_delimiter={"://",3};
	static str port_delimiter={":",1};
	static str host_delimiter={"@",1};
	static str database_delimiter={"/",1};
	str port;

	/* allocate memory for the database url if necessary*/
	database_url.len = 0;

	/* sanity checks */
	if (id == NULL)
		return;

	database_url.s = pkg_realloc(database_url.s, url->len * sizeof(char));

	if (database_url.s == NULL) {
		free_db_id(id);
		return;
	}

	/* shortest database_url is s://a/b so we always need the scheme delimiter*/
	if (id->scheme != NULL) {
		memcpy(database_url.s + database_url.len, id->scheme, strlen(id->scheme));
		database_url.len += strlen(id->scheme);
		memcpy(database_url.s + database_url.len, scheme_delimiter.s, scheme_delimiter.len);
		database_url.len += scheme_delimiter.len;
	}

	if (id->username != NULL) {
		memcpy(database_url.s + database_url.len, id->username, strlen(id->username));
		database_url.len += strlen(id->username);
	}

	if (id->host != NULL) {
		memcpy(database_url.s + database_url.len, host_delimiter.s, host_delimiter.len);
		database_url.len += host_delimiter.len;
		memcpy(database_url.s + database_url.len, id->host, strlen(id->host));
		database_url.len += strlen(id->host);
	}

	if (id->port > 0) {
		port.s = int2str(id->port,&port.len);
		memcpy(database_url.s + database_url.len, port_delimiter.s, port_delimiter.len);
		database_url.len += port_delimiter.len;
		memcpy(database_url.s + database_url.len, port.s, port.len);
		database_url.len += port.len;
	}

	if (id->database != NULL){
		memcpy(database_url.s + database_url.len,
			database_delimiter.s, database_delimiter.len);
		database_url.len += database_delimiter.len;
		memcpy(database_url.s + database_url.len, id->database, strlen(id->database));
		database_url.len += strlen(id->database);
	}

	/* free alocated memory */
	free_db_id(id);
}