#include <fxesql.h>

#ifdef ANSIPROTO
static int getwidth( short sqltype, short sqllen);
static int testfield( char *sqlname, short sqltype);
#else
static int getwidth();
static int testfield();
#endif

fxeqprfld( col)
struct sqlvar_struct *col;
{
    char dbuf[256];
    char tempbuf[256];
    int width;

    width = getrptwidth( col->sqlname, col->sqltype, col->sqllen);
    switch( col->sqltype) {
	case SQLSMFLOAT:
	case SQLFLOAT:
	case SQLDECIMAL:
	case SQLMONEY:
	    fmtfield( col->sqldata, dbuf, col->sqltype, UNDEF, (short) width, 2, F_RIGHT);
	    break;
	case SQLSMINT:
	case SQLINT:
	case SQLSERIAL:
	    fmtfield( col->sqldata, dbuf, col->sqltype, UNDEF, (short) width, 0, F_RIGHT);
	    break;
	default:
	    fmtfield( col->sqldata, dbuf, col->sqltype, UNDEF, (short) width, 0, 0);
	    break;
    }

    if (testfield( col->sqlname, col->sqltype) == YES) {
	/* append .00 to ints and longs for consistency with INFORMIX */
	strcpy( tempbuf, dbuf);
	strcompress( tempbuf);
	if (*tempbuf != '\0') {
	    strcpy( tempbuf, &dbuf[3]);
	    strcat( tempbuf, ".00");
	    strcpy( dbuf, tempbuf);
	}
    }

    rwputs( 1, dbuf);
}

#ifdef ANSIPROTO
getrptwidth( char *sqlname, short sqltype, short sqllen)
#else
getrptwidth( sqlname, sqltype, sqllen)
char *sqlname;
short sqltype;
short sqllen;
#endif
{
    int datalength;
    int headlength;

    if (testfield( sqlname, sqltype) == YES) 
        datalength = getwidth( (short) SQLFLOAT, sqllen);
    else
	datalength = getwidth( sqltype, sqllen);
    headlength = strlen( sqlname); 
    if (datalength > headlength) 
        return( datalength);
    return( headlength);
}

static int
#ifdef ANSIPROTO
getwidth( short sqltype, short sqllen)
#else
getwidth( sqltype, sqllen)
short sqltype;
short sqllen;
#endif
{
    int size;

    switch( sqltype) {
	case SQLSMFLOAT:
	case SQLFLOAT:
	case SQLDECIMAL:
	case SQLMONEY:
	    size = 16;
	    break;
	case SQLSMINT:
	    size = 6;  /* 8 */
	    break;
	case SQLINT:
	case SQLSERIAL:
	    size = 8;
	    break;
	case SQLDATE:
	    size = 10;
	    break;
	default:
	    size = (int) sqllen;
	    break;
    }

    return( size);
}

static int
#ifdef ANSIPROTO
testfield( char *sqlname, short sqltype)
#else
testfield(sqlname, sqltype)
char *sqlname;
short sqltype;
#endif
{
#   ifndef INFORMIX 
    if ((sqltype == SQLSMINT || sqltype == SQLINT)
         && (strcmp(sqlname, "(expression)") == 0 || 
             strcmp(sqlname, "(avg)") == 0  || 
             strcmp(sqlname, "(sum)") == 0  || 
             strncmp(sqlname, "avg", 3) == 0  || 
             strncmp(sqlname, "sum", 3) == 0 )) {
             return(YES);
             }
#    endif
    return(NO);
}

prsqlca()
{
     char buf[40];

     rwputs( 1, "SQLCA structure:\n");
     sprintf( buf, "    sqlcode    = %ld\n", sqlca.sqlcode);
     rwputs( 1, buf);
     sprintf( buf, "    sqlerrd[1] = %ld\n", sqlca.sqlerrd[1]);
     rwputs( 1, buf);
     sprintf( buf, "    sqlerrd[2] = %ld\n", sqlca.sqlerrd[2]);
     rwputs( 1, buf);
     sprintf( buf, "    sqlerrd[4] = %ld\n", sqlca.sqlerrd[4]);
     rwputs( 1, buf);
     sprintf( buf, "    sqlerrd[5] = %ld\n", sqlca.sqlerrd[5]);
     rwputs( 1, buf);
     sprintf( buf, "    sqlwarn0   = %c\n", sqlca.sqlwarn.sqlwarn0);
     rwputs( 1, buf);
     sprintf( buf, "    sqlwarn1   = %c\n", sqlca.sqlwarn.sqlwarn1);
     rwputs( 1, buf);
     sprintf( buf, "    sqlwarn2   = %c\n", sqlca.sqlwarn.sqlwarn2);
     rwputs( 1, buf);
     sprintf( buf, "    sqlwarn3   = %c\n", sqlca.sqlwarn.sqlwarn3);
     rwputs( 1, buf);
     sprintf( buf, "    sqlwarn4   = %c\n", sqlca.sqlwarn.sqlwarn4);
     rwputs( 1, buf);
}
