You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by pc...@apache.org on 2006/07/19 23:35:07 UTC

svn commit: r423615 [7/44] - in /incubator/openjpa/trunk: ./ openjpa-jdbc-5/ openjpa-jdbc-5/src/ openjpa-jdbc-5/src/main/ openjpa-jdbc-5/src/main/java/ openjpa-jdbc-5/src/main/java/org/ openjpa-jdbc-5/src/main/java/org/apache/ openjpa-jdbc-5/src/main/j...

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Const.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Const.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Const.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Const.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,211 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.Map;
+
+import org.apache.openjpa.jdbc.kernel.JDBCFetchState;
+import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.meta.JavaSQLTypes;
+import org.apache.openjpa.jdbc.schema.Column;
+import org.apache.openjpa.jdbc.sql.Joins;
+import org.apache.openjpa.jdbc.sql.Result;
+import org.apache.openjpa.jdbc.sql.SQLBuffer;
+import org.apache.openjpa.jdbc.sql.Select;
+import org.apache.openjpa.kernel.Filters;
+import org.apache.openjpa.kernel.exps.Constant;
+import org.apache.openjpa.meta.ClassMetaData;
+import org.apache.openjpa.meta.JavaTypes;
+
+/**
+ * A literal or parameter in the filter.
+ *
+ * @author Abe White
+ */
+abstract class Const
+    implements Val, Constant {
+
+    private ClassMetaData _meta = null;
+    private Column[] _cols = null;
+
+    public ClassMetaData getMetaData() {
+        return _meta;
+    }
+
+    public void setMetaData(ClassMetaData meta) {
+        _meta = meta;
+    }
+
+    public boolean isVariable() {
+        return false;
+    }
+
+    /**
+     * Return the column for the value at the specified index, or null.
+     */
+    public Column getColumn(int index) {
+        return (_cols != null && _cols.length > index) ? _cols[index] : null;
+    }
+
+    /**
+     * Return the value of this constant.
+     */
+    public abstract Object getValue();
+
+    public Object getValue(Object[] parameters) {
+        return getValue();
+    }
+
+    /**
+     * Return the SQL value of this constant.
+     */
+    public Object getSQLValue() {
+        return getValue();
+    }
+
+    /**
+     * Return true if this constant's SQL value is equivalent to NULL.
+     */
+    public boolean isSQLValueNull() {
+        Object val = getSQLValue();
+        if (val == null)
+            return true;
+        if (!(val instanceof Object[]))
+            return false;
+
+        // all-null array is considered null
+        Object[] arr = (Object[]) val;
+        for (int i = 0; i < arr.length; i++)
+            if (arr[i] != null)
+                return false;
+        return true;
+    }
+
+    public void initialize(Select sel, JDBCStore store, boolean nullTest) {
+    }
+
+    public Joins getJoins() {
+        return null;
+    }
+
+    public void calculateValue(Select sel, JDBCStore store,
+        Object[] params, Val other, JDBCFetchState fetchState) {
+        if (other instanceof PCPath)
+            _cols = ((PCPath) other).getColumns();
+        else
+            _cols = null;
+    }
+
+    public Object toDataStoreValue(Object val, JDBCStore store) {
+        return val;
+    }
+
+    public void select(Select sel, JDBCStore store, Object[] params,
+        boolean pks, JDBCFetchState fetchState) {
+        sel.select(newSQLBuffer(sel, store, params, fetchState), this);
+    }
+
+    private SQLBuffer newSQLBuffer(Select sel, JDBCStore store,
+        Object[] params, JDBCFetchState fetchState) {
+        calculateValue(sel, store, params, null, fetchState);
+        SQLBuffer buf = new SQLBuffer(store.getDBDictionary());
+        appendTo(buf, 0, sel, store, params, fetchState);
+        clearParameters();
+        return buf;
+    }
+
+    public void selectColumns(Select sel, JDBCStore store,
+        Object[] params, boolean pks, JDBCFetchState fetchState) {
+    }
+
+    public void groupBy(Select sel, JDBCStore store, Object[] params,
+        JDBCFetchState fetchState) {
+        sel.groupBy(newSQLBuffer(sel, store, params, fetchState), false);
+    }
+
+    public void orderBy(Select sel, JDBCStore store, Object[] params,
+        boolean asc, JDBCFetchState fetchState) {
+        sel.orderBy(newSQLBuffer(sel, store, params, fetchState), asc, false);
+    }
+
+    public Object load(Result res, JDBCStore store,
+        JDBCFetchState fetchState)
+        throws SQLException {
+        int code = JavaTypes.getTypeCode(getType());
+        if (code == JavaTypes.OBJECT)
+            code = JavaSQLTypes.JDBC_DEFAULT;
+        return Filters.convert(res.getObject(this, code, null), getType());
+    }
+
+    public boolean hasVariable(Variable var) {
+        return false;
+    }
+
+    public int length() {
+        return 1;
+    }
+
+    public void appendIsEmpty(SQLBuffer sql, Select sel,
+        JDBCStore store, Object[] params, JDBCFetchState fetchState) {
+        Object obj = getValue();
+        if (obj instanceof Collection && ((Collection) obj).isEmpty())
+            sql.append("1 = 1");
+        else if (obj instanceof Map && ((Map) obj).isEmpty())
+            sql.append("1 = 1");
+        else
+            sql.append("1 <> 1");
+    }
+
+    public void appendIsNotEmpty(SQLBuffer sql, Select sel,
+        JDBCStore store, Object[] params, JDBCFetchState fetchState) {
+        Object obj = getValue();
+        if (obj instanceof Collection && ((Collection) obj).isEmpty())
+            sql.append("1 <> 1");
+        else if (obj instanceof Map && ((Map) obj).isEmpty())
+            sql.append("1 <> 1");
+        else
+            sql.append("1 = 1");
+    }
+
+    public void appendSize(SQLBuffer sql, Select sel, JDBCStore store,
+        Object[] params, JDBCFetchState fetchState) {
+        Object obj = getValue();
+        if (obj instanceof Collection)
+            sql.appendValue(((Collection) obj).size());
+        else if (obj instanceof Map)
+            sql.appendValue(((Map) obj).size());
+        else
+            sql.append("1");
+    }
+
+    public void appendIsNull(SQLBuffer sql, Select sel,
+        JDBCStore store, Object[] params, JDBCFetchState fetchState) {
+        if (getSQLValue() == null)
+            sql.append("1 = 1");
+        else
+            sql.append("1 <> 1");
+    }
+
+    public void appendIsNotNull(SQLBuffer sql, Select sel,
+        JDBCStore store, Object[] params, JDBCFetchState fetchState) {
+        if (getSQLValue() != null)
+            sql.append("1 = 1");
+        else
+            sql.append("1 <> 1");
+    }
+}

