Example #1
0
void DBConnQueryWorker::doJob(DBConnQueryJobPtr job) {
  string &sql = job->m_sql;
  Util::replaceAll(sql, "INDEX", lexical_cast<string>(job->m_index).c_str());

  if (!job->m_server) {
    job->m_affected = -1;
    job->m_error.code = -1;
    job->m_error.msg = "(server info missing)";
    return;
  }

  try {
    DBConn conn;
    int count = 0;
  retry:
    try {
      count++;
      conn.open(job->m_server, job->m_connectTimeout, job->m_readTimeout);
    } catch (DatabaseException &e) {
      if (job->m_retryQueryOnFail &&
          count <= job->m_maxRetryQueryOnFail) {
        goto retry;
      } else {
        throw;
      }
    }

    if (job->m_dsResult) {
      DBDataSet ds;
      job->m_affected = conn.execute(sql.c_str(), &ds,
                                     job->m_retryQueryOnFail);
      Lock lock(*job->m_dsMutex);
      job->m_dsResult->addDataSet(ds);
    } else {
      job->m_affected = conn.execute(sql.c_str(), nullptr,
                                     job->m_retryQueryOnFail);
    }
  } catch (DatabaseException &e) {
    job->m_affected = -1;
    job->m_error.code = e.m_code;
    job->m_error.msg = e.getMessage();
  } catch (Exception &e) {
    job->m_affected = -1;
    job->m_error.code = -1;
    job->m_error.msg = e.getMessage();
  } catch (std::exception &e) {
    job->m_affected = -1;
    job->m_error.code = -1;
    job->m_error.msg = e.what();
  } catch (...) {
    job->m_affected = -1;
    job->m_error.code = -1;
    job->m_error.msg = "(unknown exception)";
  }
}
Example #2
0
void Package::commitStats(ServerDataPtr server, int runId) const {
  DBConn conn;
  conn.open(server);

  {
    DBQuery q(&conn, "UPDATE hphp_dep");
    q.setField("parent_file = parent");
    q.filterBy("run = %d", runId);
    q.filterBy("kind IN ('PHPInclude', 'PHPTemplate')");
    q.execute();
  }
  {
    DBQuery q(&conn, "UPDATE hphp_run");
    q.setField("committed = 1");
    q.filterBy("id = %d", runId);
    q.execute();
  }
}
Example #3
0
/**
 * Load from database "config" table that looks like this:
 *
 * CREATE TABLE `config` (
 *   `id` varchar(255) NOT NULL,
 *   `name` varchar(255) NOT NULL,
 *   `value` varchar(255) NOT NULL,
 *   PRIMARY KEY  (`id`,`name`)
 * ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
 *
 * Only a subset of options are supported.
 */
