~mysql/mysql-server/mysql-6.0

« back to all changes in this revision

Viewing changes to sql/protocol_cursor.cc

  • Committer: hf at r18
  • Date: 2003-04-23 14:37:33 UTC
  • mto: This revision was merged to the branch mainline in revision 1523.
  • Revision ID: sp1r-hf@deer.mysql.r18.ru-20030423143733-09970
SCRUM
Protocol_cursor class and sql-common/ directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000-2003 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; either version 2 of the License, or
 
6
   (at your option) any later version.
 
7
 
 
8
   This program is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
   GNU General Public License for more details.
 
12
 
 
13
   You should have received a copy of the GNU General Public License
 
14
   along with this program; if not, write to the Free Software
 
15
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
16
 
 
17
/*
 
18
  Low level functions for storing data to be send to the MySQL client
 
19
  The actual communction is handled by the net_xxx functions in net_serv.cc
 
20
*/
 
21
 
 
22
#ifdef __GNUC__
 
23
#pragma implementation                          // gcc: Class implementation
 
24
#endif
 
25
 
 
26
#include "mysql_priv.h"
 
27
#include <mysql.h>
 
28
 
 
29
bool Protocol_cursor::send_fields(List<Item> *list, uint flag)
 
30
{
 
31
  List_iterator_fast<Item> it(*list);
 
32
  Item                     *item;
 
33
  MYSQL_FIELD              *field, *client_field;
 
34
  
 
35
  DBUG_ENTER("send_fields");
 
36
  if (prepare_for_send(list))
 
37
      return FALSE;
 
38
 
 
39
  fields= (MYSQL_FIELD *)alloc_root(alloc, sizeof(MYSQL_FIELD) * field_count);
 
40
  if (!fields)
 
41
    goto err;
 
42
 
 
43
  client_field= fields;
 
44
  while ((item= it++))
 
45
  {
 
46
    Send_field server_field;
 
47
    item->make_field(&server_field);
 
48
 
 
49
    client_field->db=     strdup_root(alloc, server_field.db_name);
 
50
    client_field->table=  strdup_root(alloc, server_field.table_name);
 
51
    client_field->name=   strdup_root(alloc, server_field.col_name);
 
52
    client_field->org_table= strdup_root(alloc, server_field.org_table_name);
 
53
    client_field->org_name=  strdup_root(alloc, server_field.org_col_name);
 
54
    client_field->length= server_field.length;
 
55
    client_field->type=   server_field.type;
 
56
    client_field->flags= server_field.flags;
 
57
    client_field->decimals= server_field.decimals;
 
58
    client_field->db_length=            strlen(client_field->db);
 
59
    client_field->table_length=         strlen(client_field->table);
 
60
    client_field->name_length=          strlen(client_field->name);
 
61
    client_field->org_name_length=      strlen(client_field->org_name);
 
62
    client_field->org_table_length=     strlen(client_field->org_table);
 
63
    client_field->charsetnr=            server_field.charsetnr;
 
64
    
 
65
    if (INTERNAL_NUM_FIELD(client_field))
 
66
      client_field->flags|= NUM_FLAG;
 
67
 
 
68
    if (flag & 2)
 
69
    {
 
70
      char buff[80];
 
71
      String tmp(buff, sizeof(buff), default_charset_info), *res;
 
72
 
 
73
      if (!(res=item->val_str(&tmp)))
 
74
        client_field->def= strdup_root(alloc, "");
 
75
      else
 
76
        client_field->def= strdup_root(alloc, tmp.ptr());
 
77
    }
 
78
    else
 
79
      client_field->def=0;
 
80
    client_field->max_length= 0;
 
81
    ++client_field;
 
82
  }
 
83
 
 
84
  DBUG_RETURN(FALSE);
 
85
 err:
 
86
  send_error(thd, ER_OUT_OF_RESOURCES); /* purecov: inspected */
 
87
  DBUG_RETURN(TRUE);                            /* purecov: inspected */
 
88
}
 
89
 
 
90
/* Get the length of next field. Change parameter to point at fieldstart */
 
91
bool Protocol_cursor::write()
 
92
{
 
93
  byte *cp= (byte *)packet->ptr();
 
94
  byte *end_pos= (byte *)packet->ptr() + packet->length();
 
95
  ulong len;
 
96
  MYSQL_FIELD *cur_field= fields;
 
97
  MYSQL_FIELD *fields_end= fields + field_count;
 
98
  MYSQL_ROWS *new_record;
 
99
  byte **data;
 
100
  byte *to;
 
101
 
 
102
  new_record= (MYSQL_ROWS *)alloc_root(alloc, 
 
103
       sizeof(MYSQL_ROWS) + (field_count + 1)*sizeof(char *) + packet->length());
 
104
  if (!new_record)
 
105
    goto err;
 
106
  data= (byte **)(new_record + 1);
 
107
  new_record->data= (char **)data;
 
108
 
 
109
  to= (byte *)(fields + field_count + 1);
 
110
 
 
111
  for (; cur_field < fields_end; ++cur_field, ++data)
 
112
  {
 
113
    if ((len=net_field_length((uchar **)&cp)))
 
114
    {
 
115
      *data= 0;
 
116
    }
 
117
    else
 
118
    {
 
119
      if ((long)len > (end_pos - cp))
 
120
      {
 
121
// TODO error signal      send_error(thd, CR_MALFORMED_PACKET);
 
122
        return TRUE;
 
123
      }
 
124
      memcpy(to,(char*) cp,len);
 
125
      to[len]=0;
 
126
      to+=len+1;
 
127
      cp+=len;
 
128
      if (cur_field->max_length < len)
 
129
        cur_field->max_length=len;
 
130
    }
 
131
  }
 
132
 
 
133
  *prev_record= new_record;
 
134
  prev_record= &new_record->next;
 
135
  new_record->next= NULL;
 
136
  row_count++;
 
137
  return FALSE;
 
138
 err:
 
139
// TODO error signal      send_error(thd, ER_OUT_OF_RESOURCES);
 
140
  return TRUE;
 
141
}
 
142
 
 
143