Database.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. <?php
  2. namespace Drupal\Core\Database;
  3. /**
  4. * Primary front-controller for the database system.
  5. *
  6. * This class is uninstantiatable and un-extendable. It acts to encapsulate
  7. * all control and shepherding of database connections into a single location
  8. * without the use of globals.
  9. */
  10. abstract class Database {
  11. /**
  12. * Flag to indicate a query call should simply return NULL.
  13. *
  14. * This is used for queries that have no reasonable return value anyway, such
  15. * as INSERT statements to a table without a serial primary key.
  16. */
  17. const RETURN_NULL = 0;
  18. /**
  19. * Flag to indicate a query call should return the prepared statement.
  20. */
  21. const RETURN_STATEMENT = 1;
  22. /**
  23. * Flag to indicate a query call should return the number of affected rows.
  24. */
  25. const RETURN_AFFECTED = 2;
  26. /**
  27. * Flag to indicate a query call should return the "last insert id".
  28. */
  29. const RETURN_INSERT_ID = 3;
  30. /**
  31. * An nested array of all active connections. It is keyed by database name
  32. * and target.
  33. *
  34. * @var array
  35. */
  36. static protected $connections = [];
  37. /**
  38. * A processed copy of the database connection information from settings.php.
  39. *
  40. * @var array
  41. */
  42. static protected $databaseInfo = [];
  43. /**
  44. * A list of key/target credentials to simply ignore.
  45. *
  46. * @var array
  47. */
  48. static protected $ignoreTargets = [];
  49. /**
  50. * The key of the currently active database connection.
  51. *
  52. * @var string
  53. */
  54. static protected $activeKey = 'default';
  55. /**
  56. * An array of active query log objects.
  57. *
  58. * Every connection has one and only one logger object for all targets and
  59. * logging keys.
  60. *
  61. * array(
  62. * '$db_key' => DatabaseLog object.
  63. * );
  64. *
  65. * @var array
  66. */
  67. static protected $logs = [];
  68. /**
  69. * Starts logging a given logging key on the specified connection.
  70. *
  71. * @param string $logging_key
  72. * The logging key to log.
  73. * @param string $key
  74. * The database connection key for which we want to log.
  75. *
  76. * @return \Drupal\Core\Database\Log
  77. * The query log object. Note that the log object does support richer
  78. * methods than the few exposed through the Database class, so in some
  79. * cases it may be desirable to access it directly.
  80. *
  81. * @see \Drupal\Core\Database\Log
  82. */
  83. final public static function startLog($logging_key, $key = 'default') {
  84. if (empty(self::$logs[$key])) {
  85. self::$logs[$key] = new Log($key);
  86. // Every target already active for this connection key needs to have the
  87. // logging object associated with it.
  88. if (!empty(self::$connections[$key])) {
  89. foreach (self::$connections[$key] as $connection) {
  90. $connection->setLogger(self::$logs[$key]);
  91. }
  92. }
  93. }
  94. self::$logs[$key]->start($logging_key);
  95. return self::$logs[$key];
  96. }
  97. /**
  98. * Retrieves the queries logged on for given logging key.
  99. *
  100. * This method also ends logging for the specified key. To get the query log
  101. * to date without ending the logger request the logging object by starting
  102. * it again (which does nothing to an open log key) and call methods on it as
  103. * desired.
  104. *
  105. * @param string $logging_key
  106. * The logging key to log.
  107. * @param string $key
  108. * The database connection key for which we want to log.
  109. *
  110. * @return array
  111. * The query log for the specified logging key and connection.
  112. *
  113. * @see \Drupal\Core\Database\Log
  114. */
  115. final public static function getLog($logging_key, $key = 'default') {
  116. if (empty(self::$logs[$key])) {
  117. return [];
  118. }
  119. $queries = self::$logs[$key]->get($logging_key);
  120. self::$logs[$key]->end($logging_key);
  121. return $queries;
  122. }
  123. /**
  124. * Gets the connection object for the specified database key and target.
  125. *
  126. * @param string $target
  127. * The database target name.
  128. * @param string $key
  129. * The database connection key. Defaults to NULL which means the active key.
  130. *
  131. * @return \Drupal\Core\Database\Connection
  132. * The corresponding connection object.
  133. */
  134. final public static function getConnection($target = 'default', $key = NULL) {
  135. if (!isset($key)) {
  136. // By default, we want the active connection, set in setActiveConnection.
  137. $key = self::$activeKey;
  138. }
  139. // If the requested target does not exist, or if it is ignored, we fall back
  140. // to the default target. The target is typically either "default" or
  141. // "replica", indicating to use a replica SQL server if one is available. If
  142. // it's not available, then the default/primary server is the correct server
  143. // to use.
  144. if (!empty(self::$ignoreTargets[$key][$target]) || !isset(self::$databaseInfo[$key][$target])) {
  145. $target = 'default';
  146. }
  147. if (!isset(self::$connections[$key][$target])) {
  148. // If necessary, a new connection is opened.
  149. self::$connections[$key][$target] = self::openConnection($key, $target);
  150. }
  151. return self::$connections[$key][$target];
  152. }
  153. /**
  154. * Determines if there is an active connection.
  155. *
  156. * Note that this method will return FALSE if no connection has been
  157. * established yet, even if one could be.
  158. *
  159. * @return bool
  160. * TRUE if there is at least one database connection established, FALSE
  161. * otherwise.
  162. */
  163. final public static function isActiveConnection() {
  164. return !empty(self::$activeKey) && !empty(self::$connections) && !empty(self::$connections[self::$activeKey]);
  165. }
  166. /**
  167. * Sets the active connection to the specified key.
  168. *
  169. * @return string|null
  170. * The previous database connection key.
  171. */
  172. final public static function setActiveConnection($key = 'default') {
  173. if (!empty(self::$databaseInfo[$key])) {
  174. $old_key = self::$activeKey;
  175. self::$activeKey = $key;
  176. return $old_key;
  177. }
  178. }
  179. /**
  180. * Process the configuration file for database information.
  181. *
  182. * @param array $info
  183. * The database connection information, as defined in settings.php. The
  184. * structure of this array depends on the database driver it is connecting
  185. * to.
  186. */
  187. final public static function parseConnectionInfo(array $info) {
  188. // If there is no "driver" property, then we assume it's an array of
  189. // possible connections for this target. Pick one at random. That allows
  190. // us to have, for example, multiple replica servers.
  191. if (empty($info['driver'])) {
  192. $info = $info[mt_rand(0, count($info) - 1)];
  193. }
  194. // Parse the prefix information.
  195. if (!isset($info['prefix'])) {
  196. // Default to an empty prefix.
  197. $info['prefix'] = [
  198. 'default' => '',
  199. ];
  200. }
  201. elseif (!is_array($info['prefix'])) {
  202. // Transform the flat form into an array form.
  203. $info['prefix'] = [
  204. 'default' => $info['prefix'],
  205. ];
  206. }
  207. return $info;
  208. }
  209. /**
  210. * Adds database connection information for a given key/target.
  211. *
  212. * This method allows to add new connections at runtime.
  213. *
  214. * Under normal circumstances the preferred way to specify database
  215. * credentials is via settings.php. However, this method allows them to be
  216. * added at arbitrary times, such as during unit tests, when connecting to
  217. * admin-defined third party databases, etc.
  218. *
  219. * If the given key/target pair already exists, this method will be ignored.
  220. *
  221. * @param string $key
  222. * The database key.
  223. * @param string $target
  224. * The database target name.
  225. * @param array $info
  226. * The database connection information, as defined in settings.php. The
  227. * structure of this array depends on the database driver it is connecting
  228. * to.
  229. */
  230. final public static function addConnectionInfo($key, $target, array $info) {
  231. if (empty(self::$databaseInfo[$key][$target])) {
  232. self::$databaseInfo[$key][$target] = self::parseConnectionInfo($info);
  233. }
  234. }
  235. /**
  236. * Gets information on the specified database connection.
  237. *
  238. * @param string $key
  239. * (optional) The connection key for which to return information.
  240. *
  241. * @return array|null
  242. */
  243. final public static function getConnectionInfo($key = 'default') {
  244. if (!empty(self::$databaseInfo[$key])) {
  245. return self::$databaseInfo[$key];
  246. }
  247. }
  248. /**
  249. * Gets connection information for all available databases.
  250. *
  251. * @return array
  252. */
  253. final public static function getAllConnectionInfo() {
  254. return self::$databaseInfo;
  255. }
  256. /**
  257. * Sets connection information for multiple databases.
  258. *
  259. * @param array $databases
  260. * A multi-dimensional array specifying database connection parameters, as
  261. * defined in settings.php.
  262. */
  263. final public static function setMultipleConnectionInfo(array $databases) {
  264. foreach ($databases as $key => $targets) {
  265. foreach ($targets as $target => $info) {
  266. self::addConnectionInfo($key, $target, $info);
  267. }
  268. }
  269. }
  270. /**
  271. * Rename a connection and its corresponding connection information.
  272. *
  273. * @param string $old_key
  274. * The old connection key.
  275. * @param string $new_key
  276. * The new connection key.
  277. *
  278. * @return bool
  279. * TRUE in case of success, FALSE otherwise.
  280. */
  281. final public static function renameConnection($old_key, $new_key) {
  282. if (!empty(self::$databaseInfo[$old_key]) && empty(self::$databaseInfo[$new_key])) {
  283. // Migrate the database connection information.
  284. self::$databaseInfo[$new_key] = self::$databaseInfo[$old_key];
  285. unset(self::$databaseInfo[$old_key]);
  286. // Migrate over the DatabaseConnection object if it exists.
  287. if (isset(self::$connections[$old_key])) {
  288. self::$connections[$new_key] = self::$connections[$old_key];
  289. unset(self::$connections[$old_key]);
  290. }
  291. return TRUE;
  292. }
  293. else {
  294. return FALSE;
  295. }
  296. }
  297. /**
  298. * Remove a connection and its corresponding connection information.
  299. *
  300. * @param string $key
  301. * The connection key.
  302. *
  303. * @return bool
  304. * TRUE in case of success, FALSE otherwise.
  305. */
  306. final public static function removeConnection($key) {
  307. if (isset(self::$databaseInfo[$key])) {
  308. self::closeConnection(NULL, $key);
  309. unset(self::$databaseInfo[$key]);
  310. return TRUE;
  311. }
  312. else {
  313. return FALSE;
  314. }
  315. }
  316. /**
  317. * Opens a connection to the server specified by the given key and target.
  318. *
  319. * @param string $key
  320. * The database connection key, as specified in settings.php. The default is
  321. * "default".
  322. * @param string $target
  323. * The database target to open.
  324. *
  325. * @throws \Drupal\Core\Database\ConnectionNotDefinedException
  326. * @throws \Drupal\Core\Database\DriverNotSpecifiedException
  327. */
  328. final protected static function openConnection($key, $target) {
  329. // If the requested database does not exist then it is an unrecoverable
  330. // error.
  331. if (!isset(self::$databaseInfo[$key])) {
  332. throw new ConnectionNotDefinedException('The specified database connection is not defined: ' . $key);
  333. }
  334. if (!$driver = self::$databaseInfo[$key][$target]['driver']) {
  335. throw new DriverNotSpecifiedException('Driver not specified for this database connection: ' . $key);
  336. }
  337. if (!empty(self::$databaseInfo[$key][$target]['namespace'])) {
  338. $driver_class = self::$databaseInfo[$key][$target]['namespace'] . '\\Connection';
  339. }
  340. else {
  341. // Fallback for Drupal 7 settings.php.
  342. $driver_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Connection";
  343. }
  344. $pdo_connection = $driver_class::open(self::$databaseInfo[$key][$target]);
  345. $new_connection = new $driver_class($pdo_connection, self::$databaseInfo[$key][$target]);
  346. $new_connection->setTarget($target);
  347. $new_connection->setKey($key);
  348. // If we have any active logging objects for this connection key, we need
  349. // to associate them with the connection we just opened.
  350. if (!empty(self::$logs[$key])) {
  351. $new_connection->setLogger(self::$logs[$key]);
  352. }
  353. return $new_connection;
  354. }
  355. /**
  356. * Closes a connection to the server specified by the given key and target.
  357. *
  358. * @param string $target
  359. * The database target name. Defaults to NULL meaning that all target
  360. * connections will be closed.
  361. * @param string $key
  362. * The database connection key. Defaults to NULL which means the active key.
  363. */
  364. public static function closeConnection($target = NULL, $key = NULL) {
  365. // Gets the active connection by default.
  366. if (!isset($key)) {
  367. $key = self::$activeKey;
  368. }
  369. // To close a connection, it needs to be set to NULL and removed from the
  370. // static variable. In all cases, closeConnection() might be called for a
  371. // connection that was not opened yet, in which case the key is not defined
  372. // yet and we just ensure that the connection key is undefined.
  373. if (isset($target)) {
  374. if (isset(self::$connections[$key][$target])) {
  375. self::$connections[$key][$target]->destroy();
  376. self::$connections[$key][$target] = NULL;
  377. }
  378. unset(self::$connections[$key][$target]);
  379. }
  380. else {
  381. if (isset(self::$connections[$key])) {
  382. foreach (self::$connections[$key] as $target => $connection) {
  383. self::$connections[$key][$target]->destroy();
  384. self::$connections[$key][$target] = NULL;
  385. }
  386. }
  387. unset(self::$connections[$key]);
  388. }
  389. }
  390. /**
  391. * Instructs the system to temporarily ignore a given key/target.
  392. *
  393. * At times we need to temporarily disable replica queries. To do so, call this
  394. * method with the database key and the target to disable. That database key
  395. * will then always fall back to 'default' for that key, even if it's defined.
  396. *
  397. * @param string $key
  398. * The database connection key.
  399. * @param string $target
  400. * The target of the specified key to ignore.
  401. */
  402. public static function ignoreTarget($key, $target) {
  403. self::$ignoreTargets[$key][$target] = TRUE;
  404. }
  405. /**
  406. * Converts a URL to a database connection info array.
  407. *
  408. * @param string $url
  409. * The URL.
  410. * @param string $root
  411. * The root directory of the Drupal installation.
  412. *
  413. * @return array
  414. * The database connection info.
  415. *
  416. * @throws \InvalidArgumentException
  417. * Exception thrown when the provided URL does not meet the minimum
  418. * requirements.
  419. */
  420. public static function convertDbUrlToConnectionInfo($url, $root) {
  421. $info = parse_url($url);
  422. if (!isset($info['scheme'], $info['host'], $info['path'])) {
  423. throw new \InvalidArgumentException('Minimum requirement: driver://host/database');
  424. }
  425. $info += [
  426. 'user' => '',
  427. 'pass' => '',
  428. 'fragment' => '',
  429. ];
  430. // A SQLite database path with two leading slashes indicates a system path.
  431. // Otherwise the path is relative to the Drupal root.
  432. if ($info['path'][0] === '/') {
  433. $info['path'] = substr($info['path'], 1);
  434. }
  435. if ($info['scheme'] === 'sqlite' && $info['path'][0] !== '/') {
  436. $info['path'] = $root . '/' . $info['path'];
  437. }
  438. $database = [
  439. 'driver' => $info['scheme'],
  440. 'username' => $info['user'],
  441. 'password' => $info['pass'],
  442. 'host' => $info['host'],
  443. 'database' => $info['path'],
  444. ];
  445. if (isset($info['port'])) {
  446. $database['port'] = $info['port'];
  447. }
  448. return $database;
  449. }
  450. /**
  451. * Gets database connection info as a URL.
  452. *
  453. * @param string $key
  454. * (Optional) The database connection key.
  455. *
  456. * @return string
  457. * The connection info as a URL.
  458. */
  459. public static function getConnectionInfoAsUrl($key = 'default') {
  460. $db_info = static::getConnectionInfo($key);
  461. if ($db_info['default']['driver'] == 'sqlite') {
  462. $db_url = 'sqlite://localhost/' . $db_info['default']['database'];
  463. }
  464. else {
  465. $user = '';
  466. if ($db_info['default']['username']) {
  467. $user = $db_info['default']['username'];
  468. if ($db_info['default']['password']) {
  469. $user .= ':' . $db_info['default']['password'];
  470. }
  471. $user .= '@';
  472. }
  473. $db_url = $db_info['default']['driver'] . '://' . $user . $db_info['default']['host'];
  474. if (isset($db_info['default']['port'])) {
  475. $db_url .= ':' . $db_info['default']['port'];
  476. }
  477. $db_url .= '/' . $db_info['default']['database'];
  478. }
  479. if ($db_info['default']['prefix']['default']) {
  480. $db_url .= '#' . $db_info['default']['prefix']['default'];
  481. }
  482. return $db_url;
  483. }
  484. }