void Option::Load(ServerDataPtr server) {
  DBConn conn;
  conn.open(server);

  DBQueryPtr q(new DBQuery(&conn, "SELECT id, name, value FROM config"));
  DBDataSet ds;
  q->execute(ds);

  for (ds.moveFirst(); ds.getRow(); ds.moveNext()) {
    const char *id = ds.getField(0);
    string name = ds.getField(1);
    string value = ds.getField(2);
    boost::algorithm::trim<std::string>(name);
    boost::algorithm::trim<std::string>(value);

    if (strcmp(id, "IncludeRoots") == 0) {
      IncludeRoots[name] = value;
    } else if (strcmp(id, "AutoloadRoots") == 0) {
      AutoloadRoots[name] = value;
    } else if (strcmp(id, "IncludeSearchPaths") == 0) {
      IncludeSearchPaths.push_back(name);
    } else if (strcmp(id, "PackageDirectories") == 0) {
      PackageDirectories.insert(name);
    } else if (strcmp(id, "PackageExcludeDirs") == 0) {
      PackageExcludeDirs.insert(name);
    } else if (strcmp(id, "PackageExcludeFiles") == 0) {
      PackageExcludeFiles.insert(name);
    } else if (strcmp(id, "PackageExcludeStaticFiles") == 0) {
      PackageExcludeStaticFiles.insert(name);
    } else if (strcmp(id, "DynamicFunctionPrefix") == 0) {
      DynamicFunctionPrefixes.push_back(name);
    } else if (strcmp(id, "DynamicFunctionPostfix") == 0) {
      DynamicFunctionPostfixes.push_back(name);
    } else if (strcmp(id, "DynamicMethodPrefix") == 0) {
      DynamicMethodPrefixes.push_back(name);
    } else if (strcmp(id, "DynamicInvokeFunctions") == 0) {
      DynamicInvokeFunctions.insert(name);
    }
  }
}
Example #4
0
int Package::saveStatsToDB(ServerDataPtr server, int totalSeconds,
                           const std::string &branch, int revision) const {
  std::map<std::string, int> counts;
  SymbolTable::CountTypes(counts);
  m_ar->countReturnTypes(counts);
  ostringstream sout;
  JSON::OutputStream o(sout);
  o << counts;

  DBConn conn;
  conn.open(server);

  const char *sql = "INSERT INTO hphp_run (branch, revision, file, line, "
    "byte, program, function, class, types, time)";
  DBQuery q(&conn, sql);
  q.insert("'%s', %d, %d, %d, %d, %d, %d, %d, '%s', %d",
           branch.c_str(), revision,
           getFileCount(), getLineCount(), getCharCount(),
           1, m_ar->getFunctionCount(),
           m_ar->getClassCount(), sout.str().c_str(), totalSeconds);
  q.execute();
  return conn.getLastInsertId();
}
void DependencyGraph::saveToDB(ServerDataPtr server, int runId) const {
  vector<const char *> &dependencyTexts = getDependencyTexts();

  DBConn conn;
  conn.open(server);

  const char *sql = "INSERT INTO hphp_dep (run, program, kind, parent, "
    "parent_file, parent_line, child, child_file, child_line)";
  DBQueryPtr q(new DBQuery(&conn, sql));
  int count = 0;
  const int MAX_COUNT = 1000;

  for (int kindOf = 0; kindOf < KindOfCount; kindOf++) {
    const DependencyMapMap &mapmap = m_forwards[kindOf];
    const char *depText = dependencyTexts[kindOf];

    int k = kindOf;
    if (k == KindOfProgramMaxInclude) {
      k = KindOfPHPInclude; // all three share the same list of parents
    } else if (k == KindOfProgramUserFunction) {
      k = KindOfFunctionCall; // both share the same list of parents
    }
    const StringToDependencyPtrMap &parents = m_parents[k];

    // non-orphaned parents
    for (MapMapConstIter iterParent = mapmap.begin();
         iterParent != mapmap.end(); ++iterParent) {
      const std::string &parent = iterParent->first;
      const DependencyMap &depMap = iterParent->second;
      for (MapConstIter iterChild = depMap.begin();
           iterChild != depMap.end(); ++iterChild) {
        const std::string &child = iterChild->first;
        const DependencyPtrVec &deps = *iterChild->second;
        for (unsigned int i = 0; i < deps.size(); i++) {
          Dependency &dep = *deps[i];

          const char *parentFile = "";
          const char *childFile = "";
          int parentLine = 0;
          int childLine = 0;
          StringToDependencyPtrMap::const_iterator iter =
            parents.find(parent);
          if (iter != parents.end()) {
            if (iter->second->m_parent) {
              parentFile = iter->second->m_parent->getLocation()->file;
              parentLine = iter->second->m_parent->getLocation()->line1;
            }
          } else if (dep.m_parent) {
            parentFile = dep.m_parent->getLocation()->file;
            parentLine = dep.m_parent->getLocation()->line1;
          }
          if (dep.m_child) {
            childFile = dep.m_child->getLocation()->file;
            childLine = dep.m_child->getLocation()->line1;
          }

          vector<string> programs = dep.m_programs;
          if (programs.empty()) programs.push_back("");
          for (unsigned int p = 0; p < programs.size(); p++) {
            q->insert("%d,'%s','%s', '%s','%s',%d, '%s','%s',%d",
                      runId, programs[p].c_str(), depText,
                      parent.c_str(), parentFile, parentLine,
                      child.c_str(), childFile, childLine);
            if (++count >= MAX_COUNT) {
              count = 0;
              q->execute();
              q = DBQueryPtr(new DBQuery(&conn, sql));
            }
          }
        }
      }
    }

    // orphaned parents
    for (StringToDependencyPtrMap::const_iterator iter = parents.begin();
         iter != parents.end(); ++iter) {
      if (mapmap.find(iter->first) == mapmap.end()) {
        Dependency &dep = *iter->second;

        const char *parentFile = "";
        int parentLine = 0;
        if (dep.m_parent && dep.m_parent->getLocation()) {
          parentFile = dep.m_parent->getLocation()->file;
          parentLine = dep.m_parent->getLocation()->line1;
        }

        vector<string> programs = dep.m_programs;
        if (programs.empty()) programs.push_back("");
        for (unsigned int p = 0; p < programs.size(); p++) {
          q->insert("%d,'%s','%s', '%s','%s',%d, '','',0",
                    runId, programs[p].c_str(), depText,
                    iter->first.c_str(), parentFile, parentLine);
          if (++count >= MAX_COUNT) {
            count = 0;
            q->execute();
            q = DBQueryPtr(new DBQuery(&conn, sql));
          }
        }
      }
    }
  }

  if (count) q->execute();
}