为了账号安全,请及时绑定邮箱和手机立即绑定

MySqlConnector.php

标签:
PHP MySQL

<?php

 

namespace Illuminate\Database\Connectors;

 

use PDO;

// this is mysql connection class

class MySqlConnector extends Connector implements ConnectorInterface

{// extends adn implements make class become more powerful

   // this is mysql connection

    /**

     * Establish a database connection.

     *

     * @param  array  $config

     * @return \PDO

     */

    public function connect(array $config)

    {// Establish a database connection

        $dsn = $this->getDsn($config);// first to get the dsn string

 

        $options = $this->getOptions($config);// second ,get the options

 

        // We need to grab the PDO options that should be used while making the brand

        // new connection instance. The PDO options control various aspects of the

        // connection's behavior, and some might be specified by the developers.

        $connection = $this->createConnection($dsn, $config, $options);

// we need to grab the pdo options that should be bused while making the brand new connection instance.

// The PDO options control various aspects of connections's behavior,and some might be specified by the developers.

        if (isset($config['unix_socket'])) {

            $connection->exec("use `{$config['database']}`;");

        }// set the unix_socket

 

        $collation = $config['collation'];//set the collation

 

        // Next we will set the "names" and "collation" on the clients connections so

        // a correct character set will be used by this client. The collation also

        // is set on the server but needs to be set here on this client objects.

        $charset = $config['charset'];

       // Next we will set the "names" and "collation" on the clients connections so a correct char

 

        $names = "set names '$charset'".

            (! is_null($collation) ? " collate '$collation'" : '');

 

        $connection->prepare($names)->execute();

 

        // Next, we will check to see if a timezone has been specified in this config

        // and if it has we will issue a statement to modify the timezone with the

        // database. Setting this DB timezone is an optional configuration item.

        if (isset($config['timezone'])) {

            $connection->prepare(

                'set time_zone="'.$config['timezone'].'"'

            )->execute();

        }// database name charset and timezone

 

        $this->setModes($connection, $config);// set the mode

 

        return $connection;

    }// just return the connection

 

    /**

     * Create a DSN string from a configuration.

     *

     * Chooses socket or host/port based on the 'unix_socket' config value.

     *

     * @param  array   $config

     * @return string

     */

    protected function getDsn(array $config)

    {

        return $this->configHasSocket($config) ? $this->getSocketDsn($config) : $this->getHostDsn($config);

    }// create a dsn string from a configuration

 

    /**

     * Determine if the given configuration array has a UNIX socket value.

     *

     * @param  array  $config

     * @return bool

     */

    protected function configHasSocket(array $config)

    {

        return isset($config['unix_socket']) && ! empty($config['unix_socket']);

    }

 

    /**

     * Get the DSN string for a socket configuration.

     *

     * @param  array  $config

     * @return string

     */

    protected function getSocketDsn(array $config)

    {

        return "mysql:unix_socket={$config['unix_socket']};dbname={$config['database']}";

    }

 

    /**

     * Get the DSN string for a host / port configuration.

     *

     * @param  array  $config

     * @return string

     */

    protected function getHostDsn(array $config)

    {

        extract($config, EXTR_SKIP);

 

        return isset($port)

                        ? "mysql:host={$host};port={$port};dbname={$database}"

                        : "mysql:host={$host};dbname={$database}";

    }

 

    /**

     * Set the modes for the connection.

     *

     * @param  \PDO  $connection

     * @param  array  $config

     * @return void

     */

    protected function setModes(PDO $connection, array $config)

    {// set te session sql_mode in this hash mode

        if (isset($config['modes'])) {

            $modes = implode(',', $config['modes']);

 

            $connection->prepare("set session sql_mode='".$modes."'")->execute();

        } elseif (isset($config['strict'])) {

            if ($config['strict']) {

                $connection->prepare("set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'")->execute();

            } else {

                $connection->prepare("set session sql_mode='NO_ENGINE_SUBSTITUTION'")->execute();

            }

        }

    }

}

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消