hbpgsql

  Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic! Mail us feedback on this topic!  
c:\harbour\contrib\hbpgsql
postgres.c
TypeFunctionSourceLine
STATIC HB_GARBAGE_FUNC(PGconn_release )
static HB_GARBAGE_FUNC( PGconn_release )
{
   void ** ph = ( void ** ) Cargo;

   /* Check if pointer is not NULL to avoid multiple freeing */
   if( ph && * ph )
   {
      /* Destroy the object */
      PQfinish( ( PGconn * ) * ph );

      /* set pointer to NULL to avoid multiple freeing */
      * ph = NULL;
   }
}
postgres.c96
STATIC VOIDPGconn_ret( PGconn * p )
static void PGconn_ret( PGconn * p )
{
   void ** ph = ( void ** ) hb_gcAlloc( sizeof( PGconn * ), PGconn_release );

   * ph = p;

   if( * ph )
      hb_retptrGC( ph );
}
postgres.c111
STATIC PGCONN *PGconn_par( int iParam )
static PGconn * PGconn_par( int iParam )
{
   void ** ph = ( void ** ) hb_parptrGC( PGconn_release, iParam );

   return ph ? ( PGconn * ) * ph : NULL;
}
postgres.c121
HB_FUNCPQCONNECT(void)
HB_FUNC( PQCONNECT )
{
   if( hb_pcount() == 5 )
   {
      char conninfo[ 512 ];

      snprintf( conninfo, sizeof( conninfo ), "dbname = %s host = %s user = %s password = %s port = %i",
         hb_parcx( 1 ), hb_parcx( 2 ), hb_parcx( 3 ), hb_parcx( 4 ), ( int ) hb_parni( 5 ) );

      PGconn_ret( PQconnectdb( conninfo ) );
   }
   else
      hb_retptr( NULL );
}
postgres.c128
HB_FUNCPQSETDBLOGIN(void)
HB_FUNC( PQSETDBLOGIN )
{
   if( hb_pcount() == 7 )
   {
      const char * pghost    = hb_parcx( 1 );
      const char * pgport    = hb_parcx( 2 );
      const char * pgoptions = hb_parcx( 3 );
      const char * pgtty     = hb_parcx( 4 );
      const char * dbName    = hb_parcx( 5 );
      const char * login     = hb_parcx( 6 );
      const char * pwd       = hb_parcx( 7 );

      PGconn_ret( PQsetdbLogin( pghost, pgport, pgoptions, pgtty, dbName, login, pwd ) );
   }
   else
      hb_retptr( NULL );
}
postgres.c143
HB_FUNCPQCLOSE(void)
HB_FUNC( PQCLOSE )
{
   void ** ph = ( void ** ) hb_parptrGC( PGconn_release, 1 );

   /* Check if pointer is not NULL to avoid multiple freeing */
   if( ph && * ph )
   {
      /* Destroy the object */
      PQfinish( ( PGconn * ) * ph );

      /* set pointer to NULL to avoid multiple freeing */
      * ph = NULL;
   }
}
postgres.c161
HB_FUNCPQRESET(void)
HB_FUNC( PQRESET )
{
   if( hb_parinfo( 1 ) )
      PQreset( PGconn_par( 1 ) );
}
postgres.c176
HB_FUNCPQPROTOCOLVERSION(void)
HB_FUNC( PQPROTOCOLVERSION )
{
   if( hb_parinfo( 1 ) )
      hb_retni( PQprotocolVersion( PGconn_par( 1 ) ) );
}
postgres.c182
HB_FUNCPQCLIENTENCODING(void)
HB_FUNC( PQCLIENTENCODING )
{
   if( hb_parinfo( 1 ) )
      hb_retni( PQclientEncoding( PGconn_par( 1 ) ) );
}
postgres.c188
HB_FUNCPQSETCLIENTENCODING(void)
HB_FUNC( PQSETCLIENTENCODING )
{
   if( hb_pcount() == 2 )
      hb_retni( PQsetClientEncoding( PGconn_par( 1 ), hb_parcx( 2 ) ) );
}
postgres.c194
HB_FUNCPQDB(void)
HB_FUNC( PQDB )
{
   if( hb_parinfo( 1 ) )
      hb_retc( PQdb( PGconn_par( 1 ) ) );
}
postgres.c200
HB_FUNCPQUSER(void)
HB_FUNC( PQUSER )
{
   if( hb_parinfo( 1 ) )
      hb_retc( PQuser( PGconn_par( 1 ) ) );
}
postgres.c206
HB_FUNCPQPASS(void)
HB_FUNC( PQPASS )
{
   if( hb_parinfo( 1 ) )
      hb_retc( PQpass( PGconn_par( 1 ) ) );
}
postgres.c212
HB_FUNCPQHOST(void)
HB_FUNC( PQHOST )
{
   if( hb_parinfo( 1 ) )
      hb_retc( PQhost( PGconn_par( 1 ) ) );
}
postgres.c218
HB_FUNCPQPORT(void)
HB_FUNC( PQPORT )
{
   if( hb_parinfo( 1 ) )
      hb_retc( PQport( PGconn_par( 1 ) ) );
}
postgres.c224
HB_FUNCPQTTY(void)
HB_FUNC( PQTTY )
{
   if( hb_parinfo( 1 ) )
      hb_retc( PQtty( PGconn_par( 1 ) ) );
}
postgres.c230
HB_FUNCPQOPTIONS(void)
HB_FUNC( PQOPTIONS )
{
   if( hb_parinfo( 1 ) )
      hb_retc( PQoptions( PGconn_par( 1 ) ) );
}
postgres.c236
HB_FUNCPQCLEAR(void)
HB_FUNC( PQCLEAR )
{
   if( hb_parinfo( 1 ) )
      PQclear( ( PGresult * ) hb_parptr( 1 ) );
}
postgres.c246
HB_FUNCPQEXEC(void)
HB_FUNC( PQEXEC )
{
   PGresult * res = NULL;

   if( hb_pcount() == 2 )
      res = PQexec( PGconn_par( 1 ), hb_parcx( 2 ) );

   hb_retptr( res );
}
postgres.c252
HB_FUNCPQEXECPARAMS(void)
HB_FUNC( PQEXECPARAMS )
{
   PGresult * res = NULL;

   if( hb_pcount() == 3 )
   {
      PHB_ITEM aParam = hb_param( 3, HB_IT_ARRAY );
      long n = hb_arrayLen( aParam );
      long i;

      char ** paramvalues = ( char ** ) hb_xgrab( sizeof( char * ) * n );

      for( i = 0; i < n; i++ )
         paramvalues[ i ] = hb_arrayGetCPtr( aParam, i + 1 );

      res = PQexecParams( PGconn_par( 1 ), hb_parcx( 2 ), n, NULL, ( const char * const * ) paramvalues, NULL, NULL, 1 );

      hb_xfree( paramvalues );
   }

   hb_retptr( res );
}
postgres.c262
HB_FUNCPQFCOUNT(void)
HB_FUNC( PQFCOUNT )
{
   int nFields = 0;

   if( hb_parinfo( 1 ) )
   {
      PGresult * res = ( PGresult * ) hb_parptr( 1 );

      if( PQresultStatus( res ) == PGRES_TUPLES_OK )
         nFields = PQnfields( res );
   }

   hb_retni( nFields );
}
postgres.c285
HB_FUNCPQLASTREC(void)
HB_FUNC( PQLASTREC )
{
   int nRows = 0;

   if( hb_parinfo( 1 ) )
   {
      PGresult * res = ( PGresult * ) hb_parptr( 1 );

      if( PQresultStatus( res ) == PGRES_TUPLES_OK )
         nRows = PQntuples( res );
   }

   hb_retni( nRows );
}
postgres.c300
HB_FUNCPQGETVALUE(void)
HB_FUNC( PQGETVALUE )
{
   if( hb_pcount() == 3 )
   {
      PGresult * res = ( PGresult * ) hb_parptr( 1 );

      if( PQresultStatus( res ) == PGRES_TUPLES_OK )
      {
         int nRow = hb_parni( 2 ) - 1;
         int nCol = hb_parni( 3 ) - 1;

         if( ! PQgetisnull( res, nRow, nCol ) )
            hb_retc( PQgetvalue( res, nRow, nCol ) );
      }
   }
}
postgres.c315
HB_FUNCPQGETLENGTH(void)
HB_FUNC( PQGETLENGTH )
{
   int result = 0;

   if( hb_pcount() == 3 )
   {
      PGresult * res = ( PGresult * ) hb_parptr( 1 );

      if( PQresultStatus( res ) == PGRES_TUPLES_OK )
      {
         int nRow = hb_parni( 2 ) - 1;
         int nCol = hb_parni( 3 ) - 1;

         result = PQgetlength( res, nRow, nCol );
      }
   }

   hb_retni( result );
}
postgres.c332
HB_FUNCPQMETADATA(void)
HB_FUNC( PQMETADATA )
{
   if( hb_parinfo( 1 ) )
   {
      PGresult * res = ( PGresult * ) hb_parptr( 1 );

      if( PQresultStatus( res ) == PGRES_TUPLES_OK )
      {
         int nFields = PQnfields( res ), i;
         PHB_ITEM pResult = hb_itemArrayNew( nFields );

         for( i = 0; i < nFields; i++ )
         {
            char  buf[ 256 ];
            Oid   type_oid = PQftype( res, i );
            int   typemod = PQfmod( res, i );
            int   length = 0;
            int   decimal = 0;

            PHB_ITEM pField;

            switch( type_oid )
            {
               case BITOID:
                  if( typemod >= 0 )
                     length = ( int ) typemod;
                  hb_strncpy( buf, "bit", sizeof( buf ) - 1 );
                  break;

               case BOOLOID:
                  length = 1;
                  hb_strncpy( buf, "boolean", sizeof( buf ) - 1 );
                  break;

               case BPCHAROID:
                  if( typemod >= 0 )
                     length = ( int ) ( typemod - VARHDRSZ );
                  hb_strncpy( buf, "character", sizeof( buf ) - 1 );
                  break;

               case FLOAT4OID:
                  hb_strncpy( buf, "real", sizeof( buf ) - 1 );
                  break;

               case FLOAT8OID:
                  hb_strncpy( buf, "double precision", sizeof( buf ) - 1 );
                  break;

               case INT2OID:
                  hb_strncpy( buf, "smallint", sizeof( buf ) - 1 );
                  break;

               case INT4OID:
                  hb_strncpy( buf, "integer", sizeof( buf ) - 1 );
                  break;

               case OIDOID:
                  hb_strncpy( buf, "bigint", sizeof( buf ) - 1 );
                  break;

               case INT8OID:
                  hb_strncpy( buf, "bigint", sizeof( buf ) - 1 );
                  break;

               case NUMERICOID:
                  length = ( ( typemod - VARHDRSZ ) >> 16 ) & 0xffff;
                  decimal = ( typemod - VARHDRSZ ) & 0xffff;
                  hb_strncpy( buf, "numeric", sizeof( buf ) - 1 );
                  break;

               case DATEOID:
                  hb_strncpy( buf, "date", sizeof( buf ) - 1 );
                  break;

               case TIMEOID:
               case TIMETZOID:
                  hb_strncpy( buf, "timezone", sizeof( buf ) - 1 );
                  break;

               case TIMESTAMPOID:
               case TIMESTAMPTZOID:
                  hb_strncpy( buf, "timestamp", sizeof( buf ) - 1 );
                  break;

               case VARBITOID:
                  if( typemod >= 0 )
                     length = (int) typemod;
                  hb_strncpy( buf, "bit varying", sizeof( buf ) - 1 );
                  break;

               case VARCHAROID:
                  if( typemod >= 0 )
                     length = ( int ) ( typemod - VARHDRSZ );
                  hb_strncpy( buf, "character varying", sizeof( buf ) - 1 );
                  break;

               case TEXTOID:
                  hb_strncpy( buf, "text", sizeof( buf ) - 1 );
                  break;

               case CASHOID:
                  hb_strncpy( buf, "money", sizeof( buf ) - 1 );
                  break;

               default:
                  hb_strncpy( buf, "not supported", sizeof( buf ) - 1 );
                  break;
            }

            pField = hb_arrayGetItemPtr( pResult, i + 1 );
            hb_arrayNew( pField, 6 );
            hb_arraySetC(  pField, 1, PQfname( res, i ) );
            hb_arraySetC(  pField, 2, buf );
            hb_arraySetNI( pField, 3, length );
            hb_arraySetNI( pField, 4, decimal );
            hb_arraySetNL( pField, 5, PQftable( res, i ) );
            hb_arraySetNI( pField, 6, PQftablecol( res, i ) );
         }

         hb_itemRelease( hb_itemReturnForward( pResult ) );
         return;
      }
   }

   hb_reta( 0 );
}
postgres.c352
HB_FUNCPQRESULT2ARRAY(void)
HB_FUNC( PQRESULT2ARRAY )
{
   if( hb_parinfo( 1 ) )
   {
      PGresult * res = ( PGresult * ) hb_parptr( 1 );

      if( PQresultStatus( res ) == PGRES_TUPLES_OK )
      {
         int nRows = PQntuples( res ), nRow;
         int nCols = PQnfields( res ), nCol;

         PHB_ITEM pResult = hb_itemArrayNew( nRows );

         for( nRow = 0; nRow < nRows ; nRow++ )
         {
            PHB_ITEM pRow = hb_arrayGetItemPtr( pResult, nRow + 1 );
            hb_arrayNew( pRow, nCols );
            for( nCol = 0; nCol < nCols; nCol++ )
               hb_arraySetC( pRow, nCol + 1, PQgetvalue( res, nRow, nCol ) );
         }

         hb_itemRelease( hb_itemReturnForward( pResult ) );
         return;
      }
   }

   hb_reta( 0 );
}
postgres.c479
HB_FUNCPQTRANSACTIONSTATUS(void)
HB_FUNC( PQTRANSACTIONSTATUS )
{
   if( hb_parinfo( 1 ) )
      hb_retni( PQtransactionStatus( PGconn_par( 1 ) ) );
}
postgres.c508
HB_FUNCPQERRORMESSAGE(void)
HB_FUNC( PQERRORMESSAGE )
{
   if( hb_parinfo( 1 ) )
      hb_retc( PQerrorMessage( PGconn_par( 1 ) ) );
}
postgres.c514
HB_FUNCPQSTATUS(void)
HB_FUNC( PQSTATUS )
{
   if( hb_parinfo( 1 ) )
      hb_retni( PQstatus( PGconn_par( 1 ) ) );
}
postgres.c520
HB_FUNCPQRESULTERRORMESSAGE(void)
HB_FUNC( PQRESULTERRORMESSAGE )
{
   if( hb_parinfo( 1 ) )
       hb_retc( PQresultErrorMessage( ( PGresult * ) hb_parptr( 1 ) ) );
}
postgres.c526
HB_FUNCPQRESULTSTATUS(void)
HB_FUNC( PQRESULTSTATUS )
{
   if( hb_parinfo( 1 ) )
      hb_retni( PQresultStatus( ( PGresult * ) hb_parptr( 1 ) ) );
}
postgres.c532
HB_FUNCPQCMDSTATUS(void)
HB_FUNC( PQCMDSTATUS )
{
   if( hb_parinfo( 1 ) )
      hb_retc( PQcmdStatus( ( PGresult * ) hb_parptr( 1 ) ) );
}
postgres.c539
HB_FUNCPQCMDTUPLES(void)
HB_FUNC( PQCMDTUPLES )
{
   if( hb_parinfo( 1 ) )
      hb_retc( PQcmdTuples( ( PGresult * ) hb_parptr( 1 ) ) );
}
postgres.c546
HB_FUNCPQESCAPESTRING(void)
HB_FUNC( PQESCAPESTRING )
{
   char * source = hb_parcx( 1 );
   size_t size = strlen( source );
   char * dest = ( char * ) hb_xgrab( size * 2 + 1 );

   PQescapeString( dest, source, size );

   hb_retc_buffer( dest );
}
postgres.c553
HB_FUNCPQESCAPEBYTEA(void)
HB_FUNC( PQESCAPEBYTEA ) /* deprecated */
{
   unsigned const char * from = ( BYTE * ) hb_parc( 1 );
   size_t from_length = hb_parclen( 1 );
   size_t to_length = from_length * 5 + 1;

   unsigned char * to = PQescapeBytea( from, from_length, &to_length );
   hb_retc( ( char * ) to ); /* TOFIX: ? hb_retc( ( char * ) to, to_length ); */
   PQfreemem( to );
}
postgres.c565
HB_FUNCPQUNESCAPEBYTEA(void)
HB_FUNC( PQUNESCAPEBYTEA )
{
   size_t to_length;
   unsigned char * from = PQunescapeBytea( ( BYTE * ) hb_parcx( 1 ), &to_length );
   hb_retclen( ( char * ) from, to_length );
   PQfreemem( from );
}
postgres.c577
HB_FUNCPQOIDVALUE(void)
HB_FUNC( PQOIDVALUE )
{
   if( hb_parinfo( 1 ) )
      hb_retnl( ( Oid ) PQoidValue( ( PGresult * ) hb_parptr( 1 ) ) );
}
postgres.c586
HB_FUNCPQOIDSTATUS(void)
HB_FUNC( PQOIDSTATUS )
{
   if( hb_parinfo( 1 ) )
      hb_retc( PQoidStatus( ( PGresult * ) hb_parptr( 1 ) ) );
}
postgres.c592
HB_FUNCPQBINARYTUPLES(void)
HB_FUNC( PQBINARYTUPLES )
{
   if( hb_parinfo( 1 ) )
      hb_retl( PQbinaryTuples( ( PGresult * ) hb_parptr( 1 ) ) );
}
postgres.c598
HB_FUNCPQFTABLE(void)
HB_FUNC( PQFTABLE )
{
   if( hb_pcount() == 2 )
      hb_retnl( ( Oid ) PQftable( ( PGresult * ) hb_parptr( 1 ), hb_parni( 2 ) - 1 ) );
}
postgres.c604
HB_FUNCPQFTYPE(void)
HB_FUNC( PQFTYPE )
{
   if( hb_pcount() == 2 )
      hb_retnl( ( Oid ) PQftype( ( PGresult * ) hb_parptr( 1 ), hb_parni( 2 ) - 1 ) );
}
postgres.c610
HB_FUNCPQFNAME(void)
HB_FUNC( PQFNAME )
{
   if( hb_pcount() == 2 )
      hb_retc( PQfname( ( PGresult * ) hb_parptr( 1 ), hb_parni( 2 ) - 1 ));
}
postgres.c616
HB_FUNCPQFMOD(void)
HB_FUNC( PQFMOD )
{
   if( hb_pcount() == 2 )
      hb_retni( PQfmod( ( PGresult * ) hb_parptr( 1 ), hb_parni( 2 ) - 1 ) );
}
postgres.c622
HB_FUNCPQFSIZE(void)
HB_FUNC( PQFSIZE )
{
   if( hb_pcount() == 2 )
      hb_retni( PQfsize( ( PGresult * ) hb_parptr( 1 ), hb_parni( 2 ) - 1 ) );
}
postgres.c628
HB_FUNCPQGETISNULL(void)
HB_FUNC( PQGETISNULL )
{
   if( hb_pcount() == 3 )
      hb_retl( PQgetisnull( ( PGresult * ) hb_parptr( 1 ), hb_parni( 2 ) - 1 , hb_parni( 3 ) - 1 ) );
}
postgres.c634
HB_FUNCPQFNUMBER(void)
HB_FUNC( PQFNUMBER )
{
   if( hb_pcount() == 2 )
      hb_retni( PQfnumber( ( PGresult * ) hb_parptr( 1 ), hb_parcx( 2 ) ) + 1 );
}
postgres.c640
HB_FUNCPQNTUPLES(void)
HB_FUNC( PQNTUPLES )
{
   if( hb_parinfo( 1 ) )
      hb_retnl( PQntuples( ( PGresult * ) hb_parptr( 1 ) ) );
}
postgres.c646
HB_FUNCPQNFIELDS(void)
HB_FUNC( PQNFIELDS )
{
   if( hb_parinfo( 1 ) )
      hb_retnl( PQnfields( ( PGresult * ) hb_parptr( 1 ) ) );
}
postgres.c652
HB_FUNCPQSENDQUERY(void)
HB_FUNC( PQSENDQUERY )
{
   int res = FALSE;

   if( hb_pcount() == 2 )
       res = PQsendQuery( PGconn_par( 1 ), hb_parcx( 2 ) );

   hb_retl( res );
}
postgres.c662
HB_FUNCPQGETRESULT(void)
HB_FUNC( PQGETRESULT )
{
   if( hb_parinfo( 1 ) )
   {
      PGresult * res = PQgetResult( PGconn_par( 1 ) );

      /* when null, no more result to catch */
      if( res )
         hb_retptr( res );
   }
}
postgres.c672
HB_FUNCPQCONSUMEINPUT(void)
HB_FUNC( PQCONSUMEINPUT )
{
   int res = 0;

   if( hb_parinfo( 1 ) )
      res = PQconsumeInput( PGconn_par( 1 ) );

   hb_retl( res );
}
postgres.c684
HB_FUNCPQISBUSY(void)
HB_FUNC( PQISBUSY )
{
   int res = FALSE;

   if( hb_parinfo( 1 ) )
      res = PQisBusy( PGconn_par( 1 ) );

   hb_retl( res );
}
postgres.c694
HB_FUNCPQREQUESTCANCEL(void)
HB_FUNC( PQREQUESTCANCEL ) /* deprecated */
{
   int res = FALSE;

   if( hb_parinfo( 1 ) )
      res = PQrequestCancel( PGconn_par( 1 ) );

   hb_retl( res );
}
postgres.c704
HB_FUNCPQFLUSH(void)
HB_FUNC( PQFLUSH )
{
   if( hb_parinfo( 1 ) )
      hb_retni( PQflush( PGconn_par( 1 ) ) );
}
postgres.c715
HB_FUNCPQSETNONBLOCKING(void)
HB_FUNC( PQSETNONBLOCKING )
{
   if( hb_pcount() == 2 )
      hb_retl( PQsetnonblocking( PGconn_par( 1 ), hb_parl( 2 ) ) );
}
postgres.c722
HB_FUNCPQISNONBLOCKING(void)
HB_FUNC( PQISNONBLOCKING )
{
   if( hb_parinfo( 1 ) )
      hb_retl( PQisnonblocking( PGconn_par( 1 ) ) );
}
postgres.c728
HB_FUNCPQCREATETRACE(void)
HB_FUNC( PQCREATETRACE )
{
#ifdef NODLL
   if( hb_parinfo( 1 ) )
   {
      FILE * pFile = fopen( hb_parcx( 1 ), "w+b" );

      if( pFile != NULL )
          hb_retptr( ( FILE * ) pFile );
   }
#endif
}
postgres.c738
HB_FUNCPQCLOSETRACE(void)
HB_FUNC( PQCLOSETRACE )
{
#ifdef NODLL
   if( hb_parinfo( 1 ) )
      fclose( ( FILE * ) hb_parptr( 1 ) );
#endif
}
postgres.c751
HB_FUNCPQTRACE(void)
HB_FUNC( PQTRACE )
{
#ifdef NODLL
   if( hb_pcount() == 2 )
      PQtrace( PGconn_par( 1 ), ( FILE * ) hb_parptr( 2 ) );
#endif
}
postgres.c759
HB_FUNCPQUNTRACE(void)
HB_FUNC( PQUNTRACE )
{
   if( hb_parinfo( 1 ) )
      PQuntrace( PGconn_par( 1 ) );
}
postgres.c767
HB_FUNCPQSETERRORVERBOSITY(void)
HB_FUNC( PQSETERRORVERBOSITY )
{
   /* PQERRORS_TERSE   0
      PQERRORS_DEFAULT 1
      PQERRORS_VERBOSE 2
   */

   if( hb_pcount() == 2 )
      hb_retni( ( PGVerbosity ) PQsetErrorVerbosity( PGconn_par( 1 ), ( PGVerbosity ) hb_parni( 2 ) ) );
}
postgres.c773
HB_FUNCLO_IMPORT(void)
HB_FUNC( LO_IMPORT )
{
   int ret = 0;

   if( hb_pcount() == 2 )
      ret = lo_import( PGconn_par( 1 ), hb_parcx( 2 ) );

   hb_retni( ret );
}
postgres.c790
HB_FUNCLO_EXPORT(void)
HB_FUNC( LO_EXPORT )
{
   int ret = FALSE;

   if( hb_pcount() == 3 )
      ret = ( lo_export( PGconn_par( 1 ), ( Oid ) hb_parnl( 2 ), hb_parcx( 3 ) ) == 1 );

   hb_retl( ret );
}
postgres.c800
HB_FUNCLO_UNLINK(void)
HB_FUNC( LO_UNLINK )
{
   int ret = FALSE;

   if( hb_pcount() == 2 )
      ret = ( lo_unlink( PGconn_par( 1 ), ( Oid ) hb_parnl( 2 ) ) == 1 );

   hb_retl( ret );
}
postgres.c810
HB_FUNCPQSERVERVERSION(void)
HB_FUNC( PQSERVERVERSION )
{
   if( hb_parinfo( 1 ) )
      hb_retni( PQserverVersion( PGconn_par( 1 ) ) );
}
postgres.c822
HB_FUNCPQGETCANCEL(void)
HB_FUNC( PQGETCANCEL )
{
   if( hb_parinfo( 1 ) )
      hb_retptr( ( PGcancel * ) PQgetCancel( PGconn_par( 1 ) ) );
}
postgres.c828
HB_FUNCPQCANCEL(void)
HB_FUNC( PQCANCEL )
{
   int ret = FALSE;

   if( hb_parinfo( 1 ) )
   {
      char errbuf[ 256 ];

      if( PQcancel( ( PGcancel * ) hb_parptr( 1 ), errbuf, sizeof( errbuf ) - 1 ) == 1 )
      {
         ret = TRUE;
         hb_storc( errbuf, 2 );
      }
   }

   hb_retl( ret );
}
postgres.c834
HB_FUNCPQFREECANCEL(void)
HB_FUNC( PQFREECANCEL )
{
   if( hb_parinfo( 1 ) )
      PQfreeCancel( ( PGcancel * ) hb_parptr( 1 ) ) ;
}
postgres.c852
HB_FUNCPQESCAPEBYTEACONN(void)
HB_FUNC( PQESCAPEBYTEACONN )
{
   unsigned const char * from = ( BYTE * ) hb_parc( 2 );
   size_t from_length = hb_parclen( 2 );
   size_t to_length = from_length * 5 + 1;

   unsigned char * to = PQescapeByteaConn( PGconn_par( 1 ), from, from_length, &to_length );
   hb_retc( ( char * ) to );
   PQfreemem( to );
}
postgres.c858
pgrdd.prg
TypeFunctionSourceLine
FUNCTIONDBPGCONNECTION( cConnString )
FUNCTION DBPGCONNECTION( cConnString )

   LOCAL aParams
   LOCAL oServer
   LOCAL nConn

   aParams := HB_ATOKENS( cConnString, ";" )

   asize( aParams, 6 )

   oServer := TPQServer():New( aParams[1], aParams[2], aParams[3], aParams[4], aParams[5], aParams[6] )

   IF oServer:NetErr()
      alert( oServer:ErrorMsg() )
      RETURN FAILURE
   ELSE
      aadd( s_aConnections, oServer )
      nConn := len( s_aConnections )
   ENDIF
     
