/*
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * Modified by
 * @author  Data Virtuality GmbH
 * @date    $Date$
 *
 */
/*
 * EXTENSION
 * ISSUE: DVCORE-2627, DVCORE-2521
 * FIXED BY: Salvatore Raunich, Roman Pedchenko
 * DATE: 2015-02-05, 2014-12-16
 * ADOPTED FROM: TEIID-3324
 * ADOPTED FROM: TEIID-2153, 2171, 2359, 2149, 2358, 2410, 2271, 2410, 2467, 2429, 2363, 2160, 2461, 2797, 2326, 1557, 2576
 * NOTES: the patch from TEIID-3324 has been manually adapted to the current code since a refactoring
 * was introduced in TEIID-2384 (not backported in this version)
 * END EXTENSION
 */
// https://issues.jboss.org/browse/TEIID-4008
// https://issues.jboss.org/browse/TEIID-4631
// https://issues.jboss.org/browse/TEIID-2904
// https://issues.jboss.org/browse/TEIID-2176
// https://issues.jboss.org/browse/TEIID-2177
// https://issues.jboss.org/browse/TEIID-2281
// https://issues.jboss.org/browse/TEIID-3530
// https://issues.redhat.com/browse/TEIID-5709
// https://issues.redhat.com/browse/TEIID-6043
// https://issues.redhat.com/browse/TEIID-4512
// https://issues.redhat.com/browse/TEIID-5313
// https://issues.redhat.com/browse/TEIID-3168
// https://issues.redhat.com/browse/TEIID-4202
// https://github.com/teiid/teiid/commit/368862d9ea7612a0fba06b772fd17e4aabe2d677
// https://issues.redhat.com/browse/TEIID-2384
// https://github.com/teiid/teiid/commit/fe313fa52c059b1d9ae3171cab557aba9fca200a
// https://issues.redhat.com/browse/TEIID-5227
// https://issues.redhat.com/browse/TEIID-5480
// https://github.com/teiid/teiid/commit/98d72229100f46dfa1426fca93a3f3cbb5753bc6
// https://issues.redhat.com/browse/TEIID-5499
package org.teiid.translator.jdbc;

import com.datavirtuality.dv.core.interfaces.DVCaller;
import org.teiid.core.types.AbstractGeospatialType;
import org.teiid.core.types.ArrayImpl;
import org.teiid.core.types.BinaryType;
import org.teiid.core.types.BlobType;
import org.teiid.core.types.DataTypeManager;
import org.teiid.core.types.GeographyType;
import org.teiid.core.types.GeometryType;
import org.teiid.core.types.JsonType;
import org.teiid.core.util.Base64;
import org.teiid.core.util.PropertiesUtils;
import org.teiid.core.util.StringUtil;
import org.teiid.language.*;
import org.teiid.language.Argument.Direction;
import org.teiid.language.SetQuery.Operation;
import org.teiid.language.visitor.HierarchyVisitor;
import org.teiid.logging.LogConstants;
import org.teiid.logging.LogManager;
import org.teiid.metadata.MetadataFactory;
import org.teiid.metadata.RuntimeMetadata;
import org.teiid.translator.*;
import org.teiid.translator.TypeFacility.RUNTIME_CODES;
import org.teiid.translator.jdbc.JDBCMetadataProcessor.ITypeConverter;
import org.teiid.translator.jdbc.plugin.JDBCPlugin;

import javax.sql.DataSource;
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.*;
import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;


/**
 * JDBC implementation of Connector interface.
 */
@Translator(name="jdbc-ansi", description="JDBC ANSI translator, can used with any ANSI compatible JDBC Driver")
public class JDBCExecutionFactory extends ExecutionFactory<DataSource, Connection> {

	public static final int DEFAULT_MAX_IN_CRITERIA = 1000;
	public static final int DEFAULT_MAX_DEPENDENT_PREDICATES = 50;
    private static final double DOUBLE_MIN = -1.797693e+308;
    private static final float FLOAT_MIN = -3.402823e+38f;

    public static void addColumnList(StringBuilder script, List<ColumnReference> columns) {
        if (columns != null) {
            script.append('(');
            Iterator<ColumnReference> iter = columns.iterator();
            int index = 0;
            while (iter.hasNext()) {
                final ColumnReference column = iter.next();
                if (column.getMetadataObject() != null) {
                    script.append(column.getMetadataObject().getNameInSource());
                } else if (column.getTable().getMetadataObject() != null) {
                    // below code line is added to make DVCORE-7796 integration test to be successful
                    // use column metadata from inside table metadata indirectly
                    script.append(column.getTable().getMetadataObject().getColumns().get(index++).getNameInSource());
                }
                if (iter.hasNext()) {
                    script.append(',');
                }
            }
            script.append(')');
        }
    }

    public enum CaseType {
        NONE,
        LOWERCASE,
        UPPERCASE,
        PRESERVE
    }

    private static final ThreadLocal<MessageFormat> COMMENT = new ThreadLocal<MessageFormat>() {
	    @Override
    	protected MessageFormat initialValue() {
    		return new MessageFormat("/*teiid sessionid:{0}, requestid:{1}.{2}*/ "); //$NON-NLS-1$
    	}
    };
    public final static TimeZone DEFAULT_TIME_ZONE = TimeZone.getDefault();

    protected static Double preventOverflowDouble(Double d, double doubleMin, double doubleMax) {
        if (d < doubleMin || d > doubleMax) {
            Double d1 = d;
            if (d < doubleMin) {
                d = doubleMin;
            } else {
                d = doubleMax;
            }
            LogManager.logWarning(LogConstants.CTX_CONNECTOR,
                    "DOUBLE overflow occurred: " + d1 + "; cutting to " + d); //$NON-NLS-1$
        }
        return d;
    }

    protected static Float preventOverflowFloat(Float f, float floatMin, float floatMax) {
        if (f < floatMin || f > floatMax) {
            Float f1 = f;
            if (f < floatMin) {
                f = floatMin;
            } else {
                f = floatMax;
            }
            LogManager.logWarning(LogConstants.CTX_CONNECTOR,
                    "FLOAT overflow occurred: " + f1 + "; cutting to " + f); //$NON-NLS-1$
        }
        return f;
    }

    protected static Double preventUnderflow(Double d, double underflowPos, double underflowNeg) {
        if ((d > 0 && d < underflowPos) || (d < 0 && d > underflowNeg)) {
            LogManager.logWarning(LogConstants.CTX_CONNECTOR,
                    "DOUBLE underflow occurred: " + d + "; cutting to 0.0"); //$NON-NLS-1$
            d = 0.0;
        }
        return d;
    }

    public Object translateLiteralFloat(float obj) {
        return obj;
    }

    public Object translateLiteralDouble(double obj) {
        return obj;
    }

    public String translateGetStringLiteralPrefix() {
        return "";
    }

    static class DatabaseCalender extends ThreadLocal<Calendar> {
    	private String timeZone;
    	public DatabaseCalender(String tz) {
    		this.timeZone = tz;
    	}
    	@Override
    	protected Calendar initialValue() {
            if(this.timeZone != null && this.timeZone.trim().length() > 0) {
            	TimeZone tz = TimeZone.getTimeZone(this.timeZone);
                if(!DEFAULT_TIME_ZONE.hasSameRules(tz)) {
            		return Calendar.getInstance(tz);
                }
            }
    		return Calendar.getInstance();
    	}
    };

    private Map<String, FunctionModifier> functionModifiers = new TreeMap<String, FunctionModifier>(StringUtil.CASE_INSENSITIVE_ORDER);

	private boolean useBindVariables = true;
	private String databaseTimeZone;
	private boolean trimStrings;
	private boolean useCommentsInSourceQuery;
	// EXTENSION
	// ISSUE: DV-99
	// FIXED BY: Salvatore Raunich
	// Applied solution from TEIID-2477
	// DATE: 2013.11.08
	// private String version;
	private Version version;
	// END EXTENSION
	private int maxInsertBatchSize = 2048;
	private DatabaseCalender databaseCalender;
	private boolean supportsGeneratedKeys;
	private boolean useBindingsForDependentJoin = true;
	private boolean enableReAuth;						  

	private AtomicBoolean initialConnection = new AtomicBoolean(true);

	public JDBCExecutionFactory() {
		setSupportsFullOuterJoins(true);
		setSupportsOrderBy(true);
		setSupportsOuterJoins(true);
		setSupportsSelectDistinct(true);
		setSupportsInnerJoins(true);
		setMaxInCriteriaSize(DEFAULT_MAX_IN_CRITERIA);
		setMaxDependentInPredicates(DEFAULT_MAX_DEPENDENT_PREDICATES);
	}

	@Override
	public void start() throws TranslatorException {
	    if (getColumnNameType() == CaseType.UPPERCASE ||
                getColumnNameType() == CaseType.PRESERVE)
	        forceQuotedIdentifiers = true;
        super.start();
		this.databaseCalender = new DatabaseCalender(this.databaseTimeZone);
    }

