package com.vladmihalcea.book.hpjp.hibernate.identifier;

import com.vladmihalcea.book.hpjp.util.AbstractTest;
import com.vladmihalcea.book.hpjp.util.providers.Database;
import org.hibernate.Session;

import java.sql.Statement;
import java.util.List;
import java.util.Properties;

import static org.junit.Assert.assertEquals;

public abstract class AbstractPooledSequenceIdentifierTest extends AbstractTest {

    protected abstract Object newEntityInstance();

    @Override
    protected Database database() {
        return Database.POSTGRESQL;
    }

    @Override
    protected Properties properties() {
        Properties properties = super.properties();
        properties.put("hibernate.id.new_generator_mappings", "true");
        return properties;
    }

    protected void insertSequences() {
        LOGGER.debug("testSequenceIdentifierGenerator");
        doInJPA(entityManager -> {
            for (int i = 0; i < 5; i++) {
                entityManager.persist(newEntityInstance());
                entityManager.flush();
            }
            entityManager.flush();
            assertEquals(5, ((Number) entityManager.createNativeQuery("SELECT COUNT(*) FROM Post").getSingleResult()).intValue());

            entityManager.unwrap(Session.class).doWork(connection -> {
                try(Statement statement = connection.createStatement()) {
                    statement.executeUpdate("INSERT INTO Post VALUES NEXT VALUE FOR post_sequence");
                }
            });

            assertEquals(6, ((Number) entityManager.createNativeQuery("SELECT COUNT(*) FROM Post").getSingleResult()).intValue());
            List<Number> ids = entityManager.createNativeQuery("SELECT id FROM Post").getResultList();
            for (Number id : ids) {
                LOGGER.debug("Found id: {}", id);
            }
            for (int i = 0; i < 3; i++) {
                entityManager.persist(newEntityInstance());
                entityManager.flush();
            }
        });
    }
}

@@ -1422,16 +1422,21 @@
 Post
+ (id)
  VALUES 
 NEXT
@@ -1435,23 +1435,18 @@
 UES 
-NEXT VALUE FOR 
+(nextval('
 post
@@ -1454,16 +1454,19 @@
 sequence
+'))
 %22);%0A    

Fix test that now runs on PostgreSQL