RETURN nConn
pgrdd.prg77
FUNCTIONDBPGCLEARCONNECTION( nConn )
FUNCTION DBPGCLEARCONNECTION( nConn )

   LOCAL oServer

   oServer := s_aConnections[ nConn ]

   oServer:Close()

   s_aConnections[ nConn ] := nil

RETURN SUCCESS
pgrdd.prg99
STATIC FUNCTIONPG_INIT( nRDD )
STATIC FUNCTION PG_INIT( nRDD )

   USRRDD_RDDDATA( nRDD )

RETURN SUCCESS
pgrdd.prg116
STATIC FUNCTIONPG_NEW( pWA )
STATIC FUNCTION PG_NEW( pWA )

   USRRDD_AREADATA( pWA, array( AREA_LEN ) )

RETURN SUCCESS
pgrdd.prg129
STATIC FUNCTIONPG_OPEN( nWA, aOpenInfo )
STATIC FUNCTION PG_OPEN( nWA, aOpenInfo )
   LOCAL aField, oError, lError, cError, nResult
   LOCAL oServer, oQuery, aStruct, aFieldStruct
   LOCAL aWAData   := USRRDD_AREADATA( nWA )

   lError := .F.

   if !empty( aOpenInfo[ UR_OI_CONNECT ] ) .and. aOpenInfo[ UR_OI_CONNECT ] <= len( s_aConnections )
      oServer := s_aConnections[ aOpenInfo[ UR_OI_CONNECT ] ]
   endif

   if !empty( oServer )
      oServer:lAllCols := .F.
      oQuery := oServer:Query( aOpenInfo[ UR_OI_NAME ] )
      lError := oQuery:NetErr()
      cError := oQuery:ErrorMsg()
   else
      lError := .T. 
      cError := "Invalid connection handle"
   endif

   IF lError
      oError := ErrorNew()
      oError:GenCode     := EG_OPEN
      oError:SubCode     := 1000
      oError:Description := HB_LANGERRMSG( EG_OPEN ) + ", " + cError
      oError:FileName    := aOpenInfo[ UR_OI_NAME ]
      oError:CanDefault  := .T.
      UR_SUPER_ERROR( nWA, oError )
      RETURN FAILURE
   ELSE
      aWAData[ AREA_QUERY ] := oQuery
   ENDIF
     
   UR_SUPER_SETFIELDEXTENT( nWA, oQuery:nFields )

   aStruct := oQuery:Struct()

   FOR EACH aFieldStruct IN aStruct

       aField := ARRAY( UR_FI_SIZE )
       aField[ UR_FI_NAME ]    := aFieldStruct[ DBS_NAME ]
       aField[ UR_FI_TYPE ]    := aFieldStruct[ DBS_TYPE ]
       aField[ UR_FI_TYPEEXT ] := 0
       aField[ UR_FI_LEN ]     := aFieldStruct[ DBS_LEN ]
       aField[ UR_FI_DEC ]     := aFieldStruct[ DBS_DEC ]
       UR_SUPER_ADDFIELD( nWA, aField )

   NEXT

   /* Call SUPER OPEN to finish allocating work area (f.e.: alias settings) */
   nResult := UR_SUPER_OPEN( nWA, aOpenInfo )

