sql_handler.cc 7.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


/* HANDLER ... commands - direct access to ISAM */

#include "mysql_priv.h"
#include "sql_select.h"
unknown's avatar
unknown committed
22
#include <assert.h>
23

unknown's avatar
unknown committed
24 25
/* TODO:
  HANDLER blabla OPEN [ AS foobar ] [ (column-list) ]
unknown's avatar
unknown committed
26

unknown's avatar
unknown committed
27 28 29
  the most natural (easiest, fastest) way to do it is to
  compute List<Item> field_list not in mysql_ha_read
  but in mysql_ha_open, and then store it in TABLE structure.
unknown's avatar
unknown committed
30

unknown's avatar
unknown committed
31 32 33 34 35
  The problem here is that mysql_parse calls free_item to free all the
  items allocated at the end of every query. The workaround would to
  keep two item lists per THD - normal free_list and handler_items.
  The second is to be freeed only on thread end. mysql_ha_open should
  then do { handler_items=concat(handler_items, free_list); free_list=0; }
unknown's avatar
unknown committed
36

unknown's avatar
unknown committed
37 38 39 40 41 42 43 44 45
  But !!! do_cammand calls free_root at the end of every query and frees up
  all the sql_alloc'ed memory. It's harder to work around...
 */

#define HANDLER_TABLES_HACK(thd) {      \
  TABLE *tmp=thd->open_tables;          \
  thd->open_tables=thd->handler_tables; \
  thd->handler_tables=tmp; }

46 47
static TABLE **find_table_ptr_by_name(THD *thd,const char *db,
				      const char *table_name, bool is_alias);
48 49 50

int mysql_ha_open(THD *thd, TABLE_LIST *tables)
{
unknown's avatar
unknown committed
51
  HANDLER_TABLES_HACK(thd);
52
  int err=open_tables(thd,tables);
unknown's avatar
unknown committed
53 54 55
  HANDLER_TABLES_HACK(thd);
  if (err)
    return -1;
unknown's avatar
unknown committed
56

57
  // there can be only one table in *tables
58
  if (!(tables->table->file->table_flags() & HA_CAN_SQL_HANDLER))
59
  {
60
    my_printf_error(ER_ILLEGAL_HA,ER(ER_ILLEGAL_HA),MYF(0), tables->alias);
61
    mysql_ha_close(thd, tables,1);
62 63 64
    return -1;
  }

unknown's avatar
unknown committed
65 66
  send_ok(&thd->net);
  return 0;
67 68
}

