~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzleclient/drizzle_data.c

  • Committer: Brian Aker
  • Date: 2009-02-27 21:38:40 UTC
  • Revision ID: brian@tangent.org-20090227213840-r9hq3sfk8d8qrg72
Code cleanup in signal_handler.cc

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#include <drizzled/global.h>
 
21
 
 
22
#include "libdrizzle_priv.h"
 
23
 
 
24
#include "drizzle_data.h"
 
25
#include "drizzle_rows.h"
 
26
#include "drizzle_field.h"
 
27
 
 
28
#include <assert.h>
 
29
#include <stdint.h>
 
30
#include <stdbool.h>
 
31
#include <stdlib.h>
 
32
#include <string.h>
 
33
 
 
34
/***************************************************************************
 
35
  Change field rows to field structs
 
36
***************************************************************************/
 
37
 
 
38
DRIZZLE_FIELD *
 
39
drizzleclient_unpack_fields(DRIZZLE_DATA *data, unsigned int fields,
 
40
              bool default_value)
 
41
{
 
42
  DRIZZLE_ROWS  *row;
 
43
  DRIZZLE_FIELD  *field,*result;
 
44
  uint32_t lengths[9];        /* Max of fields */
 
45
 
 
46
  field= result= (DRIZZLE_FIELD*) malloc((unsigned int) sizeof(*field)*fields);
 
47
  if (!result)
 
48
  {
 
49
    drizzleclient_free_rows(data);        /* Free old data */
 
50
    return(0);
 
51
  }
 
52
  memset((char*) field, 0, (unsigned int) sizeof(DRIZZLE_FIELD)*fields);
 
53
 
 
54
  for (row= data->data; row ; row = row->next,field++)
 
55
  {
 
56
    unsigned char *pos;
 
57
    /* fields count may be wrong */
 
58
    assert((unsigned int) (field - result) < fields);
 
59
    drizzleclient_cli_fetch_lengths(&lengths[0], row->data, default_value ? 8 : 7);
 
60
    field->catalog=   strdup((char*) row->data[0]);
 
61
    field->db=        strdup((char*) row->data[1]);
 
62
    field->table=     strdup((char*) row->data[2]);
 
63
    field->org_table= strdup((char*) row->data[3]);
 
64
    field->name=      strdup((char*) row->data[4]);
 
65
    field->org_name=  strdup((char*) row->data[5]);
 
66
 
 
67
    field->catalog_length=  lengths[0];
 
68
    field->db_length=    lengths[1];
 
69
    field->table_length=  lengths[2];
 
70
    field->org_table_length=  lengths[3];
 
71
    field->name_length=  lengths[4];
 
72
    field->org_name_length=  lengths[5];
 
73
 
 
74
    /* Unpack fixed length parts */
 
75
    pos= (unsigned char*) row->data[6];
 
76
    field->charsetnr= uint2korr(pos);
 
77
    field->length=  (unsigned int) uint4korr(pos+2);
 
78
    field->type=  (enum enum_field_types) pos[6];
 
79
    field->flags=  uint2korr(pos+7);
 
80
    field->decimals=  (unsigned int) pos[9];
 
81
 
 
82
    /* Test if field is Internal Number Format */
 
83
    if (((field->type <= DRIZZLE_TYPE_LONGLONG) &&
 
84
         (field->type != DRIZZLE_TYPE_TIMESTAMP)) ||
 
85
        (field->length == 14) ||
 
86
        (field->length == 8))
 
87
      field->flags|= NUM_FLAG;
 
88
    if (default_value && row->data[7])
 
89
    {
 
90
      field->def= (char *)malloc(lengths[7]);
 
91
      memcpy(field->def, row->data[7], lengths[7]);
 
92
      field->def_length= lengths[7];
 
93
    }
 
94
    else
 
95
      field->def=0;
 
96
    field->max_length= 0;
 
97
  }
 
98
 
 
99
  drizzleclient_free_rows(data);        /* Free old data */
 
100
  return(result);
 
101
}
 
102
 
 
103
void drizzleclient_free_rows(DRIZZLE_DATA *cur)
 
104
{
 
105
  if (cur)
 
106
  {
 
107
    if (cur->data != NULL)
 
108
    {
 
109
      struct st_drizzle_rows * row= cur->data;
 
110
      uint64_t x;
 
111
      for (x= 0; x< cur->rows; x++)
 
112
      {
 
113
        struct st_drizzle_rows * next_row= row->next;
 
114
        free(row);
 
115
        row= next_row;
 
116
      }
 
117
    }
 
118
    free((unsigned char*) cur);
 
119
  }
 
120
}