📄 sqltimestamp.java
字号:
/* Derby - Class org.apache.derby.iapi.types.SQLTimestamp Copyright 2001, 2004 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package org.apache.derby.iapi.types;import org.apache.derby.iapi.reference.SQLState;import org.apache.derby.iapi.services.io.ArrayInputStream;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.db.DatabaseContext;import org.apache.derby.iapi.types.DataValueDescriptor;import org.apache.derby.iapi.types.TypeId;import org.apache.derby.iapi.types.NumberDataValue;import org.apache.derby.iapi.types.DateTimeDataValue;import org.apache.derby.iapi.services.io.StoredFormatIds;import org.apache.derby.iapi.services.context.ContextService; import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.types.DataType;import org.apache.derby.iapi.services.i18n.LocaleFinder;import org.apache.derby.iapi.services.cache.ClassSize;import org.apache.derby.iapi.util.StringUtil;import org.apache.derby.iapi.util.ReuseFactory;import org.apache.derby.iapi.types.SQLDouble;import org.apache.derby.iapi.types.SQLTime;import java.sql.Date;import java.sql.Time;import java.sql.Timestamp;import java.sql.Types;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.PreparedStatement;import java.util.Calendar;import java.util.GregorianCalendar;import java.io.ObjectOutput;import java.io.ObjectInput;import java.io.IOException;import java.text.DateFormat;import java.text.ParseException;/** * This contains an instance of a SQL Timestamp object. * <p> * SQLTimestamp is stored in 3 ints - an encoded date, an encoded time and * nanoseconds * encodedDate = 0 indicates a null WSCTimestamp * * SQLTimestamp is similar to SQLTimestamp, but it does conserves space by not keeping a GregorianCalendar object * * PERFORMANCE OPTIMIZATION: * We only instantiate the value field when required due to the overhead of the * Date methods. * Thus, use isNull() instead of "value == null" and * getTimestamp() instead of using value directly. */public final class SQLTimestamp extends DataType implements DateTimeDataValue{ static final int MAX_FRACTION_DIGITS = 6; // Only microsecond resolution on conversion to/from strings static final int FRACTION_TO_NANO = 1000; // 10**(9 - MAX_FRACTION_DIGITS) static final int ONE_BILLION = 1000000000; private int encodedDate; private int encodedTime; private int nanos; // The cached value.toString() private String valueString; /* ** DataValueDescriptor interface ** (mostly implemented in DataType) */ private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( SQLTimestamp.class); public int estimateMemoryUsage() { int sz = BASE_MEMORY_USAGE + ClassSize.estimateMemoryUsage( valueString); return sz; } // end of estimateMemoryUsage public String getString() { if (!isNull()) { if (valueString == null) { valueString = getTimestamp((Calendar) null).toString(); /* The java.sql.Timestamp.toString() method is supposed to return a string in * the JDBC escape format. However the JDK 1.3 libraries truncate leading zeros from * the year. This is not acceptable to DB2. So add leading zeros if necessary. */ int separatorIdx = valueString.indexOf( '-'); if( separatorIdx >= 0 && separatorIdx < 4) { StringBuffer sb = new StringBuffer(); for( ; separatorIdx < 4; separatorIdx++) sb.append('0'); sb.append( valueString); valueString = sb.toString(); } } return valueString; } else { if (SanityManager.DEBUG) { if (valueString != null) { SanityManager.THROWASSERT( "valueString expected to be null, not " + valueString); } } return null; } } /** getDate returns the date portion of the timestamp Time is set to 00:00:00.0 Since Date is a JDBC object we use the JDBC definition for the time portion. See JDBC API Tutorial, 47.3.12. @exception StandardException thrown on failure */ public Date getDate( Calendar cal) throws StandardException { if (isNull()) return null; return newDate(cal); } private Date newDate(java.util.Calendar cal) throws StandardException { if( cal == null) cal = new GregorianCalendar(); cal.set(Calendar.YEAR, SQLDate.getYear(encodedDate) ); cal.set(Calendar.MONTH, SQLDate.getMonth(encodedDate)-1); cal.set(Calendar.DATE, SQLDate.getDay(encodedDate) ); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return new Date(cal.getTime().getTime()); } /** getTime returns the time portion of the timestamp Date is set to 1970-01-01 Since Time is a JDBC object we use the JDBC definition for the date portion. See JDBC API Tutorial, 47.3.12. @exception StandardException thrown on failure */ public Time getTime( Calendar cal) throws StandardException { if (isNull()) return null; return newTime(cal); } private Time newTime(java.util.Calendar cal) throws StandardException { if( cal == null) cal = new GregorianCalendar(); cal.set(Calendar.YEAR, 1970); cal.set(Calendar.MONTH, Calendar.JANUARY); cal.set(Calendar.DATE, 1); cal.set(Calendar.HOUR_OF_DAY, SQLTime.getHour(encodedTime)); cal.set(Calendar.MINUTE, SQLTime.getMinute(encodedTime)); cal.set(Calendar.SECOND, SQLTime.getSecond(encodedTime)); cal.set(Calendar.MILLISECOND, (int)(nanos/1E06)); return new Time(cal.getTime().getTime()); } public Object getObject() { return getTimestamp((Calendar) null); } /* get storage length */ public int getLength() { return 12; } /* this is for DataType's error generator */ public String getTypeName() { return "TIMESTAMP"; } /* * Storable interface, implies Externalizable, TypedFormat */ /** Return my format identifier. @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId */ public int getTypeFormatId() { return StoredFormatIds.SQL_TIMESTAMP_ID; } /** @exception IOException error writing data */ public void writeExternal(ObjectOutput out) throws IOException { if (SanityManager.DEBUG) SanityManager.ASSERT(!isNull(), "writeExternal() is not supposed to be called for null values."); /* ** Timestamp is written out 3 ints, encoded date, encoded time, and ** nanoseconds */ out.writeInt(encodedDate); out.writeInt(encodedTime); out.writeInt(nanos); } /** * @see java.io.Externalizable#readExternal * * @exception IOException Thrown on error reading the object */ public void readExternal(ObjectInput in) throws IOException { encodedDate = in.readInt(); encodedTime = in.readInt(); nanos = in.readInt(); // reset cached values valueString = null; } public void readExternalFromArray(ArrayInputStream in) throws IOException { encodedDate = in.readInt(); encodedTime = in.readInt(); nanos = in.readInt(); // reset cached values valueString = null; } /* * DataValueDescriptor interface */ /** @see DataValueDescriptor#getClone */ public DataValueDescriptor getClone() { // Call constructor with all of our info return new SQLTimestamp(encodedDate, encodedTime, nanos); } /** * @see DataValueDescriptor#getNewNull */ public DataValueDescriptor getNewNull() { return new SQLTimestamp(); } /** * @see org.apache.derby.iapi.services.io.Storable#restoreToNull * */ public void restoreToNull() { // clear numeric representation encodedDate = 0; encodedTime = 0; nanos = 0; // clear cached valueString valueString = null; } /* * DataValueDescriptor interface */ /** * @see DataValueDescriptor#setValueFromResultSet * * @exception SQLException Thrown on error */ public void setValueFromResultSet(ResultSet resultSet, int colNumber, boolean isNullable) throws SQLException, StandardException { setValue(resultSet.getTimestamp(colNumber), (Calendar) null); } public int compare(DataValueDescriptor other) throws StandardException { /* Use compare method from dominant type, negating result * to reflect flipping of sides. */ if (typePrecedence() < other.typePrecedence()) { return - (other.compare(this)); } boolean thisNull, otherNull; thisNull = this.isNull(); otherNull = other.isNull(); /* * thisNull otherNull return * T T 0 (this == other) * F T -1 (this < other) * T F 1 (this > other) */ if (thisNull || otherNull) { if (!thisNull) // otherNull must be true return -1; if (!otherNull) // thisNull must be true return 1; return 0; } /* Neither are null compare them */ int comparison; /* get the comparison date values */ int otherEncodedDate = 0; int otherEncodedTime = 0; int otherNanos = 0; /* if the argument is another SQLTimestamp, look up the value */ if (other instanceof SQLTimestamp) { SQLTimestamp st = (SQLTimestamp)other; otherEncodedDate= st.encodedDate; otherEncodedTime= st.encodedTime; otherNanos= st.nanos; } else { /* O.K. have to do it the hard way and calculate the numeric value * from the value */ Calendar cal = new GregorianCalendar(); Timestamp otherts = other.getTimestamp(cal); otherEncodedDate = SQLTimestamp.computeEncodedDate(otherts, cal); otherEncodedTime = SQLTimestamp.computeEncodedTime(otherts, cal); otherNanos = otherts.getNanos(); } if (encodedDate < otherEncodedDate) comparison = -1; else if (encodedDate > otherEncodedDate) comparison = 1; else if (encodedTime < otherEncodedTime) comparison = -1; else if (encodedTime > otherEncodedTime) comparison = 1; else if (nanos < otherNanos) comparison = -1; else if (nanos > otherNanos) comparison = 1; else comparison = 0; return comparison; } /** @exception StandardException thrown on error */ public boolean compare(int op, DataValueDescriptor other, boolean orderedNulls, boolean unknownRV) throws StandardException { if (!orderedNulls) // nulls are unordered { if (this.isNull() || ((DataValueDescriptor)other).isNull()) return unknownRV; } /* Do the comparison */ return super.compare(op, other, orderedNulls, unknownRV); } /* ** Class interface */ /* ** Constructors */ /** no-arg constructor required by Formattable */ public SQLTimestamp() { } public SQLTimestamp(Timestamp value) throws StandardException { setValue(value, (Calendar) null); } SQLTimestamp(int encodedDate, int encodedTime, int nanos) { this.encodedDate = encodedDate; this.encodedTime = encodedTime; this.nanos = nanos; } public SQLTimestamp( DataValueDescriptor date, DataValueDescriptor time) throws StandardException { Calendar cal = null; if( date == null || date.isNull() || time == null || time.isNull()) return; if( date instanceof SQLDate)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -