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;
}
/*
 * SubVar
 *
 * This function substitutes variables designated by the leading separator (sep)
 * with values with matching names.  The string buffer will be realloced in 512-byte
 * chunks if needed.
 */
char *SubVar(char sep, char *var_name, char *var_value, char *oldbuf, int *bufsize) {
    char *buf, *s, *t;
    int l, n;
    char o;
    char *local_buf;
#if MSQL
	char *u;
	int i,j;
    int numi, numj;
    char temp[1024];
    m_result *result=NULL;
    m_row record;
    int new_connection=0;
#endif
 
    l=strlen(oldbuf);
    n=strlen(var_value);
 
    if(l-(int)strlen(var_name)-1+(int)strlen(var_value) >(int)(*bufsize-1)) {

#if DEBUG
        fprintf(fperr,"Query buffer re-allocated\n");
        fflush(fperr);
#endif

        buf=realloc(oldbuf,*bufsize+((n>512)?n:512));
        *bufsize+=((n>512)?n:512);
    } else buf=oldbuf;
 
    /* I don't trust strdup() on many systems */
    local_buf=malloc(*bufsize*sizeof(char));
    strcpy(local_buf,buf);
 
    if(!local_buf) {
        html_error("FI Error","Unable to allocate memory");
        exit(-1);
    }
    s=local_buf;
 
    while(*s!='\0') {
        if(*s==sep) {
            if(s>local_buf && *(s-1)=='\\') {  /* Allow for escaping the separator */
                s++;
                continue;
            }
            n=strcspn(s+1," ,()'\n:;.{}[]-+=~!@#$%^*<>\"");
 
            t=s+n+1;
            o=*t;
            *t='\0';
            if(!strcasecmp(var_name,s+1)) {
                *s='\0';
#if MSQL
                if(!strcasecmp(var_name,"sql")) {
                    /* looking for database name */
 
#if DEBUG
                    fprintf(fperr,"var_name=[%s], var_value=[%s]\n",var_name,var_value);
                    fflush(fperr);
#endif
 
                    u=strchr(var_value,' ');
                    if(u) {
                        *u='\0';
                        if(strlen(current_db)==0) {
                            if(dbsock==-1) {
                                dbsock=msqlConnect(NULL);
                                new_connection=1;
                            }
                            if(msqlSelectDB(dbsock,var_value)<0) {
                                strcpy(temp,msqlErrMsg);
                                current_db[0]='\0';
                            } else {
                                strcpy(current_db,var_value);
                            }
                        }
                        if(msqlQuery(dbsock,u+1)<0) strcpy(temp,msqlErrMsg);
                        else {
                            temp[0]='\0';
                            msqlStoreResult(result);
                            numi=msqlNumRows(result);
                            for(i=0;i<numi;i++) {
                                if(strlen(before_record)) strcat(temp,before_record);
                                msqlDataSeek(result,i);
                                numj=msqlNumFields(result);
                                record=msqlFetchRow(result);
                                for(j=0;j<numj;j++) {
                                    if(strlen(before_element)) strcat(temp,before_element);
                                    strcat(temp,record[j]);
                                    if(strlen(after_element)) strcat(temp,after_element);
                                }
                                if(strlen(after_record)) strcat(temp,after_record);
                            }
                            msqlFreeResult(result);
                            if(new_connection) {
                                msqlClose(dbsock);
                                dbsock=-1;
                                current_db[0]='\0';
                            }
                        }
                    }
                }
#endif
                sprintf(buf,"%s%s%c%s",local_buf,var_value,o,t+1);
                strcpy(local_buf,buf);
            } else {
                *t=o;
            }
            s=t+1;
        } else s++;
    }
    free(local_buf);
    return(buf);
}
Esempio n. 3
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. 4
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. 5
0
    return pos > getRecordCount();
 else
    return true;