void SessionPool::setProperty(const std::string& name, const Poco::Any& value) { Poco::Mutex::ScopedLock lock(_mutex); if (_shutdown) throw InvalidAccessException("Session pool has been shut down."); if (_nSessions > 0) throw InvalidAccessException("Properties can not be set after first session was created."); _propertyMap.insert(PropertyMap::ValueType(name, value)); }
void SessionPool::setFeature(const std::string& name, bool state) { Poco::Mutex::ScopedLock lock(_mutex); if (_shutdown) throw InvalidAccessException("Session pool has been shut down."); if (_nSessions > 0) throw InvalidAccessException("Features can not be set after the first session was created."); _featureMap.insert(FeatureMap::ValueType(name, state)); }
void SessionImpl::begin() { if (isAutoCommit()) throw InvalidAccessException("Session in auto commit mode."); { Poco::FastMutex::ScopedLock l(_mutex); if (_inTransaction) throw InvalidAccessException("Transaction in progress."); _inTransaction = true; } }
void Transaction::begin() { if (!_rSession.isTransaction()) _rSession.begin(); else throw InvalidAccessException("Transaction in progress."); }
Session SessionPool::get() { Poco::Mutex::ScopedLock lock(_mutex); if (_shutdown) throw InvalidAccessException("Session pool has been shut down."); purgeDeadSessions(); if (_idleSessions.empty()) { if (_nSessions < _maxSessions) { Session newSession(SessionFactory::instance().create(_connector, _connectionString)); applySettings(newSession.impl()); customizeSession(newSession); PooledSessionHolderPtr pHolder(new PooledSessionHolder(*this, newSession.impl())); _idleSessions.push_front(pHolder); ++_nSessions; } else throw SessionPoolExhaustedException(_connector); } PooledSessionHolderPtr pHolder(_idleSessions.front()); PooledSessionImplPtr pPSI(new PooledSessionImpl(pHolder)); _activeSessions.push_front(pHolder); _idleSessions.pop_front(); return Session(pPSI); }
Poco::Dynamic::Var RecordSet::value(const std::string& name, std::size_t row, bool useFilter) const { if (useFilter && isFiltered() && !isAllowed(row)) throw InvalidAccessException("Row not allowed"); if (isNull(metaColumn(name).position(), row)) return Poco::Dynamic::Var(); switch (columnType(name)) { case MetaColumn::FDT_BOOL: return value<bool>(name, row, useFilter); case MetaColumn::FDT_INT8: return value<Int8>(name, row, useFilter); case MetaColumn::FDT_UINT8: return value<UInt8>(name, row, useFilter); case MetaColumn::FDT_INT16: return value<Int16>(name, row, useFilter); case MetaColumn::FDT_UINT16: return value<UInt16>(name, row, useFilter); case MetaColumn::FDT_INT32: return value<Int32>(name, row, useFilter); case MetaColumn::FDT_UINT32: return value<UInt32>(name, row, useFilter); case MetaColumn::FDT_INT64: return value<Int64>(name, row, useFilter); case MetaColumn::FDT_UINT64: return value<UInt64>(name, row, useFilter); case MetaColumn::FDT_FLOAT: return value<float>(name, row, useFilter); case MetaColumn::FDT_DOUBLE: return value<double>(name, row, useFilter); case MetaColumn::FDT_STRING: return value<std::string>(name, row, useFilter); case MetaColumn::FDT_WSTRING: return value<UTF16String>(name, row, useFilter); case MetaColumn::FDT_BLOB: return value<BLOB>(name, row, useFilter); case MetaColumn::FDT_DATE: return value<Date>(name, row, useFilter); case MetaColumn::FDT_TIME: return value<Time>(name, row, useFilter); case MetaColumn::FDT_TIMESTAMP: return value<DateTime>(name, row, useFilter); default: throw UnknownTypeException("Data type not supported."); } }
void SerialConfigImpl::setXoffCharImpl(unsigned char xOff) { if (FLOW_CTRL_HARDWARE_IMPL == getFlowControlImpl()) throw InvalidAccessException(); _termios.c_cc[VSTOP] = xOff; }
Session SessionPool::get(SessionPool::SessionDataPtr &session_data_ptr) { Poco::Mutex::ScopedLock lock(_mutex); if (_shutdown) throw InvalidAccessException("Session pool has been shut down."); purgeDeadSessions(); if (_idleSessions.empty()) { Session newSession(SessionFactory::instance().create(_connector, _connectionString)); applySettings(newSession.impl()); PooledSessionHolderPtr pHolder(new PooledSessionHolder(*this, newSession.impl())); session_data_ptr.assign(new SessionData); session_data_ptr->session = pHolder; _idleSessions.push_front(session_data_ptr); ++_nSessions; } PooledSessionHolderPtr pHolder(_idleSessions.front()->session); PooledSessionImplPtr pPSI(new PooledSessionImpl(pHolder)); _activeSessions.push_front(std::move(_idleSessions.front())); _idleSessions.pop_front(); if (session_data_ptr.isNull()) { session_data_ptr.assign(_activeSessions.front()); } return Session(pPSI); }
void SessionImpl::open(const std::string& connect) { if (connect != connectionString()) { if (isConnected()) throw InvalidAccessException("Session already connected"); if (!connect.empty()) setConnectionString(connect); } poco_assert_dbg (!connectionString().empty()); try { ActiveConnector connector(connectionString(), &_pDB); ActiveResult<int> result = connector.connect(); if (!result.tryWait(getLoginTimeout() * 1000)) throw ConnectionFailedException("Timed out."); int rc = result.data(); if (rc != 0) { close(); Utility::throwException(rc); } } catch (SQLiteException& ex) { throw ConnectionFailedException(ex.displayText()); } _connected = true; }
int SerialChannelImpl::readImpl(char*& pBuffer) { if (!_pConfig->getUseEOFImpl()) throw InvalidAccessException(); int bufSize = _pConfig->getBufferSizeImpl(); int it = 1; if ((0 == bufSize) || (0 != pBuffer)) throw InvalidArgumentException(); std::string buffer; DWORD read = 0; DWORD readCount = 0; pBuffer = static_cast<char*>(std::calloc(bufSize, sizeof(char)));//! freed in parent call do { if (_leftOver.size()) { read = _leftOver.size() > bufSize - readCount ? bufSize - readCount : _leftOver.size(); std::memcpy(pBuffer + readCount, _leftOver.data(), read); if (read == _leftOver.size()) _leftOver.clear(); else _leftOver.assign(_leftOver, read, _leftOver.size() - read); } else { if (!ReadFile(_handle, pBuffer + readCount, bufSize - readCount, &read, NULL)) handleError(_pConfig->name()); else if (0 == read) break; } poco_assert (read <= bufSize - readCount); buffer.assign(static_cast<char*>(pBuffer + readCount), read); size_t pos = buffer.find(_pConfig->getEOFCharImpl()); if (pos != buffer.npos) { readCount += static_cast<DWORD>(pos); PurgeComm(_handle, PURGE_RXCLEAR); _leftOver.assign(buffer, pos + 1, buffer.size() - pos - 1); break; } readCount += read; if (readCount >= bufSize) { bufSize *= ++it; pBuffer = static_cast<char*>(std::realloc(pBuffer, bufSize * sizeof(char))); } }while(true); return readCount; }
void SerialConfigImpl::setUseXonXoffImpl(unsigned char xOnChar, unsigned char xOffChar) { if (xOnChar != xOffChar) { _dcb.XonChar = xOnChar; _dcb.XoffChar = xOffChar; } else throw InvalidAccessException("XON == XOFF - not set."); }
char& Var::at(std::size_t n) { if (isString()) { return holderImpl<std::string, InvalidAccessException>("Not a string.")->operator[](n); } throw InvalidAccessException("Not a string."); }
bool SessionPool::getFeature(const std::string& name) { FeatureMap::ConstIterator it = _featureMap.find(name); if (_shutdown) throw InvalidAccessException("Session pool has been shut down."); if (_featureMap.end() == it) throw NotFoundException("Feature not found:" + name); return it->second; }
bool SessionImpl::hasTransactionIsolation(Poco::UInt32 ti) { if (isTransaction()) throw InvalidAccessException(); bool retval = true; Poco::UInt32 old = getTransactionIsolation(); try { setTransactionIsolation(ti); } catch (Poco::Exception&) { retval = false; } setTransactionIsolation(old); return retval; }
const MetaColumn& ODBCStatementImpl::metaColumn(Poco::UInt32 pos) const { Poco::UInt32 curDataSet = currentDataSet(); poco_assert_dbg (curDataSet < _columnPtrs.size()); std::size_t sz = _columnPtrs[curDataSet].size(); if (0 == sz || pos > sz - 1) throw InvalidAccessException(format("Invalid column number: %u", pos)); return *_columnPtrs[curDataSet][pos]; }
SerialConfigImpl::DataBitsImpl SerialConfigImpl::getDataBitsImpl() const { if (_termios.c_cflag & DATA_BITS_EIGHT_IMPL) return DATA_BITS_EIGHT_IMPL; else if (_termios.c_cflag & DATA_BITS_SEVEN_IMPL) return DATA_BITS_SEVEN_IMPL; else if (_termios.c_cflag & DATA_BITS_SIX_IMPL) return DATA_BITS_SIX_IMPL; else if (_termios.c_cflag & DATA_BITS_FIVE_IMPL) return DATA_BITS_FIVE_IMPL; throw InvalidAccessException("Number of data bits not set"); }
char SerialConfigImpl::getParityCharImpl() const { if (_termios.c_iflag & ~(INPCK)) return 'N'; else { if (_termios.c_cflag & (PARODD & PARENB)) return 'O'; else if (_termios.c_cflag & (~(PARODD) & PARENB)) return 'E'; } throw InvalidAccessException("Parity not set"); }
void SessionImpl::open(const std::string& connect) { if (connect != connectionString()) { if (isConnected()) throw InvalidAccessException("Session already connected"); if (!connect.empty()) setConnectionString(connect); } poco_assert_dbg (!connectionString().empty()); try { ActiveConnector connector(connectionString(), &_pDB); ActiveResult<int> result = connector.connect(); if (!result.tryWait(getLoginTimeout() * 1000)) throw ConnectionFailedException("Timed out."); int rc = result.data(); if (rc != 0) { close(); Utility::throwException(rc); } } catch (SQLiteException& ex) { throw ConnectionFailedException(ex.displayText()); } if (SQLITE_OK != sqlite3_exec(_pDB, "attach database ':memory:' as sys;" "create table sys.dual (dummy);", 0, 0, 0)) { throw ExecutionException("Cannot create system database."); } _connected = true; }
void SessionImpl::open(const std::string& connect) { if (connect != connectionString()) { if (isConnected()) throw InvalidAccessException("Session already connected"); if (!connect.empty()) setConnectionString(connect); } poco_assert_dbg (!connectionString().empty()); _handle.init(); unsigned int timeout = static_cast<unsigned int>(getLoginTimeout()); _handle.options(MYSQL_OPT_CONNECT_TIMEOUT, timeout); std::map<std::string, std::string> options; // Default values options["host"] = "localhost"; options["port"] = "3306"; options["user"] = ""; options["password"] = ""; options["db"] = ""; options["compress"] = ""; options["auto-reconnect"] = ""; options["secure-auth"] = ""; options["character-set"] = "utf8"; const std::string& connString = connectionString(); for (std::string::const_iterator start = connString.begin();;) { std::string::const_iterator finish = std::find(start, connString.end(), ';'); std::string::const_iterator middle = std::find(start, finish, '='); if (middle == finish) throw MySQLException("create session: bad connection string format, can not find '='"); options[copyStripped(start, middle)] = copyStripped(middle + 1, finish); if ((finish == connString.end()) || (finish + 1 == connString.end())) break; start = finish + 1; } if (options["user"].empty()) throw MySQLException("create session: specify user name"); const char * db = NULL; if (!options["db"].empty()) db = options["db"].c_str(); unsigned int port = 0; if (!NumberParser::tryParseUnsigned(options["port"], port) || 0 == port || port > 65535) throw MySQLException("create session: specify correct port (numeric in decimal notation)"); if (options["compress"] == "true") _handle.options(MYSQL_OPT_COMPRESS); else if (options["compress"] == "false") ; else if (!options["compress"].empty()) throw MySQLException("create session: specify correct compress option (true or false) or skip it"); if (options["auto-reconnect"] == "true") _handle.options(MYSQL_OPT_RECONNECT, true); else if (options["auto-reconnect"] == "false") _handle.options(MYSQL_OPT_RECONNECT, false); else if (!options["auto-reconnect"].empty()) throw MySQLException("create session: specify correct auto-reconnect option (true or false) or skip it"); if (options["secure-auth"] == "true") _handle.options(MYSQL_SECURE_AUTH, true); else if (options["secure-auth"] == "false") _handle.options(MYSQL_SECURE_AUTH, false); else if (!options["secure-auth"].empty()) throw MySQLException("create session: specify correct secure-auth option (true or false) or skip it"); if (!options["character-set"].empty()) _handle.options(MYSQL_SET_CHARSET_NAME, options["character-set"].c_str()); // Real connect _handle.connect(options["host"].c_str(), options["user"].c_str(), options["password"].c_str(), db, port); addFeature("autoCommit", &SessionImpl::autoCommit, &SessionImpl::isAutoCommit); _connected = true; }
void SessionImpl::open(const std::string& connect) { if (connect != connectionString()) { if (isConnected()) throw InvalidAccessException("Session already connected"); if (!connect.empty()) setConnectionString(connect); } poco_assert_dbg (!connectionString().empty()); SQLULEN tout = static_cast<SQLULEN>(getLoginTimeout()); if (Utility::isError(SQLSetConnectAttr(_db, SQL_ATTR_LOGIN_TIMEOUT, (SQLPOINTER) tout, 0))) { if (Utility::isError(SQLGetConnectAttr(_db, SQL_ATTR_LOGIN_TIMEOUT, &tout, 0, 0)) || getLoginTimeout() != tout) { ConnectionError e(_db); throw ConnectionFailedException(e.toString()); } } SQLCHAR connectOutput[512] = {0}; SQLSMALLINT result; if (Utility::isError(Poco::Data::ODBC::SQLDriverConnect(_db , NULL ,(SQLCHAR*) connectionString().c_str() ,(SQLSMALLINT) SQL_NTS , connectOutput , sizeof(connectOutput) , &result , SQL_DRIVER_NOPROMPT))) { ConnectionError err(_db); std::string errStr = err.toString(); close(); throw ConnectionFailedException(errStr); } _dataTypes.fillTypeInfo(_db); addProperty("dataTypeInfo", &SessionImpl::setDataTypeInfo, &SessionImpl::dataTypeInfo); addFeature("autoCommit", &SessionImpl::autoCommit, &SessionImpl::isAutoCommit); addFeature("autoBind", &SessionImpl::autoBind, &SessionImpl::isAutoBind); addFeature("autoExtract", &SessionImpl::autoExtract, &SessionImpl::isAutoExtract); addProperty("maxFieldSize", &SessionImpl::setMaxFieldSize, &SessionImpl::getMaxFieldSize); addProperty("queryTimeout", &SessionImpl::setQueryTimeout, &SessionImpl::getQueryTimeout); Poco::Data::ODBC::SQLSetConnectAttr(_db, SQL_ATTR_QUIET_MODE, 0, 0); if (!canTransact()) autoCommit("", true); }