VerticaYearMonthInterval
-
java.lang.Object
-
com.vertica.dsi.dataengine.utilities.DSIMonthSpan
-
com.vertica.jdbc.VerticaYearMonthInterval
-
All Implemented Interfaces: java.lang.Cloneable
public class VerticaYearMonthInterval
extends com.vertica.dsi.dataengine.utilities.DSIMonthSpan
implements java.lang.Cloneable
The JDBC representation of the Vertica INTERVAL YEAR TO MONTH type. It represents a span of time measured in years and months.
The JDBC specification does not define first class support for interval data types. There are no java.sql.Types type identifiers for intervals, there is no built in setter on the PreparedStatement interface and no built in getter on the ResultSet interface.
Instead, the Vertica JDBC driver follows the ODBC standard when it can. Each of the 3 year/month interval ranges is identified by the ODBC standard SQL type identifier. Those identifiers are recreated as integer fields in this class, analogous to those defined for the other data types by java.sql.Types.
To insert a year/month interval into the database, create an instance of this class and pass it to PreparedStatement.setObject, optionally passing one of the ODBC SQL types defined by this class as the target type parameter.
To retrieve a VericaYearMonthInterval from the database, select an INTERVAL YEAR TO MONTH column, call ResultSet.getObject and cast the result to a VerticaYearMonthInterval
Field Summary
| Modifier and Type | Field and Description |
|---|---|
| static int | INTERVAL_MONTH Deprecated. superseded by VerticaTypes.INTERVAL_MONTH |
| static int | INTERVAL_YEAR Deprecated. superseded by VerticaTypes.INTERVAL_YEAR |
| static int | INTERVAL_YEAR_TO_MONTH Deprecated. superseded by VerticaTypes.INTERVAL_YEAR_TO_MONTH |
Constructor Summary
| Constructor and Description |
|---|
| VerticaYearMonthInterval(int year, int month, boolean isNegative) Constructs a new VerticaYearMonthInterval. |
| VerticaYearMonthInterval(int type, int year, int month, boolean isNegative) Constructs a new VerticaYearMonthInterval. |
Method Summary
| Modifier and Type | Method and Description |
|---|---|
| VerticaYearMonthInterval | clone() Returns a deep copy of this VerticaYearMonthInterval. |
| boolean | equals(java.lang.Object obj) Returns true if obj is a VerticaYearMonthInterval and it represents the same span of time as this interval, regardless of which units are set. |
| int | getIntervalType() Gets the ODBC SQL type identifier for this interval. |
| int | hashCode() The hash code of an interval is calculated first by converting this interval to a number of months. |
| static boolean | isYearMonthInterval(int type) A helper method to determine if the provided ODBC interval subtype identifier corresponds to one of the 3 year/month intervals that this class is capable of representing. |
| void | setMonth(int month) Sets the months component of this interval. |
| void | setYear(int year) Sets the years component of this interval. |
| java.lang.String | toJsonString() Returns a string representation of this interval that matches the json format sent by the server for complex types. |
| java.lang.String | toString() Returns a string representation of this interval. |
Methods inherited from class com.vertica.dsi.dataengine.utilities.DSIMonthSpan
getMonth, getYear, isNegative, setIsNegative
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
Field Detail
INTERVAL_YEAR
public static final int INTERVAL_YEAR
Deprecated. superseded by VerticaTypes.INTERVAL_YEAR The ODBC SQL type identifier for an INTERVAL YEAR interval. See Also:Constant Field Values
INTERVAL_YEAR_TO_MONTH
public static final int INTERVAL_YEAR_TO_MONTH
Deprecated. superseded by VerticaTypes.INTERVAL_YEAR_TO_MONTH The ODBC SQL type identifier for an INTERVAL YEAR TO MONTH interval. See Also:Constant Field Values
INTERVAL_MONTH
public static final int INTERVAL_MONTH
Deprecated. superseded by VerticaTypes.INTERVAL_MONTH The ODBC SQL type identifier for an INTERVAL MONTH interval. See Also:Constant Field Values
Constructor Detail
VerticaYearMonthInterval
public VerticaYearMonthInterval(int year,
int month,
boolean isNegative)
Constructs a new VerticaYearMonthInterval. The type is assumed to be INTERVAL_YEAR_TO_MONTH. Parameters:year - The number of years in this interval. Must be no less than zero.month - The number of months in this interval. Must be no less than zero.isNegative - True if this interval is negative, false if positive. Throws: java.lang.IllegalArgumentException - If year or month are negative.
VerticaYearMonthInterval
public VerticaYearMonthInterval(int type,
int year,
int month,
boolean isNegative)
Constructs a new VerticaYearMonthInterval. Parameters:type - The ODBC SQL type identifier for this interval.year - The number of years in this interval. Must be no less than zero.month - The number of months in this interval. Must be no less than zero.isNegative - True if this interval is negative, false if positive. Throws: java.lang.IllegalArgumentException - If year or month are negative, or if type is not one of the ODBC SQL type identifiers defined by this class.
Method Detail
setYear
public void setYear(int year)
Sets the years component of this interval. If this interval has no year component, it will be converted to a number of months.
Overrides setYear in class com.vertica.dsi.dataengine.utilities.DSIMonthSpan Parameters:year - A positive number of years. Throws: java.lang.IllegalArgumentException - If year is negative, or if using this many years would cause the interval to overflow.
setMonth
public void setMonth(int month)
Sets the months component of this interval. If this interval has no month component, it will be converted to a number of years. Any fraction on the converted years value is rounded down.
Overrides setMonth in class com.vertica.dsi.dataengine.utilities.DSIMonthSpan Parameters:month - A positive number of months. Throws: java.lang.IllegalArgumentException - If month is negative, or if using this many months would cause the interval to overflow.
hashCode
public int hashCode()
The hash code of an interval is calculated first by converting this interval to a number of months. Since the hash code must fit in an integer, it is possible that this calculation will overflow. Regardless of whether or not there was overflow, this number is then multiplied by -1 if this interval negative.
Overrides hashCode in class com.vertica.dsi.dataengine.utilities.DSIMonthSpan Returns A hash code for this interval.
equals
public boolean equals(java.lang.Object obj)
Returns true if obj is a VerticaYearMonthInterval and it represents the same span of time as this interval, regardless of which units are set. For example, a SQL INTERVAL YEAR of 1 year is equal to an INTERVAL YEAR TO MONTH of 1 year, 0 months, and an INTERVAL MONTH of 12 months.
Overrides equals in class com.vertica.dsi.dataengine.utilities.DSIMonthSpan Parameters:obj - An object to compare. Returns true if obj represents the same span of time as this interval.
clone
public VerticaYearMonthInterval clone()
Returns a deep copy of this VerticaYearMonthInterval. Equivalent to calling new VerticaYearMonthInterval(getIntervalType(), getYear(), getMonth(), isNegative())
Overrides clone in class com.vertica.dsi.dataengine.utilities.DSIMonthSpan Returns A copy of this interval.
toString
public java.lang.String toString()
Returns a string representation of this interval. The format of this string is the same format that is recognized by the driver and database. Fields smaller than this interval's range (as defined by the ODBC type identifier) are not included in the String.
Overrides toString in class com.vertica.dsi.dataengine.utilities.DSIMonthSpan Returns The string representation of this interval.
toJsonString
public java.lang.String toJsonString()
Returns a string representation of this interval that matches the json format sent by the server for complex types. Returns The json string representation of this interval.
getIntervalType
public int getIntervalType()
Gets the ODBC SQL type identifier for this interval. An interval's range can be determined by comparing the return value of this method with the INTERVAL_* constants defined in this class.
Overrides getIntervalType in class com.vertica.dsi.dataengine.utilities.DSIMonthSpan Returns The ODBC SQL type identifier for this interval.
isYearMonthInterval
public static boolean isYearMonthInterval(int type)
A helper method to determine if the provided ODBC interval subtype identifier corresponds to one of the 3 year/month intervals that this class is capable of representing.
The SQL_DATETIME_SUB column of the ResultSet returned by DatabaseMetaData.getColumns() will contain an ODBC interval subtype when the column is an interval. When this happens, the object returned by the driver when selecting that column will be a VerticaYearMonthInterval when the subtype is one of the 3 interval subtypes defined in this class. This helper method checks the provided type to see if it matches one of those 3 types. Parameters:type - An ODBC interval subtype identifier. Returns true If type corresponds to one of the 3 year/month interval types representable by a VerticaYearMonthInterval.