SQL-Interpolate

--Simplified interpolation of variables into SQL statements (Perl module)

ABSTRACT

SQL-Interpolate is a collection of Perl modules that simplify the interpolatation of Perl variables into SQL statements. SQL::Interpolate converts a list of intermixed SQL fragments and variables into a conventional SQL string and list of bind values, which can be used directly or passed onto DBI.

EXAMPLE SYNTAX

  # SQL::Interpolate
  use SQL::Interpolate qw(:all);
  my @colors = ('blue', 'green');
  my($sql, @bind) = sql_interp
      q[SELECT * FROM mytable WHERE color IN], \@colors,
      q[AND y =], \$x, q[OR], {z => 3, w => 2} ;
  # Result:
  #   $sql = "SELECT * FROM mytable WHERE color IN (?, ?) " .
  #          "AND y = ? OR (z = ? AND w = ?)";
  #   @bind = ('blue', 'green', $x, 3, 2);
  # Passing any of the above results to DBI
  $dbh->selectall_arrayref($sql, undef, @bind);

  # DBIx::Interpolate
  use DBIx::Interpolate qw(:all);
  my $dbx = new DBIx::Interpolate($dbh);
  $dbx->selectall_arrayref(
      q[SELECT * FROM table WHERE color IN], \@colors, q[AND y =], \$x
  );

  # SQL::Interpolate::Macro
  use SQL::Interpolate::Macro qw(:all);
  sql_interp
      q[SELECT * FROM mytable WHERE],
      sql_and( sql_if($blue,  q[color = "blue"]),
               sql_if($shape, sql_fragment(q[shape =], \$shape)) ),
      q[LIMIT 10];

  # SQL::Interpolate::Filter
  use SQL::Interpolate FILTER => 1, qw(:all);
  ($sql, @bind) = sql_interp sql[
      SELECT * FROM mytable WHERE color IN @colors
      AND y = $x OR {z => 3, w => 2}
  ];

Related Links

Copyright (c) 2003-2005 David Manura. This module is licensed under the same terms as Perl itself.

Contact me for bug reports.