diff --git a/core/lib/Drupal/Core/Database/Connection.php b/core/lib/Drupal/Core/Database/Connection.php index d5b56cd..422ddbe 100644 --- a/core/lib/Drupal/Core/Database/Connection.php +++ b/core/lib/Drupal/Core/Database/Connection.php @@ -218,12 +218,12 @@ public function destroy() { * - Database::RETURN_NULL: Do not return anything, as there is no * meaningful value to return. That is the case for INSERT queries on * tables that do not contain a serial column. - * - create_missing_table: When a string, it is a table name. If the query - * fails with a table missing error then this table is created and if - * the creation succeeded then the query is re-run. When TRUE and - * return is set to Database::RETURN_STATEMENT and the query is failing - * with a table missing error then an empty statement is returned. Defaults - * to TRUE. + * - create_missing_table: Boolean value, whether to create a missing table. + * - missing_table_name: In case create_missing_table is TRUE, this will + * be the table name used for the automatic table creation. + * - schema_provider: An object implementing + * \Drupal\Core\Schema\SchemaProviderInterface . This will be called when + * both create_missing_table and missing_table_name is set. * - throw_exception: By default, the database system will catch any errors * on a query as an Exception, log it, and then rethrow it so that code * further up the call chain can take an appropriate action. To suppress @@ -596,15 +596,15 @@ public function query($query, array $args = array(), $options = array()) { * @return bool|StatementInterface */ protected function handleMissingTable($e, $query, $args, $options) { - if ($this->isTableMissingException($e) && !empty($options['create_missing_table'])) { + if ($this->isTableMissingException($e) && $options['create_missing_table']) { if ($options['return'] == Database::RETURN_STATEMENT) { // This is a SELECT on a missing table. Creating the table and // re-runing the query will result in an empty result so return that // early instead. return new StatementEmpty(); } - if (is_string($options['create_missing_table']) && isset($options['schema_provider'])) { - $table = $options['create_missing_table']; + if (!empty($options['missing_table_name']) && isset($options['schema_provider'])) { + $table = $options['missing_table_name']; /** @var \Drupal\Core\Database\SchemaProviderInterface $schema_provider */ if ($schema_provider = $options['schema_provider']) { if ($this->schema()->ensureTableExists($table, $schema_provider->getSchema($table))) { @@ -612,6 +612,7 @@ protected function handleMissingTable($e, $query, $args, $options) { // should not matter but still, databases. This makes sure // no infinite recursion happens. $options['create_missing_table'] = FALSE; + unset($options['missing_table_name']); unset($options['schema_provider']); return $this->query($query, $args, $options); } @@ -1319,6 +1320,26 @@ public function unserialize($serialized) { } /** + * Extracts the SQLSTATE error from the PDOException. + * + * @param \Exception $e + * The exception + * + * @return string + * The five character error code. + */ + protected function getSQLState(\Exception $e) { + // The PDOException code is not always reliable, try to see whether the + // message has something usable. + if (!$e->getCode() && preg_match('/^SQLSTATE\[(\w{5})\]/', $e->getMessage(), $matches)) { + return $matches[1]; + } + else { + return $e->getCode(); + } + } + + /** * Determines whether an exception means missing table. * * @param \Exception $e @@ -1330,4 +1351,5 @@ public function unserialize($serialized) { * TRUE if this exception means table missing, FALSE otherwise. */ abstract public function isTableMissingException(\Exception $e); + } diff --git a/core/lib/Drupal/Core/Database/Database.php b/core/lib/Drupal/Core/Database/Database.php index a5d104d..a799926 100644 --- a/core/lib/Drupal/Core/Database/Database.php +++ b/core/lib/Drupal/Core/Database/Database.php @@ -443,26 +443,4 @@ public static function closeConnection($target = NULL, $key = NULL) { public static function ignoreTarget($key, $target) { self::$ignoreTargets[$key][$target] = TRUE; } - - - /** - * Extracts the SQLSTATE error from the PDOException. - * - * @param \Exception $e - * The exception - * - * @return string - * The five character error code. - */ - public static function getSQLState(\Exception $e) { - // The PDOException code is not always reliable, try to see whether the - // message has something usable. - if (!$e->getCode() && preg_match('/^SQLSTATE\[(\w{5})\]/', $e->getMessage(), $matches)) { - return $matches[1]; - } - else { - return $e->getCode(); - } - } - } diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php b/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php index 8c81798..6843ade 100644 --- a/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php +++ b/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php @@ -278,7 +278,7 @@ protected function popCommittableTransactions() { public function isTableMissingException(\Exception $e) { // "42S02" is the MySQL error code for "Unknown table". // @see http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html - return Database::getSQLState($e) === '42S02'; + return $this->getSQLState($e) === '42S02'; } } diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php index b584cd4..16715c5 100644 --- a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php @@ -334,9 +334,11 @@ public function rollbackSavepoint($savepoint_name = 'mimic_implicit_commit') { public function isTableMissingException(\Exception $e) { // "42P01" is the PostgreSQL error code for "undefined_table". // @see http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html - return Database::getSQLState($e) === '42P01'; + return $this->getSQLState($e) === '42P01'; } + + } /** diff --git a/core/modules/system/src/Tests/Database/SelectTest.php b/core/modules/system/src/Tests/Database/SelectTest.php index 08cf38c..7c40f5f 100644 --- a/core/modules/system/src/Tests/Database/SelectTest.php +++ b/core/modules/system/src/Tests/Database/SelectTest.php @@ -24,7 +24,6 @@ function testSimpleSelect() { $query->addField('test', 'name'); $query->addField('test', 'age', 'age'); $num_records = $query->countQuery()->execute()->fetchField(); - debug($num_records); $this->assertEqual($num_records, 4, 'Returned the correct number of rows.'); }