Skip to content

PDO::getAttribute(): document which attributes every driver supports - #6076

Open
officialasishkumar wants to merge 1 commit into
php:masterfrom
officialasishkumar:pdo-getattribute-driver-support
Open

officialasishkumar wants to merge 1 commit into
php:masterfrom
officialasishkumar:pdo-getattribute-driver-support

Conversation

@officialasishkumar

Copy link
Copy Markdown
Contributor

PDO::getAttribute() handles eight generic attributes itself (ATTR_CASE, ATTR_DEFAULT_FETCH_MODE, ATTR_DRIVER_NAME, ATTR_ERRMODE, ATTR_ORACLE_NULLS, ATTR_PERSISTENT, ATTR_STATEMENT_CLASS and ATTR_STRINGIFY_FETCHES), so every driver supports them. It passes every other attribute to the driver. That includes generic ones such as ATTR_AUTOCOMMIT, ATTR_SERVER_VERSION and ATTR_TIMEOUT, and each driver supports only some of them. The page didn't make this distinction, which is what #4436 is about.

This updates the page:

  • The description and the attribute parameter now say that the listed attributes work with every driver and that support for any other attribute depends on the driver.
  • Return values: on failure the method returns false, not null. A new note explains that some attributes can themselves be false, so the exception error mode or PDO::errorCode() is the reliable way to detect a failure.
  • Errors: an IM001 error is raised when the driver does not support the attribute, followed by the standard &pdo.errors; text.
  • Example: the old one asked PDO_ODBC for ATTR_PREFETCH, ATTR_TIMEOUT and ATTR_CONNECTION_STATUS, which that driver doesn't support, so it threw. The new example uses SQLite, includes ATTR_DRIVER_NAME, catches the PDOException for unsupported attributes and shows its output. This covers the points in the follow-up comment on the issue.

I checked this against ext/pdo/pdo_dbh.c and the get_attribute handlers of the bundled drivers. I also ran the example on PHP 8.4, and configure.php and docbook-cs pass.

Fixes #4436

PDO::getAttribute() handles eight generic attributes itself (ATTR_CASE,
ATTR_DEFAULT_FETCH_MODE, ATTR_DRIVER_NAME, ATTR_ERRMODE,
ATTR_ORACLE_NULLS, ATTR_PERSISTENT, ATTR_STATEMENT_CLASS and
ATTR_STRINGIFY_FETCHES), so they can be retrieved with every driver.
Everything else, including generic attributes such as ATTR_AUTOCOMMIT,
ATTR_SERVER_VERSION or ATTR_TIMEOUT, is passed to the driver, and each
driver only supports some of them. The page listed the eight as "the
generic attributes that apply to database connections" and only said
that some driver combinations "may not support all" attributes, which
did not say which attributes are safe to use everywhere (php#4436).

- State that the listed attributes are handled by PDO and work with
  every driver, and that support for any other attribute depends on the
  driver.
- Fix the return value: a failure returns false, not null. Note that
  some attributes can themselves be false, so a failure is best detected
  through the exception error mode or PDO::errorCode().
- Errors: an IM001 error is raised when the driver does not support the
  attribute; use the standard &pdo.errors; text for how it is reported.
- Replace the example. It requested ATTR_PREFETCH, ATTR_TIMEOUT and
  ATTR_CONNECTION_STATUS from PDO_ODBC, which supports none of them, so
  it threw on the first of these. The new one uses SQLite, catches the
  PDOException for unsupported attributes and shows its output.

Checked against ext/pdo/pdo_dbh.c and the get_attribute handlers of the
bundled drivers, and by running the example on PHP 8.4.

Fixes php#4436
Comment on lines +81 to +83
a failure is best detected with the
<constant>PDO::ERRMODE_EXCEPTION</constant> error mode, or by checking
<methodname>PDO::errorCode</methodname>.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, that's not entirely true. It can return false without setting an error message.

Comment on lines +90 to +91
An error with SQLSTATE <literal>IM001</literal> is raised if the driver
does not support retrieving <parameter>attribute</parameter>.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't include it here, though. This is pretty much an implementation detail and it's not guaranteed. By stating this information in the manual we are enforcing on implementations that this has to be true.

Comment on lines +54 to +55
All other attributes are handled by the driver, and which of them
can be retrieved varies from driver to driver.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a run-on sentence and would be better split up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rephrase that following generic PDO attributes may not be implemented

2 participants