2
MariaDB Client for Java
4
Copyright (c) 2012 Monty Program Ab.
6
This library is free software; you can redistribute it and/or modify it under
7
the terms of the GNU Lesser General Public License as published by the Free
8
Software Foundation; either version 2.1 of the License, or (at your option)
11
This library is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16
You should have received a copy of the GNU Lesser General Public License along
17
with this library; if not, write to Monty Program Ab info@montyprogram.com.
19
This particular MariaDB Client for Java file is work
20
derived from a Drizzle-JDBC. Drizzle-JDBC file which is covered by subject to
21
the following copyright and notice provisions:
23
Copyright (c) 2009-2011, Marcus Eriksson, Trond Norbye, Stephane Giron
25
Redistribution and use in source and binary forms, with or without modification,
26
are permitted provided that the following conditions are met:
27
Redistributions of source code must retain the above copyright notice, this list
28
of conditions and the following disclaimer.
30
Redistributions in binary form must reproduce the above copyright notice, this
31
list of conditions and the following disclaimer in the documentation and/or
32
other materials provided with the distribution.
34
Neither the name of the driver nor the names of its contributors may not be
35
used to endorse or promote products derived from this software without specific
36
prior written permission.
38
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
39
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
40
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
41
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
42
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
43
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
45
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
1
50
package org.mariadb.jdbc.internal.mysql.packet;
3
52
import java.io.IOException;
53
import java.util.Arrays;
6
55
import org.mariadb.jdbc.internal.common.PacketFetcher;
7
56
import org.mariadb.jdbc.internal.common.ValueObject;
8
57
import org.mariadb.jdbc.internal.common.packet.RawPacket;
9
import org.mariadb.jdbc.internal.mysql.MySQLBinaryValueObject;
10
58
import org.mariadb.jdbc.internal.mysql.MySQLColumnInformation;
11
59
import org.mariadb.jdbc.internal.mysql.MySQLType;
12
60
import org.mariadb.jdbc.internal.mysql.MySQLValueObject;
22
70
public ValueObject[] getRow(PacketFetcher packetFetcher) throws IOException {
23
// TODO Auto-generated method stub
72
reader.readByte();//Packet Header
25
73
int nullBitmapLength=(columnInformation.length+7+2)/8;
26
74
byte[] nullBitMap=new byte[nullBitmapLength];
27
75
for(int i=0;i<nullBitmapLength;i++){
28
76
nullBitMap[i]=reader.readByte();
30
79
int[] nullPos=findNullPositions(nullBitMap);
31
80
for (int i = 0; i < columnInformation.length; i++) {
81
type=columnInformation[i].getType().getType();
34
columns[i] = new MySQLValueObject(null, columnInformation[i]);
83
columns[i] = new MySQLValueObject(null, columnInformation[i]);
37
if(columnInformation[i].getType().getType()==MySQLType.INTEGER.getType())
85
if(type==MySQLType.INTEGER.getType())
39
87
columns[i] = new MySQLValueObject((reader.readInt()+"").getBytes(), columnInformation[i]);
40
System.out.println(columns[i].getInt());
41
}else if(columnInformation[i].getType().getType()==MySQLType.VARSTRING.getType()){
42
columns[i] = new MySQLValueObject(reader.getLengthEncodedBytes(), columnInformation[i]);
44
}else if(columnInformation[i].getType().getType()==MySQLType.FLOAT.getType()){
89
}else if(type==MySQLType.VARSTRING.getType()||
90
type==MySQLType.VARCHAR.getType()||
91
type==MySQLType.STRING.getType()||
92
type==MySQLType.SET.getType()||
93
type==MySQLType.ENUM.getType())
95
columns[i] = new MySQLValueObject(reader.getLengthEncodedBytes(), columnInformation[i]);
96
}else if(type==MySQLType.FLOAT.getType()){
45
97
columns[i] = new MySQLValueObject((Float.intBitsToFloat(reader.readInt())+"").getBytes(), columnInformation[i]);
47
}else if(columnInformation[i].getType().getType()==MySQLType.DOUBLE.getType()){
99
}else if(type==MySQLType.DOUBLE.getType()){
48
100
columns[i] = new MySQLValueObject((Double.longBitsToDouble(reader.readLong())+"").getBytes(), columnInformation[i]);
50
}else if(columnInformation[i].getType().getType()==MySQLType.BLOB.getType()){
51
columns[i]=new MySQLValueObject(reader.getLengthEncodedBytes(), columnInformation[i]);
52
}else if(columnInformation[i].getType().getType()==MySQLType.DATE.getType()){
53
reader.readByte();//length
102
}else if(type==MySQLType.BLOB.getType()||
103
type==MySQLType.TINYBLOB.getType()||
104
type==MySQLType.MEDIUMBLOB.getType()||
105
type==MySQLType.LONGBLOB.getType()||
106
columnInformation[i].getType().getType()==MySQLType.GEOMETRY.getType())
108
byte[] blobArr=reader.getLengthEncodedBytes();
109
byte[] originalBlobArr=Arrays.copyOf(blobArr, blobArr.length-2);//Discard 2 values
110
columns[i]=new MySQLValueObject(originalBlobArr
111
, columnInformation[i]);
112
}else if(type==MySQLType.DATE.getType()||
113
type==MySQLType.NEWDATE.getType()){
114
if(reader.readByte()>0)
54
116
int year=reader.readShort();
55
117
int month=reader.readByte();
56
118
int date=reader.readByte();
57
119
columns[i]=new MySQLValueObject((year+"-"+month+"-"+date).getBytes(), columnInformation[i]);
58
}else if(columnInformation[i].getType().getType()==MySQLType.TIME.getType()){
121
columns[i]=new MySQLValueObject((0000+"-"+00+"-"+00).getBytes(), columnInformation[i]);
123
}else if(type==MySQLType.TIME.getType()){
124
if(reader.readByte()>0){
60
125
reader.readByte();
62
127
int hour=reader.readByte();
63
128
int minutes=reader.readByte();
64
129
int seconds=reader.readByte();
65
130
columns[i]=new MySQLValueObject((hour+":"+minutes+":"+seconds).getBytes(), columnInformation[i]);
132
columns[i]=new MySQLValueObject((00+":"+00+":"+00).getBytes(), columnInformation[i]);
67
}else if(columnInformation[i].getType().getType()==MySQLType.DATETIME.getType()){
69
int year=reader.readShort();System.out.println("year="+year);
70
int month=reader.readByte();System.out.println(month);
71
int date=reader.readByte();System.out.println(date);
72
int hours=reader.readByte();System.out.println(hours);
135
}else if(type==MySQLType.DATETIME.getType()||
136
type==MySQLType.TIMESTAMP.getType()){
137
if(reader.readByte()>0){
138
int year=reader.readShort();
139
int month=reader.readByte();
140
int date=reader.readByte();
141
int hours=reader.readByte();
73
142
int minutes=reader.readByte();
74
143
int seconds=reader.readByte();
75
144
columns[i]=new MySQLValueObject((year+"-"+month+"-"+date+" "+hours+":"+minutes+":"+seconds).getBytes(), columnInformation[i]);
146
columns[i]=new MySQLValueObject((0000+"-"+00+"-"+00+" "+00+":"+00+":"+00).getBytes(), columnInformation[i]);
148
}else if(type==MySQLType.YEAR.getType()){
149
int year=reader.readShort();
150
columns[i]=new MySQLValueObject(String.valueOf(year).getBytes(), columnInformation[i]);
151
}else if(type==MySQLType.TINYINT.getType()){
152
int tinyIntValue=reader.readByte();
153
columns[i]=new MySQLValueObject(String.valueOf(tinyIntValue).getBytes(), columnInformation[i]);
154
}else if(type==MySQLType.SMALLINT.getType()){
155
int shortIntValue=reader.readShort();
156
columns[i]=new MySQLValueObject(String.valueOf(shortIntValue).getBytes(), columnInformation[i]);
157
}else if(type==MySQLType.BIGINT.getType()){
158
long longValue=reader.readLong();
159
columns[i]=new MySQLValueObject(String.valueOf(longValue).getBytes(), columnInformation[i]);
160
}else if(type==MySQLType.MEDIUMINT.getType()){
161
int mediumIntValue=reader.read24bitword();
162
columns[i]=new MySQLValueObject(String.valueOf(mediumIntValue).getBytes(), columnInformation[i]);