Package org.jpox.store.rdbms.adapter

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

/**********************************************************************
Copyright (c) 2003 Andy Jefferson 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:
2006 Thomas Corte - updates for Sybase 15
    ...
**********************************************************************/
package org.jpox.store.rdbms.adapter;

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

import org.jpox.store.mapped.DatastoreContainerObject;
import org.jpox.store.mapped.DatastoreIdentifier;
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.ScalarExpression;
import org.jpox.store.mapped.expression.StringExpression;
import org.jpox.store.mapped.expression.TableExprAsJoins;
import org.jpox.store.rdbms.columninfo.ColumnInfo;
import org.jpox.store.rdbms.columninfo.SybaseColumnInfo;
import org.jpox.store.rdbms.table.Table;
import org.jpox.store.rdbms.typeinfo.TypeInfo;

/**
* Provides methods for adapting SQL language elements to the Sybase database.
*
* @version $Revision: 1.23 $
*/
public class SybaseAdapter extends DatabaseAdapter
{
    /**
     * Constructor.
     * @param metadata MetaData for the DB
     */
    public SybaseAdapter(DatabaseMetaData metadata)
    {
        super(metadata);
    }

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

    /**
     * Accessor for the DROP TABLE statement for Sybase.
     * Sybase doesnt support CASCADE CONSTRAINTS so we just return a simple
     * DROP TABLE table-name
     * @param table The table to drop.
     * @return The DROP TABLE statement
     **/
  public String getDropTableStatement(DatastoreContainerObject table)
  {
    return "DROP TABLE " + table.toString();
  }

    /**
     * Accessor for TableExpression for Sybase. Sybase (TransactSQL) accepts
     * the TableExprAsJoins style. The TableExprAsSubjoins doesnt work here.
     * @param qs The query statement
     * @param table The table in question
     * @param rangeVar range variable (?)
     * @return The Table Expression
     **/
    public LogicSetExpression newTableExpression(QueryExpression qs, DatastoreContainerObject table, DatastoreIdentifier rangeVar)
    {
        return new TableExprAsJoins(qs, table, rangeVar);
    }

    /**
     * Accessor for whether Sybase supports deferred constraints.
     * Sybase doesnt support INITIALLY DEFERRED keywords so we return false.
     * @return Whether Sybase supports deferred constraints
     **/
    public boolean supportsDeferredConstraints()
    {
        return false;
    }

    /**
     * Accessor for whether Sybase supports Boolean comparisons.
     * @return Whether we support Boolean comparisons
     **/
  public boolean supportsBooleanComparison()
  {
    return false;
  }

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

    public ColumnInfo newColumnInfo(ResultSet rs)
    {
        return new SybaseColumnInfo(rs);
    }

    public TypeInfo newTypeInfo(ResultSet rs)
    {
        TypeInfo ti = new TypeInfo(rs);

        // Discard the tinyint type because it doesn't support negative values.
        if (ti.typeName.toLowerCase().startsWith("tinyint"))
        {
            return null;
        }
        // Discard the longsysname type because it doesn't allow specification of length
        if (ti.typeName.toLowerCase().startsWith("longsysname"))
        {
            return null;
        }

        return ti;
    }

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

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

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

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

    /**
     * Method to handle the indexOf operation.
     * @param source The expression with the searched string
     * @param str The expression for the search string
     * @param from The from position (or null if not specified)
     * @return The expression.
     **/
    public NumericExpression indexOfMethod(ScalarExpression source, ScalarExpression str, NumericExpression from)
    {
        ScalarExpression integerLiteral = getMapping(BigInteger.class, source).newLiteral(source.getQueryExpression(), BigInteger.ONE);
        ArrayList args = new ArrayList();
        args.add(str);
        args.add(source);
        if (from != null)
        {
            // Add 1 to the passed in value so that it is of origin 1 to be compatible with CHARINDEX
            args.add(new NumericExpression(from, ScalarExpression.OP_ADD, integerLiteral));
        }
        NumericExpression locateExpr = new NumericExpression("CHARINDEX", args);

        // Subtract 1 from the result of CHARINDEX to be consistent with Java strings
        // TODO Would be nice to put this in parentheses
        return new NumericExpression(locateExpr, ScalarExpression.OP_SUB, integerLiteral);
    }

  /**
   * Accessor for the auto-increment sql statement for this datastore.
     * @param table Name of the table that the autoincrement is for
     * @param columnName Name of the column that the autoincrement is for
   * @return The statement for getting the latest auto-increment key
   **/
  public String getAutoIncrementStmt(Table table, String columnName)
  {
    return "SELECT @@IDENTITY";
  }

  /**
   * Accessor for the auto-increment keyword for generating DDLs (CREATE TABLEs...).
   * @return The keyword for a column using auto-increment
   **/
  public String getAutoIncrementKeyword()
  {
    return "IDENTITY";
  }
 
  /**
   * Whether we support auto-increment fields.
   * @return whether we support auto-increment fields.
   **/
  public boolean supportsIdentityFields()
  {
    return true;
  }

    /**
     * Whether we support auto-increment/identity keys with nullability spec.
     * @return whether we support auto-increment keys with nullability spec.
     **/
    public boolean supportsAutoIncrementKeysNullSpecification()
    {
        return false;
    }
}
TOP

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

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.