2015-04-29 12:03:35 -04:00
|
|
|
package com.moparisthebest.jdbc;
|
|
|
|
|
2018-05-20 12:51:37 -04:00
|
|
|
import com.moparisthebest.jdbc.codegen.JdbcMapper;
|
|
|
|
|
2015-04-29 12:03:35 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-06 01:04:44 -05:00
|
|
|
protected final String numericType, otherType;
|
|
|
|
|
|
|
|
public ArrayInList(final String numericType, final String otherType) {
|
|
|
|
this.numericType = numericType;
|
|
|
|
this.otherType = otherType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayInList() {
|
2018-05-22 00:11:54 -04:00
|
|
|
this(JdbcMapper.DatabaseType.ANY.arrayNumberTypeName, JdbcMapper.DatabaseType.ANY.arrayStringTypeName);
|
2015-04-29 12:03:35 -04:00
|
|
|
}
|
|
|
|
|
2018-05-23 23:41:19 -04:00
|
|
|
@Override
|
|
|
|
public InList instance(Connection conn) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-05-22 23:09:57 -04:00
|
|
|
protected String columnAppendIn(final String columnName) {
|
2015-05-01 09:45:51 -04:00
|
|
|
return "(" + columnName + " = ANY(?))";
|
2015-04-29 12:03:35 -04:00
|
|
|
}
|
|
|
|
|
2018-05-22 23:09:57 -04:00
|
|
|
protected String columnAppendNotIn(final String columnName) {
|
2018-05-23 00:45:22 -04:00
|
|
|
return "(NOT(" + columnName + " = ANY(?)))";
|
2018-05-22 23:09:57 -04:00
|
|
|
}
|
|
|
|
|
2018-03-06 01:04:44 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-04-29 12:03:35 -04:00
|
|
|
protected <T> Array toArray(final Connection conn, final Collection<T> values) throws SQLException {
|
2018-03-06 01:04:44 -05:00
|
|
|
return toArray(conn, values.iterator().next() instanceof Number, values.toArray());
|
2015-04-29 12:03:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public <T> InListObject inList(final Connection conn, final String columnName, final Collection<T> values) throws SQLException {
|
2018-05-22 23:09:57 -04:00
|
|
|
return values == null || values.isEmpty() ? InListObject.inEmpty : new ArrayListObject(
|
|
|
|
columnAppendIn(columnName),
|
|
|
|
toArray(conn, values)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public <T> InListObject notInList(final Connection conn, final String columnName, final Collection<T> values) throws SQLException {
|
|
|
|
return values == null || values.isEmpty() ? InListObject.notInEmpty : new ArrayListObject(
|
|
|
|
columnAppendNotIn(columnName),
|
2015-04-29 12:03:35 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|