package com.timgroup.eventstore.mysql;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class BasicMysqlEventStoreSetup {
    private final ConnectionProvider connectionProvider;
    private final String tableName;

    public BasicMysqlEventStoreSetup(ConnectionProvider connectionProvider, String tableName) {
        this.connectionProvider = connectionProvider;
        this.tableName = tableName;
    }

    public void drop() {
        try (Connection connection = connectionProvider.getConnection();
             Statement statement = connection.createStatement()) {
            statement.executeUpdate("drop table if exists " + tableName);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void create() {
        try (Connection connection = connectionProvider.getConnection();
             Statement statement = connection.createStatement()) {
            statement.execute("create table " + tableName + "(" +
                    "position bigint primary key, " +
                    "timestamp datetime not null, " +
                    "stream_category varchar(255) not null, " +
                    "stream_id varchar(255) not null, " +
                    "event_number bigint not null, " +
                    "event_type varchar(255) not null," +
                    "data blob not null, " +
                    "metadata blob not null," +
                    "unique(stream_category, stream_id, event_number)" +
                    ")");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

@@ -1642,11 +1642,892 @@
  %7D%0A    %7D
+%0A%0A    public void lazyCreate() %7B%0A        try (Connection connection = connectionProvider.getConnection();%0A             Statement statement = connection.createStatement()) %7B%0A            statement.execute(%22create table if not exists %22 + tableName + %22(%22 +%0A                    %22position bigint primary key, %22 +%0A                    %22timestamp datetime not null, %22 +%0A                    %22stream_category varchar(255) not null, %22 +%0A                    %22stream_id varchar(255) not null, %22 +%0A                    %22event_number bigint not null, %22 +%0A                    %22event_type varchar(255) not null,%22 +%0A                    %22data blob not null, %22 +%0A                    %22metadata blob not null,%22 +%0A                    %22unique(stream_category, stream_id, event_number)%22 +%0A                    %22)%22);%0A        %7D catch (SQLException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D
 %0A%7D%0A

support creating event table if not already present