Package org.jpox.store.mapped.query

Source Code of org.jpox.store.mapped.query.StatementText$Parameter

/**********************************************************************
Copyright (c) 2002 Kelly Grizzle (TJDO) and others. All rights reserved.
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.


Contributors:
2002 Mike Martin (TJDO)
2003 Andy Jefferson - coding standards
2005 Andy Jefferson - added support for result set type/concurrency
2007 Andy Jefferson - removed RDBMS dependency
    ...
**********************************************************************/
package org.jpox.store.mapped.query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jpox.ObjectManager;
import org.jpox.store.mapped.expression.QueryExpression;
import org.jpox.store.mapped.expression.ScalarExpression;
import org.jpox.store.mapped.mapping.JavaTypeMapping;
import org.jpox.store.mapped.mapping.Mappings;

/**
* Representation of a statement.
*
* @version $Revision: 1.1 $
**/
public class StatementText
{
    private static int nextParamID = 0;

    private StringBuffer statementText;
    private List parameterNames = null;
    private Map parameterMappingsByName = null;
    private Map parameterValuesByName = null;
    private boolean encloseWithInParentheses = false;
    private String postpend;
    private List appended;

    /**
     * Constructor
     **/
    public StatementText()
    {
        appended = new ArrayList();
    }

    /**
     * Constructor
     * @param initialStatementText
     */
    public StatementText(String initialStatementText)
    {
        this();
        append(initialStatementText);
    }
   
    /**
     * Convenience method to reset the SQL for the statement.
     * This is used when updating an expression internally, and need to regenerate
     * the statement.
     */
    public void clearStatement()
    {
        statementText = null;
        appended.clear();
    }

    /**
     * Method to initialise the statement.
     */
    private void initParameters()
    {
        if (parameterNames == null)
        {
            parameterNames          = new ArrayList();
            parameterMappingsByName = new HashMap();
            parameterValuesByName   = new HashMap();
        }
    }

    /**
     * Whether to enclose this statement within parentheses
     */
    public void encloseWithInParentheses()
    {
        statementText = null;
        this.encloseWithInParentheses = true;
    }

    /**
     * Set a String to the end of the statement. 
     * @param s the string
     * @return the StatementText
     */
    public StatementText postpend(String s)
    {
        statementText = null;
        postpend = s;
        return this;
    }   

    /**
     * Append a char 
     * @param c the char
     * @return the StatementText
     */   
    public StatementText append(char c)
    {
        statementText = null;
        appended.add(new Character(c));
        return this;
    }

    /**
     * Append a char 
     * @param s the String
     * @return the StatementText
     */   
    public StatementText append(String s)
    {
        statementText = null;
        appended.add(s);
        return this;
    }

    /**
     * Append a QueryExpression
     * @param qsc the QueryExpression
     * @return the StatementText
     */   
    public StatementText append(QueryExpression qsc)
    {
        statementText = null;
        appended.add(qsc);
        return this;
    }

    /**
     * Append a StatementText
     * @param st the StatementText
     * @return the StatementText
     */   
    public StatementText append(StatementText st, int mode)
    {
        appended.add(st.toStatementString(mode));
        if (st.parameterNames != null)
        {
            initParameters();
            parameterNames.addAll(st.parameterNames);
            parameterMappingsByName.putAll(st.parameterMappingsByName);
            parameterValuesByName.putAll(st.parameterValuesByName);
        }
        return this;
    }

    /**
     * Append a ScalarExpression
     * @param expr the ScalarExpression
     * @return the StatementText
     */   
    public StatementText append(ScalarExpression expr)
    {
        appended.add(expr);
        return this;
    }
   
    /**
     * Append a parameter
     * @param mapping the mapping
     * @param value the parameter value
     * @return the generated parameter name
     */
    public String appendParameter(JavaTypeMapping mapping, Object value)
    {
        statementText = null;
        String name = "param-" + nextParamID++;
        appended.add(new Parameter(name,mapping,value));
        return name;
    }

