Comments

danblack’s picture

Title: quoting of default database columns for mysql, postgres and sqlite » quoting of default database columns and indexes and fields in postgres
Issue summary: View changes
StatusFileSize
new12.05 KB

now includes test cases.

sun’s picture

There's a Schema-related test addition in this patch, but not a query related test?

danblack’s picture

Related issues: +#710940: Support for BINARY and VARBINARY in Database Schema
StatusFileSize
new4.99 KB

fixed patch to just handle default value. Includes testes for schema creation and altering a column definitions. After this #710940: Support for BINARY and VARBINARY in Database Schema should pass.

I'll leave field quoting for #281058: Schema API: properly quote column names.

danblack’s picture

Title: quoting of default database columns and indexes and fields in postgres » quoting of default database columns
sun’s picture

Title: quoting of default database columns » Database fields/columns are not properly quoted via PDO::quote()

Sorry, now I'm even more confused...

You removed all the changes to schema related classes, but you kept the schema related test addition... (?)

I think this patch should convert everything to use quote() (as in #1). Or is there a special reason for why not?

My comment in #2 only asked for non-schema, but query-related test coverage. There is already some test coverage for quoting values, but the change to quote() might introduce new cases?

In other words, #1 looked pretty good to me. #281058: Schema API: properly quote column names appears to be a stone-age issue, which I'd rather mark as duplicate... I think this issue should cover all instances that need to be fixed.

(That said, I'm not a database system maintainer, so I don't know why the current code doesn't use PDO::quote()...)

danblack’s picture

#1 didn't work on postgresql. Maybe I needed to change the quoting style. quote worked perfectly well on values. I suspect fields need to be handled differently.

There are two bits to the test:

  • testing creating the schema in create table
  • testing any alter table of setting the default value on a column

I'm having trouble differentiation what you mean by which tests are schema and which are query.

"I don't know why the current code doesn't use PDO::quote()...)"

I suspect because has strict rules about column names, indexes so quoting isn't required.

danblack’s picture

column names appear to be tricky and doesn't use quote.

danblack’s picture

for escaping fields there is Connection::escape_field (which could be overwritten by the Driver/pgsql/Connection class if desired) however they seem inconsistently applied.

I came across the incorrect escaping of default values when writing the improvements #710940: Support for BINARY and VARBINARY in Database Schema where I discovered other manually quoted fields. Data values should be quoted with PDO::quote however fields should be quoted as per ANSI standard comment #7. I'd rather focus on other performance problems rather than do two different kind of fixes in the same patch.

sun’s picture

Title: Database fields/columns are not properly quoted via PDO::quote() » Database Schema field/column default value is not properly quoted via PDO::quote()

I'm having trouble differentiation what you mean by which tests are schema and which are query.

All code in a class name containing "schema" is database schema related. Everything else lives in other classes (that may have "query" in their name, but not necessarily).

But I just recognized that I wrongly interpreted the patch, and you're still changing schema code only. The only difference between #1 and #3 appears to be that you removed quoting for field identifiers, only quoting values.

That's correct, see http://php.net/manual/en/pdo.quote.php (+ user comments). PDO::quote() is only meant for values, not fields/identifiers.

That said, there's an interesting user comment that says that quote() turns NULL into '' (possibly when not specifying custom PDO::PARAM_* constants) — does that affect our usage? Let's check whether there is a test for that, and if not, we might need to add one?

sun’s picture

StatusFileSize
new8.44 KB

Forgot that I wanted to attach interdiff between #1 and #3.

danblack’s picture

> That said, there's an interesting user comment that says that quote() turns NULL into ''

Conversion using quote is only done after a is_string(x) test.

> (possibly when not specifying custom PDO::PARAM_* constants) — does that affect our usage? Let's check whether there is a test for that, and if not, we might need to add one?

not currently. it passes tests on sqlite, pgsql and mysql. That doesn't rule out future improvements could be done in this respect.

sun’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for the clarifications. I think this patch makes sense.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 3: quote.patch, failed testing.

sun’s picture

3: quote.patch queued for re-testing.

sun’s picture

Status: Needs work » Reviewed & tested by the community

Testbot fluke. (Out of disk space, it seems)

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 0231d35 and pushed to 8.x. Thanks!

  • Commit 0231d35 on 8.x by alexpott:
    Issue #2232425 by danblack: Database Schema field/column default value...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

danblack’s picture

Status: Closed (fixed) » Needs review
StatusFileSize
new3.09 KB

missed handling of NULL defaults and one more instance where quotes didn't use pdo->quote.

sun’s picture

+++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
+  protected function _escapeDefaultValue($value) {

No need to add a "private" method underscore prefix here.

Also, MySQL's Schema class contains the same code, so why don't we move this helper method into the base Schema class?

danblack’s picture

StatusFileSize
new4.59 KB

thanks for the review.

danblack’s picture

StatusFileSize
new4.66 KB

minor correction

--- a/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
+++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
@@ -206,8 +206,8 @@ protected function createFieldSql($name, $spec) {
         $sql .= ' NULL';
       }
     }
-    if (isset($spec['default'])) {
+    if (array_key_exists('default', $spec)) {
      $default = $this->escapeDefaultValue($spec['default']);
       $sql .= " default $default";
     }
sun’s picture

  1. +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
    @@ -631,6 +626,7 @@
     
    +
       protected function _createIndexSql($table, $name, $fields) {
    

    Erroneous newline.

  2. +++ b/core/lib/Drupal/Core/Database/Schema.php
    @@ -722,4 +722,20 @@
    +   * Escape the value used as a default value on a column
    

    Should start with a third-person verb and end with a period.

  3. +++ b/core/lib/Drupal/Core/Database/Schema.php
    @@ -722,4 +722,20 @@
    +   * @param $value
    

    Either of type mixed or string|null if those are the only possible values at this point.

  4. +++ b/core/lib/Drupal/Core/Database/Schema.php
    @@ -722,4 +722,20 @@
    +   * @return
    

    Always appears to be of type string?

danblack’s picture

StatusFileSize
new4.4 KB

Always appears to be of type string?

or int/float if that's what was passed in.

stefan.r’s picture

Issue tags: +PostgreSQL
StatusFileSize
new1.15 KB
stefan.r’s picture

23: quote_default_null.patch queued for re-testing.

(#25 is somehow still queued but as noted in the interdiff #23 is the same except for comments and whitespace.)

The last submitted patch, 23: quote_default_null.patch, failed testing.

stefan.r’s picture

StatusFileSize
new4.35 KB

re-roll

The last submitted patch, 25: quote_default_null.patch, failed testing.

danblack’s picture

thanks for the re-roll @stefan.r

bzrudi71’s picture

Status: Needs review » Reviewed & tested by the community

Patch still applies, works and seems like a nice cleanup also.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed cfd30d6 and pushed to 8.0.x. Thanks!

  • alexpott committed cfd30d6 on 8.0.x
    Issue #2232425 followup by danblack, stefan.r: Fixed Database Schema...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.