Example #1
0
File: res.c Project: mtulio/mtulio
/**
 * Convert rows from PostgreSQL to db API representation
 */
int db_postgres_convert_rows(const db_con_t* _h, db_res_t* _r)
{
	char **row_buf, *s;
	int row, col, len;

	if (!_h || !_r)  {
		LM_ERR("invalid parameter\n");
		return -1;
	}

	if (!RES_ROW_N(_r)) {
		LM_DBG("no rows returned from the query\n");
		RES_ROWS(_r) = 0;
		return 0;
	}
	/* Allocate an array of pointers per column to holds the string 
	 * representation */
	len = sizeof(char *) * RES_COL_N(_r);
	row_buf = (char**)pkg_malloc(len);
	if (!row_buf) {
		LM_ERR("no private memory left\n");
		return -1;
	}
	LM_DBG("allocate for %d columns %d bytes in row buffer at %p\n",
		RES_COL_N(_r), len, row_buf);
	memset(row_buf, 0, len);

	if (db_allocate_rows( _r, RES_ROW_N(_r))!=0) {
		LM_ERR("no private memory left\n");
		return -2;
	}

	for(row=RES_LAST_ROW(_r); row<(RES_LAST_ROW(_r)+RES_ROW_N(_r)) ; row++) {
		for(col = 0; col < RES_COL_N(_r); col++) {
			/*
			 * The row data pointer returned by PQgetvalue points to 
			 * storage that is part of the PGresult structure. One should 
			 * not modify the data it points to, and one must explicitly 
			 * copy the data into other storage if it is to be used past 
			 * the lifetime of the PGresult structure itself.
			 */
			
			/*
			 * There's a weird bug (or just weird behavior) in the postgres
			 * API - if the result is a BLOB (like 'text') and is with 
			 * zero length, we get a pointer to nowhere, which is not 
			 * null-terminated. The fix for this is to check what does the 
			 * DB think about the length and use that as a correction.
			 */
			if (PQgetisnull(CON_RESULT(_h), row, col) == 0) {
				/* not null value */
				if ( (len=PQgetlength(CON_RESULT(_h), row, col))==0 ) {
					s="";
					LM_DBG("PQgetvalue(%p,%d,%d)=[], zero len\n", _h, row,col);
				} else {
					s = PQgetvalue(CON_RESULT(_h), row, col);
					LM_DBG("PQgetvalue(%p,%d,%d)=[%.*s]\n", _h, row,col,len,s);
				}
			
				row_buf[col] = pkg_malloc(len+1);
				if (!row_buf[col]) {
					LM_ERR("no private memory left\n");
					return -1;
				}
				memset(row_buf[col], 0, len+1);
				LM_DBG("allocated %d bytes for row_buf[%d] at %p\n",
					len, col, row_buf[col]);

				strncpy(row_buf[col], s, len);
				LM_DBG("[%d][%d] Column[%.*s]=[%s]\n",
					row, col, RES_NAMES(_r)[col]->len,
					RES_NAMES(_r)[col]->s, row_buf[col]);
			}
		}

		/* ASSERT: row_buf contains an entire row in strings */
		if(db_postgres_convert_row(_h, _r, &(RES_ROWS(_r)[row - RES_LAST_ROW(_r)]), row_buf)<0){
			LM_ERR("failed to convert row #%d\n",  row);
			RES_ROW_N(_r) = row - RES_LAST_ROW(_r);
			for (col = 0; col < RES_COL_N(_r); col++) {
				LM_DBG("freeing row_buf[%d] at %p\n", col, row_buf[col]);
				if (row_buf[col] &&  !row_buf[col][0]) pkg_free(row_buf[col]);
			}
			LM_DBG("freeing row buffer at %p\n", row_buf);
			pkg_free(row_buf);
			return -4;
		}
		/*
		 * pkg_free() must be done for the above allocations now that the row
		 * has been converted. During pg_convert_row (and subsequent pg_str2val)
		 * processing, data types that don't need to be converted (namely STRINGS
		 * and STR) have their addresses saved. These data types should not have
		 * their pkg_malloc() allocations freed here because they are still
		 * needed.  However, some data types (ex: INT, DOUBLE) should have their
		 * pkg_malloc() allocations freed because during the conversion process,
		 * their converted values are saved in the union portion of the db_val_t
		 * structure. BLOB will be copied during PQunescape in str2val, thus it
		 * has to be freed here AND in pg_free_row().
		 *
		 * Warning: when the converted row is no longer needed, the data types
		 * whose addresses were saved in the db_val_t structure must be freed
		 * or a memory leak will happen. This processing should happen in the
		 * pg_free_row() subroutine. The caller of this routine should ensure
		 * that pg_free_rows(), pg_free_row() or pg_free_result() is eventually
		 * called.
		 */
		for (col = 0; col < RES_COL_N(_r); col++) {
			switch (RES_TYPES(_r)[col]) {
				case DB_STRING:
				case DB_STR:
					break;
				default:
					LM_DBG("freeing row_buf[%d] at %p\n", col, row_buf[col]);
					if (row_buf[col]) pkg_free(row_buf[col]);
			}
			/*
			 * The following housekeeping may not be technically required, but it
			 * is a good practice to NULL pointer fields that are no longer valid.
			 * Note that DB_STRING fields have not been pkg_free(). NULLing DB_STRING
			 * fields would normally not be good to do because a memory leak would
			 * occur.  However, the pg_convert_row() routine  has saved the DB_STRING
			 * pointer in the db_val_t structure.  The db_val_t structure will 
			 * eventually be used to pkg_free() the DB_STRING storage.
			 */
			row_buf[col] = (char *)NULL;
		}
	}

	LM_DBG("freeing row buffer at %p\n", row_buf);
	pkg_free(row_buf);
	row_buf = NULL;
	return 0;
}
Example #2
0
/*!
 * \brief Convert rows from PostgreSQL to db API representation
 * \param _h database connection
 * \param _r result set
 * \return 0 on success, negative on error
 */
