Example #1
0
static HB_ERRCODE ocilibExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem )
{
   OCI_Statement * st = OCI_StatementCreate( ( ( SDDCONN * ) pConnection->pSDDConn )->pConn );
   void *          hStatement;
   char *          szError;
   HB_ERRCODE      errCode;

   if( ! st )
   {
      szError = ocilibGetError( &errCode );
      hb_errRT_OCIDD( EG_OPEN, ESQLDD_STMTALLOC, szError, hb_itemGetCPtr( pItem ), errCode );
      hb_xfree( szError );
      return HB_FAILURE;
   }

   if( OCI_ExecuteStmt( st, M_HB_ITEMGETSTR( pItem, &hStatement, NULL ) ) )
   {
      hb_strfree( hStatement );

      /* TODO: new id */
      hb_rddsqlSetError( 0, NULL, hb_itemGetCPtr( pItem ), NULL, ( unsigned long ) OCI_GetAffectedRows( st ) );
      OCI_StatementFree( st );
      return HB_SUCCESS;
   }
   else
      hb_strfree( hStatement );

   szError = ocilibGetError( &errCode );
   hb_rddsqlSetError( errCode, szError, hb_itemGetCPtr( pItem ), NULL, 0 );
   hb_xfree( szError );
   OCI_StatementFree( st );
   return HB_FAILURE;
}
Example #2
0
static HB_ERRCODE pgsqlExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem )
{
   PGconn *       pConn = ( ( SDDCONN * ) pConnection->pSDDConn )->pConn;
   int            iTuples;
   PGresult *     pResult;
   ExecStatusType status;
   unsigned long  ulAffectedRows;

   pResult = PQexec( pConn, hb_itemGetCPtr( pItem ) );
   if( ! pResult )
   {
      hb_rddsqlSetError( 1, PQerrorMessage( pConn ), hb_itemGetCPtr( pItem ), NULL, 0 );
      return HB_FAILURE;
   }

   status = PQresultStatus( pResult );
   if( status != PGRES_TUPLES_OK && status != PGRES_COMMAND_OK )
   {
      hb_rddsqlSetError( status, PQresultErrorMessage( pResult ), hb_itemGetCPtr( pItem ), NULL, 0 );
      return HB_FAILURE;
   }

   iTuples = PQntuples( pResult );
   if( iTuples > 0 )
      ulAffectedRows = ( unsigned long ) iTuples;
   else
      ulAffectedRows = ( unsigned long ) atol( PQcmdTuples( pResult ) );

   hb_rddsqlSetError( 0, NULL, hb_itemGetCPtr( pItem ), NULL, ulAffectedRows );
   PQclear( pResult );
   return HB_SUCCESS;
}
Example #3
0
File: core.c Project: NaldoDj/core
static HB_ERRCODE odbcExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem )
{
   SDDCONN *         pSDDConn = ( SDDCONN * ) pConnection->pSDDConn;
   const O_HB_CHAR * pchStatement;
   void *            hStatement;
   HB_SIZE           nStatementLen;
   SQLHSTMT          hStmt;
   SQLLEN            iCount;
   char *            szError;
   HB_ERRCODE        errCode;
   SQLRETURN         result;

#if ODBCVER >= 0x0300
   if( ! SQL_SUCCEEDED( SQLAllocHandle( SQL_HANDLE_STMT, pSDDConn->hConn, &hStmt ) ) )
#else
   if( ! SQL_SUCCEEDED( SQLAllocStmt( pSDDConn->hConn, &hStmt ) ) )
#endif
   {
      szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, SQL_NULL_HSTMT, &errCode );
      hb_errRT_ODBCDD( EG_OPEN, ESQLDD_STMTALLOC, szError, hb_itemGetCPtr( pItem ), errCode );
      hb_xfree( szError );
      return HB_FAILURE;
   }

   pchStatement = O_HB_ITEMGETSTR( pItem, &hStatement, &nStatementLen );
   result = SQLExecDirect( hStmt,
                           ( SQLTCHAR * ) HB_UNCONST( pchStatement ),
                           ( SQLINTEGER ) nStatementLen );
   hb_strfree( hStatement );

   if( SQL_SUCCEEDED( result ) )
   {

      if( SQL_SUCCEEDED( SQLRowCount( hStmt, &iCount ) ) )
      {
         /* TODO: new id */
         hb_rddsqlSetError( 0, NULL, hb_itemGetCPtr( pItem ), NULL, ( unsigned long ) iCount );
#if ODBCVER >= 0x0300
         SQLFreeHandle( SQL_HANDLE_STMT, hStmt );
#else
         SQLFreeStmt( hStmt, SQL_DROP );
#endif
         return HB_SUCCESS;
      }
   }

   szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, hStmt, &errCode );
   hb_rddsqlSetError( errCode, szError, hb_itemGetCPtr( pItem ), NULL, errCode );
   hb_xfree( szError );
