Skip to content

Commit

Permalink
Item9750: numbers work ok, but I broke regex's somewhere - m2 yay
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/MongoDBPlugin@10667 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
SvenDowideit authored and SvenDowideit committed Feb 8, 2011
1 parent 49b2f8e commit db231fd
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
31 changes: 22 additions & 9 deletions lib/Foswiki/Plugins/MongoDBPlugin/HoistMongoDB.pm
Expand Up @@ -22,8 +22,8 @@ use Assert;

use Foswiki::Query::HoistREs ();

use constant MONITOR => 1;
use constant WATCH => 1;
use constant MONITOR => 0;
use constant WATCH => 0;

=begin TML
Expand Down Expand Up @@ -184,7 +184,7 @@ sub _hoist {
( ref( $node->{op} ) eq 'Foswiki::Query::OP_pos' ) or
( ref( $node->{op} ) eq 'Foswiki::Query::OP_neg' )
) {
print STDERR "POPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOP\n";
#print STDERR "POPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOP\n";
return $node->{op}->hoistMongoDB($node);

} else {
Expand Down Expand Up @@ -317,7 +317,7 @@ sub convertFunction {
}
die "$key and $value is not a string? " if (ref($value) ne '');
die "$key is not in the js_func_map" if (not defined($js_func_map{$key}));
print STDERR "\t\tconvertfunction($value, $key) => \n";
print STDERR "\t\tconvertfunction($value, $key) => \n" if MONITOR;
return convertStringToJS($value) . $js_func_map{$key};
}

Expand All @@ -327,11 +327,15 @@ my $ops = '(' . join( '|', values(%js_op_map) ) . ')';
sub convertStringToJS {
my $string = shift;
print STDERR " convertStringToJS($string)\n" if MONITOR;
die 'here' if ($string eq '<');

return $string if ($string =~ /^\(.*\)$/); #if we're doing braces, and they're not quoted leave things be

return convertToJavascript($string) if ( ref($string) eq 'HASH' );

return $string if ( $string =~ /'.*'/ );
return $string if ( $string =~ /^'.*'^/ );
#TODO: i _think_ the line below is ok, its needed to make ::test_hoistLengthLHSString work
return $string if ( $string =~ /^\'.*/ );

return $string if ( $string =~ /^this\./ );

# all registered meta type prefixes use a this. in js
Expand All @@ -350,6 +354,10 @@ sub convertStringToJS {
return 'this.' . $string if ( $string eq '_web' );
return 'this.' . $string if ( $string eq '_topic' );
return 'this.' . $string if ( $string eq '_text' );

#if it looks like a number, lets try treating it like a number, and see what happens
#i _think_ this will result in js doing magic just like perl does, as the main diff seems to be that Perl('1'+'1'=2) and JS('1'+'1'='11')
return $string if ($string =~ /^[+-]?\d+(\.\d*)?$/);

return '\'' . $string . '\'';
}
Expand Down Expand Up @@ -407,8 +415,13 @@ sub convertToJavascript {

$statement .= convertFunction( $value, $key );
}
elsif ($k eq '#match') {
#this is essentially an operator lookahead
print STDERR ">>>>>>>>>>>>>>>>>>>> lookahead $key -> $k\n" if MONITOR;
$statement .=
convertToJavascript($value).'('.convertStringToJS($js_key).')';
}
elsif (
($k eq '#match') or
($k eq '#like') or
( $k eq '#div') or
( $k eq '#mult') or
Expand All @@ -418,7 +431,7 @@ sub convertToJavascript {
( $k eq '#neg')
) {
#this is essentially an operator lookahead
print STDERR ">>>>>>>>>>>>>>>>>>>> lookahead $key -> $k\n";
print STDERR ">>>>>>>>>>>>>>>>>>>> lookahead $key -> $k\n" if MONITOR;
$statement .= ($js_key) . ' ' . convertStringToJS($value);
#$statement .= convertFunction( convertToJavascript($value), $key );
# $statement .=
Expand Down Expand Up @@ -485,7 +498,7 @@ print STDERR ">>>>>>>>>>>>>>>>>>>> lookahead $key -> $k\n";

}
elsif ( $key =~ /^\#/ ) {
print STDERR ">>>>>>>>>>>>>>>>>>>> #hash - $key \n";
print STDERR ">>>>>>>>>>>>>>>>>>>> #hash - $key \n" if MONITOR;

$statement .= convertFunction( $value, $key );
}
Expand Down
52 changes: 48 additions & 4 deletions test/unit/HoistMongoDBsTests.pm
Expand Up @@ -778,7 +778,7 @@ sub test_hoistLengthLHSName {

$this->do_Assert( $query, $mongoDBQuery,
{
'$where' => 'this._topic.length == \'12\''
'$where' => 'this._topic.length == 12'
}
);
}
Expand All @@ -792,7 +792,7 @@ sub test_hoistLengthLHSString {

$this->do_Assert( $query, $mongoDBQuery,
{
'$where' => '\'something\'.length == \'9\''
'$where' => '\'something\'.length == 9'
}
);
}
Expand All @@ -807,7 +807,7 @@ sub test_hoistLengthLHSNameGT {

$this->do_Assert( $query, $mongoDBQuery,
{
'$where' => 'this._topic.length < \'12\''
'$where' => 'this._topic.length < 12'
}
);
}
Expand Down Expand Up @@ -882,7 +882,51 @@ sub test_hoist_maths {

$this->do_Assert( $query, $mongoDBQuery,
{
'$where' => ' ((\'12\')-(this.FIELD.Namespace.value) < (((\'24\')*(\'60\'))*(\'60\'))-(\'5\')) && ((this.FIELD.TermGroup.value)/(\'12\') > (this.FIELD.WebScale.value)*(\'42.8\')) '
'$where' => ' ((12)-(this.FIELD.Namespace.value) < (((24)*(60))*(60))-(5)) && ((this.FIELD.TermGroup.value)/(12) > (this.FIELD.WebScale.value)*(42.8)) '
}
);
}

sub test_hoist_concat {
my $this = shift;
my $s = "'asd' + 'qwe' = 'asdqwe'";
my $queryParser = new Foswiki::Query::Parser();
my $query = $queryParser->parse($s);
my $mongoDBQuery =
Foswiki::Plugins::MongoDBPlugin::HoistMongoDB::hoist($query);

$this->do_Assert( $query, $mongoDBQuery,
{
'$where' => '(\'asd\')+(\'qwe\') == \'asdqwe\''
}
);
}
#this one is a nasty perler-ism
sub test_hoist_concat2 {
my $this = shift;
my $s = "'2' + '3' = '5'";
my $queryParser = new Foswiki::Query::Parser();
my $query = $queryParser->parse($s);
my $mongoDBQuery =
Foswiki::Plugins::MongoDBPlugin::HoistMongoDB::hoist($query);

$this->do_Assert( $query, $mongoDBQuery,
{
'$where' => '(2)+(3) == 5'
}
);
}
sub test_hoist_concat3 {
my $this = shift;
my $s = "2 + 3 = 5";
my $queryParser = new Foswiki::Query::Parser();
my $query = $queryParser->parse($s);
my $mongoDBQuery =
Foswiki::Plugins::MongoDBPlugin::HoistMongoDB::hoist($query);

$this->do_Assert( $query, $mongoDBQuery,
{
'$where' => '(2)+(3) == 5'
}
);
}
Expand Down

0 comments on commit db231fd

Please sign in to comment.