Alzabo
view release on metacpan or search on metacpan
lib/Alzabo/Runtime/Schema.pm view on Meta::CPAN
package Alzabo::Runtime::Schema;
use strict;
use vars qw($VERSION);
use Alzabo::Exceptions ( abbr => [ qw( logic_exception params_exception ) ] );
use Alzabo::Runtime;
use Alzabo::Utils;
use Params::Validate qw( :all );
Params::Validate::validation_options( on_fail => sub { params_exception join '', @_ } );
use base qw(Alzabo::Schema);
$VERSION = 2.0;
1;
sub load_from_file
{
my $class = shift;
my $self = $class->_load_from_file(@_);
$self->prefetch_all_but_blobs;
return $self;
}
sub _schema_file_type
{
return 'runtime';
}
sub user
{
my $self = shift;
return $self->{user};
}
sub password
{
my $self = shift;
return $self->{password};
}
sub host
{
my $self = shift;
return $self->{host};
}
sub port
{
my $self = shift;
return $self->{port};
}
sub referential_integrity
{
my $self = shift;
return defined $self->{maintain_integrity} ? $self->{maintain_integrity} : 0;
}
sub set_db_schema_name
{
my $self = shift;
$self->{db_schema_name} = shift;
}
sub set_user
{
my $self = shift;
$self->{user} = shift;
}
sub set_password
{
my $self = shift;
$self->{password} = shift;
}
sub set_host
{
my $self = shift;
$self->{host} = shift;
}
sub set_port
{
my $self = shift;
$self->{port} = shift;
}
sub set_referential_integrity
{
my $self = shift;
my $val = shift;
$self->{maintain_integrity} = $val if defined $val;
}
sub set_quote_identifiers
{
my $self = shift;
my $val = shift;
$self->{quote_identifiers} = $val if defined $val;
}
sub connect
{
my $self = shift;
my %p;
$p{user} = $self->user if defined $self->user;
$p{password} = $self->password if defined $self->password;
$p{host} = $self->host if defined $self->host;
$p{port} = $self->port if defined $self->port;
$self->driver->connect( %p, @_ );
# $self->set_referential_integrity( ! $self->driver->supports_referential_integrity );
}
sub disconnect
{
my $self = shift;
$self->driver->disconnect;
}
sub one_row
{
# could be replaced with something potentially more efficient
return shift->join(@_)->next;
}
use constant JOIN_SPEC => { join => { type => ARRAYREF | OBJECT,
optional => 1 },
tables => { type => ARRAYREF | OBJECT,
optional => 1 },
select => { type => ARRAYREF | OBJECT,
optional => 1 },
where => { type => ARRAYREF,
optional => 1 },
order_by => { type => ARRAYREF | HASHREF | OBJECT,
optional => 1 },
limit => { type => SCALAR | ARRAYREF,
optional => 1 },
distinct => { type => ARRAYREF | OBJECT,
optional => 1 },
quote_identifiers => { type => BOOLEAN,
optional => 1 },
};
sub join
{
my $self = shift;
my %p = validate( @_, JOIN_SPEC );
$p{join} ||= delete $p{tables};
$p{join} = [ $p{join} ] unless Alzabo::Utils::is_arrayref( $p{join} );
my @tables;
if ( Alzabo::Utils::is_arrayref( $p{join}->[0] ) )
{
# flattens the nested structure and produces a unique set of
# tables
@tables = values %{ { map { $_ => $_ }
grep { Alzabo::Utils::safe_isa( $_, 'Alzabo::Table' ) }
map { @$_ } @{ $p{join} } } };
}
else
{
@tables = grep { Alzabo::Utils::safe_isa($_, 'Alzabo::Table') } @{ $p{join} };
}
lib/Alzabo/Runtime/Schema.pm view on Meta::CPAN
logic_exception
( "The " . $table_1->name .
" table has more than 1 foreign key to the " .
$table_2->name . " table" )
if @fk > 1;
$fk = $fk[0];
}
foreach my $cp ( $fk->column_pair_names )
{
if ( $op eq 'where' )
{
# first time through loop only
$sql->where;
$sql->subgroup_start;
$sql->condition( $table_1->column( $cp->[0] ), '=', $table_2->column( $cp->[1] ) );
}
else
{
$sql->$op( $table_1->column( $cp->[0] ), '=', $table_2->column( $cp->[1] ) );
}
$op = 'and';
}
}
sub prefetch_all
{
my $self = shift;
$_->set_prefetch( $_->columns ) for $self->tables;
}
sub prefetch_all_but_blobs
{
my $self = shift;
$_->set_prefetch( grep { ! $_->is_blob } $_->columns ) for $self->tables;
}
sub prefetch_none
{
my $self = shift;
$_->set_prefetch() for $self->tables;
}
__END__
=head1 NAME
Alzabo::Runtime::Schema - Schema objects
=head1 SYNOPSIS
use Alzabo::Runtime::Schema qw(some_schema);
my $schema = Alzabo::Runtime::Schema->load_from_file( name => 'foo' );
$schema->set_user( $username );
$schema->set_password( $password );
$schema->connect;
=head1 DESCRIPTION
Objects in this class represent schemas, and can be used to retrieve
data from that schema.
This object can only be loaded from a file. The file is created
whenever a corresponding
L<C<Alzabo::Create::Schema>|Alzabo::Create::Schema> object is saved.
=head1 INHERITS FROM
C<Alzabo::Schema>
=for pod_merge merged
=head1 METHODS
=head2 load_from_file ( name => $schema_name )
Loads a schema from a file. This is the only constructor for this
class. It returns an C<Alzabo::Runtime::Schema> object. Loaded
objects are cached in memory, so future calls to this method may
return the same object.
Throws: L<C<Alzabo::Exception::Params>|Alzabo::Exceptions>,
L<C<Alzabo::Exception::System>|Alzabo::Exceptions>
=head2 set_user ($user)
Sets the username to use when connecting to the database.
=head2 user
Return the username used by the schema when connecting to the database.
=head2 set_password ($password)
Set the password to use when connecting to the database.
=head2 password
Returns the password used by the schema when connecting to the
database.
=head2 set_host ($host)
Set the host to use when connecting to the database.
=head2 host
Returns the host used by the schema when connecting to the database.
=head2 set_port ($port)
Set the port to use when connecting to the database.
=head2 port
Returns the port used by the schema when connecting to the database.
=head2 set_referential_integrity ($boolean)
Turns referential integrity checking on or off. If it is on, then
when L<C<Alzabo::Runtime::Row>|Alzabo::Runtime::Row> objects are
deleted, updated, or inserted, they will report this activity to any
relevant L<C<Alzabo::Runtime::ForeignKey>|Alzabo::Runtime::ForeignKey>
objects for the row, so that the foreign key objects can take
appropriate action.
This defaults to false. If your RDBMS supports foreign key
constraints, these should be used instead of Alzabo's built-in
referential integrity checking, as they will be much faster.
=head2 referential_integrity
Returns a boolean value indicating whether this schema will attempt to
maintain referential integrity.
=head2 set_quote_identifiers ($boolean)
If this is true, then all SQL constructed for this schema will have
quoted identifiers (like `Table`.`column` in MySQL).
This defaults to false. Turning this on adds some overhead to all SQL
generation.
=head2 connect (%params)
Calls the L<C<Alzabo::Driver-E<gt>connect>|Alzabo::Driver/connect>
method for the driver owned by the schema. The username, password,
host, and port set for the schema will be passed to the driver, as
will any additional parameters given to this method. See the L<C<<
Alzabo::Driver->connect() method >>|Alzabo::Driver/connect> for more
details.
=head2 disconnect
Calls the L<C<< Alzabo::Driver->disconnect()
>>|Alzabo::Driver/disconnect> method for the driver owned by the
schema.
=head2 join
Joins are done by taking the tables provided in order, and finding a
relation between them. If any given table pair has more than one
relation, then this method will fail. The relations, along with the
values given in the optional where clause will then be used to
generate the necessary SQL. See
L<C<Alzabo::Runtime::JoinCursor>|Alzabo::Runtime::JoinCursor> for more
information.
This method takes the following parameters:
=over 4
=item * join => <see below>
This parameter can either be a simple array reference of tables or an
array reference of array references. In the latter case, each array
reference should contain two tables. These array references can also
include an optional modifier specifying a type of join for the two
tables, like 'left_outer_join', an optional foreign key object which
will be used to join the two tables, and an optional where clause used
to restrict the join.
If a simple array reference is given, then the order of these tables
is significant when there are more than 2 tables. Alzabo expects to
find relationships between tables 1 & 2, 2 & 3, 3 & 4, etc.
For example, given:
join => [ $table_A, $table_B, $table_C ]
Alzabo would expect that table A has a relationship to table B, which
in turn has a relationship to table C. If you simply provide a simple
array reference, you cannot include any outer joins, and every element
of the array reference must be a table object.
If you need to specify a more complicated set of relationships, this
can be done with a slightly more complicated data structure, which
looks like this:
join => [ [ $table_A, $table_B ],
[ $table_A, $table_C ],
[ $table_C, $table_D ],
[ $table_C, $table_E ] ]
This is fairly self explanatory. Alzabo will expect to find a
relationship between each pair of tables. This allows for the
construction of arbitrarily complex join clauses.
lib/Alzabo/Runtime/Schema.pm view on Meta::CPAN
=for pod_merge has_table
=for pod_merge begin_work
=for pod_merge rollback
=for pod_merge commit
=for pod_merge run_in_transaction ( sub { code... } )
=for pod_merge driver
=for pod_merge rules
=for pod_merge sqlmaker
=head1 JOINING A TABLE MORE THAN ONCE
It is possible to join to the same table more than once in a query.
Table objects contain an L<C<alias()>|Alzabo::Runtime::Table/alias>
method that, when called, returns an object that can be used in the
same query as the original table object, but which will be treated as
a separate table. This faciliaties queries similar to the following
SQL::
SELECT ... FROM Foo AS F1, Foo as F2, Bar AS B ...
The object returned from the table functions more or less exactly like
a table object. When using this table to set where clause or order by
(or any other) conditions, it is important that the column objects for
these conditions be retrieved from the alias object.
For example:
my $foo_alias = $foo->alias;
my $cursor = $schema->join( select => $foo,
join => [ $foo, $bar, $foo_alias ],
where => [ [ $bar->column('baz'), '=', 10 ],
[ $foo_alias->column('quux'), '=', 100 ] ],
order_by => $foo_alias->column('briz') );
If we were to use the C<$foo> object to retrieve the 'quux' and
'briz' columns then the join would simply not work as expected.
It is also possible to use multiple aliases of the same table in a
join, so that this will work properly:
my $foo_alias1 = $foo->alias;
my $foo_alias2 = $foo->alias;
=head1 USER AND PASSWORD INFORMATION
This information is never saved to disk. This means that if you're
operating in an environment where the schema object is reloaded from
disk every time it is used, such as a CGI program spanning multiple
requests, then you will have to make a new connection every time. In
a persistent environment, this is not a problem. For example, in a
mod_perl environment, you could load the schema and call the
L<C<set_user()>|Alzabo::Runtime::Schema/set_user ($user)> and
L<C<set_password()>|Alzabo::Runtime::Schema/set_password ($password)>
methods in the server startup file. Then all the mod_perl children
will inherit the schema with the user and password already set.
Otherwise you will have to provide it for each request.
You may ask why you have to go to all this trouble to deal with the
user and password information. The basic reason was that I did not
feel I could come up with a solution to this problem that was secure,
easy to configure and use, and cross-platform compatible. Rather, I
think it is best to let each user decide on a security practice with
which they feel comfortable.
In addition, there are a number of modules aimed at helping store and
use this sort of information on CPAN, including C<DBIx::Connect> and
C<AppConfig>, among others.
=head1 AUTHOR
Dave Rolsky, <autarch@urth.org>
=cut
( run in 0.481 second using v1.01-cache-2.11-cpan-5a3173703d6 )