#if ODBCVER >= 0x0300
   SQLFreeHandle( SQL_HANDLE_STMT, hStmt );
#else
   SQLFreeStmt( hStmt, SQL_DROP );
#endif
   return HB_FAILURE;
}
Example #4
0
static HB_ERRCODE sqlite3Execute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem )
{
   sqlite3 *  pDb = ( ( SDDCONN * ) pConnection->pSDDConn )->pDb;
   HB_ERRCODE errCode;
   int        iRow, iCol;
   void *     hStatement;
   char **    pResult   = NULL;
   char *     pszErrMsg = NULL;

   if( sqlite3_get_table( pDb, S_HB_ITEMGETSTR( pItem, &hStatement, NULL ), &pResult, &iRow, &iCol, &pszErrMsg ) != SQLITE_OK )
   {
      hb_strfree( hStatement );
      hb_xfree( sqlite3GetError( pDb, &errCode ) );
      hb_errRT_SQLT3DD( EG_OPEN, ESQLDD_STMTALLOC, pszErrMsg, hb_itemGetCPtr( pItem ), errCode );
      hb_xfree( pszErrMsg );
      return HB_FAILURE;
   }
   else
      hb_strfree( hStatement );

   sqlite3_free_table( pResult );

   /* TODO: new id */
   hb_rddsqlSetError( 0, NULL, hb_itemGetCPtr( pItem ), NULL, ( unsigned long ) iRow );
   return HB_SUCCESS;
}
Example #5
0
static HB_ERRCODE mysqlExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem )
{
   MYSQL *     pMySql = ( ( SDDCONN * ) pConnection->pSDDConn )->pMySql;
   MYSQL_RES * pResult;
   HB_ULONG    ulAffectedRows;
   PHB_ITEM    pNewID = NULL;

   if( mysql_real_query( pMySql, hb_itemGetCPtr( pItem ), ( unsigned long ) hb_itemGetCLen( pItem ) ) )
   {
      hb_rddsqlSetError( mysql_errno( pMySql ), mysql_error( pMySql ), hb_itemGetCPtr( pItem ), NULL, 0 );
      return HB_FAILURE;
   }

   pResult = mysql_store_result( pMySql );
   if( pResult )
   {
      ulAffectedRows = ( HB_ULONG ) mysql_num_rows( pResult );
      mysql_free_result( pResult );
      hb_rddsqlSetError( 0, NULL, hb_itemGetCPtr( pItem ), NULL, ulAffectedRows );
   }
   else
   {
      if( mysql_field_count( pMySql ) == 0 )
      {
         ulAffectedRows = ( HB_ULONG ) mysql_affected_rows( pMySql );
         if( mysql_insert_id( pMySql ) != 0 )
         {
            pNewID = hb_itemPutNInt( NULL, mysql_insert_id( pMySql ) );
         }
         hb_rddsqlSetError( 0, NULL, hb_itemGetCPtr( pItem ), pNewID, ulAffectedRows );
         if( pNewID )
            hb_itemRelease( pNewID );
      }
      else /* error */
      {
         hb_rddsqlSetError( mysql_errno( pMySql ), mysql_error( pMySql ), hb_itemGetCPtr( pItem ), NULL, 0 );
         return HB_FAILURE;
      }
   }
   return HB_SUCCESS;
}
Example #6
0
/* --- SDD METHODS --- */
static HB_ERRCODE mysqlConnect( SQLDDCONNECTION * pConnection, PHB_ITEM pItem )
{
   MYSQL *  pMySql;
   PHB_ITEM pItemUnixSocket = hb_arrayGetItemPtr( pItem, 7 );

   pMySql = mysql_init( NULL );
   if( ! mysql_real_connect( pMySql,
                             hb_arrayGetCPtr( pItem, 2 ) /* host */,
                             hb_arrayGetCPtr( pItem, 3 ) /* user */,
                             hb_arrayGetCPtr( pItem, 4 ) /* password */,
                             hb_arrayGetCPtr( pItem, 5 ) /* db */,
                             hb_arrayGetNI( pItem, 6 ) /* port */,
                             pItemUnixSocket && HB_IS_STRING( pItemUnixSocket ) ? hb_itemGetCPtr( pItemUnixSocket ) : NULL,
                             hb_arrayGetNI( pItem, 8 ) /* flags*/ ) )
   {
      hb_rddsqlSetError( mysql_errno( pMySql ), mysql_error( pMySql ), NULL, NULL, 0 );
      mysql_close( pMySql );
      return HB_FAILURE;
   }
   pConnection->pSDDConn = hb_xgrab( sizeof( SDDCONN ) );
   ( ( SDDCONN * ) pConnection->pSDDConn )->pMySql = pMySql;
   return HB_SUCCESS;
}
Example #7
0
static HB_ERRCODE sqlbaseRddInfo( LPRDDNODE pRDD, HB_USHORT uiIndex, HB_ULONG ulConnect, PHB_ITEM pItem )
{
   HB_ULONG ulConn;
   SQLDDCONNECTION * pConn;

   HB_SYMBOL_UNUSED( pRDD );

   ulConn = ulConnect ? ulConnect : s_ulConnectionCurrent;
   if( ulConn > 0 && ulConn <= s_ulConnectionCount )
      pConn = s_pConnection[ ulConn - 1 ];
   else
      pConn = NULL;

   switch( uiIndex )
   {
      case RDDI_REMOTE:
         hb_itemPutL( pItem, HB_TRUE );
         break;

      case RDDI_CONNECTION:
      {
         HB_ULONG ulNewConnection = 0;

         if( hb_itemType( pItem ) & HB_IT_NUMERIC )
            ulNewConnection = hb_itemGetNL( pItem );

         hb_itemPutNL( pItem, ulConnect ? ulConnect : s_ulConnectionCurrent );

         if( ulNewConnection )
            s_ulConnectionCurrent = ulNewConnection;
         break;
      }

      case RDDI_ISDBF:
         hb_itemPutL( pItem, HB_FALSE );
         break;

      case RDDI_CANPUTREC:
         hb_itemPutL( pItem, HB_TRUE );
         break;

      case RDDI_CONNECT:
      {
         PSDDNODE     pNode = NULL;
         HB_ULONG     ul;
         const char * pStr;

         pStr = hb_arrayGetCPtr( pItem, 1 );
         if( pStr )
         {
            pNode = s_pSdd;
            while( pNode )
            {
               if( ! hb_stricmp( pNode->Name, pStr ) )
                  break;
               pNode = pNode->pNext;
            }
         }

         hb_rddsqlSetError( 0, NULL, NULL, NULL, 0 );
         pConn = ( SQLDDCONNECTION * ) hb_xgrab( sizeof( SQLDDCONNECTION ) );
         memset( pConn, 0, sizeof( SQLDDCONNECTION ) );
         if( pNode && pNode->Connect( pConn, pItem ) == HB_SUCCESS )
         {
            pConn->pSDD = pNode;

            /* Find free connection handle */
            for( ul = 0; ul < s_ulConnectionCount; ul++ )
            {
               if( ! s_pConnection[ ul ] )
                  break;
            }
            if( ul >= s_ulConnectionCount )
            {
               /* Realloc connection table */
               if( s_pConnection )
                  s_pConnection = ( SQLDDCONNECTION ** ) hb_xrealloc( s_pConnection, sizeof( SQLDDCONNECTION * ) * ( s_ulConnectionCount + CONNECTION_LIST_EXPAND ) );
               else
                  s_pConnection = ( SQLDDCONNECTION ** ) hb_xgrab( sizeof( SQLDDCONNECTION * ) * CONNECTION_LIST_EXPAND );

               memset( s_pConnection + s_ulConnectionCount, 0, sizeof( SQLDDCONNECTION * ) * CONNECTION_LIST_EXPAND );
               ul = s_ulConnectionCount;
               s_ulConnectionCount += CONNECTION_LIST_EXPAND;
            }
            s_pConnection[ ul ] = pConn;
            ul++;
            s_ulConnectionCurrent = ul;
         }
         else
         {
            hb_xfree( pConn );
            ul = 0;
         }

         hb_itemPutNI( pItem, ul );
         break;
      }

      case RDDI_DISCONNECT:
         hb_rddsqlSetError( 0, NULL, NULL, NULL, 0 );
         if( pConn && ! pConn->uiAreaCount && pConn->pSDD->Disconnect( pConn ) == HB_SUCCESS )
         {
            hb_xfree( pConn );
            s_pConnection[ ulConn - 1 ] = NULL;
            if( s_ulConnectionCurrent == ulConn )
               s_ulConnectionCurrent = 0;

            hb_itemPutL( pItem, HB_TRUE );
            return HB_SUCCESS;
         }
         hb_itemPutL( pItem, HB_FALSE );
         return HB_SUCCESS;

      case RDDI_EXECUTE:
         hb_rddsqlSetError( 0, NULL, NULL, NULL, 0 );
         if( pConn )
            hb_itemPutL( pItem, pConn->pSDD->Execute( pConn, pItem ) == HB_SUCCESS );
         else
            hb_itemPutL( pItem, HB_FALSE );

         return HB_SUCCESS;

      case RDDI_ERROR:
         hb_itemPutC( pItem, s_szError );
         return HB_SUCCESS;

      case RDDI_ERRORNO:
         hb_itemPutNI( pItem, s_errCode );
         return HB_SUCCESS;

      case RDDI_QUERY:
         hb_itemPutC( pItem, s_szQuery );
         return HB_SUCCESS;

      case RDDI_NEWID:
         hb_itemCopy( pItem, s_pItemNewID );
         return HB_SUCCESS;

      case RDDI_AFFECTEDROWS:
         hb_itemPutNInt( pItem, s_ulAffectedRows );
         return HB_SUCCESS;

#if 0
      default:
         return SUPER_RDDINFO( pRDD, uiIndex, ulConnect, pItem );
#endif

   }

   return HB_SUCCESS;
}
Example #8
0
File: core.c Project: NaldoDj/core
/* --- SDD METHODS --- */
static HB_ERRCODE odbcConnect( SQLDDCONNECTION * pConnection, PHB_ITEM pItem )
{
   SQLHENV    hEnv     = NULL;
   SQLHDBC    hConnect = NULL;
   char *     szError;
   HB_ERRCODE errCode;

#if ODBCVER >= 0x0300
   if( SQL_SUCCEEDED( SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv ) ) )
