2001-04-07 00:18:33 +02:00
|
|
|
/* 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 */
|
|
|
|
|
2001-04-13 16:18:44 +02:00
|
|
|
#include <assert.h>
|
2001-04-07 00:18:33 +02:00
|
|
|
#include "mysql_priv.h"
|
|
|
|
#include "sql_select.h"
|
|
|
|
|
2001-04-13 16:18:44 +02:00
|
|
|
/* TODO:
|
|
|
|
HANDLER blabla OPEN [ AS foobar ] [ (column-list) ]
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2001-04-13 16:18:44 +02:00
|
|
|
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.
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2001-04-13 16:18:44 +02:00
|
|
|
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; }
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2001-04-13 16:18:44 +02:00
|
|
|
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; }
|
|
|
|
|
2001-05-23 22:47:08 +02:00
|
|
|
static TABLE **find_table_ptr_by_name(THD *thd, const char *db,
|
|
|
|
const char *table_name);
|
2001-04-07 00:18:33 +02:00
|
|
|
|
|
|
|
int mysql_ha_open(THD *thd, TABLE_LIST *tables)
|
|
|
|
{
|
2001-04-13 16:18:44 +02:00
|
|
|
HANDLER_TABLES_HACK(thd);
|
2001-04-07 00:18:33 +02:00
|
|
|
int err=open_tables(thd,tables);
|
2001-04-13 16:18:44 +02:00
|
|
|
HANDLER_TABLES_HACK(thd);
|
|
|
|
if (err)
|
|
|
|
return -1;
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2001-04-13 16:18:44 +02:00
|
|
|
send_ok(&thd->net);
|
|
|
|
return 0;
|
2001-04-07 00:18:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int mysql_ha_close(THD *thd, TABLE_LIST *tables)
|
|
|
|
{
|
2001-04-15 22:56:20 +02:00
|
|
|
TABLE **ptr=find_table_ptr_by_name(thd, tables->db, tables->name);
|
|
|
|
|
|
|
|
if (*ptr)
|
2001-11-29 20:46:51 +01:00
|
|
|
{
|
|
|
|
VOID(pthread_mutex_lock(&LOCK_open));
|
2001-04-15 22:56:20 +02:00
|
|
|
close_thread_table(thd, ptr);
|
2001-11-29 20:46:51 +01:00
|
|
|
VOID(pthread_mutex_unlock(&LOCK_open));
|
|
|
|
}
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2001-04-07 00:18:33 +02:00
|
|
|
send_ok(&thd->net);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-04-09 15:37:19 +02:00
|
|
|
static enum enum_ha_read_modes rkey_to_rnext[]=
|
|
|
|
{ RNEXT, RNEXT, RPREV, RNEXT, RPREV, RNEXT, RPREV };
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2001-04-07 00:18:33 +02:00
|
|
|
int mysql_ha_read(THD *thd, TABLE_LIST *tables,
|
|
|
|
enum enum_ha_read_modes mode, char *keyname, List<Item> *key_expr,
|
2001-04-13 16:18:44 +02:00
|
|
|
enum ha_rkey_function ha_rkey_mode, Item *cond,
|
2001-04-09 15:37:19 +02:00
|
|
|
ha_rows select_limit,ha_rows offset_limit)
|
2001-04-07 00:18:33 +02:00
|
|
|
{
|
2001-04-13 16:18:44 +02:00
|
|
|
int err, keyno=-1;
|
2001-04-15 22:56:20 +02:00
|
|
|
TABLE *table=*find_table_ptr_by_name(thd, tables->db, tables->name);
|
2001-04-07 00:18:33 +02:00
|
|
|
if (!table)
|
|
|
|
{
|
|
|
|
my_printf_error(ER_UNKNOWN_TABLE,ER(ER_UNKNOWN_TABLE),MYF(0),
|
2001-04-13 16:18:44 +02:00
|
|
|
tables->name,"HANDLER");
|
2001-04-07 00:18:33 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tables->table=table;
|
2001-04-13 16:18:44 +02:00
|
|
|
|
|
|
|
if (cond && cond->fix_fields(thd,tables))
|
|
|
|
return -1;
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2001-04-13 16:18:44 +02:00
|
|
|
if (keyname)
|
2001-04-07 00:18:33 +02:00
|
|
|
{
|
2001-04-13 16:18:44 +02:00
|
|
|
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),
|
|
|
|
keyname,tables->name);
|
|
|
|
return -1;
|
|
|
|
}
|
2001-04-07 00:18:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
List<Item> list;
|
|
|
|
list.push_front(new Item_field(NULL,NULL,"*"));
|
|
|
|
List_iterator<Item> it(list);
|
|
|
|
it++;
|
|
|
|
|
2001-05-20 12:44:10 +02:00
|
|
|
insert_fields(thd,tables,tables->db,tables->name,&it);
|
2001-04-13 16:18:44 +02:00
|
|
|
|
2001-04-07 00:18:33 +02:00
|
|
|
table->file->index_init(keyno);
|
2001-04-13 16:18:44 +02:00
|
|
|
|
|
|
|
select_limit+=offset_limit;
|
|
|
|
send_fields(thd,list,1);
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2001-04-13 16:18:44 +02:00
|
|
|
MYSQL_LOCK *lock=mysql_lock_tables(thd,&tables->table,1);
|
2001-04-09 15:37:19 +02:00
|
|
|
|
2001-04-13 16:18:44 +02:00
|
|
|
for (uint num_rows=0; num_rows < select_limit; )
|
2001-04-07 00:18:33 +02:00
|
|
|
{
|
2001-04-09 15:37:19 +02:00
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
case RFIRST:
|
2001-04-13 16:18:44 +02:00
|
|
|
err=keyname ?
|
|
|
|
table->file->index_first(table->record[0]) :
|
|
|
|
table->file->rnd_init(1) ||
|
|
|
|
table->file->rnd_next(table->record[0]);
|
|
|
|
mode=RNEXT;
|
|
|
|
break;
|
2001-04-09 15:37:19 +02:00
|
|
|
case RLAST:
|
2001-11-28 01:55:52 +01:00
|
|
|
DBUG_ASSERT(keyname != 0);
|
2001-04-13 16:18:44 +02:00
|
|
|
err=table->file->index_last(table->record[0]);
|
|
|
|
mode=RPREV;
|
|
|
|
break;
|
2001-04-09 15:37:19 +02:00
|
|
|
case RNEXT:
|
2001-04-13 16:18:44 +02:00
|
|
|
err=keyname ?
|
|
|
|
table->file->index_next(table->record[0]) :
|
|
|
|
table->file->rnd_next(table->record[0]);
|
|
|
|
break;
|
2001-04-09 15:37:19 +02:00
|
|
|
case RPREV:
|
2001-11-28 01:55:52 +01:00
|
|
|
DBUG_ASSERT(keyname != 0);
|
2001-04-13 16:18:44 +02:00
|
|
|
err=table->file->index_prev(table->record[0]);
|
|
|
|
break;
|
2001-04-09 15:37:19 +02:00
|
|
|
case RKEY:
|
2001-04-13 16:18:44 +02:00
|
|
|
{
|
2001-11-28 01:55:52 +01:00
|
|
|
DBUG_ASSERT(keyname != 0);
|
2001-04-13 16:18:44 +02:00
|
|
|
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;
|
|
|
|
}
|
2001-08-02 05:29:50 +02:00
|
|
|
List_iterator_fast<Item> it_ke(*key_expr);
|
2001-04-13 16:18:44 +02:00
|
|
|
Item *item;
|
|
|
|
for (key_len=0 ; (item=it_ke++) ; key_part++)
|
|
|
|
{
|
|
|
|
item->save_in_field(key_part->field);
|
|
|
|
key_len+=key_part->store_length;
|
|
|
|
}
|
2001-09-16 23:32:45 +02:00
|
|
|
if (!(key= (byte*) sql_calloc(ALIGN_SIZE(key_len))))
|
2001-04-13 16:18:44 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2001-04-09 15:37:19 +02:00
|
|
|
default:
|
2001-04-13 16:18:44 +02:00
|
|
|
send_error(&thd->net,ER_ILLEGAL_HA);
|
|
|
|
goto err;
|
2001-04-09 15:37:19 +02:00
|
|
|
}
|
|
|
|
|
2001-04-13 16:18:44 +02:00
|
|
|
if (err)
|
2001-04-09 15:37:19 +02:00
|
|
|
{
|
2001-04-13 16:18:44 +02:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
err=err;
|
|
|
|
if(!cond->val_int())
|
|
|
|
continue;
|
2001-04-09 15:37:19 +02:00
|
|
|
}
|
|
|
|
if (num_rows>=offset_limit)
|
2001-04-07 00:18:33 +02:00
|
|
|
{
|
2001-04-09 15:37:19 +02:00
|
|
|
if (!err)
|
2001-04-07 00:18:33 +02:00
|
|
|
{
|
2001-04-13 16:18:44 +02:00
|
|
|
String *packet = &thd->packet;
|
|
|
|
Item *item;
|
|
|
|
packet->length(0);
|
|
|
|
it.rewind();
|
|
|
|
while ((item=it++))
|
|
|
|
{
|
2001-10-17 18:39:39 +02:00
|
|
|
if (item->send(thd,packet))
|
2001-04-13 16:18:44 +02:00
|
|
|
{
|
|
|
|
packet->free(); // Free used
|
|
|
|
my_error(ER_OUT_OF_RESOURCES,MYF(0));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
my_net_write(&thd->net, (char*)packet->ptr(), packet->length());
|
2001-04-07 00:18:33 +02:00
|
|
|
}
|
|
|
|
}
|
2001-04-13 16:18:44 +02:00
|
|
|
num_rows++;
|
2001-04-07 00:18:33 +02:00
|
|
|
}
|
2001-04-13 16:18:44 +02:00
|
|
|
ok:
|
|
|
|
mysql_unlock_tables(thd,lock);
|
2001-04-07 00:18:33 +02:00
|
|
|
send_eof(&thd->net);
|
|
|
|
return 0;
|
2001-04-13 16:18:44 +02:00
|
|
|
err:
|
|
|
|
mysql_unlock_tables(thd,lock);
|
|
|
|
return -1;
|
2001-04-07 00:18:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
2Monty: It could easily happen, that the following service functions are
|
|
|
|
already defined somewhere in the code, but I failed to find them.
|
|
|
|
If this is the case, just say a word and I'll use old functions here.
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
/* Note: this function differs from find_locked_table() because we're looking
|
|
|
|
here for alias, not real table name
|
|
|
|
*/
|
2001-05-23 22:47:08 +02:00
|
|
|
static TABLE **find_table_ptr_by_name(THD *thd, const char *db,
|
|
|
|
const char *table_name)
|
2001-04-07 00:18:33 +02:00
|
|
|
{
|
|
|
|
int dblen;
|
2001-04-15 22:56:20 +02:00
|
|
|
TABLE **ptr;
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2001-05-23 22:47:08 +02:00
|
|
|
if (!db || ! *db)
|
|
|
|
db= thd->db ? thd->db : "";
|
|
|
|
dblen=strlen(db)+1;
|
2001-04-15 22:56:20 +02:00
|
|
|
ptr=&(thd->handler_tables);
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2001-04-15 22:56:20 +02:00
|
|
|
for (TABLE *table=*ptr; table ; table=*ptr)
|
2001-04-07 00:18:33 +02:00
|
|
|
{
|
|
|
|
if (!memcmp(table->table_cache_key, db, dblen) &&
|
2001-04-13 16:18:44 +02:00
|
|
|
!my_strcasecmp(table->table_name,table_name))
|
2001-04-15 22:56:20 +02:00
|
|
|
break;
|
|
|
|
ptr=&(table->next);
|
2001-04-07 00:18:33 +02:00
|
|
|
}
|
2001-04-15 22:56:20 +02:00
|
|
|
return ptr;
|
2001-04-07 00:18:33 +02:00
|
|
|
}
|