/*
 * DBeaver - Universal Database Manager
 * Copyright (C) 2010-2020 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.athena.model;

import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.ext.generic.model.GenericTableBase;
import org.jkiss.dbeaver.ext.generic.model.GenericView;
import org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaModel;
import org.jkiss.dbeaver.model.DBPErrorAssistant;
import org.jkiss.dbeaver.model.DBPEvaluationContext;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.exec.DBCQueryTransformProvider;
import org.jkiss.dbeaver.model.exec.DBCQueryTransformType;
import org.jkiss.dbeaver.model.exec.DBCQueryTransformer;
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.sql.QueryTransformerLimit;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.utils.CommonUtils;

import java.sql.SQLException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Athena meta model
 */
public class AthenaMetaModel extends GenericMetaModel implements DBCQueryTransformProvider {

    private Pattern ERROR_POSITION_PATTERN = Pattern.compile(" line ([0-9]+)\\:([0-9]+)");

    public AthenaMetaModel() {
    }

    @Nullable
    @Override
    public DBCQueryTransformer createQueryTransformer(@NotNull DBCQueryTransformType type) {
        if (type == DBCQueryTransformType.RESULT_SET_LIMIT) {
            return new QueryTransformerLimit(false);
        }
        return null;
    }

    @Override
    public String getTableDDL(DBRProgressMonitor monitor, GenericTableBase sourceObject, Map<String, Object> options) throws DBException {
        try (JDBCSession session = DBUtils.openMetaSession(monitor, sourceObject, "Read Athena object DDL")) {
            try (JDBCPreparedStatement dbStat = session.prepareStatement(
                "SHOW CREATE TABLE " + sourceObject.getFullyQualifiedName(DBPEvaluationContext.DDL))) {
                try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                    StringBuilder sql = new StringBuilder();
                    while (dbResult.nextRow()) {
                        sql.append(dbResult.getString(1)).append("\n");
                    }
                    return sql.toString();
                }
            }
        } catch (SQLException e) {
            throw new DBException(e, sourceObject.getDataSource());
        }
    }

    @Override
    public boolean supportsTableDDLSplit(GenericTableBase sourceObject) {
        return false;
    }

    @Override
    public String getViewDDL(DBRProgressMonitor monitor, GenericView sourceObject, Map<String, Object> options) throws DBException {
        return getTableDDL(monitor, sourceObject, options);
    }

    @Override
    public DBPErrorAssistant.ErrorPosition getErrorPosition(@NotNull Throwable error) {
        String message = error.getMessage();
        if (!CommonUtils.isEmpty(message)) {
            Matcher matcher = ERROR_POSITION_PATTERN.matcher(message);
            if (matcher.find()) {
                DBPErrorAssistant.ErrorPosition pos = new DBPErrorAssistant.ErrorPosition();
                pos.line = Integer.parseInt(matcher.group(1)) - 1;
                pos.position = Integer.parseInt(matcher.group(2)) - 1;
                return pos;
            }
        }
        return null;
    }
}

@@ -1963,16 +1963,146 @@
 -9%5D+)%22);
+%0A    private static final String TABLE_DDL = %22SHOW CREATE TABLE %22;%0A    private static final String VIEW_DDL = %22SHOW CREATE VIEW %22;
 %0A%0A    pu
@@ -2572,751 +2572,71 @@
     
-try (JDBCSession session = DBUtils.openMetaSession(monitor, sourceObject, %22Read Athena o
+return getO
 bject
- 
 DDL
-%22)) %7B%0A            try (JDBCPreparedStatement dbStat = session.prepareStatement(%0A                %22SHOW CREATE TABLE %22 + sourceObject.getFullyQualifiedName(DBPEvaluationContext.DDL))) %7B%0A                try (JDBCResultSet dbResult = dbStat.executeQuery()) %7B%0A                    StringBuilder sql = new StringBuilder();%0A                    while (dbResult.nextRow()) %7B%0A                        sql.append(dbResult.getString(1)).append(%22%5Cn%22);%0A                    %7D%0A                    return sql.toString();%0A                %7D%0A            %7D%0A        %7D catch (SQLException e) %7B%0A            throw new DBException(e, sourceObject.getDataSource());%0A        %7D
+(monitor, sourceObject, options, TABLE_DDL);
 %0A   
@@ -2917,29 +2917,30 @@
   return get
-Table
+Object
 DDL(monitor,
@@ -2961,16 +2961,26 @@
  options
+, VIEW_DDL
 );%0A    %7D
@@ -3553,32 +3553,32 @@
     %7D%0A        %7D%0A
-
         return n
@@ -3587,11 +3587,930 @@
 l;%0A    %7D
+%0A%0A    private String getObjectDDL(DBRProgressMonitor monitor, GenericTableBase sourceObject, Map%3CString, Object%3E options, String ddlStatement) throws DBException %7B%0A        try (JDBCSession session = DBUtils.openMetaSession(monitor, sourceObject, %22Read Athena object DDL%22)) %7B%0A            try (JDBCPreparedStatement dbStat = session.prepareStatement(%0A                ddlStatement + %22 %22 + sourceObject.getFullyQualifiedName(DBPEvaluationContext.DDL))) %7B%0A                try (JDBCResultSet dbResult = dbStat.executeQuery()) %7B%0A                    StringBuilder sql = new StringBuilder();%0A                    while (dbResult.nextRow()) %7B%0A                        sql.append(dbResult.getString(1)).append(%22%5Cn%22);%0A                    %7D%0A                    return sql.toString();%0A                %7D%0A            %7D%0A        %7D catch (SQLException e) %7B%0A            throw new DBException(e, sourceObject.getDataSource());%0A        %7D%0A    %7D
 %0A%7D%0A

Fix Athena plugin to script view definitions