RETURN nResult
pgrdd.prg135
STATIC FUNCTIONPG_CLOSE( nWA )
STATIC FUNCTION PG_CLOSE( nWA )
   LOCAL aWAData   := USRRDD_AREADATA( nWA )

   aWAData[ AREA_QUERY ]:Close()

RETURN UR_SUPER_CLOSE( nWA )
pgrdd.prg190
STATIC FUNCTIONPG_GETVALUE( nWA, nField, xValue )
STATIC FUNCTION PG_GETVALUE( nWA, nField, xValue )
   LOCAL aWAData   := USRRDD_AREADATA( nWA )
  
   if !empty( aWAData[ AREA_ROW ] )
      xValue := aWAData[ AREA_ROW ]:FieldGet( nField )
   else
      xValue := aWAData[ AREA_QUERY ]:FieldGet( nField )    
   endif

RETURN SUCCESS
pgrdd.prg197
STATIC FUNCTIONPG_PUTVALUE( nWA, nField, xValue )
STATIC FUNCTION PG_PUTVALUE( nWA, nField, xValue )
   LOCAL aWAData   := USRRDD_AREADATA( nWA )

   if empty( aWAData[ AREA_ROW ] )
      aWAData[ AREA_ROW ] := aWAData[ AREA_QUERY ]:GetRow()
   endif

   aWAData[ AREA_ROW ]:FieldPut( nField, xValue )

