# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to you 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.

#
# This file contains SQL fragments that are used as
# part of the ODBC metadata generation process.
# This file is NOT, and is NOT intended to become,
# an ODBC substitute for metadata.properties.
# Rather, it is a place to store fragments of ODBC
# metadata statements that do not apply to JDBC
# metadata, but which are used as part of the ODBC
# metadata generation process (these fragments are
# plugged into the ODBC versions of the queries for
# which they are required).  When all is done,
# the fragments in this file will show up as part
# of the generated ODBC metadata queries, which
# will then be appended to Derby's internal list
# of metadata queries (in metadata.properties),
# which will, finally, serve as the basis for
# for both JDBC and ODBC metadata calls.
#
# This file, like the ODBCMetadataGenerator class
# that uses it, is ONLY USED AT BUILD TIME; and 
# like the ODBCMetadataGenerator class, it is NOT
# included in the final org.apache.derby package
# (and thus will NOT be included in the Derby jar
# file).
#
# Note: In this file, words between curly brackets
# (ex "{SQL_DATA_TYPE_FOR_ODBC}") are placeholders
# for other fragments in this file.  The actual
# substitutions for these placeholders occur as
# part of the ODBCMetadataGenerator class's work.
#
# Finally, note that starting a line in this file
# with the "\\\n" sequence allows formatting (esp.
# tabs and newlines) to be preserved, so that the
# generated ODBC queries are human-readable.

# ----------
#
# SQL_DATA_TYPE:
# Set SQL_DATA_TYPE, which is unused by JDBC (and
# thus returns NULL for JDBC), to the value as
# defined by the ODBC specification.  Note that
# any metadata statement requiring this fragment
# must already have a column named "DATA_TYPE"
# as part of the JDBC subquery's result set.
#
# Date, time, and timestamp columns are supposed
# to return a generic "SQL_DATETIME" value here;
# that's defined as the value "9".  All other
# types return their normal DATA_TYPE value.
#
SQL_DATA_TYPE_FOR_ODBC=\
CASE WHEN (JDBC_SUBQUERY.DATA_TYPE IN (java.sql.Types::DATE, \
\\\n	java.sql.Types::TIME, java.sql.Types::TIMESTAMP)) \
\\\n		 THEN 9 \
\\\n		 ELSE JDBC_SUBQUERY.DATA_TYPE END

# ----------
#
# SQL_DATETIME_SUB:
# Set SQL_DATETIME_SUB, which is unused by JDBC (and
# thus returns NULL for JDBC), to the value as
# defined by the ODBC specification.  Note that
# any metadata statement requiring this fragment
# must already have a column named "DATA_TYPE"
# as part of the JDBC subquery's result set.
#
# This value is null for all types except date, time,
# and timestamp.  For those, the values are defined
# as follows:
#
#	SQL_CODE_DATE       1
#	SQL_CODE_TIME       2
#	SQL_CODE_TIMESTAMP  3
#
DATETIME_SUB_FOR_ODBC=\
CASE WHEN (JDBC_SUBQUERY.DATA_TYPE = java.sql.Types::DATE) \
\\\n	THEN 1 \
\\\n	ELSE (CASE WHEN (JDBC_SUBQUERY.DATA_TYPE = java.sql.Types::TIME) \
\\\n		THEN 2 \
\\\n		ELSE (CASE WHEN (JDBC_SUBQUERY.DATA_TYPE = java.sql.Types::TIMESTAMP) \
\\\n			THEN 3 \
\\\n			ELSE CAST (NULL AS SMALLINT) END ) END ) END

