From 6a384f7ab195a180c910c32f01301e854f6f72da Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Fri, 21 Jul 2017 11:44:08 +0200
Subject: [PATCH] Bug 1xxxx: Add Koha::Autoincrement[s]
Content-Type: text/plain; charset=utf-8

---
 Koha/Autoincrement.pm  | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++
 Koha/Autoincrements.pm | 48 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 105 insertions(+)
 create mode 100644 Koha/Autoincrement.pm
 create mode 100644 Koha/Autoincrements.pm

diff --git a/Koha/Autoincrement.pm b/Koha/Autoincrement.pm
new file mode 100644
index 0000000..c7fe693
--- /dev/null
+++ b/Koha/Autoincrement.pm
@@ -0,0 +1,57 @@
+package Koha::Autoincrement;
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+
+use Koha::Database;
+
+use base qw(Koha::Object);
+
+=head1 NAME
+
+Koha::Autoincrement
+
+=head1 API
+
+=head2 Class Methods
+
+=cut
+
+sub increment {
+    my ( $self ) = @_;
+    return if !$self || !$self->tablename;
+
+    my $schema = Koha::Database->new->schema;
+    $schema->txn_begin;
+
+    $self->_result->update({ counter => \'counter + 1' });
+    $self->_result->discard_changes; # force read from db
+    my $retval = $self->_result->get_column('counter');
+
+    $schema->txn_commit;
+    return $retval;
+}
+
+=head3 type
+
+=cut
+
+sub _type {
+    return 'Autoincrement';
+}
+
+1;
diff --git a/Koha/Autoincrements.pm b/Koha/Autoincrements.pm
new file mode 100644
index 0000000..8dade9d
--- /dev/null
+++ b/Koha/Autoincrements.pm
@@ -0,0 +1,48 @@
+package Koha::Autoincrements;
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+
+use Koha::Database;
+
+use Koha::Autoincrement;
+
+use base qw(Koha::Objects);
+
+=head1 NAME
+
+Koha::Autoincrements
+
+=head1 API
+
+=head2 Class Methods
+
+=cut
+
+=head3 type
+
+=cut
+
+sub _type {
+    return 'Autoincrement';
+}
+
+sub object_class {
+    return 'Koha::Autoincrement';
+}
+
+1;
-- 
2.1.4