RETURN SUCCESS
pgrdd.prg208
STATIC FUNCTIONPG_SKIP( nWA, nRecords )
STATIC FUNCTION PG_SKIP( nWA, nRecords )
   LOCAL aWAData   := USRRDD_AREADATA( nWA )

   if !empty( aWAData[ AREA_ROW ] )
      PG_FLUSH( nWA )
   endif

   aWAData[ AREA_QUERY ]:Skip( nRecords )

RETURN SUCCESS
pgrdd.prg219
STATIC FUNCTIONPG_GOTOP( nWA )
STATIC FUNCTION PG_GOTOP( nWA )
RETURN PG_GOTO( nWA, 1 )
pgrdd.prg230
STATIC FUNCTIONPG_GOBOTTOM( nWA )
STATIC FUNCTION PG_GOBOTTOM( nWA )
RETURN PG_GOTO( nWA, -1 )
pgrdd.prg233
STATIC FUNCTIONPG_GOTOID( nWA, nRecord )
STATIC FUNCTION PG_GOTOID( nWA, nRecord )
RETURN PG_GOTO( nWA, nRecord )
pgrdd.prg236
STATIC FUNCTIONPG_GOTO( nWA, nRecord )
STATIC FUNCTION PG_GOTO( nWA, nRecord )
   LOCAL aWAData   := USRRDD_AREADATA( nWA )

   if !empty( aWAData[ AREA_ROW ] )
      PG_FLUSH( nWA )
   endif

   if nRecord < 0
      nRecord := aWAData[ AREA_QUERY ]:nLastRec
   elseif nRecord == 0
      nRecord := aWAData[ AREA_QUERY ]:nRecno
   endif

   aWAData[ AREA_QUERY ]:Goto( nRecord )

RETURN SUCCESS
pgrdd.prg239
STATIC FUNCTIONPG_RECCOUNT( nWA, nRecords )
STATIC FUNCTION PG_RECCOUNT( nWA, nRecords )
   LOCAL aWAData   := USRRDD_AREADATA( nWA )

   nRecords := aWAData[ AREA_QUERY ]:nLastRec

RETURN SUCCESS
pgrdd.prg256
STATIC FUNCTIONPG_BOF( nWA, lBof )
STATIC FUNCTION PG_BOF( nWA, lBof )
   LOCAL aWAData   := USRRDD_AREADATA( nWA )

   lBof := aWAData[ AREA_QUERY ]:lBof

RETURN SUCCESS
pgrdd.prg263
STATIC FUNCTIONPG_EOF( nWA, lEof )
STATIC FUNCTION PG_EOF( nWA, lEof )
   LOCAL aWAData   := USRRDD_AREADATA( nWA )

   lEof := aWAData[ AREA_QUERY ]:lEof

RETURN SUCCESS
pgrdd.prg270
STATIC FUNCTIONPG_RECID( nWA, nRecNo )
STATIC FUNCTION PG_RECID( nWA, nRecNo )
   LOCAL aWAData   := USRRDD_AREADATA( nWA )

   nRecno := aWAData[ AREA_QUERY ]:nRecNo

RETURN SUCCESS
pgrdd.prg277
STATIC FUNCTIONPG_DELETED( nWA, lDeleted )
STATIC FUNCTION PG_DELETED( nWA, lDeleted )
   HB_SYMBOL_UNUSED( nWA )
   lDeleted := .F.
RETURN SUCCESS
pgrdd.prg284
STATIC FUNCTIONPG_FLUSH( nWA )
STATIC FUNCTION PG_FLUSH( nWA )
   LOCAL oError
   LOCAL aWAData   := USRRDD_AREADATA( nWA )
   LOCAL nRecno

   if aWAData[ AREA_ROW ] != nil
      if !empty( aWAData[ AREA_APPEND ] )
         aWAData[ AREA_QUERY ]:Append( aWAData[ AREA_ROW ] )
      else
         nRecno := aWAData[ AREA_QUERY ]:nRecNo
         aWAData[ AREA_QUERY ]:Update( aWAData[ AREA_ROW ] )
      endif

      IF aWAData[ AREA_QUERY ]:lError
         oError := ErrorNew()
         oError:GenCode     := EG_DATATYPE
         oError:SubCode     := 3000
         oError:Description := HB_LANGERRMSG( EG_DATATYPE ) + ", " + aWAData[ AREA_QUERY ]:ErrorMsg() 
         UR_SUPER_ERROR( nWA, oError )
         RETURN FAILURE
      ENDIF

/*
 * The :Refresh() below costs a lot in term of performance. 
 * It redo the select to include inserts and updates.
 * It is the only solution I've found so far to simulate dbf behaviour
 */
      aWAData[ AREA_QUERY ]:Refresh( .T., .F. )

      if !empty( aWAData[ AREA_APPEND ] )
         aWAData[ AREA_APPEND ] := .F.
         nRecno := aWAData[ AREA_QUERY ]:nLastRec
      endif

      aWAData[ AREA_ROW ] := nil

      PG_GOTO( nWA, nRecno )

   endif

RETURN SUCCESS
pgrdd.prg289
STATIC FUNCTIONPG_APPEND( nWA, nRecords )
STATIC FUNCTION PG_APPEND( nWA, nRecords )
   LOCAL aWAData   := USRRDD_AREADATA( nWA )

   HB_SYMBOL_UNUSED( nRecords )

   aWAData[ AREA_ROW ] := aWAData[ AREA_QUERY ]:GetBlankRow()

   aWAData[ AREA_APPEND ] := .T.

RETURN SUCCESS
pgrdd.prg331
STATIC FUNCTIONPG_DELETE( nWA )
STATIC FUNCTION PG_DELETE( nWA )
   LOCAL oError
   LOCAL aWAData   := USRRDD_AREADATA( nWA )
 
   aWAData[ AREA_ROW ] := aWAData[ AREA_QUERY ]:GetRow()

   aWAData[ AREA_QUERY ]:Delete( aWAData[ AREA_ROW ] )

   IF aWAData[ AREA_QUERY ]:lError
      oError := ErrorNew()
      oError:GenCode     := EG_DATATYPE
      oError:SubCode     := 2000
      oError:Description := HB_LANGERRMSG( EG_DATATYPE ) + ", " + aWAData[ AREA_QUERY ]:ErrorMsg() 
      UR_SUPER_ERROR( nWA, oError )
      RETURN FAILURE
   ENDIF
     
   aWAData[ AREA_ROW ] := nil

RETURN SUCCESS
pgrdd.prg342
FUNCTIONPGRDD_GETFUNCTABLE( pFuncCount, pFuncTable, pSuperTable, nRddID )
FUNCTION PGRDD_GETFUNCTABLE( pFuncCount, pFuncTable, pSuperTable, nRddID )
   LOCAL cSuperRDD := NIL     /* NO SUPER RDD */
   LOCAL aMyFunc[ UR_METHODCOUNT ]

   aMyFunc[ UR_INIT         ] := ( @PG_INIT()         )
   aMyFunc[ UR_NEW          ] := ( @PG_NEW()          )
   aMyFunc[ UR_OPEN         ] := ( @PG_OPEN()         )
   aMyFunc[ UR_GETVALUE     ] := ( @PG_GETVALUE()     )
   aMyFunc[ UR_PUTVALUE     ] := ( @PG_PUTVALUE()     )
   aMyFunc[ UR_SKIP         ] := ( @PG_SKIP()         )
   aMyFunc[ UR_GOTO         ] := ( @PG_GOTO()         )
   aMyFunc[ UR_GOTOID       ] := ( @PG_GOTOID()       )
   aMyFunc[ UR_GOTOP        ] := ( @PG_GOTOP()        )
   aMyFunc[ UR_GOBOTTOM     ] := ( @PG_GOBOTTOM()     )
   aMyFunc[ UR_RECCOUNT     ] := ( @PG_RECCOUNT()     )
   aMyFunc[ UR_RECID        ] := ( @PG_RECID()        )
   aMyFunc[ UR_BOF          ] := ( @PG_BOF()          )
   aMyFunc[ UR_EOF          ] := ( @PG_EOF()          )
   aMyFunc[ UR_DELETED      ] := ( @PG_DELETED()      )
   aMyFunc[ UR_FLUSH        ] := ( @PG_FLUSH()        )
   aMyFunc[ UR_APPEND       ] := ( @PG_APPEND()       )
   aMyFunc[ UR_DELETE       ] := ( @PG_DELETE()       )
   aMyFunc[ UR_CLOSE        ] := ( @PG_CLOSE()        )

