当前位置: 首页>>代码示例>>C++>>正文


C++ DB::getConn方法代码示例

本文整理汇总了C++中DB::getConn方法的典型用法代码示例。如果您正苦于以下问题:C++ DB::getConn方法的具体用法?C++ DB::getConn怎么用?C++ DB::getConn使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DB的用法示例。


在下文中一共展示了DB::getConn方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: strlen

    string 
handle_STARTE(const string &rawtext)
{
    int err;
    string ret;

    const char *handlefind = "STAERE ";
    const char *strfind = "\r\nCookie: ";

    size_t start = rawtext.find(handlefind) + strlen(handlefind);

    if (start == string::npos)
    {
        err = PC_INPUTFORMATERROR;
        ret = sys_error(err);
        ret += "\r\n\r\n";
        return ret;
    }

    size_t stop = rawtext.find(strfind, start);

    if (stop == string::npos)
    {
        err = PC_INPUTFORMATERROR;
        ret = sys_error(err);
        ret += "\r\n\r\n";
        return ret;
    }

    eid_t eid = rawtext.substr(start, stop - start);

    start = stop + strlen(strfind);

    stop = rawtext.find("\r\n", start);

    if (stop == string::npos)
    {
        err = PC_INPUTFORMATERROR;
        ret = sys_error(err);
        ret += "\r\n\r\n";
        return ret;
    }

    string cookie = rawtext.substr(start, stop - start);

    DB db;
    PGconn *dbconn = db.getConn();

    uid_t uid = getUIDByCookie(cookie, err, dbconn);

    gid_t gid = getGIDByUID(uid, err, dbconn);

    if (GID_STUDENT == gid 
            || gid == GID_ADMIN || gid == GID_TEACHER)
    {
        char ceid[MAXBUF];
        PQescapeString(ceid, eid.c_str(), eid.size() + 1);

        char sql_buf[MAXBUF];
        PGresult *res_exam;
        snprintf(sql_buf, sizeof(sql_buf), 
                "SELECT start_time as start, end_time, status as end "
                " FROM exam WHERE exam_id=%s "
                    "AND status = 'started'",
                ceid);

        res_exam = PQexec(dbconn, sql_buf);

        if (PQresultStatus(res_exam) != PGRES_TUPLES_OK)
        {
            err = PC_DBERROR;
            ret = sys_error(err);
            ret += "\r\n\r\n";
            PQclear(res_exam);
            return ret;
        }

        if (PQntuples(res_exam) == 0)
        {
            err = PC_NOTFOUND;
            ret = sys_error(err);
            ret += "\r\n\r\n";
            PQclear(res_exam);
            return ret;
        }

        // Is exam started
        time_t start = strtoul(PQgetvalue(res_exam, 0, 0), NULL, 10);
        time_t end = strtoul(PQgetvalue(res_exam, 0, 1), NULL, 10);

#if DBG
        fprintf(stderr, "%s:%d::start: %ul, end: %ul", __FUNCTION__,
                __LINE__, start, end)

#endif

            time_t now = time(NULL);
        if ( ! (now >=start 
                    && now <= end 
                    && strcmp("started", PQgetvalue(res_exam, 0, 2) ) == 0) )
//.........这里部分代码省略.........
开发者ID:di3online,项目名称:OEMS,代码行数:101,代码来源:handle_starte.cpp

示例2: handle_MPSTA

string handle_MPSTA(const string &rawtext)
{
    uid_t userID;
    pid_t pid;
    string cookie = "";
    string status = "true";

    string response = "";

    int err = PC_UNKNOWNERROR;
    DB db;

    size_t start = rawtext.find(' ') + 1;
    size_t end = 0;

    //Get the pid
    end = rawtext.find("\r\n", start);
    pid = rawtext.substr(start, end - start);

    //Get the cookie
    start = rawtext.find(' ', end) + 1;
    end = rawtext.find("\r\n", start);
    cookie = rawtext.substr(start, end - start);

    //get the userID
    PGresult *dbres = PQexec(db.getConn(), "BEGIN");
    if (PQresultStatus(dbres) != PGRES_COMMAND_OK)
    {
        response = sys_error(PC_DBERROR);
        response += "\r\n\r\n";
        PQclear(dbres);
        return response;
    }
    PQclear(dbres);

    userID = getUIDByCookie(cookie, err, db.getConn());
    //check whether the cookie is right
     if (err != PC_SUCCESSFUL)
    {
        response = sys_error(err);
        response += "\r\n\r\n";
        PQexec(db.getConn(), "ROLLBACK");
        return response;
    }

    //check the group id
    gid_t gid = getGIDByUID(userID, err, db.getConn());
    if ((gid != GID_ADMIN) && (gid != GID_TEACHER))
    {
        err = PC_NOPERMISSION;
        response = sys_error(err);
        response += "\r\n\r\n";
        PQexec(db.getConn(), "ROLLBACK");
        return response;
    }

    char sql[300];
    size_t number_pid;
    stringstream ss;

    ss << pid;
    ss >> number_pid;
    
    //Check if there is some questions in the paper.
    snprintf(sql, sizeof(sql), "SELECT * FROM question WHERE paper_id = %lu", number_pid);

    //Exec the SQL query
    PGresult *res;
    res = PQexec(db.getConn(), sql);

    
    if( PQntuples(res) == 0)
    {
        //If there is no question in the paper, 
        //the paper won't be accomplished.

        err = PC_NOTFOUND;
        response = sys_error(err);
        response += "\r\n\r\n";
        PQclear(res);
        PQexec(db.getConn(), "ROLLBACK");
        return response;
    }

    snprintf(sql, sizeof(sql), 
            "UPDATE paper SET status = '%s' WHERE paper_id = %lu", 
            status.c_str(), number_pid);
    res = PQexec(db.getConn(), sql);
    if ((PQresultStatus(res) != PGRES_COMMAND_OK))
    {
        PQclear(res);
        err = PC_SYSTEMERROR;
        response = sys_error(err);
        response += "\r\n\r\n";
        PQexec(db.getConn(), "ROLLBACK");
        return response;
    }

    PQclear(res);
    err = PC_SUCCESSFUL;
//.........这里部分代码省略.........
开发者ID:di3online,项目名称:OEMS,代码行数:101,代码来源:handle_mpsta.cpp


注:本文中的DB::getConn方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。