Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**********************************************************************
Copyright (c) 2015 Jeff Albion 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:
...
**********************************************************************/
package org.datanucleus.store.rdbms.adapter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.JDBCType;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.List;
import java.util.Properties;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.exceptions.NucleusUserException;
import org.datanucleus.identity.DatastoreId;
import org.datanucleus.plugin.PluginManager;
import org.datanucleus.store.StoreManager;
import org.datanucleus.store.rdbms.identifier.IdentifierFactory;
import org.datanucleus.store.rdbms.key.ForeignKey;
import org.datanucleus.store.rdbms.key.PrimaryKey;
import org.datanucleus.store.rdbms.schema.RDBMSColumnInfo;
import org.datanucleus.store.rdbms.schema.SQLTypeInfo;
import org.datanucleus.store.rdbms.sql.SQLTable;
import org.datanucleus.store.rdbms.sql.SQLText;
import org.datanucleus.store.rdbms.table.Column;
import org.datanucleus.store.rdbms.table.Table;
import org.datanucleus.store.rdbms.table.TableImpl;
import org.datanucleus.util.Localiser;
import org.datanucleus.util.StringUtils;
/**
* Provides methods for adapting SQL language elements to the SQL Anywhere database.
*/
public class SQLAnywhereAdapter extends BaseDatastoreAdapter
{
/**
* SQL Anywhere uses a product version of "major.minor.revision.build"
*/
protected int datastoreBuildVersion = -1;
protected int driverBuildVersion = -1;
protected boolean usingjConnect = true;
/**
* Constructor.
* @param metadata MetaData for the DB
*/
public SQLAnywhereAdapter(DatabaseMetaData metadata)
{
super(metadata);
// Determine the build versions of the database / driver
try
{
datastoreBuildVersion = Integer.parseInt(datastoreProductVersion.substring(datastoreProductVersion.lastIndexOf(".") + 1));
if (driverName.equals("SQL Anywhere JDBC Driver"))
{
usingjConnect = false;
driverBuildVersion = Integer.parseInt(driverVersion.substring(driverVersion.lastIndexOf(".") + 1));
}
else
{
// Assume jConnect if it isn't SQLAJDBC
// TODO: jConnect version detection
driverBuildVersion = -1;
}
}
catch (Throwable t)
{
// Parsing error for the version, ignore
}
// Determine the keyword list. This list is obtained from the JDBC driver using
// DatabaseMetaData.getSQLKeywords(), but there are also user configurable options
// Instead, attempt to query the set of reserved words for SQL Anywhere directly.
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try
{
// Query the standard keywords
conn = metadata.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT \"reserved_word\" FROM sa_reserved_words() ORDER BY \"reserved_word\"");
while (rs.next())
{
reservedKeywords.add(rs.getString(1).trim().toUpperCase());
}
rs.close();
// Also use user-specified keywords, if they are specified
rs = stmt.executeQuery("SELECT \"option\", \"setting\" FROM SYS.SYSOPTION WHERE \"option\" = 'reserved_keywords' or \"option\" = 'non_keywords'");
while (rs.next())
{
if (rs.getString(1).toLowerCase().equals("reserved_keywords"))
{
String originalUserKeywords = rs.getString(2).trim().toUpperCase();
StringTokenizer tokens = new StringTokenizer(originalUserKeywords, ",");
Set userReservedWordSet = new HashSet<>();
while (tokens.hasMoreTokens())
{
userReservedWordSet.add(tokens.nextToken().trim().toUpperCase());
}
// If LIMIT isn't enabled by the customized database keywords, set it to enable LIMIT
if (!userReservedWordSet.contains("LIMIT"))
{
userReservedWordSet.add("LIMIT");
Statement setOptionStmt = null;
try
{
setOptionStmt = conn.createStatement();
setOptionStmt.executeUpdate("SET OPTION PUBLIC.reserved_keywords = 'LIMIT" +
(originalUserKeywords.length() != 0 ? "," : "") + originalUserKeywords + "'");
}
finally
{
if (setOptionStmt != null)
{
setOptionStmt.close();
}
}
}
reservedKeywords.addAll(userReservedWordSet);
// Allow the user to override and remove keywords for compatibility, if necessary
}
else if (rs.getString(1).toLowerCase().equals("non_keywords"))
{
reservedKeywords.removeAll(StringUtils.convertCommaSeparatedStringToSet(rs.getString(2).trim().toUpperCase()));
}
}
rs.close();
stmt.close();
}
catch (Throwable t)
{
// JDBC metadata is still used for keywords, and assume LIMIT is set elsewhere
}
finally
{
try
{
if (rs != null && !rs.isClosed())
{
rs.close();
}
}
catch (SQLException sqle)
{
rs = null;
}
try
{
if (stmt != null && !stmt.isClosed())
{
stmt.close();
}
}
catch (SQLException sqle)
{
stmt = null;
}
try
{
if (conn != null && !conn.isClosed())
{
conn.close();
}
}
catch (SQLException sqle)
{
conn = null;
}
}
// Provide supported / unsupported capabilities for SQL Anywhere
// See: DatastoreAdapter.java for options, BaseDatastoreAdapter.java for defaults
supportedOptions.add(IDENTITY_COLUMNS);
supportedOptions.add(STORED_PROCEDURES);
if (datastoreMajorVersion >= 12)
{
supportedOptions.add(SEQUENCES);
}
supportedOptions.add(PROJECTION_IN_TABLE_REFERENCE_JOINS);
supportedOptions.add(CATALOGS_IN_TABLE_DEFINITIONS);
supportedOptions.add(SCHEMAS_IN_TABLE_DEFINITIONS);
supportedOptions.add(IDENTIFIERS_LOWERCASE);
supportedOptions.add(IDENTIFIERS_MIXEDCASE);
supportedOptions.add(IDENTIFIERS_UPPERCASE);
supportedOptions.add(IDENTIFIERS_LOWERCASE_QUOTED);
supportedOptions.add(IDENTIFIERS_MIXEDCASE_QUOTED);
supportedOptions.add(IDENTIFIERS_UPPERCASE_QUOTED);
supportedOptions.add(ALTER_TABLE_DROP_FOREIGN_KEY_CONSTRAINT);
supportedOptions.add(STATEMENT_BATCHING);
supportedOptions.add(PRIMARYKEY_IN_CREATE_STATEMENTS);
supportedOptions.add(IDENTITY_PK_IN_CREATE_TABLE_COLUMN_DEF);
supportedOptions.add(LOCK_ROW_USING_SELECT_FOR_UPDATE);
supportedOptions.add(LOCK_ROW_USING_OPTION_AFTER_FROM);
supportedOptions.add(OPERATOR_BITWISE_AND);
supportedOptions.add(OPERATOR_BITWISE_OR);
supportedOptions.add(OPERATOR_BITWISE_XOR);
supportedOptions.remove(GET_GENERATED_KEYS_STATEMENT); // Statement.getGeneratedKeys() not supported
supportedOptions.remove(DEFERRED_CONSTRAINTS); // No
supportedOptions.remove(ANSI_JOIN_SYNTAX); // Deprecated
supportedOptions.remove(ANSI_CROSSJOIN_SYNTAX); // Deprecated
supportedOptions.remove(IDENTITY_KEYS_NULL_SPECIFICATION); // No
supportedOptions.remove(BOOLEAN_COMPARISON); // No
// Add the supported and unsupported JDBC types for lookups
supportedJdbcTypesById.clear();
supportedJdbcTypesById.put(Integer.valueOf(Types.BIGINT), "BIGINT");
supportedJdbcTypesById.put(Integer.valueOf(Types.BINARY), "BINARY");
supportedJdbcTypesById.put(Integer.valueOf(Types.BIT), "BIT");
supportedJdbcTypesById.put(Integer.valueOf(Types.BLOB), "LONG BINARY");
supportedJdbcTypesById.put(Integer.valueOf(Types.BOOLEAN), "BIT");
supportedJdbcTypesById.put(Integer.valueOf(Types.CHAR), "CHAR");
supportedJdbcTypesById.put(Integer.valueOf(Types.CLOB), "LONG VARCHAR");
supportedJdbcTypesById.put(Integer.valueOf(Types.DATE), "DATE");
supportedJdbcTypesById.put(Integer.valueOf(Types.DECIMAL), "DECIMAL");
supportedJdbcTypesById.put(Integer.valueOf(Types.DOUBLE), "DOUBLE");
supportedJdbcTypesById.put(Integer.valueOf(Types.FLOAT), "FLOAT");
supportedJdbcTypesById.put(Integer.valueOf(Types.INTEGER), "INTEGER");
supportedJdbcTypesById.put(Integer.valueOf(Types.LONGVARBINARY), "LONG BINARY");
supportedJdbcTypesById.put(Integer.valueOf(Types.LONGVARCHAR), "LONG VARCHAR");
supportedJdbcTypesById.put(Integer.valueOf(Types.NUMERIC), "NUMERIC");
supportedJdbcTypesById.put(Integer.valueOf(Types.REAL), "REAL");
supportedJdbcTypesById.put(Integer.valueOf(Types.SMALLINT), "SMALLINT");
supportedJdbcTypesById.put(Integer.valueOf(Types.SQLXML), "XML");
supportedJdbcTypesById.put(Integer.valueOf(Types.TIME), "TIME");
supportedJdbcTypesById.put(Integer.valueOf(Types.TIMESTAMP), "TIMESTAMP");
supportedJdbcTypesById.put(Integer.valueOf(Types.TINYINT), "TINYINT");
supportedJdbcTypesById.put(Integer.valueOf(Types.VARBINARY), "BINARY");
supportedJdbcTypesById.put(Integer.valueOf(Types.VARCHAR), "VARCHAR");
supportedJdbcTypesById.put(Integer.valueOf(Types.NVARCHAR), "NVARCHAR");
supportedJdbcTypesById.put(Integer.valueOf(Types.NCHAR), "NCHAR");
supportedJdbcTypesById.put(Integer.valueOf(Types.NCLOB), "LONG NVARCHAR");
supportedJdbcTypesById.put(Integer.valueOf(Types.OTHER), "OTHER");
unsupportedJdbcTypesById.clear();
unsupportedJdbcTypesById.put(Integer.valueOf(Types.ARRAY), "ARRAY"); // Maybe...?
unsupportedJdbcTypesById.put(Integer.valueOf(Types.DATALINK), "DATALINK");
unsupportedJdbcTypesById.put(Integer.valueOf(Types.DISTINCT), "DISTINCT");
unsupportedJdbcTypesById.put(Integer.valueOf(Types.JAVA_OBJECT), "JAVA_OBJECT");
unsupportedJdbcTypesById.put(Integer.valueOf(Types.NULL), "NULL");
unsupportedJdbcTypesById.put(Integer.valueOf(Types.REF), "REF");
unsupportedJdbcTypesById.put(Integer.valueOf(Types.STRUCT), "STRUCT"); // Maybe ROW would work here?
}
public String getVendorID()
{
return "sqlanywhere";
}
public String getCreateDatabaseStatement(String catalogName, String schemaName)
{
throw new UnsupportedOperationException("SQL Anywhere does not support CREATE DATABASE via a schema name");
}
public String getDropDatabaseStatement(String catalogName, String schemaName)
{
throw new UnsupportedOperationException("SQL Anywhere does not support DROP DATABASE via a schema name");
}
/**
* Returns the appropriate SQL to create the given table having the given columns. No column constraints
* or key definitions should be included. It should return something like:
*
*