Skip to content

Commit

Permalink
Bugfix: skip NULL fields when reading from MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzhuoyu authored and andreas-bok-sociomantic committed May 10, 2017
1 parent 2fbfe3b commit db121fe
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
15 changes: 14 additions & 1 deletion libdrizzle/field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,21 @@ drizzle_return_t drizzle_state_binary_field_read(drizzle_st *con)
{
drizzle_return_t ret;

switch(con->result->column_buffer[con->result->field_current].type)
/* Skip NULL fields */
while (con->result->null_bitmap[(con->result->field_current_read + 2) / 8] &
(1 << ((con->result->field_current_read + 2) % 8)))
{
con->result->field_current_read++;
}

switch(con->result->column_buffer[con->result->field_current_read].type)
{
case DRIZZLE_COLUMN_TYPE_NULL:
/* This should not happen since NULL fields are masked by `null_bitmap`, but cover it anyway */
drizzle_set_error(con, __func__,
"Unexpected column type: DRIZZLE_COLUMN_TYPE_NULL");
con->result->field_size= 0;
return DRIZZLE_RETURN_UNEXPECTED_DATA;
break;
case DRIZZLE_COLUMN_TYPE_TINY:
con->result->field_size= 1;
Expand Down Expand Up @@ -462,6 +474,7 @@ drizzle_return_t drizzle_state_binary_field_read(drizzle_st *con)
con->result->field_total= con->result->field_size;

con->result->field_current++;
con->result->field_current_read++;
con->pop_state();
return DRIZZLE_RETURN_OK;
}
2 changes: 2 additions & 0 deletions libdrizzle/result.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct drizzle_result_st
uint64_t row_current;

uint16_t field_current; /* field index */
uint16_t field_current_read; /* index of the field currently being read */
uint64_t field_total; /* total length of the field currently being read */
uint64_t field_offset; /* offset within field of most recently read field fragment (0 if first/only fragment) */
uint32_t field_size; /* size of most recently read field value or fragment of field value; max 2^24 */
Expand Down Expand Up @@ -102,6 +103,7 @@ struct drizzle_result_st
row_count(0),
row_current(0),
field_current(0),
field_current_read(0),
field_total(0),
field_offset(0),
field_size(0),
Expand Down
1 change: 1 addition & 0 deletions libdrizzle/row.cc
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ drizzle_return_t drizzle_state_row_read(drizzle_st *con)
con->result->row_count++;
con->result->row_current++;
con->result->field_current= 0;
con->result->field_current_read= 0;
}

con->pop_state();
Expand Down
5 changes: 2 additions & 3 deletions libdrizzle/statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,6 @@ drizzle_return_t drizzle_stmt_fetch(drizzle_stmt_st *stmt)
drizzle_return_t ret= DRIZZLE_RETURN_OK;
uint16_t column_counter= 0, current_column= 0;
drizzle_row_t row;
drizzle_column_st *column;

if (stmt == NULL)
{
Expand Down Expand Up @@ -422,11 +421,12 @@ drizzle_return_t drizzle_stmt_fetch(drizzle_stmt_st *stmt)
{
return DRIZZLE_RETURN_ROW_END;
}
drizzle_column_seek(stmt->execute_result, 0);

for (column_counter = 0; column_counter < stmt->execute_result->column_count; column_counter++)
{
drizzle_bind_st *param= &stmt->result_params[column_counter];
drizzle_column_st *column= drizzle_column_index(stmt->execute_result, column_counter);

/* if this row is null in the result bitmap, skip first 2 bits */
if (stmt->execute_result->null_bitmap[(column_counter+2)/8] & (1 << ((column_counter+2) % 8)))
{
Expand All @@ -439,7 +439,6 @@ drizzle_return_t drizzle_stmt_fetch(drizzle_stmt_st *stmt)
uint16_t short_data;
uint32_t long_data;
uint64_t longlong_data;
column= drizzle_column_next(stmt->execute_result);
param->type= column->type;
param->options.is_null= false;
param->length= stmt->execute_result->field_sizes[current_column];
Expand Down

0 comments on commit db121fe

Please sign in to comment.