예제 #1
0
bool SQLConnector::doTableColumnsWork(std::string tableName, 
				   const std::vector<std::string>& colNames)
{
  std::stringstream ss;
  ss << "describe " << tableName << ";";
  std::shared_ptr<sql::Statement> pStatement(m_pConnection->createStatement());
  std::shared_ptr<sql::ResultSet> pResults(pStatement->executeQuery(ss.str()));

  std::map<std::string, int> colsFound;
  for (auto& colName : colNames)
    colsFound[colName] = 0;
  
  pResults->beforeFirst();
  while (pResults->next())
  {
    for (auto& colName : colNames)
    {
      if (pResults->getString("Field") == colName)
	colsFound[colName] = 1;
    }
  }
  
  for (auto& colPair : colsFound)
  {
    if (colPair.second == 0)
      return false;
  }

  return true;
}
예제 #2
0
int SQLConnector::createTable(std::string tableName,
			      const std::vector<std::string>& colNames)
{
  if (colNames.size() == 0)
  {
    std::cout << SQLLOG << " createTable provided empty colNames" 
	      << std::endl;
    return -1;
  }

  std::vector<std::string> colNamesLocal(colNames);
  std::stringstream ss;
  ss << "create table " << tableName << "\n"
     << "  (id INT AUTO_INCREMENT PRIMARY KEY,\n"
     << "   time              TIMESTAMP,\n";
  std::string last = colNamesLocal.back();
  colNamesLocal.pop_back();
  for (auto& colName : colNamesLocal)
  {
    ss << "   " << colName << "   FLOAT,\n";
  }
  ss << "   " << last << "   FLOAT );";
  std::shared_ptr<sql::Statement> pStatement(m_pConnection->createStatement());
  bool result = pStatement->execute(ss.str());

  if (!result)
  {
    std::cout << SQLLOG << " created table with:" << std::endl;
    std::cout << ss.str() << std::endl;
  }
  else
  {
    std::cout << SQLLOG << " error creating table with:" << std::endl;
    std::cout << ss.str() << std::endl;
    return -1;
  }
   
  ss.str(std::string());
  ss << "CREATE INDEX " << tableName << "_time_index ON " 
     << tableName << " (time);";
  result = pStatement->execute(ss.str());
  if (!result)
  {
    std::cout << SQLLOG << " created index with:" << std::endl;
    std::cout << ss.str() << std::endl;
  }
  else
  {
    std::cout << SQLLOG << " error creating index with:" << std::endl;
    std::cout << ss.str() << std::endl;
    return -1;
  }

  return 0;
}
예제 #3
0
bool SQLConnector::doesTableExist(std::string tableName)
{
  std::stringstream ss;
  ss << "show tables;";
  std::shared_ptr<sql::Statement> pStatement(m_pConnection->createStatement());
  std::shared_ptr<sql::ResultSet> pResults(pStatement->executeQuery(ss.str()));
    
  pResults->beforeFirst();
  while (pResults->next())
  {
    if (pResults->getString(1) == tableName)
      return true;
  }
  return false;
}
예제 #4
0
int SQLConnector::addRow(std::string tableName,
			  long int time,
		 const std::vector<std::pair<std::string, double>>& vals)
{
  struct tm* t = localtime(&time);
  char buf[50];
  snprintf(buf, 50, "%04d-%02d-%02d %02d:%02d:%02d",
	   t->tm_year+1900, t->tm_mon+1, t->tm_mday,
	   t->tm_hour, t->tm_min, t->tm_sec);
  std::string timestring(buf);

  std::vector<std::pair<std::string, double>> valsCopy(vals);
  std::pair<std::string, double> lastVal = valsCopy.back();
  valsCopy.pop_back();

  std::stringstream ss;
  ss << "INSERT INTO " << tableName << " ( time, ";
  for (auto& val : valsCopy)
  {
    ss << val.first << ", ";
  }
  ss << lastVal.first << " ) ";
  ss << "VALUES ( '" << timestring << "', ";
  for (auto& val : valsCopy)
  {
    ss << val.second << ", ";
  }
  ss << lastVal.second << ");";

  std::shared_ptr<sql::Statement> pStatement(m_pConnection->createStatement());
  bool result = pStatement->execute(ss.str());
  
  if (!result)
  {
    std::cout << SQLLOG << " added row with:" << std::endl;
    std::cout << ss.str() << std::endl;
  }
  else
  {
    std::cout << SQLLOG << " error adding row with:" << std::endl;
    std::cout << ss.str() << std::endl;
    return -1;
  }
  
  return 0;

}
예제 #5
0
int SQLConnector::makeTableColumnsWork(std::string tableName, 
				   const std::vector<std::string>& colNames)
{
  std::stringstream ss;
  ss << "describe " << tableName << ";";
  std::shared_ptr<sql::Statement> pStatement(m_pConnection->createStatement());
  std::shared_ptr<sql::ResultSet> pResults(pStatement->executeQuery(ss.str()));

  std::map<std::string, int> colsFound;
  for (auto& colName : colNames)
    colsFound[colName] = 0;
  
  pResults->beforeFirst();
  while (pResults->next())
  {
    for (auto& colName : colNames)
    {
      if (pResults->getString("Field") == colName)
	colsFound[colName] = 1;
    }
  }
  
  for (auto& colPair : colsFound)
  {
    if (colPair.second == 0)
    {
      // Add the column
      ss.str(std::string());
      ss << "ALTER TABLE " << tableName << " ADD " 
	 << colPair.first << " FLOAT;";
      bool result = pStatement->execute(ss.str());
      if (!result)
      {
	std::cout << SQLLOG << " added column with:" << std::endl;
	std::cout << ss.str() << std::endl;
      }
      else
      {
	std::cout << SQLLOG << " error adding column with:" << std::endl;
	std::cout << ss.str() << std::endl;
	return -1;
      }
    }
  }

  return 0;
}
예제 #6
0
 pStatement Statement::parse() {
     if (currentToken().type == TT_NAMESPACE) 
     {
         nextToken();
         _namespace = new_Namespace()->parse();       
     }
     else if(currentToken().type == TT_TYPEDEF) 
     {
             
     }
     else if(currentToken().type == TT_STRUCT || currentToken().type == TT_CLASS) 
     {
         isClass = true;
         if(currentToken().type == TT_STRUCT) {
             isStruct = true;
         }
         nextToken();
         _namespace = new_Namespace();
         _namespace->statementsBlock = new_StatementsBlock();
         _namespace->id = currentToken().strVal;
         pType newType = pType(new Type(owner, false) );
         pExprResult res = owner->new_ExprResult();
         res->evalType = ERT_TYPE;
         res->type = newType;
         newType->definition = this;
         owner->parsingStatementsBlockStack.back()->vars[_namespace->id] = res;
         _namespace->parse();   
         if(currentToken().type != TT_SEMICOLON) 
         {
             Parser::get()->parsingException(__FUNCTION__, __FILE__, __LINE__, currentToken(), "Expected semicolon");   
         }
         nextToken();
     }
     else if (currentToken().type == TT_BRACE_OPEN) {
         this->sb = new_StatementsBlock()->parse();
     }
     else if (currentToken().type == TT_BREAK 
         || currentToken().type == TT_RETURN 
         || currentToken().type == TT_ECHO 
         || currentToken().type == TT_CONTINUE) {
             isSpecial = true;
             specialType = currentToken().type;
             nextToken();
             if (specialType == TT_RETURN || specialType == TT_ECHO) {
                 expr = new_Expr()->parse();
             }
             if (currentToken().type != TT_SEMICOLON) {
                 throwTokenExpected(TT_SEMICOLON);
             }
             nextToken();
     }
     else if (currentToken().type == TT_IF) 
     {
         isSpecial = true;
         specialType = currentToken().type;
         nextToken();
         expr = new_Expr()->parse();
         statement1 = new_Statement()->parse();
         if (currentToken().type == TT_ELSE) 
         {
             nextToken();
             statement2 = new_Statement()->parse();
         }
     }
     else if (currentToken().type == TT_WHILE) 
     {
         localStatementsBlock = new_StatementsBlock();
         owner->parsingStatementsBlockStack.push_back(localStatementsBlock);
         isSpecial = true;
         specialType = currentToken().type;
         if (nextToken().type != TT_PARENTHESIS_OPEN) 
         {
             Parser::get()->parsingException(__FUNCTION__, __FILE__, __LINE__, currentToken() );
         }
         nextToken();
         expr = new_Expr()->parse();
         if (currentToken().type != TT_PARENTHESIS_CLOSE) 
         {
             Parser::get()->parsingException(__FUNCTION__, __FILE__, __LINE__, currentToken() );
         }
         nextToken();
         statement1 = new_Statement()->parse();
         owner->parsingStatementsBlockStack.pop_back();
     }
     else if (currentToken().type == TT_FOR) {
         localStatementsBlock = new_StatementsBlock();
         owner->parsingStatementsBlockStack.push_back(localStatementsBlock);
         isSpecial = true;
         specialType = currentToken().type;
         if (nextToken().type != TT_PARENTHESIS_OPEN) 
         {
             Parser::get()->parsingException(__FUNCTION__, __FILE__, __LINE__, currentToken() );
         }
         nextToken();
         statement1 = new_Statement()->parse();
         expr = new_Expr()->parse();
         if (currentToken().type != TT_SEMICOLON) 
         {
             Parser::get()->parsingException(__FUNCTION__, __FILE__, __LINE__, currentToken() );
         }
         nextToken();
         expr2 = new_Expr()->parse();
         if (currentToken().type != TT_PARENTHESIS_CLOSE) 
         {
             Parser::get()->parsingException(__FUNCTION__, __FILE__, __LINE__, currentToken() );
         }
         nextToken();
         statement2 = new_Statement()->parse();
         owner->parsingStatementsBlockStack.pop_back();
     }
     else {
         tryParse([this]() {  
             tn = new_Typename()->parse();
             pVarDeclarationAllowDefault vd = new_VarDeclarationAllowDefault()->parse();
             if (owner->currentToken().type == TT_PARENTHESIS_OPEN) 
             {
                 if (!vd->canBeFunctionDeclaration() ) 
                 {
                     Parser::get()->parsingException(__FUNCTION__, __FILE__, __LINE__, currentToken() );
                 }
                 nextToken();
                 this->function = pFunction(new Function(owner, vd->vd->id) );
                 this->function->args = new_FunctionArgs()->parse();
             
                 if (currentToken().type == TT_PARENTHESIS_CLOSE) 
                 {
                     if (nextToken().type == ';') 
                     {
                         this->function->statementsBlock = nullptr;    
                     }
                     else 
                     {
                         this->function->statementsBlock = new_StatementsBlock()->parse();
                         for (auto vd: this->function->args->vds) 
                         {
                             this->function->statementsBlock->vars[vd->vd->id] = nullptr;
                         }
                     }
                 }
                 else 
                 {
                     Parser::get()->parsingException(__FUNCTION__, __FILE__, __LINE__, currentToken(), "Unexpeted token");
                 }
             }
             else 
             {
                 if (currentToken().type == TT_COMMA || currentToken().type == TT_SEMICOLON) 
                 {
                     vds.push_back(vd);   
                 }
                 while (currentToken().type == TT_COMMA) 
                 {
                     nextToken();
                     vds.push_back(new_VarDeclarationAllowDefault()->parse() );
                 }
                 if (currentToken().type != TT_SEMICOLON) 
                 {
                     Parser::get()->parsingException(__FUNCTION__, __FILE__, __LINE__, currentToken() );
                 }
                 nextToken();
             }
         },
         [this](exception &e) {
             vds.clear();
             tn = nullptr;
             expr = new_Expr()->parse();
             this->function = nullptr;
             if (currentToken().type != TT_SEMICOLON) 
             {
                 Parser::get()->parsingException(__FUNCTION__, __FILE__, __LINE__, currentToken() );
             }
             nextToken();
         });
     }
     return pStatement(this);
 }