# ----------
#
# UNSIGNED_ATTRIBUTE:
# Set UNSIGNED_ATTRIBUTE, which defaults to "true"
# for non-numeric types in JDBC, to be NULL for
# non-numeric types according the definition as
# given in the ODBC specification.  Note that
# any metadata statement requiring this fragment
# must already have a column named "DATA_TYPE"
# and a column named "UNSIGNED_ATTRIBUTE" as
# part of the JDBC subquery's result set.
#
UNSIGNED_ATTR_FOR_ODBC=\
CASE WHEN (JDBC_SUBQUERY.DATA_TYPE IN (java.sql.Types::DECIMAL, \
\\\n	java.sql.Types::NUMERIC, java.sql.Types::INTEGER, \
\\\n	java.sql.Types::SMALLINT, java.sql.Types::TINYINT, \
\\\n	java.sql.Types::BIGINT, java.sql.Types::DOUBLE, \
\\\n	java.sql.Types::FLOAT, java.sql.Types::REAL, \
\\\n	java.sql.Types::DATE, java.sql.Types::TIME, \
\\\n	java.sql.Types::TIMESTAMP)) \
\\\n		THEN JDBC_SUBQUERY.UNSIGNED_ATTRIBUTE \
\\\n		ELSE NULL END

# ----------
#
# AUTO_UNIQUE_VAL:
# Set AUTO_UNIQUE_VAL, which defaults to "false"
# for non-numeric types in JDBC, to be NULL for
# non-numeric types according the definition as
# given in the ODBC specification.  Note that
# any metadata statement requiring this fragment
# must already have a column named "DATA_TYPE"
# and a column named "AUTO_UNIQUE_VAL" as
# part of the JDBC subquery's result set.
#
AUTO_UNIQUE_FOR_ODBC=\
CASE WHEN (JDBC_SUBQUERY.DATA_TYPE IN (java.sql.Types::DECIMAL, \
\\\n	java.sql.Types::NUMERIC, java.sql.Types::INTEGER, \
\\\n	java.sql.Types::SMALLINT, java.sql.Types::TINYINT, \
\\\n	java.sql.Types::BIGINT, java.sql.Types::DOUBLE, \
\\\n	java.sql.Types::FLOAT, java.sql.Types::REAL, \
\\\n	java.sql.Types::DATE, java.sql.Types::TIME, \
\\\n	java.sql.Types::TIMESTAMP)) \
\\\n		THEN JDBC_SUBQUERY.AUTO_UNIQUE_VAL \
\\\n		ELSE NULL END

# ----------
#
# NUM_PREC_RADIX:
# Set NUM_PREC_RADIX, which is "10" for datetime
# values in JDBC, to be "2" for datetime values
# in ODBC, as given in the ODBC specification.
# Note that any metadata statement requiring this
# fragment must already have a column named
# "DATA_TYPE" and a column named "NUM_PREC_RADIX"
# as part of the JDBC subquery's result set.
#
RADIX_FOR_ODBC=\
CASE WHEN (JDBC_SUBQUERY.DATA_TYPE IN (java.sql.Types::DATE, \
\\\n	java.sql.Types::TIME, java.sql.Types::TIMESTAMP)) \
\\\n		THEN CAST (2 AS SMALLINT) \
\\\n		ELSE JDBC_SUBQUERY.NUM_PREC_RADIX END

# ----------
#
# DECIMAL_DIGITS:
# Set DECIMAL_DIGITS to be NULL for DATE columns
# in ODBC, as given in the ODBC specification.
# Note that any metadata statement requiring this
# fragment must already have a column named
# "DATA_TYPE" and a column named "DECIMAL_DIGITS"
# as part of the JDBC subquery's result set.
#
DECIMAL_DIGITS_FOR_ODBC=\
CASE WHEN (JDBC_SUBQUERY.DATA_TYPE IN (java.sql.Types::DATE)) \
\\\n		THEN CAST (NULL AS SMALLINT) \
\\\n		ELSE JDBC_SUBQUERY.DECIMAL_DIGITS END