69
int mysql_ha_close(THD *thd, TABLE_LIST *tables, bool dont_send_ok)
70
{
71
  TABLE **ptr=find_table_ptr_by_name(thd, tables->db, tables->alias, 1);
72 73

  if (*ptr)
74 75
  {
    VOID(pthread_mutex_lock(&LOCK_open));
76
    close_thread_table(thd, ptr);
77 78
    VOID(pthread_mutex_unlock(&LOCK_open));
  }
79 80 81
  else
  {
    my_printf_error(ER_UNKNOWN_TABLE,ER(ER_UNKNOWN_TABLE),MYF(0),
82
		    tables->alias, "HANDLER");
83 84
    return -1;
  }
85 86
  if (!dont_send_ok)
    send_ok(&thd->net);
87 88 89
  return 0;
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
int mysql_ha_closeall(THD *thd, TABLE_LIST *tables, bool dont_send_ok)
{
  TABLE **ptr=find_table_ptr_by_name(thd, tables->db, tables->real_name, 0);

  DBUG_ASSERT(dont_send_ok);
  if (*ptr)
  {
//    if (!dont_send_ok) VOID(pthread_mutex_lock(&LOCK_open));
    close_thread_table(thd, ptr);
//    if (!dont_send_ok) VOID(pthread_mutex_unlock(&LOCK_open));
  }
//  if (!dont_send_ok) send_ok(&thd->net);
  return 0;
}

105
static enum enum_ha_read_modes rkey_to_rnext[]=
unknown's avatar
unknown committed
106
    { RNEXT, RNEXT, RPREV, RNEXT, RPREV, RNEXT, RPREV };
unknown's avatar
unknown committed
107

unknown's avatar
unknown committed
108

109 110
int mysql_ha_read(THD *thd, TABLE_LIST *tables,
    enum enum_ha_read_modes mode, char *keyname, List<Item> *key_expr,
unknown's avatar
unknown committed
111
    enum ha_rkey_function ha_rkey_mode, Item *cond,
unknown's avatar
unknown committed
112
    ha_rows select_limit,ha_rows offset_limit)
113
{
unknown's avatar
unknown committed
114
  int err, keyno=-1;
115
  TABLE *table=*find_table_ptr_by_name(thd, tables->db, tables->alias, 1);
116 117 118
  if (!table)
  {
    my_printf_error(ER_UNKNOWN_TABLE,ER(ER_UNKNOWN_TABLE),MYF(0),
119
		    tables->alias,"HANDLER");
120 121 122
    return -1;
  }
  tables->table=table;
unknown's avatar
unknown committed
123 124 125

  if (cond && cond->fix_fields(thd,tables))
    return -1;
unknown's avatar
unknown committed
126

unknown's avatar
unknown committed
127 128
  table->file->init_table_handle_for_HANDLER(); // Only InnoDB requires it

unknown's avatar
unknown committed
129
  if (keyname)
130
  {
unknown's avatar
unknown committed
131 132 133
    if ((keyno=find_type(keyname, &table->keynames, 1+2)-1)<0)
    {
      my_printf_error(ER_KEY_DOES_NOT_EXITS,ER(ER_KEY_DOES_NOT_EXITS),MYF(0),
134
          keyname,tables->alias);
unknown's avatar
unknown committed
135 136
      return -1;
    }
unknown's avatar
unknown committed
137
    table->file->index_init(keyno);
138 139 140 141 142
  }

  List<Item> list;
  list.push_front(new Item_field(NULL,NULL,"*"));
  List_iterator<Item> it(list);
unknown's avatar
unknown committed
143
  uint num_rows;
144 145
  it++;

146
  insert_fields(thd,tables,tables->db,tables->alias,&it);
unknown's avatar
unknown committed
147 148 149

  select_limit+=offset_limit;
  send_fields(thd,list,1);
unknown's avatar
unknown committed
150

unknown's avatar
unknown committed
151
  HANDLER_TABLES_HACK(thd);
unknown's avatar
unknown committed
152
  MYSQL_LOCK *lock=mysql_lock_tables(thd,&tables->table,1);
unknown's avatar
unknown committed
153 154 155
  HANDLER_TABLES_HACK(thd);
  if (!lock)
     goto err0; // mysql_lock_tables() printed error message already
unknown's avatar
unknown committed
156

unknown's avatar
unknown committed
157 158
  table->file->init_table_handle_for_HANDLER(); // Only InnoDB requires it

unknown's avatar
unknown committed
159
  for (num_rows=0; num_rows < select_limit; )
160
  {
unknown's avatar
unknown committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    switch(mode) {
    case RFIRST:
      err=keyname ?
	table->file->index_first(table->record[0]) :
	table->file->rnd_init(1) ||
	table->file->rnd_next(table->record[0]);
      mode=RNEXT;
      break;
    case RLAST:
      DBUG_ASSERT(keyname != 0);
      err=table->file->index_last(table->record[0]);
      mode=RPREV;
      break;
    case RNEXT:
      err=keyname ?
	table->file->index_next(table->record[0]) :
	table->file->rnd_next(table->record[0]);
      break;
    case RPREV:
      DBUG_ASSERT(keyname != 0);
      err=table->file->index_prev(table->record[0]);
      break;
    case RKEY:
unknown's avatar
unknown committed
184
    {
unknown's avatar
unknown committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
      DBUG_ASSERT(keyname != 0);
      KEY *keyinfo=table->key_info+keyno;
      KEY_PART_INFO *key_part=keyinfo->key_part;
      uint key_len;
      byte *key;
      if (key_expr->elements > keyinfo->key_parts)
      {
	my_printf_error(ER_TOO_MANY_KEY_PARTS,ER(ER_TOO_MANY_KEY_PARTS),
			MYF(0),keyinfo->key_parts);
	goto err;
      }
      List_iterator_fast<Item> it_ke(*key_expr);
      Item *item;
      for (key_len=0 ; (item=it_ke++) ; key_part++)
      {
200
	item->save_in_field(key_part->field, 1);
unknown's avatar
unknown committed
201 202
	key_len+=key_part->store_length;
      }
203
      if (!(key= (byte*) thd->calloc(ALIGN_SIZE(key_len))))
unknown's avatar
unknown committed
204 205 206 207 208 209 210 211 212 213 214 215 216
      {
	send_error(&thd->net,ER_OUTOFMEMORY);
	goto err;
      }
      key_copy(key, table, keyno, key_len);
      err=table->file->index_read(table->record[0],
				  key,key_len,ha_rkey_mode);
      mode=rkey_to_rnext[(int)ha_rkey_mode];
      break;
    }
    default:
      send_error(&thd->net,ER_ILLEGAL_HA);
      goto err;
unknown's avatar
unknown committed
217 218
    }

unknown's avatar
unknown committed
219
    if (err)
unknown's avatar
unknown committed
220
    {
unknown's avatar
unknown committed
221 222 223 224 225 226 227 228 229 230
      if (err != HA_ERR_KEY_NOT_FOUND && err != HA_ERR_END_OF_FILE)
      {
        sql_print_error("mysql_ha_read: Got error %d when reading table",
                        err);
        table->file->print_error(err,MYF(0));
        goto err;
      }
      goto ok;
    }
    if (cond)
231
    {
unknown's avatar
unknown committed
232
      err=err;
233
      if (!cond->val_int())
unknown's avatar
unknown committed
234
        continue;
unknown's avatar
unknown committed
235 236
    }
    if (num_rows>=offset_limit)
237
    {
unknown's avatar
unknown committed
238
      if (!err)
239
      {
unknown's avatar
unknown committed
240 241 242 243 244 245
        String *packet = &thd->packet;
        Item *item;
        packet->length(0);
        it.rewind();
        while ((item=it++))
        {
246
          if (item->send(thd,packet))
unknown's avatar
unknown committed
247 248 249 250 251 252 253
          {
            packet->free();                             // Free used
            my_error(ER_OUT_OF_RESOURCES,MYF(0));
            goto err;
          }
        }
        my_net_write(&thd->net, (char*)packet->ptr(), packet->length());
254 255
      }
    }
unknown's avatar
unknown committed
256
    num_rows++;
257
  }
unknown's avatar
unknown committed
258 259
ok:
  mysql_unlock_tables(thd,lock);
260 261
  send_eof(&thd->net);
  return 0;
unknown's avatar
unknown committed
262 263
err:
  mysql_unlock_tables(thd,lock);
unknown's avatar
unknown committed
264
err0:
unknown's avatar
unknown committed
265
  return -1;
266 267
}

268
static TABLE **find_table_ptr_by_name(THD *thd, const char *db,
269
				      const char *table_name, bool is_alias)
270 271
{
  int dblen;
272
  TABLE **ptr;
unknown's avatar
unknown committed
273

274 275
  if (!db || ! *db)
    db= thd->db ? thd->db : "";
276
  dblen=strlen(db)+1;
277
  ptr=&(thd->handler_tables);
unknown's avatar
unknown committed
278

279
  for (TABLE *table=*ptr; table ; table=*ptr)
280 281
  {
    if (!memcmp(table->table_cache_key, db, dblen) &&
282
        !my_strcasecmp((is_alias ? table->table_name : table->real_name),table_name))
283 284
      break;
    ptr=&(table->next);
285
  }
286
  return ptr;
287
}
288