Propchange: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Const.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ConstGetObjectId.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ConstGetObjectId.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ConstGetObjectId.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ConstGetObjectId.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import org.apache.openjpa.jdbc.kernel.JDBCFetchState;
+import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.sql.SQLBuffer;
+import org.apache.openjpa.jdbc.sql.Select;
+
+/**
+ * Obtaining the object id of a constant.
+ *
+ * @author Abe White
+ */
+class ConstGetObjectId
+    extends Const {
+
+    private final Const _constant;
+    private Object _val = null;
+    private Object _sqlVal = null;
+    private int _otherLen = 0;
+
+    /**
+     * Constructor. Supply constant to traverse.
+     */
+    public ConstGetObjectId(Const constant) {
+        _constant = constant;
+    }
+
+    public Class getType() {
+        return Object.class;
+    }
+
+    public void setImplicitType(Class type) {
+    }
+
+    public Object getValue() {
+        return _val;
+    }
+
+    public Object getSQLValue() {
+        return _sqlVal;
+    }
+
+    public void calculateValue(Select sel, JDBCStore store,
+        Object[] params, Val other, JDBCFetchState fetchState) {
+        super.calculateValue(sel, store, params, other, fetchState);
+        _constant.calculateValue(sel, store, params, null, fetchState);
+        _val = store.getContext().getObjectId(_constant.getValue());
+        if (other != null) {
+            _sqlVal = other.toDataStoreValue(_val, store);
+            _otherLen = other.length();
+        } else
+            _sqlVal = _val;
+    }
+
+    public void appendTo(SQLBuffer sql, int index, Select sel,
+        JDBCStore store, Object[] params, JDBCFetchState fetchState) {
+        if (_otherLen > 1)
+            sql.appendValue(((Object[]) _sqlVal)[index], getColumn(index));
+        else
+            sql.appendValue(_sqlVal, getColumn(index));
+    }
+
+    public void clearParameters() {
+        _constant.clearParameters();
+        _val = null;
+        _sqlVal = null;
+    }
+}

Propchange: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ConstGetObjectId.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ConstInstanceofExpression.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ConstInstanceofExpression.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ConstInstanceofExpression.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ConstInstanceofExpression.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import java.util.Map;
+
+import org.apache.openjpa.jdbc.kernel.JDBCFetchState;
+import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.sql.Joins;
+import org.apache.openjpa.jdbc.sql.SQLBuffer;
+import org.apache.openjpa.jdbc.sql.Select;
+import org.apache.openjpa.kernel.Filters;
+
+/**
+ * Tests whether a value is an instance of a class.
+ *
+ * @author Abe White
+ */
+class ConstInstanceofExpression
+    implements Exp {
+
+    private final Const _const;
+    private final Class _cls;
+
+    /**
+     * Constructor. Supply the constant to test and the class.
+     */
+    public ConstInstanceofExpression(Const val, Class cls) {
+        _const = val;
+        _cls = Filters.wrap(cls);
+    }
+
+    public void initialize(Select sel, JDBCStore store,
+        Object[] params, Map contains) {
+        _const.initialize(sel, store, false);
+    }
+
+    public void appendTo(SQLBuffer buf, Select sel, JDBCStore store,
+        Object[] params, JDBCFetchState fetchState) {
+        _const.calculateValue(sel, store, params, null, fetchState);
+        if (_cls.isInstance(_const.getValue()))
+            buf.append("1 = 1");
+        else
+            buf.append("1 <> 1");
+        _const.clearParameters();
+    }
+
+    public void selectColumns(Select sel, JDBCStore store,
+        Object[] params, boolean pks, JDBCFetchState fetchState) {
+        _const.selectColumns(sel, store, params, pks, fetchState);
+    }
+
+    public Joins getJoins() {
+        return _const.getJoins();
+    }
+
+    public boolean hasContainsExpression() {
+        return false;
+    }
+
+    public boolean hasVariable(Variable var) {
+        return _const.hasVariable(var);
+    }
+}

Propchange: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ConstInstanceofExpression.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ConstPath.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ConstPath.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ConstPath.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ConstPath.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,178 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.ListIterator;
+
+import org.apache.openjpa.enhance.PersistenceCapable;
+import org.apache.openjpa.jdbc.kernel.JDBCFetchState;
+import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.sql.SQLBuffer;
+import org.apache.openjpa.jdbc.sql.Select;
+import org.apache.openjpa.kernel.Broker;
+import org.apache.openjpa.kernel.Filters;
+import org.apache.openjpa.kernel.OpenJPAStateManager;
+import org.apache.openjpa.meta.ClassMetaData;
+import org.apache.openjpa.meta.FieldMetaData;
+
+/**
+ * A field traversal starting with a constant filter parameter.
+ *
+ * @author Abe White
+ */
+class ConstPath
+    extends Const
+    implements JDBCPath {
+
+    private final Const _constant;
+    private final LinkedList _actions = new LinkedList();
+    private Object _val = null;
+    private Object _sqlVal = null;
+    private int _otherLen = 0;
+
+    /**
+     * Constructor. Supply constant to traverse.
+     */
+    public ConstPath(Const constant) {
+        _constant = constant;
+    }
+
+    public Class getType() {
+        if (_actions.isEmpty()) {
+            ClassMetaData meta = getMetaData();
+            if (meta == null)
+                return Object.class;
+            return meta.getDescribedType();
+        }
+
+        Object last = _actions.getLast();
+        if (last instanceof Class)
+            return (Class) last;
+        FieldMetaData fmd = (FieldMetaData) last;
+        return fmd.getDeclaredType();
+    }
+
+    public void setImplicitType(Class type) {
+        _actions.add(type);
+    }
+
+    public void get(FieldMetaData field, boolean nullTraversal) {
+        _actions.add(field);
+    }
+
+    public void getKey() {
+    }
+
+    public FieldMetaData last() {
+        ListIterator itr = _actions.listIterator(_actions.size());
+        Object prev;
+        while (itr.hasPrevious()) {
+            prev = itr.previous();
+            if (prev instanceof FieldMetaData)
+                return (FieldMetaData) prev;
+        }
+        return null;
+    }
+
+    public Object getValue() {
+        return _val;
+    }
+
+    public Object getSQLValue() {
+        return _sqlVal;
+    }
+
+    public void calculateValue(Select sel, JDBCStore store,
+        Object[] params, Val other, JDBCFetchState fetchState) {
+        super.calculateValue(sel, store, params, other, fetchState);
+        _constant.calculateValue(sel, store, params, null, fetchState);
+        _val = _constant.getValue();
+        boolean failed = false;
+
+        // copied from org.apache.openjpa.query.InMemoryPath
+        Object action;
+        OpenJPAStateManager sm;
+        Broker tmpBroker = null;
+        for (Iterator itr = _actions.iterator(); itr.hasNext();) {
+            // fail on null value
+            if (_val == null) {
+                failed = true;
+                break;
+            }
+
+            action = itr.next();
+            if (action instanceof Class) {
+                try {
+                    _val = Filters.convert(_val, (Class) action);
+                    continue;
+                } catch (ClassCastException cce) {
+                    failed = true;
+                    break;
+                }
+            }
+
+            // make sure we can access the instance; even non-pc vals might
+            // be proxyable
+            sm = null;
+            tmpBroker = null;
+            if (_val instanceof PersistenceCapable)
+                sm = (OpenJPAStateManager) ((PersistenceCapable) _val).
+                    pcGetStateManager();
+            if (sm == null) {
+                tmpBroker = store.getContext().getBroker();
+                tmpBroker.transactional(_val, false, null);
+                sm = tmpBroker.getStateManager(_val);
+            }
+
+            try {
+                // get the specified field value and switch candidate
+                _val = sm.fetchField(((FieldMetaData) action).getIndex(),
+                    true);
+            } finally {
+                // setTransactional does not clear the state, which is
+                // important since tmpVal might be also managed by
+                // another broker if it's a proxied non-pc instance
+                if (tmpBroker != null)
+                    tmpBroker.nontransactional(sm.getManagedInstance(), null);
+            }
+        }
+
+        if (failed)
+            _val = null;
+
+        if (other != null) {
+            _sqlVal = other.toDataStoreValue(_val, store);
+            _otherLen = other.length();
+        } else
+            _sqlVal = _val;
+    }
+
+    public void appendTo(SQLBuffer sql, int index, Select sel,
+        JDBCStore store, Object[] params, JDBCFetchState fetchState) {
+        if (_otherLen > 1)
+            sql.appendValue(((Object[]) _sqlVal)[index], getColumn(index));
+        else
+            sql.appendValue(_sqlVal, getColumn(index));
+    }
+
+    public void clearParameters() {
+        _constant.clearParameters();
+        _val = null;
+        _sqlVal = null;
+    }
+}

