2014-12-15 16:43:32 +01:00
|
|
|
#ifndef __PREPARE_STATEMENT_H__
|
|
|
|
#define __PREPARE_STATEMENT_H__
|
|
|
|
|
2016-02-27 14:24:23 +01:00
|
|
|
#include "../SQLParserResult.h"
|
2014-12-15 16:43:32 +01:00
|
|
|
#include "SQLStatement.h"
|
|
|
|
#include "SelectStatement.h"
|
2015-01-06 15:29:18 +01:00
|
|
|
#include <algorithm>
|
2014-12-15 16:43:32 +01:00
|
|
|
|
|
|
|
namespace hsql {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @struct PrepareStatement
|
2014-12-15 18:32:46 +01:00
|
|
|
* @brief Represents "PREPARE ins_prep: SELECT * FROM t1 WHERE c1 = ? AND c2 = ?"
|
2014-12-15 16:43:32 +01:00
|
|
|
*/
|
|
|
|
struct PrepareStatement : SQLStatement {
|
|
|
|
PrepareStatement() :
|
|
|
|
SQLStatement(kStmtPrepare),
|
|
|
|
name(NULL),
|
2015-01-06 15:29:18 +01:00
|
|
|
query(NULL) {}
|
2014-12-15 16:43:32 +01:00
|
|
|
|
2014-12-15 18:32:46 +01:00
|
|
|
virtual ~PrepareStatement() {
|
2015-01-06 15:29:18 +01:00
|
|
|
delete query;
|
2014-12-15 18:32:46 +01:00
|
|
|
delete name;
|
|
|
|
}
|
2014-12-15 16:43:32 +01:00
|
|
|
|
2015-01-06 15:29:18 +01:00
|
|
|
/**
|
|
|
|
* @param vector of placeholders that the parser found
|
|
|
|
*
|
|
|
|
* When setting the placeholders we need to make sure that they are in the correct order.
|
|
|
|
* To ensure that, during parsing we store the character position use that to sort the list here.
|
|
|
|
*/
|
|
|
|
void setPlaceholders(std::vector<void*> ph) {
|
|
|
|
for (void* e : ph) {
|
|
|
|
if (e != NULL)
|
|
|
|
placeholders.push_back((Expr*) e);
|
|
|
|
}
|
|
|
|
// Sort by col-id
|
|
|
|
std::sort(placeholders.begin(), placeholders.end(), [](Expr* i, Expr* j) -> bool { return (i->ival < j->ival); });
|
|
|
|
|
|
|
|
// Set the placeholder id on the Expr. This replaces the previously stored column id
|
|
|
|
for (uint i = 0; i < placeholders.size(); ++i) placeholders[i]->ival = i;
|
|
|
|
}
|
|
|
|
|
2014-12-15 16:43:32 +01:00
|
|
|
const char* name;
|
2016-02-27 14:24:23 +01:00
|
|
|
SQLParserResult* query;
|
2015-01-06 15:29:18 +01:00
|
|
|
std::vector<Expr*> placeholders;
|
2014-12-15 16:43:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namsepace hsql
|
|
|
|
#endif
|