# ----------
#
# Columns that need to be added to the getProcedureColumns result
# set for ODBC compliance.
#
GET_PROC_COLS_NEW_COLS=\
CAST (NULL AS VARCHAR(254)) AS COLUMN_DEF, \
\\\nCAST (({SQL_DATA_TYPE_FOR_ODBC}) AS SMALLINT) \
\\\n	AS SQL_DATA_TYPE, \
\\\nCAST (({DATETIME_SUB_FOR_ODBC}) AS SMALLINT) \
\\\n	AS SQL_DATETIME_SUB, \
\\\nCASE WHEN (JDBC_SUBQUERY.DATA_TYPE IN (java.sql.Types::CHAR, \
\\\n		java.sql.Types::VARCHAR, java.sql.Types::BINARY, \
\\\n		java.sql.Types::VARBINARY)) \
\\\n			THEN JDBC_SUBQUERY.BUFFER_LENGTH \
\\\n			ELSE CAST (NULL AS INT) END \
\\\n		AS CHAR_OCTET_LENGTH, \
\\\nCAST ((JDBC_SUBQUERY.PARAMETER_ID + 1) AS INT) AS ORDINAL_POSITION, \
\\\nCAST ((CASE WHEN (JDBC_SUBQUERY.NULLABLE IN \
\\\n		(java.sql.DatabaseMetaData::procedureNullable)) \
\\\n			THEN 'YES' \
\\\n			ELSE 'NO' END) \
\\\n		AS VARCHAR(128)) AS IS_NULLABLE

# ----------
#
# Columns that need to be added to the getTypeInfo result
# set for ODBC compliance.
#
GET_TYPE_INFO_NEW_COLS=\
CAST (NULL AS SMALLINT) AS INTERVAL_PRECISION

# ----------
#
# In order to correctly determine the BUFFER_LENGTH
# and CHAR_OCTET_LENGTH values for the ODBC version
# of getColumns, we need to retrieve the max width
# value of the column in question.  Since this
# specific value isn't returned as part of the JDBC
# metadata, we need to add it as a "helper" column
# to the JDBC subquery result set.  See the
# addHelperColsToSubquery method in the ODBC meta-
# data generator class for more details.
#
GET_COLS_HELPER_COLS=, \
\\\n		C.COLUMNDATATYPE.getMaximumWidthInBytes() AS COL_MAX_WIDTH_IN_BYTES

# ----------
#
# In order to correctly determine the BUFFER_LENGTH
# value for the ODBC versions of the getBestRow*
# queries, we need to retrieve the max width
# value of the column in question.  Since this
# specific value isn't returned as part of the JDBC
# metadata, we need to add it as a "helper" column
# to the JDBC subquery result set.  See the
# addHelperColsToSubquery method in the ODBC meta-
# data generator class for more details.
#
BEST_ROW_ID_HELPER_COLS=, \
\\\n		COLS.COLUMNDATATYPE.getMaximumWidthInBytes() AS COL_MAX_WIDTH_IN_BYTES

# ----------
#
# BUFFER_LENGTH:
# Set BUFFER_LENGTH, which is unused by JDBC (and
# thus returns NULL for JDBC), to the value as
# defined by the ODBC specification.  Note that
# any metadata statement requiring this fragment
# must already have a column named "DATA_TYPE"
# and a "helper" column named "COL_MAX_WIDTH_IN_BYTES"
# as part of the JDBC subquery's result set.
#
BUFFER_LEN_FOR_ODBC=\
JDBC_SUBQUERY.COL_MAX_WIDTH_IN_BYTES

# ----------
#
# CHAR_OCTET_LENGTH:
# Make CHAR_OCTET_LENGTH, which only applies to
# char cols in JDBC, apply to both char and binary
# columns per the ODBC specification.  Note that
# any metadata statement requiring this fragment
# must already have a column named "DATA_TYPE"
# and a "helper" column named "COL_MAX_WIDTH_IN_BYTES"
# as part of the JDBC subquery's result set.
#
CHAR_OCTET_FOR_ODBC=\
CASE WHEN (JDBC_SUBQUERY.DATA_TYPE IN (java.sql.Types::CHAR, \
\\\n	java.sql.Types::VARCHAR, java.sql.Types::BINARY, \
\\\n	java.sql.Types::VARBINARY)) \
\\\n		THEN JDBC_SUBQUERY.COL_MAX_WIDTH_IN_BYTES \
\\\n		ELSE CAST(NULL AS INT) END

