Code_root.cpp 7.59 KB
Newer Older
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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
/* 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 <common/StmtArea.hpp>
#include "Code_root.hpp"
#include "Code_stmt.hpp"
#include "Code_query.hpp"
#include "Code_expr_param.hpp"
#include "Code_root.hpp"

// Plan_root

Plan_root::~Plan_root()
{
}

Plan_base*
Plan_root::analyze(Ctx& ctx, Ctl& ctl)
{
    // analyze statement
    ctx_assert(m_stmt != 0);
    m_stmt = static_cast<Plan_stmt*>(m_stmt->analyze(ctx, ctl));
    if (! ctx.ok())
	return 0;
    ctx_assert(m_stmt != 0);
    // analyze parameters
    ctx_assert(m_paramList.size() > 0);
    const unsigned paramCount = m_paramList.size() - 1;
    DescArea& ipd = descArea(Desc_usage_IPD);
    ipd.setCount(ctx, paramCount);
    for (unsigned i = 1; i <= paramCount; i++) {
	Plan_expr_param* param = m_paramList[i];
	ctx_assert(param != 0);
	// analyze the parameter
	param->analyze(ctx, ctl);
	if (! ctx.ok())
	    return 0;
    }
    // must return self
    return this;
}

void
Plan_root::describe(Ctx& ctx)
{
    // describe statement
    ctx_assert(m_stmt != 0);
    m_stmt->describe(ctx);
    // describe parameters
    ctx_assert(m_paramList.size() > 0);
    const unsigned paramCount = m_paramList.size() - 1;
    DescArea& ipd = descArea(Desc_usage_IPD);
    ipd.setCount(ctx, paramCount);
    unsigned unbound = 0;
    for (unsigned i = 1; i <= paramCount; i++) {
	Plan_expr_param* param = m_paramList[i];
	ctx_assert(param != 0);
	// describe the parameter
	param->describe(ctx);
	// check if SQL type is bound
	ctx_assert(param->sqlType().type() != SqlType::Undef);
	if (param->sqlType().type() == SqlType::Unbound)
	    unbound++;
    }
    if (unbound > 0)
	ctx_log2(("%u out of %u params have unbound SQL type", unbound, paramCount));
    m_stmtArea.m_unbound = unbound;
}

Exec_base*
Plan_root::codegen(Ctx& ctx, Ctl& ctl)
{
    Exec_root* execRoot = new Exec_root(m_stmtArea);
    Exec_root::Code& code = *new Exec_root::Code;
    execRoot->setCode(code);
    // set root in helper struct
    ctl.m_execRoot = execRoot;
    // generate code for the statement
    ctx_assert(m_stmt != 0);
    Exec_stmt* execStmt = static_cast<Exec_stmt*>(m_stmt->codegen(ctx, ctl));
    if (! ctx.ok())
	return 0;
    execRoot->setStmt(execStmt);
    // create parameters list
    execRoot->m_paramList.resize(m_paramList.size());
    for (unsigned i = 1; i < m_paramList.size(); i++) {
	Plan_expr_param* param = m_paramList[i];
	ctx_assert(param != 0);
	Exec_expr_param* execParam = static_cast<Exec_expr_param*>(param->codegen(ctx, ctl));
	ctx_assert(execParam != 0);
	execRoot->m_paramList[i] = execParam;
    }
    return execRoot;
}

void
Plan_root::print(Ctx& ctx)
{
    ctx.print("[root");
    Plan_base* a[] = { m_stmt };
    printList(ctx, a, 1);
    ctx.print("]\n");
}

void
Plan_root::saveNode(Plan_base* node)
{
    ctx_assert(node != 0);
    m_nodeList.push_back(node);
}

void
Plan_root::freeNodeList()
{
    for (NodeList::iterator i = m_nodeList.begin(); i != m_nodeList.end(); i++) {
	Plan_base* node = *i;
	*i = 0;
	delete node;
    }
    m_nodeList.clear();
}

// Exec_root

Exec_root::Code::~Code()
{
}

Exec_root::Data::~Data()
{
}

Exec_root::~Exec_root()
{
}

StmtArea&
Exec_root::stmtArea() const
{
    return m_stmtArea;
}

void
Exec_root::alloc(Ctx& ctx, Ctl& ctl)
{
    ctx_assert(m_stmt != 0);
    m_stmt->alloc(ctx, ctl);
}

void
Exec_root::bind(Ctx& ctx)
{
    // bind output cols
    ctx_assert(m_stmt != 0);
    m_stmt->bind(ctx);
    // bind input params
    for (unsigned i = 1; i < m_paramList.size(); i++) {
	Exec_expr_param* param = m_paramList[i];
	ctx_assert(param != 0);
	param->bind(ctx);
	if (! ctx.ok())
	    return;
    }
}

void
Exec_root::execute(Ctx& ctx, Ctl& ctl)
{
    ctx_assert(m_stmt != 0);
    // check if data is needed
    for (unsigned i = 1; i < m_paramList.size(); i++) {
	Exec_expr_param* param = m_paramList[i];
	ctx_assert(param != 0);
	Exec_expr_param::Data& paramData = param->getData();
	if (paramData.m_atExec && paramData.m_extPos == -1) {
	    ctx.setCode(SQL_NEED_DATA);
	    return;
	}
    }
    m_stmt->execute(ctx, ctl);
}

void
Exec_root::fetch(Ctx& ctx, Ctl& ctl)
{
    ctx_assert(m_stmt != 0);
    Exec_query* query = static_cast<Exec_query*>(m_stmt);
    ctx_assert(query != 0);
    query->fetch(ctx, ctl);
}

void
Exec_root::close(Ctx& ctx)
{
    ctx_assert(m_stmt != 0);
    m_stmt->close(ctx);
    for (unsigned i = 1; i < m_paramList.size(); i++) {
	Exec_expr_param* param = m_paramList[i];
	ctx_assert(param != 0);
	param->close(ctx);
    }
}

void
Exec_root::print(Ctx& ctx)
{
    ctx.print("[root");
    Exec_base* a[] = { m_stmt };
    printList(ctx, a, sizeof(a)/sizeof(a[0]));
    ctx.print("]\n");
}

void
Exec_root::saveNode(Exec_base* node)
{
    ctx_assert(node != 0);
    m_nodeList.push_back(node);
}

void
Exec_root::freeNodeList()
{
    for (NodeList::iterator i = m_nodeList.begin(); i != m_nodeList.end(); i++) {
	Exec_base* node = *i;
	*i = 0;
	delete node;
    }
    m_nodeList.clear();
}

// odbc support

void
Exec_root::sqlGetData(Ctx& ctx, SQLUSMALLINT columnNumber, SQLSMALLINT targetType, SQLPOINTER targetValue, SQLINTEGER bufferLength, SQLINTEGER* strlen_or_Ind)
{
    ctx_assert(m_stmt != 0);
    Exec_query* query = static_cast<Exec_query*>(m_stmt);
    ctx_assert(query != 0);
    query->sqlGetData(ctx, columnNumber, targetType, targetValue, bufferLength, strlen_or_Ind);
}

void
Exec_root::sqlParamData(Ctx& ctx, SQLPOINTER* value)
{
    ctx_assert(m_paramList.size() > 0);
    unsigned count = m_paramList.size() - 1;
    for (unsigned i = 1; i <= count; i++) {
	Exec_expr_param* param = m_paramList[i];
	ctx_assert(param != 0);
	Exec_expr_param::Data& paramData = param->getData();
	if (! paramData.m_atExec || paramData.m_extPos >= 0)
	    continue;
	ctx_assert(paramData.m_extField != 0);
	ExtField& extField = *paramData.m_extField;
	if (value != 0)
	    *value = extField.m_dataPtr;
	m_paramData = i;
	ctx.setCode(SQL_NEED_DATA);
	return;
    }
}

void
Exec_root::sqlPutData(Ctx& ctx, SQLPOINTER data, SQLINTEGER strlen_or_Ind)
{
    ctx_assert(m_paramList.size() > 0);
    unsigned count = m_paramList.size() - 1;
    unsigned i = m_paramData;
    if (i == 0) {
	ctx.pushStatus(Sqlstate::_HY010, Error::Gen, "missing call to SQLParamData");
	return;
    }
    if (i > count) {
	ctx.pushStatus(Sqlstate::_HY010, Error::Gen, "parameter %u out of range 1 to %u", i, count);
	return;
    }
    Exec_expr_param* param = m_paramList[i];
    ctx_assert(param != 0);
    Exec_expr_param::Data& paramData = param->getData();
    if (! paramData.m_atExec) {
	ctx.pushStatus(Sqlstate::_HY010, Error::Gen, "parameter %u not marked for data-at-exec", i);
	return;
    }
    ctx_assert(paramData.m_extField != 0);
    ExtField extField(paramData.m_extField->extSpec(), data, 0, &strlen_or_Ind, i);
    if (paramData.m_extPos == -1)
	paramData.m_extPos = 0;
    extField.setPos(paramData.m_extPos);
    // copy in and update position
    SqlField& sqlField = paramData.m_sqlField;
    sqlField.copyin(ctx, extField);
    paramData.m_extPos = extField.getPos();
    ctx_log4(("parameter %u data received", i));
}