コード例 #1
2
 function connect($server, $db, $user, $password, $socketPath, $charset = null, $port = false)
 {
     $connection = false;
     if ($socketPath !== false) {
         ini_set("mysqli.default_socket", $socketPath);
     }
     if ($this->UsePersistentConnection == true) {
         // Only supported on PHP 5.3 (mysqlnd)
         if (version_compare(PHP_VERSION, '5.3') > 0) {
             $this->Server = 'p:' . $this->Server;
         } else {
             eZDebug::writeWarning('mysqli only supports persistent connections when using php 5.3 and higher', 'eZMySQLiDB::connect');
         }
     }
     eZPerfLogger::accumulatorStart('mysqli_connection', 'mysqli_total', 'Database connection');
     $connection = mysqli_connect($server, $user, $password, null, (int) $port, $socketPath);
     $dbErrorText = mysqli_connect_error();
     eZPerfLogger::accumulatorStop('mysqli_connection');
     $maxAttempts = $this->connectRetryCount();
     $waitTime = $this->connectRetryWaitTime();
     $numAttempts = 1;
     while (!$connection && $numAttempts <= $maxAttempts) {
         sleep($waitTime);
         eZPerfLogger::accumulatorStart('mysqli_connection', 'mysqli_total', 'Database connection');
         $connection = mysqli_connect($this->Server, $this->User, $this->Password, null, (int) $this->Port, $this->SocketPath);
         eZPerfLogger::accumulatorStop('mysqli_connection');
         $numAttempts++;
     }
     $this->setError();
     $this->IsConnected = true;
     if (!$connection) {
         eZDebug::writeError("Connection error: Couldn't connect to database. Please try again later or inform the system administrator.\n{$dbErrorText}", __CLASS__);
         $this->IsConnected = false;
         throw new eZDBNoConnectionException($server);
     }
     if ($this->IsConnected && $db != null) {
         eZPerfLogger::accumulatorStart('mysqli_connection', 'mysqli_total', 'Database connection');
         $ret = mysqli_select_db($connection, $db);
         eZPerfLogger::accumulatorStop('mysqli_connection');
         if (!$ret) {
             //$this->setError();
             eZDebug::writeError("Connection error: " . mysqli_errno($connection) . ": " . mysqli_error($connection), "eZMySQLiDB");
             $this->IsConnected = false;
         }
     }
     if ($charset !== null) {
         $originalCharset = $charset;
         $charset = eZCharsetInfo::realCharsetCode($charset);
     }
     if ($this->IsConnected and $charset !== null) {
         eZPerfLogger::accumulatorStart('mysqli_connection', 'mysqli_total', 'Database connection');
         $status = mysqli_set_charset($connection, eZMySQLCharset::mapTo($charset));
         eZPerfLogger::accumulatorStop('mysqli_connection');
         if (!$status) {
             $this->setError();
             eZDebug::writeWarning("Connection warning: " . mysqli_errno($connection) . ": " . mysqli_error($connection), "eZMySQLiDB");
         }
     }
     return $connection;
 }
