Package org.jpox.store.rdbms.adapter

Source Code of org.jpox.store.rdbms.adapter.SAPDBAdapter

/**********************************************************************
Copyright (c) 2002 Mike Martin (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:
2003 Andy Jefferson - coding standards
2004 Erik Bengtson - added sequence handling
2006 Andy Jefferson - updated to MaxDB 7.6
    ...
**********************************************************************/
package org.jpox.store.rdbms.adapter;

import java.math.BigInteger;
import java.sql.DatabaseMetaData;
import java.util.ArrayList;

import org.jpox.ClassLoaderResolver;
import org.jpox.exceptions.JPOXUserException;
import org.jpox.store.mapped.DatastoreContainerObject;
import org.jpox.store.mapped.DatastoreIdentifier;
import org.jpox.store.mapped.IdentifierFactory;
import org.jpox.store.mapped.expression.LogicSetExpression;
import org.jpox.store.mapped.expression.NumericExpression;
import org.jpox.store.mapped.expression.QueryExpression;
import org.jpox.store.mapped.expression.StringExpression;
import org.jpox.store.mapped.expression.TableExprAsJoins;
import org.jpox.store.rdbms.key.CandidateKey;
import org.jpox.store.rdbms.key.ForeignKey;
import org.jpox.store.rdbms.key.Index;
import org.jpox.store.rdbms.key.PrimaryKey;
import org.jpox.store.rdbms.query.SAPDBQueryStatement;

/**
* Provides methods for adapting SQL language elements to the SAPDB/MaxDB database.
*
* @version $Revision: 1.26 $
*/
public class SAPDBAdapter extends DatabaseAdapter
{
    /**
     * Constructs an SAP DB adapter based on the given JDBC metadata.
     * @param metadata the database metadata.
     */
    public SAPDBAdapter(DatabaseMetaData metadata)
    {
        super(metadata);
    }

    public String getVendorID()
    {
        return "sapdb";
    }

    /**
     * Accessor for whether a JDBC Types.BIT is really mapped in the datastore
     * to a boolean field.
     * @return Whether it is really a boolean
     */
    public boolean isBitReallyBoolean()
    {
        return true;
    }

    public boolean supportsAlterTableDropConstraint()
    {
        return false;
    }

    public boolean supportsBooleanComparison()
    {
        return true;
    }

    public boolean supportsDeferredConstraints()
    {
        return false;
    }

    /**
     * Whether this datastore supports locking using SELECT ... FOR UPDATE.
     * @return whether we support locking using SELECT ... FOR UPDATE.
     **/
    public boolean supportsLockWithSelectForUpdate()
    {
        return true;
    }

    /**
     * The option to specify in "SELECT ... WITH (option)" to lock instances
     * Null if not supported.
     * @return The option to specify with "SELECT ... WITH (option)"
     **/
    public String getSelectWithLockOption()
    {
        return "EXCLUSIVE LOCK";
    }

    /**
     * Whether this datastore supports the specified foreign key update action.
     * @param action The update action
     * @return Whether it is supported
     */
    public boolean supportsForeignKeyUpdateAction(ForeignKey.FKAction action)
    {
        return false;
    }

    public QueryExpression newQueryStatement(DatastoreContainerObject table, DatastoreIdentifier rangeVar, ClassLoaderResolver clr)
    {
        return new SAPDBQueryStatement(table, rangeVar, clr);
    }

    public QueryExpression newQueryStatement(DatastoreContainerObject table, ClassLoaderResolver clr)
    {
        return new SAPDBQueryStatement(table, clr);
    }

    public LogicSetExpression newTableExpression(QueryExpression qs, DatastoreContainerObject table, DatastoreIdentifier rangeVar)
    {
        return new TableExprAsJoins(qs, table, rangeVar);
    }

