<commit_before>/*
 * DBeaver - Universal Database Manager
 * Copyright (C) 2013-2015 <NAME> (<EMAIL>)
 * Copyright (C) 2010-2021 DBeaver Corp and others
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jkiss.dbeaver.ext.db2.editors;

import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.ext.db2.model.*;
import org.jkiss.dbeaver.ext.db2.model.dict.DB2TableType;
import org.jkiss.dbeaver.model.DBConstants;
import org.jkiss.dbeaver.model.DBPNamedObject;
import org.jkiss.dbeaver.model.exec.DBCExecutionPurpose;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCUtils;
import org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.sql.SQLConstants;
import org.jkiss.dbeaver.model.struct.*;
import org.jkiss.utils.CommonUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * DB2 Structure Assistant
 * 
 * @author <NAME>
 */
public class DB2StructureAssistant implements DBSStructureAssistant<DB2ExecutionContext> {
    private static final Log LOG = Log.getLog(DB2StructureAssistant.class);

    private static final String WITH_UR = "WITH UR";
    private static final String LF = "\n";

    // TODO DF: Work in progess

    private static final DBSObjectType[] SUPP_OBJ_TYPES = { DB2ObjectType.ALIAS, DB2ObjectType.TABLE, DB2ObjectType.VIEW,
        DB2ObjectType.MQT, DB2ObjectType.NICKNAME, DB2ObjectType.COLUMN, DB2ObjectType.ROUTINE };

    private static final DBSObjectType[] HYPER_LINKS_TYPES = { DB2ObjectType.ALIAS, DB2ObjectType.TABLE, DB2ObjectType.VIEW,
        DB2ObjectType.MQT, DB2ObjectType.NICKNAME, DB2ObjectType.ROUTINE, };

    private static final DBSObjectType[] AUTOC_OBJ_TYPES = { DB2ObjectType.ALIAS, DB2ObjectType.TABLE, DB2ObjectType.VIEW,
        DB2ObjectType.MQT, DB2ObjectType.NICKNAME, DB2ObjectType.ROUTINE, };

    private static final String SQL_COLS_ALL;
    private static final String SQL_COLS_SCHEMA;

    private final DB2DataSource dataSource;

    // -----------------
    // Constructors
    // -----------------
    public DB2StructureAssistant(DB2DataSource dataSource)
    {
        this.dataSource = dataSource;
    }

    // -----------------
    // Method Interface
    // -----------------

    @Override
    public DBSObjectType[] getSupportedObjectTypes()
    {
        return SUPP_OBJ_TYPES;
    }

    @Override
    public DBSObjectType[] getSearchObjectTypes() {
        return getSupportedObjectTypes();
    }

    @Override
    public DBSObjectType[] getHyperlinkObjectTypes()
    {
        return HYPER_LINKS_TYPES;
    }

    @Override
    public DBSObjectType[] getAutoCompleteObjectTypes()
    {
        return AUTOC_OBJ_TYPES;
    }

    @NotNull
    @Override
    public List<DBSObjectReference> findObjectsByMask(@NotNull DBRProgressMonitor monitor, @NotNull DB2ExecutionContext executionContext,
                                                      @NotNull ObjectsSearchParams params) throws DBException {
        List<DB2ObjectType> db2ObjectTypes = new ArrayList<>(params.getObjectTypes().length);
        for (DBSObjectType dbsObjectType : params.getObjectTypes()) {
            db2ObjectTypes.add((DB2ObjectType) dbsObjectType);
        }

        DB2Schema schema = params.getParentObject() instanceof DB2Schema ? (DB2Schema) params.getParentObject() : null;
        if (schema == null && !params.isGlobalSearch()) {
            schema = executionContext.getContextDefaults().getDefaultSchema();
        }

        try (JDBCSession session = executionContext.openSession(monitor, DBCExecutionPurpose.META, "Find objects by name")) {
            return searchAllObjects(session, schema, db2ObjectTypes, params);
        } catch (SQLException ex) {
            throw new DBException(ex, dataSource);
        }
    }

    // -----------------
    // Helpers
    // -----------------