    @TranslatorProperty(display="Database Version", description= "Database Version")
    public String getDatabaseVersion() {
    	// EXTENSION
    	// ISSUE: DV-99
    	// FIXED BY: Salvatore Raunich
    	// Applied solutions from TEIID-2344, TEIID-2477
    	// DATE: 2013.11.08
//    	return this,version;
    	return this.version.toString();
    	// END EXTENSION
    }

    /**
     * Sets the database version.  See also {@link #getVersion()}
     * @param version
     */
    public void setDatabaseVersion(String version) {
        // EXTENSION
    	// ISSUE: DV-99
    	// FIXED BY: Salvatore Raunich
    	// Applied solution from TEIID-2477
    	// DATE: 2013.11.08
//    	this.version = version;
    	this.version = Version.getVersion(version);
    	// END EXTENSION
    }
    // EXTENSION
	// ISSUE: DV-99
	// FIXED BY: Salvatore Raunich
	// Applied solution from TEIID-2477
	// DATE: 2013.11.08
    public void setDatabaseVersion(Version version) {
        this.version = version;
    }

    /**
     * Get the database version as a comparable object
     * @return
     */
    public Version getVersion() {
        if (version == null) {
                return Version.DEFAULT_VERSION;
        }
        return this.version;
    }
    // END EXTENSION

	public Version getCompatibleVersion() {
		return getVersion();
	}

	public String getTranslatorName() {
		Translator translator = this.getClass().getAnnotation(Translator.class);
		return translator != null ? translator.name() : null;
	}
	
	public Integer getResolvedVersion() {
		Version version = getCompatibleVersion();
		Version dbVersion = getVersion();
		return version != null ? dbVersion != null && dbVersion.getMajorVersion() > version.getMajorVersion() ? dbVersion.getMajorVersion() : version.getMajorVersion() : null;
	}

	// EXTENSION
	// ISSUE: DVCORE-749
	// FIXED BY: Roman Pedchenko
	// DATE: 2013.11.20
	private boolean forceQuotedIdentifiers = true;
	@TranslatorProperty(display="Force Quoted Identifiers", description="Forces table columns to be quoted",advanced=true)
	public boolean forceQuotedIdentifiers() {
		return this.forceQuotedIdentifiers;
	}

	public void setForceQuotedIdentifiers(boolean forceQuotedIdentifiers) {
		this.forceQuotedIdentifiers = forceQuotedIdentifiers;
	}
	// END EXTENSION

    // DVCORE-7662
    // Whether 'forceQuotedIdentifiers' field should override JDBCMetadataProcessor.QuoteNameInSource field
    private boolean overrideQuotingOfNameInSource = true;

    public boolean isOverrideQuotingOfNameInSource() {
        return overrideQuotingOfNameInSource;
    }

    public void setOverrideQuotingOfNameInSource(boolean overrideQuotingOfNameInSource) {
        this.overrideQuotingOfNameInSource = overrideQuotingOfNameInSource;
    }

    // EXTENSION
	// ISSUE: DVCORE-2598
	// FIXED BY: Salvatore Raunich
	// DATE: 2015.01.14
	private boolean useDoubleSlashToEscapeRegex = false;
	@TranslatorProperty(display="Force double slashes to escape regex", description="Forces double slashes to escape chars in like_regex expressions",advanced=true)
	public boolean useDoubleSlashToEscapeRegex() {
		return this.useDoubleSlashToEscapeRegex;
	}

	public void setUseDoubleSlashToEscapeRegex(boolean useDoubleSlashToEscapeRegex) {
		this.useDoubleSlashToEscapeRegex = useDoubleSlashToEscapeRegex;
	}
	// END EXTENSION

	// EXTENSION
	// ISSUE: DVCORE-2252
	// FIXED BY: Salvatore Raunich
	// DATE: 2014.08.07
	private boolean clearStatementWarnings = true;
	@TranslatorProperty(display="Clear statement warnings", description="Clear the warnings from the statement",advanced=true)
	public boolean clearStatementWarnings() {
		return this.clearStatementWarnings;
	}

	public void setClearStatementWarnings(boolean clearStatementWarnings) {
		this.clearStatementWarnings = clearStatementWarnings;
	}
	// END EXTENSION

	@TranslatorProperty(display="Use Bind Variables", description="Use prepared statements and bind variables",advanced=true)
	public boolean useBindVariables() {
		return this.useBindVariables;
	}

	public void setUseBindVariables(boolean useBindVariables) {
		this.useBindVariables = useBindVariables;
	}

	@TranslatorProperty(display="Database time zone", description="Time zone of the database, if different than Integration Server", advanced=true)
	public String getDatabaseTimeZone() {
		return this.databaseTimeZone;
	}

	public void setDatabaseTimeZone(String databaseTimeZone) {
		this.databaseTimeZone = databaseTimeZone;
	}

	@TranslatorProperty(display="Trim string flag", description="Right Trim fixed character types returned as Strings - note that the native type must be char or nchar and the source must support the rtrim function.",advanced=true)
	public boolean isTrimStrings() {
		return this.trimStrings;
	}

	public void setTrimStrings(boolean trimStrings) {
		this.trimStrings = trimStrings;
	}

	@TranslatorProperty(display="Use informational comments in Source Queries", description="This will embed a /*comment*/ leading comment with session/request id in source SQL query for informational purposes", advanced=true)
	public boolean useCommentsInSourceQuery() {
		return this.useCommentsInSourceQuery;
	}

	public void setUseCommentsInSourceQuery(boolean useCommentsInSourceQuery) {
		this.useCommentsInSourceQuery = useCommentsInSourceQuery;
	}

	@Override
	public boolean isSourceRequired() {
		return true;
	}

	// EXTENSION
	// ISSUE: DV-99
	// FIXED BY: Salvatore Raunich
	// Applied solution from TEIID-2344
	// DATE: 2013.11.08
	@Override
	public boolean isSourceRequiredForCapabilities() {
		return isSourceRequired() && this.version == null && usesDatabaseVersion();
	}

	protected boolean usesDatabaseVersion() {
		return false;
	}

	@Override
	public void initCapabilities(Connection connection) throws TranslatorException {
		try {
			DatabaseMetaData metadata = connection.getMetaData();
			String fullVersion = metadata.getDatabaseProductVersion();
			setDatabaseVersion(fullVersion);
			LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Setting the database version to", fullVersion); //$NON-NLS-1$
		} catch (SQLException e) {
			throw new TranslatorException(e);
		}
	}

	// END EXTENSION

    @Override
    public ResultSetExecution createResultSetExecution(QueryExpression command, ExecutionContext executionContext, RuntimeMetadata metadata, Connection conn)
    		throws TranslatorException {
    	//TODO: This is not correct; this should be only called once for connection creation
    	obtainedConnection(conn);
    	return new JDBCQueryExecution(command, conn, executionContext, this);
    }

    @Override
    public ProcedureExecution createDirectExecution(List<Argument> arguments, Command command, ExecutionContext executionContext, RuntimeMetadata metadata, Connection conn)
    		throws TranslatorException {
    	//TODO: This is not correct; this should be only called once for connection creation
    	obtainedConnection(conn);
    	return new JDBCDirectQueryExecution(arguments, command, conn, executionContext, this);
    }

    @Override
    public ProcedureExecution createProcedureExecution(Call command, ExecutionContext executionContext, RuntimeMetadata metadata, Connection conn)
    		throws TranslatorException {
		//TODO: This is not correct; this should be only called once for connection creation
		obtainedConnection(conn);
		return new JDBCProcedureExecution(command, conn, executionContext, this);
    }

    @Override
    public UpdateExecution createUpdateExecution(Command command, ExecutionContext executionContext, RuntimeMetadata metadata, Connection conn)
    		throws TranslatorException {
		//TODO: This is not correct; this should be only called once for connection creation
		obtainedConnection(conn);
		return new JDBCUpdateExecution(command, conn, executionContext, this);
    }

    @Override
    public Connection getConnection(DataSource ds)
    		throws TranslatorException {
		return getConnection(ds, null, null);
    }

	public Connection getConnection(DataSource ds, String username, String password)
			throws TranslatorException {
		try {
			if (username != null && password != null)
				return ds.getConnection(username, password);
			else
				return ds.getConnection();
		} catch (SQLException e) {
			throw new TranslatorException(JDBCPlugin.Event.TEIID11009, e);
		}
	}

    @Override
    public void closeConnection(Connection connection, DataSource factory) {
    	if (connection == null) {
    		return;
    	}
    	try {
			connection.close();
		} catch (SQLException e) {
			LogManager.logDetail(LogConstants.CTX_CONNECTOR, e, "Error closing"); //$NON-NLS-1$
		}
    }

	// EXTENSION
	// ISSUE: DVCORE-532, DVCORE-702, DVCORE-731
	// FIXED BY: Roman Pedchenko, Salvatore Raunich
	// DATE: 2013.07.09, 2013.10.11, 2013.10.21
	public ITypeConverter getTypeConverter() {
		return new ITypeConverter() {
			@Override
			public String getRuntimeType(int type, String typeName, int precision, int scale) {
				if ("char".equalsIgnoreCase(typeName) && type == -8)
					return DataTypeManager.DefaultDataTypes.STRING;
				return null;
			}
		};
	}
	// END EXTENSION

