<commit_before>/* Datamodel license.
 * Exclusive rights on this code in any form
 * are belong to it's author. This code was
 * developed for commercial purposes only. 
 * For any questions and any actions with this
 * code in any form you have to contact to it's
 * author.
 * All rights reserved.
 */
package com.eas.client.sqldrivers;

import com.bearsoft.rowset.Converter;
import com.bearsoft.rowset.Rowset;
import com.bearsoft.rowset.exceptions.RowsetException;
import com.bearsoft.rowset.metadata.Field;
import com.bearsoft.rowset.metadata.ForeignKeySpec;
import com.bearsoft.rowset.metadata.PrimaryKeySpec;
import com.eas.client.ClientConstants;
import com.eas.client.SQLUtils;
import com.eas.client.metadata.DbTableIndexSpec;
import com.eas.client.settings.SettingsConstants;
import com.eas.client.sqldrivers.resolvers.TypesResolver;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mg
 */
public abstract class SqlDriver {

    // символы для экранирования имен схем,таблиц,колонок,индексов и т.д.
    private String leftCharForWrap = "\"";
    private String rightCharForWrap = "\"";
    private String[] wrappedChars = new String[]{" "};
    // error codes
    protected static final String EAS_TABLE_ALREADY_EXISTS = "EAS_TABLE_ALREADY_EXISTS";
    protected static final String EAS_TABLE_DOESNT_EXISTS = "EAS_TABLE_DOESNT_EXISTS";
    // misc
    protected static final String EAS_SQL_SCRIPT_DELIMITER = "#GO";
    public static final String DROP_FIELD_SQL_PREFIX = "alter table %s drop column ";
    public static final String ADD_FIELD_SQL_PREFIX = "alter table %s add ";

    public SqlDriver() {
        super();
    }

    public void initializeApplicationSchema(Connection aConnection) throws Exception {
        if (!checkApplicationSchemaInitialized(aConnection)) {
            String scriptText = readInitScriptResource();
            applyScript(scriptText, aConnection);
        }
    }

    /**
     * *
     * The database supports deferrable constraints to enable constrains check
     * on transaction commit.
     *
     * @return true if constraints is deferrable
     */
    public abstract boolean isConstraintsDeferrable();

    /**
     * *
     * Gets converter to convert some value of any compatible class to value of
     * predefined class, according to sql type.
     *
     * @return converter instance
     */
    public abstract Converter getConverter();

    /**
     * *
     * Gets type resolver to convert SQL types to JDBC types and vice-versa.
     *
     * @return TypesResolver instance
     */
    public abstract TypesResolver getTypesResolver();

    /**
     * *
     * Gets database initial script location and file name.
     *
     * @return
     */
    public abstract String getApplicationSchemaInitResourceName();

    /**
     * Returns subset of jdbc types, supported by particular database. The trick
     * is that database uses own identifiers for it's types and we need an extra
     * abstraction level.
     *
     * @return Subset of jdbc types, supported by particular database.
     */
    public abstract Set<Integer> getSupportedJdbcDataTypes();

    /**
     * *
     * Sets current schema for current session.
     *
     * @param aConnection JDBC connection
     * @param aSchema Schema name
     * @throws Exception Exception in the case of operation failure
     */
    public abstract void applyContextToConnection(Connection aConnection, String aSchema) throws Exception;

    /**
     * Gets current schema for connection
     *
     * @param aConnection JDBC connection
     * @return Schema name
     * @throws Exception Exception Exception in the case of operation failure
     */
    public String getConnectionContext(Connection aConnection) throws Exception {
        try (PreparedStatement stmt = aConnection.prepareStatement(getSql4GetConnectionContext())) {
            ResultSet rs = stmt.executeQuery();
            rs.next();
            return rs.getString(1);
        }
    }

    protected abstract String getSql4GetConnectionContext();

