package com.moparisthebest.jdbc; import com.moparisthebest.jdbc.codegen.JdbcMapper; import java.sql.Array; import java.sql.Connection; import java.sql.SQLException; import java.util.Collection; /** * Created by mopar on 4/29/15. */ public class ArrayInList implements InList { private static final InList instance = new ArrayInList(); public static InList instance() { return instance; } protected final String numericType, otherType; public ArrayInList(final String numericType, final String otherType) { this.numericType = numericType; this.otherType = otherType; } public ArrayInList() { this(JdbcMapper.DatabaseType.ANY.arrayNumberTypeName, JdbcMapper.DatabaseType.ANY.arrayStringTypeName); } @Override public InList instance(Connection conn) { return this; } protected String columnAppendIn(final String columnName) { return "(" + columnName + " = ANY(?))"; } protected String columnAppendNotIn(final String columnName) { return "(NOT(" + columnName + " = ANY(?)))"; } public Array toArray(final Connection conn, final String typeName, final Object[] elements) throws SQLException { return conn.createArrayOf(typeName, elements); } public Array toArray(final Connection conn, final boolean numeric, final Object[] elements) throws SQLException { return toArray(conn, numeric ? numericType : otherType, elements); } protected Array toArray(final Connection conn, final Collection values) throws SQLException { return toArray(conn, values.iterator().next() instanceof Number, values.toArray()); } public InListObject inList(final Connection conn, final String columnName, final Collection values) throws SQLException { return values == null || values.isEmpty() ? InListObject.inEmpty : new ArrayListObject( columnAppendIn(columnName), toArray(conn, values) ); } public InListObject notInList(final Connection conn, final String columnName, final Collection values) throws SQLException { return values == null || values.isEmpty() ? InListObject.notInEmpty : new ArrayListObject( columnAppendNotIn(columnName), toArray(conn, values) ); } class ArrayListObject extends InListObject { private final Array array; public ArrayListObject(final String sql, final Array array) { super(sql); this.array = array; } public Array getArray() { return array; } } }