コード例 #2
0
 /**
  * Reimplement parent's method to make use of a tracing dfs backend class
  */
 public function _connect()
 {
     $siteINI = eZINI::instance('site.ini');
     // DB Connection setup
     // This part is not actually required since _connect will only be called
     // once, but it is useful to run the unit tests. So be it.
     // @todo refactor this using eZINI::setVariable in unit tests
     if (parent::$dbparams === null) {
         $fileINI = eZINI::instance('file.ini');
         parent::$dbparams = array();
         parent::$dbparams['host'] = $fileINI->variable('eZDFSClusteringSettings', 'DBHost');
         $dbPort = $fileINI->variable('eZDFSClusteringSettings', 'DBPort');
         parent::$dbparams['port'] = $dbPort !== '' ? $dbPort : null;
         parent::$dbparams['socket'] = $fileINI->variable('eZDFSClusteringSettings', 'DBSocket');
         parent::$dbparams['dbname'] = $fileINI->variable('eZDFSClusteringSettings', 'DBName');
         parent::$dbparams['user'] = $fileINI->variable('eZDFSClusteringSettings', 'DBUser');
         parent::$dbparams['pass'] = $fileINI->variable('eZDFSClusteringSettings', 'DBPassword');
         parent::$dbparams['max_connect_tries'] = $fileINI->variable('eZDFSClusteringSettings', 'DBConnectRetries');
         parent::$dbparams['max_execute_tries'] = $fileINI->variable('eZDFSClusteringSettings', 'DBExecuteRetries');
         parent::$dbparams['sql_output'] = $siteINI->variable("DatabaseSettings", "SQLOutput") == "enabled";
         parent::$dbparams['cache_generation_timeout'] = $siteINI->variable("ContentSettings", "CacheGenerationTimeout");
     }
     $serverString = parent::$dbparams['host'];
     if (parent::$dbparams['socket']) {
         $serverString .= ':' . parent::$dbparams['socket'];
     } elseif (parent::$dbparams['port']) {
         $serverString .= ':' . parent::$dbparams['port'];
     }
     $maxTries = parent::$dbparams['max_connect_tries'];
     $tries = 0;
     eZPerfLogger::accumulatorStart('mysql_cluster_connect', 'MySQL Cluster', 'Cluster database connection');
     while ($tries < $maxTries) {
         if ($this->db = mysqli_connect(parent::$dbparams['host'], parent::$dbparams['user'], parent::$dbparams['pass'], parent::$dbparams['dbname'], parent::$dbparams['port'])) {
             break;
         }
         ++$tries;
     }
     eZPerfLogger::accumulatorStop('mysql_cluster_connect');
     if (!$this->db) {
         throw new eZClusterHandlerDBNoConnectionException($serverString, parent::$dbparams['user'], parent::$dbparams['pass']);
     }
     /*if ( !mysql_select_db( parent::$dbparams['dbname'], $this->db ) )
       throw new eZClusterHandlerDBNoDatabaseException( parent::$dbparams['dbname'] );*/
     // DFS setup
     if ($this->dfsbackend === null) {
         $this->dfsbackend = new eZDFSFileHandlerTracing46DFSBackend();
     }
     $charset = trim($siteINI->variable('DatabaseSettings', 'Charset'));
     if ($charset === '') {
         $charset = eZTextCodec::internalCharset();
     }
     if ($charset) {
         if (!mysqli_set_charset($this->db, eZMySQLCharset::mapTo($charset))) {
             $this->_fail("Failed to set Database charset to {$charset}.");
         }
     }
 }
コード例 #3
0
ファイル: mysqli.php プロジェクト: legende91/ez
 function _connect()
 {
     $siteINI = eZINI::instance('site.ini');
     if (!isset($GLOBALS['eZDBFileHandlerMysqlBackend_dbparams'])) {
         $fileINI = eZINI::instance('file.ini');
         $params['host'] = $fileINI->variable('ClusteringSettings', 'DBHost');
         $params['port'] = $fileINI->variable('ClusteringSettings', 'DBPort');
         $params['socket'] = $fileINI->variable('ClusteringSettings', 'DBSocket');
         $params['dbname'] = $fileINI->variable('ClusteringSettings', 'DBName');
         $params['user'] = $fileINI->variable('ClusteringSettings', 'DBUser');
         $params['pass'] = $fileINI->variable('ClusteringSettings', 'DBPassword');
         $params['chunk_size'] = $fileINI->variable('ClusteringSettings', 'DBChunkSize');
         $params['max_connect_tries'] = $fileINI->variable('ClusteringSettings', 'DBConnectRetries');
         $params['max_execute_tries'] = $fileINI->variable('ClusteringSettings', 'DBExecuteRetries');
         $params['sql_output'] = $siteINI->variable("DatabaseSettings", "SQLOutput") == "enabled";
         $params['cache_generation_timeout'] = $siteINI->variable("ContentSettings", "CacheGenerationTimeout");
         $GLOBALS['eZDBFileHandlerMysqlBackend_dbparams'] = $params;
     } else {
         $params = $GLOBALS['eZDBFileHandlerMysqlBackend_dbparams'];
     }
     $this->dbparams = $params;
     $maxTries = $params['max_connect_tries'];
     $tries = 0;
     eZDebug::accumulatorStart('mysql_cluster_connect', 'mysql_cluster_total', 'Cluster_database_connection');
     while ($tries < $maxTries) {
         /// @todo what if port is null, '' ??? to be tested
         if ($this->db = mysqli_connect($params['host'], $params['user'], $params['pass'], $params['dbname'], $params['port'])) {
             break;
         }
         ++$tries;
     }
     eZDebug::accumulatorStop('mysql_cluster_connect');
     if (!$this->db) {
         return $this->_die("Unable to connect to storage server");
     }
     $charset = trim($siteINI->variable('DatabaseSettings', 'Charset'));
     if ($charset === '') {
         $charset = eZTextCodec::internalCharset();
     }
     if ($charset) {
         if (!mysqli_set_charset($this->db, eZMySQLCharset::mapTo($charset))) {
             return $this->_die("Failed to set Database charset to {$charset}.");
         }
     }
 }
