Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
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;
}
Exemplo n.º 3
0
/*!\brief Connect auf eine SQLite-Datenbank erstellen
 *
 * \descr
 * Mit dieser Funktion wird eine Verbindung zu einer SQLite Datenbank hergestellt, wobei
 * die dafür notwendigen Parameter dem Array \p params entnommen werden.
 * \par
 * Mögliche Parameter:
 * - \b filename: Dateiname der Datenbank
 *
 * \param params Ein Assoziatives Array mit den für den Connect erforderlichen Parameter.
 *
 * \exception OutOfMemoryException
 * \exception ConnectionFailedException
 *
 */
void SQLite::connect(const AssocArray &params)
{
#ifndef HAVE_SQLITE3
	throw UnsupportedFeatureException("SQLite");
#else
	if (conn) close();
	condata=params;
	String filename=params["filename"];
	int ret=sqlite3_open((const char*)filename,(sqlite3 **)&conn);
	if (!conn) throw OutOfMemoryException();
	if (ret != SQLITE_OK) {
		String err(sqlite3_errmsg((sqlite3*)conn));
		sqlite3_close((sqlite3*)conn);
		conn=NULL;
		throw ConnectionFailedException(err);
	}
#endif
}
Exemplo n.º 4
0
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);
}
Exemplo n.º 5
0
void Web::Request::Connect() {
   
	SOCKET httpSocket;
	int ret;
	struct sockaddr_in httpAddr;
	struct hostent *hostinfo;
	char buffer[REQUESTBUFFERSIZE + 1];
	int bufferSize;
   int timeout = REQUESTTIMEOUT;

   /* Choses settings for the socket to connect to */
   httpAddr.sin_family = AF_INET;

   /* Set the port */
   //httpAddr.sin_port = htons(url->getPort());
   httpAddr.sin_port = htons(80);

   /* Do a hostname lookup on the host */
   //hostinfo = gethostbyname (url->getHost());
   hostinfo = gethostbyname ("localhost");
   if (hostinfo == NULL) {
      throw HostNotFoundException();
   }
   
   /* Fill the httpAddr with IP data */
   httpAddr.sin_addr = *(struct in_addr *) hostinfo->h_addr;
    
	/* Creates the socket */
	httpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) ;
	
	/* Check for errors */
	if (httpSocket == INVALID_SOCKET)
	    throw SocketException();

   /* Add a timeout on all communiction*/
   ret = setsockopt(httpSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));

   /* Check for errors */
   if (ret == SOCKET_ERROR) {
      closesocket(httpSocket);
      throw SocketException();
   }

   /* Connect to the server */
   ret = connect(httpSocket,(LPSOCKADDR)&httpAddr, sizeof(struct sockaddr));

   /* Check for errors */
   if (ret == SOCKET_ERROR) {
      closesocket(httpSocket);
      throw ConnectionFailedException(WSAGetLastError());
   }

   /* Create a request */
   bufferSize = _snprintf(buffer, REQUESTBUFFERSIZE, "GET %s HTTP/1.0\r\n\r\n", url->getURL());

   if (bufferSize <= 0) {
      closesocket(httpSocket);
      throw Exception("Request Buffer Too Small");
   }
      
   /* Send a HTTP request */
   ret = send(httpSocket, buffer, bufferSize, 0);

   if (ret < bufferSize) {
      closesocket(httpSocket);
      throw TransferException();
   }
   
   reply.assign("");
   ret = REQUESTBUFFERSIZE;
   
   /* Now wait for the return and loop while its coming */
   while (ret > 0) {
      ret = recv(httpSocket, buffer, REQUESTBUFFERSIZE, 0);
      
      /* Append data to the buffer */
      if (ret > 0)
         reply.append(buffer, ret);
   }

   /* Clean up the socket */
   closesocket(httpSocket);

   /* Now if we didn't get any data throw a error */
   if (reply.length()==0) {
      throw TransferException();
   }

   /* Got some data let parse it */
   ret = (int)reply.find("\r\n\r\n");

   if (ret==-1) {
      throw InvalidDataException();
   }
   
   header = reply.substr(0, ret);
   ret += 4;
   body = reply.substr(ret, reply.length() - ret);  

}