001package com.identityworksllc.iiq.common.query; 002 003import sailpoint.object.SailPointObject; 004 005import java.sql.PreparedStatement; 006import java.sql.SQLException; 007import java.sql.Timestamp; 008import java.sql.Types; 009import java.util.Date; 010 011/** 012 * Utility class for setting the parameters on a query based on the input, 013 * invoking the appropriately-typed input values. 014 */ 015public class Parameters { 016 /** 017 * Sets up the parameter in the given PreparedStatement per the type 018 * of the input. 019 * 020 * @param stmt The PreparedStatement to wire up 021 * @param param The parameter number to wire up 022 * @param value The value of the parameter 023 * @throws SQLException on any database failures 024 */ 025 private static void setupParameter(PreparedStatement stmt, int param, Object value) throws SQLException { 026 if (value == null) { 027 stmt.setNull(param, Types.VARCHAR); 028 } else if (value instanceof TimestampWithTimezone) { 029 stmt.setTimestamp(param, (Timestamp) value, ((TimestampWithTimezone) value).getZonedCalendar()); 030 } else if (value instanceof Timestamp) { 031 stmt.setTimestamp(param, (Timestamp) value); 032 } else if (value instanceof Date) { 033 stmt.setDate(param, new java.sql.Date(((Date) value).getTime())); 034 } else if (value instanceof Long) { 035 stmt.setLong(param, (Long) value); 036 } else if (value instanceof Integer) { 037 stmt.setInt(param, (Integer) value); 038 } else if (value instanceof SailPointObject) { 039 stmt.setString(param, ((SailPointObject) value).getId()); 040 } else { 041 stmt.setString(param, String.valueOf(value)); 042 } 043 } 044 045 /** 046 * Set up the given parameters in the prepared statmeent 047 * @param stmt The statement 048 * @param parameters The parameters 049 * @throws SQLException if any failures occur setting parameters 050 */ 051 public static void setupParameters(PreparedStatement stmt, Object[] parameters) throws SQLException { 052 if (parameters == null || parameters.length == 0) { 053 return; 054 } 055 int param = 1; 056 for (Object parameter : parameters) { 057 setupParameter(stmt, param, parameter); 058 param++; 059 } 060 } 061 062 /** 063 * Private utility constructor 064 */ 065 private Parameters() { 066 067 } 068}