예제 #1
0
파일: val.c 프로젝트: NormB/opensips
/*
 * Used when converting result from a query
 */
int db_postgres_val2str(const db_con_t* _con, const db_val_t* _v,
														char* _s, int* _len)
{
	int l, ret;
	int pgret;
	char *tmp_s;
	size_t tmp_len;
	char* old_s;

	if ((!_v) || (!_s) || (!_len) || (!*_len)) {
		LM_ERR("invalid parameter value\n");
		return -1;
	}

	if (VAL_NULL(_v)) {
		if ( *_len < (l=(int)sizeof("NULL")-1)) {
			LM_ERR("buffer too short to print NULL\n");
			return -1;
		}
		memcpy(_s, "NULL", l);
		*_len = l;
		return 0;
	}

	switch(VAL_TYPE(_v)) {
	case DB_INT:
		if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
			LM_ERR("failed to convert string to int\n");
			return -2;
		} else {
			return 0;
		}
		break;

	case DB_BIGINT:
		if (db_bigint2str(VAL_BIGINT(_v), _s, _len) < 0) {
			LM_ERR("failed to convert string to big int\n");
			return -2;
		} else {
			return 0;
		}
		break;

	case DB_BITMAP:
		if (db_int2str(VAL_BITMAP(_v), _s, _len) < 0) {
			LM_ERR("failed to convert string to int\n");
			return -3;
		} else {
			return 0;
		}
		break;

	case DB_DOUBLE:
		if (db_double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
			LM_ERR("failed to convert string to double\n");
			return -3;
		} else {
			return 0;
		}
		break;

	case DB_STRING:
		l = strlen(VAL_STRING(_v));
		if (*_len < (l * 2 + 3)) {
			LM_ERR("destination STRING buffer too short (have %d, need %d)\n",
			       *_len, l * 2 + 3);
			return -4;
		} else {
			old_s = _s;
			*_s++ = '\'';
			ret = PQescapeStringConn(CON_CONNECTION(_con), _s, VAL_STRING(_v),
					l, &pgret);
			if(pgret!=0)
			{
				LM_ERR("PQescapeStringConn failed\n");
				return -4;
			}
			LM_DBG("PQescapeStringConn: in: %d chars,"
				" out: %d chars\n", l, ret);
			_s += ret;
			*_s++ = '\'';
			*_s = '\0'; /* FIXME */
			*_len = _s - old_s;
			return 0;
		}
		break;

	case DB_STR:
		l = VAL_STR(_v).len;
		if (*_len < (l * 2 + 3)) {
			LM_ERR("destination STR buffer too short (have %d, need %d)\n",
			       *_len, l * 2 + 3);
			return -5;
		} else {
			old_s = _s;
			*_s++ = '\'';
			ret = PQescapeStringConn(CON_CONNECTION(_con), _s, VAL_STRING(_v),
					l, &pgret);
			if(pgret!=0)
			{
				LM_ERR("PQescapeStringConn failed \n");
				return -5;
			}
	        LM_DBG("PQescapeStringConn: in: %d chars, out: %d chars\n", l, ret);
			_s += ret;
			*_s++ = '\'';
			*_s = '\0'; /* FIXME */
			*_len = _s - old_s;
			return 0;
		}
		break;

	case DB_DATETIME:
		if (db_time2str(VAL_TIME(_v), _s, _len) < 0) {
			LM_ERR("failed to convert string to time_t\n");
			return -6;
		} else {
			return 0;
		}
		break;

	case DB_BLOB:
		l = VAL_BLOB(_v).len;
		/* this estimation is not always correct, thus we need to check later again */
		if (*_len < (l * 2 + 3)) {
			LM_ERR("destination BLOB buffer too short (have %d, need %d)\n",
			       *_len, l * 2 + 3);
			return -7;
		} else {
			*_s++ = '\'';
			tmp_s = (char*)PQescapeByteaConn(CON_CONNECTION(_con), (unsigned char*)VAL_STRING(_v),
					(size_t)l, (size_t*)&tmp_len);
			if(tmp_s==NULL)
			{
				LM_ERR("PQescapeBytea failed\n");
				return -7;
			}
			if (tmp_len > *_len) {
				LM_ERR("escaped result too long\n");
				return -7;
			}
			memcpy(_s, tmp_s, tmp_len);
			PQfreemem(tmp_s);
			tmp_len = strlen(_s);
			*(_s + tmp_len) = '\'';
			*(_s + tmp_len + 1) = '\0';
			*_len = tmp_len + 2;
			return 0;
		}
		break;

	default:
		LM_DBG("unknown data type\n");
		return -7;
	}
}
예제 #2
0
파일: val.c 프로젝트: KISSMonX/opensips
/*
 * Used when converting values to be used in a DB query
 */
int db_mysql_val2str(const db_con_t* _c, const db_val_t* _v, char* _s, int* _len)
{
	int l;
	char* old_s;

	if (!_c || !_v || !_s || !_len || !*_len) {
		LM_ERR("invalid parameter value\n");
		return -1;
	}

	if (VAL_NULL(_v)) {
		if (*_len < sizeof("NULL")) {
			LM_ERR("buffer too small\n");
			return -1;
		}
		*_len = snprintf(_s, *_len, "NULL");
		return 0;
	}
	
	switch(VAL_TYPE(_v)) {
	case DB_INT:
		if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
			LM_ERR("error while converting string to int\n");
			return -2;
		} else {
			return 0;
		}
		break;

	case DB_BIGINT:
		if (db_bigint2str(VAL_BIGINT(_v), _s, _len) < 0) {
			LM_ERR("error while converting bigint to string\n");
			return -2;
		} else {
			return 0;
		}
		break;


	case DB_BITMAP:
		if (db_int2str(VAL_BITMAP(_v), _s, _len) < 0) {
			LM_ERR("error while converting string to int\n");
			return -3;
		} else {
			return 0;
		}
		break;

	case DB_DOUBLE:
		if (db_double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
			LM_ERR("error while converting string to double\n");
			return -4;
		} else {
			return 0;
		}
		break;

	case DB_STRING:
		l = strlen(VAL_STRING(_v));
		if (*_len < (l * 2 + 3)) {
			LM_ERR("destination buffer too short\n");
			return -5;
		} else {
			old_s = _s;
			*_s++ = '\'';
			_s += mysql_real_escape_string(CON_CONNECTION(_c), _s, VAL_STRING(_v), l);
			*_s++ = '\'';
			*_s = '\0'; /* FIXME */
			*_len = _s - old_s;
			return 0;
		}
		break;

	case DB_STR:
		if (*_len < (VAL_STR(_v).len * 2 + 3)) {
			LM_ERR("destination buffer too short\n");
			return -6;
		} else {
			old_s = _s;
			*_s++ = '\'';
			_s += mysql_real_escape_string(CON_CONNECTION(_c), _s, VAL_STR(_v).s, VAL_STR(_v).len);
			*_s++ = '\'';
			*_s = '\0';
			*_len = _s - old_s;
			return 0;
		}
		break;

	case DB_DATETIME:
		if (db_time2str(VAL_TIME(_v), _s, _len) < 0) {
			LM_ERR("error while converting string to time_t\n");
			return -7;
		} else {
			return 0;
		}
		break;

	case DB_BLOB:
		l = VAL_BLOB(_v).len;
		if (*_len < (l * 2 + 3)) {
			LM_ERR("destination buffer too short\n");
			return -8;
		} else {
			old_s = _s;
			*_s++ = '\'';
			_s += mysql_real_escape_string(CON_CONNECTION(_c), _s, VAL_STR(_v).s, l);
			*_s++ = '\'';
			*_s = '\0';
			*_len = _s - old_s;
			return 0;
		}			
		break;

	default:
		LM_DBG("unknown data type\n");
		return -9;
	}
	/*return -8; --not reached*/
}
예제 #3
0
파일: km_bdb_val.c 프로젝트: 2pac/kamailio
/*
 * Used when converting result from a query
 */
