From 67696f3dcb968bde52bee4bb841b642c41fc6009 Mon Sep 17 00:00:00 2001 From: Henri Sara Date: Tue, 14 May 2013 15:25:58 +0300 Subject: [PATCH] SQLContainer.indexOfId() also searches backwards (#11849, #10376) Change-Id: Iea3f832cd50314f747b82b774c3be57797f9ac1d --- .../data/util/sqlcontainer/SQLContainer.java | 8 ++-- .../sqlcontainer/DatabaseHelper.java | 34 ++++++++++++++ .../SqlcontainertableApplication.java | 47 +++++++++++++++++++ 3 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 uitest/src/com/vaadin/tests/containers/sqlcontainer/SqlcontainertableApplication.java diff --git a/server/src/com/vaadin/data/util/sqlcontainer/SQLContainer.java b/server/src/com/vaadin/data/util/sqlcontainer/SQLContainer.java index 64c16b2798..aa8234ebb9 100644 --- a/server/src/com/vaadin/data/util/sqlcontainer/SQLContainer.java +++ b/server/src/com/vaadin/data/util/sqlcontainer/SQLContainer.java @@ -629,23 +629,25 @@ public class SQLContainer implements Container, Container.Filterable, getPage(); } int size = size(); - boolean wrappedAround = false; - while (!wrappedAround) { + // this protects against infinite looping + int counter = 0; + while (counter < size) { for (Integer i : itemIndexes.keySet()) { if (itemIndexes.get(i).equals(itemId)) { return i; } + counter++; } // load in the next page. int nextIndex = (currentOffset / (pageLength * CACHE_RATIO) + 1) * (pageLength * CACHE_RATIO); if (nextIndex >= size) { // Container wrapped around, start from index 0. - wrappedAround = true; nextIndex = 0; } updateOffsetAndCache(nextIndex); } + // safeguard in case item not found return -1; } diff --git a/uitest/src/com/vaadin/tests/containers/sqlcontainer/DatabaseHelper.java b/uitest/src/com/vaadin/tests/containers/sqlcontainer/DatabaseHelper.java index 287d772901..7e95a41742 100644 --- a/uitest/src/com/vaadin/tests/containers/sqlcontainer/DatabaseHelper.java +++ b/uitest/src/com/vaadin/tests/containers/sqlcontainer/DatabaseHelper.java @@ -15,6 +15,8 @@ class DatabaseHelper { private JDBCConnectionPool connectionPool = null; private SQLContainer testContainer = null; private static final String TABLENAME = "testtable"; + private SQLContainer largeContainer = null; + private static final String LARGE_TABLENAME = "largetable"; public DatabaseHelper() { initConnectionPool(); @@ -32,24 +34,42 @@ class DatabaseHelper { // Will fail if table doesn't exist, which is OK. conn.rollback(); } + try { + statement.execute("drop table " + LARGE_TABLENAME); + } catch (SQLException e) { + // Will fail if table doesn't exist, which is OK. + conn.rollback(); + } switch (SQLTestsConstants.db) { case HSQLDB: statement .execute("create table " + TABLENAME + " (id integer GENERATED BY DEFAULT AS IDENTITY, field1 varchar(100), field2 boolean, primary key(id))"); + statement + .execute("create table " + + LARGE_TABLENAME + + " (id integer GENERATED BY DEFAULT AS IDENTITY, field1 varchar(100), primary key(id))"); break; case MYSQL: statement .execute("create table " + TABLENAME + " (id integer auto_increment not null, field1 varchar(100), field2 boolean, primary key(id))"); + statement + .execute("create table " + + LARGE_TABLENAME + + " (id integer auto_increment not null, field1 varchar(100), primary key(id))"); break; case POSTGRESQL: statement .execute("create table " + TABLENAME + " (\"id\" serial primary key, \"field1\" varchar(100), \"field2\" boolean)"); + statement + .execute("create table " + + LARGE_TABLENAME + + " (\"id\" serial primary key, \"field1\" varchar(100))"); break; } statement.executeUpdate("insert into " + TABLENAME @@ -58,6 +78,12 @@ class DatabaseHelper { + " values(default, 'Ville', 'true')"); statement.executeUpdate("insert into " + TABLENAME + " values(default, 'Jussi', 'true')"); + + for (int i = 0; i < 400; ++i) { + statement.executeUpdate("insert into " + LARGE_TABLENAME + + " values(default, 'User " + i + "')"); + } + statement.close(); conn.commit(); connectionPool.releaseConnection(conn); @@ -71,6 +97,10 @@ class DatabaseHelper { TableQuery q1 = new TableQuery(TABLENAME, connectionPool); q1.setVersionColumn("id"); testContainer = new SQLContainer(q1); + + TableQuery q2 = new TableQuery(LARGE_TABLENAME, connectionPool); + q2.setVersionColumn("id"); + largeContainer = new SQLContainer(q2); } catch (SQLException e) { e.printStackTrace(); } @@ -89,4 +119,8 @@ class DatabaseHelper { public SQLContainer getTestContainer() { return testContainer; } + + public SQLContainer getLargeContainer() { + return largeContainer; + } } \ No newline at end of file diff --git a/uitest/src/com/vaadin/tests/containers/sqlcontainer/SqlcontainertableApplication.java b/uitest/src/com/vaadin/tests/containers/sqlcontainer/SqlcontainertableApplication.java new file mode 100644 index 0000000000..8d4f1c5842 --- /dev/null +++ b/uitest/src/com/vaadin/tests/containers/sqlcontainer/SqlcontainertableApplication.java @@ -0,0 +1,47 @@ +package com.vaadin.tests.containers.sqlcontainer; + +import com.vaadin.data.Property.ValueChangeEvent; +import com.vaadin.data.Property.ValueChangeListener; +import com.vaadin.server.LegacyApplication; +import com.vaadin.ui.HorizontalSplitPanel; +import com.vaadin.ui.Label; +import com.vaadin.ui.LegacyWindow; +import com.vaadin.ui.Table; + +public class SqlcontainertableApplication extends LegacyApplication { + private LegacyWindow mainWindow; + private Table table; + private HorizontalSplitPanel panel; + private Label label = new Label(); + + @Override + public void init() { + mainWindow = new LegacyWindow("SQLContainer Test"); + setMainWindow(mainWindow); + mainWindow.getContent().setSizeFull(); + + panel = new HorizontalSplitPanel(); + panel.setSecondComponent(label); + + final DatabaseHelper helper = new DatabaseHelper(); + table = new Table(); + table.setSizeFull(); + table.setContainerDataSource(helper.getLargeContainer()); + table.setSelectable(true); + table.setImmediate(true); + table.setMultiSelect(true); + table.addValueChangeListener(new ValueChangeListener() { + @Override + public void valueChange(ValueChangeEvent event) { + label.setValue(table.getValue().toString()); + } + }); + + panel.setSizeFull(); + panel.addComponent(table); + + mainWindow.addComponent(panel); + + } + +} \ No newline at end of file -- 2.39.5