DBD-DB2

 view release on metacpan or  search on metacpan

dbdimp.c  view on Meta::CPAN

    	if( strncmp( imp_dbh->sqlerrp, "SQL", 3 ) == 0 )      /* UNO */ {
		/* Do nothing, use the default statement */
	}
    	else if( strncmp( imp_dbh->sqlerrp, "DSN", 3 ) == 0 ) /* MVS */	{
	  	pSQL = "select 1 from sysibm.sysdummy1";
    	}
    	else if( strncmp( imp_dbh->sqlerrp, "QSQ", 3 ) == 0 ) /* AS/400 */ {
	  	pSQL = "select 1 from qsys2.qsqptabl";
    	}
    	else if( strncmp( imp_dbh->sqlerrp, "ARI", 3 ) == 0 ) /* VM */ {
	  	pSQL = "select 1 from system.sysoptions";
    	}
    	else {
	  	/* Do nothing, use the default statement */
    	}
	
    	ret = SQLAllocHandle( SQL_HANDLE_STMT, imp_dbh->hdbc, &stmt );
	CHECK_ERROR(dbh, (SQLSMALLINT)(
	    			stmt == SQL_NULL_HSTMT ? SQL_HANDLE_DBC
				: SQL_HANDLE_STMT),
    			(SQLHANDLE)(
	    			stmt == SQL_NULL_HSTMT ? imp_dbh->hdbc
				: stmt), ret, "dbd_db_ping: Statement allocation error");
    	if( SQL_SUCCESS != ret )
	 	goto exit;
	
#ifndef AS400
    	ret = SQLSetStmtAttr( stmt,
			SQL_ATTR_DEFERRED_PREPARE,
			SQL_DEFERRED_PREPARE_OFF,
			0 );
	CHECK_ERROR(dbh, SQL_HANDLE_STMT, stmt, ret, "dbd_db_ping: Error turning off deferred prepare");
    	if( SQL_SUCCESS != ret )
	 	goto exit;
#endif
    	ret = SQLPrepare( stmt, (SQLCHAR*)pSQL, SQL_NTS );
	CHECK_ERROR(dbh, SQL_HANDLE_STMT, stmt, ret, "dbd_db_ping: Error preparing statement");
	
exit:
    	if( stmt )
	  	SQLFreeHandle( SQL_HANDLE_STMT, stmt );
	
    	/* If any error occured, check the state to determine if the cause */
    	/* is a broken connection.                                         */
    	if( SQL_SUCCESS != ret ) {
	  	STRLEN len;
	  	char *pState = SvPV( DBIc_STATE(imp_dbh), len );
		
	  	if( pState && len >= 5 &&
		      		( strncmp( pState, "08", 2 ) == 0 ||
		      		  strncmp( pState, "40003", 5 ) == 0 ) ) {
			/* ping should not throw an error when it detects a dead  */
			/* connection so reset error code and message but keep    */
			/* connection state                                       */
			sv_setsv( DBIc_ERRSTR(imp_dbh), &PL_sv_undef );
			sv_setsv( DBIc_ERR(imp_dbh), &PL_sv_undef );
			return FALSE; /* Connection is dead */
	  	}
    	}
	
    	return TRUE; /* Connection is still alive */
}

int dbd_db_commit( SV *dbh,
		imp_dbh_t *imp_dbh ) {

	SQLRETURN ret;
    	ret = SQLEndTran(SQL_HANDLE_DBC,imp_dbh->hdbc,SQL_COMMIT);
	CHECK_ERROR(dbh, SQL_HANDLE_DBC, imp_dbh->hdbc, ret, "Commit Failed");
    	EOI(ret);
    	return TRUE;
}

int dbd_db_rollback( SV *dbh,
		imp_dbh_t *imp_dbh ) {
    
	SQLRETURN ret;
    	ret = SQLEndTran(SQL_HANDLE_DBC,imp_dbh->hdbc,SQL_ROLLBACK);
	CHECK_ERROR(dbh, SQL_HANDLE_DBC, imp_dbh->hdbc, ret, "Rollback Failed")
    	EOI(ret);
    	return TRUE;
}

int dbd_db_disconnect( SV *dbh,
		imp_dbh_t *imp_dbh ) {
	D_imp_drh_from_dbh;

	SQLRETURN ret;
	ret = SQLDisconnect(imp_dbh->hdbc);
	CHECK_ERROR(dbh, SQL_HANDLE_DBC, imp_dbh->hdbc, ret, "Disconnect Failed");
    	EOI(ret);
	
    	/* Only turn off the ACTIVE attribute of the database handle        */
    	/* if SQLDisconnect() was successful.  If it wasn't successful,     */
    	/* we still have a connection!                                      */
	
    	DBIc_ACTIVE_off(imp_dbh);
	
    	ret = SQLFreeHandle( SQL_HANDLE_DBC, imp_dbh->hdbc );
	CHECK_ERROR(dbh, SQL_HANDLE_DBC, imp_dbh->hdbc, ret, "Free Connect Failed");

	EOI(ret);
	
    	imp_dbh->hdbc = SQL_NULL_HDBC;
    	imp_drh->connects--;
    	if (imp_drh->connects == 0) {
	  	if( NULL != imp_drh->svNUM_OF_FIELDS ) {
			SvREFCNT_dec( imp_drh->svNUM_OF_FIELDS );
			imp_drh->svNUM_OF_FIELDS = NULL;
	  	}
	  	ret = SQLFreeHandle( SQL_HANDLE_ENV, imp_drh->henv );
		CHECK_ERROR(dbh, SQL_HANDLE_ENV, imp_drh->henv, ret, "Free HENV Failed");
	  	EOI(ret);
      		imp_drh->henv = SQL_NULL_HENV;
    	}
	
    	/* We don't free imp_dbh since a reference still exists    */
    	/* The DESTROY method is the only one to 'free' memory.    */
    	/* Note that statement objects may still exist for this dbh!    */
    	return TRUE;
}



( run in 1.436 second using v1.01-cache-2.11-cpan-39bf76dae61 )