void StatementExecutor::bindResult(MYSQL_BIND* result) { if (_state < STMT_COMPILED) throw StatementException("Statement is not compiled yet"); if (mysql_stmt_bind_result(_pHandle, result) != 0) throw StatementException("mysql_stmt_bind_result error ", _pHandle, _query); }
bool StatementExecutor::fetchColumn(std::size_t n, MYSQL_BIND *bind) { if (_state < STMT_EXECUTED) throw StatementException("Statement is not executed yet"); int res = mysql_stmt_fetch_column(_pHandle, bind, static_cast<unsigned int>(n), 0); if ((res != 0) && (res != MYSQL_NO_DATA)) throw StatementException(Poco::format("mysql_stmt_fetch_column(%z) error", n), _pHandle, _query); return (res == 0); }
bool StatementExecutor::fetch() { if (_state < STMT_EXECUTED) throw StatementException("Statement is not executed yet"); int res = mysql_stmt_fetch(_pHandle); // we have specified zero buffers for BLOBs, so DATA_TRUNCATED is normal in this case if ((res != 0) && (res != MYSQL_NO_DATA) && (res != MYSQL_DATA_TRUNCATED)) throw StatementException("mysql_stmt_fetch error", _pHandle, _query); return (res == 0) || (res == MYSQL_DATA_TRUNCATED); }
void StatementExecutor::bindParams(MYSQL_BIND* params, std::size_t count) { if (_state < STMT_COMPILED) throw StatementException("Statement is not compiled yet"); if (count != mysql_stmt_param_count(_pHandle)) throw StatementException("wrong bind parameters count", 0, _query); if (count == 0) return; if (mysql_stmt_bind_param(_pHandle, params) != 0) throw StatementException("mysql_stmt_bind_param() error ", _pHandle, _query); }
void StatementExecutor::bindResult(MYSQL_BIND* result) { if (_state < STMT_COMPILED) { throw StatementException("Satement is not compiled yet"); } int res = mysql_stmt_bind_result(h, result); if (res != 0) { throw StatementException("mysql_stmt_bind_result error ", h, _query); } }
void StatementExecutor::execute() { if (_state < STMT_COMPILED) throw StatementException("Statement is not compiled yet"); if (mysql_stmt_execute(_pHandle) != 0) throw StatementException("mysql_stmt_execute error", _pHandle, _query); _state = STMT_EXECUTED; my_ulonglong affectedRows = mysql_affected_rows(_pSessionHandle); if (affectedRows != ((my_ulonglong) - 1)) _affectedRowCount = static_cast<std::size_t>(affectedRows); //Was really a DELETE, UPDATE or INSERT statement }
void StatementExecutor::execute() { if (_state < STMT_COMPILED) { throw StatementException("Satement is not compiled yet"); } int res = mysql_stmt_execute(h); if (res != 0) { throw StatementException("mysql_stmt_execute error", h, _query); } _state = STMT_EXECUTED; }
unsigned Statement::GetFieldsCount() const { unsigned FieldsCount = 0; if (SQLNumResultCols(StmtHldr->GetHandle(), reinterpret_cast<SQLSMALLINT *>(&FieldsCount)) != SQL_SUCCESS) StatementException("Error get fields count", seErrorGetFieldsCount); return FieldsCount; }
void Statement::ExecuteDirect(const std::string &query) { CloseCursor(); if (SQLExecDirect(StmtHldr->GetHandle(), const_cast<SQLCHAR *>(reinterpret_cast<const SQLCHAR *>(query.c_str())), SQL_NTS) != SQL_SUCCESS) throw StatementException(GetLastDBError(SQL_HANDLE_STMT, StmtHldr->GetHandle()), seErrorExecute); IsOpen = true; }
bool StatementExecutor::fetch() { if (_state < STMT_EXECUTED) { throw StatementException("Satement is not executed yet"); } int res = mysql_stmt_fetch(h); if ((res != 0) && (res != MYSQL_NO_DATA)) { throw StatementException("mysql_stmt_fetch error", h, _query); } return (res == 0); }
void FunctionStatement::checkTree(Type* functionType) { if (func_->getFunction().get() == nullptr) { std::string error = ""; if (func_->getNamespace().length() > 0) { error = std::string("Function ") + func_->getNamespace() + "." + func_->getName() + " could not be resolved. " + func_->getResolveIssue(); } else { error = std::string("Function ") + func_->getName() + " could not be resolved. " + func_->getResolveIssue(); } throw StatementException(this, std::string("Function ") + func_->getNamespace() + "." + func_->getName() + " could not be resolved. " + func_->getResolveIssue()); } StatementAssert(this, func_->getFunction()->getSignature().argumentsEqual( func_->getTargetArguments()), "The resolved function has incorrect arguments. This is an internal compiler issue."); }
void ODBCStatementImpl::clear() { SQLRETURN rc = SQLCloseCursor(_stmt); _stepCalled = false; _affectedRowCount = 0; if (Utility::isError(rc)) { StatementError err(_stmt); bool ignoreError = false; const StatementDiagnostics& diagnostics = err.diagnostics(); //ignore "Invalid cursor state" error //(returned by 3.x drivers when cursor is not opened) for (int i = 0; i < diagnostics.count(); ++i) { if (ignoreError = (INVALID_CURSOR_STATE == std::string(diagnostics.sqlState(i)))) { break; } } if (!ignoreError) throw StatementException(_stmt, "SQLCloseCursor()"); } }
Poco::UInt32 ODBCStatementImpl::next() { std::size_t count = 0; if (nextRowReady()) { Extractions& extracts = extractions(); Extractions::iterator it = extracts.begin(); Extractions::iterator itEnd = extracts.end(); std::size_t prevCount = 0; for (std::size_t pos = 0; it != itEnd; ++it) { count = (*it)->extract(pos); if (prevCount && count != prevCount) throw IllegalStateException("Different extraction counts"); prevCount = count; pos += (*it)->numOfColumnsHandled(); } _stepCalled = false; } else { throw StatementException(_stmt, std::string("Iterator Error: trying to access the next value")); } return static_cast<Poco::UInt32>(count); return 0; }
const Parameter& Statement::GetParameter(unsigned index) const { ++index; ParameterPool::const_iterator Iter = Parameters.find(index); if (Iter == Parameters.end()) throw StatementException("Parameters not exists", seParameterNotExists); return *Iter->second.Get(); }
void Statement::Execute() { CloseCursor(); SQLRETURN RetCode = SQLExecute(StmtHldr->GetHandle()); if (RetCode != SQL_SUCCESS) throw StatementException(GetLastDBError(SQL_HANDLE_STMT, StmtHldr->GetHandle()), seErrorExecute); IsOpen = true; }
void StatementExecutor::prepare(const std::string& query) { if (_state >= STMT_COMPILED) { throw StatementException("Satement is already compiled"); } // compile int res = mysql_stmt_prepare(h, query.c_str(), static_cast<unsigned int>(query.length())); if (res != 0) { throw StatementException("mysql_stmt_prepare error", h, query); } _query = query; _state = STMT_COMPILED; }
StatementExecutor::StatementExecutor(MYSQL* mysql) : _pSessionHandle(mysql) , _affectedRowCount(0) { if (!(_pHandle = mysql_stmt_init(mysql))) throw StatementException("mysql_stmt_init error"); _state = STMT_INITED; }
bool Statement::Fetch() { SQLRETURN Ret = SQLFetch(StmtHldr->GetHandle()); if (Ret == SQL_NO_DATA_FOUND) return false; if (Ret != SQL_SUCCESS && Ret != SQL_SUCCESS_WITH_INFO) throw StatementException(GetLastDBError(SQL_HANDLE_STMT, StmtHldr->GetHandle()), seErrorFetch); return true; }
void Statement::CloseCursor() { if (IsOpen) { if (SQLCloseCursor(StmtHldr->GetHandle()) != SQL_SUCCESS) throw StatementException("Error close cursor", seErrorCloseCursor); IsOpen = false; } }
bool StatementExecutor::fetchColumn(size_t n, MYSQL_BIND *bind) { if (_state < STMT_EXECUTED) { throw StatementException("Satement is not executed yet"); } int res = mysql_stmt_fetch_column(h, bind, static_cast<unsigned int>(n), 0); if ((res != 0) && (res != MYSQL_NO_DATA)) { std::ostringstream msg; msg << "mysql_stmt_fetch_column(" << n << ") error"; throw StatementException(msg.str(), h, _query); } return (res == 0); }
StatementExecutor::StatementExecutor(MYSQL* mysql) { h = mysql_stmt_init(mysql); if (!h) { throw StatementException("mysql_stmt_init error"); } _state = STMT_INITED; }
Preparator::Preparator(const StatementHandle& rStmt, const std::string& statement, std::size_t maxFieldSize, DataExtraction dataExtraction): _rStmt(rStmt), _maxFieldSize(maxFieldSize), _dataExtraction(dataExtraction) { SQLCHAR* pStr = (SQLCHAR*) statement.c_str(); if (Utility::isError(Poco::Data::ODBC::SQLPrepare(_rStmt, pStr, (SQLINTEGER) statement.length()))) throw StatementException(_rStmt); }
Preparation::Preparation(const StatementHandle& rStmt, const std::string& statement, std::size_t maxFieldSize, DataExtraction dataExtraction): _rStmt(rStmt), _maxFieldSize(maxFieldSize), _dataExtraction(dataExtraction) { POCO_SQLCHAR* pStr = (POCO_SQLCHAR*) statement.c_str(); if (Utility::isError(SQLPrepare(_rStmt, pStr, (SQLINTEGER) statement.length()))) throw StatementException(_rStmt); SQLSMALLINT nCol; if (Utility::isError(SQLNumResultCols(_rStmt, &nCol))) throw StatementException(_rStmt); if (nCol) { _pValues.resize(nCol, 0); _pLengths.resize(nCol, 0); } }
void StatementExecutor::prepare(const std::string& query) { if (_state >= STMT_COMPILED) { _state = STMT_COMPILED; return; } if (mysql_stmt_prepare(_pHandle, query.c_str(), static_cast<unsigned int>(query.length())) != 0) throw StatementException("mysql_stmt_prepare error", _pHandle, query); _query = query; _state = STMT_COMPILED; }
void ODBCStatementImpl::checkError(SQLRETURN rc, const std::string& msg) { if (SQL_NO_DATA == rc) return; if (Utility::isError(rc)) { std::ostringstream os; os << std::endl << "Requested SQL statement: " << toString() << std::endl; os << "Native SQL statement: " << nativeSQL() << std::endl; std::string str(msg); str += os.str(); throw StatementException(_stmt, str); } }
void StatementExecutor::bindParams(MYSQL_BIND* params, size_t count) { if (_state < STMT_COMPILED) { throw StatementException("Satement is not compiled yet"); } if (count != mysql_stmt_param_count(h)) { throw StatementException("wrong bind parameters count", 0, _query); } if (count == 0) { return; } int res = mysql_stmt_bind_param(h, params); if (res != 0) { throw StatementException("mysql_stmt_bind_param() error ", h, _query); } }
std::size_t MySQLStatementImpl::next() { if (!hasNext()) throw StatementException("No data received"); Poco::Data::AbstractExtractionVec::iterator it = extractions().begin(); Poco::Data::AbstractExtractionVec::iterator itEnd = extractions().end(); std::size_t pos = 0; for (; it != itEnd; ++it) { (*it)->extract(pos); pos += (*it)->numOfColumnsHandled(); } _hasNext = NEXT_DONTKNOW; return 1; }
void ODBCStatementImpl::putData() { SQLPOINTER pParam = 0; SQLINTEGER dataSize = 0; SQLRETURN rc; while (SQL_NEED_DATA == (rc = SQLParamData(_stmt, &pParam))) { poco_assert_dbg (pParam); dataSize = (SQLINTEGER) _pBinder->parameterSize(pParam); if (Utility::isError(SQLPutData(_stmt, pParam, dataSize))) throw StatementException(_stmt, "SQLPutData()"); } checkError(rc, "SQLParamData()"); }
void ODBCMetaColumn::getDescription() { std::memset(_columnDesc.name, 0, NAME_BUFFER_LENGTH); _columnDesc.nameBufferLength = 0; _columnDesc.dataType = 0; _columnDesc.size = 0; _columnDesc.decimalDigits = 0; _columnDesc.isNullable = 0; if (Utility::isError(Poco::Data::ODBC::SQLDescribeCol(_rStmt, (SQLUSMALLINT) position() + 1, // ODBC columns are 1-based _columnDesc.name, NAME_BUFFER_LENGTH, &_columnDesc.nameBufferLength, &_columnDesc.dataType, &_columnDesc.size, &_columnDesc.decimalDigits, &_columnDesc.isNullable))) { throw StatementException(_rStmt); } }
void Preparator::prepareBoolArray(std::size_t pos, SQLSMALLINT valueType, std::size_t length) { poco_assert_dbg (DE_BOUND == _dataExtraction); poco_assert_dbg (pos < _values.size()); poco_assert_dbg (pos < _lengths.size()); poco_assert_dbg (pos < _lenLengths.size()); bool* pArray = (bool*) std::calloc(length, sizeof(bool)); _values[pos] = Any(pArray); _lengths[pos] = 0; _lenLengths[pos].resize(length); _varLengthArrays.insert(IndexMap::value_type(pos, DT_BOOL_ARRAY)); if (Utility::isError(SQLBindCol(_rStmt, (SQLUSMALLINT) pos + 1, valueType, (SQLPOINTER) pArray, (SQLINTEGER) sizeof(bool), &_lenLengths[pos][0]))) { throw StatementException(_rStmt, "SQLBindCol()"); } }