[interchange] * Add ability to have a NO_UPDATE field in SQL tables, which field will
Mike Heins <[email protected]> Mon, 23 May 2016 13:03:38 +0000
| Newsgroups | gmane.comp.web.interchange.cvs |
|---|---|
| Message-ID | <[email protected]> |
commit 10c2e8ae294165bfae2e22e482d0437b4d9d96a5 Author: Mike Heins <[email protected]> Date: Mon May 23 09:02:51 2016 -0400 * 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 WHATSNEW-5.11 | 26 +++++++++++++++++ lib/Vend/Config.pm | 1 + lib/Vend/Table/DBI.pm | 73 ++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 99 insertions(+), 1 deletions(-) --- diff --git a/WHATSNEW-5.11 b/WHATSNEW-5.11 new file mode 100644 index 0000000..bcd63b9 --- /dev/null +++ b/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 diff --git a/lib/Vend/Config.pm b/lib/Vend/Config.pm index 032f27c..5d29384 100644 --- a/lib/Vend/Config.pm +++ b/lib/Vend/Config.pm @@ -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 ! ); diff --git a/lib/Vend/Table/DBI.pm b/lib/Vend/Table/DBI.pm index 268b5bc..0223e8d 100644 --- a/lib/Vend/Table/DBI.pm +++ b/lib/Vend/Table/DBI.pm @@ -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'; @@ -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]; @@ -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 {