Propchange: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ConstPath.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ContainsExpression.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ContainsExpression.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ContainsExpression.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ContainsExpression.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import java.util.Map;
+
+import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.sql.Select;
+import serp.util.Numbers;
+
+/**
+ * Tests whether one value contains another.
+ *
+ * @author Abe White
+ */
+class ContainsExpression
+    extends EqualExpression {
+
+    /**
+     * Constructor. Supply values to test.
+     */
+    public ContainsExpression(Val val1, Val val2) {
+        super(val1, val2);
+    }
+
+    public void initialize(Select sel, JDBCStore store,
+        Object[] params, Map contains) {
+        Val val1 = getValue1();
+        if (contains != null && val1 instanceof PCPath) {
+            PCPath sql = (PCPath) val1;
+            String path = sql.getPath();
+
+            // update the count for this path
+            Integer count = (Integer) contains.get(path);
+            if (count == null)
+                count = Numbers.valueOf(0);
+            else
+                count = Numbers.valueOf(count.intValue() + 1);
+            contains.put(path, count);
+
+            sql.setContainsId(count.toString());
+        }
+        super.initialize(sel, store, params, contains);
+    }
+
+    protected boolean isDirectComparison() {
+        return false;
+    }
+
+    public boolean hasContainsExpression() {
+        return true;
+    }
+}

Propchange: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ContainsExpression.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ContainsKeyExpression.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ContainsKeyExpression.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ContainsKeyExpression.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ContainsKeyExpression.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import java.util.Map;
+
+import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.sql.Select;
+
+/**
+ * Tests whether a map value contains a key.
+ *
+ * @author Abe White
+ */
+class ContainsKeyExpression
+    extends ContainsExpression {
+
+    /**
+     * Constructor. Supply values to test.
+     */
+    public ContainsKeyExpression(Val val1, Val val2) {
+        super(val1, val2);
+    }
+
+    public void initialize(Select sel, JDBCStore store,
+        Object[] params, Map contains) {
+        Val val1 = getValue1();
+        if (val1 instanceof PCPath)
+            ((PCPath) val1).getKey();
+
+        super.initialize(sel, store, params, contains);
+    }
+}

Propchange: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ContainsKeyExpression.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Count.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Count.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Count.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Count.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.sql.Select;
+
+/**
+ * Count non-null values.
+ *
+ * @author Abe White
+ */
+class Count
+    extends UnaryOp {
+
+    /**
+     * Constructor. Provide the value to operate on.
+     */
+    public Count(Val val) {
+        super(val);
+    }
+
+    public void initialize(Select sel, JDBCStore store, boolean nullTest) {
+        super.initialize(sel, store, nullTest);
+
+        // join into related object if present
+        if (getVal()instanceof PCPath)
+            ((PCPath) getVal()).joinRelation();
+    }
+
+    protected Class getType(Class c) {
+        return long.class;
+    }
+
+    protected String getOperator() {
+        return "COUNT";
+    }
+
+    protected boolean isAggregate() {
+        return true;
+    }
+}
+

Propchange: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Count.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/CurrentDate.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/CurrentDate.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/CurrentDate.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/CurrentDate.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import java.util.Date;
+
+import org.apache.openjpa.jdbc.kernel.JDBCFetchState;
+import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.sql.SQLBuffer;
+import org.apache.openjpa.jdbc.sql.Select;
+
+/**
+ * A literal current DATE/TIME/TIMESTAMP value in a filter.
+ *
+ * @author Marc Prud'hommeaux
+ */
+class CurrentDate
+    extends Const {
+
+    static final int DATE = 1;
+    static final int TIME = 2;
+    static final int TIMESTAMP = 3;
+
+    private final int _type;
+
+    CurrentDate(int type) {
+        _type = type;
+    }
+
+    public Class getType() {
+        return Date.class;
+    }
+
+    public void setImplicitType(Class type) {
+    }
+
+    public Object getValue() {
+        return new Date();
+    }
+
+    public void calculateValue(Select sel, JDBCStore store,
+        Object[] params, Val other, JDBCFetchState fetchState) {
+    }
+
+    public void appendTo(SQLBuffer sql, int index, Select sel,
+        JDBCStore store, Object[] params, JDBCFetchState fetchState) {
+        if (_type == DATE)
+            sql.append(store.getDBDictionary().currentDateFunction);
+        else if (_type == TIME)
+            sql.append(store.getDBDictionary().currentTimeFunction);
+        else if (_type == TIMESTAMP)
+            sql.append(store.getDBDictionary().currentTimestampFunction);
+    }
+
+    public void clearParameters() {
+    }
+}

Propchange: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/CurrentDate.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Distinct.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Distinct.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Distinct.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Distinct.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.sql.Select;
+
+/**
+ * Distinct the specified path.
+ *
+ * @author Marc Prud'hommeaux
+ */
+class Distinct
+    extends UnaryOp {
+
+    public Distinct(Val val) {
+        super(val);
+    }
+
+    public void initialize(Select sel, JDBCStore store, boolean nullTest) {
+        super.initialize(sel, store, nullTest);
+
+        // join into related object if present
+        if (getVal()instanceof PCPath)
+            ((PCPath) getVal()).joinRelation();
+    }
+
+    protected String getOperator() {
+        return "DISTINCT";
+    }
+
+    protected boolean isAggregate() {
+        return false;
+    }
+}
+

Propchange: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Distinct.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/EmptyExpression.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/EmptyExpression.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/EmptyExpression.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/EmptyExpression.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import java.util.Map;
+
+import org.apache.openjpa.jdbc.kernel.JDBCFetchState;
+import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.sql.Joins;
+import org.apache.openjpa.jdbc.sql.SQLBuffer;
+import org.apache.openjpa.jdbc.sql.Select;
+
+/**
+ * An empty expression.
+ *
+ * @author Abe White
+ */
+class EmptyExpression
+    implements Exp {
+
+    public void initialize(Select sel, JDBCStore store,
+        Object[] params, Map contains) {
+    }
+
+    public void appendTo(SQLBuffer buf, Select sel, JDBCStore store,
+        Object[] params, JDBCFetchState fetchState) {
+    }
+
+    public void selectColumns(Select sel, JDBCStore store,
+        Object[] params, boolean pks, JDBCFetchState fetchState) {
+    }
+
+    public Joins getJoins() {
+        return null;
+    }
+
+    public boolean hasContainsExpression() {
+        return false;
+    }
+
+    public boolean hasVariable(Variable var) {
+        return false;
+    }
+}

