~drizzle-trunk/drizzle/development

1089.1.13 by Brian Aker
Sorting methods into class files.
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
1089.1.13 by Brian Aker
Sorting methods into class files.
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; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
20
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
21
#include "config.h"
1089.1.13 by Brian Aker
Sorting methods into class files.
22
#include <drizzled/join_table.h>
23
#include <drizzled/field/blob.h>
2141.3.1 by vjsamuel1990 at gmail
Merge encapsulate join_read_const_table() into JoinTable.
24
#include <drizzled/sql_select.h>
1089.1.13 by Brian Aker
Sorting methods into class files.
25
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
26
namespace drizzled
27
{
1089.1.13 by Brian Aker
Sorting methods into class files.
28
2141.3.3 by vjsamuel1990 at gmail
Merge change nested_join_st to NestedJoin
29
int JoinTable::joinReadConstTable(optimizer::Position *pos)
2141.3.1 by vjsamuel1990 at gmail
Merge encapsulate join_read_const_table() into JoinTable.
30
{
31
  int error;
32
  Table *Table= this->table;
33
  Table->const_table=1;
34
  Table->null_row=0;
35
  Table->status=STATUS_NO_RECORD;
36
37
  if (this->type == AM_SYSTEM)
38
  {
2147.5.3 by vjsamuel1990 at gmail
Merge re write encapsulated join_read_system() function name to abide to coding standards.
39
    if ((error=this->joinReadSystem()))
2141.3.1 by vjsamuel1990 at gmail
Merge encapsulate join_read_const_table() into JoinTable.
40
    {						// Info for DESCRIBE
41
      this->info="const row not found";
42
      /* Mark for EXPLAIN that the row was not found */
43
      pos->setFanout(0.0);
44
      pos->clearRefDependMap();
45
      if (! Table->maybe_null || error > 0)
46
        return(error);
47
    }
48
  }
49
  else
50
  {
51
    if (! Table->key_read && 
52
        Table->covering_keys.test(this->ref.key) && 
53
        ! Table->no_keyread &&
54
        (int) Table->reginfo.lock_type <= (int) TL_READ_WITH_SHARED_LOCKS)
55
    {
56
      Table->key_read=1;
57
      Table->cursor->extra(HA_EXTRA_KEYREAD);
58
      this->index= this->ref.key;
59
    }
60
    error=join_read_const(this);
61
    if (Table->key_read)
62
    {
63
      Table->key_read=0;
64
      Table->cursor->extra(HA_EXTRA_NO_KEYREAD);
65
    }
66
    if (error)
67
    {
68
      this->info="unique row not found";
69
      /* Mark for EXPLAIN that the row was not found */
70
      pos->setFanout(0.0);
71
      pos->clearRefDependMap();
72
      if (!Table->maybe_null || error > 0)
73
        return(error);
74
    }
75
  }
76
  if (*this->on_expr_ref && !Table->null_row)
77
  {
78
    if ((Table->null_row= test((*this->on_expr_ref)->val_int() == 0)))
79
      Table->mark_as_null_row();
80
  }
81
  if (!Table->null_row)
82
    Table->maybe_null=0;
83
84
  /* Check appearance of new constant items in Item_equal objects */
85
  Join *Join= this->join;
86
  if (Join->conds)
87
    update_const_equal_items(Join->conds, this);
88
  TableList *tbl;
89
  for (tbl= Join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
90
  {
91
    TableList *embedded;
92
    TableList *embedding= tbl;
93
    do
94
    {
95
      embedded= embedding;
96
      if (embedded->on_expr)
97
         update_const_equal_items(embedded->on_expr, this);
98
      embedding= embedded->getEmbedding();
99
    }
100
    while (embedding &&
101
           embedding->getNestedJoin()->join_list.head() == embedded);
102
  }
103
104
  return(0);
105
}
106
1089.1.13 by Brian Aker
Sorting methods into class files.
107
void JoinTable::readCachedRecord()
108
{
109
  unsigned char *pos;
110
  uint32_t length;
111
  bool last_record;
1539.1.6 by Brian Aker
Update for Join structure changes.
112
  CacheField *copy,*end_field;
1089.1.13 by Brian Aker
Sorting methods into class files.
113
114
  last_record= this->cache.record_nr++ == this->cache.ptr_record;
115
  pos= this->cache.pos;
116
  for (copy= this->cache.field, end_field= copy+this->cache.fields;
117
       copy < end_field;
118
       copy++)
119
  {
120
    if (copy->blob_field)
121
    {
122
      if (last_record)
123
      {
124
        copy->blob_field->set_image(pos, copy->length+sizeof(char*),
125
                  copy->blob_field->charset());
126
        pos+=copy->length+sizeof(char*);
127
      }
128
      else
129
      {
130
        copy->blob_field->set_ptr(pos, pos+copy->length);
131
        pos+=copy->length+copy->blob_field->get_length();
132
      }
133
    }
134
    else
135
    {
136
      if (copy->strip)
137
      {
138
        length= uint2korr(pos);
139
        memcpy(copy->str, pos+2, length);
140
        memset(copy->str+length, ' ', copy->length-length);
141
        pos+= 2 + length;
142
      }
143
      else
144
      {
145
        memcpy(copy->str,pos,copy->length);
146
        pos+=copy->length;
147
      }
148
    }
149
  }
150
  this->cache.pos=pos;
151
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
152
2147.5.3 by vjsamuel1990 at gmail
Merge re write encapsulated join_read_system() function name to abide to coding standards.
153
int JoinTable::joinReadSystem()
2147.5.1 by vjsamuel1990 at gmail
Merge encapsulate join_system_read() into JoinTable
154
{
155
  Table *Table= this->table;
156
  int error;
157
  if (Table->status & STATUS_GARBAGE)		// If first read
158
  {
159
    if ((error=Table->cursor->read_first_row(table->getInsertRecord(),
160
					   Table->getShare()->getPrimaryKey())))
161
    {
162
      if (error != HA_ERR_END_OF_FILE)
163
        return Table->report_error(error);
164
      this->table->mark_as_null_row();
165
      Table->emptyRecord();			// Make empty record
166
      return -1;
167
    }
168
    Table->storeRecord();
169
  }
170
  else if (!Table->status)			// Only happens with left join
171
    Table->restoreRecord();			// restore old record
172
  Table->null_row=0;
173
  return Table->status ? -1 : 0;
174
}
175
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
176
} /* namespace drizzled */