-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
List databases #50
base: master
Are you sure you want to change the base?
List databases #50
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -559,26 +559,62 @@ sub server_version { | |
} | ||
|
||
|
||
=head2 list_dbs([$dbname]) | ||
=head2 list_dbs([$dbname],[$excludes]) | ||
|
||
Returns a list of db names. | ||
|
||
When a database name is specified, uses that database to connect to, | ||
using the credentials specified in the instance. | ||
|
||
If no database name is specified, 'template1' is used. | ||
If an array of excluded databases is provided, it is used to limit the list | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. But what is the reason we want this? I mean Perl has a nice construct with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The latest commit does address the SQL injection, thanks. That leaves the above question (why is it necessary to do this at the query level) though. |
||
|
||
=cut | ||
|
||
sub list_dbs { | ||
my $self = shift; | ||
my $dbname = (shift @_) || 'template1'; | ||
my $dbname = shift || 'template1'; | ||
my @excludes = @_; | ||
|
||
return map { $_->[0] } | ||
@{ __PACKAGE__->new($self->export, (dbname => $dbname)) | ||
->connect->selectall_arrayref( | ||
'SELECT datname from pg_database | ||
WHERE datallowconn | ||
AND datname NOT IN (?) | ||
ORDER BY datname', | ||
{},\@excludes) | ||
}; | ||
} | ||
|
||
=head2 list_dbs_this_user($user,[$dbname],[$excludes]) | ||
|
||
Returns a list of db names. | ||
|
||
When a database name is specified, uses that database to connect to, | ||
using the credentials specified in the instance. | ||
|
||
If no database name is specified, 'template1' is used. | ||
If an array of excluded databases is provided, it is used to limit the list | ||
|
||
=cut | ||
|
||
sub list_dbs_this_user { | ||
my $self = shift; | ||
my $user = shift; | ||
my $dbname = shift || 'template1'; | ||
my @excludes = @_; | ||
|
||
return map { $_->[0] } | ||
@{ __PACKAGE__->new($self->export, (dbname => $dbname) | ||
)->connect->selectall_arrayref( | ||
'SELECT datname from pg_database order by datname' | ||
) }; | ||
@{ __PACKAGE__->new($self->export, (dbname => $dbname)) | ||
->connect->selectall_arrayref( | ||
"SELECT datname from pg_database | ||
WHERE datdba=(SELECT usesysid FROM pg_user WHERE usename='$user') | ||
AND datallowconn | ||
AND datname NOT IN (?) | ||
ORDER BY datname", | ||
{},\@excludes) | ||
}; | ||
} | ||
|
||
=head2 create | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implies
my $excludes = shift
with a list of excludes passed as an arrayref. The implementation below takes all remaining arguments as excludes. I like this definition better than the implementation, because the latter does not allow further extension of the API.