/**********************************************************************
Copyright (c) 2002 David Jencks 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
...
**********************************************************************/
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.StringExpression;
import org.jpox.store.mapped.expression.TableExprAsJoins;
import org.jpox.store.rdbms.typeinfo.FirebirdTypeInfo;
import org.jpox.store.rdbms.typeinfo.TypeInfo;
/**
* Provides methods for adapting SQL language elements to the Firebird database.
*
* @see DatabaseAdapter
* @version $Revision: 1.19 $
*/
public class FirebirdAdapter extends DatabaseAdapter
{
/**
* Constructs a Firebird adapter based on the given JDBC metadata.
* @param metadata the database metadata.
*/
public FirebirdAdapter(DatabaseMetaData metadata)
{
super(metadata);
}
public String getVendorID()
{
return "firebird";
}
public String getDropTableStatement(DatastoreContainerObject table)
{
return "DROP TABLE " + table.toString();
}
public boolean supportsDeferredConstraints()
{
return false;
}
/**
* Whether to create indexes before foreign keys.
* @return Whether to create indexes before foreign keys
**/
public boolean createIndexesBeforeForeignKeys()
{
return true;
}
public boolean supportsBooleanComparison()
{
return false;
}
public boolean supportsNullsInCandidateKeys()
{
return false;
}
/**
* Whether the database support NULLs in the column options for table creation.
* @return whether the database support NULLs in the column options for table creation.
**/
public boolean supportsNullsKeywordInColumnOptions()
{
return false;
}
/**
* Firebird doesn't like 'order by' in selects.
* @return Whether to include ORDER BY in SELECTs, always false for Firebird
**/
public boolean includeOrderByColumnsInSelect()
{
return false;
}
/**
* Whether this datastore supports ALTER TABLE DROP FOREIGN KEY constraints
* @return whether we support ALTER TABLE DROP FOREIGN KEY constraints
**/
public boolean supportsAlterTableDropForeignKeyConstraint()
{
return true;
}
/**
* Whether this datastore supports locking using SELECT ... FOR UPDATE.
* @return whether we support locking using SELECT ... FOR UPDATE.
**/
public boolean supportsLockWithSelectForUpdate()
{
return true;
}
public LogicSetExpression newTableExpression(QueryExpression qs, DatastoreContainerObject table, DatastoreIdentifier rangeVar)
{
return new TableExprAsJoins(qs, table, rangeVar);
}
public NumericExpression lengthMethod(StringExpression str)
{
ArrayList args = new ArrayList();
args.add(str);
return new NumericExpression("STRLEN", 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(BigInteger.class, str).newLiteral(str.getQueryExpression(), BigInteger.ONE)));
args.add(end);
return new StringExpression("SUBSTR", args);
}
/**
* Factory for TypeInfo objects.
* @param rs The ResultSet from DatabaseMetaData.getTypeInfo().
* @return A TypeInfo object.
**/
public TypeInfo newTypeInfo(ResultSet rs)
{
TypeInfo ti=new FirebirdTypeInfo(rs);
return ti;
}
/**
* Whether we support sequences.
* @return whether we support sequences.
**/
public boolean supportsSequences()
{
return true;
}
/**
* Accessor for the sequence create statement for this datastore.
* TODO Change param types to int.
* @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 UnsupportedOperationException("Adapter.SequenceNameNullNotSupported");
}
StringBuffer stmt = new StringBuffer("CREATE GENERATOR ");
stmt.append(sequence_name);
// TODO Can we use the additional parameters ?
return stmt.toString();
}
/**
* Accessor for the sequence statement to get the next id 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 UnsupportedOperationException("Adapter.SequenceNameNullNotSupported");
}
StringBuffer stmt=new StringBuffer("SELECT GEN_ID(");
stmt.append(sequence_name);
stmt.append(",1) FROM RDB$DATABASE");
return stmt.toString();
}
}