int db_postgres_convert_rows(const db1_con_t* _h, db1_res_t* _r)
{
    char **row_buf, *s;
    int row, col, len;

    if (!_h || !_r)  {
        LM_ERR("invalid parameter\n");
        return -1;
    }

    if (!RES_ROW_N(_r)) {
        LM_DBG("no rows returned from the query\n");
        RES_ROWS(_r) = 0;
        return 0;
    }
    /*Allocate an array of pointers per column to holds the string representation */
    len = sizeof(char *) * RES_COL_N(_r);
    row_buf = (char**)pkg_malloc(len);
    if (!row_buf) {
        LM_ERR("no private memory left\n");
        return -1;
    }
    LM_DBG("allocate for %d columns %d bytes in row buffer at %p\n", RES_COL_N(_r), len, row_buf);

    if (db_allocate_rows(_r) < 0) {
        LM_ERR("could not allocate rows\n");
        LM_DBG("freeing row buffer at %p\n", row_buf);
        pkg_free(row_buf);
        return -2;
    }

    for(row = RES_LAST_ROW(_r); row < (RES_LAST_ROW(_r) + RES_ROW_N(_r)); row++) {
        /* reset row buf content */
        memset(row_buf, 0, len);
        for(col = 0; col < RES_COL_N(_r); col++) {
            /*
             * The row data pointer returned by PQgetvalue points to storage
             * that is part of the PGresult structure. One should not modify
             * the data it points to, and one must explicitly copy the data
             * into other storage if it is to be used past the lifetime of
             * the PGresult structure itself.
             */
            s = PQgetvalue(CON_RESULT(_h), row, col);
            LM_DBG("PQgetvalue(%p,%d,%d)=[%s]\n", _h, row, col, s);
            /*
             * A empty string can be a NULL value, or just an empty string.
             * This differs from the mysql behaviour, that further processing
             * steps expect. So we need to simulate this here unfortunally.
             */
            if (PQgetisnull(CON_RESULT(_h), row, col) == 0) {
                row_buf[col] = s;
                LM_DBG("[%d][%d] Column[%.*s]=[%s]\n",
                       row, col, RES_NAMES(_r)[col]->len, RES_NAMES(_r)[col]->s, row_buf[col]);
            }
        }

        /* ASSERT: row_buf contains an entire row in strings */
        if(db_postgres_convert_row(_h, _r, &(RES_ROWS(_r)[row - RES_LAST_ROW(_r)]), row_buf)<0) {
            LM_ERR("failed to convert row #%d\n",  row);
            RES_ROW_N(_r) = row - RES_LAST_ROW(_r);
            LM_DBG("freeing row buffer at %p\n", row_buf);
            pkg_free(row_buf);
            db_free_rows(_r);
            return -4;
        }
    }

    LM_DBG("freeing row buffer at %p\n", row_buf);
    pkg_free(row_buf);
    row_buf = NULL;
    return 0;
}