Hbpgsql postgres.c suggestions.

96 views
Skip to first unread message

Fausto Trautwein

unread,
Jun 20, 2016, 11:15:19 AM6/20/16
to Harbour Users
Hi all.

I'd like to suggest some modifications in contrib\hbpgsql\postgres.c

In HB_FUNC( PQEXECPARAMS ), line 422, there is:

hb_PGresult_ret( PQexecParams( conn, hb_parcx( 2 ), n, NULL, paramvalues, NULL, NULL, 1 ) );

the last parameter is resultformat and in documentation says "Specify zero to obtain results in text format, or one to obtain results in binary format"
If 0 is used in place of 1, we can obtain the results from, for example, an UPDATE .. RETURNING xxx. 
With the actual 1 it is not returning the values.

Another change is a new function that returns a formatted array from a SELECT statement. 
There is already HB_FUNC( PQRESULT2ARRAY ) but it returns only strings. 
The proposed function returns the appropriate data type.
It is working the way I´ve done but my acknowledge of c is almost null and the function needs improvement. 

HB_FUNC( PQRESULT2ARRAYFORM )
{
   PGresult * res = hb_PGresult_par( 1 );
   if( res )
   {
      if( PQresultStatus( res ) == PGRES_TUPLES_OK )
      {
         int nRows = PQntuples(res), nRow;
         int nCols = PQnfields( res ), nCol;
         PHB_ITEM pResult = hb_itemArrayNew( nRows ), pRow;

         for( nRow = 0; nRow < nRows ; nRow++ )
         {
            PHB_ITEM pRow = hb_arrayGetItemPtr( pResult, nRow + 1 );
            hb_arrayNew ( pRow, nCols );
            for( nCol = 0; nCol < nCols ; nCol++ )
            {
               Oid   type_oid = PQftype( res, nCol );
               int  tipo;
               char buf[1];
               char * pgvalue;
               switch( type_oid )
               {
                  case BITOID:
                     /*strcpy( buf, "N" );*/
                     tipo = 2;
                     break;

                  case BOOLOID:
                     tipo = 4;
                     break;

                  case BPCHAROID:
                     tipo = 1;
                     break;

                  case FLOAT4OID:
                     tipo = 2;
                     break;

                  case FLOAT8OID:
                     tipo = 2;
                     break;

                  case INT2OID:
                     tipo = 2;
                     break;

                  case INT4OID:
                     tipo = 2;
                     break;

                  case OIDOID:
                     tipo = 2;
                     break;

                  case INT8OID:
                     tipo = 2;
                     break;

                  case NUMERICOID:
                     tipo = 2;
                     break;

                  case DATEOID:
                     tipo = 3;
                     break;

                  case TIMEOID:
                  case TIMETZOID:
                     tipo = 3;
                     break;

                  case TIMESTAMPOID:
                  case TIMESTAMPTZOID:
                     tipo = 3;
                     break;

                  case VARBITOID:
                     tipo = 2;
                     break;

                  case VARCHAROID:
                     tipo = 1;
                     break;

                  case TEXTOID:
                     tipo = 1;
                     break;

                  case CASHOID:
                     tipo = 2;
                     break;

                  default:
                     tipo = 1;
                    break;
               }
               //tipo = 1;
               pgvalue = PQgetvalue(res, nRow, nCol);
               if ( tipo == 1 )
                  {
                     hb_arraySetC( pRow, nCol + 1, pgvalue );
                  }

               if ( tipo == 2 )
                  {
                     int      iWidth, iDec;
                     HB_LONG  lValue;
                     double   dValue;
                     if( ! hb_valStrnToNum( pgvalue, ( int ) strlen(pgvalue), &lValue, &dValue, &iDec, &iWidth ) )
                        hb_arraySetNI( pRow, nCol + 1, lValue);
                     else
                        hb_arraySetND( pRow, nCol + 1, dValue);
                  }
               if ( tipo == 3 )
                  {
                     char  szDate[ 9 ];
                     szDate[ 0 ] = pgvalue[ 0 ];
                     szDate[ 1 ] = pgvalue[ 1 ];
                     szDate[ 2 ] = pgvalue[ 2 ];
                     szDate[ 3 ] = pgvalue[ 3 ];
                     szDate[ 4 ] = pgvalue[ 5 ];
                     szDate[ 5 ] = pgvalue[ 6 ];
                     szDate[ 6 ] = pgvalue[ 8 ];
                     szDate[ 7 ] = pgvalue[ 9 ];
                     szDate[ 8 ] = '\0';
                     hb_arraySetDS( pRow, nCol + 1, szDate );
                  }
               if ( tipo == 4 )
                  {
                     hb_arraySetL( pRow, nCol + 1, pgvalue[ 0 ] == 't' || pgvalue[ 0 ] == 'y' );
                  }

            }
         }
         hb_itemReturnRelease( pResult );
         //hb_itemRelease( hb_itemReturnForward( pResult ) );
      }
   }
}


Thanks,

Fausto Di Creddo Trautwein
Reply all
Reply to author
Forward
0 new messages