    public String getAddPrimaryKeyStatement(PrimaryKey pk, IdentifierFactory factory)
    {
        // MaxDB doesnt support having "ADD CONSTRAINT pk_name"
        return "ALTER TABLE " + pk.getDatastoreContainerObject().toString() + " ADD " + pk;
    }

    public String getAddCandidateKeyStatement(CandidateKey ck, IdentifierFactory factory)
    {
        Index idx = new Index(ck);
        idx.setName(ck.getName());
        return getCreateIndexStatement(idx, factory);
    }

    public String getAddForeignKeyStatement(ForeignKey fk, IdentifierFactory factory)
    {
        // MaxDB doesnt support having "ADD CONSTRAINT fk_name"
        return "ALTER TABLE " + fk.getDatastoreContainerObject().toString() + " ADD " + fk;
    }

    public NumericExpression lengthMethod(StringExpression str)
    {
        ArrayList args = new ArrayList();
        args.add(str);

        return new NumericExpression("LENGTH", args);
    }

    public StringExpression substringMethod(StringExpression str, NumericExpression begin)
    {
        ArrayList args = new ArrayList();
        args.add(str);
        args.add(begin.add(getMapping(BigInteger.class, str).newLiteral(str.getQueryExpression(), BigInteger.ONE)));

        return new StringExpression("SUBSTR", args);
    }

    public StringExpression substringMethod(StringExpression str,
                                               NumericExpression begin,
                                               NumericExpression end)
    {
        ArrayList args = new ArrayList();
        args.add(str);
        args.add(begin.add(getMapping(Integer.class, str).newLiteral(str.getQueryExpression(), BigInteger.ONE)));
        args.add(end.sub(begin));

        return new StringExpression("SUBSTR", args);
    }

    // ---------------------------- Sequence Support ---------------------------
   
    /**
     * Whether we support sequences.
     * @return whether we support sequences.
     **/
    public boolean supportsSequences()
    {
        return true;
    }
   
    /**
     * Accessor for the sequence statement to create the sequence.
     * @param sequence_name Name of the sequence
     * @param min Minimum value for the sequence
     * @param max Maximum value for the sequence
     * @param start Start value for the sequence
     * @param increment Increment value for the sequence
     * @param cache_size Cache size for the sequence
     * @return The statement for getting the next id from the sequence
     **/
    public String getSequenceCreateStmt(String sequence_name,
            String min,String max,
            String start,String increment,
            String cache_size)
    {
        if (sequence_name == null)
        {
            throw new JPOXUserException(LOCALISER.msg("Adapter.SequenceNameNullNotSupported"));
        }
       
        StringBuffer stmt = new StringBuffer("CREATE SEQUENCE ");
        stmt.append(sequence_name);
        if (min != null && min.length() > 0)
        {
            stmt.append(" MINVALUE " + min);
        }
        if (max != null && max.length() > 0)
        {
            stmt.append(" MAXVALUE " + max);
        }
        if (start != null && start.length() > 0)
        {
            stmt.append(" START WITH " + start);
        }
        if (increment != null && increment.length() > 0)
        {
            stmt.append(" INCREMENT BY " + increment);
        }
        if (cache_size != null && cache_size.length() > 0)
        {
            stmt.append(" CACHE " + cache_size);
        }
        else
        {
            stmt.append(" NOCACHE");
        }
       
        return stmt.toString();
    }

    /**
     * Accessor for the statement for getting the next id from the sequence
     * for this datastore.
     * @param sequence_name Name of the sequence
     * @return The statement for getting the next id for the sequence
     **/
    public String getSequenceNextStmt(String sequence_name)
    {
        if (sequence_name == null)
        {
            throw new JPOXUserException(LOCALISER.msg("Adapter.SequenceNameNullNotSupported"));
        }

        StringBuffer stmt=new StringBuffer("SELECT ");
        stmt.append(sequence_name);
        stmt.append(".nextval FROM dual");

        return stmt.toString();
    }
}
TOP

Related Classes of org.jpox.store.rdbms.adapter.SAPDBAdapter

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.