/*
 * 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 io.prestosql.testing.sql;

import com.google.common.collect.ImmutableList;

import java.security.SecureRandom;
import java.util.List;

import static java.lang.Character.MAX_RADIX;
import static java.lang.Math.abs;
import static java.lang.Math.min;
import static java.lang.String.format;

public class TestTable
        implements AutoCloseable
{
    private static final SecureRandom random = new SecureRandom();
    private static final int RANDOM_SUFFIX_LENGTH = 5;

    private final SqlExecutor sqlExecutor;
    private final String name;

    public TestTable(SqlExecutor sqlExecutor, String namePrefix, String tableDefinition)
    {
        this(sqlExecutor, namePrefix, tableDefinition, ImmutableList.of());
    }

    public TestTable(SqlExecutor sqlExecutor, String namePrefix, String tableDefinition, List<String> rowsToInsert)
    {
        this.sqlExecutor = sqlExecutor;
        this.name = namePrefix + "_" + randomTableSuffix();
        sqlExecutor.execute(format("CREATE TABLE %s %s", name, tableDefinition));
        try {
            for (String row : rowsToInsert) {
                // some databases do not support multi value insert statement
                sqlExecutor.execute(format("INSERT INTO %s VALUES (%s)", name, row));
            }
        }
        catch (Exception e) {
            try (TestTable ignored = this) {
                throw e;
            }
        }
    }

    public String getName()
    {
        return name;
    }

    @Override
    public void close()
    {
        sqlExecutor.execute("DROP TABLE " + name);
    }

    public static String randomTableSuffix()
    {
        String randomSuffix = Long.toString(abs(random.nextLong()), MAX_RADIX);
        return randomSuffix.substring(0, min(RANDOM_SUFFIX_LENGTH, randomSuffix.length()));
    }
}

@@ -693,24 +693,151 @@
 .util.List;%0A
+import java.util.Map;%0Aimport java.util.function.Function;%0A%0Aimport static com.google.common.collect.ImmutableMap.toImmutableMap;
 %0Aimport stat
@@ -976,16 +976,53 @@
 .format;
+%0Aimport static java.lang.String.join;
 %0A%0Apublic
@@ -2190,24 +2190,2493 @@
 ame;%0A    %7D%0A%0A
+    public static TestTable fromColumns(SqlExecutor sqlExecutor, String namePrefix, Map%3CString, List%3CString%3E%3E columns)%0A    %7B%0A        return fromColumns(%0A                sqlExecutor,%0A                namePrefix,%0A                columns,%0A                column -%3E %7B%0A                    throw new IllegalArgumentException(String.format(%22Some values missing for column '%25s'%22, column));%0A                %7D);%0A    %7D%0A%0A    public static TestTable fromColumns(SqlExecutor sqlExecutor, String namePrefix, Map%3CString, List%3CString%3E%3E columns, String defaultValue)%0A    %7B%0A        return fromColumns(sqlExecutor, namePrefix, columns, column -%3E defaultValue);%0A    %7D%0A%0A    private static TestTable fromColumns(SqlExecutor sqlExecutor, String namePrefix, Map%3CString, List%3CString%3E%3E columns, Function%3CString, String%3E defaultValueSupplier)%0A    %7B%0A        int rowsCount = columns.values().stream()%0A                .mapToInt(List::size)%0A                .max().orElseThrow(() -%3E new IllegalArgumentException(%22please, give me at least one column to work with%22));%0A        return fromColumnValueProviders(%0A                sqlExecutor,%0A                namePrefix,%0A                rowsCount,%0A                columns.entrySet().stream()%0A                        .collect(toImmutableMap(%0A                                Map.Entry::getKey,%0A                                entry -%3E index -%3E %7B%0A                                    if (index %3C entry.getValue().size()) %7B%0A                                        return entry.getValue().get(index);%0A                                    %7D%0A                                    return defaultValueSupplier.apply(entry.getKey());%0A                                %7D)));%0A    %7D%0A%0A    public static TestTable fromColumnValueProviders(SqlExecutor sqlExecutor, String namePrefix, int rowsCount, Map%3CString, Function%3CInteger, String%3E%3E columnsValueProviders)%0A    %7B%0A        String tableDefinition = %22(%22 + join(%22,%22, columnsValueProviders.keySet()) + %22)%22;%0A%0A        ImmutableList.Builder%3CString%3E rows = ImmutableList.builder();%0A        for (int rowId = 0; rowId %3C rowsCount; rowId++) %7B%0A            ImmutableList.Builder%3CString%3E rowValues = ImmutableList.builder();%0A            for (Function%3CInteger, String%3E columnValues : columnsValueProviders.values()) %7B%0A                rowValues.add(columnValues.apply(rowId));%0A            %7D%0A            rows.add(join(%22,%22, rowValues.build()));%0A        %7D%0A        return new TestTable(sqlExecutor, namePrefix, tableDefinition, rows.build());%0A    %7D%0A%0A
     @Overrid

Allow specifying entires in TestTable by columns