~2591kuldeep/libdrizzle/libdrizzle-binlogapi

« back to all changes in this revision

Viewing changes to libdrizzle/table_map_event.cc

  • Committer: kuldeep
  • Date: 2013-09-19 22:27:25 UTC
  • Revision ID: kuldeep@kuldeep-laptop-20130919222725-xsb4tlxp0tp3mm2t
Adding Tested code on local environment, now we can access the new api functions within library

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * Copyright (C) 2013 Drizzle Developer Group
 
3
 * Copyright (C) 2013 Kuldeep Porwal
 
4
 * All rights reserved.
 
5
 *
 
6
 * Use and distribution licensed under the BSD license.  See
 
7
 * the COPYING file in the parent directory for full text.
 
8
 *
 
9
 * summary: parse table map event
 
10
 *
 
11
 */
 
12
#include "config.h"
 
13
#include<iostream>
 
14
#include "libdrizzle/common.h"
 
15
#include "libdrizzle/commonapi.h"
 
16
#include <stdio.h>
 
17
#include <stdlib.h>
 
18
#include <errno.h>
 
19
#include <inttypes.h>
 
20
#include<string.h>
 
21
 
 
22
/*#ifndef table_map
 
23
 
 
24
#define table_map
 
25
 
 
26
#include<libdrizzle-5.1/table_map_event.h>
 
27
 
 
28
#endif*/
 
29
 
 
30
 
 
31
using namespace std;
 
32
using namespace binlogevent;
 
33
 
 
34
 
 
35
 
 
36
void TableMapEvent::initWithData(const unsigned char* data)
 
37
{
 
38
        uint64_t tmp;
 
39
        char *tmp_char;
 
40
        int tmp_int;
 
41
        int start_pos = header.setHeader(data);
 
42
        if(start_pos==-1)
 
43
                return;
 
44
 
 
45
        tmp = getByte6(start_pos,data); 
 
46
        if(tmp==UINT_MAX)
 
47
                return;
 
48
        setTableId((uint64_t)tmp);
 
49
        start_pos+=6;// 6 byte for table id.
 
50
        
 
51
        tmp = getByte2(start_pos,data); 
 
52
        if(tmp==USHRT_MAX)
 
53
                return;
 
54
        setFlagPh((uint16_t)tmp);
 
55
        start_pos+=2;// 2 byte for post-header flag.
 
56
 
 
57
        if(sizeof(data)-start_pos<1)
 
58
                return;
 
59
        setSchemaNameLen((uint8_t)data[start_pos]);
 
60
        start_pos+=1;// 1 byte for schema name length.
 
61
        
 
62
        tmp_char = getString(start_pos,schema_name_len,data);
 
63
        if(tmp_char==NULL)
 
64
                return;
 
65
        setSchemaName(tmp_char);
 
66
        start_pos+= schema_name_len; //schema_name_len byte for schema name.
 
67
 
 
68
        //  data[start_pos+getSchemaNameLen()] is Null
 
69
        start_pos+=1; //  +1 for Null.
 
70
        
 
71
        if(sizeof(data)-start_pos<1)
 
72
                return;
 
73
        setTableNameLen((uint8_t)data[start_pos]);
 
74
        start_pos+=1;// 1 byte for table name length.
 
75
        
 
76
        tmp_char = getString(start_pos,schema_name_len,data);
 
77
        if(tmp_char==NULL)
 
78
                return;
 
79
        setTableName(tmp_char);
 
80
        start_pos+=table_name_len+1; // +1 for null
 
81
 
 
82
        tmp_int = getEncodedLen(start_pos,data);
 
83
        if(tmp_int==0)
 
84
                return;
 
85
        setColumnCount(getEncodedLen(start_pos,data)); // start_pos will also get updated
 
86
 
 
87
 
 
88
        if(sizeof(data)-start_pos<getColumnCount())
 
89
                return;
 
90
        uint8_t *tmp_array = (uint8_t *)(malloc(sizeof(uint8_t)*getColumnCount()));
 
91
        for(uint64_t i=0;i<getColumnCount();i++)
 
92
        {
 
93
                tmp_array[i]=(uint8_t)data[start_pos+i];
 
94
        }
 
95
        setColumnTypeDef(tmp_array);
 
96
        start_pos+=column_count;
 
97
 
 
98
 
 
99
        int metaSize= getEncodedLen(start_pos,data);
 
100
        if(metaSize==0)
 
101
                return;
 
102
        column_meta_data = (uint64_t *)(malloc(sizeof(uint64_t)*column_count));
 
103
        
 
104
        if(sizeof(data)-start_pos<column_count)
 
105
                return;
 
106
        for(uint64_t col=0;col<column_count;col++)
 
107
        {
 
108
                int type= column_type_def[col];
 
109
                int nextBytes= lookup_metadata_field_size((enum_field_types)type);
 
110
                int metaData=0;
 
111
                switch(nextBytes)
 
112
                {
 
113
                        case 1:
 
114
                                metaData= (int)data[start_pos];
 
115
                                break;
 
116
                        case 2: 
 
117
                                metaData= (int)getByte2(start_pos,data);
 
118
                                break;
 
119
                        default:
 
120
                                break;
 
121
                }
 
122
                column_meta_data[col]= metaData;
 
123
                start_pos+=nextBytes;
 
124
        }
 
125
 
 
126
}
 
