package org.xblackcat.sjpu.storage.impl;

import org.xblackcat.sjpu.storage.AnObjectMapper;
import org.xblackcat.sjpu.storage.StorageException;
import org.xblackcat.sjpu.storage.StorageSetupException;
import org.xblackcat.sjpu.storage.converter.IToObjectConverter;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 21.11.13 17:50
 *
 * @author xBlackCat
 */
public abstract class AQueryHelper {
    protected final Map<Class, AnObjectMapper> mappers = new HashMap<>();

    @SafeVarargs
    protected AQueryHelper(Class<? extends AnObjectMapper>... mappers) {
        try {
            for (Class<? extends AnObjectMapper> om : mappers) {
                final AnObjectMapper mapper;
                mapper = om.newInstance();

                this.mappers.put(mapper.getOrigClass(), mapper);
            }
        } catch (InstantiationException | IllegalAccessException e) {
            throw new StorageSetupException("Mapper should have default constructor", e);
        }

    }

    @SuppressWarnings("unchecked")
    protected Object[] preProcessing(Object[] params) {
        final Object[] processed = new Object[params.length];

        int i = 0;
        while (i < params.length) {
            Object o = params[i];

            AnObjectMapper m = mappers.get(o.getClass());
            if (m == null) {
                processed[i] = o;
            } else {
                processed[i] = m.convert(o);
            }

            i++;
        }

        return processed;
    }

    public abstract <T> List<T> execute(IToObjectConverter<T> c, String sql, Object... parameters) throws StorageException;

    public abstract <T> T executeSingle(IToObjectConverter<T> c, String sql, Object... parameters) throws StorageException;

    public abstract int update(String sql, Object... parameters) throws StorageException;

    public abstract Connection getConnection() throws SQLException;
}

@@ -1350,24 +1350,118 @@
 rams%5Bi%5D;%0D%0A%0D%0A
+            if (o == null) %7B%0D%0A                processed%5Bi%5D = null;%0D%0A            %7D else %7B%0D%0A    
             
@@ -1515,24 +1515,28 @@
             
+   
+ 
 if (m == nul
@@ -1533,32 +1533,36 @@
  (m == null) %7B%0D%0A
+    
                 
@@ -1584,32 +1584,36 @@
 o;%0D%0A            
+   
+ 
 %7D else %7B%0D%0A      
@@ -1598,32 +1598,36 @@
       %7D else %7B%0D%0A
+    
                 
@@ -1652,24 +1652,43 @@
 onvert(o);%0D%0A
+                %7D%0D%0A
             

Fix NPE when processing null value