perl5-dbi/dbi

Please make DBI::Const::GetInfoType and %GetInfoType hash public

pali opened this issue · 9 comments

pali commented

%GetInfoType hash from the DBI::Const::GetInfoType package provides mapping from string identifiers to DBI get_info numeric values. But documentation says this package is "new" and "nothing what is written here is guaranteed". Which means that it is not ready for public API usage and for stable applications.

Package DBI::Const::GetInfo::ODBC is even more strict and its documentation contains: "The API for this module is private and subject to change.". So is not for public usage too.

I would really like to use human readable identifiers (e.g. SQL_DBMS_VER) instead of magic number 18.

So can you please make current API of the %GetInfoType hash public and reflect it into DBI::Const::GetInfoType documentation?

E.g. code:

my $database_name = $dbh->get_info($GetInfoType{SQL_DBMS_NAME});

is more readable as:

my $database_name = $dbh->get_info(17);

I agree with the general need. I've wanted the same myself before.

Rather than exporting the hash, and leaving people at risk of typos, I'd prefer a solution that lets code declare the constants that are needed. Something like:

use ...mumble... qw($SQL_DBMS_NAME);

my $database_name = $dbh->get_info($SQL_DBMS_NAME);

Adding a trivial import() method could enable that.

pali commented

Or those numbers can be contant perl functions (not variables) like for SQL_INTEGER, SQL_DOUBLE which are exported by :sql_types.

Yes, that would be more consistent.

pali commented

So what about use DBI qw(:sql_info_types) to export all those constant functions like SQL_DBMS_NAME and then use it as my $database_name = $dbh->get_info(SQL_DBMS_NAME);?

I don't think that's worth the cost of supporting in the DBI package itself given there there are many values and few would be used.

It should be easy enough to add to DBI::Const::GetInfoType though:

use DBI::Const::GetInfoType qw(SQL_DBMS_NAME);

by adding an import method into lib/DBI/Const/GetInfoType.pm

pali commented

Usage use DBI::Const::GetInfoType qw(SQL_DBMS_NAME); should be enough... Will you prepare this extension for import method together with updating documentation?

pali commented

Anyway, DBI documentation already says to use %GetInfoType hash:

https://metacpan.org/pod/DBI#get_info

The DBI::Const::GetInfoType module exports a %GetInfoType hash that can be used to map info type names to numbers. For example:

$database_version = $dbh->get_info( $GetInfoType{SQL_DBMS_VER} );

The names are a merging of the ANSI and ODBC standards (which differ in some cases). See DBI::Const::GetInfoType for more details.

pali commented

@timbunce Are you going to add import method for lib/DBI/Const/GetInfoType.pm?

I'd welcome a PR.