コード例 #4
0
ファイル: mysql.php プロジェクト: rmiguel/ezpublish
 function _connect($newLink = false)
 {
     $siteINI = eZINI::instance('site.ini');
     if (!isset($GLOBALS['eZDBFileHandlerMysqlBackend_dbparams'])) {
         $fileINI = eZINI::instance('file.ini');
         $params['host'] = $fileINI->variable('ClusteringSettings', 'DBHost');
         $params['port'] = $fileINI->variable('ClusteringSettings', 'DBPort');
         $params['socket'] = $fileINI->variable('ClusteringSettings', 'DBSocket');
         $params['dbname'] = $fileINI->variable('ClusteringSettings', 'DBName');
         $params['user'] = $fileINI->variable('ClusteringSettings', 'DBUser');
         $params['pass'] = $fileINI->variable('ClusteringSettings', 'DBPassword');
         $params['chunk_size'] = $fileINI->variable('ClusteringSettings', 'DBChunkSize');
         $params['max_connect_tries'] = $fileINI->variable('ClusteringSettings', 'DBConnectRetries');
         $params['max_execute_tries'] = $fileINI->variable('ClusteringSettings', 'DBExecuteRetries');
         $params['sql_output'] = $siteINI->variable("DatabaseSettings", "SQLOutput") == "enabled";
         $params['cache_generation_timeout'] = $siteINI->variable("ContentSettings", "CacheGenerationTimeout");
         $GLOBALS['eZDBFileHandlerMysqlBackend_dbparams'] = $params;
     } else {
         $params = $GLOBALS['eZDBFileHandlerMysqlBackend_dbparams'];
     }
     $this->dbparams = $params;
     $serverString = $params['host'];
     if ($params['socket']) {
         $serverString .= ':' . $params['socket'];
     } elseif ($params['port']) {
         $serverString .= ':' . $params['port'];
     }
     $maxTries = $params['max_connect_tries'];
     $tries = 0;
     while ($tries < $maxTries) {
         if ($this->db = mysql_connect($serverString, $params['user'], $params['pass'], $newLink)) {
             break;
         }
         ++$tries;
     }
     if (!$this->db) {
         return $this->_die("Unable to connect to storage server");
     }
     if (!mysql_select_db($params['dbname'], $this->db)) {
         return $this->_die("Unable to select database {$params['dbname']}");
     }
     $charset = trim($siteINI->variable('DatabaseSettings', 'Charset'));
     if ($charset === '') {
         $charset = eZTextCodec::internalCharset();
     }
     if ($charset) {
         if (!mysql_query("SET NAMES '" . eZMySQLCharset::mapTo($charset) . "'", $this->db)) {
             return $this->_die("Failed to set Database charset to {$charset}.");
         }
     }
 }