RETURN USRRDD_GETFUNCTABLE( pFuncCount, pFuncTable, pSuperTable, nRddID, ;
                            cSuperRDD, aMyFunc )
pgrdd.prg367
INIT PROCPG_INIT()
INIT PROC PG_INIT()
   rddRegister( "PGRDD", RDT_FULL )
RETURN
pgrdd.prg394
tpostgre.prg
TypeFunctionSourceLine
CLASSTPQServer
CLASS TPQServer
    DATA     pDb
    DATA     lTrans
    DATA     lallCols  INIT .T.
    DATA     Schema    INIT 'public'
    DATA     lError    INIT .F.
    DATA     cError    INIT ''
    DATA     lTrace    INIT .F.
    DATA     pTrace

    METHOD   New( cHost, cDatabase, cUser, cPass, nPort, Schema )
    METHOD   Destroy()
tpostgre.prg59
TPQSERVER:METHODClose()
    METHOD   Close()              INLINE ::Destroy()

    METHOD   StartTransaction()
tpostgre.prg71
TPQSERVER:METHODTransactionStatus()
    METHOD   TransactionStatus()  INLINE PQtransactionstatus(::pDb)
    METHOD   Commit()
    METHOD   Rollback()

    METHOD   Query( cQuery )
tpostgre.prg74
TPQSERVER:METHODExecute( cQuery )
    METHOD   Execute( cQuery )    INLINE ::Query(cQuery)
    METHOD   SetSchema( cSchema )
tpostgre.prg79
TPQSERVER:METHODNetErr()
    METHOD   NetErr()             INLINE ::lError
tpostgre.prg82
TPQSERVER:METHODErrorMsg()
    METHOD   ErrorMsg()           INLINE ::cError

    METHOD   TableExists( cTable )
    METHOD   ListTables()
    METHOD   TableStruct( cTable )
    METHOD   CreateTable( cTable, aStruct )
    METHOD   DeleteTable( cTable  )
    METHOD   TraceOn(cFile)
    METHOD   TraceOff()
tpostgre.prg83
TPQSERVER:METHODSetVerbosity(num)
    METHOD   SetVerbosity(num)    INLINE PQsetErrorVerbosity( ::pDb, iif( num >= 0 .and. num <= 2, num, 1 )  )

    //DESTRUCTOR Destroy
ENDCLASS
tpostgre.prg92
TPQSERVER:METHODNew( cHost, cDatabase, cUser, cPass, nPort, Schema ) CLASS TPQserver
METHOD New( cHost, cDatabase, cUser, cPass, nPort, Schema ) CLASS TPQserver
    Local res
    DEFAULT nPort TO 5432

    ::pDB := PQconnect(cDatabase, cHost, cUser, cPass, nPort)

    if PQstatus(::pDb) != CONNECTION_OK
        ::lError := .T.
        ::cError := PQerrormessage(::pDb)

    else
        if ! Empty(Schema)
            ::SetSchema(Schema)
        else
            res := PQexec( ::pDB, 'SELECT current_schema()' )
            if PQresultStatus(res) == PGRES_TUPLES_OK
                ::Schema := PQgetvalue( res, 1, 1 )
            endif
            PQclear(res)
        endif
    endif

RETURN self
tpostgre.prg98
TPQSERVER:METHODDestroy() CLASS TPQserver
METHOD Destroy() CLASS TPQserver
    ::TraceOff()
    PQClose(::pDb)
RETURN nil
tpostgre.prg123
TPQSERVER:METHODSetSchema( cSchema ) CLASS TPQserver
METHOD SetSchema( cSchema ) CLASS TPQserver
    Local res
    Local result := .F.

    if PQstatus(::pDb) == CONNECTION_OK
        ::Schema := cSchema
        res := PQexec( ::pDB, 'SET search_path TO ' + cSchema )
        result := (PQresultStatus(res) == PGRES_COMMAND_OK)
        PQclear(res)
    endif
RETURN result
tpostgre.prg129
TPQSERVER:METHODStartTransaction() CLASS TPQserver
METHOD StartTransaction() CLASS TPQserver
    Local res, lError

    res    := PQexec( ::pDB, 'BEGIN' )
    lError := PQresultstatus(res) != PGRES_COMMAND_OK

    if lError
        ::lError := .T.
        ::cError := PQresultErrormessage(res)
    else
        ::lError := .F.
        ::cError := ''
    endif
    PQclear(res)
RETURN lError
tpostgre.prg142
TPQSERVER:METHODCommit() CLASS TPQserver
METHOD Commit() CLASS TPQserver
    Local res, lError

    res    := PQexec( ::pDB, 'COMMIT' )
    lError := PQresultstatus(res) != PGRES_COMMAND_OK

    if lError
        ::lError := .T.
        ::cError := PQresultErrormessage(res)
    else
        ::lError := .F.
        ::cError := ''
    endif
    PQclear(res)
RETURN lError
tpostgre.prg159
TPQSERVER:METHODRollback() CLASS TPQserver
METHOD Rollback() CLASS TPQserver
    Local res, lError

    res    := PQexec( ::pDB, 'ROLLBACK' )
    lError := PQresultstatus(res) != PGRES_COMMAND_OK

    if lError
        ::lError := .T.
        ::cError := PQresultErrormessage(res)
    else
        ::lError := .F.
        ::cError := ''
    endif
    PQclear(res)
RETURN lError
tpostgre.prg176
TPQSERVER:METHODQuery( cQuery ) CLASS TPQserver
METHOD Query( cQuery ) CLASS TPQserver
    Local oQuery

    oQuery := TPQquery():New(::pDB, cQuery, ::lallCols, ::Schema)
RETURN oQuery
tpostgre.prg193
TPQSERVER:METHODTableExists( cTable ) CLASS TPQserver
METHOD TableExists( cTable ) CLASS TPQserver
    Local result := .F.
    Local cQuery
    Local res

    cQuery := "select table_name "
    cQuery += "  from information_schema.tables "
    cQuery += " where table_type = 'BASE TABLE' and table_schema = " + DataToSql(::Schema) + " and table_name = " + DataToSql(lower(cTable))

    res := PQexec( ::pDB, cQuery )

    if PQresultstatus(res) == PGRES_TUPLES_OK
        result := (PQlastrec(res) != 0)
        ::lError := .F.
        ::cError := ''
    else
        ::lError := .T.
        ::cError := PQresultErrormessage(res)
    endif

    PQclear(res)
RETURN result
tpostgre.prg200
TPQSERVER:METHODListTables() CLASS TPQserver
METHOD ListTables() CLASS TPQserver
    Local result := {}
    Local cQuery
    Local res
    Local i

    cQuery := "select table_name "
    cQuery += "  from information_schema.tables "
    cQuery += " where table_schema = " + DataToSql(::Schema) + " and table_type = 'BASE TABLE' "

    res := PQexec( ::pDB, cQuery )

    if PQresultstatus(res) == PGRES_TUPLES_OK
        For i := 1 to PQlastrec(res)
            aadd( result, PQgetvalue( res, i, 1 ) )
        Next
        ::lError := .F.
        ::cError := ''
    else
        ::lError := .T.
        ::cError := PQresultErrormessage(res)
    endif

    PQclear(res)
RETURN result
tpostgre.prg224
TPQSERVER:METHODTableStruct( cTable ) CLASS TPQserver
METHOD TableStruct( cTable ) CLASS TPQserver
    Local result := {}
    Local cQuery
    Local res
    Local i
    Local cField
    Local cType
    Local nSize
    Local nDec

    cQuery := "SELECT column_name, data_type, character_maximum_length, numeric_precision, numeric_scale "
    cQuery += "  FROM information_schema.columns "
    cQuery += " WHERE table_schema = " + DataToSql(::Schema) + " and table_name = " + DataToSql(lower(cTable))
    cQuery += "ORDER BY ordinal_position "

    res := PQexec( ::pDB, cQuery )

    if PQresultstatus(res) == PGRES_TUPLES_OK
        For i := 1 to PQlastrec(res)
            cField    := PQgetvalue(res, i, 1)
            cType     := PQgetvalue(res, i, 2)
            nSize     := PQgetvalue(res, i, 4)
            nDec      := PQgetvalue(res, i, 5)

            if 'char' $ cType
                cType := 'C'
                nSize := Val(PQgetvalue(res, i, 3))
                nDec  := 0

            elseif 'text' $ cType
                cType := 'M'
                nSize := 10
                nDec := 0

            elseif 'boolean' $ cType
                cType := 'L'
                nSize := 1
                nDec  := 0

            elseif 'smallint' $ cType
                cType := 'N'
                nSize := 5
                nDec  := 0

            elseif 'integer' $ cType .or. 'serial' $ cType
                cType := 'N'
                nSize := 9
                nDec  := 0

            elseif 'bigint' $ cType .or. 'bigserial' $ cType
                cType := 'N'
                nSize := 19
                nDec  := 0

            elseif 'decimal' $ cType .or. 'numeric' $ cType
                cType := 'N'
                nDec  := val(nDec)
                // Postgres don't store ".", but .dbf does, it can cause data width problem
                nSize := val(nSize) + iif( nDec > 0, 1, 0 )

                // Numeric/Decimal without scale/precision can genarete big values, so, i limit this to 10,5

                if nDec > 100
                    nDec := 5
                endif

                if nSize > 100
                    nSize := 15
                endif

            elseif 'real' $ cType .or. 'float4' $ cType
                cType := 'N'
                nSize := 15
                nDec  :=  4

            elseif 'double precision' $ cType .or. 'float8' $ cType
                cType := 'N'
                nSize := 19
                nDec  := 9

            elseif 'money' $ cType
                cType := 'N'
                nSize := 9
                nDec  := 2

            elseif 'timestamp' $ cType
                cType := 'C'
                nSize := 20
                nDec  := 0

            elseif 'date' $ cType
                cType := 'D'
                nSize := 8
                nDec  := 0

            elseif 'time' $ cType
                cType := 'C'
                nSize := 10
                nDec  := 0

            else
                // Unsuported
                cType := 'U'
                nSize := 0
                nDec  := -1

            end

            if !( cType == 'U' )
                aadd( result, { cField, cType, nSize, nDec } )
            end

        Next
        ::lError := .F.
        ::cError := ''
    else
        ::lError := .T.
        ::cError := PQresultErrormessage(res)
    endif

    PQclear(res)
