Apache-iNcom

 view release on metacpan or  search on metacpan

lib/DBIx/SearchProfiles.pm  view on Meta::CPAN

    }
    $fdat->{dbix_sp_found} = $found;
    $fdat->{dbix_sp_total} = $total;
    return $result;
}

=pod

=head2 record_insert ( $name, \%params );

This method will insert a record in the table specified by the profile
$name. The I<params> argument is a reference to an hash which contains
the record data to be inserted. The hash should contains one element
for each key specified in the I<keys> field of the profile. Each
elements in the fields that is a valid table fields (as specified by
the I<fields> element of the profile) will be inserted. Any elements
specified I<defaults> and not present in the I<params> hash will also
be inserted.

Return value is undefined.

=cut

sub record_insert {
    my $self = shift;
    my $name = shift;
    my $fdat = shift || {};

    $self->load_profiles;

    my $profile = $self->{profiles}{$name};
    die "No such profile: $name\n" unless $profile;

    my $table	= $profile->{table};
    my @fields	= (@{$profile->{keys}}, @{$profile->{fields}} );

    my @names	= ();
    my @values	= ();
    for my $name ( @fields ) {
	my $value = defined $fdat->{$name} ? $fdat->{$name} :
	  $profile->{defaults}{$name};
	next unless defined $value;
	push @names,	$name;
	push @values,	$value;
    }

    die "Nothing to insert\n" unless @values;

    my $statement = "INSERT INTO $table ( " . join (", ", @names ) .
      ") VALUES (" . join ( ",", ("?") x @names ) . ")";

    $self->sql_do( $statement, @values );
}

=pod

=head2 record_update ( $name, \%params );

This method will update a record in the table specified by the profile
$name. The I<params> argument is a reference to an hash which contains
the record data to be updated. The hash should contains one element
for each key specified in the I<keys> field of the profile. Each
elements in the fields that is a valid table fields (as specified by
the I<fields> element of the profile) will be updated. Any elements
specified I<defaults> and not present in the I<params> hash will also
be updated.

Return value is undefined.

=cut

sub record_update {
    my $self = shift;
    my $name = shift;
    my $fdat = shift || {};

    $self->load_profiles;

    my $profile = $self->{profiles}{$name};
    die "No such profile: $name\n" unless $profile;

    my $table	= $profile->{table};
    my $fields	= $profile->{fields};
    my $keys	= $profile->{keys};

    my @names	= ();
    my @values	= ();
    for my $name ( @$fields ) {
	my $value = defined $fdat->{$name} ? $fdat->{$name} :
	  $profile->{defaults}{$name};
	next unless defined $value;
	push @names, $name . " = ?";
	push @values, $value;
    }

    die "Nothing to update\n" unless @values;

    my @keys	= ();
    for my $name ( @$keys ) {
	my $value = $fdat->{$name};
	die "Missing key attribute $name\n"
	  unless defined $value;
	push @keys,	$name . " = ?";
	push @values,	$value;
    }

    my $query = "UPDATE $table SET " . join( ", ", @names ) .
      " WHERE " . join ( " AND ", @keys );

    $self->sql_do( $query, @values );
}

=pod

=head2 record_delete ( $name, $keys );

This method will delete a record in the table specified by the profile
$name. The I<keys> argument is a reference to an hash which contains
the keys to the record to delete. The hash should contains one element
for each key specified in the I<keys> field of the profile.

Return value is undefined.

=cut

sub record_delete {

lib/DBIx/SearchProfiles.pm  view on Meta::CPAN


=head2 template_insert ( $name, params )

This method will insert a record according to the profile in $name.
Normal template substitutions will be used.

Return value is undefined.

=cut

sub template_insert {
    my $self = shift;
    my $name = shift;

    my ( $fdat, @params );

    # Params is either an hash ref in which we will look
    # for named base params or it can be an array (or array ref)
    # of which the elements will be used for substitutions in
    # the SQL query
    if ( ref $_[0] eq "HASH" ) {
	$fdat = shift;
    } elsif ( ref $_[0] eq "ARRAY" ) {
	@params = @{$_[0]};
    } else {
	@params = @_;
    }
    $fdat ||= {};

    $self->load_profiles;

    my $profile = $self->{profiles}{$name};
    die "No such profile: $name\n" unless $profile;

    my $statement_spec  = $profile->{query};
    die "Query doesn't start with INSERT"
      unless  $statement_spec =~ /^\s*INSERT /;

    my $params_spec = $profile->{params} || [];

    # Fetch the param from the query
    if ( @params ) {
	die "Number of params doesn't match the number of specs\n"
	  unless @params == @$params_spec;
    } else {
	@params = map { 
	    defined $fdat->{$_} ? $fdat->{$_} : $profile->{defaults}{$_}
	} @$params_spec;
    }

    $self->sql_do( $statement_spec, @params );
}

=pod

=head2 template_update ( $name, params )

This method will update records according to the profile $name and
using standard template's placholders substitutions semantics.

Return value is the number of rows updated.

=cut

sub template_update {
    my $self = shift;
    my $name = shift;

    my ( $fdat, @params );

    # Params is either an hash ref in which we will look
    # for named base params or it can be an array (or array ref)
    # of which the elements will be used for substitutions in
    # the SQL query
    if ( ref $_[0] eq "HASH" ) {
	$fdat = shift;
    } elsif ( ref $_[0] eq "ARRAY" ) {
	@params = @{$_[0]};
    } else {
	@params = @_;
    }
    $fdat ||= {};

    $self->load_profiles;

    my $profile = $self->{profiles}{$name};
    die "No such profile: $name\n" unless $profile;

    my $statement_spec  = $profile->{query};
    die "Query doesn't start with UPDATE"
      unless  $statement_spec =~ /^\s*UPDATE /;

    my $params_spec = $profile->{params} || [];

    # Fetch the param from the query
    if ( @params ) {
	die "Number of params doesn't match the number of specs\n"
	  unless @params == @$params_spec;
    } else {
	@params = map { 
	    defined $fdat->{$_} ? $fdat->{$_} : $profile->{defaults}{$_}
	} @$params_spec;
    }

    $self->sql_do( $statement_spec, @params );
}

=pod

=head2 template_delete ( $name, params )

This method will delete records according to the template $name and
using regular template's placeholders substitutions semantics.

Return value is the number of records deleted.

=cut

sub template_delete {
    my $self = shift;
    my $name = shift;



( run in 0.640 second using v1.01-cache-2.11-cpan-39bf76dae61 )