Re: Stolen Ideas

[prev] [thread] [next] [Date index for 2004/08/24]

From: Tim Bunce
Subject: Re: Stolen Ideas
Date: 08:48 on 24 Aug 2004
On Tue, Aug 10, 2004 at 07:42:29PM -0400, Perrin Harkins wrote:
> On Tue, 2004-08-10 at 19:16, Tony Bowden wrote:
> > > I realized that my sample case, where search is fast but fetching
> > > objects is slow, was an anomaly.  For most people, search is the slow
> > > part.
> > 
> > How would the results be cached?
> 
> The same as objects would be cached, using a caching module.  My current
> plan is to create some new modules that glue faster storage methods
> (BerkeleyDB, Cache::FastMmap, memcached) under the Cache::Cache API. 
> The current Cache::Cache implementations are too slow to be useful for
> much of anything.
> 
> > Perhaps a Class::DBI::Plugin::FastDelete that you can explicitly use
> > when you know what you're doing is safe?
> 
> Yes, that would work.  Or it could just check to see if there are any
> triggers.

See appended working code.

I'd be happy for someone to turn this into a plugin (though 'fast'
is true it's not the best name, especially with auto-fallback to
per-row behaviour if triggers are present).

Tim.



sub update_rows { # Class->update_rows(where => { ... }, set => { ... });
    my ($class, %args) = @_;
    $class->_carp("$class->update_rows isn't an instance method")
	if ref $class;
    my $where  = delete $args{where} || {};
    my $bypass = delete $args{bypass_update_triggers};
    my $set    = delete $args{set}
	or $class->_croak("$class->update_rows called with no set clause");
    keys %args == 0
	or $class->_croak("$class->update_rows called with unknown parameters: @{[ %args ]}");

    unless ($bypass) {
	# in future if $bypass isn't true then we'll check if any
	# update triggers exist and if they do then we'll automatically
	# switch to a per-row mode
	$class->_croak("$class->update_rows currently requires bypass_update_triggers=>1");
    }

    require SQL::Abstract;
    $sql_abs ||= SQL::Abstract->new;

    my ($where_sql, @bind) = ($where eq "ALL") ? ("") : $sql_abs->where($where);
    $class->_croak("$class->update_rows called with no where clause (use 'ALL' to update all rows)")
	if !$where_sql and $where ne "ALL";
    
    $set = { %$set };	# copy so we can edit safely
    my @set_sql;	# fragments of SET sql

    for my $column (sort keys %$set) {
	$class->_croak("Column $column doesn't exist in $class")
	    unless $class->find_column($column);

	# process any \"literal sql" values first
	# (should not be used for simple values as it bypasses
	# normalize_column_values and validate_column_values)
	next unless ref $set->{$column};

	my $value = delete $set->{$column}; # note delete()
	push @set_sql, "$column=$$value";
    }

    # normalize and validate the new column values
    # (we're calling these as class methods, the implementation of
    # the methods is expected to cope with that)
    $class->normalize_column_values($set);
    # calls before_set_$column triggers created by add_constraint()
    $class->validate_column_values($set);

    for my $column (sort keys %$set) {
	my $value = $set->{$column};
	# unshift so @bind values in sync with placeholders for SET
	unshift @set_sql, "$column=?";
	unshift @bind, $value;
    }

    my $dbh = $class->db_Main;
    my $sth = $dbh->prepare_cached(sprintf "UPDATE %s SET %s %s",
	$class->table, join(", ", @set_sql), $where_sql);
    return $sth->execute(@bind);
}

sub delete_rows { # Class->delete_rows(where => { ... }, set => { ... });
    my ($class, %args) = @_;
    $class->_carp("$class->delete_rows isn't an instance method")
	if ref $class;
    my $where = delete $args{where} || {};
    my $bypass = delete $args{bypass_delete_triggers};
    keys %args == 0
	or $class->_croak("$class->delete_rows called with unknown parameters: @{[ %args ]}");

    unless ($bypass) {
	# in future if $bypass isn't true then we'll check if any
	# delete triggers exist and if they do then we'll automatically
	# switch to a per-row mode
	$class->_croak("$class->delete_rows currently requires bypass_delete_triggers=>1");
    }

    require SQL::Abstract;
    $sql_abs ||= SQL::Abstract->new;

    my ($where_sql, @bind) = ($where eq "ALL") ? ("") : $sql_abs->where($where);
    $class->_croak("$class->delete_rows called with no where clause (use 'ALL' to delete all rows)")
	if !$where_sql and $where ne "ALL";
    
    my $dbh = $class->db_Main;
    my $sth = $dbh->prepare_cached(sprintf "DELETE FROM %s %s",
	$class->table, $where_sql);
    return $sth->execute(@bind);
}