int km_bdb_val2str(db_val_t* _v, char* _s, int* _len)
{
	int l;

	if (VAL_NULL(_v)) 
	{
		*_len = snprintf(_s, *_len, "NULL");
		return 0;
	}
	
	switch(VAL_TYPE(_v)) {
	case DB1_INT:
		if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
			LM_ERR("Error while converting int to string\n");
			return -2;
		} else {
			LM_DBG("Converted int to string\n");
			return 0;
		}
		break;

	case DB1_BITMAP:
		if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
			LM_ERR("Error while converting bitmap to string\n");
			return -3;
		} else {
			LM_DBG("Converted bitmap to string\n");
			return 0;
		}
		break;

	case DB1_DOUBLE:
		if (db_double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
			LM_ERR("Error while converting double  to string\n");
			return -3;
		} else {
			LM_DBG("Converted double to string\n");
			return 0;
		}
		break;

	case DB1_STRING:
		l = strlen(VAL_STRING(_v));
		if (*_len < l ) 
		{	LM_ERR("Destination buffer too short for string\n");
			return -4;
		} 
		else 
		{	LM_DBG("Converted string to string\n");
			strncpy(_s, VAL_STRING(_v) , l);
			_s[l] = 0;
			*_len = l;
			return 0;
		}
		break;

	case DB1_STR:
		l = VAL_STR(_v).len;
		if (*_len < l) 
		{
			LM_ERR("Destination buffer too short for str\n");
			return -5;
		} 
		else 
		{
			LM_DBG("Converted str to string\n");
			strncpy(_s, VAL_STR(_v).s , VAL_STR(_v).len);
			*_len = VAL_STR(_v).len;
			return 0;
		}
		break;

	case DB1_DATETIME:
		if (km_bdb_time2str(VAL_TIME(_v), _s, _len) < 0) {
			LM_ERR("Error while converting time_t to string\n");
			return -6;
		} else {
			LM_DBG("Converted time_t to string\n");
			return 0;
		}
		break;

	case DB1_BLOB:
		l = VAL_BLOB(_v).len;
		if (*_len < l) 
		{
			LM_ERR("Destination buffer too short for blob\n");
			return -7;
		} 
		else 
		{
			LM_DBG("Converting BLOB [%s]\n", _s);
			_s = VAL_BLOB(_v).s;
			*_len = 0;
			return -8;
		}
		break;

	default:
		LM_DBG("Unknown data type\n");
		return -8;
	}
}
예제 #4
0
파일: val.c 프로젝트: alias-neo/opensips
/*
 * Used when converting values to be used in a DB query
 */
int db_sqlite_val2str(const db_con_t* _c, const db_val_t* _v, char* _s, int* _len)
{
	int l;

	if (!_c || !_v || !_s || !_len || !*_len) {
		LM_ERR("invalid parameter value\n");
		return -1;
	}

	if (VAL_NULL(_v)) {
		if (*_len < sizeof("NULL")) {
			LM_ERR("buffer too small\n");
			return -1;
		}
		*_len = snprintf(_s, *_len, "NULL");
		return 0;
	}

	switch(VAL_TYPE(_v)) {
	case DB_INT:
		if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
			LM_ERR("error while converting string to int\n");
			return -2;
		} else {
			return 0;
		}
		break;

	case DB_BIGINT:
		if (db_bigint2str(VAL_BIGINT(_v), _s, _len) < 0) {
			LM_ERR("error while converting bigint to string\n");
			return -2;
		} else {
			return 0;
		}
		break;


	case DB_BITMAP:
		if (db_int2str(VAL_BITMAP(_v), _s, _len) < 0) {
			LM_ERR("error while converting string to int\n");
			return -3;
		} else {
			return 0;
		}
		break;

	case DB_DOUBLE:
		if (db_double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
			LM_ERR("error while converting string to double\n");
			return -4;
		} else {
			return 0;
		}
		break;

	case DB_STRING:
		/* TODO check escpaing */
		l = strlen(VAL_STRING(_v));
		if (*_len < l )
		{	LM_ERR("Destination buffer too short for string\n");
			return -4;
		}
		else
		{	LM_DBG("Converted string to string\n");
			strncpy(_s, VAL_STRING(_v) , l);
			_s[l] = 0;
			*_len = l+1; /* count the 0 also */
			return 0;
		}
		break;

	case DB_STR:
		/* TODO check escpaing */
		l = VAL_STR(_v).len;
		if (*_len < l)
		{
			LM_ERR("Destination buffer too short for str\n");
			return -5;
		}
		else
		{
			LM_DBG("Converted str to string\n");
			strncpy(_s, VAL_STR(_v).s , l);
			*_len = l;
			return 0;
		}
		break;


		break;

	case DB_DATETIME:
		if (db_time2str(VAL_TIME(_v), _s, _len) < 0) {
			LM_ERR("error while converting string to time_t\n");
			return -7;
		} else {
			return 0;
		}
		break;

	case DB_BLOB:
		/* TODO check escpaing */

		l = VAL_BLOB(_v).len;
		if (*_len < l)
		{
			LM_ERR("Destination buffer too short for blob\n");
			return -7;
		}
		else
		{
			strncpy(_s, VAL_BLOB(_v).s , l);
			LM_DBG("Converting BLOB [%.*s]\n", l,_s);
			*_len = l;
			return 0;
		}
		break;

	default:
		LM_DBG("unknown data type\n");
		return -9;
	}
}