<?phpnamespace Illuminate\Database\Connectors;use PDO;// this is mysql connection classclass 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 */ &NBSP;&NBSP;&NBSP;&NBSP;PROTECTED&NBSP;FUNCTION&NBSP;GETDSN (array $config) { return $this->confighassocket ($config) ? $this- >GETSOCKETDSN ($config) &NBsp;: $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 &NBSP;STRING&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;*/&NBSP;&NBSP;&NBSP;&NBSP;PROTECTED&NBSP;FUNCTION&NBSP;GETSOCKETDSN ( array $config) { return "MySQL : unix_socket={$config [' Unix_socket ']};d bname={$config [' Database ']} "; } /** * Get the DSN string for a host / port configuration. * * @ param array $config * @return string &NBSP;&NBSP;&NBSP;*/&NBSP;&NBSP;&NBSP;&NBSP;PROTECTED&NBSP;FUNCTION&NBSP;GETHOSTDSN (array $config) { extract ($config, extr_skip); return isset ($port) ? "mysql:host={$host};p ort={$port };d bname={$database} " : "mysql:host={$host};d bname={$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 (); } } }}
This article is from the "Focus on PHP Group number: 414194301" blog, please be sure to keep this source http://jingshanls.blog.51cto.com/3357095/1853489
[Li Jingshan php] every day laravel-20161120| mysqlconnector.php