Propchange: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/EmptyExpression.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/EndsWithExpression.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/EndsWithExpression.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/EndsWithExpression.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/EndsWithExpression.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,202 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import java.util.Map;
+
+import org.apache.openjpa.jdbc.kernel.JDBCFetchState;
+import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.meta.ClassMapping;
+import org.apache.openjpa.jdbc.meta.FieldMapping;
+import org.apache.openjpa.jdbc.schema.Column;
+import org.apache.openjpa.jdbc.schema.Table;
+import org.apache.openjpa.jdbc.sql.DBDictionary;
+import org.apache.openjpa.jdbc.sql.Joins;
+import org.apache.openjpa.jdbc.sql.SQLBuffer;
+import org.apache.openjpa.jdbc.sql.Select;
+
+/**
+ * Test if one string ends with another.
+ *
+ * @author Abe White
+ */
+class EndsWithExpression
+    implements Exp {
+
+    private final Val _val1;
+    private final Val _val2;
+    private Joins _joins = null;
+    private String _pre = null;
+    private String _post = null;
+
+    /**
+     * Constructor. Supply values.
+     */
+    public EndsWithExpression(Val val1, Val val2) {
+        _val1 = val1;
+        _val2 = val2;
+    }
+
+    public void initialize(Select sel, JDBCStore store,
+        Object[] params, Map contains) {
+        _val1.initialize(sel, store, false);
+        _val2.initialize(sel, store, false);
+        _joins = sel.and(_val1.getJoins(), _val2.getJoins());
+
+        DBDictionary dict = store.getDBDictionary();
+        String func = dict.stringLengthFunction;
+        if (func != null) {
+            int idx = func.indexOf("{0}");
+            _pre = func.substring(0, idx);
+            _post = func.substring(idx + 3);
+        }
+    }
+
+    public void appendTo(SQLBuffer buf, Select sel, JDBCStore store,
+        Object[] params, JDBCFetchState fetchState) {
+        _val1.calculateValue(sel, store, params, _val2, fetchState);
+        _val2.calculateValue(sel, store, params, _val1, fetchState);
+
+        if (_val1 instanceof Const && ((Const) _val1).getValue() == null)
+            buf.append("1 <> 1");
+        else if (_val2 instanceof Const) {
+            Object o = ((Const) _val2).getValue();
+            if (o == null)
+                buf.append("1 <> 1");
+            else {
+                Column col = null;
+                if (_val1 instanceof PCPath) {
+                    Column[] cols = ((PCPath) _val1).getColumns();
+                    if (cols.length == 1)
+                        col = cols[0];
+                }
+
+                _val1.appendTo(buf, 0, sel, store, params, fetchState);
+                buf.append(" LIKE ");
+                buf.appendValue("%" + o.toString(), col);
+            }
+        } else {
+            // if we can't use LIKE, we have to take the substring of the
+            // first value and compare it to the second
+            DBDictionary dict = store.getDBDictionary();
+            dict.assertSupport(_pre != null, "StringLengthFunction");
+            dict.substring(buf,
+                new FilterValueImpl(_val1, sel, store, params, fetchState),
+                new StringLengthDifferenceFilterValue(sel, store, params,
+                    fetchState), null);
+            buf.append(" = ");
+            _val2.appendTo(buf, 0, sel, store, params, fetchState);
+        }
+
+        sel.append(buf, _joins);
+        _val1.clearParameters();
+        _val2.clearParameters();
+    }
+
+    public void selectColumns(Select sel, JDBCStore store,
+        Object[] params, boolean pks, JDBCFetchState fetchState) {
+        _val1.selectColumns(sel, store, params, true, fetchState);
+        _val2.selectColumns(sel, store, params, true, fetchState);
+    }
+
+    public Joins getJoins() {
+        return _joins;
+    }
+
+    public boolean hasContainsExpression() {
+        return false;
+    }
+
+    public boolean hasVariable(Variable var) {
+        return _val1.hasVariable(var) || _val2.hasVariable(var);
+    }
+
+    /**
+     * Evaluates to the length of a given value.
+     */
+    private class StringLengthDifferenceFilterValue
+        implements FilterValue {
+
+        private final Select _sel;
+        private final JDBCStore _store;
+        private final Object[] _params;
+        private final JDBCFetchState _fetchState;
+
+        public StringLengthDifferenceFilterValue(Select sel,
+            JDBCStore store, Object[] params, JDBCFetchState fetchState) {
+            _sel = sel;
+            _store = store;
+            _params = params;
+            _fetchState = fetchState;
+        }
+
+        public Class getType() {
+            return int.class;
+        }
+
+        public int length() {
+            return 1;
+        }
+
+        public void appendTo(SQLBuffer buf) {
+            appendTo(buf, 0);
+        }
+
+        public void appendTo(SQLBuffer buf, int index) {
+            buf.append(_pre);
+            _val1.appendTo(buf, index, _sel, _store, _params, _fetchState);
+            buf.append(_post).append(" - ").append(_pre);
+            _val2.appendTo(buf, index, _sel, _store, _params, _fetchState);
+            buf.append(_post);
+        }
+
+        public String getColumnAlias(Column col) {
+            return _sel.getColumnAlias(col, _joins);
+        }
+
+        public String getColumnAlias(String col, Table table) {
+            return _sel.getColumnAlias(col, table, _joins);
+        }
+
+        public Object toDataStoreValue(Object val) {
+            return val;
+        }
+
+        public boolean isConstant() {
+            return false;
+        }
+
+        public Object getValue() {
+            return null;
+        }
+
+        public Object getSQLValue() {
+            return null;
+        }
+
+        public boolean isPath() {
+            return false;
+        }
+
+        public ClassMapping getClassMapping() {
+            return null;
+        }
+
+        public FieldMapping getFieldMapping() {
+            return null;
+        }
+    }
+}

Propchange: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/EndsWithExpression.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/EqualExpression.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/EqualExpression.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/EqualExpression.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/EqualExpression.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import org.apache.openjpa.jdbc.kernel.JDBCFetchState;
+import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.sql.SQLBuffer;
+import org.apache.openjpa.jdbc.sql.Select;
+
+/**
+ * Compares two values.
+ *
+ * @author Abe White
+ */
+class EqualExpression
+    extends CompareEqualExpression {
+
+    /**
+     * Constructor. Supply values to compare.
+     */
+    public EqualExpression(Val val1, Val val2) {
+        super(val1, val2);
+    }
+
+    public void appendTo(SQLBuffer buf, Select sel, JDBCStore store,
+        Object[] params, JDBCFetchState fetchState,
+        boolean val1Null, boolean val2Null) {
+        if (val1Null && val2Null)
+            buf.appendValue(null).append(" IS ").appendValue(null);
+        else if (val1Null || val2Null) {
+            Val val = (val1Null) ? getValue2() : getValue1();
+            if (!isDirectComparison()) {
+                int len = val.length();
+                for (int i = 0; i < len; i++) {
+                    if (i > 0)
+                        buf.append(" AND ");
+                    val.appendTo(buf, i, sel, store, params, fetchState);
+                    buf.append(" IS ").appendValue(null);
+                }
+            } else
+                val.appendIsNull(buf, sel, store, params, fetchState);
+        } else {
+            Val val1 = getValue1();
+            Val val2 = getValue2();
+            if (val1.length() == 1 && val2.length() == 1) {
+                store.getDBDictionary().comparison(buf, "=",
+                    new FilterValueImpl(val1, sel, store, params, fetchState),
+                    new FilterValueImpl(val2, sel, store, params, fetchState));
+            } else {
+                int len = java.lang.Math.max(val1.length(), val2.length());
+                for (int i = 0; i < len; i++) {
+                    if (i > 0)
+                        buf.append(" AND ");
+
+                    val1.appendTo(buf, i, sel, store, params, fetchState);
+                    buf.append(" = ");
+                    val2.appendTo(buf, i, sel, store, params, fetchState);
+                }
+            }
+        }
+    }
+}

