All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.moparisthebest.jdbc.ArrayInList Maven / Gradle / Ivy

The newest version!
package com.moparisthebest.jdbc;

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 ArrayInList() {
	}

	protected String columnAppend(final String columnName) {
		return "(" + columnName + " = ANY(?))";
	}

	protected  Array toArray(final Connection conn, final Collection values) throws SQLException {
		return conn.createArrayOf(
				values.iterator().next() instanceof Number ? "number" : "text",
				values.toArray()
		);
	}

	public  InListObject inList(final Connection conn, final String columnName, final Collection values) throws SQLException {
		return values == null || values.isEmpty() ? InListObject.empty : new ArrayListObject(
				columnAppend(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;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy