summaryrefslogtreecommitdiff
path: root/storage/ndb/src/old_files/client/odbc/codegen/Code_query_join.cpp
blob: 89aafe13610c4a1bc7bb01f0292b8ff6c49d9b2e (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
/* 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 */

#include "Code_query.hpp"
#include "Code_query_join.hpp"
#include "Code_query_scan.hpp"
#include "Code_root.hpp"

// Plan_query_join

Plan_query_join::~Plan_query_join()
{
}

Plan_base*
Plan_query_join::analyze(Ctx& ctx, Ctl& ctl)
{
    ctx_assert(m_inner != 0);
    m_inner->analyze(ctx, ctl);
    if (! ctx.ok())
	return 0;
    ctx_assert(m_outer != 0);
    m_outer->analyze(ctx, ctl);
    if (! ctx.ok())
	return 0;
    return this;
}

Exec_base*
Plan_query_join::codegen(Ctx& ctx, Ctl& ctl)
{
    // generate code for subqueries
    ctx_assert(m_inner != 0);
    Exec_query* execInner = static_cast<Exec_query*>(m_inner->codegen(ctx, ctl));
    if (! ctx.ok())
	return 0;
    ctx_assert(execInner != 0);
    ctx_assert(m_outer != 0);
    ctl.m_execQuery = execInner;
    Exec_query* execOuter = static_cast<Exec_query*>(m_outer->codegen(ctx, ctl));
    if (! ctx.ok())
	return 0;
    ctx_assert(execOuter != 0);
    // combine sql specs from subqueries
    const SqlSpecs& specsInner = execInner->getCode().sqlSpecs();
    const SqlSpecs& specsOuter = execOuter->getCode().sqlSpecs();
    SqlSpecs sqlSpecs(specsInner.count() + specsOuter.count());
    for (unsigned i = 1; i <= specsInner.count(); i++) {
	const SqlSpec sqlSpec(specsInner.getEntry(i), SqlSpec::Reference);
	sqlSpecs.setEntry(i, sqlSpec);
    }
    for (unsigned i = 1; i <= specsOuter.count(); i++) {
	const SqlSpec sqlSpec(specsOuter.getEntry(i), SqlSpec::Reference);
	sqlSpecs.setEntry(specsInner.count() + i, sqlSpec);
    }
    // create the code
    Exec_query_join* exec = new Exec_query_join(ctl.m_execRoot);
    ctl.m_execRoot->saveNode(exec);
    exec->setInner(execInner);
    exec->setOuter(execOuter);
    Exec_query_join::Code& code = *new Exec_query_join::Code(sqlSpecs);
    exec->setCode(code);
    return exec;
}

void
Plan_query_join::print(Ctx& ctx)
{
    ctx.print(" [query_join");
    Plan_base* a[] = { m_inner, m_outer };
    printList(ctx, a, 2);
    ctx.print("]");
}

// Exec_query_join

Exec_query_join::Code::~Code()
{
}

Exec_query_join::Data::~Data()
{
}

Exec_query_join::~Exec_query_join()
{
}

void
Exec_query_join::alloc(Ctx& ctx, Ctl& ctl)
{
    // allocate the subqueries
    ctx_assert(m_inner != 0);
    m_inner->alloc(ctx, ctl);
    if (! ctx.ok())
	return;
    ctx_assert(m_outer != 0);
    ctl.m_query = m_inner;
    m_outer->alloc(ctx, ctl);
    if (! ctx.ok())
	return;
    // combine data rows from subqueries
    const Code& code = getCode();
    const SqlRow& rowInner = m_inner->getData().sqlRow();
    const SqlRow& rowOuter = m_outer->getData().sqlRow();
    SqlRow sqlRow(code.m_sqlSpecs);
    for (unsigned i = 1; i <= rowInner.count(); i++) {
	const SqlSpec& sqlSpec = code.m_sqlSpecs.getEntry(i);
	const SqlField sqlField(sqlSpec, &rowInner.getEntry(i));
	sqlRow.setEntry(i, sqlField);
    }
    for (unsigned i = 1; i <= rowOuter.count(); i++) {
	const SqlSpec& sqlSpec = code.m_sqlSpecs.getEntry(rowInner.count() + i);
	const SqlField sqlField(sqlSpec, &rowOuter.getEntry(i));
	sqlRow.setEntry(rowInner.count() + i, sqlField);
    }
    // create the data
    Data& data = *new Data(this, sqlRow);
    setData(data);
}

void
Exec_query_join::execImpl(Ctx& ctx, Ctl& ctl)
{
    // execute only inner query
    ctx_assert(m_inner != 0);
    m_inner->execute(ctx, ctl);
}

bool
Exec_query_join::fetchImpl(Ctx& ctx, Ctl& ctl)
{
    ctx_assert(m_inner != 0);
    ctx_assert(m_outer != 0);
    if (getData().getState() == ResultSet::State_init) {
	// fetch first row from inner
	if (! m_inner->fetch(ctx, ctl))
	    return false;
    }
    while (1) {
	if (m_outer->getData().getState() == ResultSet::State_end) {
	    // execute or re-execute outer
	    Ctl ctl(0);
	    m_outer->close(ctx);
	    if (! ctx.ok())
		return false;
	    m_outer->execute(ctx, ctl);
	    if (! ctx.ok())
		return false;
	}
	if (! m_outer->fetch(ctx, ctl)) {
	    if (! ctx.ok())
		return false;
	    // fetch next row from inner
	    if (! m_inner->fetch(ctx, ctl))
		return false;
	}
	else
	    return true;
    }
}

void
Exec_query_join::close(Ctx& ctx)
{
    ctx_assert(m_inner != 0);
    m_inner->close(ctx);
    ctx_assert(m_outer != 0);
    m_outer->close(ctx);
}

void
Exec_query_join::print(Ctx& ctx)
{
    ctx.print(" [query_join");
    Exec_base* a[] = { m_inner, m_outer };
    printList(ctx, a, 2);
    ctx.print("]");
}