Example #1
0
bool CSqlWhere::AddCond(const CString& oName, uint32 nOp, const CString& oVal)
{
    if(m_pAccess == NULL)
        return false;
    uint32 nFieldNo = m_pAccess->GetFieldNo(oName.GetStr());
    if(nFieldNo == (uint32)(-1))
    {
        m_bError = true;
        FocpCmdLogEx("SQL", FOCP_LOG_ERROR, ("CSqlWhere::AddCond() there isn't the field '%s' in the table '%s'", oName.GetStr(), m_oName.GetStr()));
        return false;
    }
    bool bSet;
    CMdbParaSet* pParaSet;
    uint32 i, nCount = m_pWhere->GetParaCount();
    if(nCount)
        pParaSet = (CMdbParaSet*)m_pWhere->GetPara(nCount - 1, bSet);
    else
        pParaSet = (CMdbParaSet*)m_pWhere->AddParaSet();
    nCount = pParaSet->GetParaCount();
    for(i=0; i<nCount; ++i)
    {
        CMdbSqlPara* pPara = (CMdbSqlPara*)pParaSet->GetPara(i, bSet);
        if(!pPara->IsSetField(nFieldNo))
        {
            pPara->SetFromString(nFieldNo, oVal.GetStr(), nOp);
            return true;
        }
    }
    CMdbSqlPara* pPara = (CMdbSqlPara*)pParaSet->AddPara();
    pPara->SetFromString(nFieldNo, oVal.GetStr(), nOp);
    return true;
}
Example #2
0
bool CSqlInsert::AddValue(const CString& oVal)
{
    if(m_pAccess == NULL)
        return false;
    uint32 nFldNo = m_nFldNo++;
    if(!m_oFieldTable.GetSize())
    {
        uint32 nFieldCount = m_pAccess->GetFieldCount();
        if(nFldNo < nFieldCount)
            m_pInsertAttr->SetFromString(nFldNo, (char*)oVal.GetStr(), MDB_SQLPARA_OPERATOR_EQUAL);
        else
        {
            m_bError = true;
            FocpCmdLogEx("SQL", FOCP_LOG_ERROR, ("CSqlInsert::AddValue() there isn't the field no '%u' in the table '%s'", nFldNo, m_oName.GetStr()));
            return false;
        }
    }
    else
    {
        CString& oFieldName = m_oFieldTable[nFldNo];
        uint32 nFieldNo = m_pAccess->GetFieldNo(oFieldName.GetStr());
        if(nFieldNo == (uint32)(-1))
        {
            m_bError = true;
            FocpCmdLogEx("SQL", FOCP_LOG_ERROR, ("CSqlInsert::AddValue() there isn't the field no '%u' in the table '%s'", nFldNo, m_oName.GetStr()));
            return false;
        }
        m_pInsertAttr->SetFromString(nFieldNo, oVal.GetStr(), MDB_SQLPARA_OPERATOR_EQUAL);
    }
    return true;
}
Example #3
0
bool CSqlUpdate::AddSetPara(const CString& oName, const CString& oVal, uint32 nOp)
{
    if(m_pAccess == NULL)
        return false;
    uint32 nFieldNo = m_pAccess->GetFieldNo(oName.GetStr());
    if(nFieldNo == (uint32)(-1))
    {
        m_bError = true;
        FocpCmdLogEx("SQL", FOCP_LOG_ERROR, ("CSqlUpdate::AddSetPara() there isn't the field '%s' in the table '%s'", oName.GetStr(), m_oName.GetStr()));
        return false;
    }
    m_pSetAttr->SetFromString(nFieldNo, oVal.GetStr(), nOp);
    return true;
}
Example #4
0
uint32 CSqlDataBase::CreateSqlInsert(const CString& oName)
{
    CMdbAccess * pAccess = m_pDb->QueryAccess(oName.GetStr());
    if(!pAccess)
    {
        FocpCmdLogEx("SQL", FOCP_LOG_ERROR, ("CSqlDataBase::CreateSqlInsert(%s.%s) failure: MDB_TABLE_NOTEXIST", m_pDb->GetDbName(), oName.GetStr()));
        return MDB_TABLE_NOTEXIST;
    }
    m_oInsert.Bind(oName, pAccess);
    return MDB_SUCCESS;
}
Example #5
0
uint32 CSqlDataBase::TruncateTable(const CString& oName)
{
    CMdbAccess * pAccess = m_pDb->QueryAccess(oName.GetStr());
    if(!pAccess)
    {
        FocpCmdLogEx("SQL", FOCP_LOG_ERROR, ("CSqlDataBase::TruncateTable(%s.%s) failure: MDB_TABLE_NOTEXIST", m_pDb->GetDbName(), oName.GetStr()));
        return MDB_TABLE_NOTEXIST;
    }
    pAccess->Truncate();
    pAccess->Release();
    FocpCmdLogEx("SQL", FOCP_LOG_SYSLOG, ("CSqlDataBase::TruncateTable(%s.%s) success", m_pDb->GetDbName(), oName.GetStr()));
    return 0;
}
Example #6
0
uint32 CSqlEnv::SelectDataBase(const CString& oName)
{
    CMdb* pDb = CMdb::GetMdb(oName.GetStr());
    if(!pDb)
    {
        FocpCmdLogEx("SQL", FOCP_LOG_ERROR,
                     ("CSqlEnv::SelectDataBase(%s) failure", oName.GetStr()));
        return MDB_DB_NOTEXIST;
    }
    if(!pDb->GetLocalInterface())
    {
        if(!oName.Compare("sysdb", false))
        {
            FocpCmdLogEx("SQL", FOCP_LOG_ERROR,
                         ("CSqlEnv::SelectDataBase(%s) failure", oName.GetStr()));
            return MDB_DB_NOTEXIST;
        }
    }
    m_oDataBase.Bind(pDb);
    FocpCmdLogEx("SQL", FOCP_LOG_CLOSE,
                 ("CSqlEnv::SelectDataBase(%s) success", oName.GetStr()));
    return MDB_SUCCESS;
}
Example #7
0
uint32 CSqlDataBase::QueryTable()
{
    if(!m_oSelect.m_pAccess)
        return 0;

    CMdbFilter* pFilter = m_oSelect.m_pAccess->GetResultFilter();
    uint32 nSize = m_oSelect.m_oFieldTable.GetSize();
    for(uint32 i=0; i<nSize; ++i)
        pFilter->SetField(m_oSelect.m_pAccess->GetFieldNo(m_oSelect.m_oFieldTable[i].GetStr()));

    uint32 nRet;
    if(m_oSelect.m_bError)
        return 0;
    uint32 nTotal = 0;
    CString oList = GetFieldList();
    while(true)
    {
        nRet = m_oSelect.m_pAccess->Query(m_nResultSize, nTotal);
        m_oSelect.m_pRecordSet = m_oSelect.m_pAccess->GetResultSet();
        if(nRet)
        {
            FocpCmdLogEx("SQL", FOCP_LOG_ERROR, ("CSqlDataBase::QueryTable(%s.%s) failure: %s", m_pDb->GetDbName(), m_oSelect.m_oName.GetStr(), GetMdbError(nRet)));
            m_pEnv->Flush(false);
            m_oSelect.UnBind();
            return 0;
        }
        else
        {
            if(!nTotal)
            {
                FocpCmdLogEx("SQL", FOCP_LOG_CLOSE, ("%s", oList.GetStr()));
                m_pEnv->Flush(false);
            }
            uint32 nCount = m_oSelect.GetResultCount();
            for(uint32 i=0; i<nCount; ++i)
            {
                CMdbResult* pRecord = m_oSelect.GetRecord(i);
                FocpCmdLogEx("SQL", FOCP_LOG_CLOSE, ("%s", GetRecordText(pRecord, oList).GetStr()));
                m_pEnv->Flush(false);
            }
            nTotal += nCount;
            if(nCount < m_nResultSize)
                break;
        }
    }
    FocpCmdLogEx("SQL", FOCP_LOG_CLOSE, ("\r\ntotal %u records in %s.%s", nTotal, m_pDb->GetDbName(), m_oSelect.m_oName.GetStr()));
    m_oSelect.UnBind();
    return 0;
}
Example #8
0
uint32 CSqlDataBase::CreateIndex()
{
    CString oFieldName;
    CVector<CString>& oFieldTable = m_oIndex.oIndexFieldTable;
    uint32 nSize = oFieldTable.GetSize();
    for(uint32 i=0; i<nSize; ++i)
    {
        if(!oFieldName.Empty())
            oFieldName += ",";
        oFieldName += oFieldTable[i];
    }

    CMdbIndexDef oIdxDef;
    CBinary::MemorySet(&oIdxDef, 0, sizeof(oIdxDef));
    oIdxDef.sIndexName = (char*)m_oIndex.oIndexName.GetStr();
    oIdxDef.sTableName = (char*)m_oIndex.oTableName.GetStr();
    oIdxDef.sPrimaryIndex = (char*)m_oIndex.oPrimaryIndex.GetStr();
    if(!oIdxDef.sPrimaryIndex[0])
        oIdxDef.sPrimaryIndex = NULL;
    oIdxDef.nQualifier = m_oIndex.nQualifier;
    oIdxDef.nArithmetic = m_oIndex.nArithmetic;
    oIdxDef.nHashRate = m_oIndex.nHashRate;
    oIdxDef.pFieldList = (char*)oFieldName.GetStr();

    CMdbLocalInterface* pItf = m_pDb->GetLocalInterface();
    if(pItf == NULL)
    {
        FocpCmdLogEx("SQL", FOCP_LOG_ERROR, ("CSqlDataBase::CreateIndex(%s.%s) failure: MDB_INVALID_INTERFACE", m_pDb->GetDbName(), oIdxDef.sIndexName));
        return MDB_INVALID_INTERFACE;
    }
    uint32 nRet = pItf->CreateIndex(&oIdxDef);
    if(nRet)
        FocpCmdLogEx("SQL", FOCP_LOG_ERROR, ("CSqlDataBase::CreateIndex(%s.%s) failure: %s", m_pDb->GetDbName(), oIdxDef.sIndexName, GetMdbError(nRet)));
    else
        FocpCmdLogEx("SQL", FOCP_LOG_SYSLOG, ("CSqlDataBase::CreateIndex(%s.%s) success", m_pDb->GetDbName(), oIdxDef.sIndexName));
    return nRet;
}
Example #9
0
const char* CMdb::GetDbList()
{
	return g_oDbList.GetStr();
}
Example #10
0
CString CSqlDataBase::GetRecordText(CMdbResult* pRecord, CString& oFieldList)
{
    char sName[MDB_NAME_MAXLEN+1];
    CString oRet;
    const char* s = oFieldList.GetStr();
    uint32 nCount = 0;
    while(s)
    {
        char* pShift = (char*)CString::CharOfString(s, ',');
        if(pShift)
        {
            uint32 nLen = pShift - s;
            CBinary::MemoryCopy(sName, s, nLen);
            sName[nLen] = 0;
        }
        else
            CString::StringCopy(sName, s);
        s = (const char*)pShift;
        if(s) s += 2;
        if(nCount)
            oRet += ", ";

        uint32 nFieldNo = m_oSelect.m_pAccess->GetFieldNo((const char*)sName);
        if(nFieldNo == (uint32)(-1))
            oRet += "NULL";
        else
        {
            uint32 nSize = pRecord->GetStringSize(nFieldNo);
            char * pStr = new char[nSize+1];
            pRecord->GetAsString(nFieldNo, pStr);
            if(!pStr[0])
                oRet += "NULL";
            else
            {
                char* pTmp = pStr;
                while(pTmp[0])
                {
                    switch(pTmp[0])
                    {
//					case ' ':   oRet += "\\w"; break;
                    case '\r':
                        oRet += "\\r";
                        break;
                    case '\n':
                        oRet += "\\n";
                        break;
                    case '\f':
                        oRet += "\\f";
                        break;
                    case '\v':
                        oRet += "\\v";
                        break;
                    case '\t':
                        oRet += "\\t";
                        break;
                    case '\a':
                        oRet += "\\a";
                        break;
                    case '\b':
                        oRet += "\\b";
                        break;
                    case '\\':
                        oRet += "\\\\";
                        break;
                    default:
                        if(pTmp[0] == ',' || !isprint(pTmp[0]))
                        {
                            char cBuf[6];
                            StringPrint(cBuf, "\\X%2X", (uint32)(uint8)pTmp[0]);
                            oRet += cBuf;
                        }
                        else
                            oRet += pTmp[0];
                        break;
                    }
                    ++pTmp;
                }
            }
            delete[] pStr;
        }
        ++nCount;
    }
    return oRet;
}
Example #11
0
void CSqlDataBase::OrderBy(const CString& oIdxName, bool bAsc)
{
    if(m_oSelect.m_pAccess && !m_oSelect.m_pAccess->SetOrderBy(oIdxName.GetStr(), bAsc))
        FocpCmdLogEx("SQL", FOCP_LOG_WARNING, ("CSqlDataBase::OrderBy(%s.%s.%s) failure", m_pDb->GetDbName(), m_oSelect.m_oName.GetStr(), oIdxName.GetStr()));
}