package org.simpleflatmapper.jdbc.test;

import org.junit.Test;
import org.simpleflatmapper.jdbc.Crud;
import org.simpleflatmapper.jdbc.DynamicJdbcMapper;
import org.simpleflatmapper.jdbc.JdbcMapperFactory;
import org.simpleflatmapper.map.property.AutoGeneratedProperty;
import org.simpleflatmapper.map.property.KeyProperty;
import org.simpleflatmapper.test.beans.DbObject;
import org.simpleflatmapper.test.jdbc.DbHelper;
import org.simpleflatmapper.util.CheckedConsumer;

import java.net.InetAddress;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.Iterator;
import java.util.UUID;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class PostgresqlMapperTest {

    @Test
    public void testBatchUpsertOnDb() throws SQLException {
        Connection connection = DbHelper.getDbConnection(DbHelper.TargetDB.POSTGRESQL);
        if (connection == null) { System.err.println("Db Postgresql not available"); return; }
        try {
            DynamicJdbcMapper<InetAndCidr> mapper = JdbcMapperFactory.newInstance().newMapper(InetAndCidr.class);
            DynamicJdbcMapper<InetAndCidrAsString> mapperStr = JdbcMapperFactory.newInstance().newMapper(InetAndCidrAsString.class);

            Statement statement = connection.createStatement();
            try  {
                statement.execute("DROP TABLE IF EXISTS  iac ");
                statement.execute("create TABLE iac (  field1 inet, field2 cidr)");
                statement.execute("INSERT INTO iac(field1, field2) VALUES('10.1.0.1', '10.1.0.0/22')");


                ResultSet rs = statement.executeQuery("SELECT field1, field2 from iac");
                try  {
                    Iterator<InetAndCidr> iterator = mapper.iterator(rs);
                    InetAndCidr next = iterator.next();
                    System.out.println("next = " + next);

                } finally {
                    rs.close();
                }
                rs = statement.executeQuery("SELECT field1, field2 from iac")
                try {
                    Iterator<InetAndCidrAsString> iteratorStr = mapperStr.iterator(rs);
                    InetAndCidrAsString inetAndCidrAsString = iteratorStr.next();
                    System.out.println("inetAndCidrAsString = " + inetAndCidrAsString);
                } finally {
                    rs.close();
                }
            } finally {
                statement.close();
            }



        } finally {
            connection.close();
        }
    }

    
    
    public static class InetAndCidr {
        public InetAddress field1;
        public Object field2;

        @Override
        public String toString() {
            return "InetAndCidr{" +
                    "field1=" + field1 +
                    ", field2=" + field2 +
                    '}';
        }
    }

    public static class InetAndCidrAsString {
        public String field1;
        public String field2;

        @Override
        public String toString() {
            return "InetAndCidrAsString{" +
                    "field1='" + field1 + '\'' +
                    ", field2='" + field2 + '\'' +
                    '}';
        }
    }

}
@@ -2162,16 +2162,17 @@
 om iac%22)
+;
 %0A       

remove try with resources