⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lang_cmd.cpp

📁 ncbi源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* * =========================================================================== * PRODUCTION $Log: lang_cmd.cpp,v $ * PRODUCTION Revision 1000.2  2004/06/01 19:21:51  gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.8 * PRODUCTION * =========================================================================== *//* $Id: lang_cmd.cpp,v 1000.2 2004/06/01 19:21:51 gouriano Exp $ * =========================================================================== * *                            PUBLIC DOMAIN NOTICE *               National Center for Biotechnology Information * *  This software/database is a "United States Government Work" under the *  terms of the United States Copyright Act.  It was written as part of *  the author's official duties as a United States Government employee and *  thus cannot be copyrighted.  This software/database is freely available *  to the public for use. The National Library of Medicine and the U.S. *  Government have not placed any restriction on its use or reproduction. * *  Although all reasonable efforts have been taken to ensure the accuracy *  and reliability of the software and data, the NLM and the U.S. *  Government do not and cannot warrant the performance or results that *  may be obtained by using this software or data. The NLM and the U.S. *  Government disclaim all warranties, express or implied, including *  warranties of performance, merchantability or fitness for any particular *  purpose. * *  Please cite the author in any work or product based on this material. * * =========================================================================== * * Author:  Vladimir Soussov * * File Description:  ODBC language command * */#include <ncbi_pch.hpp>#include <stdio.h>#include <dbapi/driver/odbc/interfaces.hpp>BEGIN_NCBI_SCOPE/////////////////////////////////////////////////////////////////////////////////  CODBC_LangCmd:://CODBC_LangCmd::CODBC_LangCmd(CODBC_Connection* conn, SQLHSTMT cmd,                           const string& lang_query,                           unsigned int nof_params) :    m_Connect(conn), m_Cmd(cmd), m_Query(lang_query), m_Params(nof_params),     m_Reporter(&conn->m_MsgHandlers, SQL_HANDLE_STMT, cmd){    m_WasSent   =  false;    m_HasFailed =  false;    m_Res       =  0;    m_RowCount  = -1;}bool CODBC_LangCmd::More(const string& query_text){    m_Query.append(query_text);    return true;}bool CODBC_LangCmd::BindParam(const string& param_name, CDB_Object* param_ptr){    return        m_Params.BindParam(CDB_Params::kNoParamNumber, param_name, param_ptr);}bool CODBC_LangCmd::SetParam(const string& param_name, CDB_Object* param_ptr){    return        m_Params.SetParam(CDB_Params::kNoParamNumber, param_name, param_ptr);}bool CODBC_LangCmd::Send(){    if (m_WasSent)        Cancel();    m_HasFailed = false;    CMemPot bindGuard;    string q_str;	if(m_Params.NofParams() > 0) {		SQLINTEGER* indicator= (SQLINTEGER*)				bindGuard.Alloc(m_Params.NofParams()*sizeof(SQLINTEGER));		if (!x_AssignParams(q_str, bindGuard, indicator)) {			SQLFreeStmt(m_Cmd, SQL_RESET_PARAMS);			m_HasFailed = true;			throw CDB_ClientEx(eDB_Error, 420003, "CODBC_LangCmd::Send",                           "cannot assign params");		}	}    string* real_query;    if(!q_str.empty()) {        q_str.append(m_Query);        real_query= &q_str;    }    else {        real_query= &m_Query;    }    switch(SQLExecDirect(m_Cmd, (SQLCHAR*)real_query->c_str(), SQL_NTS)) {    case SQL_SUCCESS:        m_hasResults= true;        break;    case SQL_NO_DATA:        m_hasResults= false;        m_RowCount= 0;        break;    case SQL_ERROR:        m_Reporter.ReportErrors();        SQLFreeStmt(m_Cmd, SQL_RESET_PARAMS);        m_HasFailed = true;        throw CDB_ClientEx(eDB_Fatal, 420001, "CODBC_LangCmd::Send",                           "SQLExecDirect failed");    case SQL_SUCCESS_WITH_INFO:        m_Reporter.ReportErrors();        m_hasResults= true;        break;    case SQL_STILL_EXECUTING:        m_Reporter.ReportErrors();        SQLFreeStmt(m_Cmd, SQL_RESET_PARAMS);        m_HasFailed = true;        throw CDB_ClientEx(eDB_Fatal, 420002, "CODBC_LangCmd::Send",                           "Some other query is executing on this connection");            case SQL_INVALID_HANDLE:        m_HasFailed= true;        throw CDB_ClientEx(eDB_Fatal, 420004, "CODBC_LangCmd::Send",                           "The statement handler is invalid (memory corruption suspected)");            default:        m_Reporter.ReportErrors();        SQLFreeStmt(m_Cmd, SQL_RESET_PARAMS);        m_HasFailed = true;        throw CDB_ClientEx(eDB_Fatal, 420005, "CODBC_LangCmd::Send",                           "Unexpected error");            }    m_WasSent = true;    return true;}bool CODBC_LangCmd::WasSent() const{    return m_WasSent;}bool CODBC_LangCmd::Cancel(){    if (m_WasSent) {        if (m_Res) {            delete m_Res;            m_Res = 0;        }        m_WasSent = false;        switch(SQLFreeStmt(m_Cmd, SQL_CLOSE)) {        case SQL_SUCCESS_WITH_INFO: m_Reporter.ReportErrors();        case SQL_SUCCESS:           break;        case SQL_ERROR:             m_Reporter.ReportErrors();        default:                    return false;        }    }    SQLFreeStmt(m_Cmd, SQL_RESET_PARAMS);    // m_Query.erase();    return true;}bool CODBC_LangCmd::WasCanceled() const{    return !m_WasSent;}CDB_Result* CODBC_LangCmd::Result(){    if (m_Res) {        delete m_Res;        m_Res = 0;        m_hasResults= xCheck4MoreResults();    }    if (!m_WasSent) {        throw CDB_ClientEx(eDB_Error, 420010, "CODBC_LangCmd::Result",                           "a command has to be sent first");    }    if(!m_hasResults) {		m_WasSent= false;        return 0;    }    SQLSMALLINT nof_cols= 0;    while(m_hasResults) {        switch(SQLNumResultCols(m_Cmd, &nof_cols)) {        case SQL_SUCCESS_WITH_INFO:            m_Reporter.ReportErrors();        case SQL_SUCCESS:            break;        case SQL_ERROR:            m_Reporter.ReportErrors();            throw CDB_ClientEx(eDB_Error, 420011, "CODBC_LangCmd::Result",                               "SQLNumResultCols failed");        default:            throw CDB_ClientEx(eDB_Error, 420012, "CODBC_LangCmd::Result",                               "SQLNumResultCols failed (memory corruption suspected)");        }        if(nof_cols < 1) { // no data in this result set			SQLINTEGER rc;			switch(SQLRowCount(m_Cmd, &rc)) {				case SQL_SUCCESS_WITH_INFO:					m_Reporter.ReportErrors(); 				case SQL_SUCCESS: break;				case SQL_ERROR:						m_Reporter.ReportErrors();						throw CDB_ClientEx(eDB_Error, 420013, "CODBC_LangCmd::Result",                               "SQLRowCount failed");				default:					throw CDB_ClientEx(eDB_Error, 420014, "CODBC_LangCmd::Result",						"SQLRowCount failed (memory corruption suspected)");			}            m_RowCount = rc;            m_hasResults= xCheck4MoreResults();            continue;        }        m_Res = new CODBC_RowResult(nof_cols, m_Cmd, m_Reporter);        return Create_Result(*m_Res);    }    m_WasSent = false;    return 0;}bool CODBC_LangCmd::HasMoreResults() const{    return m_hasResults;}void CODBC_LangCmd::DumpResults(){    CDB_Result* dbres;    while(m_WasSent) {        dbres= Result();        if(dbres) {            if(m_Connect->m_ResProc) {                m_Connect->m_ResProc->ProcessResult(*dbres);            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -