| DBIx::SimpleQuery - Query databases using as little code as possible |
DBIx::SimpleQuery - Query databases using as little code as possible
use DBIx::SimpleQuery;
DBIx::SimpleQuery::set_defaults(
"dsn" => "DBI:Pg:test_database",
"user" => "test_user",
"password" => "test_password",
);
sub get_name {
my $user_id = qs(shift());
return query "SELECT name FROM users WHERE user_id = $user_id";
}
print get_name("mo'connor") . "\n";
foreach my $name (query "SELECT name FROM users ORDER BY name") {
print $name . "\n";
}
foreach my $row (query "SELECT user_id, name FROM users") {
print $row->{"user_id"} . "\t" . $row->{"name"} . "\n";
}
DBIx::SimpleQuery is designed for anyone who wants to run specific SQL commands against a database with as little surrounding structure as possible.
It exports two functions, query and qs (quote-string), which allow you to include SQL in your code without needing to deal with database handlers, DSNs, and the like.
qs() escapes a string and surrounds it with single quotes, so that it
can be safely embedded in a query. Whenever possible, it uses the
appropriate DBD module to do this.
query() runs one or more queries against a database, and returns a
value or a structure that can be evaluated in a number of ways,
depending on your need.
Multi-word functions may be called either in function_name or functionName form, as per your preference.
If you pass a hash containing keys ``dsn'', ``user'', and ``password'', these values will be used when making the database connection.
qs($string)If you pass in connection parameters (see the new method above), they will be used instead of the defaults for this query.
set_defaults(%connection_defaults)query() and qs().
%connection_defaults should contain keys ``dsn'', ``user'', and
``password''. See the synopsis for an example.
get_dsn()get_user()get_password()
Steve Simms (ssimms@cpan.org)
Copyright 2004 Steve Simms. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
DBI DBIx::SimpleQuery::Object
| DBIx::SimpleQuery - Query databases using as little code as possible |