(message missing)

Stolen Ideas
Tony Bowden 18:58 on 10 Aug 2004

Re: Stolen Ideas
ed-cdbi 19:03 on 10 Aug 2004

Re: Stolen Ideas
Tony Bowden 19:06 on 10 Aug 2004

Re: Stolen Ideas
ed-cdbi 19:08 on 10 Aug 2004

Re: Stolen Ideas
Tony Bowden 19:11 on 10 Aug 2004

Re: Stolen Ideas
ed-cdbi 19:19 on 10 Aug 2004

RE: Stolen Ideas
Thomas, Mark - BLS CTR 19:11 on 10 Aug 2004

Re: Stolen Ideas
Tony Bowden 19:27 on 10 Aug 2004

Re: Stolen Ideas
Nelson C. T. Ferraz 15:14 on 12 Aug 2004

Re: Stolen Ideas
Scott McWhirter 19:18 on 10 Aug 2004

Re: Stolen Ideas
Tony Bowden 19:28 on 10 Aug 2004

Re: Stolen Ideas
Scott McWhirter 20:42 on 10 Aug 2004

Re: Stolen Ideas
Perrin Harkins 21:08 on 10 Aug 2004

Re: Stolen Ideas
Tony Bowden 21:33 on 10 Aug 2004

Re: Stolen Ideas
Scott McWhirter 22:58 on 10 Aug 2004

Re: Stolen Ideas
Perrin Harkins 23:23 on 10 Aug 2004

Re: Stolen Ideas
Gregory P. Smith 19:55 on 10 Aug 2004

Re: Stolen Ideas
Perrin Harkins 20:02 on 10 Aug 2004

Re: Stolen Ideas
Tony Bowden 21:47 on 10 Aug 2004

Re: Stolen Ideas
Perrin Harkins 22:23 on 10 Aug 2004

Re: Stolen Ideas
Tony Bowden 22:37 on 10 Aug 2004

Re: Stolen Ideas
Perrin Harkins 22:57 on 10 Aug 2004

Re: Stolen Ideas
Yuval Kogman 15:39 on 11 Aug 2004

Re: Stolen Ideas
Tony Bowden 22:02 on 10 Aug 2004

Re: Stolen Ideas
ed-cdbi 22:11 on 10 Aug 2004

Re: Stolen Ideas
Tony Bowden 22:25 on 10 Aug 2004

Re: Stolen Ideas
Tim Bunce 08:53 on 24 Aug 2004

Re: Stolen Ideas
Perrin Harkins 22:16 on 10 Aug 2004

Re: Stolen Ideas
Tony Bowden 22:42 on 10 Aug 2004

Re: Stolen Ideas
Tony Bowden 22:16 on 10 Aug 2004

Re: Stolen Ideas
Tony Bowden 23:18 on 10 Aug 2004

Re: Stolen Ideas
Scott McWhirter 23:48 on 10 Aug 2004

Re: Stolen Ideas
Perrin Harkins 00:22 on 11 Aug 2004

Re: Stolen Ideas
Perrin Harkins 03:45 on 11 Aug 2004

Re: Stolen Ideas
Tony Bowden 08:36 on 11 Aug 2004

Re: Stolen Ideas
Aaron Trevena 19:05 on 13 Aug 2004

Re: Stolen Ideas
Aaron Trevena 17:13 on 15 Aug 2004

Re: Stolen Ideas
Dan Friedman 04:47 on 11 Aug 2004

Re: Stolen Ideas
Tony Bowden 08:39 on 11 Aug 2004

Re: Stolen Ideas
Branislav Zahradnik 15:21 on 11 Aug 2004

RE: Stolen Ideas
Thomas, Mark - BLS CTR 19:13 on 13 Aug 2004

Re: Stolen Ideas
Tony Bowden 23:16 on 10 Aug 2004

Re: Stolen Ideas
Perrin Harkins 23:42 on 10 Aug 2004

Re: Stolen Ideas
Tim Bunce 08:48 on 24 Aug 2004

Re: Stolen Ideas
Tony Bowden 21:57 on 11 Aug 2004

Re: Stolen Ideas
Simon Cozens 09:03 on 24 Aug 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52