コード例 #5
0
ファイル: ezmysqldb.php プロジェクト: nottavi/ezpublish
    function connect( $server, $db, $user, $password, $socketPath, $charset = null, $port = false )
    {
        // if a port is specified, we add it to $server, this is how mysql_(p)connect accepts a port number
        if ( $port )
        {
            $server .= ':' . $port;
        }

        $connection = false;

        if ( $socketPath !== false )
        {
            ini_set( "mysql.default_socket", $socketPath );
        }

        $oldHandling = eZDebug::setHandleType( eZDebug::HANDLE_EXCEPTION );
        try {
            if ( $this->UsePersistentConnection == true )
            {
                $connection = mysql_pconnect( $server, $user, $password );
            }
            else
            {
                $connection = mysql_connect( $server, $user, $password, true );
            }
        } catch( ErrorException $e ) {}
        eZDebug::setHandleType( $oldHandling );

        $this->setError();
        $maxAttempts = $this->connectRetryCount();
        $waitTime = $this->connectRetryWaitTime();
        $numAttempts = 1;
        while ( !$connection && $numAttempts <= $maxAttempts )
        {
            sleep( $waitTime );
            $oldHandling = eZDebug::setHandleType( eZDebug::HANDLE_EXCEPTION );
            eZDebug::accumulatorStart( 'mysql_connection', 'mysql_total', 'Database connection' );
            try {
                if ( $this->UsePersistentConnection == true )
                {
                    $connection = mysql_pconnect( $this->Server, $this->User, $this->Password );
                }
                else
                {
                    $connection = mysql_connect( $this->Server, $this->User, $this->Password );
                }
            } catch( ErrorException $e ) {}
            eZDebug::accumulatorStop( 'mysql_connection' );
            eZDebug::setHandleType( $oldHandling );
            $numAttempts++;
        }
        $this->setError();

        $this->IsConnected = true;

        if ( !$connection )
        {
            eZDebug::writeError( "Connection error: Couldn't connect to database server. Please try again later or inform the system administrator.\n{$this->ErrorMessage}", __CLASS__ );
            $this->IsConnected = false;
            throw new eZDBNoConnectionException( $server, $this->ErrorMessage, $this->ErrorNumber );
        }

        if ( $this->IsConnected && $db != null )
        {
            $ret = mysql_select_db( $db, $connection );
            if ( !$ret )
            {
                $this->setError( $connection );
                eZDebug::writeError( "Connection error: Couldn't select the database. Please try again later or inform the system administrator.\n{$this->ErrorMessage}", __CLASS__ );
                $this->IsConnected = false;
            }
        }

        if ( $charset !== null )
        {
            $charset = eZCharsetInfo::realCharsetCode( $charset );
        }

        if ( $this->IsConnected and $charset !== null and $this->isCharsetSupported( $charset ) )
        {
            $query = "SET NAMES '" . eZMySQLCharset::mapTo( $charset ) . "'";
            $status = mysql_query( $query, $connection );
            $this->reportQuery( 'eZMySQLDB', $query, false, false, true );
            if ( !$status )
            {
                $this->setError();
                eZDebug::writeWarning( "Connection warning: " . mysql_errno( $connection ) . ": " . mysql_error( $connection ), "eZMySQLDB" );
            }
        }

        return $connection;
    }
コード例 #6
0
 public function testUnknownUppercaseCharsetMapTo()
 {
     $this->assertSame( "uNknOwN", eZMySQLCharset::mapTo( "uNknOwN" ) );
 }
コード例 #7
0
ファイル: mysql.php プロジェクト: robinmuilwijk/ezpublish
    /**
     * Connects to the database.
     *
     * @return void
     * @throw eZClusterHandlerDBNoConnectionException
     * @throw eZClusterHandlerDBNoDatabaseException
     */
    public function _connect()
    {
        $siteINI = eZINI::instance( 'site.ini' );
        // DB Connection setup
        // This part is not actually required since _connect will only be called
        // once, but it is useful to run the unit tests. So be it.
        // @todo refactor this using eZINI::setVariable in unit tests
        if ( self::$dbparams === null )
        {
            $fileINI = eZINI::instance( 'file.ini' );

            self::$dbparams = array();
            self::$dbparams['host']       = $fileINI->variable( 'eZDFSClusteringSettings', 'DBHost' );
            self::$dbparams['port']       = $fileINI->variable( 'eZDFSClusteringSettings', 'DBPort' );
            self::$dbparams['socket']     = $fileINI->variable( 'eZDFSClusteringSettings', 'DBSocket' );
            self::$dbparams['dbname']     = $fileINI->variable( 'eZDFSClusteringSettings', 'DBName' );
            self::$dbparams['user']       = $fileINI->variable( 'eZDFSClusteringSettings', 'DBUser' );
            self::$dbparams['pass']       = $fileINI->variable( 'eZDFSClusteringSettings', 'DBPassword' );

            self::$dbparams['max_connect_tries'] = $fileINI->variable( 'eZDFSClusteringSettings', 'DBConnectRetries' );
            self::$dbparams['max_execute_tries'] = $fileINI->variable( 'eZDFSClusteringSettings', 'DBExecuteRetries' );

            self::$dbparams['sql_output'] = $siteINI->variable( "DatabaseSettings", "SQLOutput" ) == "enabled";

            self::$dbparams['cache_generation_timeout'] = $siteINI->variable( "ContentSettings", "CacheGenerationTimeout" );
        }

        $serverString = self::$dbparams['host'];
        if ( self::$dbparams['socket'] )
            $serverString .= ':' . self::$dbparams['socket'];
        elseif ( self::$dbparams['port'] )
            $serverString .= ':' . self::$dbparams['port'];

        $maxTries = self::$dbparams['max_connect_tries'];
        $tries = 0;
        while ( $tries < $maxTries )
        {
            if ( $this->db = mysql_connect( $serverString, self::$dbparams['user'], self::$dbparams['pass'] ) )
                break;
            ++$tries;
        }
        if ( !$this->db )
            throw new eZClusterHandlerDBNoConnectionException( $serverString, self::$dbparams['user'], self::$dbparams['pass'] );

        if ( !mysql_select_db( self::$dbparams['dbname'], $this->db ) )
            throw new eZClusterHandlerDBNoDatabaseException( self::$dbparams['dbname'] );

        // DFS setup
        if ( $this->dfsbackend === null )
        {
            $this->dfsbackend = new eZDFSFileHandlerDFSBackend();
        }

        $charset = trim( $siteINI->variable( 'DatabaseSettings', 'Charset' ) );
        if ( $charset === '' )
        {
            $charset = eZTextCodec::internalCharset();
        }

        if ( $charset )
        {
            if ( !mysql_query( "SET NAMES '" . eZMySQLCharset::mapTo( $charset ) . "'", $this->db ) )
            {
                $this->_fail( "Failed to set Database charset to $charset." );
            }
        }
    }
