You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2010/01/14 20:14:58 UTC
svn commit: r899361 - in /pivot/trunk:
core/src/org/apache/pivot/sql/ResultList.java
tests/src/org/apache/pivot/tests/sql/
tests/src/org/apache/pivot/tests/sql/ResultListTest.java
tests/src/org/apache/pivot/tests/sql/result_list_test.sql
Author: gbrown
Date: Thu Jan 14 19:14:53 2010
New Revision: 899361
URL: http://svn.apache.org/viewvc?rev=899361&view=rev
Log:
Add ResultList test.
Added:
pivot/trunk/tests/src/org/apache/pivot/tests/sql/
pivot/trunk/tests/src/org/apache/pivot/tests/sql/ResultListTest.java
pivot/trunk/tests/src/org/apache/pivot/tests/sql/result_list_test.sql
Modified:
pivot/trunk/core/src/org/apache/pivot/sql/ResultList.java
Modified: pivot/trunk/core/src/org/apache/pivot/sql/ResultList.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/sql/ResultList.java?rev=899361&r1=899360&r2=899361&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/sql/ResultList.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/sql/ResultList.java Thu Jan 14 19:14:53 2010
@@ -19,7 +19,6 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Comparator;
-import java.util.Date;
import java.util.NoSuchElementException;
import org.apache.pivot.collections.ArrayList;
@@ -33,6 +32,9 @@
/**
* Implementation of the {@link List} interface that is backed by a
* instance of {@link java.sql.ResultSet}.
+ * <p>
+ * Note that this list is not suitable for random access and can only be
+ * navigated via an iterator.
*/
public class ResultList implements List<Map<String, Object>> {
/**
@@ -45,7 +47,7 @@
public final String columnName;
/**
- * The name of the map key.
+ * The name of the map key. If <tt>null</tt>, the column name will be used.
*/
public final String key;
@@ -54,8 +56,12 @@
*/
public final Class<?> type;
+ public Field(String columnName) {
+ this(columnName, null, null);
+ }
+
public Field(String columnName, String key) {
- this(key, columnName, null);
+ this(columnName, key, null);
}
public Field(String columnName, String key, Class<?> type) {
@@ -71,7 +77,7 @@
boolean hasNext;
try {
- hasNext = !resultSet.isAfterLast();
+ hasNext = !resultSet.isLast();
} catch (SQLException exception) {
throw new RuntimeException(exception);
}
@@ -99,7 +105,7 @@
boolean hasPrevious;
try {
- hasPrevious = !resultSet.isBeforeFirst();
+ hasPrevious = !resultSet.isFirst();
} catch (SQLException exception) {
throw new RuntimeException(exception);
}
@@ -150,13 +156,18 @@
} else if (field.type == Double.class
|| field.type == Double.TYPE) {
value = resultSet.getDouble(field.columnName);
- } else if (field.type == Date.class) {
- value = resultSet.getDate(field.columnName);
+ } else if (field.type == String.class) {
+ value = resultSet.getString(field.columnName);
} else {
value = resultSet.getObject(field.columnName);
}
- current.put(field.key, value);
+ if (resultSet.wasNull()) {
+ value = null;
+ }
+
+ String key = (field.key == null) ? field.columnName : field.key;
+ current.put(key, value);
}
} catch (SQLException exception) {
throw new RuntimeException(exception);
@@ -225,6 +236,10 @@
}
for (Field field : fields) {
+ if (field.columnName == null) {
+ throw new IllegalArgumentException("columnName is required.");
+ }
+
if (!(field.type == null
|| field.type == Boolean.class
|| field.type == Boolean.TYPE
@@ -240,7 +255,7 @@
|| field.type == Float.TYPE
|| field.type == Double.class
|| field.type == Double.TYPE
- || field.type == Date.class)) {
+ || field.type == String.class)) {
throw new IllegalArgumentException(field.type.getName()
+ " is not a supported type.");
}
Added: pivot/trunk/tests/src/org/apache/pivot/tests/sql/ResultListTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/sql/ResultListTest.java?rev=899361&view=auto
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/sql/ResultListTest.java (added)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/sql/ResultListTest.java Thu Jan 14 19:14:53 2010
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pivot.tests.sql;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.Statement;
+
+import org.apache.pivot.collections.List;
+import org.apache.pivot.collections.Map;
+import org.apache.pivot.serialization.JSONSerializer;
+import org.apache.pivot.sql.ResultList;
+
+import static java.lang.System.out;
+
+public class ResultListTest {
+ public static void main(String[] args) throws Exception {
+ // e.g. jdbc:mysql://localhost/test
+ String connectionURL = args[0];
+
+ Connection connection = DriverManager.getConnection(connectionURL);
+ Statement statement = connection.createStatement();
+ ResultSet resultSet;
+ ResultList resultList;
+
+ resultSet = statement.executeQuery("SELECT * FROM result_list_test");
+ resultList = new ResultList(resultSet,
+ new ResultList.Field("i"),
+ new ResultList.Field("f"),
+ new ResultList.Field("s"),
+ new ResultList.Field("b"));
+ out.println(JSONSerializer.toString(resultList));
+
+ resultSet = statement.executeQuery("SELECT * FROM result_list_test");
+ resultList = new ResultList(resultSet,
+ new ResultList.Field("i", "integer"),
+ new ResultList.Field("f", "float"),
+ new ResultList.Field("s", "string"),
+ new ResultList.Field("b", "boolean"));
+ out.println(JSONSerializer.toString(resultList));
+
+ resultSet = statement.executeQuery("SELECT * FROM result_list_test");
+ resultList = new ResultList(resultSet,
+ new ResultList.Field("i", "integer", Integer.class),
+ new ResultList.Field("f", "float", Float.class),
+ new ResultList.Field("s", "string", String.class),
+ new ResultList.Field("b", "boolean", Boolean.class));
+ out.println(JSONSerializer.toString(resultList));
+
+ // Test forward and backward iteration
+ resultSet = statement.executeQuery("SELECT * FROM result_list_test");
+ resultList = new ResultList(resultSet,
+ new ResultList.Field("i"),
+ new ResultList.Field("f"),
+ new ResultList.Field("s"),
+ new ResultList.Field("b"));
+
+ List.ItemIterator<Map<String, Object>> iterator = resultList.iterator();
+ while (iterator.hasNext()) {
+ out.println(JSONSerializer.toString(iterator.next()));
+ }
+
+ while(iterator.hasPrevious()) {
+ out.println(JSONSerializer.toString(iterator.previous()));
+ }
+ }
+}
Added: pivot/trunk/tests/src/org/apache/pivot/tests/sql/result_list_test.sql
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/sql/result_list_test.sql?rev=899361&view=auto
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/sql/result_list_test.sql (added)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/sql/result_list_test.sql Thu Jan 14 19:14:53 2010
@@ -0,0 +1,27 @@
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to you under the Apache License,
+-- Version 2.0 (the "License"); you may not use this file except in
+-- compliance with the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+CREATE TABLE result_list_test (
+ i INTEGER,
+ f FLOAT(5),
+ s VARCHAR(256),
+ b TINYINT
+);
+
+INSERT INTO result_list_test (i, f, s, b)
+ VALUES (1, 101.1, 'ABCD', 0);
+
+INSERT INTO result_list_test (i, f, s, b)
+ VALUES (NULL, NULL, NULL, NULL);