public static Connection::open(array &$connection_options = array())
Opens a PDO connection.
Parameters
array $connection_options: The database connection settings array.
Return value
\PDO A \PDO object.
Overrides Connection::open
File
- core/lib/Drupal/Core/Database/Driver/mysql/Connection.php, line 98
Class
- Connection
- MySQL implementation of \Drupal\Core\Database\Connection.
Namespace
Drupal\Core\Database\Driver\mysql
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | public static function open( array & $connection_options = array ()) { if (isset( $connection_options [ '_dsn_utf8_fallback' ]) && $connection_options [ '_dsn_utf8_fallback' ] === TRUE) { // Only used during the installer version check, as a fallback from utf8mb4. $charset = 'utf8' ; } else { $charset = 'utf8mb4' ; } // The DSN should use either a socket or a host/port. if (isset( $connection_options [ 'unix_socket' ])) { $dsn = 'mysql:unix_socket=' . $connection_options [ 'unix_socket' ]; } else { // Default to TCP connection on port 3306. $dsn = 'mysql:host=' . $connection_options [ 'host' ] . ';port=' . ( empty ( $connection_options [ 'port' ]) ? 3306 : $connection_options [ 'port' ]); } // Character set is added to dsn to ensure PDO uses the proper character // set when escaping. This has security implications. See // https://www.drupal.org/node/1201452 for further discussion. $dsn .= ';charset=' . $charset ; if (! empty ( $connection_options [ 'database' ])) { $dsn .= ';dbname=' . $connection_options [ 'database' ]; } // Allow PDO options to be overridden. $connection_options += array ( 'pdo' => array (), ); $connection_options [ 'pdo' ] += array ( \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, // So we don't have to mess around with cursors and unbuffered queries by default. \PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE, // Make sure MySQL returns all matched rows on update queries including // rows that actually didn't have to be updated because the values didn't // change. This matches common behavior among other database systems. \PDO::MYSQL_ATTR_FOUND_ROWS => TRUE, // Because MySQL's prepared statements skip the query cache, because it's dumb. \PDO::ATTR_EMULATE_PREPARES => TRUE, ); if (defined( '\PDO::MYSQL_ATTR_MULTI_STATEMENTS' )) { // An added connection option in PHP 5.5.21 to optionally limit SQL to a // single statement like mysqli. $connection_options [ 'pdo' ] += [\PDO::MYSQL_ATTR_MULTI_STATEMENTS => FALSE]; } $pdo = new \PDO( $dsn , $connection_options [ 'username' ], $connection_options [ 'password' ], $connection_options [ 'pdo' ]); // Force MySQL to use the UTF-8 character set. Also set the collation, if a // certain one has been set; otherwise, MySQL defaults to // 'utf8mb4_general_ci' for utf8mb4. if (! empty ( $connection_options [ 'collation' ])) { $pdo -> exec ( 'SET NAMES ' . $charset . ' COLLATE ' . $connection_options [ 'collation' ]); } else { $pdo -> exec ( 'SET NAMES ' . $charset ); } // Set MySQL init_commands if not already defined. Default Drupal's MySQL // behavior to conform more closely to SQL standards. This allows Drupal // to run almost seamlessly on many different kinds of database systems. // These settings force MySQL to behave the same as postgresql, or sqlite // in regards to syntax interpretation and invalid data handling. See // https://www.drupal.org/node/344575 for further discussion. Also, as MySQL // 5.5 changed the meaning of TRADITIONAL we need to spell out the modes one // by one. $connection_options += array ( 'init_commands' => array (), ); $connection_options [ 'init_commands' ] += array ( 'sql_mode' => "SET sql_mode = 'ANSI,STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,ONLY_FULL_GROUP_BY'" , ); // Execute initial commands. foreach ( $connection_options [ 'init_commands' ] as $sql ) { $pdo -> exec ( $sql ); } return $pdo ; } |
Please login to continue.