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);
            
            try (Statement statement = connection.createStatement()) {
                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')");
                
                
                try (ResultSet rs = statement.executeQuery("SELECT field1, field2 from iac")) {
                    Iterator<InetAndCidr> iterator = mapper.iterator(rs);
                    InetAndCidr next = iterator.next();
                    System.out.println("next = " + next);

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



        } 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 + '\'' +
                    '}';
        }
    }

}
@@ -1367,28 +1367,16 @@
 class);%0A
-            
 %0A       
@@ -1380,21 +1380,16 @@
         
-try (
 Statemen
@@ -1430,17 +1430,34 @@
 tement()
-)
+;%0A            try 
  %7B%0A     
@@ -1708,41 +1708,9 @@
 %22);%0A
-                %0A                
+%0A
 %0A   
@@ -1714,37 +1714,32 @@
                 
-try (
 ResultSet rs = s
@@ -1785,33 +1785,54 @@
 ield2 from iac%22)
-)
+;%0A                try 
  %7B%0A             
@@ -2016,32 +2016,42 @@
                %7D
+ finally %7B
 %0A               
@@ -2055,22 +2055,57 @@
     
-try (ResultSet
+    rs.close();%0A                %7D%0A               
  rs 
@@ -2162,17 +2162,36 @@
 om iac%22)
-)
+%0A                try
  %7B%0A     
@@ -2452,32 +2452,151 @@
                %7D
+ finally %7B%0A                    rs.close();%0A                %7D%0A            %7D finally %7B%0A                statement.close();
 %0A            %7D%0A%0A

remove try with resources