OXID Community Forum

> International Forums > Feature requests > Make the oxdb-class usable for accessing external databases
Login
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Thread: Make the oxdb-class usable for accessing external databases


Reply
 
Thread Tools Search this Thread Display Modes
#Top   #1  
Old 01-18-2010, 02:07 PM
avenger's Avatar
avenger avenger is offline
Senior Member
Join Date: May 2009
Posts: 2,083
avenger is on a distinguished road
Default Make the oxdb-class usable for accessing external databases

Sometimes it is necessary to access external databases (e.g. from a Worpress-blog).

It would be desirable to be able to utilize the oxdb class for that purpose.

Currently, however, the "getDb"-function in "core/oxdb.php" is hardwired to access only the database, whose parameters are defined in the config file.

Solution:

Replace "public static function getDb" in "core/oxdb.php" with:

Code:
    public static function getDb( $blAssoc = false, $dbHost=null, $dbUser=null, $dbPwd=null, $dbName=null)
    {
        if ( defined( 'OXID_PHP_UNIT' ) ) {
            if ( isset( modDB::$unitMOD ) && is_object( modDB::$unitMOD ) ) {
                return modDB::$unitMOD;
            }
        }

        global  $ADODB_FETCH_MODE;

        if ( $blAssoc )
            $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
        else
            $ADODB_FETCH_MODE = ADODB_FETCH_NUM;

        if ( self::$_oDB !== null ) {
            return self::$_oDB;
        }

        global  $ADODB_CACHE_DIR;
        global  $ADODB_DRIVER,
                $ADODB_SESSION_TBL,
                $ADODB_SESSION_CONNECT,
                $ADODB_SESSION_DRIVER,
                $ADODB_SESSION_USER,
                $ADODB_SESSION_PWD,
                $ADODB_SESSION_DB,
                $ADODB_SESS_LIFE,
                $ADODB_SESS_DEBUG;

        //adding exception handler for SQL errors
        $myConfig = self::getInstance()->getConfig();
        $iDebug = $myConfig->getConfigParam( 'iDebug' );
        if ( $iDebug ) {
            require_once getShopBasePath() . 'core/adodblite/adodb-exceptions.inc.php';
        }

        // session related parameters. don't change.

        //Tomas
        //the default setting is 3000 * 60, but actually changing this will give no effect as now redefinition of this constant
        //appears after OXID custom settings are loaded and $ADODB_SESS_LIFE depends on user settings.
        //You can find the redefinition of ADODB_SESS_LIFE @ oxconfig.php:: line ~ 390.
        $ADODB_SESS_LIFE       = 3000 * 60;
        $ADODB_SESSION_TBL     = "oxsessions";
        $ADODB_SESSION_DRIVER  = $ADODB_DRIVER;
        $ADODB_SESS_DEBUG      = false;
        $ADODB_CACHE_DIR       = $myConfig->getConfigParam( 'sCompileDir' );
        if ($dbHost)
        {
          $ADODB_SESSION_USER    = $dbUser;
          $ADODB_SESSION_PWD     = $dbPwd;
          $ADODB_SESSION_DB      = $dbName;
          $ADODB_SESSION_CONNECT = $dbHost;
        }
        else
        {
          $ADODB_SESSION_USER    = $myConfig->getConfigParam( 'dbUser' );
          $ADODB_SESSION_PWD     = $myConfig->getConfigParam( 'dbPwd' );
          $ADODB_SESSION_DB      = $myConfig->getConfigParam( 'dbName' );
          $ADODB_SESSION_CONNECT = $myConfig->getConfigParam( 'dbHost' );
        }
        $sModules = '';
        if (  $iDebug == 2 || $iDebug == 3 || $iDebug == 4 || $iDebug == 7  ) {
            $sModules = 'perfmon';
        }

        // log admin changes ?
        if ( $myConfig->isAdmin() && $myConfig->getConfigParam( 'blLogChangesInAdmin' ) ) {
            $sModules = ( $sModules ? ':' : '' ) . 'oxadminlog';
        }

        self::$_oDB = ADONewConnection( $myConfig->getConfigParam( 'dbType' ), $sModules );

        $sVerPrefix = '';
        $sVerPrefix = '_ce';
        if ( !self::$_oDB->connect( $ADODB_SESSION_CONNECT, $ADODB_SESSION_USER, $ADODB_SESSION_PWD, $ADODB_SESSION_DB ) ) {
            $sConfig = join( '', file( getShopBasePath().'config.inc.php' ) );
            if ( strpos( $sConfig, '<dbHost'.$sVerPrefix.'>' ) !== false &&
                 strpos( $sConfig, '<dbName'.$sVerPrefix.'>' ) !== false ) {
                header( 'location:setup/index.php' ); // pop to setup as there is something wrong
                exit();
            } else {

                // notifying shop owner about connection problems
                $sFailedShop = isset( $_REQUEST['shp'] )?addslashes( $_REQUEST['shp'] ):'Base shop';

                $sDate = date( 'l dS of F Y h:i:s A');
                $sScript  = $_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING'];
                $sReferer = $_SERVER['HTTP_REFERER'];

                //sending a message to admin
                $sWarningSubject = 'Offline warning!';
                $sWarningBody = "
                Database error in OXID eShop:
                Date: $sDate
                Shop: $sFailedShop

                mysql error: ".self::$_oDB->errorMsg()."
                mysql error no: ".self::$_oDB->errorNo()."

                Script: $sScript
                Referer: $sReferer";

                if ( ( $sAdminEmail = $myConfig->getConfigParam( 'sAdminEmail' ) ) ) {
                    include 'core/phpmailer/class.phpmailer.php';

                    $oMailer = new phpmailer();
                    $oMailer->isMail();
                    $oMailer->From = $sAdminEmail;
                    $oMailer->AddAddress( $sAdminEmail );
                    $oMailer->Subject = $sWarningSubject;
                    $oMailer->Body = $sWarningBody;
                    $oMailer->send();
                }

                //only exception to default construction method
                $oEx = new oxConnectionException();
                $oEx->setMessage( 'EXCEPTION_CONNECTION_NODB' );
                $oEx->setConnectionError( $myConfig->getConfigParam( 'dbUser' ).'s'.getShopBasePath().self::$_oDB->errorMsg() );
                throw $oEx;
            }
        }

        if (  $iDebug == 2 || $iDebug == 3 || $iDebug == 4  || $iDebug == 7 ) {
            try {
                self::$_oDB->execute('truncate table adodb_logsql;');
            } catch (ADODB_Exception $e) {
                // nothing
            }
            self::$_oDB->logSQL( true );
        }

        self::$_oDB->cacheSecs = 60 * 10; // 10 minute caching
        self::$_oDB->execute( 'SET @@session.sql_mode = ""' );

        if ( $myConfig->isUtf() ) {
            self::$_oDB->execute( 'SET NAMES "utf8"' );
            self::$_oDB->execute( 'SET CHARACTER SET utf8' );
            self::$_oDB->execute( 'SET CHARACTER_SET_CONNECTION = utf8' );
            self::$_oDB->execute( 'SET CHARACTER_SET_DATABASE = utf8' );
            self::$_oDB->execute( 'SET character_set_results = utf8' );
            self::$_oDB->execute( 'SET character_set_server = utf8' );
        }

        return self::$_oDB;
    }
