Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Public Firebird Java API.
   *
   * Redistribution and use in source and binary forms, with or without 
   * modification, are permitted provided that the following conditions are met:
   *    1. Redistributions of source code must retain the above copyright notice, 
   *       this list of conditions and the following disclaimer.
   *    2. Redistributions in binary form must reproduce the above copyright 
   *       notice, this list of conditions and the following disclaimer in the 
  *       documentation and/or other materials provided with the distribution. 
  *    3. The name of the author may not be used to endorse or promote products 
  *       derived from this software without specific prior written permission.
  * 
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 
  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
 /*
  * The Original Code is the Firebird Java GDS implementation.
  *
  * The Initial Developer of the Original Code is Alejandro Alberola.
  * Portions created by Alejandro Alberola are Copyright (C) 2001
  * Boix i Oltra, S.L. All Rights Reserved.
  *
  */
 package org.firebirdsql.gds;
 
 import java.sql.Date;
 import java.sql.Time;
 
The class XSQLDA is a java mapping of the XSQLVAR server data structure used to represent one column for input and output.

Author(s):
Alejandro Alberola
Version:
1.0
 
 public class XSQLVAR {
     
     public int sqltype;
     public int sqlscale;
     public int sqlsubtype;
     public int sqllen;
     public byte[] sqldata;
     public Object cachedobject;
 //    public int sqlind;
     public String sqlname;
     public String relname;
     public String relaliasname;
     public String ownname;
     public String aliasname;
 
     protected Encoding coder;
 
     public XSQLVAR() {
     }
    
    
Get a deep copy of this object.

Returns:
deep copy of this object.
 
     public XSQLVAR deepCopy() {
         XSQLVAR result = new XSQLVAR();
         result.copyFrom(this);
         return result;
     }
    
    
Copy constructor. Initialize this instance of XSQLVAR with values from another instance.

Parameters:
original The other instance of XSQLVAR to be used as the base for initializing this instance
 
     public void copyFrom(XSQLVAR original) {
         copyFrom(originaltrue);
     }
    
    
Copy constructor. Initialize this instance of XSQLVAR with values from another instance.

Parameters:
original The other instance of XSQLVAR to be used as the base for initializing this instance
    public void copyFrom(XSQLVAR originalboolean copyData) {
        this. = original.sqltype;
        this. = original.sqlscale;
        this. = original.sqlsubtype;
        this. = original.sqllen;
        
        if (original.sqldata != null && copyData) {
            this. = new byte[original.sqldata.length]; 
            System.arraycopy(original.sqldata, 0, this., 0, original.sqldata.length);
        } else
            this. = null;
        
        this. = original.sqlname;
        this. = original.relname;
        this. = original.relaliasname;
        this. = original.ownname;
        this. = original.aliasname;
    }
    // numbers

    
Encode a short value as a byte array.

Parameters:
value The value to be encoded
Returns:
The value of value encoded as a byte array
    public byte[] encodeShort(short value){
        return encodeInt(value);
    }

    
Decode a byte array into a short value.

Parameters:
byte_int The byte array to be decoded
Returns:
The short value of the decoded byte array
    public short decodeShort(byte[] byte_int){
        return (shortdecodeInt(byte_int);		 
    }

    
Encode an int value as a byte array.

Parameters:
value The value to be encoded
Returns:
The value of value encoded as a byte array
    public byte[] encodeInt(int value){
        byte ret[] = new byte[4];
        ret[0] = (byte) ((value >>> 24) & 0xff);
        ret[1] = (byte) ((value >>> 16) & 0xff);
        ret[2] = (byte) ((value >>> 8) & 0xff);
        ret[3] = (byte) ((value) & 0xff);
        return ret;
    }

    
Decode a byte array into an int value.

Parameters:
byte_int The byte array to be decoded
Returns:
The int value of the decoded byte array
    public int decodeInt(byte[] byte_int){
        int b1 = byte_int[0]&0xFF;
        int b2 = byte_int[1]&0xFF;
        int b3 = byte_int[2]&0xFF;
        int b4 = byte_int[3]&0xFF;
        return ((b1 << 24) + (b2 << 16) + (b3 << 8) + (b4));
    }

    
Encode a long value as a byte array.

Parameters:
value The value to be encoded
Returns:
The value of value encoded as a byte array
    public  byte[] encodeLong(long value){
        byte[] ret = new byte[8];
        ret[0] = (byte) (value >>> 56 & 0xFF);
        ret[1] = (byte) (value >>> 48 & 0xFF);
        ret[2] = (byte) (value >>> 40 & 0xFF);
        ret[3] = (byte) (value >>> 32 & 0xFF);
        ret[4] = (byte) (value >>> 24 & 0xFF);
        ret[5] = (byte) (value >>> 16 & 0xFF);
        ret[6] = (byte) (value >>>  8 & 0xFF);
        ret[7] = (byte) (value & 0xFF);
        return ret;
    }


    
Decode a byte array into a long value.

Parameters:
byte_int The byte array to be decoded
Returns:
The long value of the decoded byte array
    public  long decodeLong(byte[] byte_int){
        long b1 = byte_int[0]&0xFF;
        long b2 = byte_int[1]&0xFF;
        long b3 = byte_int[2]&0xFF;
        long b4 = byte_int[3]&0xFF;
        long b5 = byte_int[4]&0xFF;
        long b6 = byte_int[5]&0xFF;
        long b7 = byte_int[6]&0xFF;
        long b8 = byte_int[7]&0xFF;
        return ((b1 << 56) + (b2 << 48) + (b3 << 40) + (b4 << 32) 
        + (b5 << 24) + (b6 << 16) + (b7 << 8) + (b8));
    }

    
Encode a float value as a byte array.

Parameters:
value The value to be encoded
Returns:
The value of value encoded as a byte array
    public byte[] encodeFloat(float value){
        return encodeInt(Float.floatToIntBits(value));
    }

    
Decode a byte array into a float value.

Parameters:
byte_int The byte array to be decoded
Returns:
The float value of the decoded byte array
    public float decodeFloat(byte[] byte_int){
        return Float.intBitsToFloat(decodeInt(byte_int));
    }

    
Encode a double value as a byte array.

Parameters:
value The value to be encoded
Returns:
The value of value encoded as a byte array
    public byte[] encodeDouble(double value){
        return encodeLong(Double.doubleToLongBits(value));
    }

    
Decode a byte array into a double value.

Parameters:
byte_int The byte array to be decoded
Returns:
The double value of the decoded byte array
    public double decodeDouble(byte[] byte_int){
        return Double.longBitsToDouble(decodeLong(byte_int));
    }
    // Strings with mapping

    
Encode a String value into a byte array using a given encoding.

Parameters:
value The String to be encoded
encoding The encoding to use in the encoding process
mappingPath The character mapping path to be used in the encoding
Returns:
The value of value as a byte array
Throws:
java.sql.SQLException if the given encoding cannot be found, or an error occurs during the encoding
    public byte[] encodeString(String valueString encodingString mappingPaththrows SQLException {
        if ( == null)
             = EncodingFactory.getEncoding(encodingmappingPath);
        return .encodeToCharset(value);
    }

    
Encode a byte array using a given encoding.

Parameters:
value The byte array to be encoded
encoding The encoding to use in the encoding process
mappingPath The character mapping path to be used in the encoding
Returns:
The value of value encoded using the given encoding
Throws:
java.sql.SQLException if the given encoding cannot be found, or an error occurs during the encoding
    public byte[] encodeString(byte[] valueString encodingString mappingPath)throws SQLException {
        if (encoding == null)
            return value;
        else {
            if ( == null)
                 = EncodingFactory.getEncoding(encodingmappingPath);
            return .encodeToCharset(.decodeFromCharset(value));
        }
    }

    
Decode an encoded byte array into a String using a given encoding.

Parameters:
value The value to be decoded
encoding The encoding to be used in the decoding process
mappingPath The character mapping path to be used in the decoding
Returns:
The decoded String
Throws:
java.sql.SQLException if the given encoding cannot be found, or an error occurs during the decoding
    public String decodeString(byte[] valueString encodingString mappingPaththrows SQLException{
        if ( == null)
             = EncodingFactory.getEncoding(encodingmappingPath);
        return .decodeFromCharset(value);
    }
    
    // times,dates...
   
    
Encode a Timestamp using a given Calendar.

Parameters:
value The Timestamp to be encoded
cal The Calendar to be used for encoding, may be null
    public Timestamp encodeTimestamp(java.sql.Timestamp valueCalendar cal){
        return encodeTimestamp(valuecalfalse);
    }
    
    
Encode a Timestamp using a given Calendar.

Parameters:
value The Timestamp to be encoded
cal The Calendar to be used for encoding, may be null
invertTimeZone If true, the timezone offset value will be subtracted from the encoded value, otherwise it will be added
Returns:
The encoded Timestamp
    public Timestamp encodeTimestamp(java.sql.Timestamp valueCalendar calboolean invertTimeZone){
        if (cal == null) {
            return value;
        }
        else {
            long time = value.getTime() + 
                (invertTimeZone ? -1 : 1) * (cal.getTimeZone().getRawOffset() - 
                Calendar.getInstance().getTimeZone().getRawOffset());
            
            return new Timestamp(time);
        }
    }

    
Encode a Timestamp as a byte array.

Parameters:
value The Timestamp to be encoded
Returns:
The array of bytes that represents the given Timestamp value
    public byte[] encodeTimestamp(Timestamp value){
    	return encodeTimestampCalendar(valuenew GregorianCalendar());
    }
    	
	public byte[] encodeTimestampCalendar(Timestamp valueCalendar c){
        /* note, we cannot simply pass millis to the database, because
         * Firebird stores timestamp in format (citing Ann W. Harrison):
         * 
         * "[timestamp is] stored a two long words, one representing
         * the number of days since 17 Nov 1858 and one representing number
         * of 100 nano-seconds since midnight" (NOTE: It is actually 100 microseconds!)
         */
        datetime d = new datetime(valuec);
        byte[] date = d.toDateBytes();
        byte[] time = d.toTimeBytes();
        byte[] result = new byte[8];
        System.arraycopy(date, 0, result, 0, 4);
        System.arraycopy(time, 0, result, 4, 4);
        return result;
    }

    
Decode a Timestamp value using a given Calendar.

Parameters:
value The Timestamp to be decoded
cal The Calendar to be used in decoding, may be null
Returns:
The decoded Timestamp
    public java.sql.Timestamp decodeTimestamp(Timestamp valueCalendar cal) {
        return decodeTimestamp(valuecalfalse);
    }

    
Decode a Timestamp value using a given Calendar.

Parameters:
value The Timestamp to be decoded
cal The Calendar to be used in decoding, may be null
invertTimeZone If true, the timezone offset value will be subtracted from the decoded value, otherwise it will be added
Returns:
The encoded Timestamp
    public java.sql.Timestamp decodeTimestamp(Timestamp valueCalendar calboolean invertTimeZone){
        if (cal == null) {
            return value;
        }
        else {
            long time = value.getTime() - 
                (invertTimeZone ? -1 : 1) * (cal.getTimeZone().getRawOffset() - 
                 Calendar.getInstance().getTimeZone().getRawOffset());
            
            return new Timestamp(time);
        }
    }

    
Decode a byte array into a Timestamp.

Parameters:
byte_int The byte array to be decoded
Returns:
A Timestamp value from the decoded bytes
    public Timestamp decodeTimestamp(byte[] byte_int){
    	return decodeTimestampCalendar(byte_intnew GregorianCalendar());
    }
    
	public Timestamp decodeTimestampCalendar(byte[] byte_intCalendar c){
        
        if (byte_int.length != 8)
            throw new IllegalArgumentException("Bad parameter to decode");
        /* we have to extract time and date correctly
         * see encodeTimestamp(...) for explanations
         */
        byte[] date = new byte[4];
        byte[] time = new byte[4];
        
        System.arraycopy(byte_int, 0, date, 0, 4);
        System.arraycopy(byte_int, 4, time, 0, 4);
        datetime d = new datetime(date,time);
        return d.toTimestamp(c);
    }

    
Encode a given Time value using a given Calendar.

Parameters:
d The Time to be encoded
cal The Calendar to be used in the encoding, may be null
Returns:
The encoded Time
    public java.sql.Time encodeTime(Time dCalendar calboolean invertTimeZone) {
        if (cal == null) {
            return d;
        }
        else {
            long time = d.getTime() + 
            (invertTimeZone ? -1 : 1) * (cal.getTimeZone().getRawOffset() - 
            Calendar.getInstance().getTimeZone().getRawOffset());
        
            return new Time(time);
        }
    }

    
Encode a Time value into a byte array.

Parameters:
d The Time to be encoded
Returns:
The array of bytes representing the given Time
    public byte[] encodeTime(Time d) {
    	return encodeTimeCalendar(dnew GregorianCalendar());
    }
    	
	public byte[] encodeTimeCalendar(Time dCalendar c) {
        datetime dt = new datetime(dc);
        return dt.toTimeBytes();
    }

    
Decode a Time value using a given Calendar.

Parameters:
d The Time to be decoded
cal The Calendar to be used in the decoding, may be null
Returns:
The decooded Time
    public java.sql.Time decodeTime(java.sql.Time dCalendar calboolean invertTimeZone) {
        if (cal == null) {
            return d;
        }
        else {
            long time = d.getTime() - 
            (invertTimeZone ? -1 : 1) * (cal.getTimeZone().getRawOffset() - 
             Calendar.getInstance().getTimeZone().getRawOffset());
        
            return new Time(time);
        }
    }

    
Decode a byte array into a Time value.

Parameters:
int_byte The byte array to be decoded
Returns:
The decoded Time
    public Time decodeTime(byte[] int_byte) {
    	return decodeTimeCalendar(int_bytenew GregorianCalendar());
    }
    
	public Time decodeTimeCalendar(byte[] int_byteCalendar c) {
        datetime dt = new datetime(null,int_byte);
        return dt.toTime(c);
    }

    
Encode a given Date value using a given Calendar.

Parameters:
d The Date to be encoded
cal The Calendar to be used in the encoding, may be null
Returns:
The encoded Date
    public Date encodeDate(java.sql.Date dCalendar cal) {
        if (cal == null) {
            return (d);
        }
        else {
            cal.setTime(d);
            return new Date(cal.getTime().getTime());
        }
    }

    
Encode a Date value into a byte array.

Parameters:
d The Date to be encoded
Returns:
The array of bytes representing the given Date
    public byte[] encodeDate(Date d) {
    	return encodeDateCalendar(dnew GregorianCalendar());
    }
    
	public byte[] encodeDateCalendar(Date dCalendar c) {
        datetime dt = new datetime(dc);
        return dt.toDateBytes();
    }

    
Decode a Date value using a given Calendar.

Parameters:
d The Date to be decoded
cal The Calendar to be used in the decoding, may be null
Returns:
The decoded Date
    public java.sql.Date decodeDate(Date dCalendar cal) {
        if (cal == null || d == null) {
            return d;
        } 
        else {
            cal.setTime(d);
            return new Date(cal.getTime().getTime());
        }
    }

    
Decode a byte array into a Date value.

Parameters:
byte_int The byte array to be decoded
Returns:
The decoded Date
    public Date decodeDate(byte[] byte_int) {
    	return decodeDateCalendar(byte_intnew GregorianCalendar());
    }
    
	public Date decodeDateCalendar(byte[] byte_intCalendar c) {
       datetime dt = new datetime(byte_intnull);
       return dt.toDate(c);
    }

    
Decode boolean from supplied data.

Parameters:
data (expected) 1 bytes
Returns:
false when 0, true for all other values
    public boolean decodeBoolean(byte[] data) {
        return data[0] != 0;
    }

    
Encodes boolean to 1 byte data.

Parameters:
value Boolean value to encode
Returns:
true as 1, false as 0.
    public byte[] encodeBoolean(boolean value) {
        return new byte[] { (byte) (value ? 1 : 0) };
    }

    
Encodes a java.time.LocalTime equivalent to time bytes.

Parameters:
hour Number of hours (is assumed to be 0..23)
minute Number of minutes (is assumed to be 0..59)
second Number of seconds (is assumed to be 0..59)
nanos Sub-second nanoseconds (actual resolution is 100 microseconds, is assumed to be 0 .. 10^9 - 1 ns)
Returns:
Byte array for time
    public byte[] encodeLocalTime(int hourint minuteint secondint nanos) {
        datetime dt = new datetime(0, 0, 0, hourminutesecondnanos);
        return dt.toTimeBytes();
    }

    
Encodes a java.time.LocalDate equivalent to date bytes.

Parameters:
year Year
month Month (is assumed to be 1..12)
day Day (is assumed to be valid for year and month)
Returns:
Byte array for date
    public byte[] encodeLocalDate(int yearint monthint day) {
        datetime dt = new datetime(yearmonthday, 0, 0, 0, 0);
        return dt.toDateBytes();
    }

    
Encodes a java.time.LocalDateTime equivalent to timestamp bytes.

Parameters:
year Year
month Month (is assumed to be 1..12)
day Day (is assumed to be valid for year and month)
hour Number of hours (is assumed to be 0..23)
minute Number of minutes (is assumed to be 0..59)
second Number of seconds (is assumed to be 0..59)
nanos Sub-second nanoseconds (actual resolution is 100 microseconds, is assumed to be 0 .. 10^9 - 1 ns)
Returns:
Byte array for timestamp
    public byte[] encodeLocalDateTime(int yearint monthint dayint hourint minuteint secondint nanos) {
        datetime dt = new datetime(yearmonthdayhourminutesecondnanos);
        byte[] date = dt.toDateBytes();
        byte[] time = dt.toTimeBytes();
        byte[] result = new byte[8];
        System.arraycopy(date, 0, result, 0, 4);
        System.arraycopy(time, 0, result, 4, 4);
        return result;
    }

    
Helper Class to encode/decode times/dates
    private class datetime{
        private static final int NANOSECONDS_PER_FRACTION = 100 * 1000;
        private static final int FRACTIONS_PER_MILLISECOND = 10;
        private static final int FRACTIONS_PER_SECOND = 1000 * ;
        private static final int FRACTIONS_PER_MINUTE = 60 * ;
        private static final int FRACTIONS_PER_HOUR = 60 * ;
        int year;
        int month;
        int day;
        int hour;
        int minute;
        int second;
        int fractions// Sub-second precision in 100 microseconds
        datetime(int yearint monthint dayint hourint minuteint secondint nanos) {
            this. = year;
            this. = month;
            this. = day;
            this. = hour;
            this. = minute;
            this. = second;
             = (nanos / ) % ;
        }
        datetime(Timestamp valueCalendar cOrig){
        	Calendar c = (Calendar)cOrig.clone();
            c.setTime(value);
             = c.get(.);
             = c.get(.) + 1;
             = c.get(.);
             = c.get(.);
             = c.get(.);
             = c.get(.);
             = value.getNanos() / ;
        }
        datetime(Date valueCalendar cOrig){
        	Calendar c = (Calendar)cOrig.clone();
            c.setTime(value);
             = c.get(.);
             = c.get(.) + 1;
             = c.get(.);
             = 0;
             = 0;
             = 0;
             = 0;
        }
        datetime(Time valueCalendar cOrig){
        	Calendar c = (Calendar)cOrig.clone();
            c.setTime(value);
             = 0;
             = 0;
             = 0;
             = c.get(.);
             = c.get(.);
             = c.get(.);
             = c.get(.) * ;
        }
        datetime(byte[] datebyte[] time){
            if (date != null){
                int sql_date = decodeInt(date);
                int century;
                sql_date -= 1721119 - 2400001;
                century = (4 * sql_date - 1) / 146097;
                sql_date = 4 * sql_date - 1 - 146097 * century;
                 = sql_date / 4;
                sql_date = (4 *  + 3) / 1461;
                 = 4 *  + 3 - 1461 * sql_date;
                 = ( + 4) / 4;
                 = (5 *  - 3) / 153;
                 = 5 *  - 3 - 153 * ;
                 = ( + 5) / 5;
                 = 100 * century + sql_date;
                if ( < 10) {
                     += 3;
                } else {
                     -= 9;
                     += 1;
                }
            }
            if (time != null){		
                int fractionsInDay = decodeInt(time);
                 = fractionsInDay / ;
                fractionsInDay -=  * ;
                 = fractionsInDay / ;
                fractionsInDay -=  * ;
                 = fractionsInDay / ;
                 = fractionsInDay -  * ;
            }
        }
        byte[] toTimeBytes(){
            int fractionsInDay =
                     * 
                    +  * 
                    +  * 
                    + ;
            return encodeInt(fractionsInDay);
        }
        byte[] toDateBytes(){
            int cpMonth = ;
            int cpYear = ;
            int cya;
            if (cpMonth > 2) {
                cpMonth -= 3;
            } else {
                cpMonth += 9;
                cpYear -= 1;
            }
            c = cpYear / 100;
            ya = cpYear - 100 * c;
            int value = ((146097 * c) / 4 +
                 (1461 * ya) / 4 +
                 (153 * cpMonth + 2) / 5 +
                  + 1721119 - 2400001);
            return encodeInt(value);
        }
        Time toTime(Calendar cOrig){
        	Calendar c = (Calendar)cOrig.clone();
            c.set(., 1970);
            c.set(..);
            c.set(., 1);
            c.set(.,);
            c.set(.);
            c.set(.);
            return new Time(c.getTime().getTime());
        }
        Timestamp toTimestamp(Calendar cOrig){
        	Calendar c = (Calendar)cOrig.clone();
            c.set(.);
            c.set(. - 1);
            c.set(.);
            c.set(.);
            c.set(.);
            c.set(.);
            Timestamp timestamp = new Timestamp(c.getTime().getTime());
            timestamp.setNanos( * );
            return timestamp;
        }
        Date toDate(Calendar cOrig){
        	Calendar c = (Calendar)cOrig.clone();
            c.set(.);
            c.set(. - 1);
            c.set(.);
            c.set(., 0);
            c.set(., 0);
            c.set(., 0);
            c.set(., 0);
            return new Date(c.getTime().getTime());
        }
    }
New to GrepCode? Check out our FAQ X