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 00:49:35 UTC

svn commit: r899002 - in /incubator/pivot/trunk: core/src/org/apache/pivot/collections/adapter/ core/src/org/apache/pivot/sql/ tutorials/www/

Author: gbrown
Date: Wed Jan 13 23:49:35 2010
New Revision: 899002

URL: http://svn.apache.org/viewvc?rev=899002&view=rev
Log:
Initial pass at org.apache.pivot.sql.ResultList (untested).

Added:
    incubator/pivot/trunk/core/src/org/apache/pivot/sql/
    incubator/pivot/trunk/core/src/org/apache/pivot/sql/ResultList.java
    incubator/pivot/trunk/core/src/org/apache/pivot/sql/package.html
Modified:
    incubator/pivot/trunk/core/src/org/apache/pivot/collections/adapter/ListAdapter.java
    incubator/pivot/trunk/tutorials/www/platform-overview.xml

Modified: incubator/pivot/trunk/core/src/org/apache/pivot/collections/adapter/ListAdapter.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/org/apache/pivot/collections/adapter/ListAdapter.java?rev=899002&r1=899001&r2=899002&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/org/apache/pivot/collections/adapter/ListAdapter.java (original)
+++ incubator/pivot/trunk/core/src/org/apache/pivot/collections/adapter/ListAdapter.java Wed Jan 13 23:49:35 2010
@@ -31,7 +31,7 @@
 
 /**
  * Implementation of the {@link List} interface that is backed by an
- * instance of {@link java.util.List}</tt>.
+ * instance of {@link java.util.List}.
  */
 public class ListAdapter<T> implements List<T>, Serializable {
     private static final long serialVersionUID = 1649736907064653706L;

Added: incubator/pivot/trunk/core/src/org/apache/pivot/sql/ResultList.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/org/apache/pivot/sql/ResultList.java?rev=899002&view=auto
==============================================================================
--- incubator/pivot/trunk/core/src/org/apache/pivot/sql/ResultList.java (added)
+++ incubator/pivot/trunk/core/src/org/apache/pivot/sql/ResultList.java Wed Jan 13 23:49:35 2010
@@ -0,0 +1,331 @@
+/*
+ * 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.sql;
+
+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;
+import org.apache.pivot.collections.HashMap;
+import org.apache.pivot.collections.List;
+import org.apache.pivot.collections.ListListener;
+import org.apache.pivot.collections.Map;
+import org.apache.pivot.collections.Sequence;
+import org.apache.pivot.util.ListenerList;
+
+/**
+ * Implementation of the {@link List} interface that is backed by a
+ * instance of {@link java.sql.ResultSet}.
+ */
+public class ResultList implements List<Map<String, Object>> {
+    /**
+     * Class that maps a result set column to a map key/value pair.
+     */
+    public static final class Field {
+        /**
+         * The source column name.
+         */
+        public final String columnName;
+
+        /**
+         * The name of the map key.
+         */
+        public final String key;
+
+        /**
+         * The type of the map value. If <tt>null</tt>, the default SQL type will be used.
+         */
+        public final Class<?> type;
+
+        public Field(String columnName, String key) {
+            this(key, columnName, null);
+        }
+
+        public Field(String columnName, String key, Class<?> type) {
+            this.columnName = columnName;
+            this.key = key;
+            this.type = type;
+        }
+    }
+
+    private class ResultListItemIterator implements ItemIterator<Map<String, Object>> {
+        @Override
+        public boolean hasNext() {
+            boolean hasNext;
+
+            try {
+                hasNext = !resultSet.isAfterLast();
+            } catch (SQLException exception) {
+                throw new RuntimeException(exception);
+            }
+
+            return hasNext;
+        }
+
+        @Override
+        public Map<String, Object> next() {
+            if (!hasNext()) {
+                throw new NoSuchElementException();
+            }
+
+            try {
+                resultSet.next();
+            } catch (SQLException exception) {
+                throw new RuntimeException(exception);
+            }
+
+            return current();
+        }
+
+        @Override
+        public boolean hasPrevious() {
+            boolean hasPrevious;
+
+            try {
+                hasPrevious = !resultSet.isBeforeFirst();
+            } catch (SQLException exception) {
+                throw new RuntimeException(exception);
+            }
+
+            return hasPrevious;
+        }
+
+        @Override
+        public Map<String, Object> previous() {
+            if (!hasPrevious()) {
+                throw new NoSuchElementException();
+            }
+
+            try {
+                resultSet.previous();
+            } catch (SQLException exception) {
+                throw new RuntimeException(exception);
+            }
+
+            return current();
+        }
+
+        private Map<String, Object> current() {
+            HashMap<String, Object> current = new HashMap<String, Object>();
+
+            try {
+                for (Field field : fields) {
+                    Object value;
+
+                    if (field.type == Boolean.class
+                        || field.type == Boolean.TYPE) {
+                        value = resultSet.getBoolean(field.columnName);
+                    } else if (field.type == Byte.class
+                        || field.type == Byte.TYPE) {
+                        value = resultSet.getByte(field.columnName);
+                    } else if (field.type == Short.class
+                        || field.type == Short.TYPE) {
+                        value = resultSet.getShort(field.columnName);
+                    } else if (field.type == Integer.class
+                        || field.type == Integer.TYPE) {
+                        value = resultSet.getInt(field.columnName);
+                    } else if (field.type == Long.class
+                        || field.type == Long.TYPE) {
+                        value = resultSet.getLong(field.columnName);
+                    } else if (field.type == Float.class
+                        || field.type == Float.TYPE) {
+                        value = resultSet.getFloat(field.columnName);
+                    } 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 {
+                        value = resultSet.getObject(field.columnName);
+                    }
+
+                    current.put(field.key, value);
+                }
+            } catch (SQLException exception) {
+                throw new RuntimeException(exception);
+            }
+
+            return current;
+        }
+
+        @Override
+        public void toStart() {
+            try {
+                resultSet.beforeFirst();
+            } catch (SQLException exception) {
+                throw new RuntimeException(exception);
+            }
+        }
+
+        @Override
+        public void toEnd() {
+            try {
+                resultSet.afterLast();
+            } catch (SQLException exception) {
+                throw new RuntimeException(exception);
+            }
+        }
+
+        @Override
+        public void insert(Map<String, Object> item) {
+            // TODO
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public void update(Map<String, Object> item) {
+            // TODO
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public void remove() {
+            try {
+                resultSet.deleteRow();
+            } catch (SQLException exception) {
+                throw new RuntimeException(exception);
+            }
+        }
+    }
+
+    private ResultSet resultSet;
+    private ArrayList<Field> fields;
+
+    private ListListenerList<Map<String, Object>> listListeners =
+        new ListListenerList<Map<String,Object>>();
+
+    public ResultList(ResultSet resultSet, Field... fields) {
+        this(resultSet, new ArrayList<Field>(fields));
+    }
+
+    public ResultList(ResultSet resultSet, Sequence<Field> fields) {
+        this(resultSet, new ArrayList<Field>(fields));
+    }
+
+    private ResultList(ResultSet resultSet, ArrayList<Field> fields) {
+        if (resultSet == null) {
+            throw new IllegalArgumentException();
+        }
+
+        for (Field field : fields) {
+            if (!(field.type == null
+                || field.type == Boolean.class
+                || field.type == Boolean.TYPE
+                || field.type == Byte.class
+                || field.type == Byte.TYPE
+                || field.type == Short.class
+                || field.type == Short.TYPE
+                || field.type == Integer.class
+                || field.type == Integer.TYPE
+                || field.type == Long.class
+                || field.type == Long.TYPE
+                || field.type == Float.class
+                || field.type == Float.TYPE
+                || field.type == Double.class
+                || field.type == Double.TYPE
+                || field.type == Date.class)) {
+                throw new IllegalArgumentException(field.type.getName()
+                    + " is not a supported type.");
+            }
+        }
+
+        this.resultSet = resultSet;
+        this.fields = fields;
+    }
+
+
+    public Field getField(int index) {
+        return fields.get(index);
+    }
+
+    public int getFieldCount() {
+        return fields.getLength();
+    }
+
+    @Override
+    public int add(Map<String, Object> item) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public void insert(Map<String, Object> item, int index) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public Map<String, Object> update(int index, Map<String, Object> item) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public int remove(Map<String, Object> item) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public Sequence<Map<String, Object>> remove(int index, int count) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public void clear() {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public Map<String, Object> get(int index) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public int indexOf(Map<String, Object> item) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public boolean isEmpty() {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public int getLength() {
+        return -1;
+    }
+
+    @Override
+    public Comparator<Map<String, Object>> getComparator() {
+        return null;
+    }
+
+    @Override
+    public void setComparator(Comparator<Map<String, Object>> comparator) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public ItemIterator<Map<String, Object>> iterator() {
+        return new ResultListItemIterator();
+    }
+
+    @Override
+    public ListenerList<ListListener<Map<String, Object>>> getListListeners() {
+        return listListeners;
+    }
+}

Added: incubator/pivot/trunk/core/src/org/apache/pivot/sql/package.html
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/org/apache/pivot/sql/package.html?rev=899002&view=auto
==============================================================================
--- incubator/pivot/trunk/core/src/org/apache/pivot/sql/package.html (added)
+++ incubator/pivot/trunk/core/src/org/apache/pivot/sql/package.html Wed Jan 13 23:49:35 2010
@@ -0,0 +1,23 @@
+<!--
+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.
+-->
+
+<html>
+<head></head>
+<body>
+<p>Contains classes for working with SQL data.</p>
+</body>
+</html>

Modified: incubator/pivot/trunk/tutorials/www/platform-overview.xml
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tutorials/www/platform-overview.xml?rev=899002&r1=899001&r2=899002&view=diff
==============================================================================
--- incubator/pivot/trunk/tutorials/www/platform-overview.xml (original)
+++ incubator/pivot/trunk/tutorials/www/platform-overview.xml Wed Jan 13 23:49:35 2010
@@ -87,6 +87,9 @@
                 <tt>org.apache.pivot.serialization</tt> - Classes for use in data serialization.
             </li>
             <li>
+                <tt>org.apache.pivot.sql</tt> - Classes for working with SQL data.
+            </li>
+            <li>
                 <tt>org.apache.pivot.text</tt> - Classes for working with text.
             </li>
             <li>