Propchange: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/EqualExpression.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Exp.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Exp.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Exp.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Exp.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import java.util.Map;
+
+import org.apache.openjpa.jdbc.kernel.JDBCFetchState;
+import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.sql.Joins;
+import org.apache.openjpa.jdbc.sql.SQLBuffer;
+import org.apache.openjpa.jdbc.sql.Select;
+import org.apache.openjpa.kernel.exps.Expression;
+
+/**
+ * An Expression represents a query ready for execution. Generally, it is
+ * a set of conditions that must be met for the query to be true.
+ *
+ * @author Abe White
+ */
+interface Exp
+    extends Expression {
+
+    /**
+     * Initialize the expression. This method should recursively
+     * initialize any sub-expressions or values. It should also cache
+     * the {@link Joins} instance containing the joins for this expression.
+     *
+     * @param params the parameter values; the initialization process
+     * should not rely on exact values, but may need
+     * to see if parameter values are null
+     * @param contains map of relation paths to the number of times
+     * the paths appear in a contains() expression;
+     * used to ensure paths used for contains() within
+     * the same AND expression used different aliases
+     */
+    public void initialize(Select sel, JDBCStore store,
+        Object[] params, Map contains);
+
+    /**
+     * Append the SQL for this expression to the given buffer. The SQL
+     * should optionally include any joins this expression needs.
+     */
+    public void appendTo(SQLBuffer buf, Select sel, JDBCStore store,
+        Object[] params, JDBCFetchState fetchState);
+
+    /**
+     * Select just the columns for this value.
+     */
+    public void selectColumns(Select sel, JDBCStore store,
+        Object[] params, boolean pks, JDBCFetchState fetchState);
+
+    /**
+     * Return the joins for this expression. These joins should be created
+     * and cached during the {@link #initialize} method. The parent
+     * expression might modify these joins during its own initialization so
+     * that common joins are moved up the expression tree.
+     */
+    public Joins getJoins();
+
+    /**
+     * Return true if this expression is or is made up of a contains expression.
+     */
+    public boolean hasContainsExpression();
+
+    /**
+     * Return true if the expression or any subexpression uses the given
+     * variable.
+     */
+    public boolean hasVariable(Variable var);
+}

Propchange: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Exp.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Extension.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Extension.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Extension.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Extension.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,232 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import java.sql.SQLException;
+import java.util.Map;
+
+import org.apache.openjpa.jdbc.kernel.JDBCFetchState;
+import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.meta.ClassMapping;
+import org.apache.openjpa.jdbc.meta.JavaSQLTypes;
+import org.apache.openjpa.jdbc.sql.Joins;
+import org.apache.openjpa.jdbc.sql.Result;
+import org.apache.openjpa.jdbc.sql.SQLBuffer;
+import org.apache.openjpa.jdbc.sql.Select;
+import org.apache.openjpa.kernel.Filters;
+import org.apache.openjpa.meta.ClassMetaData;
+
+/**
+ * Filter listener that evaluates to a value.
+ *
+ * @author Abe White
+ */
+class Extension
+    extends AbstractVal
+    implements Val, Exp {
+
+    private final JDBCFilterListener _listener;
+    private final Val _target;
+    private final Val _arg;
+    private final ClassMapping _candidate;
+    private Joins _joins = null;
+    private ClassMetaData _meta = null;
+    private Class _cast = null;
+
+    /**
+     * Constructor.
+     */
+    public Extension(JDBCFilterListener listener, Val target,
+        Val arg, ClassMapping candidate) {
+        _listener = listener;
+        _target = target;
+        _arg = arg;
+        _candidate = candidate;
+    }
+
+    public ClassMetaData getMetaData() {
+        return _meta;
+    }
+
+    public void setMetaData(ClassMetaData meta) {
+        _meta = meta;
+    }
+
+    public boolean isVariable() {
+        return false;
+    }
+
+    public Class getType() {
+        if (_cast != null)
+            return _cast;
+        Class targetClass = (_target == null) ? null : _target.getType();
+        return _listener.getType(targetClass, getArgTypes());
+    }
+
+    private Class[] getArgTypes() {
+        if (_arg == null)
+            return null;
+        if (_arg instanceof Args)
+            return ((Args) _arg).getTypes();
+        return new Class[]{ _arg.getType() };
+    }
+
+    public void setImplicitType(Class type) {
+        _cast = type;
+    }
+
+    public void initialize(Select sel, JDBCStore store, boolean nullTest) {
+        // note that we tell targets and args to extensions that are sql
+        // paths to go ahead and join to their related object (if any),
+        // because we assume that, unlike most operations, if a relation
+        // field like a 1-1 is given as the target of an extension, then
+        // the extension probably acts on some field or column in the
+        // related object, not the 1-1 field itself
+        Joins j1 = null;
+        Joins j2 = null;
+        if (_target != null) {
+            _target.initialize(sel, store, false);
+            if (_target instanceof PCPath)
+                ((PCPath) _target).joinRelation();
+            j1 = _target.getJoins();
+        }
+        if (_arg != null) {
+            _arg.initialize(sel, store, false);
+            if (_arg instanceof PCPath)
+                ((PCPath) _arg).joinRelation();
+            j2 = _arg.getJoins();
+        }
+        _joins = sel.and(j1, j2);
+    }
+
+    public Joins getJoins() {
+        return _joins;
+    }
+
+    public Object toDataStoreValue(Object val, JDBCStore store) {
+        return val;
+    }
+
+    public void select(Select sel, JDBCStore store, Object[] params,
+        boolean pks, JDBCFetchState fetchState) {
+        sel.select(newSQLBuffer(sel, store, params, fetchState), this);
+    }
+
+    public void selectColumns(Select sel, JDBCStore store,
+        Object[] params, boolean pks, JDBCFetchState fetchState) {
+        if (_target != null)
+            _target.selectColumns(sel, store, params, true, fetchState);
+        if (_arg != null)
+            _arg.selectColumns(sel, store, params, true, fetchState);
+    }
+
+    public void groupBy(Select sel, JDBCStore store, Object[] params,
+        JDBCFetchState fetchState) {
+        sel.groupBy(newSQLBuffer(sel, store, params, fetchState), false);
+    }
+
+    public void orderBy(Select sel, JDBCStore store, Object[] params,
+        boolean asc, JDBCFetchState fetchState) {
+        sel.orderBy(newSQLBuffer(sel, store, params, fetchState), asc, false);
+    }
+
+    private SQLBuffer newSQLBuffer(Select sel, JDBCStore store,
+        Object[] params, JDBCFetchState fetchState) {
+        calculateValue(sel, store, params, null, fetchState);
+        SQLBuffer buf = new SQLBuffer(store.getDBDictionary());
+        appendTo(buf, 0, sel, store, params, fetchState);
+        clearParameters();
+        return buf;
+    }
+
+    public Object load(Result res, JDBCStore store,
+        JDBCFetchState fetchState)
+        throws SQLException {
+        return Filters.convert(res.getObject(this,
+            JavaSQLTypes.JDBC_DEFAULT, null), getType());
+    }
+
+    public boolean hasVariable(Variable var) {
+        return (_target != null && _target.hasVariable(var))
+            || (_arg != null && _arg.hasVariable(var));
+    }
+
+    public void calculateValue(Select sel, JDBCStore store,
+        Object[] params, Val other, JDBCFetchState fetchState) {
+        if (_target != null)
+            _target.calculateValue(sel, store, params, null, fetchState);
+        if (_arg != null)
+            _arg.calculateValue(sel, store, params, null, fetchState);
+    }
+
+    public void clearParameters() {
+        if (_target != null)
+            _target.clearParameters();
+        if (_arg != null)
+            _arg.clearParameters();
+    }
+
+    public int length() {
+        return 1;
+    }
+
+    public void appendTo(SQLBuffer sql, int index, Select sel,
+        JDBCStore store, Object[] params, JDBCFetchState fetchState) {
+        FilterValue target = (_target == null) ? null
+            : new FilterValueImpl(_target, sel, store, params, fetchState);
+        _listener.appendTo(sql, target, getArgs(sel, store, params,
+            fetchState), _candidate, store);
+        sel.append(sql, _joins);
+    }
+
+    private FilterValue[] getArgs(Select sel, JDBCStore store,
+        Object[] params, JDBCFetchState fetchState) {
+        if (_arg == null)
+            return null;
+        if (_arg instanceof Args) {
+            Val[] vals = ((Args) _arg).getVals();
+            FilterValue[] filts = new FilterValue[vals.length];
+            for (int i = 0; i < vals.length; i++)
+                filts[i] = new FilterValueImpl(vals[i], sel, store, params,
+                    fetchState);
+            return filts;
+        }
+        return new FilterValue[]{
+            new FilterValueImpl(_arg, sel, store, params, fetchState)
+        };
+    }
+
+    //////////////////////
+    // Exp implementation
+    //////////////////////
+
+    public void initialize(Select sel, JDBCStore store,
+        Object[] params, Map contains) {
+        initialize(sel, store, false);
+    }
+
+    public void appendTo(SQLBuffer sql, Select sel, JDBCStore store,
+        Object[] params, JDBCFetchState fetchState) {
+        calculateValue(sel, store, params, null, fetchState);
+        appendTo(sql, 0, sel, store, params, fetchState);
+        sel.append(sql, getJoins());
+        clearParameters();
+    }
+
+    public boolean hasContainsExpression() {
+        return false;
+    }
+}