    private List<DBSObjectReference> searchAllObjects(final JDBCSession session, final DB2Schema schema, List<DB2ObjectType> db2ObjectTypes,
                                                      @NotNull ObjectsSearchParams params) throws SQLException, DBException {
        List<DBSObjectReference> objects = new ArrayList<>();

        String searchObjectNameMask = params.getMask();
        if (!params.isCaseSensitive()) {
            searchObjectNameMask = searchObjectNameMask.toUpperCase();
        }

        int nbResults = 0;
        int maxResults = params.getMaxResults();

        // Tables, Alias, Views, Nicknames, MQT
        if (db2ObjectTypes.stream().anyMatch(DB2StructureAssistant::isTable)) {
            searchTables(session, schema, searchObjectNameMask, db2ObjectTypes, maxResults, objects, nbResults, params.isSearchInDefinitions());
            if (nbResults >= maxResults) {
                return objects;
            }
        }
<commit_msg>#13831 fix search for table columns in db2<commit_after>/*
 * DBeaver - Universal Database Manager
 * Copyright (C) 2013-2015 <NAME> (<EMAIL>)
 * Copyright (C) 2010-2021 DBeaver Corp and others
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jkiss.dbeaver.ext.db2.editors;

import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.ext.db2.model.*;
import org.jkiss.dbeaver.ext.db2.model.dict.DB2TableType;
import org.jkiss.dbeaver.model.DBConstants;
import org.jkiss.dbeaver.model.DBPNamedObject;
import org.jkiss.dbeaver.model.exec.DBCExecutionPurpose;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCUtils;
import org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.sql.SQLConstants;
import org.jkiss.dbeaver.model.struct.*;
import org.jkiss.utils.CommonUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * DB2 Structure Assistant
 * 
 * @author <NAME>
 */
public class DB2StructureAssistant implements DBSStructureAssistant<DB2ExecutionContext> {
    private static final Log LOG = Log.getLog(DB2StructureAssistant.class);

    private static final String WITH_UR = "WITH UR";
    private static final String LF = "\n";

    // TODO DF: Work in progess

    private static final DBSObjectType[] SUPP_OBJ_TYPES = { DB2ObjectType.ALIAS, DB2ObjectType.TABLE, DB2ObjectType.VIEW,
        DB2ObjectType.MQT, DB2ObjectType.NICKNAME, DB2ObjectType.COLUMN, DB2ObjectType.ROUTINE };

    private static final DBSObjectType[] HYPER_LINKS_TYPES = { DB2ObjectType.ALIAS, DB2ObjectType.TABLE, DB2ObjectType.VIEW,
        DB2ObjectType.MQT, DB2ObjectType.NICKNAME, DB2ObjectType.ROUTINE, };

    private static final DBSObjectType[] AUTOC_OBJ_TYPES = { DB2ObjectType.ALIAS, DB2ObjectType.TABLE, DB2ObjectType.VIEW,
        DB2ObjectType.MQT, DB2ObjectType.NICKNAME, DB2ObjectType.ROUTINE, };

    private static final String SQL_COLS_ALL;
    private static final String SQL_COLS_SCHEMA;

    private final DB2DataSource dataSource;

    // -----------------
    // Constructors
    // -----------------
    public DB2StructureAssistant(DB2DataSource dataSource)
    {
        this.dataSource = dataSource;
    }

    // -----------------
    // Method Interface
    // -----------------

    @Override
    public DBSObjectType[] getSupportedObjectTypes()
    {
        return SUPP_OBJ_TYPES;
    }

    @Override
    public DBSObjectType[] getSearchObjectTypes() {
        return getSupportedObjectTypes();
    }

    @Override
    public DBSObjectType[] getHyperlinkObjectTypes()
    {
        return HYPER_LINKS_TYPES;
    }

    @Override
    public DBSObjectType[] getAutoCompleteObjectTypes()
    {
        return AUTOC_OBJ_TYPES;
    }

    @NotNull
    @Override
    public List<DBSObjectReference> findObjectsByMask(@NotNull DBRProgressMonitor monitor, @NotNull DB2ExecutionContext executionContext,
                                                      @NotNull ObjectsSearchParams params) throws DBException {
        List<DB2ObjectType> db2ObjectTypes = new ArrayList<>(params.getObjectTypes().length);
        for (DBSObjectType dbsObjectType : params.getObjectTypes()) {
            db2ObjectTypes.add((DB2ObjectType) dbsObjectType);
        }

        DB2Schema schema = params.getParentObject() instanceof DB2Schema ? (DB2Schema) params.getParentObject() : null;
        if (schema == null && !params.isGlobalSearch()) {
            schema = executionContext.getContextDefaults().getDefaultSchema();
        }

        try (JDBCSession session = executionContext.openSession(monitor, DBCExecutionPurpose.META, "Find objects by name")) {
            return searchAllObjects(session, schema, db2ObjectTypes, params);
        } catch (SQLException ex) {
            throw new DBException(ex, dataSource);
        }
    }

    // -----------------
    // Helpers
    // -----------------

    private List<DBSObjectReference> searchAllObjects(final JDBCSession session, final DB2Schema schema, List<DB2ObjectType> db2ObjectTypes,
                                                      @NotNull ObjectsSearchParams params) throws SQLException, DBException {
        List<DBSObjectReference> objects = new ArrayList<>();

        String searchObjectNameMask = params.getMask();
        if (!params.isCaseSensitive()) {
            searchObjectNameMask = searchObjectNameMask.toUpperCase();
        }

        int nbResults = 0;
        int maxResults = params.getMaxResults();

        // Tables, Alias, Views, Nicknames, MQT
        if (db2ObjectTypes.stream().anyMatch(DB2StructureAssistant::isTable)) {
            searchTables(session, schema, searchObjectNameMask, db2ObjectTypes, maxResults, objects, nbResults, params.isSearchInDefinitions());
            if (nbResults >= maxResults) {
                return objects;
            }
        }

        // Columns
        if (db2ObjectTypes.contains(DB2ObjectType.COLUMN)) {
            searchColumns(session, schema, searchObjectNameMask, db2ObjectTypes, maxResults, objects, nbResults);
            if (nbResults >= maxResults) {
                return objects;
            }
        }

{'fraction_non_alphanumeric': 0.10746345216722236, 'fraction_numerical': 0.01205437291613234, 'mean_word_length': 3.042156185210781, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '264831', 'n_tokens_mistral': 3457, 'n_tokens_neox': 3324, 'n_words': 760}