コード例 #8
0
    function connect( $server, $db, $user, $password, $socketPath, $charset = null, $port = false )
    {
        $connection = false;

        if ( $socketPath !== false )
        {
            ini_set( "mysqli.default_socket", $socketPath );
        }

        if ( $this->UsePersistentConnection == true )
        {
            // Only supported on PHP 5.3 (mysqlnd)
            if ( version_compare( PHP_VERSION, '5.3' ) > 0 )
                $this->Server = 'p:' . $this->Server;
            else
                eZDebug::writeWarning( 'mysqli only supports persistent connections when using php 5.3 and higher', __METHOD__ );
        }

        $oldHandling = eZDebug::setHandleType( eZDebug::HANDLE_EXCEPTION );
        try {
            $connection = mysqli_connect( $server, $user, $password, null, (int)$port, $socketPath );
        } catch( ErrorException $e ) {}
        eZDebug::setHandleType( $oldHandling );

        $maxAttempts = $this->connectRetryCount();
        $waitTime = $this->connectRetryWaitTime();
        $numAttempts = 1;
        while ( !$connection && $numAttempts <= $maxAttempts )
        {
            sleep( $waitTime );

            $oldHandling = eZDebug::setHandleType( eZDebug::HANDLE_EXCEPTION );
            try {
                $connection = mysqli_connect( $this->Server, $this->User, $this->Password, null, (int)$this->Port, $this->SocketPath );
            } catch( ErrorException $e ) {}
            eZDebug::setHandleType( $oldHandling );

            $numAttempts++;
        }
        $this->setError();

        $this->IsConnected = true;

        if ( !$connection )
        {
            eZDebug::writeError( "Connection error: Couldn't connect to database server. Please try again later or inform the system administrator.\n{$this->ErrorMessage}", __CLASS__ );
            $this->IsConnected = false;
            throw new eZDBNoConnectionException( $server, $this->ErrorMessage, $this->ErrorNumber );
        }

        if ( $this->IsConnected && $db != null )
        {
            $ret = mysqli_select_db( $connection, $db );
            if ( !$ret )
            {
                $this->setError( $connection );
                eZDebug::writeError( "Connection error: Couldn't select the database. Please try again later or inform the system administrator.\n{$this->ErrorMessage}", __CLASS__ );
                $this->IsConnected = false;
            }
        }

        if ( $charset !== null )
        {
            $originalCharset = $charset;
            $charset = eZCharsetInfo::realCharsetCode( $charset );
        }

        if ( $this->IsConnected and $charset !== null )
        {
            $status = mysqli_set_charset( $connection, eZMySQLCharset::mapTo( $charset ) );
            if ( !$status )
            {
                $this->setError();
                eZDebug::writeWarning( "Connection warning: " . mysqli_errno( $connection ) . ": " . mysqli_error( $connection ), "eZMySQLiDB" );
            }
        }

        return $connection;
    }