    private String modelName;

	private String connectionName;

    private String sourceName;

    private String importerTableNamePattern;

    private boolean importerUseFullSchemaName = false;

    public boolean isImporterUseFullSchemaName() {
        return importerUseFullSchemaName;
    }

	public String getModelName() {
		return modelName;
    }

	public String getConnectionName() {
		return connectionName;
	}

	public String getSourceName() {
        return sourceName;
    }

    /**
     * Implementation of the method that sets importer.tableNamePattern property.
     * @param modelName a model name (name of datasource)
     * @param connectionName a connection name (could be different from model name)
     * @param properties properties where importer.tableNamePattern is transferred
     */
    @Override
    public void initModelProperties(String modelName, String connectionName, String sourceName, Properties properties) {
        this.modelName = modelName;
		this.connectionName = connectionName;
        this.sourceName = sourceName;
    	if (properties != null) {
            importerTableNamePattern = properties.getProperty("importer.tableNamePattern");
            importerUseFullSchemaName = Boolean.valueOf(properties.getProperty("importer.useFullSchemaName"));
        }
    }

    @Override
	public void getMetadata(MetadataFactory metadataFactory, Connection conn) throws TranslatorException {
		try {
			if (conn == null) {
				throw new TranslatorException(JDBCPlugin.Event.TEIID11018, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID11018));
			}
			JDBCMetadataProcessor metadataProcessor = createMetadataProcessor();
			PropertiesUtils.setBeanProperties(metadataProcessor, metadataFactory.getModelProperties(), "importer"); //$NON-NLS-1$

            // DVCORE-7662: explicitly set translator property 'ForceQuotedIdentifiers' should override the original 'QuoteNameInSource' data source parameter
            // by default both properties are TRUE
            if (!this.forceQuotedIdentifiers && this.overrideQuotingOfNameInSource) {
                metadataProcessor.setQuoteNameInSource(false);
            }

            // EXTENSION
			// ISSUE: DVCORE-532
			// FIXED BY: Roman Pedchenko
			// DATE: 2013.07.09
//			metadataProcessor.getConnectorMetadata(conn, metadataFactory);
            metadataProcessor.setColumnProcessor(getCustomColumnProcessor());
			metadataProcessor.getConnectorMetadata(conn, metadataFactory, getTypeConverter());
			// END EXTENSION
		} catch (SQLException e) {
			 throw new TranslatorException(JDBCPlugin.Event.TEIID11010, e);
		}
	}

    public JDBCMetadataProcessor.IColumnProcessor getCustomColumnProcessor() {
        return null;
    }

    public String getImporterTableNamePattern() {
        return importerTableNamePattern;
    }

    /**
	 * @deprecated
	 * @see getMetadataProcessor
	 */
 	@Deprecated
	protected JDBCMetadataProcessor createMetadataProcessor() {
	    return (JDBCMetadataProcessor)getMetadataProcessor();
	}

	@Override
 	public MetadataProcessor<Connection> getMetadataProcessor() {
		return new JDBCMetadataProcessor();
 	}

	@Override
    public List<String> getSupportedFunctions() {
        return getDefaultSupportedFunctions();
    }

	public List<String> getDefaultSupportedFunctions(){
		return Arrays.asList(new String[] { "+", "-", "*", "/" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
	}

    @Override
    public boolean supportsGroupBy() {
    	return true;
    }

    @Override
    public boolean supportsAggregatesAvg() {
        return true;
    }

    @Override
    public boolean supportsAggregatesCount() {
        return true;
    }

    @Override
    public boolean supportsAggregatesCountStar() {
        return true;
    }

    @Override
    public boolean supportsAggregatesDistinct() {
        return true;
    }

    @Override
    public boolean supportsAggregatesMax() {
        return true;
    }

    @Override
    public boolean supportsAggregatesMin() {
        return true;
    }

    @Override
    public boolean supportsAggregatesSum() {
        return true;
    }

    @Override
    public boolean supportsAliasedTable() {
        return true;
    }

    @Override
    public boolean supportsCompareCriteriaEquals() {
        return true;
    }

    @Override
    public boolean supportsCorrelatedSubqueries() {
        return true;
    }

    @Override
    public boolean supportsExistsCriteria() {
        return true;
    }

    @Override
    public boolean supportsInCriteria() {
        return true;
    }

    @Override
    public boolean supportsInCriteriaSubquery() {
        return true;
    }

    @Override
    public boolean supportsIsNullCriteria() {
        return true;
    }

    @Override
    public boolean supportsLikeCriteria() {
        return true;
    }

    @Override
    public boolean supportsLikeCriteriaEscapeCharacter() {
        return true;
    }

    @Override
    public boolean supportsNotCriteria() {
        return true;
    }

    @Override
    public boolean supportsOrCriteria() {
        return true;
    }

    @Override
    public boolean supportsOrderByUnrelated() {
    	return true;
    }

    @Override
    public boolean supportsQuantifiedCompareCriteriaAll() {
        return true;
    }

    @Override
    public boolean supportsScalarSubqueries() {
        return true;
    }

    @Override
    public boolean supportsSearchedCaseExpressions() {
        return true;
    }

    @Override
    public boolean supportsSelfJoins() {
        return true;
    }

    @Override
    public boolean supportsInlineViews() {
        return false;
    }

    @Override
    public boolean supportsQuantifiedCompareCriteriaSome() {
        return true;
    }

    @Override
    public boolean supportsSetQueryOrderBy() {
        return true;
    }

    @Override
    public boolean supportsUnions() {
        return true;
    }

    protected Boolean supportsConvertFromClobToString = null;
    @TranslatorProperty(display="Supports Convert/Cast function from Clob to String", description="True, if this connector supports Convert/Cast function from Clob to String", advanced=true)
    public boolean supportsConvertFromClobToString() {
    	if (supportsConvertFromClobToString == null || supportsConvertFromClobToString == false)
			return false;
		return true;
    }

    public void setSupportsConvertFromClobToString(boolean supportsConvertFromClobToString) {
		this.supportsConvertFromClobToString = supportsConvertFromClobToString;
	}

    @Override
	public boolean supportsConvert(int fromType, int toType) {
		if (supportsConvertFromClobToString() && fromType == RUNTIME_CODES.CLOB && toType == RUNTIME_CODES.STRING)
			return true;
		return super.supportsConvert(fromType, toType);
	}

    // EXTENSION
	// ISSUE: DV-125
	// FIXED BY: Roman Pedchenko
	// DATE: 2014.02.27
//    @Override
//    public boolean supportsBulkUpdate() {
//    	return true;
//    }
    private boolean supportsBulkUpdate = true;
    @TranslatorProperty(display="Supports Bulk Update", description="True, if this connector supports Bulk Update", advanced=true)
    @Override
    public boolean supportsBulkUpdate() {
    	return supportsBulkUpdate;
    }

    public void setSupportsBulkUpdate(boolean supportsBulkUpdate) {
		this.supportsBulkUpdate = supportsBulkUpdate;
	}
	// END EXTENSION

    @Override
    public boolean supportsBatchedUpdates() {
    	return true;
    }

    @Override
    public boolean supportsCompareCriteriaOrdered() {
    	return true;
    }

    @Override
    public boolean supportsHaving() {
    	return true;
    }

    @Override
    public boolean supportsSelectExpression() {
    	return true;
    }

    @Override
    public boolean supportsInsertWithQueryExpression() {
    	return true;
    }

    /**
     * Get the max number of inserts to perform in one batch.
     * @return
     */
    @TranslatorProperty(display="Max Prepared Insert Batch Size", description="The max size of a prepared insert batch.  Default 2048.", advanced=true)
    public int getMaxPreparedInsertBatchSize() {
    	return maxInsertBatchSize;
    }

    public void setMaxPreparedInsertBatchSize(int maxInsertBatchSize) {
    	if (maxInsertBatchSize < 1) {
    		throw new AssertionError(TranslatorJdbcPlugin.Util.gs(TranslatorJdbcPlugin.Event.DVCORE94096)); 
    	}
		this.maxInsertBatchSize = maxInsertBatchSize;
	}

    /**
     * Gets the database calendar.  This will be set to the time zone
     * specified by the property {@link JDBCPropertyNames#DATABASE_TIME_ZONE}, or
     * the local time zone if none is specified.
     * @return the database calendar
     */
    public Calendar getDatabaseCalendar() {
    	return this.databaseCalender.get();
    }

    /**
     * Return a List of translated parts ({@link LanguageObject}s and Objects), or null
     * if to rely on the default translation.  Override with care.
     * @param context
     * @param isUpdate TODO
     * @param command
     * @return list of translated parts
     */
    public List<?> translate(LanguageObject obj, ExecutionContext context, boolean isUpdate) {
		List<?> parts = null;

        List<?> parts1 = (List<?>) DVCaller.call(161, this, obj, context, isUpdate);
        if (parts1 != null)
            return parts1;

    	if (obj instanceof Function) {
    		Function function = (Function)obj;
    		if (functionModifiers != null) {
			    String name = function.getName();
				if (function.getMetadataObject() != null && function.getMetadataObject().getModifier() != null && !functionModifiers.containsKey(name)) {
					try {
						DVCaller.callWithExc(243, function, functionModifiers);
				    } catch (Throwable e) {
					    LogManager.logError(LogConstants.CTX_CONNECTOR, e); //$NON-NLS-1$
				    }
			    }
    			while (true) {
	    			FunctionModifier modifier = functionModifiers.get(name);
	    			if (modifier != null) {
	    				parts = modifier.translate(function);
	    				break;
	    			}
	    			int index = name.indexOf('.');
	    			if (index < 0 || index == name.length() - 1) {
	    				break;
	    			}
    				name = name.substring(index + 1);
    			}
    		}
    	} else if (obj instanceof Command) {
    		parts = translateCommand((Command)obj, context);
    	} else if (obj instanceof Limit) {
    		parts = translateLimit((Limit)obj, context);
    	} else if (obj instanceof ColumnReference) {
    		ColumnReference elem = (ColumnReference)obj;
			if (isTrimStrings() && elem.getType() == TypeFacility.RUNTIME_TYPES.STRING && elem.getMetadataObject() != null
					// EXTENSION
					// ISSUE: DVCORE-490
					// FIXED BY: Roman Pedchenko
					// DATE: 2013.06.20
//					&& ("char".equalsIgnoreCase(elem.getMetadataObject().getNativeType()) || "nchar".equalsIgnoreCase(elem.getMetadataObject().getNativeType()))) { //$NON-NLS-1$ //$NON-NLS-2$
					&& ("char".equalsIgnoreCase(elem.getMetadataObject().getNativeType()) || "bpchar".equalsIgnoreCase(elem.getMetadataObject().getNativeType()) || "nchar".equalsIgnoreCase(elem.getMetadataObject().getNativeType()))) { //$NON-NLS-1$ //$NON-NLS-2$
					// END EXTENSION
				return Arrays.asList(getLanguageFactory().createFunction(SourceSystemFunctions.RTRIM, new Expression[] {elem}, TypeFacility.RUNTIME_TYPES.STRING));
			}
	    } else if (obj instanceof DerivedColumn) {
		    DerivedColumn dc = (DerivedColumn) obj;
		    if (dc.isProjected()) {
			    Expression expr = dc.getExpression();
			    if (expr.getType() == TypeFacility.RUNTIME_TYPES.GEOMETRY) {
				    dc.setExpression(translateGeometrySelect(expr));
			    } else if (expr.getType() == TypeFacility.RUNTIME_TYPES.GEOGRAPHY) {
				    dc.setExpression(translateGeographySelect(expr));
    	}
		    }
	    } else if (obj instanceof Literal) {
		    Literal l = (Literal) obj;
		    if (l.getType() == TypeFacility.RUNTIME_TYPES.GEOMETRY && l.getValue() != null) {
			    return translateGeometryLiteral(l);
		    }
		    if (l.getType() == TypeFacility.RUNTIME_TYPES.GEOGRAPHY && l.getValue() != null) {
			    return translateGeographyLiteral(l);
	    }
	    } else if (obj instanceof Parameter) {
		    Parameter p = (Parameter) obj;
		    if (p.getType() == TypeFacility.RUNTIME_TYPES.GEOMETRY) {
			    return translateGeometryParameter(p);
		    }
		    if (p.getType() == TypeFacility.RUNTIME_TYPES.GEOGRAPHY) {
			    return translateGeographyParameter(p);
		    }
	    }
    	if (!supportsBooleanExpressions() && obj instanceof Condition) {
            Condition c = (Condition)obj;
            if (c.isExpression()) {
                if (c instanceof IsNull) {
                    return Arrays.asList(new SearchedCase(Arrays.asList(
                            new SearchedWhenClause(c, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER))),
                            new Literal(0, TypeFacility.RUNTIME_TYPES.INTEGER), TypeFacility.RUNTIME_TYPES.BOOLEAN));
                }
                return Arrays.asList(new SearchedCase(Arrays.asList(
                        new SearchedWhenClause(c, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)),
                        new SearchedWhenClause(new Not(c), new Literal(0, TypeFacility.RUNTIME_TYPES.INTEGER))), null, TypeFacility.RUNTIME_TYPES.BOOLEAN));
            }
        }
    	return parts;
    }

    /**
	 * Translate GEOMETRY column reference into an expression that
	 * will return WKB and possibly the SRID.
	 *
	 * @param expr
	 * @return
	 */
	public Expression translateGeometrySelect(Expression expr) {
		return new Function(SourceSystemFunctions.ST_ASBINARY, Arrays.asList(expr), TypeFacility.RUNTIME_TYPES.BLOB);
	}

	/**
	 * Translate GEOGRAPHY column reference into an expression that
	 * will return WKB and possibly the SRID.
	 *
	 * @param expr
	 * @return
	 */
	public Expression translateGeographySelect(Expression expr) {
		return translateGeometrySelect(expr);
	}

	/**
	 * Translate GEOMETRY literal into an expression that will convert to database
	 * geometry type.
	 *
	 * @param l
	 * @return
	 */
	public List<?> translateGeometryLiteral(Literal l) {
		Literal srid = getLanguageFactory().createLiteral(
				((AbstractGeospatialType) l.getValue()).getSrid(),
				Integer.class
		);
		return createGeometryExpression(l, srid);
	}

	/**
	 * Translate GEOGRAPHY literal into an expression that will convert to database
	 * geography type.
	 *
	 * @param l
	 * @return
	 */
	public List<?> translateGeographyLiteral(Literal l) {
		Function geog = getLanguageFactory().createFunction(
				SourceSystemFunctions.ST_GEOGFROMWKB, new Expression[] { l },
				TypeFacility.RUNTIME_TYPES.GEOGRAPHY);
		int sridVal = ((AbstractGeospatialType) l.getValue()).getSrid();
		if (sridVal == GeographyType.DEFAULT_SRID) {
			return Arrays.asList(geog);
		}
		Literal srid = getLanguageFactory().createLiteral(
				sridVal,
				Integer.class);
		return createGeographyExpression(geog, srid);
	}

	/**
	 * Translate GEOMETRY parameter into an expression that will convert to database
	 * geometry type.
	 *
	 * @param p
	 * @return
	 */
	public List<?> translateGeometryParameter(Parameter p) {
		Parameter srid = new Parameter();
		srid.setType(TypeFacility.RUNTIME_TYPES.INTEGER);
		srid.setValueIndex(p.getValueIndex());

		return createGeometryExpression(p, srid);
	}

	private List<?> createGeometryExpression(Expression geom, Expression srid) {
		return Arrays.asList(getLanguageFactory().createFunction(
				SourceSystemFunctions.ST_GEOMFROMWKB,
				new Expression[] { geom, srid },
				TypeFacility.RUNTIME_TYPES.GEOMETRY)
		);
	}

	/**
	 * Translate GEOGRAPHY parameter into an expression that will convert to database
	 * geography type.
	 *
	 * @param p
	 * @return
	 */
	public List<?> translateGeographyParameter(Parameter p) {
		Parameter srid = new Parameter();
		srid.setType(TypeFacility.RUNTIME_TYPES.INTEGER);
		srid.setValueIndex(p.getValueIndex());
		return createGeographyExpression(p, srid);
	}

	private List<?> createGeographyExpression(Expression geogVal, Expression srid) {
		Function geog = getLanguageFactory().createFunction(
				SourceSystemFunctions.ST_GEOGFROMWKB,
				new Expression[] { geogVal },
				TypeFacility.RUNTIME_TYPES.GEOGRAPHY);
		return Arrays.asList(getLanguageFactory().createFunction(
				SourceSystemFunctions.ST_SETSRID,
				new Expression[] { geog, srid },
				TypeFacility.RUNTIME_TYPES.GEOGRAPHY));
	}

	/**
	 * The default strategy assumes a blob value containing wkb
	 * @param results
	 * @param paramIndex
	 * @return
	 * @throws SQLException
	 */
	public Object retrieveGeometryValue(ResultSet results, int paramIndex) throws SQLException {
		GeometryType geom = null;
		Blob val = results.getBlob(paramIndex);
		if (val != null) {
			geom = new GeometryType(val);
		}
		return geom;
	}

	/**
	 * The default strategy assumes a blob value containing wkb
	 * @param results
	 * @param paramIndex
	 * @return
	 * @throws SQLException
	 */
	public Object retrieveGeographyValue(ResultSet results, int paramIndex) throws SQLException {
		GeographyType geog = null;
		Blob val = results.getBlob(paramIndex);
		if (val != null) {
			geog = new GeographyType(val);
		}
		return geog;
	}

	public GeometryType retrieveGeometryValue(CallableStatement results, int parameterIndex) throws SQLException {
		throw new SQLException(JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID11022));
	}

	public GeographyType retrieveGeographyValue(CallableStatement results, int parameterIndex) throws SQLException {
		throw new SQLException(JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID11022));
	}

    /**
     * if boolean expressions are directly supported.
     * will generally be false if there is no boolean datatype
     * @return
     */
    protected boolean supportsBooleanExpressions() {
        return true;
    }

    /**
     * Return a List of translated parts ({@link LanguageObject}s and Objects), or null
     * if to rely on the default translation.
     * @param command
     * @param context
     * @return a list of translated parts
     */
    public List<?> translateCommand(Command command, ExecutionContext context) {
    	return null;
    }

    /**
     * Return a List of translated parts ({@link LanguageObject}s and Objects), or null
     * if to rely on the default translation.
     * @param limit
     * @param context
     * @return a list of translated parts
     */
    public List<?> translateLimit(Limit limit, ExecutionContext context) {
    	return null;
    }

    /**
     * Return a map of function name to FunctionModifier.
     * @return Map of function name to FunctionModifier.
     */
    public Map<String, FunctionModifier> getFunctionModifiers() {
    	return functionModifiers;
    }

    /**
     * Add the {@link FunctionModifier} to the set of known modifiers.
     * @param name
     * @param modifier
     */
    public void registerFunctionModifier(String name, FunctionModifier modifier) {
    	this.functionModifiers.put(name, modifier);
    }

    /**
     * Subclasses should override this method to provide a different sql translation
     * of the literal boolean value.  By default, a boolean literal is represented as:
     * <code>'0'</code> or <code>'1'</code>.
     * @param booleanValue Boolean value, never null
     * @return Translated string
     */
    public String translateLiteralBoolean(Boolean booleanValue) {
        if(booleanValue.booleanValue()) {
            return "1"; //$NON-NLS-1$
        }
        return "0"; //$NON-NLS-1$
    }

    /**
     * Subclasses should override this method to provide a different sql translation
     * of the literal date value.  By default, a date literal is represented as:
     * <code>{d '2002-12-31'}</code>
     * @param dateValue Date value, never null
     * @return Translated string
     */
    public String translateLiteralDate(java.sql.Date dateValue) {
        return "{d '" + formatDateValue(dateValue) + "'}"; //$NON-NLS-1$ //$NON-NLS-2$
    }

    /**
     * Subclasses should override this method to provide a different sql translation
     * of the literal time value.  By default, a time literal is represented as:
     * <code>{t '23:59:59'}</code>
     *
     * See {@link JDBCExecutionFactory#hasTimeType()} to represent literal times as timestamps.
     *
     * @param timeValue Time value, never null
     * @return Translated string
     */
    public String translateLiteralTime(Time timeValue) {
    	if (!hasTimeType()) {
    		return translateLiteralTimestamp(new Timestamp(timeValue.getTime()));
    	}
        return "{t '" + formatDateValue(timeValue) + "'}"; //$NON-NLS-1$ //$NON-NLS-2$
    }

    /**
     * Subclasses should override this method to provide a different sql translation
     * of the literal timestamp value.  By default, a timestamp literal is
     * represented as: <code>{ts '2002-12-31 23:59:59'}</code>.
     *
     * See {@link JDBCExecutionFactory#getTimestampNanoPrecision()} to control the literal
     * precision.
     *
     * @param timestampValue Timestamp value, never null
     * @return Translated string
     */
    public String translateLiteralTimestamp(Timestamp timestampValue) {
        return "{ts '" + formatDateValue(timestampValue) + "'}"; //$NON-NLS-1$ //$NON-NLS-2$
    }

    /**
     * Format the dateObject (of type date, time, or timestamp) into a string
     * using the DatabaseTimeZone format.
     * @param dateObject
     * @return Formatted string
     */
    public String formatDateValue(java.util.Date dateObject) {
        if (dateObject instanceof Timestamp && getTimestampNanoPrecision() < 9) {
        	Timestamp ts = (Timestamp)dateObject;
        	Timestamp newTs = new Timestamp(ts.getTime());
        	if (getTimestampNanoPrecision() > 0) {
	        	int mask = (int)Math.pow(10, 9-getTimestampNanoPrecision());
	        	newTs.setNanos(ts.getNanos()/mask*mask);
        	} else {
        		newTs.setNanos(0);
        	}
        	dateObject = newTs;
        }
        return getTypeFacility().convertDate(dateObject, DEFAULT_TIME_ZONE, getDatabaseCalendar(), dateObject.getClass()).toString();
    }

	/**
	 * Subclasses should override this method to provide a different sql translation
	 * of the literal varbinary value.  By default, a varbinary literal is represented as:
	 * <code>X'hex value'</code>
	 * @param obj value, never null
	 * @return Translated string
	 */
	public String translateLiteralBinaryType(BinaryType obj) {
		return "X'" + obj + "'"; //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
     * Returns true to indicate that SQL should include a comment
     * indicating the session and request ids.
     */
    public boolean addSourceComment() {
        return useCommentsInSourceQuery();
    }

    /**
     * Indicates whether group alias should be of the form
     * "...FROM groupA AS X" or "...FROM groupA X".  Certain
     * data sources (such as Oracle) may not support the first
     * form.
     * @return boolean
     */
    public boolean useAsInGroupAlias(){
        return true;
    }

    /**
     * Use PreparedStatements (or CallableStatements) as
     * appropriate for all commands.  Bind values will be
     * determined by the {@link BindValueVisitor}.  {@link Literal#setBindValue(boolean)}
     * can be used to force a literal to be a bind value.
     */
    public boolean usePreparedStatements() {
    	return useBindVariables();
    }

    /**
     * Set to true to indicate that every branch of a set query
     * should have parenthesis, i.e. (query) union (query)
     * @return true if parenthesis should be used for each set branch
     */
    public boolean useParensForSetQueries() {
    	return false;
    }

    /**
     * Return false to indicate that time support should be emulated
     * with timestamps.
     * @return true if database has a time type
     */
    public boolean hasTimeType() {
    	return true;
    }

    /**
     * Returns the name for a given {@link Operation}
     * @param operation
     * @return the name for the set operation
     */
    public String getSetOperationString(SetQuery.Operation operation) {
    	return operation.toString();
    }

    /**
     * Returns the source comment for the given command
     * @param context
     * @param command
     * @return the comment
     */
    public String getSourceComment(ExecutionContext context, Command command) {
	    if (addSourceComment() && context != null) {
            return COMMENT.get().format(new Object[] {context.getConnectionId(), context.getRequestId(), context.getPartIdentifier()});
	    }
	    return ""; //$NON-NLS-1$
    }

    /**
     * Override to return a name other than the default [group.]element
     * @param group
     * @param element
     * @return thre replacement name
     */
    public String replaceElementName(String group, String element) {
    	return null;
    }

    /**
     * Return the precision of timestamp literals.  Defaults to 9.
     * @return digits of timestamp nano precision.
     */
    public int getTimestampNanoPrecision() {
    	return 9;
    }

    /**
     * This is a generic implementation. Because different databases handle
     * stored procedures differently, subclasses should override this method
     * if necessary.
     */
    public ResultSet executeStoredProcedure(CallableStatement statement, List<Argument> preparedValues, Class<?> returnType) throws SQLException {
		int index = 1;

		if(returnType != null){
			registerSpecificTypeOfOutParameter(statement, returnType, index++);
		}

		Iterator<?> iter = preparedValues.iterator();
		while(iter.hasNext()){
			Argument param = (Argument)iter.next();

			if(param.getDirection() == Direction.INOUT){
				registerSpecificTypeOfOutParameter(statement,param.getType(), index);
			}else if(param.getDirection() == Direction.OUT){
				registerSpecificTypeOfOutParameter(statement,param.getType(), index++);
			}

			if(param.getDirection() == Direction.IN || param.getDirection() == Direction.INOUT){
				bindValue(statement, param.getArgumentValue().getValue(), param.getType(), index++);
			}
		}
		boolean resultSetNext = statement.execute();

		while (!resultSetNext) {
			int update_count = statement.getUpdateCount();
			if (update_count == -1) {
				break;
			}
			resultSetNext = statement.getMoreResults();
		}
		return statement.getResultSet();
    }

    /**
     * For registering specific output parameter types we need to translate these into the appropriate
     * java.sql.Types output parameters
     * We will need to match these up with the appropriate standard sql types
     * @param cstmt
     * @param parameter
     * @throws SQLException
     */
    protected void registerSpecificTypeOfOutParameter(CallableStatement statement, Class<?> runtimeType, int index) throws SQLException {
        int typeToSet = TypeFacility.getSQLTypeFromRuntimeType(runtimeType);

        statement.registerOutParameter(index,typeToSet);
    }

    /**
     * Sets prepared statement parameter i with param.
     *
     * Performs special handling to translate dates using the database time zone and to
     * translate biginteger, float, and char to JDBC safe objects.
     *
     * @param stmt
     * @param param
     * @param paramType
     * @param i
     * @param cal
     * @throws SQLException
     */
    public void bindValue(PreparedStatement stmt, Object param, Class<?> paramType, int i) throws SQLException {
        int type = TypeFacility.getSQLTypeFromRuntimeType(paramType);

        if (param == null) {
        	if (type == Types.JAVA_OBJECT) {
        		stmt.setNull(i, Types.OTHER);
        	} else {
        		stmt.setNull(i, type);
        	}
            return;
        }
        //if this is a Date object, then use the database calendar
        if (paramType.equals(TypeFacility.RUNTIME_TYPES.DATE)) {
            stmt.setDate(i,(java.sql.Date)param, getDatabaseCalendar());
            return;
        }
        if (paramType.equals(TypeFacility.RUNTIME_TYPES.TIME)) {
            stmt.setTime(i,(java.sql.Time)param, getDatabaseCalendar());
            return;
        }
        if (paramType.equals(TypeFacility.RUNTIME_TYPES.TIMESTAMP)) {
            stmt.setTimestamp(i,(java.sql.Timestamp)param, getDatabaseCalendar());
            return;
        }
        //not all drivers handle the setObject call with BigDecimal correctly (namely jConnect 6.05)
        if (TypeFacility.RUNTIME_TYPES.BIG_DECIMAL.equals(paramType)) {
        	stmt.setBigDecimal(i, (BigDecimal)param);
            return;
        }

	    //special handling for the srid parameter
	    if (paramType == TypeFacility.RUNTIME_TYPES.INTEGER && param instanceof AbstractGeospatialType) {
		    stmt.setInt(i, ((AbstractGeospatialType)param).getSrid());
		    return;
	    }

        if (param != null && (paramType.equals(TypeFacility.RUNTIME_TYPES.STRING) || paramType.equals(TypeFacility.RUNTIME_TYPES.CHAR))) {
        	if (param instanceof String) {
                param = stringCutConvertFail((String) param);
            }
        }

        if (useStreamsForLobs()) {
        	if (param instanceof Blob) {
        		Blob blob = (Blob)param;
        		long length = blob.length();
        		if (length <= Integer.MAX_VALUE) {
        		    stmt.setBinaryStream(i, blob.getBinaryStream(), (int)length);
        		} else {
        		    stmt.setBinaryStream(i, blob.getBinaryStream(), length);
        		}
        		return;
        	}
        	if (param instanceof Clob) {
        		Clob clob = (Clob)param;
        		long length = clob.length();
        		if (length <= Integer.MAX_VALUE) {
        		    stmt.setCharacterStream(i, clob.getCharacterStream(), (int)clob.length());
        		} else {
        		    stmt.setCharacterStream(i, clob.getCharacterStream(), length);
        		}
        		return;
        	}
        }
        //convert these the following to jdbc safe values
        if (TypeFacility.RUNTIME_TYPES.BIG_INTEGER.equals(paramType)) {
            param = new BigDecimal((BigInteger)param);
        } else if (TypeFacility.RUNTIME_TYPES.FLOAT.equals(paramType)) {
            param = new Double(((Float)param).doubleValue());
        } else if (TypeFacility.RUNTIME_TYPES.CHAR.equals(paramType) && param instanceof Character) {
            param = ((Character)param).toString();
        } else if (paramType.equals(TypeFacility.RUNTIME_TYPES.VARBINARY)) {
        	param = ((BinaryType)param).getBytesDirect();
        }
        if (TypeFacility.RUNTIME_TYPES.DOUBLE.equals(param.getClass())) {
            param = pretranslateDouble(param);
        }

        ArrayEmulation arrEmu = supportsArrayEmulation();
		if (arrEmu != ArrayEmulation.NONE) {
        	if (param instanceof ArrayImpl) {
        		try {
        			ByteArrayOutputStream baos = new ByteArrayOutputStream();
        			ObjectOutputStream out = null;
        			try {
        				out = new ObjectOutputStream(baos);
        				((ArrayImpl) param).writeExternal(out);
        				out.flush();
        				byte[] bytes = baos.toByteArray();
            			if (arrEmu == ArrayEmulation.BINARY)
            				try {
            					stmt.setBinaryStream(i, BlobType.createBlob(bytes).getBinaryStream());
            				} catch (Exception e) {
            					stmt.setBytes(i, bytes);
            				}
            			else if (arrEmu == ArrayEmulation.STRING)
            				stmt.setString(i, Base64.encodeBytes(bytes));
        			} finally {
        				if (out != null)
        					out.close();
        			}
					return;
        		} catch (IOException e) {
        			throw new SQLException(e);
        		}
        	}
        }
        if (type != Types.JAVA_OBJECT) {
            if (useUnicodePrefix()) {
                //sources that require the prefix, also require binding as an N type
                if (type == Types.VARCHAR) {
                    if (SQLConversionVisitor.isNonAscii(param.toString())) {
                        type = Types.NVARCHAR;
                    }
                } else if (type == Types.CLOB) {
                    //don't try to read the clob as it could be streaming
                    type = Types.NCLOB;
                }
            }
        	stmt.setObject(i, param, type);
        } else {
        	stmt.setObject(i, param);
        }
    }

    /**
     * @return true if the N prefix should be used for strings containing non-ascii characters
     */
    public boolean useUnicodePrefix() {
        return false;
    }

    protected Object pretranslateDouble(Object param) {
        Double d = (Double) param;
        if (d.isNaN())
            param = null;
        else if (Double.POSITIVE_INFINITY == d)
            param = Double.MAX_VALUE;
        else if (Double.NEGATIVE_INFINITY == d)
            param = DOUBLE_MIN;
        return param;
    }

    protected Object pretranslateFloat(Object param) {
        Float f = (Float) param;
        if (f.isNaN())
            param = null;
        if (Float.POSITIVE_INFINITY == f)
            param = Float.MAX_VALUE;
        else if (Float.NEGATIVE_INFINITY == f)
            param = FLOAT_MIN;
        return param;
    }

    public String valueToString(ColumnReference col, Object value, String quote) throws TranslatorException {
        if (value instanceof Double) {
            value = pretranslateDouble(value);
        }
        if (value instanceof Float) {
            value = pretranslateFloat(value);
        }
        return value != null ? value.toString() : null;
    }

    /**
     * If streams should be used for Blob/Clob sets on {@link PreparedStatement}s
     * @return
     */
    public boolean useStreamsForLobs() {
    	return false;
    }

	/**
	 * Retrieve the value on the current resultset row for the given column index.
	 * @param results
	 * @param columnIndex
	 * @param expectedType
	 * @return the value
	 * @throws SQLException
	 */
    public Object retrieveValue(ResultSet results, int columnIndex, Class<?> expectedType) throws SQLException {
		ArrayEmulation arrEmu = supportsArrayEmulation();
		if (arrEmu != ArrayEmulation.NONE) {
        	if (expectedType.isArray()) {
        		try {
        			InputStream is = null;
        			if (arrEmu == ArrayEmulation.BINARY) {
        				try {
                            final Blob blob = results.getBlob(columnIndex);
                            if (blob == null)
                                return null;
                            is = blob.getBinaryStream();
        				} catch (SQLException e) {
        					// ignore
        				}
        				try {
                            final byte[] bytes = results.getBytes(columnIndex);
                            if (bytes == null)
                                return null;
                            is = new ByteArrayInputStream(bytes);
        				} catch (SQLException e) {
        					// ignore
        				}
        			} else if (arrEmu == ArrayEmulation.STRING) {
                        final String string = results.getString(columnIndex);
                        if (string == null)
                            return null;
                        is = new ByteArrayInputStream(Base64.decode(string));
        			}
        			if (is != null) {
                        try (ObjectInputStream in = new ObjectInputStream(is)) {
                            ArrayImpl arr = new ArrayImpl();
                            arr.readExternal(in);
                            return arr;
                        }
        			}
				} catch (ClassNotFoundException | IOException e) {
        			throw new SQLException(e);
        		}
            }
        }

        Integer code = DataTypeManager.getTypeCode(expectedType);
        if(code != null) {
            // Calling the specific methods here is more likely to get uniform (and fast) results from different
            // data sources as the driver likely knows the best and fastest way to convert from the underlying
            // raw form of the data to the expected type.  We use a switch with codes in order without gaps
            // as there is a special bytecode instruction that treats this case as a map such that not every value 
            // needs to be tested, which means it is very fast.
            switch(code.intValue()) {
                case DataTypeManager.DefaultTypeCodes.INTEGER:  {
                    int value = results.getInt(columnIndex);
                    if(results.wasNull()) {
                        return null;
                    }
                    return Integer.valueOf(value);
                }
                case DataTypeManager.DefaultTypeCodes.LONG:  {
                    long value = results.getLong(columnIndex);
                    if(results.wasNull()) {
                        return null;
                    }
                    return Long.valueOf(value);
                }
                case DataTypeManager.DefaultTypeCodes.DOUBLE:  {
                    double value = results.getDouble(columnIndex);
                    if(results.wasNull()) {
                        return null;
                    }
                    return Double.valueOf(value);
                }
                case DataTypeManager.DefaultTypeCodes.BIGDECIMAL:  {
	                return results.getBigDecimal(columnIndex);
                }
                case DataTypeManager.DefaultTypeCodes.SHORT:  {
                    short value = results.getShort(columnIndex);
                    if(results.wasNull()) {
                        return null;
                    }
                    return Short.valueOf(value);
                }
                case DataTypeManager.DefaultTypeCodes.FLOAT:  {
                    float value = results.getFloat(columnIndex);
                    if(results.wasNull()) {
                        return null;
                    }
                    return Float.valueOf(value);
                }
                case DataTypeManager.DefaultTypeCodes.TIME: {
            		return results.getTime(columnIndex, getDatabaseCalendar());
                }
                case DataTypeManager.DefaultTypeCodes.DATE: {
            		return results.getDate(columnIndex, getDatabaseCalendar());
                }
                case DataTypeManager.DefaultTypeCodes.TIMESTAMP: {
            		return results.getTimestamp(columnIndex, getDatabaseCalendar());
                }
    			case DataTypeManager.DefaultTypeCodes.BLOB: {
    				try {
    					return results.getBlob(columnIndex);
    				} catch (SQLException e) {
    					// ignore
    				}
    				try {
    					return results.getBytes(columnIndex);
    				} catch (SQLException e) {
    					// ignore
    				}
    				break;
    			}
	            case DataTypeManager.DefaultTypeCodes.GEOGRAPHY: {
		            return retrieveGeographyValue(results, columnIndex);
	            }
	            case DataTypeManager.DefaultTypeCodes.GEOMETRY: {
		            return retrieveGeometryValue(results, columnIndex);
			            }
	            case DataTypeManager.DefaultTypeCodes.JSON:
		            try {
			            return new JsonType(results.getClob(columnIndex));
		            } catch (SQLException e) {
			            // ignore
		            }
		            break;
	            case DataTypeManager.DefaultTypeCodes.CLOB: {
    				try {
    					return results.getClob(columnIndex);
    				} catch (SQLException e) {
    					// ignore
    				}
    				break;
    			}
    			case DataTypeManager.DefaultTypeCodes.BOOLEAN: {
    				boolean result = results.getBoolean(columnIndex);
    				if(results.wasNull()) {
    					return null;
    				}
    				return result;
    			}
	            case DataTypeManager.DefaultTypeCodes.BIGINTEGER: {
                    BigDecimal bd = results.getBigDecimal(columnIndex);
                    if(results.wasNull() || bd == null) {
                        return null;
                    }
                    return bd.toBigInteger();
                }
            }
        }

        return retrieveObjectValue(results, columnIndex, expectedType);
    }

    public Object retrieveObjectValue(ResultSet results, int columnIndex, Class<?> expectedType) throws SQLException {
        return results.getObject(columnIndex);
    }

    /**
     * Retrieve the value for the given parameter index
     * @param results
     * @param parameterIndex
     * @param expectedType
     * @return the value
     * @throws SQLException
     */
    public Object retrieveValue(CallableStatement results, int parameterIndex, Class<?> expectedType) throws SQLException{
		ArrayEmulation arrEmu = supportsArrayEmulation();
		if (arrEmu != ArrayEmulation.NONE) {
        	if (expectedType.isArray()) {
        		try {
        			InputStream is = null;
        			if (arrEmu == ArrayEmulation.BINARY) {
        				try {
        					is = results.getBlob(parameterIndex).getBinaryStream();
        				} catch (SQLException e) {
        					// ignore
        				}
        				try {
        					is = new ByteArrayInputStream(results.getBytes(parameterIndex));
        				} catch (SQLException e) {
        					// ignore
        				}
        			} else if (arrEmu == ArrayEmulation.STRING) {
    					is = new ByteArrayInputStream(Base64.decode(results.getString(parameterIndex)));
        			}
        			if (is != null) {
                        try (ObjectInputStream in = new ObjectInputStream(is)) {
                            ArrayImpl arr = new ArrayImpl();
                            arr.readExternal(in);
                            return arr;
                        }
        			}
				} catch (ClassNotFoundException e) {
        			throw new SQLException(e);
        		} catch (IOException e) {
        			throw new SQLException(e);
        		}
        	}
        }

		Integer code = DataTypeManager.getTypeCode(expectedType);
        if(code != null) {
            switch(code.intValue()) {
                case DataTypeManager.DefaultTypeCodes.INTEGER:  {
                    int value = results.getInt(parameterIndex);
                    if(results.wasNull()) {
                        return null;
                    }
                    return Integer.valueOf(value);
                }
                case DataTypeManager.DefaultTypeCodes.LONG:  {
                    long value = results.getLong(parameterIndex);
                    if(results.wasNull()) {
                        return null;
                    }
                    return Long.valueOf(value);
                }
                case DataTypeManager.DefaultTypeCodes.DOUBLE:  {
                    double value = results.getDouble(parameterIndex);
                    if(results.wasNull()) {
                        return null;
                    }
                    return new Double(value);
                }
                case DataTypeManager.DefaultTypeCodes.BIGDECIMAL:  {
                    return results.getBigDecimal(parameterIndex);
                }
                case DataTypeManager.DefaultTypeCodes.SHORT:  {
                    short value = results.getShort(parameterIndex);
                    if(results.wasNull()) {
                        return null;
                    }
                    return Short.valueOf(value);
                }
                case DataTypeManager.DefaultTypeCodes.FLOAT:  {
                    float value = results.getFloat(parameterIndex);
                    if(results.wasNull()) {
                        return null;
                    }
                    return new Float(value);
                }
                case DataTypeManager.DefaultTypeCodes.TIME: {
            		return results.getTime(parameterIndex, getDatabaseCalendar());
                }
                case DataTypeManager.DefaultTypeCodes.DATE: {
            		return results.getDate(parameterIndex, getDatabaseCalendar());
                }
                case DataTypeManager.DefaultTypeCodes.TIMESTAMP: {
            		return results.getTimestamp(parameterIndex, getDatabaseCalendar());
                }
    			case DataTypeManager.DefaultTypeCodes.BLOB: {
    				try {
    					return results.getBlob(parameterIndex);
    				} catch (SQLException e) {
    					// ignore
    				}
    				try {
    					return results.getBytes(parameterIndex);
    				} catch (SQLException e) {
    					// ignore
    				}
    			}
	            case DataTypeManager.DefaultTypeCodes.GEOGRAPHY: {
		            return retrieveGeographyValue(results, parameterIndex);
	            }
	            case DataTypeManager.DefaultTypeCodes.GEOMETRY: {
		            return retrieveGeometryValue(results, parameterIndex);
			            }
	            case DataTypeManager.DefaultTypeCodes.JSON:
		            try {
			            new JsonType(results.getClob(parameterIndex));
		            } catch (SQLException e) {
			            // ignore
		            }
	            case DataTypeManager.DefaultTypeCodes.CLOB: {
    				try {
    					return results.getClob(parameterIndex);
    				} catch (SQLException e) {
    					// ignore
    				}
    			}
    			case DataTypeManager.DefaultTypeCodes.BOOLEAN: {
    				boolean result = results.getBoolean(parameterIndex);
    				if(results.wasNull()) {
    					return null;
    				}
    				return result;
    			}
            }
        }

        // otherwise fall through and call getObject() and rely on the normal
		// translation routines
        return retrieveObjectValue(results, parameterIndex, expectedType);
    }

    public Object retrieveObjectValue(CallableStatement results, int parameterIndex, Class<?> expectedType) throws SQLException {
        return results.getObject(parameterIndex);
    }

    /**
     * Called exactly once for this source.
     * @param connection
     */
    protected void afterInitialConnectionObtained(Connection connection) {
        // now dig some details about this driver/database for log.
        try {
            StringBuffer sb = new StringBuffer(getClass().getSimpleName());
            DatabaseMetaData dbmd = connection.getMetaData();
            sb.append(" Commit=").append(connection.getAutoCommit()); //$NON-NLS-1$
            sb.append(";DatabaseProductName=").append(dbmd.getDatabaseProductName()); //$NON-NLS-1$
            sb.append(";DatabaseProductVersion=").append(dbmd.getDatabaseProductVersion()); //$NON-NLS-1$
            sb.append(";DriverMajorVersion=").append(dbmd.getDriverMajorVersion()); //$NON-NLS-1$
            sb.append(";DriverMajorVersion=").append(dbmd.getDriverMinorVersion()); //$NON-NLS-1$
            sb.append(";DriverName=").append(dbmd.getDriverName()); //$NON-NLS-1$
            sb.append(";DriverVersion=").append(dbmd.getDriverVersion()); //$NON-NLS-1$
            sb.append(";IsolationLevel=").append(dbmd.getDefaultTransactionIsolation()); //$NON-NLS-1$
            supportsGeneratedKeys = dbmd.supportsGetGeneratedKeys();
            LogManager.logInfo(LogConstants.CTX_CONNECTOR, sb.toString());
        } catch (SQLException e) {
            LogManager.logInfo(LogConstants.CTX_CONNECTOR, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID11002));
        }
    }

    /**
     * Provides a hook to call source specific logic when
     * a connection is obtained.
     *
     * defect request 13979 & 13978
     */
    public void obtainedConnection(Connection connection) {
        if (initialConnection.compareAndSet(true, false)) {
            afterInitialConnectionObtained(connection);
        }
    }



    /**
     * Create the {@link SQLConversionVisitor} that will perform translation.  Typical custom
     * JDBC connectors will not need to create custom conversion visitors, rather implementors
     * should override existing {@link JDBCExecutionFactory} methods.
     * @return the {@link SQLConversionVisitor}
     */
	public SQLConversionVisitor getSQLConversionVisitor() {
        return new SQLConversionVisitor(this);
    }

    public SQLConversionVisitor getIndexVisitor() {
        // we need this to fix an issue with Oracle
        return getSQLConversionVisitor();
    }

    /**
     * Set to true to indicate that every branch of a join
     * should have parenthesis.
     * @return true if every branch of a join should use parenthesis
     */
    public boolean useParensForJoins() {
    	return false;
    }

    @Override
    public NullOrder getDefaultNullOrder() {
    	return NullOrder.LOW;
    }

    /**
     * Returns whether the limit clause is applied to the select clause.
     * @return true if the limit clause is part of the select
     */
    public boolean useSelectLimit() {
    	return false;
    }

	/**
	 * Get the predicate name for LIKE_REGEX
	 * @return
	 */
	public String getLikeRegexString() {
		return SQLConstants.Reserved.LIKE_REGEX;
	}

	/**
	 * Set the fetch size on the given statement.
	 * @param context
	 * @param command
	 * @param statement
	 * @param fetchSize
	 * @throws SQLException
	 */
	public void setFetchSize(Command command, ExecutionContext context, Statement statement, int fetchSize) throws SQLException {
		statement.setFetchSize(fetchSize);
	}

	// EXTENSION
	// ISSUE: DVCORE-980
	// FIXED BY: Roman Pedchenko
	// DATE: 2014.02.07
	private Boolean supportsOrderByAlias = null;
	@TranslatorProperty(display="Supports Order By Alias", description="True, if source supports aliases in ORDER BY clause", advanced=true)
	public boolean supportsOrderByAlias() {
		return supportsOrderByAlias != null ? supportsOrderByAlias : (supportsOrderBy() && (getOrderByStringConversion() == null || getOrderByStringConversion().isEmpty() || getOrderByStringConversion().trim().equals("%s")));
	}

	public void setSupportsOrderByAlias(boolean supportsOrderByAlias) {
		this.supportsOrderByAlias = supportsOrderByAlias;
	}

	protected Boolean supportsOrderByString = null;
	@TranslatorProperty(display="Supports Order By String", description="True, if source supports strings in ORDER BY clause", advanced=true)
	public boolean supportsOrderByString() {
		return supportsOrderByString != null ? supportsOrderByString : supportsOrderBy();
	}

	public void setSupportsOrderByString(boolean supportsOrderByString) {
		this.supportsOrderByString = supportsOrderByString;
	}

	protected boolean forbidNonMatchingNames = true;
    @TranslatorProperty(display="Forbid non-matching table names", description="True by default. Creation of tables with non-matching pattern name from importer.tableNamePattern is forbidden", advanced=true)
    public boolean forbidNonMatchingNames() {
        return forbidNonMatchingNames;
    }

    public void setForbidNonMatchingNames(boolean forbidNonMatchingTableNames) {
        this.forbidNonMatchingNames = forbidNonMatchingTableNames;
    }

    public CaseType columnNameType = CaseType.NONE;
    @TranslatorProperty(display="Set column names in either capital, preserve or lowercase letters", description="NONE by default. Table column names will be displayed in capital letters when columnNameType=UPPERCASE, in lowercase when columnNameType=NONE or preserving case when columnNameType=PRESERVE.", advanced=true)
    public CaseType getColumnNameType() {
        return columnNameType;
    }
    public void setColumnNameType(CaseType type) {
        this.columnNameType = type;
    }

    public CaseType tableNameType = CaseType.NONE;
    @TranslatorProperty(display="Set table names in either capital or lowercase letters", description="NONE by default. Table name will be displayed in capital letters if tableNameType=UPPERCASE or in lowercase letters when tableNameType=LOWERCASE.", advanced=true)
    public CaseType getTableNameType() { return tableNameType; }
    public void setTableNameType(CaseType type) { this.tableNameType = type; }
    // END EXTENSION

	public boolean supportsGeneratedKeys() {
		return supportsGeneratedKeys;
	}

	/**
	 *
	 * @param context
	 * @param command
	 * @return true if generated keys can be returned
	 */
	protected boolean supportsGeneratedKeys(ExecutionContext context,
			Command command) {
		return supportsGeneratedKeys() && command instanceof Insert;
	}

    /**
     * Return true if null literals should retain a type (typically handled by a cast).
     * @return
     */
    public boolean preserveNullTyping() {
        return false;
    }

	public void prepareBind(ColumnReference column, Literal obj, StringBuilder buffer) { }

	private static final SimpleDateFormat format10kts = new SimpleDateFormat("yyyyy-MM-dd HH:mm:ss.S");
	private java.util.Date _10kMinusOneDate = null;
    public java.util.Date get10kMinusOneDate() {
    	if (_10kMinusOneDate == null) {
    		Calendar cal = getDatabaseCalendar();
    		try {
				cal.setTime(format10kts.parse("10000-01-01 00:00:00.0"));
			} catch (ParseException e) {
				LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Error parsing lastDate");
			}
    		cal.add(Calendar.MILLISECOND, -1);
    		_10kMinusOneDate = cal.getTime();
    	}
    	return _10kMinusOneDate;
    }

    public QueryExpression pretranslateQuery(QueryExpression command, ExecutionContext ctx) {
		return null;
	}

	@Override
	@TranslatorProperty(display="Use Bindings For Dependent Join", description= "If PreparedStatement bindings should be used for dependent join values.")
	public boolean useBindingsForDependentJoin() {
		return useBindingsForDependentJoin;
	}

	public void setUseBindingsForDependentJoin(
			boolean useBindingsForDependentJoin) {
		this.useBindingsForDependentJoin = useBindingsForDependentJoin;
	}

	/**
	 *
	 * @return true if column names are required to retrieve generated keys
	 */
	public boolean useColumnNamesForGeneratedKeys() {
		return false;
	}

	public String getLateralKeyword() {
		return SQLConstants.Reserved.LATERAL;
	}

	@TranslatorProperty(display="Enables re-authentication mechanism", description= "Enables re-authentication mechanism")
	public boolean isEnableReAuth() {
		return enableReAuth;
	}

	public void setEnableReAuth(boolean enableReAuth) {
		this.enableReAuth = enableReAuth;
	}

    /**
     *
     * @param obj
     * @return true if the string expression is possibly non-ascii
     */
    protected boolean isNonAscii(Expression obj) {
        if (obj == null
		        || !(obj.getType() == TypeFacility.RUNTIME_TYPES.STRING || obj.getType() == TypeFacility.RUNTIME_TYPES.CHAR
		        || obj.getType() == TypeFacility.RUNTIME_TYPES.CLOB || obj.getType() == TypeFacility.RUNTIME_TYPES.JSON)) {
            return false;
        }
        if (obj instanceof ColumnReference) {
            ColumnReference cr = (ColumnReference)obj;
            return cr.getMetadataObject() == null
                    || (StringUtil.startsWithIgnoreCase(cr.getMetadataObject().getNativeType(), "N")); //$NON-NLS-1$

        }
	    if (obj.getType() == TypeFacility.RUNTIME_TYPES.CLOB || obj.getType() == TypeFacility.RUNTIME_TYPES.JSON) {
            return true;
        }
        if (obj instanceof Literal) {
            Object value = ((Literal) obj).getValue();
            if (value != null) {
                return SQLConversionVisitor.isNonAscii(value.toString());
            }
            return false;
        }
        if (obj instanceof Parameter) {
            return true;
        }
        if (obj instanceof Function) {
            Function f = (Function)obj;
            if (isNonAsciiFunction(f)) {
                return true;
            }
        }
        final boolean[] result = new boolean[1];
        HierarchyVisitor v = new HierarchyVisitor() {
            @Override
            public void visit(ColumnReference cr) {
                if (isNonAscii(cr)) {
                    result[0] = true;
                }
            }
            public void visit(Literal l) {
                if (isNonAscii(l)) {
                    result[0] = true;
                }
            }
            public void visit(Parameter p) {
                result[0] = true;
            }
        };
        v.visitNode(obj);
        return result[0];
    }

    /**
     *
     * @param f
     * @return true if the function is a conversion function to a non-ascii string type
     *
     * An implementation is not required if no such function exists, or the source can handle unicode string using the standard types.
     */
    protected boolean isNonAsciiFunction(Function f) {
        return false;
    }
}