    /**
     * Method to set the parameters in the datastore statement.
     * @param om ObjectManager
     * @param datastoreStatement The datastore "statement"
     */
    public void setParameters(ObjectManager om, Object datastoreStatement)
    {
        if (parameterNames != null)
        {
            Iterator i = parameterNames.iterator();
            int stmtParamNum = 1;

            while (i.hasNext())
            {
                Object name = i.next();
                JavaTypeMapping mapping = (JavaTypeMapping)parameterMappingsByName.get(name);
                Object value = parameterValuesByName.get(name);

                mapping.setObject(om, datastoreStatement, Mappings.getParametersIndex(stmtParamNum,mapping), value);
                if (mapping.getNumberOfDatastoreFields() > 0)
                {
                    stmtParamNum += mapping.getNumberOfDatastoreFields();
                }
                else
                {
                    //if the JavaTypeMapping has no datastoreFields, at least one it may have
                    stmtParamNum += 1;
                }
            }
        }
    }

    /**
     * Accessor for the SQL of the statement.
     * @param mode (0=PROJECTION;1=FILTER)
     * @return The SQL text
     */
    public String toStatementString(int mode)
    {
        if (statementText == null)
        {
            statementText = new StringBuffer();
            if (encloseWithInParentheses)
            {
                statementText.append("(");
            }
            for (int i = 0; i < appended.size(); i++)
            {
                Object item = appended.get(i);
                if (item instanceof ScalarExpression)
                {
                    ScalarExpression expr = (ScalarExpression) item;
                    StatementText st = expr.toStatementText(mode);
                    statementText.append(st.toStatementString(mode));

                    if (st.parameterNames != null)
                    {
                        initParameters();
                        parameterNames.addAll(st.parameterNames);
                        parameterMappingsByName.putAll(st.parameterMappingsByName);
                        parameterValuesByName.putAll(st.parameterValuesByName);
                    }
                }
                else if (item instanceof Parameter)
                {
                    Parameter param = (Parameter) item;
                    statementText.append('?');

                    initParameters();
                    parameterNames.add(param.name);
                    parameterMappingsByName.put(param.name, param.mapping);
                    parameterValuesByName.put(param.name, param.value);
                }
                else if (item instanceof QueryExpression)
                {
                    QueryExpression qe = (QueryExpression) item;
                    StatementText st = qe.toStatementText(false); // TODO use the same lock as the caller
                    statementText.append(st.toStatementString(mode));
                    if (st.parameterNames != null)
                    {
                        initParameters();
                        parameterNames.addAll(st.parameterNames);
                        parameterMappingsByName.putAll(st.parameterMappingsByName);
                        parameterValuesByName.putAll(st.parameterValuesByName);
                    }
                }
                else if (item instanceof StatementText)
                {
                    StatementText st = (StatementText) item;
                    statementText.append(st.toStatementString(mode));
                    if (st.parameterNames != null)
                    {
                        initParameters();
                        parameterNames.addAll(st.parameterNames);
                        parameterMappingsByName.putAll(st.parameterMappingsByName);
                        parameterValuesByName.putAll(st.parameterValuesByName);
                    }
                }
                else
                {
                    statementText.append(item);
                }
            }
            if (encloseWithInParentheses)
            {
                statementText.append(")");
            }
            statementText.append((postpend == null ? "" : postpend));
        }
        return statementText.toString();
    }

    /**
     * Accessor for the string form of the statement.
     * @return String form of the statement
     */
    public String toString()
    {
        //the mode should be indifferent, so we use projection
        return toStatementString(ScalarExpression.PROJECTION);
    }

    /**
     * Internal class to keep parameters
     */
    private class Parameter
    {
        final String name;
        final JavaTypeMapping mapping;
        final Object value;

        public Parameter(String name, JavaTypeMapping mapping, Object value)
        {
            super();
            this.name = name;
            this.mapping = mapping;
            this.value = value;
        }
    }
}
TOP

Related Classes of org.jpox.store.mapped.query.StatementText$Parameter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.