Skip to content

Commit

Permalink
* Add ability to have a NO_UPDATE field in SQL tables, which field will
Browse files Browse the repository at this point in the history
  not affect a TIMESTAMP field (in MySQL or Postgres, at least).

  Intended to allow mod_time in user logins to be updated without
  changing a timestamp field. May have other uses, but is only
  honored at this time in the set_field() method, so has limited
  applicability.

  Assuming one has a timestamp field in the userdb table called
  "update_date", this is effected by changing the configuration as:

  	Database userdb  TIMESTAMP_FIELD  update_date
  	Database userdb  NO_UPDATE        mod_time
  • Loading branch information
perusionmike committed May 23, 2016
1 parent c5ee9b0 commit 10c2e8a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
26 changes: 26 additions & 0 deletions WHATSNEW-5.11
@@ -0,0 +1,26 @@
------------------------------------------------------------------------------

What's new in each version of Interchange
(since the version 5.11 branch)

See UPGRADE document for a list of incompatible changes.

------------------------------------------------------------------------------


Database
--------

* Add ability to have a NO_UPDATE field in SQL tables, which field will
not affect a TIMESTAMP field (in MySQL or Postgres, at least).

Intended to allow mod_time in user logins to be updated without
changing a timestamp field. May have other uses, but is only
honored at this time in the set_field() method, so has limited
applicability.

Assuming one has a timestamp field in the userdb table called
"update_date", this is effected by changing the configuration as:

Database userdb TIMESTAMP_FIELD update_date
Database userdb NO_UPDATE mod_time
1 change: 1 addition & 0 deletions lib/Vend/Config.pm
Expand Up @@ -4257,6 +4257,7 @@ my %Hash_ref = ( qw!
DEFAULT_SESSION DEFAULT_SESSION
FIELD_ALIAS FIELD_ALIAS
NUMERIC NUMERIC
NO_UPDATE NO_UPDATE
PREFER_NULL PREFER_NULL
WRITE_CATALOG WRITE_CATALOG
! );
Expand Down
73 changes: 72 additions & 1 deletion lib/Vend/Table/DBI.pm
Expand Up @@ -18,6 +18,28 @@
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301 USA.

=head1 NAME
Vend::Table::DBI
=head1 SYNOPSIS
use Vend::Table::Common;
use Vend::Table::DBI;
=head1 DESCRIPTION
This is the database code that links Interchange to database tables as
presented by Perl DBI, and which are interfaced by SQL. Some may
conceivably be non-SQL tables with DBI frontents, but most Interchange
installations use MySQL or Postgres.
=head1 METHODS
Selected methods used in this module are documented below.
=cut

package Vend::Table::DBI;
$VERSION = '2.89';

Expand Down Expand Up @@ -1797,6 +1819,50 @@ sub field {
$data;
}

=over
=item set_field
This method sets a single field with a value, i.e.
$dbobj->set_field($key,$field,$value)
The primary key for the field is passed as the first parameter, the
field name as defined in the Interchange table definition is the
second parameter, and the value to be set is the last value.
If the row does not exist, it will be created. (Note that this
may fail if there are NOT NULL values without defaults.)
This will essentially perform this pseudo-query:
UPDATE $table
SET $field = $value
WHERE $primary_key = $key
If there is a database configuration TIMESTAMP_FIELD, and the column
is defined as NO_UPDATE, the TIMESTAMP_FIELD will be set to its
current value to avoid being marked as updated. This changes the
pseudo_query to be
UPDATE $table
SET $timestamp_field = $timestamp_field, $field = $value
WHERE $primary_key = $key
This is most commonly needed in Interchange's userdb definition,
where it sets the mod_time field and you may not wish to indicate
the user as having been updated.
Database userdb TIMESTAMP_FIELD update_date
Database userdb NO_UPDATE mod_time
WARNING: There is no update_date field in Interchange's table definition
in the demo. You would need to add that to make this work.
=back
=cut

sub set_field {
my ($s, $key, $column, $value) = @_;
$s = $s->import_db() if ! defined $s->[$DBI];
Expand Down Expand Up @@ -1840,9 +1906,14 @@ sub set_field {
}
}

my $extra = '';
if( my $f = $s->[$CONFIG]{TIMESTAMP_FIELD} and exists $s->[$CONFIG]{NO_UPDATE}{$column} ) {
$extra = "$f = $f, ";
}

my @args;
if(!$q) {
$q = qq{update $s->[$QTABLE] SET $column = ? where $s->[$QKEY] = ?};
$q = qq{update $s->[$QTABLE] SET $extra$column = ? where $s->[$QKEY] = ?};
@args = ($value, $key);
}
else {
Expand Down

0 comments on commit 10c2e8a

Please sign in to comment.