Skip to content

Commit

Permalink
Item11808: perltidy them all, so people are not accidentally impacted…
Browse files Browse the repository at this point in the history
… by the new enforced rule -- Sorry Micha, could not find a way to define specific values within the file

git-svn-id: http://svn.foswiki.org/trunk/DBIStoreContrib@14686 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
OlivierRaginel authored and OlivierRaginel committed May 4, 2012
1 parent 7e7e2ab commit 29dd1cb
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 158 deletions.
5 changes: 3 additions & 2 deletions lib/Foswiki/Contrib/DBIStoreContrib.pm
Expand Up @@ -3,9 +3,10 @@ package Foswiki::Contrib::DBIStoreContrib;

use strict;

our $VERSION = '$Rev$'; # version of *this file*.
our $VERSION = '$Rev$'; # version of *this file*.

our $RELEASE = '1.0';

our $SHORTDESCRIPTION = '(Experimental) use of DBI to implement an SQL query search';
our $SHORTDESCRIPTION =
'(Experimental) use of DBI to implement an SQL query search';

117 changes: 66 additions & 51 deletions lib/Foswiki/Contrib/DBIStoreContrib/HoistSQL.pm
Expand Up @@ -62,13 +62,14 @@ use constant {
};

BEGIN {

# Foswiki 1.1 doesn't have makeConstant; monkey-patch it
unless (defined &Foswiki::Infix::Node::makeConstant) {
*Foswiki::Infix::Node::makeConstant = sub {
my ($this, $type, $val) = @_;
$this->{op} = $type;
$this->{params} = [ $val ];
}
unless ( defined &Foswiki::Infix::Node::makeConstant ) {
*Foswiki::Infix::Node::makeConstant = sub {
my ( $this, $type, $val ) = @_;
$this->{op} = $type;
$this->{params} = [$val];
}
}
}

Expand All @@ -89,7 +90,7 @@ the hoisted expressions with constants.
=cut

sub hoist {
my ($node, $indent) = @_;
my ( $node, $indent ) = @_;

return undef unless ref( $node->{op} );

Expand All @@ -104,25 +105,25 @@ sub hoist {
my $lhs = hoist( $node->{params}[0], "${indent}l" );
my $rhs = _hoistB( $node->{params}[1], "${indent}r" );
if ( $lhs && $rhs ) {
$node->makeConstant(NUMBER, 1);
$node->makeConstant( NUMBER, 1 );
print STDERR "${indent}L&R\n" if MONITOR;
return "($lhs) AND ($rhs)";
}
elsif ( $lhs ) {
$node->{params}[0]->makeConstant(NUMBER, 1);
elsif ($lhs) {
$node->{params}[0]->makeConstant( NUMBER, 1 );
print STDERR "${indent}L\n" if MONITOR;
return $lhs;
}
elsif ($rhs) {
$node->{params}[1]->makeConstant(NUMBER, 1);
$node->{params}[1]->makeConstant( NUMBER, 1 );
print STDERR "${indent}R\n" if MONITOR;
return $rhs;
}
}
else {
my $or = _hoistB($node, "${indent}|");
my $or = _hoistB( $node, "${indent}|" );
if ($or) {
$node->makeConstant(NUMBER, 1);
$node->makeConstant( NUMBER, 1 );
return $or;
}
}
Expand All @@ -132,7 +133,7 @@ sub hoist {
}

sub _hoistB {
my ($node, $indent) = @_;
my ( $node, $indent ) = @_;

return unless ref( $node->{op} );

Expand All @@ -151,34 +152,35 @@ sub _hoistB {
}
}
else {
return _hoistC($node, "${indent}|", 0);
return _hoistC( $node, "${indent}|", 0 );
}

return undef;
}

sub _hoistC {
my ($node, $indent, $negated) = @_;
my ( $node, $indent, $negated ) = @_;

return undef unless ref( $node->{op} );

my $op = $node->{op}->{name};
my $op = $node->{op}->{name};
if ( $op eq '(' ) {
return _hoistC( $node->{params}[0], "${indent}(", $negated );
}

print STDERR "${indent}EQ ", $node->stringify(), "\n" if MONITOR;
my ($lhs, $rhs, $table, $test);
my ( $lhs, $rhs, $table, $test );

if ( $op eq 'not' ) {
return _hoistC( $node->{params}[0], "${indent}(", !$negated );
}
elsif ( $op eq '=' || $op eq '!=' ) {
($lhs, $table) = _hoistValue( $node->{params}[0], "${indent}l" );
( $lhs, $table ) = _hoistValue( $node->{params}[0], "${indent}l" );
$rhs = _hoistConstant( $node->{params}[1] );
if ( !$lhs || !$rhs ) {

# = and != are symmetric, so try the other order
($lhs, $table) = _hoistValue( $node->{params}[1], "${indent}r" );
( $lhs, $table ) = _hoistValue( $node->{params}[1], "${indent}r" );
$rhs = _hoistConstant( $node->{params}[0] );
}
if ( $lhs && $rhs ) {
Expand All @@ -188,12 +190,12 @@ sub _hoistC {
}
}
elsif ( $op eq '~' ) {
($lhs, $table) = _hoistValue( $node->{params}[0], "${indent}l" );
( $lhs, $table ) = _hoistValue( $node->{params}[0], "${indent}l" );
$rhs = _hoistConstant( $node->{params}[1] );
if ( $lhs && $rhs ) {
my $escape = '';
$rhs = quotemeta($rhs);
if ($rhs =~ /'/) {
if ( $rhs =~ /'/ ) {
$rhs =~ s/([s'])/s$1/g;
$escape = " ESCAPE 's'";
}
Expand All @@ -205,11 +207,11 @@ sub _hoistC {
}
}
elsif ( $op eq '=~' ) {
($lhs, $table) = _hoistValue( $node->{params}[0], "${indent}l" );
( $lhs, $table ) = _hoistValue( $node->{params}[0], "${indent}l" );
$rhs = _hoistConstant( $node->{params}[1] );
if ( $lhs && $rhs ) {
my $escape = '';
if ($rhs =~ /'/) {
if ( $rhs =~ /'/ ) {
$rhs =~ s/([s'])/s$1/g;
$escape = " ESCAPE 's'";
}
Expand All @@ -218,13 +220,15 @@ sub _hoistC {
$test = "NOT($test)" if $negated;
}
}
if ($table && $test) {
if ($table ne 'topic') {
if ( $table && $test ) {
if ( $table ne 'topic' ) {

# Have to use an EXISTS if the sub-test refers to another table
return <<SQL;
EXISTS(SELECT * FROM $table WHERE $table.tid=topic.tid AND $test)
SQL
} else {
}
else {
return $test;
}
}
Expand All @@ -239,33 +243,39 @@ SQL
# <rootfield> may be aliased
# Returns a partial SQL statement that can be followed by a condition for
# testing the value.
# A limited set of functions - UPPER, LOWER,
# A limited set of functions - UPPER, LOWER,
sub _hoistValue {
my ($node, $indent) = @_;
my $op = ref( $node->{op}) ? $node->{op}->{name} : '';
my ( $node, $indent ) = @_;
my $op = ref( $node->{op} ) ? $node->{op}->{name} : '';

print STDERR "${indent}V ", $node->stringify(), "\n" if MONITOR;

if ( $op eq '(' ) {
return _hoistValue( $node->{params}[0] );
}

if ( $op eq 'lc' ) {
my ($lhs, $table) = _hoistValue( $node->{params}[0], "${indent}$op" );
return ("LOWER($lhs)", $table) if $lhs;
} elsif ( $op eq 'uc' ) {
my ($lhs, $table) = _hoistValue( $node->{params}[0], "${indent}$op" );
return ("UPPER($lhs)", $table) if $lhs;
} elsif ( $op eq 'length' ) {
my ( $lhs, $table ) = _hoistValue( $node->{params}[0], "${indent}$op" );
return ( "LOWER($lhs)", $table ) if $lhs;
}
elsif ( $op eq 'uc' ) {
my ( $lhs, $table ) = _hoistValue( $node->{params}[0], "${indent}$op" );
return ( "UPPER($lhs)", $table ) if $lhs;
}
elsif ( $op eq 'length' ) {

# This is slightly risky, because 'length' also works on array
# values, but SQL LEN only works on text values.
my ($lhs, $table) = _hoistValue( $node->{params}[0], "${indent}$op" );
return ("LENGTH($lhs)", $table) if $lhs;
} elsif ( $op eq '.' ) {
my ( $lhs, $table ) = _hoistValue( $node->{params}[0], "${indent}$op" );
return ( "LENGTH($lhs)", $table ) if $lhs;
}
elsif ( $op eq '.' ) {
my $lhs = $node->{params}[0];
my $rhs = $node->{params}[1];
if ( !ref( $lhs->{op} ) && !ref( $rhs->{op} )
&& $lhs->{op} == NAME && $rhs->{op} == NAME )
if ( !ref( $lhs->{op} )
&& !ref( $rhs->{op} )
&& $lhs->{op} == NAME
&& $rhs->{op} == NAME )
{
$lhs = $lhs->{params}[0];
$rhs = $rhs->{params}[0];
Expand All @@ -274,39 +284,44 @@ sub _hoistValue {
}
if ( $lhs =~ /^META:(\w+)/ ) {

return ("$1.$rhs", $1);
return ( "$1.$rhs", $1 );
}

if ( $rhs eq 'text' ) {

# Special case for the text body
return ('topic.text', 'topic');
return ( 'topic.text', 'topic' );
}

if ( $rhs eq 'raw' ) {

# Special case for the text body
return ('topic.raw', 'topic');
return ( 'topic.raw', 'topic' );
}

# Otherwise assume the term before the dot is the form name
return ("EXISTS(SELECT * FROM FORM WHERE FORM.tid=topic.tid AND FORM.name='$lhs') AND FIELD.name='$rhs' AND FIELD.value",
"FIELD")
return (
"EXISTS(SELECT * FROM FORM WHERE FORM.tid=topic.tid AND FORM.name='$lhs') AND FIELD.name='$rhs' AND FIELD.value",
"FIELD"
);
}
}
elsif ( !ref( $node->{op} ) && $node->{op} == NAME ) {

# A simple name
if ( $node->{params}[0] =~ /^(name|web|text|raw)$/ ) {

# Special case for the topic name, web or text body
return ("topic.$1", 'topic');
return ( "topic.$1", 'topic' );
}
else {
return ("FIELD.name='$node->{params}[0]' AND FIELD.value",
'FIELD');
return ( "FIELD.name='$node->{params}[0]' AND FIELD.value",
'FIELD' );
}
}

print STDERR "\tFAILED\n" if MONITOR;
return (undef, undef);
return ( undef, undef );
}

# Expecting a constant
Expand Down
32 changes: 19 additions & 13 deletions lib/Foswiki/Plugins/DBIStorePlugin.pm
Expand Up @@ -6,16 +6,17 @@ package Foswiki::Plugins::DBIStorePlugin;

use Foswiki::Contrib::DBIStoreContrib ();

our $VERSION = $Foswiki::Plugins::DBIStoreContrib::VERSION;
our $RELEASE = $Foswiki::Plugins::DBIStoreContrib::RELEASE;
our $VERSION = $Foswiki::Plugins::DBIStoreContrib::VERSION;
our $RELEASE = $Foswiki::Plugins::DBIStoreContrib::RELEASE;
our $NO_PREFS_IN_TOPIC = 1;

our $listener;

sub initPlugin {
if (defined &Foswiki::Store::tellListeners) {
# Will not enable this plugin if tellListeners is present
return 0;
if ( defined &Foswiki::Store::tellListeners ) {

# Will not enable this plugin if tellListeners is present
return 0;
}
require Foswiki::Contrib::DBIStoreContrib::Listener;
$listener = Foswiki::Contrib::DBIStoreContrib::Listener->new();
Expand All @@ -24,10 +25,11 @@ sub initPlugin {
# If the getField method is missing, then get it from the BruteForce
# module that it was moved from.
require Foswiki::Store::QueryAlgorithms::DBIStoreContrib;
unless (Foswiki::Store::QueryAlgorithms::DBIStoreContrib->can('getField')) {
require Foswiki::Store::QueryAlgorithms::BruteForce;
*Foswiki::Store::QueryAlgorithms::DBIStoreContrib::getField =
\&Foswiki::Store::QueryAlgorithms::BruteForce::getField;
unless ( Foswiki::Store::QueryAlgorithms::DBIStoreContrib->can('getField') )
{
require Foswiki::Store::QueryAlgorithms::BruteForce;
*Foswiki::Store::QueryAlgorithms::DBIStoreContrib::getField =
\&Foswiki::Store::QueryAlgorithms::BruteForce::getField;
}
print STDERR "Constructed listener\n";
return 1;
Expand All @@ -48,16 +50,20 @@ sub initPlugin {

# Required for most save operations
sub afterSaveHandler {

# $text, $topic, $web, $error, $meta
$listener->update($_[4]);
$listener->update( $_[4] );
}

# Required for a web or topic move
sub afterRenameHandler {

# $oldWeb, $oldTopic, $oldAttachment, $newWeb, $newTopic, $newAttachment
my $old = new Foswiki::Meta($Foswiki::Plugins::SESSION, $oldWeb, $oldTopic);
my $new = new Foswiki::Meta($Foswiki::Plugins::SESSION, $newWeb, $newTopic);
$listener->update($old, $new);
my $old =
new Foswiki::Meta( $Foswiki::Plugins::SESSION, $oldWeb, $oldTopic );
my $new =
new Foswiki::Meta( $Foswiki::Plugins::SESSION, $newWeb, $newTopic );
$listener->update( $old, $new );
}

1;

0 comments on commit 29dd1cb

Please sign in to comment.