Steve Simms

NAME

DBIx::SimpleQuery - Query databases using as little code as possible

SYNOPSIS

  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";
  }
  

DESCRIPTION

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.

Methods

  • new([%connection_defaults]);

    Returns a DBIx::SimpleQuery object that allows you to reuse the same connection properties in multiple queries.

    If you pass a hash containing keys "dsn", "user", and "password", these values will be used when making the database connection.

  • qs($string)

    Return a quoted and escaped version of $string. Whenever possible, it will use the appropriate DBD module's escape function. If that is not available, it replaces single quotes with two single quotes (s{'}{''}g).

  • query($query), $sql->query($query), query($query, %parameters)

    Connect to the database and execute $query. If only one row and column are returned by the query, it will returned to the calling function. If only one column is returned, but there are multiple rows, it will return an array of the values of that column. Otherwise, a DBIx::SimpleQuery::Object will be returned containing the results of the query.

    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)

    Sets the default connection parameters for calls to query() and qs(). %connection_defaults should contain keys "dsn", "user", and "password". See the synopsis for an example.

  • get_dsn()

  • get_user()

  • get_password()

    Return the values that SimpleQuery intends to use for each parameter.

AUTHOR

Steve Simms (ssimms@cpan.org)

COPYRIGHT

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.

SEE ALSO

DBI DBIx::SimpleQuery::Object