comparison core/lib/Drupal/Core/Database/Connection.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
806 * Prepares and returns an INSERT query object. 806 * Prepares and returns an INSERT query object.
807 * 807 *
808 * @param string $table 808 * @param string $table
809 * The table to use for the insert statement. 809 * The table to use for the insert statement.
810 * @param array $options 810 * @param array $options
811 * (optional) An array of options on the query. 811 * (optional) An associative array of options to control how the query is
812 * run. The given options will be merged with
813 * \Drupal\Core\Database\Connection::defaultOptions().
812 * 814 *
813 * @return \Drupal\Core\Database\Query\Insert 815 * @return \Drupal\Core\Database\Query\Insert
814 * A new Insert query object. 816 * A new Insert query object.
815 * 817 *
816 * @see \Drupal\Core\Database\Query\Insert 818 * @see \Drupal\Core\Database\Query\Insert
819 * @see \Drupal\Core\Database\Connection::defaultOptions()
817 */ 820 */
818 public function insert($table, array $options = []) { 821 public function insert($table, array $options = []) {
819 $class = $this->getDriverClass('Insert'); 822 $class = $this->getDriverClass('Insert');
820 return new $class($this, $table, $options); 823 return new $class($this, $table, $options);
821 } 824 }
860 * Prepares and returns an UPDATE query object. 863 * Prepares and returns an UPDATE query object.
861 * 864 *
862 * @param string $table 865 * @param string $table
863 * The table to use for the update statement. 866 * The table to use for the update statement.
864 * @param array $options 867 * @param array $options
865 * (optional) An array of options on the query. 868 * (optional) An associative array of options to control how the query is
869 * run. The given options will be merged with
870 * \Drupal\Core\Database\Connection::defaultOptions().
866 * 871 *
867 * @return \Drupal\Core\Database\Query\Update 872 * @return \Drupal\Core\Database\Query\Update
868 * A new Update query object. 873 * A new Update query object.
869 * 874 *
870 * @see \Drupal\Core\Database\Query\Update 875 * @see \Drupal\Core\Database\Query\Update
876 * @see \Drupal\Core\Database\Connection::defaultOptions()
871 */ 877 */
872 public function update($table, array $options = []) { 878 public function update($table, array $options = []) {
873 $class = $this->getDriverClass('Update'); 879 $class = $this->getDriverClass('Update');
874 return new $class($this, $table, $options); 880 return new $class($this, $table, $options);
875 } 881 }
878 * Prepares and returns a DELETE query object. 884 * Prepares and returns a DELETE query object.
879 * 885 *
880 * @param string $table 886 * @param string $table
881 * The table to use for the delete statement. 887 * The table to use for the delete statement.
882 * @param array $options 888 * @param array $options
883 * (optional) An array of options on the query. 889 * (optional) An associative array of options to control how the query is
890 * run. The given options will be merged with
891 * \Drupal\Core\Database\Connection::defaultOptions().
884 * 892 *
885 * @return \Drupal\Core\Database\Query\Delete 893 * @return \Drupal\Core\Database\Query\Delete
886 * A new Delete query object. 894 * A new Delete query object.
887 * 895 *
888 * @see \Drupal\Core\Database\Query\Delete 896 * @see \Drupal\Core\Database\Query\Delete
897 * @see \Drupal\Core\Database\Connection::defaultOptions()
889 */ 898 */
890 public function delete($table, array $options = []) { 899 public function delete($table, array $options = []) {
891 $class = $this->getDriverClass('Delete'); 900 $class = $this->getDriverClass('Delete');
892 return new $class($this, $table, $options); 901 return new $class($this, $table, $options);
893 } 902 }
1470 */ 1479 */
1471 public function __sleep() { 1480 public function __sleep() {
1472 throw new \LogicException('The database connection is not serializable. This probably means you are serializing an object that has an indirect reference to the database connection. Adjust your code so that is not necessary. Alternatively, look at DependencySerializationTrait as a temporary solution.'); 1481 throw new \LogicException('The database connection is not serializable. This probably means you are serializing an object that has an indirect reference to the database connection. Adjust your code so that is not necessary. Alternatively, look at DependencySerializationTrait as a temporary solution.');
1473 } 1482 }
1474 1483
1484 /**
1485 * Creates an array of database connection options from a URL.
1486 *
1487 * @internal
1488 * This method should not be called. Use
1489 * \Drupal\Core\Database\Database::convertDbUrlToConnectionInfo() instead.
1490 *
1491 * @param string $url
1492 * The URL.
1493 * @param string $root
1494 * The root directory of the Drupal installation. Some database drivers,
1495 * like for example SQLite, need this information.
1496 *
1497 * @return array
1498 * The connection options.
1499 *
1500 * @throws \InvalidArgumentException
1501 * Exception thrown when the provided URL does not meet the minimum
1502 * requirements.
1503 *
1504 * @see \Drupal\Core\Database\Database::convertDbUrlToConnectionInfo()
1505 */
1506 public static function createConnectionOptionsFromUrl($url, $root) {
1507 $url_components = parse_url($url);
1508 if (!isset($url_components['scheme'], $url_components['host'], $url_components['path'])) {
1509 throw new \InvalidArgumentException('Minimum requirement: driver://host/database');
1510 }
1511
1512 $url_components += [
1513 'user' => '',
1514 'pass' => '',
1515 'fragment' => '',
1516 ];
1517
1518 // Remove leading slash from the URL path.
1519 if ($url_components['path'][0] === '/') {
1520 $url_components['path'] = substr($url_components['path'], 1);
1521 }
1522
1523 // Use reflection to get the namespace of the class being called.
1524 $reflector = new \ReflectionClass(get_called_class());
1525
1526 $database = [
1527 'driver' => $url_components['scheme'],
1528 'username' => $url_components['user'],
1529 'password' => $url_components['pass'],
1530 'host' => $url_components['host'],
1531 'database' => $url_components['path'],
1532 'namespace' => $reflector->getNamespaceName(),
1533 ];
1534
1535 if (isset($url_components['port'])) {
1536 $database['port'] = $url_components['port'];
1537 }
1538
1539 if (!empty($url_components['fragment'])) {
1540 $database['prefix']['default'] = $url_components['fragment'];
1541 }
1542
1543 return $database;
1544 }
1545
1546 /**
1547 * Creates a URL from an array of database connection options.
1548 *
1549 * @internal
1550 * This method should not be called. Use
1551 * \Drupal\Core\Database\Database::getConnectionInfoAsUrl() instead.
1552 *
1553 * @param array $connection_options
1554 * The array of connection options for a database connection.
1555 *
1556 * @return string
1557 * The connection info as a URL.
1558 *
1559 * @throws \InvalidArgumentException
1560 * Exception thrown when the provided array of connection options does not
1561 * meet the minimum requirements.
1562 *
1563 * @see \Drupal\Core\Database\Database::getConnectionInfoAsUrl()
1564 */
1565 public static function createUrlFromConnectionOptions(array $connection_options) {
1566 if (!isset($connection_options['driver'], $connection_options['database'])) {
1567 throw new \InvalidArgumentException("As a minimum, the connection options array must contain at least the 'driver' and 'database' keys");
1568 }
1569
1570 $user = '';
1571 if (isset($connection_options['username'])) {
1572 $user = $connection_options['username'];
1573 if (isset($connection_options['password'])) {
1574 $user .= ':' . $connection_options['password'];
1575 }
1576 $user .= '@';
1577 }
1578
1579 $host = empty($connection_options['host']) ? 'localhost' : $connection_options['host'];
1580
1581 $db_url = $connection_options['driver'] . '://' . $user . $host;
1582
1583 if (isset($connection_options['port'])) {
1584 $db_url .= ':' . $connection_options['port'];
1585 }
1586
1587 $db_url .= '/' . $connection_options['database'];
1588
1589 if (isset($connection_options['prefix']['default']) && $connection_options['prefix']['default'] !== '') {
1590 $db_url .= '#' . $connection_options['prefix']['default'];
1591 }
1592
1593 return $db_url;
1594 }
1595
1475 } 1596 }