1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package util;
  24 
  25 import java.math.BigDecimal;
  26 import java.sql.Date;
  27 import java.sql.SQLData;
  28 import java.sql.SQLException;
  29 import java.sql.SQLInput;
  30 import java.sql.SQLOutput;
  31 import java.sql.Time;
  32 import java.sql.Timestamp;
  33 import java.time.LocalDate;
  34 import java.time.LocalDateTime;
  35 import java.time.LocalTime;
  36 import java.util.Arrays;
  37 
  38 public class TestSQLDataImpl implements SQLData {
  39 
  40     private final int stringPos = 0;
  41     private final int datePos = 1;
  42     private final int timePos = 2;
  43     private final int timestampPos = 3;
  44     private final int intPos = 4;
  45     private final int longPos = 5;
  46     private final int shortPos = 6;
  47     private final int bigDecimalPos = 7;
  48     private final int doublePos = 8;
  49     private final int booleanPos = 9;
  50     private final int floatPos = 10;
  51     private final int bytePos = 11;
  52     private final int bytesPos = 12;
  53     private final int MAX_TYPES = bytesPos + 1;
  54     private final Object[] types = new Object[MAX_TYPES];
  55 
  56     private final static byte[] b = {1, 2, 3};
  57 
  58     // attributes entries must line up with the ordering of the reading and
  59     // writing of the fields in readSQL and writeSQL
  60     public final static Object[] attributes = {"The Dark Knight",
  61         Date.valueOf(LocalDate.now()), Time.valueOf(LocalTime.now()),
  62         Timestamp.valueOf(LocalDateTime.now()), Integer.MAX_VALUE,
  63         Long.MAX_VALUE, Short.MIN_VALUE, BigDecimal.ONE,
  64         Double.MAX_VALUE, true, 1.5f, Byte.MAX_VALUE, b};
  65 
  66     private String sqlType;
  67 
  68     public TestSQLDataImpl(String type) {
  69         sqlType = type;
  70     }
  71 
  72     @Override
  73     public String getSQLTypeName() throws SQLException {
  74         return sqlType;
  75     }
  76 
  77     @Override
  78     public void readSQL(SQLInput stream, String typeName) throws SQLException {
  79 
  80         sqlType = typeName;
  81         types[stringPos] = stream.readString();
  82         types[datePos] = stream.readDate();
  83         types[timePos] = stream.readTime();
  84         types[timestampPos] = stream.readTimestamp();
  85         types[intPos] = stream.readInt();
  86         types[longPos] = stream.readLong();
  87         types[shortPos] = stream.readShort();
  88         types[bigDecimalPos] = stream.readBigDecimal();
  89         types[doublePos] = stream.readDouble();
  90         types[booleanPos] = stream.readBoolean();
  91         types[floatPos] = stream.readFloat();
  92         types[bytePos] = stream.readByte();
  93         types[bytesPos] = stream.readBytes();
  94     }
  95 
  96     @Override
  97     public void writeSQL(SQLOutput stream) throws SQLException {
  98 
  99         stream.writeString((String) types[stringPos]);
 100         stream.writeDate((Date) types[datePos]);
 101         stream.writeTime((Time) types[timePos]);
 102         stream.writeTimestamp((Timestamp) types[timestampPos]);
 103         stream.writeInt((Integer) types[intPos]);
 104         stream.writeLong((Long) types[longPos]);
 105         stream.writeShort((Short) types[shortPos]);
 106         stream.writeBigDecimal((BigDecimal) types[bigDecimalPos]);
 107         stream.writeDouble((Double) types[doublePos]);
 108         stream.writeBoolean((Boolean) types[booleanPos]);
 109         stream.writeFloat((Float) types[floatPos]);
 110         stream.writeByte((Byte) types[bytePos]);
 111         stream.writeBytes((byte[]) types[bytesPos]);
 112     }
 113 
 114     public Object[] toArray() {
 115 
 116         Object[] result = Arrays.copyOf(types, types.length);
 117         return result;
 118     }
 119 
 120     @Override
 121     public String toString() {
 122         return "[" + sqlType + " " + types + "]";
 123     }
 124 
 125 }