Propchange: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Extension.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/FilterValue.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/FilterValue.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/FilterValue.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/FilterValue.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,103 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import org.apache.openjpa.jdbc.meta.ClassMapping;
+import org.apache.openjpa.jdbc.meta.FieldMapping;
+import org.apache.openjpa.jdbc.schema.Column;
+import org.apache.openjpa.jdbc.schema.Table;
+import org.apache.openjpa.jdbc.sql.SQLBuffer;
+
+/**
+ * The simplified public view of any non-operator in a query filter,
+ * including constants, variables, and object fields.
+ *
+ * @author Abe White
+ */
+public interface FilterValue {
+
+    /**
+     * Return the expected type of this value.
+     */
+    public Class getType();
+
+    /**
+     * Return the number of SQL elements in this value. Usually 1.
+     */
+    public int length();
+
+    /**
+     * Append the first SQL element for this value to the given buffer.
+     */
+    public void appendTo(SQLBuffer buf);
+
+    /**
+     * Append the <code>index</code>th SQL element for this value to the
+     * given buffer.
+     */
+    public void appendTo(SQLBuffer buf, int index);
+
+    /**
+     * Return the alias to use for the given column (this includes the table
+     * alias prefix, if any).
+     */
+    public String getColumnAlias(Column col);
+
+    /**
+     * Return the alias to use for the given column (this includes the table
+     * alias prefix, if any).
+     */
+    public String getColumnAlias(String col, Table table);
+
+    /**
+     * Transform the given value into its datastore equivalent.
+     */
+    public Object toDataStoreValue(Object val);
+
+    /**
+     * Return true if this value represents a literal or parameter.
+     */
+    public boolean isConstant();
+
+    /**
+     * If this is a constant, return its value, else return null.
+     */
+    public Object getValue();
+
+    /**
+     * If this is a constant, returns its value as it would be represented
+     * in the database in this context, else return null.
+     */
+    public Object getSQLValue();
+
+    /**
+     * Return true if this value represents a persistent field traversal,
+     * such as 'this', 'address.street', or 'projectVariable.title'.
+     */
+    public boolean isPath();
+
+    /**
+     * If this is a path to a persistent object, return its class mapping,
+     * else return null.
+     */
+    public ClassMapping getClassMapping();
+
+    /**
+     * If this is a path to a persistent field, return its mapping, else
+     * return null.
+     */
+    public FieldMapping getFieldMapping();
+}

Propchange: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/FilterValue.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/FilterValueImpl.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/FilterValueImpl.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/FilterValueImpl.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/FilterValueImpl.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import org.apache.openjpa.jdbc.kernel.JDBCFetchState;
+import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.meta.ClassMapping;
+import org.apache.openjpa.jdbc.meta.FieldMapping;
+import org.apache.openjpa.jdbc.schema.Column;
+import org.apache.openjpa.jdbc.schema.Table;
+import org.apache.openjpa.jdbc.sql.SQLBuffer;
+import org.apache.openjpa.jdbc.sql.Select;
+
+/**
+ * Implementation of {@link FilterValue} that wraps a {@link Val}.
+ *
+ * @author Abe White
+ */
+class FilterValueImpl
+    implements FilterValue {
+
+    private final Val _val;
+    private final Select _sel;
+    private final JDBCStore _store;
+    private final Object[] _params;
+    private final JDBCFetchState _fetchState;
+
+    public FilterValueImpl(Val val, Select sel, JDBCStore store,
+        Object[] params, JDBCFetchState fetchState) {
+        _val = val;
+        _sel = sel;
+        _store = store;
+        _params = params;
+        _fetchState = fetchState;
+    }
+
+    public Class getType() {
+        return _val.getType();
+    }
+
+    public int length() {
+        return _val.length();
+    }
+
+    public void appendTo(SQLBuffer buf) {
+        appendTo(buf, 0);
+    }
+
+    public void appendTo(SQLBuffer buf, int index) {
+        _val.appendTo(buf, index, _sel, _store, _params, _fetchState);
+    }
+
+    public String getColumnAlias(Column col) {
+        return _sel.getColumnAlias(col, _val.getJoins());
+    }
+
+    public String getColumnAlias(String col, Table table) {
+        return _sel.getColumnAlias(col, table, _val.getJoins());
+    }
+
+    public Object toDataStoreValue(Object val) {
+        return _val.toDataStoreValue(val, _store);
+    }
+
+    public boolean isConstant() {
+        return _val instanceof Const;
+    }
+
+    public Object getValue() {
+        return (isConstant()) ? ((Const) _val).getValue() : null;
+    }
+
+    public Object getSQLValue() {
+        return (isConstant()) ? ((Const) _val).getSQLValue() : null;
+    }
+
+    public boolean isPath() {
+        return _val instanceof PCPath;
+    }
+
+    public ClassMapping getClassMapping() {
+        return (isPath()) ? ((PCPath) _val).getClassMapping() : null;
+    }
+
+    public FieldMapping getFieldMapping() {
+        return (isPath()) ? ((PCPath) _val).getFieldMapping() : null;
+    }
+}

