int
main(int argc, char **argv)
{
	int ch;
	char *query = NULL;
	while ((ch = getopt(argc, argv, "cs:w")) != -1) {
		switch (ch) {
		case 'c':
			makedb(1);
			break;
		case 's':
			query = optarg;
			break;
		case 'w':
			makedb(0);
			break;
		case '?':
		default:
			usage();
		}
	}
	
	argc -= optind;
	argv += optind;
	
	if (query) {
		query_db(query);
	}
		
	return 0;
}
Beispiel #2
0
QSqlDatabase DatabaseFactory::sqliteInitializeInMemoryDatabase() {
  QSqlDatabase database = QSqlDatabase::addDatabase(APP_DB_SQLITE_DRIVER);

  database.setDatabaseName(QSL(":memory:"));

  if (!database.open()) {
    qFatal("In-memory SQLite database was NOT opened. Delivered error message: '%s'", qPrintable(database.lastError().text()));
  }
  else {
    QSqlQuery query_db(database);

    query_db.setForwardOnly(true);
    query_db.exec(QSL("PRAGMA encoding = \"UTF-8\""));
    query_db.exec(QSL("PRAGMA synchronous = OFF"));
    query_db.exec(QSL("PRAGMA journal_mode = MEMORY"));
    query_db.exec(QSL("PRAGMA page_size = 4096"));
    query_db.exec(QSL("PRAGMA cache_size = 16384"));
    query_db.exec(QSL("PRAGMA count_changes = OFF"));
    query_db.exec(QSL("PRAGMA temp_store = MEMORY"));

    // Sample query which checks for existence of tables.
    query_db.exec(QSL("SELECT inf_value FROM Information WHERE inf_key = 'schema_version'"));

    if (query_db.lastError().isValid()) {
      qWarning("Error occurred. In-memory SQLite database is not initialized. Initializing now.");

      QFile file_init(APP_MISC_PATH + QDir::separator() + APP_DB_SQLITE_INIT);

      if (!file_init.open(QIODevice::ReadOnly | QIODevice::Text)) {
        // Database initialization file not opened. HUGE problem.
        qFatal("In-memory SQLite database initialization file '%s' from directory '%s' was not found. In-memory database is uninitialized.",
               APP_DB_SQLITE_INIT,
               qPrintable(APP_MISC_PATH));
      }

      const QStringList statements = QString(file_init.readAll()).split(APP_DB_COMMENT_SPLIT, QString::SkipEmptyParts);
      database.transaction();

      foreach(const QString &statement, statements) {
        query_db.exec(statement);

        if (query_db.lastError().isValid()) {
          qFatal("In-memory SQLite database initialization failed. Initialization script '%s' is not correct.", APP_DB_SQLITE_INIT);
        }
      }

      database.commit();
      qDebug("In-memory SQLite database backend should be ready now.");
    }
Beispiel #3
0
int receive(SOCKET client_socket){

	
	char recvbuf[DEFAULT_BUFLEN];
	int iResult;
	int recvbuflen = DEFAULT_BUFLEN;
	struct sockaddr_in address;
	int addrlen = sizeof(struct sockaddr_in);

	getpeername(client_socket, (struct sockaddr*)&address, (int*)&addrlen);

	// Receive until the peer shuts down the connection
	iResult = recv(client_socket, recvbuf, recvbuflen, 0);
		
	if (iResult > 0) {
		recvbuf[iResult] = '\0';
		printf("Bytes received: %d\n", iResult);
		printf("Received: %s \n", recvbuf);
		if (is_select_stament(recvbuf) == 0){
			query_db(recvbuf, query_callback, client_socket);
		}
		else{
			printf("Was not a select stament\n");		
		}
		send_packet(client_socket, "\"end\"", strlen("\"end\""));
	} else if (iResult == 0){
		printf("Host disconnected , ip %s , port %d \n", inet_ntoa(address.sin_addr), ntohs(address.sin_port));
	}else if (iResult == SOCKET_ERROR){
		int errorcode = WSAGetLastError();
		if (errorcode  == WSAECONNRESET){
			printf("Host disconnected unexpectedly , ip %s , port %d \n", inet_ntoa(address.sin_addr), ntohs(address.sin_port));
		}
		else{
			printf("recv failed: %d\n", errorcode);
		}
			
	}



	return iResult;

}