]> source.dussan.org Git - vaadin-framework.git/commitdiff
SQLContainer.indexOfId() also searches backwards (#11849, #10376)
authorHenri Sara <hesara@vaadin.com>
Tue, 14 May 2013 12:25:58 +0000 (15:25 +0300)
committerVaadin Code Review <review@vaadin.com>
Tue, 14 May 2013 13:37:25 +0000 (13:37 +0000)
Change-Id: Iea3f832cd50314f747b82b774c3be57797f9ac1d

server/src/com/vaadin/data/util/sqlcontainer/SQLContainer.java
uitest/src/com/vaadin/tests/containers/sqlcontainer/DatabaseHelper.java
uitest/src/com/vaadin/tests/containers/sqlcontainer/SqlcontainertableApplication.java [new file with mode: 0644]

index 64c16b2798155770c2f435e760cf29fd897035fa..aa8234ebb9250ce758b27aabf03549c47c4c2631 100644 (file)
@@ -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;
     }
 
index 287d772901ec01a3a3775e3442530e18159d4d09..7e95a41742889fe758e9939beb192226e5db26f7 100644 (file)
@@ -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 (file)
index 0000000..8d4f1c5
--- /dev/null
@@ -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