Propchange: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/FilterValueImpl.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/GetColumn.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/GetColumn.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/GetColumn.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/GetColumn.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.meta.ClassMapping;
+import org.apache.openjpa.jdbc.sql.SQLBuffer;
+import org.apache.openjpa.kernel.StoreContext;
+import org.apache.openjpa.lib.util.Localizer;
+import org.apache.openjpa.util.UnsupportedException;
+import org.apache.openjpa.util.UserException;
+
+/**
+ * Returns the SQL alias of the named column for use in a query. Note that
+ * to retrieve columns in the table of the candidate object, you must
+ * explicitly qualify the extension with <code>this</code>, as demonstrated
+ * in the second example below.
+ *  Examples:<br />
+ * <code> "company.address.ext:getColumn (\"ID\") == 5"<br />
+ * "this.ext:getColumn (\"CLS\") == \"org.apache.openjpa.example.Person\""
+ * </code>
+ *
+ * @nojavadoc
+ */
+public class GetColumn
+    implements JDBCFilterListener {
+
+    public static final String TAG = "getColumn";
+
+    private static final Localizer _loc = Localizer.forPackage
+        (GetColumn.class);
+
+    public String getTag() {
+        return TAG;
+    }
+
+    public boolean expectsArguments() {
+        return true;
+    }
+
+    public boolean expectsTarget() {
+        return true;
+    }
+
+    public Object evaluate(Object target, Class targetClass, Object[] args,
+        Class[] argClasses, Object candidate, StoreContext ctx) {
+        throw new UnsupportedException(_loc.get("no-in-mem", TAG));
+    }
+
+    public void appendTo(SQLBuffer buf, FilterValue target,
+        FilterValue[] args, ClassMapping type, JDBCStore store) {
+        if (!args[0].isConstant())
+            throw new UserException(_loc.get("const-only", TAG));
+        if (!target.isPath())
+            throw new UserException(_loc.get("path-only", TAG));
+
+        // alias the column using the target's context, so that the
+        // correct relation path to the target is used
+        ClassMapping mapping = target.getClassMapping();
+        String colName = args[0].getValue().toString();
+        buf.append(target.getColumnAlias(colName, mapping.getTable()));
+    }
+
+    public Class getType(Class targetClass, Class[] argClasses) {
+        return Object.class;
+    }
+}

Propchange: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/GetColumn.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/GetObjectId.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/GetObjectId.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/GetObjectId.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/GetObjectId.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,184 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import java.sql.SQLException;
+
+import org.apache.openjpa.jdbc.kernel.JDBCFetchState;
+import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.meta.ClassMapping;
+import org.apache.openjpa.jdbc.meta.Joinable;
+import org.apache.openjpa.jdbc.schema.Column;
+import org.apache.openjpa.jdbc.sql.Joins;
+import org.apache.openjpa.jdbc.sql.Result;
+import org.apache.openjpa.jdbc.sql.SQLBuffer;
+import org.apache.openjpa.jdbc.sql.Select;
+import org.apache.openjpa.kernel.Filters;
+import org.apache.openjpa.lib.util.Localizer;
+import org.apache.openjpa.meta.ClassMetaData;
+import org.apache.openjpa.util.ApplicationIds;
+import org.apache.openjpa.util.Id;
+import org.apache.openjpa.util.OpenJPAId;
+import org.apache.openjpa.util.UserException;
+import serp.util.Numbers;
+
+/**
+ * Select the oid value of an object; typically used in projections.
+ *
+ * @author Abe White
+ */
+class GetObjectId
+    extends AbstractVal
+    implements Val {
+
+    private static final Localizer _loc = Localizer.forPackage
+        (GetObjectId.class);
+
+    private final PCPath _path;
+    private ClassMetaData _meta = null;
+
+    /**
+     * Constructor. Provide the value whose oid to extract.
+     */
+    public GetObjectId(PCPath path) {
+        _path = path;
+    }
+
+    public Column[] getColumns() {
+        return _path.getClassMapping().getPrimaryKeyColumns();
+    }
+
+    public ClassMetaData getMetaData() {
+        return _meta;
+    }
+
+    public void setMetaData(ClassMetaData meta) {
+        _meta = meta;
+    }
+
+    public boolean isVariable() {
+        return false;
+    }
+
+    public Class getType() {
+        return Object.class;
+    }
+
+    public void setImplicitType(Class type) {
+    }
+
+    public void initialize(Select sel, JDBCStore store, boolean nullTest) {
+        _path.initialize(sel, store, false);
+        _path.joinRelation();
+
+        // it's difficult to get calls on non-pc fields to always return null
+        // without screwing up the SQL, to just don't let users call it on
+        // non-pc fields at all
+        if (_path.getClassMapping() == null
+            || _path.getClassMapping().getEmbeddingMapping() != null)
+            throw new UserException(_loc.get("bad-getobjectid",
+                _path.getFieldMapping()));
+    }
+
+    public Joins getJoins() {
+        return _path.getJoins();
+    }
+
+    public Object toDataStoreValue(Object val, JDBCStore store) {
+        // if datastore identity, try to convert to a long value
+        ClassMapping mapping = _path.getClassMapping();
+        if (mapping.getIdentityType() == mapping.ID_DATASTORE) {
+            if (val instanceof Id)
+                return Numbers.valueOf(((Id) val).getId());
+            return Filters.convert(val, long.class);
+        }
+
+        // if unknown identity, can't do much
+        if (mapping.getIdentityType() == mapping.ID_UNKNOWN)
+            return (val instanceof OpenJPAId) ?
+                ((OpenJPAId) val).getIdObject() : val;
+
+        // application identity; convert to pk values in the same order as
+        // the mapping's primary key columns will be returned
+        Object[] pks = ApplicationIds.toPKValues(val, mapping);
+        if (pks.length == 1)
+            return pks[0];
+        if (val == null)
+            return pks;
+        while (!mapping.isPrimaryKeyObjectId(false))
+            mapping = mapping.getJoinablePCSuperclassMapping();
+
+        // relies on single-column primary key field mappings
+        Column[] cols = mapping.getPrimaryKeyColumns();
+        Object[] ordered = new Object[cols.length];
+        Joinable join;
+        for (int i = 0; i < cols.length; i++) {
+            join = mapping.assertJoinable(cols[i]);
+            ordered[i] = pks[mapping.getField(join.getFieldIndex()).
+                getPrimaryKeyIndex()];
+        }
+        return ordered;
+    }
+
+    public void select(Select sel, JDBCStore store, Object[] params,
+        boolean pks, JDBCFetchState fetchState) {
+        selectColumns(sel, store, params, true, fetchState);
+    }
+
+    public void selectColumns(Select sel, JDBCStore store,
+        Object[] params, boolean pks, JDBCFetchState fetchState) {
+        _path.selectColumns(sel, store, params, true, fetchState);
+    }
+
+    public void groupBy(Select sel, JDBCStore store, Object[] params,
+        JDBCFetchState fetchState) {
+        _path.groupBy(sel, store, params, fetchState);
+    }
+
+    public void orderBy(Select sel, JDBCStore store, Object[] params,
+        boolean asc, JDBCFetchState fetchState) {
+        _path.orderBy(sel, store, params, asc, fetchState);
+    }
+
+    public Object load(Result res, JDBCStore store,
+        JDBCFetchState fetchState)
+        throws SQLException {
+        return _path.load(res, store, true, fetchState);
+    }
+
+    public boolean hasVariable(Variable var) {
+        return _path.hasVariable(var);
+    }
+
+    public void calculateValue(Select sel, JDBCStore store,
+        Object[] params, Val other, JDBCFetchState fetchState) {
+        _path.calculateValue(sel, store, params, null, fetchState);
+    }
+
+    public void clearParameters() {
+        _path.clearParameters();
+    }
+
+    public int length() {
+        return _path.length();
+    }
+
+    public void appendTo(SQLBuffer sql, int index, Select sel,
+        JDBCStore store, Object[] params, JDBCFetchState fetchState) {
+        _path.appendTo(sql, index, sel, store, params, fetchState);
+    }
+}
+