RETURN result
tpostgre.prg250
TPQSERVER:METHODCreateTable( cTable, aStruct ) CLASS TPQserver
METHOD CreateTable( cTable, aStruct ) CLASS TPQserver
    Local result := .T.
    Local cQuery
    Local res
    Local i

    cQuery := 'CREATE TABLE ' + ::Schema + '.' + cTable + '( '

    For i := 1 to Len(aStruct)

        cQuery += aStruct[i, 1]

        if aStruct[ i, 2 ] == "C"
            cQuery += ' Char(' + ltrim(str(aStruct[i, 3])) + ')'

        elseif aStruct[ i, 2 ] == "D"
            cQuery += ' Date '

        elseif aStruct[ i, 2 ] == "N"
            cQuery += ' Numeric(' + ltrim(str(aStruct[i, 3])) + ',' + ltrim(str(aStruct[i,4])) + ')'

        elseif aStruct[ i, 2 ] == "L"
            cQuery += ' boolean '

        elseif aStruct[ i, 2 ] == "M"
            cQuery += ' text '
        end

        if i == Len(aStruct)
            cQuery += ')'
        else
            cQuery += ','
        end
    Next

    res := PQexec( ::pDB, cQuery )

    if PQresultstatus(res) != PGRES_COMMAND_OK
        result := .F.
        ::lError := .T.
        ::cError := PQresultErrormessage(res)
    else
        ::lError := .F.
        ::cError := ''
    end

    PQclear(res)
RETURN result
tpostgre.prg373
TPQSERVER:METHODDeleteTable( cTable ) CLASS TPQserver
METHOD DeleteTable( cTable ) CLASS TPQserver
    Local result := .T.
    Local res

    res := PQexec( ::pDB, 'DROP TABLE ' + ::Schema + '.' + cTable  )

    if PQresultstatus(res) != PGRES_COMMAND_OK
        result := .F.
        ::lError := .T.
        ::cError := PQresultErrormessage(res)
    else
        ::lError := .F.
        ::cError := ''
    end

    PQclear(res)
RETURN result
tpostgre.prg423
TPQSERVER:METHODTraceOn( cFile ) CLASS TPQserver
METHOD TraceOn( cFile ) CLASS TPQserver
    ::pTrace := PQcreatetrace( cFile )

    if ::pTrace != NIL
        PQtrace( ::pDb, ::pTrace )
        ::lTrace := .t.
    endif
RETURN nil
tpostgre.prg442
TPQSERVER:METHODTraceOff() CLASS TPQserver
METHOD TraceOff() CLASS TPQserver
    if ::pTrace != NIL
        PQuntrace( ::pDb )
        PQclosetrace( ::pTrace )
    endif

    ::lTrace := .f.
RETURN nil
tpostgre.prg452
CLASSTPQQuery
CLASS TPQQuery
    DATA     pQuery
    DATA     pDB

    DATA     nResultStatus

    DATA     lBof
    DATA     lEof
    DATA     lRead
    DATA     lAllCols INIT .T.

    DATA     lError   INIT .F.
    DATA     cError   INIT ''

    DATA     cQuery
    DATA     nRecno
    DATA     nFields
    DATA     nLastrec

    DATA     aStruct
    DATA     aKeys
    DATA     TableName
    DATA     Schema
    DATA     rows     INIT 0

    METHOD   New( pDB, cQuery, lallCols, cSchema, res )
    METHOD   Destroy()
tpostgre.prg463
TPQQUERY:METHODClose()
    METHOD   Close()            INLINE ::Destroy()

    METHOD   Refresh()
tpostgre.prg490
TPQQUERY:METHODFetch()
    METHOD   Fetch()            INLINE ::Skip()
    METHOD   Read()
    METHOD   Skip( nRecno )
tpostgre.prg493
TPQQUERY:METHODBof()
    METHOD   Bof()              INLINE ::lBof
tpostgre.prg497
TPQQUERY:METHODEof()
    METHOD   Eof()              INLINE ::lEof
tpostgre.prg498
TPQQUERY:METHODRecNo()
    METHOD   RecNo()            INLINE ::nRecno
tpostgre.prg499
TPQQUERY:METHODLastrec()
    METHOD   Lastrec()          INLINE ::nLastrec
    METHOD   Goto(nRecno)
tpostgre.prg500
TPQQUERY:METHODNetErr()
    METHOD   NetErr()           INLINE ::lError
tpostgre.prg503
TPQQUERY:METHODErrorMsg()
    METHOD   ErrorMsg()         INLINE ::cError
tpostgre.prg504
TPQQUERY:METHODFCount()
    METHOD   FCount()           INLINE ::nFields
    METHOD   FieldName( nField )
    METHOD   FieldPos( cField )
    METHOD   FieldLen( nField )
    METHOD   FieldDec( nField )
    METHOD   FieldType( nField )
    METHOD   Update( oRow )
    METHOD   Delete( oRow )
    METHOD   Append( oRow )
    METHOD   SetKey()
tpostgre.prg506
TPQQUERY:METHODChanged(nField)
    METHOD   Changed(nField)    INLINE ::aRow[nField] != ::aOld[nField]
tpostgre.prg517
TPQQUERY:METHODBlank()
    METHOD   Blank()            INLINE ::GetBlankRow()

    METHOD   Struct()

    METHOD   FieldGet( nField, nRow )
    METHOD   GetRow( nRow )
    METHOD   GetBlankRow()

    //DESTRUCTOR Destroy
ENDCLASS
tpostgre.prg518
TPQQUERY:METHODNew( pDB, cQuery, lallCols, cSchema, res ) CLASS TPQquery
METHOD New( pDB, cQuery, lallCols, cSchema, res ) CLASS TPQquery
    ::pDB      := pDB
    ::nResultStatus := -1
    ::cQuery   := cQuery
    ::lallCols := lallCols
    ::Schema   := cSchema

    if ! ISNIL(res)
        ::pQuery := res
    endif

    ::Refresh(ISNIL(res))
RETURN self
tpostgre.prg530
TPQQUERY:METHODDestroy() CLASS TPQquery
METHOD Destroy() CLASS TPQquery
    if !( ::nResultStatus == -1 )
        PQclear( ::pQuery )
        ::nResultStatus := -1
    endif
RETURN .T.
tpostgre.prg545
TPQQUERY:METHODRefresh(lQuery,lMeta) CLASS TPQquery
METHOD Refresh(lQuery,lMeta) CLASS TPQquery
    Local res
    Local cTableCodes := ''
    Local cFieldCodes := ''
    Local aStruct := {}
    Local aTemp := {}
    Local i
    Local cType, nDec, nSize

    Default lQuery To .T.
    Default lMeta To .T.

    ::Destroy()

    ::lBof     := .T.
    ::lEof     := .T.
    ::lRead    := .F.
    ::nRecno   := 0
    ::nLastrec := 0
    ::Rows     := 0

    if lQuery
        res := PQexec( ::pDB, ::cQuery )
    else
        res := ::pQuery
    endif

    ::nResultStatus := PQresultstatus(res)

    if ::nResultStatus == PGRES_TUPLES_OK

       if lMeta
          ::aStruct  := {}
          ::nFields  := 0
          // Get some information about metadata
           aTemp := PQmetadata(res)
           if ISARRAY(aTemp)
              For i := 1 to Len(aTemp)
                   cType := aTemp[ i, 2 ]
                   nSize := aTemp[ i, 3 ]
                   nDec  := aTemp[ i, 4 ]

                   if 'char' $ cType
                       cType := 'C'

                   elseif 'numeric' $ cType .or. 'decimal' $ cType
                       cType := 'N'

                       // Postgres don't store ".", but .dbf does, it can cause data width problem
                       if nDec > 0
                          nSize++
                          // Numeric/Decimal without scale/precision can genarete big values, so, i limit this to 10,5
                          if nDec > 100
                             nDec := 5
                          endif
                       endif

                       if nSize > 100
                          nSize := 15
                       endif

                   elseif 'date' $ cType
                       cType := 'D'
                       nSize := 8

                   elseif 'text' $ cType
                       cType := 'M'

                   elseif 'boolean' $ cType
                       cType := 'L'
                       nSize := 1

                   elseif 'smallint' $ cType
                       cType := 'N'
                       nSize := 5

                   elseif 'integer' $ cType .or. 'serial' $ cType
                       cType := 'N'
                       nSize := 9

                   elseif 'bigint' $ cType .or. 'bigserial' $ cType
                       cType := 'N'
                       nSize := 19

                   elseif 'real' $ cType .or. 'float4' $ cType
                       cType := 'N'
                       nSize := 15
                       nDec  :=  4

                   elseif 'double precision' $ cType .or. 'float8' $ cType
                       cType := 'N'
                       nSize := 19
                       nDec  := 9

                   elseif 'money' $ cType
                       cType := 'N'
                       nSize := 10
                       nDec  := 2

                   elseif 'timestamp' $ cType
                       cType := 'C'
                       nSize := 20

                   elseif 'time' $ cType
                       cType := 'C'
                       nSize := 10

                   else
                       // Unsuported
                       cType := 'K'
                   endif

                   aadd( aStruct, {aTemp[ i, 1 ], cType, nSize, nDec, aTemp[i, 5], aTemp[i, 6]} )
               Next

               ::nFields := PQfcount(res)

               ::aStruct := aStruct

           endif
        endif

        ::nLastrec := PQlastrec(res)
        ::lError := .F.
        ::cError := ''

        if ::nLastrec != 0
           ::nRecno := 1
           ::lBof := .F.
           ::lEof := .F.
        endif

    elseif ::nResultStatus == PGRES_COMMAND_OK
        ::lError := .F.
        ::cError := ''
        ::rows   := val(PQcmdTuples(res))

    else
        ::lError := .T.
        ::cError := PQresultErrormessage(res)
    endif

    ::pQuery := res

