~team-java-connector/mariadb-java-client/mariadb_java_connector_gsoc

« back to all changes in this revision

Viewing changes to src/main/java/org/mariadb/jdbc/internal/mysql/packet/MySQLBinaryRowPacket.java

  • Committer: Puneet Dewan
  • Date: 2014-08-11 05:54:44 UTC
  • Revision ID: puneetd30@gmail.com-20140811055444-rhhgixqueva5x6d9
Cleaned code, Added MariaDB Headers & added support for Additional datatypes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 /*
 
2
MariaDB Client for Java
 
3
 
 
4
Copyright (c) 2012 Monty Program Ab.
 
5
 
 
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)
 
9
any later version.
 
10
 
 
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
 
14
for more details.
 
15
 
 
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.
 
18
 
 
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:
 
22
 
 
23
Copyright (c) 2009-2011, Marcus Eriksson, Trond Norbye, Stephane Giron
 
24
 
 
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.
 
29
 
 
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.
 
33
 
 
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.
 
37
 
 
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
 
47
OF SUCH DAMAGE.
 
48
*/
 
49
 
1
50
package org.mariadb.jdbc.internal.mysql.packet;
2
51
 
3
52
import java.io.IOException;
4
 
import java.sql.Date;
 
53
import java.util.Arrays;
5
54
 
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;
20
68
 
21
69
        @Override
22
70
        public ValueObject[] getRow(PacketFetcher packetFetcher) throws IOException {
23
 
                // TODO Auto-generated method stub
24
 
                reader.readByte();
 
71
                
 
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();
29
77
        }
 
78
        int type=0;
30
79
        int[] nullPos=findNullPositions(nullBitMap);
31
80
        for (int i = 0; i < columnInformation.length; i++) {
32
 
                
 
81
                 type=columnInformation[i].getType().getType();
33
82
                if(nullPos[i]>0){
34
 
                        columns[i] = new MySQLValueObject(null, columnInformation[i]);
35
 
                        
 
83
                        columns[i] = new MySQLValueObject(null, columnInformation[i]);                  
36
84
                }else
37
 
                if(columnInformation[i].getType().getType()==MySQLType.INTEGER.getType())            
 
85
                if(type==MySQLType.INTEGER.getType())            
38
86
                {
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]);
43
 
                
44
 
          }else if(columnInformation[i].getType().getType()==MySQLType.FLOAT.getType()){
 
88
                        
 
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())
 
94
            {
 
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]);
46
98
                  
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]);
49
101
                  
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())
 
107
          {
 
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)
 
115
                  {
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()){
59
 
                  reader.readByte();
 
120
                  }else{
 
121
                  columns[i]=new MySQLValueObject((0000+"-"+00+"-"+00).getBytes(), columnInformation[i]);   
 
122
                  }
 
123
          }else if(type==MySQLType.TIME.getType()){
 
124
                  if(reader.readByte()>0){
60
125
                  reader.readByte();
61
126
                  reader.readInt();
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]);
 
131
                  }else{
 
132
                  columns[i]=new MySQLValueObject((00+":"+00+":"+00).getBytes(), columnInformation[i]);
 
133
                  }
66
134
                  
67
 
          }else if(columnInformation[i].getType().getType()==MySQLType.DATETIME.getType()){
68
 
                  reader.readByte();
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]);
 
145
                  }else{
 
146
                  columns[i]=new MySQLValueObject((0000+"-"+00+"-"+00+" "+00+":"+00+":"+00).getBytes(), columnInformation[i]);    
 
147
                  }
 
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]);
76
163
          }
 
164
                
77
165
        }
78
166
        
79
167
        return columns;
85
173
                int[] colPositions=new int[len];
86
174
                bytes[0]=(byte)(bytes[0]>>2);
87
175
 
88
 
                System.out.println(bytes[0]);
 
176
                
89
177
                while(i<len){
90
178
                        if((i/8)>0){
91
179
                                index=(byte)(i/8);
92
180
                        }
93
 
                        if(1== (((byte)bytes[index]>>i)& (byte)1)){
94
 
                                System.out.println("index="+i);
95
 
                                colPositions[i]=i+1;                            
96
 
                                System.out.println("pos="+(i+1));
 
181
                        if(1== (((byte)bytes[index]>>i)& (byte)1)){             
 
182
                                colPositions[i]=i+1;                                            
97
183
                        }
98
184
                        i++;
99
 
                }
100
 
                for(int id=0;id<colPositions.length;id++){
101
 
                System.out.println(colPositions[id]);
102
 
                }
 
185
                }               
103
186
                return colPositions;
104
187
        }
105
188
}