Esempio n. 1
0
File: db.c Progetto: BPotato/fluffos
static array_t *msql_fetch (dbconn_t * c, int row)
{
	int i, num_fields;
	m_row this_row;
	array_t *v;

	if (!c->msql.result_set) {
		return &the_null_array;
	}
	if (row < 1 || row > msqlNumRows(c->msql.result_set)) {
		return &the_null_array;
	}

	num_fields = msqlNumFields(c->msql.result_set);
	if (num_fields < 1) {
		return &the_null_array;
	}

	msqlDataSeek(c->msql.result_set, row - 1);
	this_row = msqlFetchRow(c->msql.result_set);
	if (!this_row) {
		return &the_null_array;
	}

	v = allocate_empty_array(num_fields);
	for (i = 0;  i < num_fields;  i++) {
		m_field *field;

		field = msqlFetchField(c->msql.result_set);
		if (!field || !this_row[i]) {
			v->item[i] = const0u;
		} else {
			switch (field->type) {
			case INT_TYPE:
			case UINT_TYPE:
				v->item[i].type = T_NUMBER;
				v->item[i].u.number = atoi(this_row[i]);
				break;

			case REAL_TYPE:
			case MONEY_TYPE:
				v->item[i].type = T_REAL;
				v->item[i].u.real = atof(this_row[i]);
				break;

			case CHAR_TYPE:
			case TEXT_TYPE:
			case DATE_TYPE:
			case TIME_TYPE:
				v->item[i].type = T_STRING;
				v->item[i].subtype = STRING_MALLOC;
				v->item[i].u.string = string_copy(this_row[i], "msql_fetch");
				break;

			default:
				v->item[i] = const0u;
				break;
			}
		}
	}

	msqlFieldSeek(c->msql.result_set, 0);
	return v;
}
Esempio n. 2
0
SQLRETURN SQLColumns( 	SQLHSTMT    hDrvStmt,
						SQLCHAR     *szCatalogName,
						SQLSMALLINT nCatalogNameLength,
						SQLCHAR     *szSchemaName,
						SQLSMALLINT nSchemaNameLength,
						SQLCHAR     *szTableName,
						SQLSMALLINT nTableNameLength,
						SQLCHAR     *szColumnName,
						SQLSMALLINT nColumnNameLength )
{
    HDRVSTMT	hStmt		= (HDRVSTMT)hDrvStmt;
    m_result   	*pResults;						/* mSQL DATA	*/
	m_field		*pField;						/* mSQL field	*/
	COLUMNHDR	*pColumnHeader;			
	int			nColumn;
	long		nCols;
	long		nRow;
	long		nCurRow;
	char		szBuffer[101];
	int			nIndexColumns = 0;

    /* SANITY CHECKS */
    if( NULL == hStmt )
        return SQL_INVALID_HANDLE;

	sprintf( hStmt->szSqlMsg, "hStmt = $%08lX", hStmt );
    logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_WARNING, LOG_WARNING, hStmt->szSqlMsg );

	if ( szTableName == NULL || szTableName[0] == '\0' )
	{
		logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_WARNING, LOG_WARNING, "SQL_ERROR Must supply a valid table name" );
		return SQL_ERROR;
	}

    /**************************
	 * close any existing result
     **************************/
	if ( hStmt->hStmtExtras->aResults )
		_FreeResults( hStmt->hStmtExtras );

	if ( hStmt->pszQuery != NULL )
		free( hStmt->pszQuery );

	hStmt->pszQuery							= NULL;
	
    /************************
     * generate a result set listing columns (these will be our rows)
     ************************/
	pResults = msqlListFields( ((HDRVDBC)hStmt->hDbc)->hDbcExtras->hServer, szTableName );
    if ( pResults == NULL )
	{
        sprintf( hStmt->szSqlMsg, "SQL_ERROR Query failed. %s", msqlErrMsg );
		logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_WARNING, LOG_WARNING, hStmt->szSqlMsg );
        return SQL_ERROR;
	}
    /************************
     * how many columns are indexs?
     ************************/
    nIndexColumns = 0;
	while ( pField = msqlFetchField( pResults ) )
	{
		if ( pField->type == IDX_TYPE)
            nIndexColumns++;
		
	}
	msqlFieldSeek( pResults, 0 );

    /**************************
	 * allocate memory for columns headers and result data (row 0 is column header while col 0 is reserved for bookmarks)
     **************************/
	hStmt->hStmtExtras->nCols 		= COL_MAX-1;
	hStmt->hStmtExtras->nRows 		= msqlNumFields( pResults ) - nIndexColumns;
    hStmt->hStmtExtras->nRow		= 0;
	hStmt->hStmtExtras->aResults 	=  malloc( sizeof(char*) * (hStmt->hStmtExtras->nRows+1) * (hStmt->hStmtExtras->nCols+1) );
	memset( hStmt->hStmtExtras->aResults, 0, sizeof(char*) * (hStmt->hStmtExtras->nRows+1) * (hStmt->hStmtExtras->nCols+1) );
	if ( hStmt->hStmtExtras->aResults == NULL )
	{
		logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_WARNING, LOG_WARNING, "Not enough memory. (malloc failed)" );
        hStmt->hStmtExtras->nRows = 0;
        hStmt->hStmtExtras->nCols = 0;	
		msqlFreeResult( pResults );
        return SQL_ERROR;
	}

    /**************************
	 * gather column header information (save col 0 for bookmarks)
     **************************/
	for ( nColumn = 1; nColumn < COL_MAX; nColumn++ )
	{
		(hStmt->hStmtExtras->aResults)[nColumn] = malloc( sizeof(COLUMNHDR) );
		pColumnHeader = (COLUMNHDR*)(hStmt->hStmtExtras->aResults)[nColumn];
		memset( pColumnHeader, 0, sizeof(COLUMNHDR) );
		_NativeToSQLColumnHeader( pColumnHeader, &(aSQLColumns[nColumn]) );
	}

	/************************
	 * gather data (save col 0 for bookmarks and factor out index columns)
	 ************************/
	nCols 	= hStmt->hStmtExtras->nCols;
	hStmt->hStmtExtras->nRow	= 0;
    for ( nCurRow = 1; nCurRow <= msqlNumFields( pResults ); nCurRow++ )
	{
		pField = msqlFetchField( pResults );
        if ( pField->type != IDX_TYPE)
        {
			hStmt->hStmtExtras->nRow++;
			nRow	= hStmt->hStmtExtras->nRow;
			for ( nColumn = 1; nColumn < COL_MAX; nColumn++ )
			{
				switch ( nColumn )
				{
				case TABLE_NAME:
					(hStmt->hStmtExtras->aResults)[nRow*nCols+nColumn] = (char*)strdup( szTableName );
					break;
				case COLUMN_NAME:
					(hStmt->hStmtExtras->aResults)[nRow*nCols+nColumn] = (char*)strdup( pField->name );
					break;
				case TYPE_NAME:
					(hStmt->hStmtExtras->aResults)[nRow*nCols+nColumn] = (char*)strdup( msqlTypeNames[pField->type] );
					break;
				case COLUMN_SIZE:
					sprintf( szBuffer, "%d", pField->length );
					(hStmt->hStmtExtras->aResults)[nRow*nCols+nColumn] = (char*)strdup( szBuffer );
					break;
				case NULLABLE:
					(hStmt->hStmtExtras->aResults)[nRow*nCols+nColumn] = (char *)strdup( IS_NOT_NULL( pField->flags ) ? "0" : "1" );
					break;
				default:
					(hStmt->hStmtExtras->aResults)[nRow*nCols+nColumn] = NULL;
				} /* switch nColumn */
			} /* for nColumn */
		} /* if */
	} /* for nRow */
	hStmt->hStmtExtras->nRow  = 0;

    /**************************
	 * free the snapshot
     **************************/
	msqlFreeResult( pResults );

    logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_INFO, LOG_INFO, "SQL_SUCCESS" );
    return SQL_SUCCESS;
}
Esempio n. 3
0
SQLRETURN _Execute( SQLHSTMT  hDrvStmt )
{
    HDRVSTMT hStmt	= (HDRVSTMT)hDrvStmt;
	int				nColumn;
	int				nCols;
	int				nRow;
    m_result    	*pResults;						/* mSQL DATA	*/
    m_row       	rowResult;						/* mSQL ROW		*/
    m_field     	*pField;						/* mSQL COL HDR	*/
	COLUMNHDR		*pColumnHeader;			

	/* SANITY CHECKS */
    if( NULL == hStmt )
        return SQL_INVALID_HANDLE;

	sprintf( hStmt->szSqlMsg, "hStmt = $%08lX", hStmt );
    logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_WARNING, LOG_WARNING, hStmt->szSqlMsg );

    if( hStmt->pszQuery == NULL )
    {
		logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_WARNING, LOG_WARNING, "SQL_ERROR No prepared statement" );
        return SQL_ERROR;
    }

    /**************************
	 * Free any current results
     **************************/
	if ( hStmt->hStmtExtras->aResults )
		_FreeResults( hStmt->hStmtExtras );

    /**************************
	 * send prepared query to server
     **************************/
    if ( (hStmt->hStmtExtras->nRows = msqlQuery( ((HDRVDBC)hStmt->hDbc)->hDbcExtras->hServer, hStmt->pszQuery )) == -1 )
    {
        sprintf( hStmt->szSqlMsg, "SQL_ERROR Query failed. %s", msqlErrMsg );
		logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_WARNING, LOG_WARNING, hStmt->szSqlMsg );
        return SQL_ERROR;
    }

    /**************************
	 * snapshot our results (assume no results means UPDATE, DELETE or INSERT
     **************************/
    pResults = msqlStoreResult();
    if ( !pResults )
        return SQL_SUCCESS;

    /**************************
	 * allocate memory for columns headers and result data (row 0 is column header while col 0 is reserved for bookmarks)
     **************************/
    hStmt->hStmtExtras->nRows		= msqlNumRows( pResults );
    hStmt->hStmtExtras->nCols 		= msqlNumFields( pResults );
	hStmt->hStmtExtras->aResults 	=  malloc( sizeof(char*) * (hStmt->hStmtExtras->nRows+1) * (hStmt->hStmtExtras->nCols+1) );
	if ( hStmt->hStmtExtras->aResults == NULL )
	{
		logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_WARNING, LOG_WARNING, "Not enough memory. (malloc failed)" );
        hStmt->hStmtExtras->nRows = 0;
        hStmt->hStmtExtras->nCols = 0;	
		msqlFreeResult( pResults );
        return SQL_ERROR;
	}
	memset( hStmt->hStmtExtras->aResults, 0, sizeof(char*) * (hStmt->hStmtExtras->nRows+1) * (hStmt->hStmtExtras->nCols+1) );

    /**************************
	 * gather column header information (save col 0 for bookmarks)
     **************************/
	for ( nColumn = 1; nColumn <= hStmt->hStmtExtras->nCols; nColumn++ )
	{
		pField = msqlFetchField( pResults );
        (hStmt->hStmtExtras->aResults)[nColumn] = malloc( sizeof(COLUMNHDR) );
		memset( (hStmt->hStmtExtras->aResults)[nColumn], 0, sizeof(COLUMNHDR) );
        pColumnHeader = (COLUMNHDR*)(hStmt->hStmtExtras->aResults)[nColumn];
		_NativeToSQLColumnHeader( pColumnHeader, pField  );
	}

	/************************
	 * gather data (save col 0 for bookmarks)
	 ************************/
	nCols 	= hStmt->hStmtExtras->nCols;
	nRow	= 0;
    while ( (rowResult = msqlFetchRow( pResults )) != NULL )
	{
        nRow++;
		msqlFieldSeek( pResults, 0 );
		for ( nColumn=1; nColumn <= nCols; nColumn++ )
		{
			if ( rowResult[nColumn-1] )
				(hStmt->hStmtExtras->aResults)[nRow*nCols+nColumn] = (char *)strdup( rowResult[nColumn-1] );
		}
	}
	hStmt->hStmtExtras->nRow = 0;

    /**************************
	 * free the snapshot
     **************************/
	msqlFreeResult( pResults );

    logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_INFO, LOG_INFO, "SQL_SUCCESS" );
    return SQL_SUCCESS;
}
Esempio n. 4
0
unsigned int mSQLDBC::getFieldCount(){
   return msqlNumFields(result);
};