summaryrefslogtreecommitdiff
path: root/storage/ndb/src/old_files/client/odbc/codegen/Code_table.hpp
blob: 8a95b8fa26c40a98b0be2c25a116812106263d87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* Copyright (C) 2003 MySQL 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 */

#ifndef ODBC_CODEGEN_Code_table_hpp
#define ODBC_CODEGEN_Code_table_hpp

#include <vector>
#include <common/common.hpp>
#include "Code_base.hpp"

class DictTable;
class DictColumn;
class DictIndex;
class Plan_query_filter;
class Plan_query_lookup;
class Plan_query_range;
class Plan_column;
class Plan_expr_column;
class Plan_select;
class Plan_delete;
class Plan_delete_lookup;
class Plan_update;
class Plan_update_lookup;

/**
 * @class Plan_table
 * @brief Table node in PlanTree
 *
 * This is a pure Plan node.  Final executable nodes have table
 * information built-in.
 */
class Plan_table : public Plan_base {
public:
    Plan_table(Plan_root* root, const BaseString& name);
    virtual ~Plan_table();
    Plan_base* analyze(Ctx& ctx, Ctl& ctl);
    Exec_base* codegen(Ctx& ctx, Ctl& ctl);
    void print(Ctx& ctx);
    // attributes
    const BaseString& getName() const;
    const BaseString& getCname() const;
    const char* getPrintName() const;
    void setCname(const BaseString& cname);
    const DictTable& dictTable() const;
    unsigned indexCount() const;
    // resolve
    const ColumnVector& exprColumns() const;
    const ColumnVector& dmlColumns() const;
protected:
    friend class Plan_column;
    friend class Plan_query_filter;
    friend class Plan_query_lookup;
    friend class Plan_query_index;
    friend class Plan_query_range;
    friend class Plan_expr_column;
    friend class Plan_select;
    friend class Plan_delete;
    friend class Plan_delete_lookup;
    friend class Plan_delete_index;
    friend class Plan_update;
    friend class Plan_update_lookup;
    friend class Plan_update_index;
    BaseString m_name;
    BaseString m_cname;
    BaseString m_printName;
    DictTable* m_dictTable;
    /*
     * Resolve column.  Returns 1 on found, 0 on not found, and -1 on error.
     * Modifies both table and column data.
     */
    int resolveColumn(Ctx& ctx, Plan_column* column, bool stripSchemaName = false);
    ColumnVector m_exprColumns;
    ColumnVector m_dmlColumns;
    /*
     * Offset for resolved columns in join.  This is sum over m_exprColumns
     * lengths for all preceding tables.
     */
    unsigned m_resOff;
    /*
     * Each column in primary key and unique hash index has list of
     * expressions it is set equal to in the where-clause (at top level).
     */
    bool resolveEq(Ctx& ctx, Plan_expr_column* column, Plan_expr* expr);
    /*
     * Index struct for primary key and indexes.
     */
    struct Index {
	Index() :
	    m_pos(0),
	    m_keyFound(false),
	    m_dictIndex(0),
	    m_rank(~0),
	    m_keyCount(0),
	    m_keyCountUsed(0) {
	}
	unsigned m_pos;
	ExprListVector m_keyEqList;
	bool m_keyFound;
	ExprVector m_keyEq;
	TableSet m_keySet;
	const DictIndex* m_dictIndex;		// for index only
	unsigned m_rank;			// 0-pk 1-hash index 2-ordered index
	unsigned m_keyCount;			// number of columns
	unsigned m_keyCountUsed;		// may be less for ordered index
	unsigned m_keyCountUnused;		// m_keyCount - m_keyCountUsed
    };
    typedef std::vector<Index> IndexList;	// primary key is entry 0
    IndexList m_indexList;
    /*
     * Find set of additional tables (maybe empty) required to resolve the key
     * columns.
     */
    void resolveSet(Ctx& ctx, Index& index, const TableSet& tsDone);
    void resolveSet(Ctx& ctx, Index& index, const TableSet& tsDone, ExprVector& keyEq, unsigned n);
    /*
     * Check for exactly one key or index match.
     */
    bool exactKey(Ctx& ctx, const Index* indexKey) const;
};

inline
Plan_table::Plan_table(Plan_root* root, const BaseString& name) :
    Plan_base(root),
    m_name(name),
    m_printName(name),
    m_dictTable(0),
    m_exprColumns(1),		// 1-based
    m_dmlColumns(1),		// 1-based
    m_resOff(0),
    m_indexList(1)
{
}

inline const BaseString&
Plan_table::getName() const
{
    return m_name;
}

inline const BaseString&
Plan_table::getCname() const
{
    return m_cname;
}

inline const char*
Plan_table::getPrintName() const
{
    return m_printName.c_str();
}

inline void
Plan_table::setCname(const BaseString& cname)
{
    m_cname.assign(cname);
    m_printName.assign(m_name);
    if (! m_cname.empty()) {
	m_printName.append(" ");
	m_printName.append(m_cname);
    }
}

inline const DictTable&
Plan_table::dictTable() const
{
    ctx_assert(m_dictTable != 0);
    return *m_dictTable;
}

inline unsigned
Plan_table::indexCount() const
{
    ctx_assert(m_indexList.size() > 0);
    return m_indexList.size() - 1;
}

inline const Plan_table::ColumnVector&
Plan_table::exprColumns() const
{
    return m_exprColumns;
}

inline const Plan_table::ColumnVector&
Plan_table::dmlColumns() const
{
    return m_dmlColumns;
}

#endif