RETURN ! ::lError
tpostgre.prg553
TPQQUERY:METHODStruct() CLASS TPQquery
METHOD Struct() CLASS TPQquery
    Local result := {}
    Local i

    For i := 1 to Len(::aStruct)
        aadd( result, { ::aStruct[i, 1], ::aStruct[i, 2], ::aStruct[i, 3], ::aStruct[i, 4] })
    Next
RETURN result
tpostgre.prg700
TPQQUERY:METHODRead() CLASS TPQquery
METHOD Read() CLASS TPQquery

   if !::lEof
      if !::lRead
       ::lRead := .T.
      else
       ::Skip( 1 )
      endif
   endif

RETURN !::lEof
tpostgre.prg709
TPQQUERY:METHODSkip( nrecno ) CLASS TPQquery
METHOD Skip( nrecno ) CLASS TPQquery
    DEFAULT nRecno TO 1

    if ::nRecno + nRecno > 0 .and. ::nRecno + nRecno <= ::nLastrec
        ::nRecno := ::nRecno + nRecno
        ::lEof := .F.
        ::lBof := .F.

    else
        if ::nRecno + nRecno > ::nLastRec
            ::nRecno := ::nLastRec + 1
            ::lEof := .T.
        end

        if ::nRecno + nRecno < 1
            ::nRecno := 1
            ::lBof := .T.
        end
    end
RETURN .T.
tpostgre.prg721
TPQQUERY:METHODGoto( nRecno ) CLASS TPQquery
METHOD Goto( nRecno ) CLASS TPQquery
    if nRecno > 0 .and. nRecno <= ::nLastrec
        ::nRecno := nRecno
        ::lEof := .F.
    end
RETURN .T.
tpostgre.prg743
TPQQUERY:METHODFieldPos( cField ) CLASS TPQquery
METHOD FieldPos( cField ) CLASS TPQquery

    cField := trim(Lower(cField))

RETURN AScan( ::aStruct, {|x| x[1] == cField })
tpostgre.prg751
TPQQUERY:METHODFieldName( nField ) CLASS TPQquery
METHOD FieldName( nField ) CLASS TPQquery
    Local result

    if ISCHARACTER(nField)
        nField := ::Fieldpos(nField)
    elseif nField < 1 .or. nField > len(::aStruct)
        nField := 0
    endif

    if nField > 0
        result := ::aStruct[nField, 1]
    endif
RETURN result
tpostgre.prg757
TPQQUERY:METHODFieldType( nField ) CLASS TPQquery
METHOD FieldType( nField ) CLASS TPQquery
    Local result

    if ISCHARACTER(nField)
        nField := ::Fieldpos(nField)
    elseif nField < 1 .or. nField > len(::aStruct)
        nField := 0
    endif

    if nField > 0
        result := ::aStruct[nField, 2]
    end
RETURN result
tpostgre.prg772
TPQQUERY:METHODFieldLen( nField ) CLASS TPQquery
METHOD FieldLen( nField ) CLASS TPQquery
    Local result

    if ISCHARACTER(nField)
        nField := ::Fieldpos(nField)
    elseif nField < 1 .or. nField > len(::aStruct)
        nField := 0
    endif

    if nField > 0
        result := ::aStruct[nField, 3]
    end
RETURN result
tpostgre.prg787
TPQQUERY:METHODFieldDec( nField ) CLASS TPQquery
METHOD FieldDec( nField ) CLASS TPQquery
    Local result

    if ISCHARACTER(nField)
        nField := ::Fieldpos(nField)
    elseif nField < 1 .or. nField > len(::aStruct)
        nField := 0
    endif

    if nField > 0
        result := ::aStruct[nField, 4]
    end
RETURN result
tpostgre.prg802
TPQQUERY:METHODDelete(oRow) CLASS TPQquery
METHOD Delete(oRow) CLASS TPQquery
    Local res
    Local i
    Local nField
    Local xField
    Local cQuery
    Local cWhere := ''
    Local aParams := {}

    ::SetKey()

    if ! Empty(::Tablename) .and. ! Empty(::aKeys)
        For i := 1 to len(::aKeys)
            nField := oRow:Fieldpos(::aKeys[i])
            xField := oRow:FieldGetOld(nField)

            cWhere += ::aKeys[i] + ' = $' + ltrim(str(i))

            AADD( aParams, ValueToString(xField) )

            if i != len(::aKeys)
                cWhere += ' and '
            endif
        Next

        if ! (cWhere == '')
            cQuery := 'DELETE FROM ' + ::Schema + '.' + ::Tablename + ' WHERE ' + cWhere
            res := PQexecParams( ::pDB, cQuery, aParams)

            if PQresultstatus(res) != PGRES_COMMAND_OK
                ::lError := .T.
                ::cError := PQresultErrormessage(res)
                ::rows   := 0
            else
                ::lError := .F.
                ::cError := ''
                ::rows   := val(PQcmdTuples(res))
            endif
            PQclear(res)
        end
    else
        ::lError := .T.
        ::cError := 'There is no primary keys or query is a joined table'
    endif
RETURN ! ::lError
tpostgre.prg817
TPQQUERY:METHODAppend( oRow ) CLASS TPQquery
METHOD Append( oRow ) CLASS TPQquery
    Local cQuery
    Local i
    Local res
    Local lChanged := .f.
    Local aParams := {}
    Local nParams := 0

    ::SetKey()

    if ! Empty(::Tablename)
        cQuery := 'INSERT INTO ' + ::Schema + '.' + ::Tablename + '('
        For i := 1 to oRow:FCount()
            if ::lallCols .or. oRow:changed(i)
                lChanged := .t.
                cQuery += oRow:Fieldname(i) + ','
            endif
        Next

        cQuery := Left( cQuery, len(cQuery) - 1 ) +  ') VALUES ('

        For i := 1 to oRow:FCount()
            if ::lallCols .or. oRow:Changed(i)
                nParams++
                cQuery += '$' + ltrim(str(nParams)) + ','
                aadd( aParams, ValueToString(oRow:FieldGet(i)) )
            endif
        Next

        cQuery := Left( cQuery, len(cQuery) - 1  ) + ')'

        if lChanged
            res := PQexecParams( ::pDB, cQuery, aParams)

            if PQresultstatus(res) != PGRES_COMMAND_OK
                ::lError := .T.
                ::cError := PQresultErrormessage(res)
                ::rows   := 0
            else
                ::lError := .F.
                ::cError := ''
                ::rows   := val(PQcmdTuples(res))
            endif

            PQclear(res)
        endif
    else
        ::lError := .T.
        ::cError := 'Cannot insert in a joined table, or unknown error'
    endif
RETURN ! ::lError
tpostgre.prg864
TPQQUERY:METHODUpdate(oRow) CLASS TPQquery
METHOD Update(oRow) CLASS TPQquery
    Local result := .F.
    Local cQuery
    Local i
    Local nField
    Local xField
    Local cWhere
    Local res
    Local lChanged := .f.
    Local aParams := {}
    Local nParams := 0

    ::SetKey()

    if ! Empty(::Tablename) .and. ! Empty(::aKeys)
        cWhere := ''
        For i := 1 to len(::aKeys)

            nField := oRow:Fieldpos(::aKeys[i])
            xField := oRow:FieldGetOld(nField)

            cWhere += ::aKeys[i] + '=' + DataToSql(xField)

            if i != len(::aKeys)
                cWhere += ' and '
            end
        Next

        cQuery := 'UPDATE ' + ::Schema + '.' + ::Tablename + ' SET '
        For i := 1 to oRow:FCount()
            if ::lallcols .or. oRow:Changed(i)
                lChanged := .t.
                nParams++
                cQuery += oRow:Fieldname(i) + ' = $' + ltrim(str(nParams)) + ','
                aadd( aParams, ValueToString(oRow:FieldGet(i)) )
            end
        Next

        if ! (cWhere == '') .and. lChanged

            cQuery := Left( cQuery, len(cQuery) - 1 ) + ' WHERE ' + cWhere

            res := PQexecParams( ::pDB, cQuery, aParams)

            if PQresultstatus(res) != PGRES_COMMAND_OK
                ::lError := .T.
                ::cError := PQresultErrormessage(res)
                ::rows   := 0
            else
                ::lError := .F.
                ::cError := ''
                ::rows   := val(PQcmdTuples(res))
            endif

            PQclear(res)
        end
    else
        ::lError := .T.
        ::cError := 'Cannot insert in a joined table, or unknown error'
    endif
