Adding Ben Simpson's new persistence layer.
This commit is contained in:
parent
af4efc0333
commit
d48579b177
16 changed files with 2127 additions and 56 deletions
101
lib/WebGUI/Persistent/Query/Delete.pm
Normal file
101
lib/WebGUI/Persistent/Query/Delete.pm
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
package WebGUI::Persistent::Query::Delete;
|
||||
|
||||
=head1 LEGAL
|
||||
|
||||
-------------------------------------------------------------------
|
||||
WebGUI is Copyright 2001-2003 Plain Black LLC.
|
||||
-------------------------------------------------------------------
|
||||
Please read the legal notices (docs/legal.txt) and the license
|
||||
(docs/license.txt) that came with this distribution before using
|
||||
this software.
|
||||
-------------------------------------------------------------------
|
||||
http://www.plainblack.com info@plainblack.com
|
||||
-------------------------------------------------------------------
|
||||
|
||||
=cut
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use WebGUI::Persistent::Query;
|
||||
|
||||
our @ISA = qw(WebGUI::Persistent::Query);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Package WebGUI::Persistent::Query::Insert
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
This class allows reliable dynamic building of Sql delete queries.
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
my $query = WebGUI::Persistent::Query::Insert->new(
|
||||
table => 'myTable',
|
||||
where => [A => [1,2],[{C => 'hello',B => 1}]],
|
||||
properties => {
|
||||
A => { },
|
||||
B => { },
|
||||
C => { quote => 1 },
|
||||
D => { quote => 1 },
|
||||
}
|
||||
);
|
||||
|
||||
$query->buildQuery();
|
||||
|
||||
Returns:
|
||||
|
||||
DELETE FROM myTable
|
||||
WHERE A IN (1,2) AND (C = 'hello' OR B = 1)
|
||||
|
||||
=cut
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 buildQuery
|
||||
|
||||
=cut
|
||||
|
||||
sub buildQuery {
|
||||
my ($self,%p) = @_;
|
||||
|
||||
my $query = 'DELETE FROM '.$self->{_table};
|
||||
if (my $where = $self->buildWhere()) {
|
||||
$query .= " $where";
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
=head2 new( %p )
|
||||
|
||||
=over
|
||||
|
||||
=item properties
|
||||
|
||||
A hashref of field name to a hash reference of property settings.
|
||||
|
||||
Currently used settings are:
|
||||
|
||||
=over
|
||||
|
||||
=item * quote
|
||||
|
||||
If true values for this field are automatically quoted.
|
||||
|
||||
=back
|
||||
|
||||
=item table
|
||||
|
||||
The name of the table to query.
|
||||
|
||||
=item where
|
||||
|
||||
A hash reference or array reference of arguments to build a where clause from.
|
||||
See parseWhereArgs for details.
|
||||
|
||||
=back
|
||||
|
||||
=cut
|
||||
|
||||
1;
|
||||
115
lib/WebGUI/Persistent/Query/Insert.pm
Normal file
115
lib/WebGUI/Persistent/Query/Insert.pm
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
package WebGUI::Persistent::Query::Insert;
|
||||
|
||||
=head1 LEGAL
|
||||
|
||||
-------------------------------------------------------------------
|
||||
WebGUI is Copyright 2001-2003 Plain Black LLC.
|
||||
-------------------------------------------------------------------
|
||||
Please read the legal notices (docs/legal.txt) and the license
|
||||
(docs/license.txt) that came with this distribution before using
|
||||
this software.
|
||||
-------------------------------------------------------------------
|
||||
http://www.plainblack.com info@plainblack.com
|
||||
-------------------------------------------------------------------
|
||||
|
||||
=cut
|
||||
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use WebGUI::Persistent::Query::Insert;
|
||||
|
||||
our @ISA = qw(WebGUI::Persistent::Query);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Package WebGUI::Persistent::Query::Insert
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
This class allows reliable dynamic building of Sql insert queries.
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
my $query = WebGUI::Persistent::Query::Insert->new(
|
||||
table => 'myTable',
|
||||
data => {
|
||||
A => 1,
|
||||
B => 2,
|
||||
C => 'hello',
|
||||
D => 'world'
|
||||
},
|
||||
properties => {
|
||||
A => { },
|
||||
B => { },
|
||||
C => { quote => 1 },
|
||||
D => { quote => 1 },
|
||||
}
|
||||
);
|
||||
|
||||
$query->buildQuery();
|
||||
|
||||
Returns:
|
||||
|
||||
INSERT INTO myTable (A,B,C,D) VALUES (1,2,'hello','world');
|
||||
|
||||
=cut
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
sub buildFieldValues {
|
||||
my ($self) = @_;
|
||||
|
||||
my @fields = keys %{$self->{_data}};
|
||||
my @values = map { $self->quote($_,$self->{_data}{$_})} @fields;
|
||||
|
||||
return "(".join(', ',@fields).") VALUES (".join(', ',@values).")";
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 buildQuery
|
||||
|
||||
=cut
|
||||
|
||||
sub buildQuery {
|
||||
my ($self) = @_;
|
||||
return join(' ','INSERT INTO',$self->{_table},$self->buildFieldValues());
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 new( %p )
|
||||
|
||||
=over
|
||||
|
||||
=item data
|
||||
|
||||
A hash reference of field name to value.
|
||||
|
||||
=item properties
|
||||
|
||||
=over
|
||||
|
||||
=item * quote
|
||||
|
||||
If true values for this field are automatically quoted.
|
||||
|
||||
=back
|
||||
|
||||
=item table
|
||||
|
||||
The name of the table to query.
|
||||
|
||||
=back
|
||||
|
||||
=cut
|
||||
|
||||
sub new {
|
||||
my ($class,%p) = @_;
|
||||
my $self = $class->SUPER::new(%p);
|
||||
$self->{_data} = $p{data} || {};
|
||||
return $self;
|
||||
}
|
||||
|
||||
1;
|
||||
171
lib/WebGUI/Persistent/Query/Select.pm
Normal file
171
lib/WebGUI/Persistent/Query/Select.pm
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
package WebGUI::Persistent::Query::Select;
|
||||
|
||||
=head1 LEGAL
|
||||
|
||||
-------------------------------------------------------------------
|
||||
WebGUI is Copyright 2001-2003 Plain Black LLC.
|
||||
-------------------------------------------------------------------
|
||||
Please read the legal notices (docs/legal.txt) and the license
|
||||
(docs/license.txt) that came with this distribution before using
|
||||
this software.
|
||||
-------------------------------------------------------------------
|
||||
http://www.plainblack.com info@plainblack.com
|
||||
-------------------------------------------------------------------
|
||||
|
||||
=cut
|
||||
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use WebGUI::Persistent::Query;
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Package WebGUI::Persistent::Query::Select
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
This class allows reliable dynamic building of Sql select queries.
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
my $query = WebGUI::Persistent::Query::Select->new(
|
||||
where => [A => [1,2],[{C => 'hello',B => 1}]],
|
||||
table => 'myTable',
|
||||
limit => 1,
|
||||
groupBy => 'D',
|
||||
properties => {
|
||||
A => { },
|
||||
B => { },
|
||||
C => { quote => 1 },
|
||||
D => { quote => 1 },
|
||||
}
|
||||
);
|
||||
|
||||
$query->buildQuery();
|
||||
|
||||
Returns:
|
||||
|
||||
SELECT A,B,C,D
|
||||
FROM myTable
|
||||
WHERE A IN (1,2) AND (C = 'hello' OR B = 1) LIMIT 1 GROUP BY D
|
||||
|
||||
=cut
|
||||
|
||||
our @ISA = qw(WebGUI::Persistent::Query);
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
sub buildFrom { "FROM ".$_[0]->{_table} }
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
sub buildGroupBy {
|
||||
my ($self) = @_;
|
||||
return '' unless $self->{_groupBy} && @{$self->{_groupBy}};
|
||||
return 'GROUP BY '.join(',',@{$self->{_groupBy}});
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
sub buildLimit { $_[0]->{_limit} ? "LIMIT ".$_[0]->{_limit} : '' }
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
sub buildOrderBy {
|
||||
my ($self) = @_;
|
||||
return '' unless $self->{_orderBy} && @{$self->{_orderBy}};
|
||||
return 'ORDER BY '.join(',',@{$self->{_orderBy}});
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 buildQuery
|
||||
|
||||
=cut
|
||||
|
||||
sub buildQuery {
|
||||
my ($self) = @_;
|
||||
|
||||
my @clauses = ('SELECT',
|
||||
$self->buildSelectFields(),
|
||||
$self->buildFrom());
|
||||
|
||||
if (my $where = $self->buildWhere()) {
|
||||
push @clauses,$where;
|
||||
}
|
||||
if (my $group_by = $self->buildGroupBy()) {
|
||||
push @clauses,$group_by;
|
||||
}
|
||||
if (my $order_by = $self->buildOrderBy()) {
|
||||
push @clauses,$order_by;
|
||||
}
|
||||
if (my $limit = $self->buildLimit()) {
|
||||
push @clauses,$limit;
|
||||
}
|
||||
return join(' ',@clauses);
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
sub buildSelectFields {
|
||||
my ($self) = @_;
|
||||
return join(', ',@{$self->{_fields}}) if @{$self->{_fields}};
|
||||
return join(', ',keys %{$self->{_properties}}) if %{$self->{_properties}};
|
||||
return '*';
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 new( %p )
|
||||
|
||||
=over
|
||||
|
||||
=item fields
|
||||
|
||||
An array reference of field names (optional).
|
||||
|
||||
=item groupBy
|
||||
|
||||
An array reference of fields to group results by
|
||||
|
||||
=item limit
|
||||
|
||||
A scalar limit.
|
||||
|
||||
=item orderBy
|
||||
|
||||
An array reference of fields to order results by
|
||||
|
||||
=item properties
|
||||
|
||||
=over
|
||||
|
||||
=item * quote
|
||||
|
||||
If true values for this field are automatically quoted.
|
||||
|
||||
=back
|
||||
|
||||
=item table
|
||||
|
||||
The name of the table to query.
|
||||
|
||||
=item where
|
||||
|
||||
A hash reference or array reference of arguments to build a where clause from.
|
||||
See WebGUI::Persistent::Query::parseWhereArgs for details.
|
||||
|
||||
=cut
|
||||
|
||||
sub new {
|
||||
my ($class,%p) = @_;
|
||||
my $self = $class->SUPER::new(%p);
|
||||
$self->{_fields} = $p{fields} || [];
|
||||
$self->{_limit} = $p{limit};
|
||||
$self->{_group_by} = $p{groupBy};
|
||||
$self->{_order_by} = $p{orderBy};
|
||||
return $self;
|
||||
}
|
||||
|
||||
1;
|
||||
118
lib/WebGUI/Persistent/Query/Update.pm
Normal file
118
lib/WebGUI/Persistent/Query/Update.pm
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
package WebGUI::Persistent::Query::Update;
|
||||
|
||||
=head1 LEGAL
|
||||
|
||||
-------------------------------------------------------------------
|
||||
WebGUI is Copyright 2001-2003 Plain Black LLC.
|
||||
-------------------------------------------------------------------
|
||||
Please read the legal notices (docs/legal.txt) and the license
|
||||
(docs/license.txt) that came with this distribution before using
|
||||
this software.
|
||||
-------------------------------------------------------------------
|
||||
http://www.plainblack.com info@plainblack.com
|
||||
-------------------------------------------------------------------
|
||||
|
||||
=cut
|
||||
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use WebGUI::Persistent::Query;
|
||||
|
||||
our @ISA = qw(WebGUI::Persistent::Query);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Package WebGUI::Persistent::Query::Insert
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
This class allows reliable dynamic building of Sql insert queries.
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
my $query = WebGUI::Persistent::Query::Insert->new(
|
||||
table => 'myTable',
|
||||
where => [A => [1,2],[{C => 'hello',B => 1}]],
|
||||
data => {
|
||||
A => 1,
|
||||
B => 2,
|
||||
C => 'hello',
|
||||
D => 'world'
|
||||
},
|
||||
properties => {
|
||||
A => { },
|
||||
B => { },
|
||||
C => { quote => 1 },
|
||||
D => { quote => 1 },
|
||||
}
|
||||
);
|
||||
|
||||
$query->buildQuery();
|
||||
|
||||
Returns:
|
||||
|
||||
UPDATE myTable SET A = 1, B = 2, C = 'hello' C = 'world'
|
||||
WHERE A IN (1,2) AND (C = 'hello' OR B = 1)
|
||||
|
||||
=cut
|
||||
|
||||
=head2 buildQuery
|
||||
|
||||
=cut
|
||||
|
||||
sub buildQuery {
|
||||
my ($self) = @_;
|
||||
|
||||
my @clauses = ('UPDATE',$self->{_table},$self->buildSet());
|
||||
if (my $where = $self->buildWhere()) {
|
||||
push @clauses,$where;
|
||||
}
|
||||
|
||||
return join(' ',@clauses);
|
||||
}
|
||||
|
||||
sub buildSet {
|
||||
my ($self) = @_;
|
||||
'SET '.join(', ',map {
|
||||
"$_ = ". $self->quote($_,$self->{_data}{$_})
|
||||
} keys %{$self->{_data}});
|
||||
}
|
||||
|
||||
=head2 new( %p )
|
||||
|
||||
=over
|
||||
|
||||
=item data
|
||||
|
||||
A hash reference of field name to value.
|
||||
|
||||
=item properties
|
||||
|
||||
=over
|
||||
|
||||
=item * quote
|
||||
|
||||
If true values for this field are automatically quoted.
|
||||
|
||||
=back
|
||||
|
||||
=item table
|
||||
|
||||
=item where
|
||||
|
||||
A hash reference or array reference of arguments to build a where clause from.
|
||||
See WebGUI::Persistent::Query::parseWhereArgs for details.
|
||||
|
||||
=back
|
||||
|
||||
=cut
|
||||
|
||||
sub new {
|
||||
my ($class,%p) = @_;
|
||||
my $self = $class->SUPER::new(%p);
|
||||
$self->{_data} = $p{data} || {};
|
||||
return $self;
|
||||
}
|
||||
|
||||
1;
|
||||
Loading…
Add table
Add a link
Reference in a new issue