Luke Ross

DBIx::InterpolationBinding

5 releases git clone https://lukeross.name/projects/dbix-interpolationbinding.git/

Perl extension for turning perl double-quote string interpolation into DBI bind parameters.

Commit 3b3f4e6241d2f07032cc505e678588a9a43a6acf

Initial revision of new project git-svn-id: file:///home/lukeross/ToDo/code-subversion/perl-SQL-InterpolationBinding@16 3208c2a6-1f24-0410-bbd8-cb17f2cc2c65

Committed 16 Dec 2006 by lukeross

Changes

@@ -0,0 +1,6 @@
+Revision history for Perl extension SQL::InterpolationBinding.
+
+0.01  Sat Dec 16 08:47:52 2006
+	- original version; created by h2xs 1.23 with options
+		-b 5.0.0 -X -n SQL::InterpolationBinding
+


MANIFEST

@@ -0,0 +1,6 @@
+Changes
+Makefile.PL
+MANIFEST
+README
+t/SQL-InterpolationBinding.t
+lib/SQL/InterpolationBinding.pm


Makefile.PL

@@ -0,0 +1,12 @@
+use 5.000;
+use ExtUtils::MakeMaker;
+# See lib/ExtUtils/MakeMaker.pm for details of how to influence
+# the contents of the Makefile that is written.
+WriteMakefile(
+    NAME              => 'SQL::InterpolationBinding',
+    VERSION_FROM      => 'lib/SQL/InterpolationBinding.pm', # finds $VERSION
+    PREREQ_PM         => {}, # e.g., Module::Name => 1.1
+    ($] >= 5.005 ?     ## Add these new keywords supported since 5.005
+      (ABSTRACT_FROM  => 'lib/SQL/InterpolationBinding.pm', # retrieve abstract from module
+       AUTHOR         => 'Luke Ross <lukeross@localdomain>') : ()),
+);


README

@@ -0,0 +1,40 @@
+SQL-InterpolationBinding version 0.01
+=====================================
+
+The README is used to introduce the module and provide instructions on
+how to install the module, any machine dependencies it may have (for
+example C compilers and installed libraries) and any other information
+that should be provided before the module is installed.
+
+A README file is required for CPAN modules since CPAN extracts the
+README file from a module distribution so that people browsing the
+archive can use it get an idea of the modules uses. It is usually a
+good idea to provide version information here so that people can
+decide whether fixes for the module are worth downloading.
+
+INSTALLATION
+
+To install this module type the following:
+
+   perl Makefile.PL
+   make
+   make test
+   make install
+
+DEPENDENCIES
+
+This module requires these other modules and libraries:
+
+  blah blah blah
+
+COPYRIGHT AND LICENCE
+
+Put the correct copyright and licence information here.
+
+Copyright (C) 2006 by Luke Ross
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself, either Perl version 5.8.5 or,
+at your option, any later version of Perl 5 you may have available.
+
+


lib/SQL/InterpolationBinding.pm

@@ -0,0 +1,137 @@
+#!/usr/bin/perl -w
+
+package SQL::InterpolationBinding;
+
+use 5.005;
+use strict;
+use vars qw($VERSION @ISA @EXPORT $DEBUG);
+
+use overload	'""'	=> \&_convert_object_to_string,
+		'.'	=> \&_append_item_to_object;
+
+require Exporter;
+@ISA = qw(Exporter);
+@EXPORT = qw(dbi_exec);
+
+$VERSION = '0.01';
+
+$DEBUG = 0;
+
+sub import {
+	overload::constant 'q' => \&_prepare_object_from_string;
+	SQL::InterpolationBinding->export_to_level(1, @_);
+}
+
+sub dbi_exec {
+	my $dbi = shift;
+	my $sql = shift;
+
+	if (ref $sql) {
+		# We have a fake string
+		unshift @_, @{ $sql->{bind_params} };
+		$sql = $sql->{sql_string}
+	}
+
+	print "prepare($sql)\nexecute(@_)\n" if $DEBUG;
+	my $sth = $dbi->prepare($sql) or return;
+	$sth->execute($sql, @_) or return;
+	return $sth;
+}
+
+sub _prepare_object_from_string {
+	my (undef, $string, $mode) = @_;
+	return $string unless ($mode eq "qq");
+	my $self = {
+		string => $string,
+		sql_string => $string,
+		bind_params => [ ]
+	};
+	return bless $self, "SQL::InterpolationBinding";
+}
+
+sub _convert_object_to_string {
+	my $self = shift;
+	return $self->{string};
+}
+
+sub _append_item_to_object {
+	my ($self, $string, $flipped) = @_;
+
+	my $new_hash = { %$self };
+	if (ref $string) {
+		# We're adding another constant
+		if ($flipped) {
+			$new_hash->{sql_string} = $string->{sql_string} . $new_hash->{sql_string};
+			$new_hash->{string} = $string->{string} . $new_hash->{string};
+			unshift @{ $new_hash->{bind_params} }, @{ $string->{bind_params} };
+		} else {
+			$new_hash->{sql_string} .= $string->{sql_string};
+			$new_hash->{string} .= $string->{string};
+			push @{ $new_hash->{bind_params} }, @{ $string->{bind_params} };
+		}
+	} else {
+		# We're interpolating
+		if ($flipped) {
+			$new_hash->{sql_string} = "?" . $new_hash->{sql_string};
+			$new_hash->{string} = $string . $new_hash->{string};
+			unshift @{ $new_hash->{bind_params} }, $string;
+		} else {
+			$new_hash->{sql_string} .= "?";
+			$new_hash->{string} .= $string;
+			push @{ $new_hash->{bind_params} }, $string;
+		}
+	}
+	return bless $new_hash, ref($self);
+}
+
+1;
+__END__
+
+=head1 NAME
+
+SQL::InterpolationBinding - Perl extension for blah blah blah
+
+=head1 SYNOPSIS
+
+  use SQL::InterpolationBinding;
+  blah blah blah
+
+=head1 DESCRIPTION
+
+Stub documentation for SQL::InterpolationBinding, created by h2xs. It looks like the
+author of the extension was negligent enough to leave the stub
+unedited.
+
+Blah blah blah.
+
+=head2 EXPORT
+
+None by default.
+
+
+
+=head1 SEE ALSO
+
+Mention other useful documentation such as the documentation of
+related modules or operating system documentation (such as man pages
+in UNIX), or any relevant external documentation such as RFCs or
+standards.
+
+If you have a mailing list set up for your module, mention it here.
+
+If you have a web site set up for your module, mention it here.
+
+=head1 AUTHOR
+
+Luke Ross, E<lt>lukeross@localdomainE<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright (C) 2006 by Luke Ross
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself, either Perl version 5.8.5 or,
+at your option, any later version of Perl 5 you may have available.
+
+
+=cut


t/SQL-InterpolationBinding.t

@@ -0,0 +1,17 @@
+# Before `make install' is performed this script should be runnable with
+# `make test'. After `make install' it should work as `perl SQL-InterpolationBinding.t'
+
+#########################
+
+# change 'tests => 1' to 'tests => last_test_to_print';
+
+use Test;
+BEGIN { plan tests => 1 };
+use SQL::InterpolationBinding;
+ok(1); # If we made it this far, we're ok.
+
+#########################
+
+# Insert your test code below, the Test::More module is use()ed here so read
+# its man page ( perldoc Test::More ) for help writing this test script.
+