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) 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
...
**********************************************************************/
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 org.datanucleus.ClassLoaderResolver;
import org.datanucleus.exceptions.ClassNotResolvedException;
import org.datanucleus.exceptions.NucleusDataStoreException;
import org.datanucleus.exceptions.NucleusUserException;
import org.datanucleus.identity.DatastoreId;
import org.datanucleus.metadata.JdbcType;
import org.datanucleus.plugin.PluginManager;
import org.datanucleus.store.connection.ManagedConnection;
import org.datanucleus.store.rdbms.identifier.IdentifierFactory;
import org.datanucleus.store.rdbms.key.Index;
import org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping;
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.sql.SelectStatement;
import org.datanucleus.store.rdbms.table.Column;
import org.datanucleus.store.rdbms.table.Table;
import org.datanucleus.store.schema.StoreSchemaHandler;
import org.datanucleus.util.Localiser;
import org.datanucleus.util.NucleusLogger;
import org.datanucleus.util.StringUtils;
/**
* Provides methods for adapting SQL language elements to the Microsoft SQL Server database.
* Note that majorVersion from JDBC doesn't align to version number of SQLServer.
* 1995 = "6", 1998 = "7", 2000/2003 = "8", 2005 = "9", 2008 = "10", 2012 = "11", 2014 = "12", ...
*
* @see BaseDatastoreAdapter
*/
public class SQLServerAdapter extends BaseDatastoreAdapter
{
/**
* Microsoft SQL Server 2000 uses reserved keywords for defining,
* manipulating, and accessing databases. Reserved keywords are part of the
* grammar of the Transact-SQL language used by SQL Server to parse and
* understand Transact-SQL statements and batches. Although it is
* syntactically possible to use SQL Server reserved keywords as identifiers
* and object names in Transact-SQL scripts, this can be done only using
* delimited identifiers.
*/
private static final String MSSQL_RESERVED_WORDS =
"ADD,ALL,ALTER,AND,ANY,AS," +
"ASC,AUTHORIZATION,BACKUP,BEGIN,BETWEEN,BREAK," +
"BROWSE,BULK,BY,CASCADE,CASE,CHECK," +
"CHECKPOINT,CLOSE,CLUSTERED,COALESCE,COLLATE,COLUMN," +
"COMMIT,COMPUTE,CONSTRAINT,CONTAINS,CONTAINSTABLE,CONTINUE," +
"CONVERT,CREATE,CROSS,CURRENT,CURRENT_DATE,CURRENT_TIME," +
"CURRENT_TIMESTAMP,CURRENT_USER,CURSOR,DBCC,DEALLOCATE,DECLARE," +
"DEFAULT,DELETE,DENY,DESC,DISK,DISTINCT," +
"DISTRIBUTED,DOUBLE,DROP,DUMMY,DUMP,ELSE," +
"END,ERRLVL,ESCAPE,EXCEPT,EXEC,EXECUTE," +
"EXISTS,EXIT,FETCH,FILE,FILLFACTOR,FOR," +
"FOREIGN,FREETEXT,FREETEXTTABLE,FROM,FULL,FUNCTION," +
"GOTO,GRANT,GROUP,HAVING,HOLDLOCK,IDENTITY," +
"IDENTITY_INSERT,IDENTITYCOL,IF,IN,INDEX,INNER," +
"INSERT,INTERSECT,INTO,IS,JOIN,KEY," +
"KILL,LEFT,LIKE,LINENO,LOAD,NATIONAL," +
"NOCHECK,NONCLUSTERED,NOT,NULL,NULLIF,OF," +
"OFF,OFFSETS,ON,OPEN,OPENDATASOURCE,OPENQUERY," +
"OPENROWSET,OPENXML,OPTION,OR,ORDER,OUTER," +
"OVER,PERCENT,PLAN,PRECISION,PRIMARY,PRINT," +
"PROC,PROCEDURE,PUBLIC,RAISERROR,READ,READTEXT," +
"RECONFIGURE,REFERENCES,REPLICATION,RESTORE,RESTRICT,RETURN," +
"REVOKE,RIGHT,ROLLBACK,ROWCOUNT,ROWGUIDCOL,RULE," +
"SAVE,SCHEMA,SELECT,SESSION_USER,SET,SETUSER," +
"SHUTDOWN,SOME,STATISTICS,SYSTEM_USER,TABLE,TEXTSIZE," +
"THEN,TO,TOP,TRAN,DATABASE,TRANSACTION,TRIGGER," +
"TRUNCATE,TSEQUAL,UNION,UNIQUE,UPDATE,UPDATETEXT," +
"USE,USER,VALUES,VARYING,VIEW,WAITFOR," +
"WHEN,WHERE,WHILE,WITH,WRITETEXT";
/**
* Constructs a SQL Server adapter based on the given JDBC metadata.
* @param metadata the database metadata.
*/
public SQLServerAdapter(DatabaseMetaData metadata)
{
super(metadata);
reservedKeywords.addAll(StringUtils.convertCommaSeparatedStringToSet(MSSQL_RESERVED_WORDS));
supportedOptions.add(IDENTITY_COLUMNS);
supportedOptions.add(LOCK_OPTION_PLACED_AFTER_FROM);
supportedOptions.add(LOCK_OPTION_PLACED_WITHIN_JOIN);
supportedOptions.add(ANALYSIS_METHODS);
supportedOptions.add(STORED_PROCEDURES);
supportedOptions.add(ORDERBY_NULLS_USING_CASE_NULL);
supportedOptions.remove(BOOLEAN_COMPARISON);
supportedOptions.remove(DEFERRED_CONSTRAINTS);
supportedOptions.remove(FK_DELETE_ACTION_DEFAULT);
supportedOptions.remove(FK_DELETE_ACTION_RESTRICT);
supportedOptions.remove(FK_DELETE_ACTION_NULL);
supportedOptions.remove(FK_UPDATE_ACTION_DEFAULT);
supportedOptions.remove(FK_UPDATE_ACTION_RESTRICT);
supportedOptions.remove(FK_UPDATE_ACTION_NULL);
if (datastoreMajorVersion >= 11)
{
// SQLServer 2012+ support these features
supportedOptions.add(SEQUENCES);
}
if (datastoreMajorVersion >= 12)
{
// SQLServer 2014+ support these features (what about earlier?)
supportedOptions.add(OPERATOR_BITWISE_AND);
supportedOptions.add(OPERATOR_BITWISE_OR);
supportedOptions.add(OPERATOR_BITWISE_XOR);
}
}
/**
* Initialise the types for this datastore.
* @param handler SchemaHandler that we initialise the types for
* @param mconn Managed connection to use
*/
public void initialiseTypes(StoreSchemaHandler handler, ManagedConnection mconn)
{
super.initialiseTypes(handler, mconn);
// Add on any missing JDBC types
SQLTypeInfo sqlType = new org.datanucleus.store.rdbms.adapter.SQLServerTypeInfo(
"UNIQUEIDENTIFIER", (short)Types.CHAR, 36, "'", "'", "", 1, false, (short)2, false, false, false, "UNIQUEIDENTIFIER", (short)0, (short)0, 10);
sqlType.setAllowsPrecisionSpec(false);
addSQLTypeForJDBCType(handler, mconn, (short)SQLServerTypeInfo.UNIQUEIDENTIFIER, sqlType, true);
sqlType = new org.datanucleus.store.rdbms.adapter.SQLServerTypeInfo(
"IMAGE", (short)Types.BLOB, 2147483647, null, null, null, 1, false, (short)1, false, false, false, "BLOB", (short)0, (short)0, 0);
addSQLTypeForJDBCType(handler, mconn, (short)Types.BLOB, sqlType, true);
sqlType = new org.datanucleus.store.rdbms.adapter.SQLServerTypeInfo(
"TEXT", (short)Types.CLOB, 2147483647, null, null, null, 1, true, (short)1, false, false, false, "TEXT", (short)0, (short)0, 0);
addSQLTypeForJDBCType(handler, mconn, (short)Types.CLOB, sqlType, true);
sqlType = new org.datanucleus.store.rdbms.adapter.SQLServerTypeInfo(
"float", (short)Types.DOUBLE, 53, null, null, null, 1, false, (short)2, false, false, false, null, (short)0, (short)0, 2);
addSQLTypeForJDBCType(handler, mconn, (short)Types.DOUBLE, sqlType, true);
sqlType = new org.datanucleus.store.rdbms.adapter.SQLServerTypeInfo(
"IMAGE", (short)Types.LONGVARBINARY, 2147483647, null, null, null, 1, false, (short)1, false, false, false, "LONGVARBINARY", (short)0, (short)0, 0);
addSQLTypeForJDBCType(handler, mconn, (short)Types.LONGVARBINARY, sqlType, true);
if (datastoreMajorVersion > 9)
{
// Support for build-in TIME and DATE data type for MS SQL Server version >= 2008
sqlType = new org.datanucleus.store.rdbms.adapter.SQLServerTypeInfo(
"TIME", (short)Types.TIME, 0, null, null, null, 1, false, (short)1, true, true, false, "TIME", (short)0, (short)0, 0);
addSQLTypeForJDBCType(handler, mconn, (short)Types.TIME, sqlType, true);
sqlType = new org.datanucleus.store.rdbms.adapter.SQLServerTypeInfo(
"DATE", (short)Types.DATE, 0, null, null, null, 1, false, (short)1, true, true, false, "DATE", (short)0, (short)0, 0);
addSQLTypeForJDBCType(handler, mconn, (short)Types.DATE, sqlType, true);
}
}
public String getVendorID()
{
return "sqlserver";
}
/**
* Accessor for the catalog name.
* @param conn The Connection to use
* @return The catalog name used by this connection
* @throws SQLException if an error occurs
*/
public String getCatalogName(Connection conn)
throws SQLException
{
String catalog = conn.getCatalog();
// the ProbeTable approach returns empty string instead of null here,
// so do the same
return catalog != null ? catalog : "";
}
public String getSchemaName(Connection conn) throws SQLException
{
if (datastoreMajorVersion >= 9) // SQLServer 2005 onwards
{
// Since SQL Server 2005, SCHEMA_NAME() will return the current schema name of the caller
Statement stmt = conn.createStatement();
try
{
String stmtText = "SELECT SCHEMA_NAME();";
ResultSet rs = stmt.executeQuery(stmtText);
try
{
if (!rs.next())
{
throw new NucleusDataStoreException("No result returned from " + stmtText).setFatal();
}
return rs.getString(1);
}
finally
{
rs.close();
}
}
finally
{
stmt.close();
}
}
/*
* As of version 7 there was no equivalent to the concept of "schema"
* in SQL Server. For DatabaseMetaData functions that include
* SCHEMA_NAME drivers usually return the user name that owns the table.
*
* So the default ProbeTable method for determining the current schema
* just ends up returning the current user. If we then use that name in
* performing metadata queries our results may get filtered down to just
* objects owned by that user. So instead we report the schema name as
* null which should cause those queries to return everything.
*
* DO not use the user name here, as in MSSQL, you are able to use
* an user name and access any schema.
*
* Use an empty string, otherwise fullyqualified object name is invalid
* In MSSQL, fully qualified must include the object owner, or an empty owner
* ..