RETURN ! ::lError
tpostgre.prg917
TPQQUERY:METHODFieldGet( nField, nRow ) CLASS TPQquery
METHOD FieldGet( nField, nRow ) CLASS TPQquery
    Local result
    Local cType
    Local nSize

    if ISCHARACTER(nField)
        nField := ::Fieldpos(nField)
    elseif nField < 1 .or. nField > ::nFields
        nField := 0
    endif

    if nField > 0 .and. ::nResultStatus == PGRES_TUPLES_OK

        if ISNIL(nRow)
            nRow := ::nRecno
        endif

        result := PQgetvalue( ::pQuery, nRow, nField)
        cType := ::aStruct[ nField, 2 ]
        nSize := ::aStruct[ nField, 3 ]

        if cType == "C"
            if ISNIL(result)
                result := ""
            else
                result := result
            end

        elseif cType == "N"
            if ! ISNIL(result)
                result := val(result)
            else
                result := 0
            end

        elseif cType == "D"
            if ! ISNIL(result)
                result := StoD( strtran( result, "-", "" ) )
            else
                result := CtoD('')
            end

        elseif cType == "L"
            if ! ISNIL(result)
                result := (result == 't')
            else
                result := .F.
            end

        elseif cType == "M"
            if ISNIL(result)
                result := ""
            else
                result := result
            end

        end
    end
RETURN result
tpostgre.prg980
TPQQUERY:METHODGetrow( nRow ) CLASS TPQquery
METHOD Getrow( nRow ) CLASS TPQquery
    Local result, aRow := {}, aOld := {}, nCol

    DEFAULT nRow TO ::nRecno

    if ::nResultStatus == PGRES_TUPLES_OK

        if nRow > 0 .and. nRow <= ::nLastRec

            ASize(aRow, ::nFields)
            ASize(aOld, ::nFields)

            For nCol := 1 to ::nFields
                aRow[nCol] := ::Fieldget(nCol, nRow)
                aOld[nCol] := ::Fieldget(nCol, nRow)
            Next

            result := TPQRow():New( aRow, aOld, ::aStruct )

        elseif nRow > ::nLastrec
            result := ::GetBlankRow()
        end
    end
RETURN result
tpostgre.prg1041
TPQQUERY:METHODGetBlankRow() CLASS TPQquery
METHOD GetBlankRow() CLASS TPQquery
    Local result, aRow := {}, aOld := {}, i

    ASize(aRow, ::nFields)
    ASize(aOld, ::nFields)

    For i := 1 to ::nFields
        if ::aStruct[i, 2] == 'C'
            aRow[i] := ''
            aOld[i] := ''
        elseif ::aStruct[i, 2] == 'N'
            aRow[i] := 0
            aOld[i] := 0
        elseif ::aStruct[i, 2] == 'L'
            aRow[i] := .F.
            aOld[i] := .F.
        elseif ::aStruct[i, 2] == 'D'
            aRow[i] := CtoD('')
            aOld[i] := CtoD('')
        elseif ::aStruct[i, 2] == 'M'
            aRow[i] := ''
            aOld[i] := ''
        end
    Next

    result := TPQRow():New( aRow, aOld, ::aStruct )
RETURN result
tpostgre.prg1067
TPQQUERY:METHODSetKey() CLASS TPQquery
METHOD SetKey() CLASS TPQquery
    Local cQuery
    Local i, x
    Local nTableId, xTableId := -1
    Local nCount := 0
    Local res
    Local nPos

    if ::nResultStatus == PGRES_TUPLES_OK
        if ISNIL(::Tablename)
            /* set the table name looking for table oid */
            for i := 1 to len(::aStruct)
                /* Store table codes oid */
                nTableId := ::aStruct[i, 5]

                if nTableId != xTableId
                    xTableId := nTableId
                    nCount++
                endif
            next

            if nCount == 1
                /* first, try get the table name from select, else get from pg_catalog */
                if (npos := at('FROM ', Upper(::cQuery))) != 0
                    cQuery := lower(ltrim(substr( ::cQuery, nPos + 5 )))

                    if (npos := at('.', cQuery)) != 0
                        ::Schema := alltrim(left(cQuery,npos-1))
                        cQuery := substr(cQuery, nPos + 1)
                    endif

                    if (npos := at(' ', cQuery)) != 0
                        ::Tablename := trim(Left(cQuery, npos))
                    else
                        ::Tablename := cQuery
                    endif
                endif

                if empty(::Tablename)
                    cQuery := 'select relname from pg_class where oid = ' + str(xTableId)

                    res := PQexec(::pDB, cQuery)

                    if PQresultstatus(res) == PGRES_TUPLES_OK .and. PQlastrec(res) != 0
                        ::Tablename := trim(PQgetvalue(res, 1, 1))
                    endif

                    PQclear(res)
                endif
            endif
        endif

        if ISNIL(::aKeys) .and. ! empty(::Tablename)
            /* Set the table primary keys */
            cQuery := "SELECT c.attname "
            cQuery += "  FROM pg_class a, pg_class b, pg_attribute c, pg_index d, pg_namespace e "
            cQuery += " WHERE a.oid = d.indrelid "
            cQuery += "   AND a.relname = '" + ::Tablename + "'"
            cQuery += "   AND b.oid = d.indexrelid "
            cQuery += "   AND c.attrelid = b.oid "
            cQuery += "   AND d.indisprimary "
            cQuery += "   AND e.oid = a.relnamespace "
            cQuery += "   AND e.nspname = " + DataToSql(::Schema)

            res := PQexec(::pDB, cQuery)

            if PQresultstatus(res) == PGRES_TUPLES_OK .and. PQlastrec(res) != 0
                ::aKeys := {}

                For x := 1 To PQlastrec(res)
                    aadd( ::aKeys, PQgetvalue( res, x, 1 ) )
                Next
            endif

            PQclear(res)
        endif
    endif

RETURN nil
tpostgre.prg1096
CLASSTPQRow
CLASS TPQRow
   DATA     aRow
   DATA     aOld
   DATA     aStruct

   METHOD   New( row, old, struct )
tpostgre.prg1176
TPQROW:METHODFCount()
   METHOD   FCount()           INLINE Len(::aRow)
   METHOD   FieldGet( nField )
   METHOD   FieldPut( nField, Value )
   METHOD   FieldName( nField )
   METHOD   FieldPos( cFieldName )
   METHOD   FieldLen( nField )
   METHOD   FieldDec( nField )
   METHOD   FieldType( nField )
tpostgre.prg1183
TPQROW:METHODChanged( nField )
   METHOD   Changed( nField )     INLINE !(::aRow[nField] == ::aOld[nField])
tpostgre.prg1191
TPQROW:METHODFieldGetOld( nField )
   METHOD   FieldGetOld( nField ) INLINE ::aOld[nField]
ENDCLASS
tpostgre.prg1192
TPQROW:METHODnew( row, old, struct) CLASS TPQrow
METHOD new( row, old, struct) CLASS TPQrow
    ::aRow := row
    ::aOld := old
    ::aStruct := struct
RETURN self
tpostgre.prg1196
TPQROW:METHODFieldGet( nField ) CLASS TPQrow
METHOD FieldGet( nField ) CLASS TPQrow
    Local result

    if ISCHARACTER(nField)
        nField := ::Fieldpos(nField)
    endif

    if nField >= 1 .and. nField <= len(::aRow)
        result := ::aRow[nField]
    end

RETURN result
tpostgre.prg1203
TPQROW:METHODFieldPut( nField, Value ) CLASS TPQrow
METHOD FieldPut( nField, Value ) CLASS TPQrow
    Local result

    if ISCHARACTER(nField)
        nField := ::Fieldpos(nField)
    endif

    if nField >= 1 .and. nField <= len(::aRow)
        result := ::aRow[nField] := Value
    end
RETURN result
tpostgre.prg1217
TPQROW:METHODFieldName( nField ) CLASS TPQrow
METHOD FieldName( nField ) CLASS TPQrow
    Local result

    if ISCHARACTER(nField)
        nField := ::Fieldpos(nField)
    endif

    if nField >= 1 .and. nField <= len(::aStruct)
        result := ::aStruct[nField, 1]
    end

RETURN result
tpostgre.prg1230
TPQROW:METHODFieldPos( cField ) CLASS TPQrow
METHOD FieldPos( cField ) CLASS TPQrow
    Local result  := 0

    result := AScan( ::aStruct, {|x| x[1] == trim(lower(cField)) })

RETURN result
tpostgre.prg1244
TPQROW:METHODFieldType( nField ) CLASS TPQrow
METHOD FieldType( nField ) CLASS TPQrow
    Local result

    if ISCHARACTER(nField)
        nField := ::Fieldpos(nField)
    endif

    if nField >= 1 .and. nField <= len(::aStruct)
        result := ::aStruct[nField, 2]
    end

RETURN result
tpostgre.prg1252
TPQROW:METHODFieldLen( nField ) CLASS TPQrow
METHOD FieldLen( nField ) CLASS TPQrow
    Local result

    if ISCHARACTER(nField)
        nField := ::Fieldpos(nField)
    endif

    if nField >= 1 .and. nField <= len(::aStruct)
        result := ::aStruct[nField, 3]
    end
RETURN result
tpostgre.prg1266
TPQROW:METHODFieldDec( nField ) CLASS TPQrow
METHOD FieldDec( nField ) CLASS TPQrow
    Local result

    if ISCHARACTER(nField)
        nField := ::Fieldpos(nField)
    endif

    if nField >= 1 .and. nField <= len(::aStruct)
        result := ::aStruct[nField, 4]
    end
RETURN result
tpostgre.prg1279
STATIC FUNCTIONDataToSql(xField)
Static Function DataToSql(xField)
        Local cType, result := 'NULL'

        cType := ValType(xField)

        if cType == "C" .or. cType == "M"
                result := "'"+ strtran(xField, "'", ' ') + "'"
        elseif cType == "D"
                result := dtos( xField)
        elseif cType == "N"
                result := str(xField)
        elseif cType == "L"
                result := iif( xField, "'t'", "'f'" )
        end
return result
tpostgre.prg1292
STATIC FUNCTIONValueToString(xField)
Static Function ValueToString(xField)
        Local cType, result := nil

        cType := ValType(xField)

        if cType == "D"
                result := dtos( xField )
        elseif cType == "N"
                result := str(xField)
        elseif cType == "L"
                result := iif( xField, "t", "f" )
        elseif cType == "C" .or. cType == "M"
                result := xField
        end
return result
tpostgre.prg1308

Page url: http://www.yourdomain.com/help/index.html?hbpgsql.htm