#else
   if( SQL_SUCCEEDED( SQLAllocEnv( &hEnv ) ) )
#endif
   {
#if ODBCVER >= 0x0300
      SQLSetEnvAttr( hEnv, SQL_ATTR_ODBC_VERSION, ( SQLPOINTER ) SQL_OV_ODBC3, SQL_IS_UINTEGER );
#endif

#if ODBCVER >= 0x0300
      if( SQL_SUCCEEDED( SQLAllocHandle( SQL_HANDLE_DBC, hEnv, &hConnect ) ) )
#else
      if( SQL_SUCCEEDED( SQLAllocConnect( hEnv, &hConnect ) ) )
#endif
      {
         const O_HB_CHAR * pchConStr;
         void *            hConnStr;
         HB_SIZE           nConnLen;
         SQLTCHAR          cBuffer[ 1024 ];
         SQLSMALLINT       iLen = HB_SIZEOFARRAY( cBuffer );

         cBuffer[ 0 ] = '\0';
         pchConStr = O_HB_ARRAYGETSTR( pItem, 2, &hConnStr, &nConnLen );

         if( SQL_SUCCEEDED( SQLDriverConnect( hConnect,
                                              NULL,
                                              ( SQLTCHAR * ) HB_UNCONST( pchConStr ),
                                              ( SQLSMALLINT ) nConnLen,
                                              cBuffer,
                                              HB_SIZEOFARRAY( cBuffer ),
                                              &iLen,
                                              SQL_DRIVER_NOPROMPT ) ) )
         {
            hb_strfree( hConnStr );
            pConnection->pSDDConn = hb_xgrab( sizeof( SDDCONN ) );
            ( ( SDDCONN * ) pConnection->pSDDConn )->hConn = hConnect;
            ( ( SDDCONN * ) pConnection->pSDDConn )->hEnv  = hEnv;
            return HB_SUCCESS;
         }
         else
         {
            hb_strfree( hConnStr );
            szError = odbcGetError( hEnv, hConnect, SQL_NULL_HSTMT, &errCode );
            hb_rddsqlSetError( errCode, szError, NULL, NULL, 0 );
            hb_xfree( szError );
         }
#if ODBCVER >= 0x0300
         SQLFreeHandle( SQL_HANDLE_DBC, hConnect );
#else
         SQLFreeConnect( hConnect );
#endif
      }
      else
      {
         szError = odbcGetError( hEnv, SQL_NULL_HDBC, SQL_NULL_HSTMT, &errCode );
         hb_errRT_ODBCDD( EG_OPEN, ESQLDD_CONNALLOC, szError, hb_arrayGetCPtr( pItem, 2 ), errCode );
         hb_xfree( szError );
      }
#if ODBCVER >= 0x0300
      SQLFreeHandle( SQL_HANDLE_ENV, hEnv );
#else
      SQLFreeEnv( hEnv );
#endif
   }
   else
   {
      szError = odbcGetError( SQL_NULL_HENV, SQL_NULL_HDBC, SQL_NULL_HSTMT, &errCode );
      hb_errRT_ODBCDD( EG_OPEN, ESQLDD_ENVALLOC, szError, hb_arrayGetCPtr( pItem, 2 ), errCode );
      hb_xfree( szError );
   }
   return HB_FAILURE;
}