This optionally allows to define a different database for access.
__________________


Individuelle und hochwertige OXID 4 Templates und Shop-Erweiterungen gibt es bei http://www.powertemplate.de
  1. Dem Ingeniör ist nichts zu schwör (Daniel Düsentrieb, 1952)
  2. Geht nicht, gibt's nicht.
  3. Was nicht passt, wird passend gemacht
Weitere Informationen zum powertemplate.de Angebotsspektrum unter http://www.shopplaner.de
Reply With Quote
#Top   #2  
Old 07-21-2014, 12:29 PM
R.Nitzer R.Nitzer is offline
Senior Member
Join Date: Jul 2013
Posts: 130
R.Nitzer is on a distinguished road
Default Re: Make the oxdb-class usable for accessing external databases

Hi,

is there meanwhile the possibility to access external databases without editing oxdb?

Because http://wiki.oxidforge.org/Tutorials/...adable_classes tells that oxdb is not overridable.

p.s. nice work avenger
Reply With Quote
Reply

Bookmarks

Tags
accessing, databases, external, make, oxdbclass, usable

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Nicht Sichtbar
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make a theme? monteiro Templates and Design 3 01-10-2013 06:24 PM
How to make an update buxfix Newbies 2 04-20-2010 01:09 PM
How to make a template-changer rubbercut Templates and Design 0 04-02-2010 11:09 AM
Can i extend two Modules with the same class aranex Modules 1 02-10-2010 02:14 PM
Make as sophisticated as OSCMax jamesanderson Feature requests 0 03-14-2009 01:26 PM

All times are GMT +1. The time now is 07:32 PM.