Propchange: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/GetObjectId.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InExpression.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InExpression.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InExpression.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InExpression.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,176 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.openjpa.jdbc.kernel.JDBCFetchState;
+import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.schema.Column;
+import org.apache.openjpa.jdbc.sql.Joins;
+import org.apache.openjpa.jdbc.sql.SQLBuffer;
+import org.apache.openjpa.jdbc.sql.Select;
+
+/**
+ * Tests whether a value is IN a collection.
+ *
+ * @author Abe White
+ */
+class InExpression
+    implements Exp {
+
+    private final Val _val;
+    private final Const _const;
+
+    /**
+     * Constructor. Supply the value to test and the constant to obtain
+     * the parameters from.
+     */
+    public InExpression(Val val, Const constant) {
+        _val = val;
+        _const = constant;
+    }
+
+    public Const getConst() {
+        return _const;
+    }
+
+    public Val getValue() {
+        return _val;
+    }
+
+    public void initialize(Select sel, JDBCStore store,
+        Object[] params, Map contains) {
+        _val.initialize(sel, store, false);
+        _const.initialize(sel, store, false);
+    }
+
+    public void appendTo(SQLBuffer buf, Select sel, JDBCStore store,
+        Object[] params, JDBCFetchState fetchState) {
+        _val.calculateValue(sel, store, params, null, fetchState);
+        _const.calculateValue(sel, store, params, null, fetchState);
+
+        Collection coll = getCollection();
+        if (coll != null) {
+            Collection ds = new ArrayList(coll.size());
+            for (Iterator itr = coll.iterator(); itr.hasNext();)
+                ds.add(_val.toDataStoreValue(itr.next(), store));
+            coll = ds;
+        }
+
+        Column[] cols = null;
+        if (_val instanceof PCPath)
+            cols = ((PCPath) _val).getColumns();
+        else if (_val instanceof GetObjectId)
+            cols = ((GetObjectId) _val).getColumns();
+
+        if (coll == null || coll.isEmpty())
+            buf.append("1 <> 1");
+        else if (_val.length() == 1)
+            inContains(buf, sel, store, params, fetchState, coll, cols);
+        else
+            orContains(buf, sel, store, params, fetchState, coll, cols);
+        sel.append(buf, _val.getJoins());
+
+        _val.clearParameters();
+        _const.clearParameters();
+    }
+
+    /**
+     * Construct an IN clause with the value of the given collection.
+     */
+    private void inContains(SQLBuffer buf, Select sel, JDBCStore store,
+        Object[] params, JDBCFetchState fetchState,
+        Collection coll, Column[] cols) {
+        _val.appendTo(buf, 0, sel, store, params, fetchState);
+        buf.append(" IN (");
+
+        Column col = (cols != null && cols.length == 1) ? cols[0] : null;
+        for (Iterator itr = coll.iterator(); itr.hasNext();) {
+            buf.appendValue(itr.next(), col);
+            if (itr.hasNext())
+                buf.append(", ");
+        }
+        buf.append(")");
+    }
+
+    /**
+     * If the value to test is a compound key, we can't use IN,
+     * so create a clause like '(a = b AND c = d) OR (e = f AND g = h) ...'
+     */
+    private void orContains(SQLBuffer buf, Select sel, JDBCStore store,
+        Object[] params, JDBCFetchState fetchState, Collection coll,
+        Column[] cols) {
+        if (coll.size() > 1)
+            buf.append("(");
+
+        Object[] vals;
+        int len;
+        Column col;
+        for (Iterator itr = coll.iterator(); itr.hasNext();) {
+            vals = (Object[]) itr.next();
+
+            buf.append("(");
+            for (int i = 0; i < vals.length; i++) {
+                col = (cols != null && cols.length == vals.length)
+                    ? cols[i] : null;
+                if (i > 0)
+                    buf.append(" AND ");
+
+                _val.appendTo(buf, i, sel, store, params, fetchState);
+                if (vals[i] == null)
+                    buf.append(" IS ");
+                else
+                    buf.append(" = ");
+                buf.appendValue(vals[i], col);
+            }
+            buf.append(")");
+
+            if (itr.hasNext())
+                buf.append(" OR ");
+        }
+        if (coll.size() > 1)
+            buf.append(")");
+    }
+
+    public void selectColumns(Select sel, JDBCStore store,
+        Object[] params, boolean pks, JDBCFetchState fetchState) {
+        _val.selectColumns(sel, store, params, true, fetchState);
+        _const.selectColumns(sel, store, params, pks, fetchState);
+    }
+
+    public Joins getJoins() {
+        return _val.getJoins();
+    }
+
+    public boolean hasContainsExpression() {
+        return false;
+    }
+
+    public boolean hasVariable(Variable var) {
+        return _const.hasVariable(var) || _val.hasVariable(var);
+    }
+
+    /**
+     * Return the collection to test for containment with.
+     */
+    protected Collection getCollection() {
+        return (Collection) _const.getValue();
+    }
+}

Propchange: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InExpression.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InKeyExpression.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InKeyExpression.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InKeyExpression.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InKeyExpression.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ * Tests whether a value is IN a map key set.
+ *
+ * @author Abe White
+ */
+class InKeyExpression
+    extends InExpression {
+
+    /**
+     * Constructor. Supply the value to test and the constant to obtain
+     * the parameters from.
+     */
+    public InKeyExpression(Val val, Const constant) {
+        super(val, constant);
+    }
+
+    /**
+     * Return the collection to test for containment with.
+     */
+    protected Collection getCollection() {
+        Map map = (Map) getConst().getValue();
+        return (map == null) ? null : map.keySet();
+    }
+}

Propchange: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InKeyExpression.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InSubQExpression.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InSubQExpression.java?rev=423615&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InSubQExpression.java (added)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/InSubQExpression.java Wed Jul 19 14:34:44 2006
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.openjpa.jdbc.kernel.exps;
+
+import java.util.Map;
+
+import org.apache.openjpa.jdbc.kernel.JDBCFetchState;
+import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.sql.Joins;
+import org.apache.openjpa.jdbc.sql.SQLBuffer;
+import org.apache.openjpa.jdbc.sql.Select;
+
+/**
+ * Tests whether a value is IN a subquery.
+ *
+ * @author Abe White
+ */
+class InSubQExpression
+    implements Exp {
+
+    private final Val _val;
+    private final SubQ _sub;
+
+    /**
+     * Constructor. Supply the value to test and the subquery.
+     */
+    public InSubQExpression(Val val, SubQ sub) {
+        _val = val;
+        _sub = sub;
+    }
+
+    public void initialize(Select sel, JDBCStore store,
+        Object[] params, Map contains) {
+        _val.initialize(sel, store, false);
+        _sub.initialize(sel, store, false);
+    }
+
+    public void appendTo(SQLBuffer buf, Select sel, JDBCStore store,
+        Object[] params, JDBCFetchState fetchState) {
+        _val.calculateValue(sel, store, params, null, fetchState);
+        _sub.calculateValue(sel, store, params, null, fetchState);
+        _val.appendTo(buf, 0, sel, store, params, fetchState);
+        buf.append(" IN ");
+        _sub.appendTo(buf, 0, sel, store, params, fetchState);
+        _val.clearParameters();
+        _sub.clearParameters();
+    }
+
+    public void selectColumns(Select sel, JDBCStore store,
+        Object[] params, boolean pks, JDBCFetchState fetchState) {
+        _val.selectColumns(sel, store, params, true, fetchState);
+        _sub.selectColumns(sel, store, params, pks, fetchState);
+    }
+
+    public Joins getJoins() {
+        return _val.getJoins();
+    }
+
+    public boolean hasContainsExpression() {
+        return false;
+    }
+
+    public boolean hasVariable(Variable var) {
+        return _val.hasVariable(var) || _sub.hasVariable(var);
+    }
+}