    /**
     * Returns sql query text, usable for enumerating tables in particular
     * schema<commit_msg>minor changes<commit_after>/* Datamodel license.
 * Exclusive rights on this code in any form
 * are belong to it's author. This code was
 * developed for commercial purposes only. 
 * For any questions and any actions with this
 * code in any form you have to contact to it's
 * author.
 * All rights reserved.
 */
package com.eas.client.sqldrivers;

import com.bearsoft.rowset.Converter;
import com.bearsoft.rowset.Rowset;
import com.bearsoft.rowset.exceptions.RowsetException;
import com.bearsoft.rowset.metadata.Field;
import com.bearsoft.rowset.metadata.ForeignKeySpec;
import com.bearsoft.rowset.metadata.PrimaryKeySpec;
import com.eas.client.ClientConstants;
import com.eas.client.SQLUtils;
import com.eas.client.metadata.DbTableIndexSpec;
import com.eas.client.settings.SettingsConstants;
import com.eas.client.sqldrivers.resolvers.TypesResolver;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mg
 */
public abstract class SqlDriver {

    // символы для экранирования имен схем,таблиц,колонок,индексов и т.д.
    private String leftCharForWrap = "\"";
    private String rightCharForWrap = "\"";
    private String[] wrappedChars = new String[]{" "};
    // error codes
    protected static final String EAS_TABLE_ALREADY_EXISTS = "EAS_TABLE_ALREADY_EXISTS";
    protected static final String EAS_TABLE_DOESNT_EXISTS = "EAS_TABLE_DOESNT_EXISTS";
    // misc
    protected static final String EAS_SQL_SCRIPT_DELIMITER = "#GO";
    public static final String DROP_FIELD_SQL_PREFIX = "alter table %s drop column ";
    public static final String ADD_FIELD_SQL_PREFIX = "alter table %s add ";

    public SqlDriver() {
        super();
    }

    public void initializeApplicationSchema(Connection aConnection) throws Exception {
        if (!checkApplicationSchemaInitialized(aConnection)) {
            String scriptText = readInitScriptResource();
            applyScript(scriptText, aConnection);
        }
    }

    /**
     * *
     * The database supports deferrable constraints to enable constrains check
     * on transaction commit.
     *
     * @return true if constraints is deferrable
     */
    public abstract boolean isConstraintsDeferrable();

    /**
     * *
     * Gets converter to convert some value of any compatible class to value of
     * predefined class, according to sql type.
     *
     * @return converter instance
     */
    public abstract Converter getConverter();

    /**
     * *
     * Gets type resolver to convert SQL types to JDBC types and vice-versa.
     *
     * @return TypesResolver instance
     */
    public abstract TypesResolver getTypesResolver();

    /**
     * *
     * Gets database initial script location and file name.
     *
     * @return
     */
    public abstract String getApplicationSchemaInitResourceName();

    /**
     * Returns subset of jdbc types, supported by particular database. The trick
     * is that database uses own identifiers for it's types and we need an extra
     * abstraction level.
     *
     * @return Subset of jdbc types, supported by particular database.
     */
    public abstract Set<Integer> getSupportedJdbcDataTypes();

    /**
     * *
     * Sets current schema for current session.
     *
     * @param aConnection JDBC connection
     * @param aSchema Schema name
     * @throws Exception in the case of operation failure
     */
    public abstract void applyContextToConnection(Connection aConnection, String aSchema) throws Exception;

    /**
     * Gets current schema for connection
     *
     * @param aConnection JDBC connection
     * @return Schema name
     * @throws Exception in the case of operation failure
     */
    public String getConnectionContext(Connection aConnection) throws Exception {
        try (PreparedStatement stmt = aConnection.prepareStatement(getSql4GetConnectionContext())) {
            ResultSet rs = stmt.executeQuery();
            rs.next();
            return rs.getString(1);
        }
    }

    protected abstract String getSql4GetConnectionContext();

    /**
     * Returns sql query text, usable for enumerating tables in particular
     * schema
{'fraction_non_alphanumeric': 0.08099906629318394, 'fraction_numerical': 0.0007002801120448179, 'mean_word_length': 3.1137782045127222, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3837780', 'n_tokens_mistral': 2342, 'n_tokens_neox': 2264, 'n_words': 735}