127
 
 
128
// getters
 
129
 
 
130
uint32_t TableMapEvent::getTimestamp()
 
131
{
 
132
        return  header.timestamp;
 
133
}
 
134
enum_event_type TableMapEvent::getType()
 
135
{
 
136
        return (enum_event_type)header.type; 
 
137
}
 
138
uint32_t TableMapEvent::getServerId()
 
139
{
 
140
        return header.server_id;
 
141
}
 
142
uint32_t TableMapEvent::getLogPos()
 
143
{
 
144
        return header.log_pos;
 
145
}
 
146
uint16_t TableMapEvent::getFlagH()
 
147
{
 
148
        return header.flag; 
 
149
}
 
150
uint64_t TableMapEvent::getTableId()
 
151
{
 
152
        return table_id;
 
153
}
 
154
uint16_t TableMapEvent::getFlagPh()
 
155
{
 
156
        return flag;
 
157
}
 
158
int TableMapEvent::getSchemaNameLen()
 
159
{
 
160
        return (int)schema_name_len;
 
161
}
 
162
char * TableMapEvent::getSchemaName()
 
163
{
 
164
        return schema_name;
 
165
}
 
166
int TableMapEvent::getTableNameLen()
 
167
{
 
168
        return (int)table_name_len;
 
169
}
 
170
char * TableMapEvent::getTableName()
 
171
{
 
172
        return table_name;
 
173
}
 
174
uint64_t TableMapEvent::getColumnCount()
 
175
{
 
176
        return column_count;
 
177
}
 
178
uint8_t * TableMapEvent::getColumnTypeDef()
 
179
{
 
180
        return column_type_def;
 
181
}
 
182
 
 
183
enum_col_type TableMapEvent::getColType(int colNo)
 
184
{
 
185
        enum_field_bytes num;
 
186
        num = lookup_field_bytes((enum_field_types)column_type_def[colNo]);
 
187
        switch(num)
 
188
        {
 
189
                 case LEN_ENC_STR:
 
190
                         return (enum_col_type)1;
 
191
                 case READ_1_BYTE:
 
192
                 case READ_2_BYTE:
 
193
                 case READ_4_BYTE:
 
194
                 case READ_8_BYTE:
 
195
                 case NOT_FOUND:
 
196
                 default:
 
197
                         return (enum_col_type)2;
 
198
        }
 
199
}
 
200
 
 
201
 
 
202
//setters
 
203
 
 
204
void TableMapEvent::setTableId(uint64_t value)
 
205
{
 
206
        table_id = value;
 
207
}
 
208
void TableMapEvent::setFlagPh(uint16_t value)
 
209
{
 
210
        flag = value;
 
211
}
 
212
void TableMapEvent::setSchemaNameLen(uint8_t value)
 
213
{
 
214
        schema_name_len = value;
 
215
}
 
216
void TableMapEvent::setSchemaName(char *value)
 
217
{
 
218
        schema_name = value;
 
219
}
 
220
void TableMapEvent::setTableNameLen(uint8_t value)
 
221
{
 
222
        table_name_len = value;
 
223
}
 
224
void TableMapEvent::setTableName(char *value)
 
225
{
 
226
        table_name = value;
 
227
}
 
228
void TableMapEvent::setColumnCount(uint64_t value)
 
229
{
 
230
        column_count = value;
 
231
}
 
232
void TableMapEvent::setColumnTypeDef(uint8_t * value)
 
233
{
 
234
        column_type_def = value;
 
235
}