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

📄 dbbaseclass.cpp

📁 用ODBC写的数据库接口,支持SQlserver,mysql
💻 CPP
📖 第 1 页 / 共 2 页
字号:
            while(pose[0] && pose[0]!=' ' && pose[0]!=',' && pose[0]!=')' && pose[0]!='\r' && pose[0]!='\n')
                pose++;
            if(pose-pos>110)
                break;
            if(!tmp)
            {
                tmp = new PARAM_SQL;
                m_ParamSql = tmp;
            }
            else
            {
                tmp->pNext = new PARAM_SQL;
                tmp = tmp->pNext;
            }
            memset(tmp,0,sizeof(PARAM_SQL));
            memcpy(tmp->Param,pos+1,pose-pos-1);
            UpperStr(tmp->Param,tmp->Param);
            memset(pos,' ',pose-pos);
            pos[0]='?';
            pos=pose;
        }
        else
            pos++;
    }
    m_pDataConnect->AddSql(szSQL);
/*
   char szSQL[2048];
    strncpy(szSQL,sql,2047);
    char *pos = strchr(szSQL,':');
    char *pose;
    bool noParam;
    PARAM_SQL *tmp = NULL;
    while(pos[0] )
    {
        pose  = pos;
        while(pose[0] && pose[0]!=' ' && pose[0]!=',' && pose[0]!=')' && pose[0]!='\r' && pose[0]!='\n')
            pose++;
        if(pose-pos>110)
            break;
        if(!tmp)
        {
            tmp = new PARAM_SQL;
            m_ParamSql = tmp;
        }
        else
        {
            tmp->pNext = new PARAM_SQL;
            tmp = tmp->pNext;
        }
        memset(tmp,0,sizeof(PARAM_SQL));
        memcpy(tmp->Param,pos+1,pose-pos-1);
        UpperStr(tmp->Param,tmp->Param);
        memset(pos,' ',pose-pos);
        pos[0]='?';
        pos = strchr(pose,':');
    }
    m_pDataConnect->AddSql(szSQL);
    */
}

bool   CDBFaceBase::SetParam(char *parm,char *value,int size,int type)
{
    PARAM_SQL *tmp = m_ParamSql;
    int  nIdx = 0;
    char szParam[120];
    strcpy(szParam,parm);
    UpperStr(szParam,szParam);
    while(tmp)
    {
        nIdx++;
        if(strcmp(tmp->Param,szParam)==0)
        {
            break;
        };
        tmp = tmp->pNext;
    }
    if(!tmp||nIdx<=0)
    {
        strcpy(m_Err,"未找到参数");
        return false;
    }
    if(tmp->pData)
        return false;
    tmp->pData = new char[size+1];
    memcpy(tmp->pData,value,size);
    tmp->nValue = atoi(tmp->pData);
    tmp->pData[size]=0;
    bool ret;
    if(type==AS_INT)
        ret = m_pDataConnect->SetParam(nIdx,(char *)&(tmp->nValue),size,type,&(tmp->nStatus));
    else
        ret = m_pDataConnect->SetParam(nIdx,tmp->pData,size,type,&(tmp->nStatus));
    if(!ret)
        strcpy(m_Err,m_pDataConnect->GetErr());
    return ret;
    
}

bool   CDBFaceBase::RunSql()
{
    bool ret = m_pDataConnect->Run();
    if(!ret)
    {
        strcpy(m_Err,m_pDataConnect->GetErr());
        WriteLog(m_Err);
    }
    return ret;
}

bool   CDBFaceBase::Query(char * szSQL)
{
    bool bRet = m_pDataConnect->PrepareSql(szSQL,m_pDataSet);
    if(!bRet)
    {
        strcpy(m_Err,m_pDataConnect->GetErr());
        WriteLog(m_Err);
        WriteLog(szSQL);
    }
    else
    {
        iRecordCount = m_pDataSet->GetRows();
        //m_pDataConnect->FetchData();
    }
    return bRet;
}

int   CDBFaceBase::RecordCount()
{
    return iRecordCount;
}

bool  CDBFaceBase::First()
{
    return m_pDataConnect->FetchFirst();
}

bool  CDBFaceBase::Next()
{
    return m_pDataConnect->FetchData();
}

char *CDBFaceBase::GetDataValue(char *szFieldName,int *length)
{
    int fid;
    const COL_DATA_ODBC *field = m_pDataSet->GetValueByName(szFieldName,fid);
    if(!field||!field->value)
        return NULL;
    const COL_DATAFMT_ODBC *pFieldFmt = m_pDataSet->GetColInfo(fid);
    if(pFieldFmt && pFieldFmt->c_datatype==SQL_C_CHAR)
    {
        *length = field->valuelen;
        while(*length>0 && field->value[*length-1]==' ')
        {
            field->value[*length-1] = 0;
            (*length)--;
        }
    }
    else
        *length = field->valuelen;
//    *length = field->valuelen;
    if(*length==-1)
        return NULL;
    else
        return field->value;
/*
    int len;
    if(length==NULL)
        length=&len;
    TField *field=m_adQuery->FindField(szFieldName);
    *length=0;
    if(!field||field->IsNull)
        return NULL;
    if(m_Data)
        delete m_Data;
    m_Data=NULL;
    if(field->DataType==ftBlob)
    {
        TStream *Stream = NULL;
        try
        {
            *length = ((TBlobField *)field)->BlobSize;
            Stream = m_adQuery->CreateBlobStream(field, bmRead);
            Stream->Seek(0,soFromBeginning);
            *length=Stream->Size;
            m_Data=new char [Stream->Size+1];
            Stream->ReadBuffer(m_Data,*length);
            m_Data[(int)(Stream->Size)] = 0;
        }
        catch(Exception &exception)
        {
            strncpy(m_Err,exception.Message.c_str(),sizeof(m_Err)-1);
        }
        if(Stream)
            delete Stream;
    }
    else
    {
        *length=field->AsString.Length();
        m_Data=new char [field->AsString.Length()+1];
        strcpy(m_Data,field->AsString.c_str());
    }
    return m_Data;*/
}

int   CDBFaceBase::GetIntValue(char *szFieldName)
{
    int length;
    char *pStr = GetDataValue(szFieldName,&length);
    if(pStr)
        return atoi(pStr);
    return 0;
//    return atoi(m_adQuery->FieldByName(szFieldName)->AsString.c_str());
}

char *CDBFaceBase::GetValueByIdx(int fid,int *length)
{
    const COL_DATA_ODBC *field = (*m_pDataSet)[fid];
    int nLength;
    if(!field||!field->value)
        return NULL;
    const COL_DATAFMT_ODBC *pFieldFmt = m_pDataSet->GetColInfo(fid);
    if(pFieldFmt && pFieldFmt->c_datatype==SQL_C_CHAR)
    {
        nLength = field->valuelen;
        while(nLength>0 && field->value[nLength-1]==' ')
        {
            field->value[nLength-1] = 0;
            nLength--;
        }
    }
    else
        nLength = field->valuelen;
    if(length)
        *length = nLength;
    if(nLength==-1)
        return NULL;
    else
        return field->value;
/*
    int len;
    if(length==NULL)
        length=&len;
    if(!(fid>=0 && fid<m_adQuery->FieldList->Count))
    {
        strcpy(m_Err,"超出字段范围!");
        return NULL;
    }
    TField *field=m_adQuery->FieldList->Fields[fid];
    *length=0;
    if(!field||field->IsNull)
    {
        strcpy(m_Err,"空字段!");
        return NULL;
    }
    if(m_Data)
        delete m_Data;
    m_Data=NULL;
    if(field->DataType==ftBlob)
    {
        TStream *Stream = NULL;
        try
        {
            *length = ((TBlobField *)field)->BlobSize;
            Stream = m_adQuery->CreateBlobStream(field, bmRead);
            Stream->Seek(0,soFromBeginning);
            *length=Stream->Size;
            m_Data=new char [Stream->Size+1];
            Stream->ReadBuffer(m_Data,*length);
            m_Data[(int)(Stream->Size)] = 0;
        }
        catch(Exception &exception)
        {
            strncpy(m_Err,exception.Message.c_str(),sizeof(m_Err)-1);
        }
        if(Stream)
            delete Stream;
    }
    else
    {
        *length=field->AsString.Length();
        m_Data=new char [field->AsString.Length()+1];
        strcpy(m_Data,field->AsString.c_str());
    }
    strcpy(szFieldName,field->FieldName.c_str());
    return m_Data;*/
}

int   CDBFaceBase::GetIntByIdx(int fid)
{
    char *pData = GetValueByIdx(fid,NULL);
    if(pData)
        return atoi(pData);
    return 0;
}

bool  CDBFaceBase::BeginTrans(int *outLevel)
{
    return m_pDataConnect->BeginTrans();
/*    if(*outLevel)
        *outLevel = m_adMain->BeginTrans();
    else
        m_adMain->BeginTrans();
    return m_adMain->InTransaction;*/
//    return false;
}

bool  CDBFaceBase::Commit()
{
    return m_pDataConnect->CommitTrans();
//    m_adMain->CommitTrans();
//    return !(m_adMain->InTransaction);
//    return false;
}

bool  CDBFaceBase::Rollback()
{
    return m_pDataConnect->Rollback();
//    m_adMain->RollbackTrans();
//    return !(m_adMain->InTransaction);
//    return false;
}

bool  CDBFaceBase::LockTables(char *tables)
{
    return false;
}

bool  CDBFaceBase::UnlockTables(char *tables)
{
    return false;
}

HANDLE CDBFaceBase::BatchBegin(char *szTable,char *Fields)
{
    char *pos = Fields;
    char *pose;
    int fid = 0,len;
    DB_BATCH_INFO *pDbBatchInfo = new DB_BATCH_INFO;
    memset(pDbBatchInfo,0,sizeof(DB_BATCH_INFO));
    strcpy(pDbBatchInfo->TableName,szTable);
    //pDbBatchInfo->filetype = filetype;
    while(pos && pos[0])
    {
        pose = strchr(pos,',');
        if(pose)
        {
            len = pose-pos;
            pose++;
        }
        else
            len = strlen(pos);
        if(len>=120)
        {
            strcpy(m_Err,"字段太长");
            delete pDbBatchInfo;
            return NULL;
        }
        memcpy(pDbBatchInfo->Fields[fid],pos,len);
        pDbBatchInfo->Fields[fid][len] = '\0';
        fid++;
        pos = pose;
    }
    pDbBatchInfo->Fields[fid][0] = '\0';
    if(fid<=0)
    {
        delete pDbBatchInfo;
        return NULL;
    }
//    if(m_DbBatchInfo.fp)
//        fclose(m_DbBatchInfo.fp);
    extern char g_SysDir[MAX_PATH];
    sprintf(pDbBatchInfo->tmpFile,"%s\\W_%X.~tmp",g_SysDir,(ULONG)pDbBatchInfo);
//    WriteLog(pDbBatchInfo->tmpFile);
    pDbBatchInfo->fp = fopen(pDbBatchInfo->tmpFile,"wb");
    if(!pDbBatchInfo->fp)
    {
        strcpy(m_Err,"创建文件失败!");
        delete pDbBatchInfo;
        return NULL;
    }
    return pDbBatchInfo;
}

bool  CDBFaceBase::BatchAdd(HANDLE hdBatchHandle,char *szValues,int type,int ValueLen,bool NewLin)
{
    return false;
}

bool  CDBFaceBase::BatchEnd(HANDLE hdBatchHandle,void *callback)
{
    return false;
}

bool  CDBFaceBase::BatchByFile(char *sFileName,int filetype,char *szTableName,void *callback)
{
    return false;
}

bool  CDBFaceBase::FetchToFile(char *sql,char *pathname,char *szTable,void *callback)
{
    return false;
}

bool   CDBFaceBase::Backup(char *pathname,char *db,void *callback)
{
    return false;
}

bool   CDBFaceBase::Restore(char *pathname,char *dbname,void *callback)
{
    return false;
}

/*bool  CDBFaceBase::GetFields(char *Fields,int *memsize,int *count)
{
    return false;
}
*/

int     CDBFaceBase::FieldCount()
{
    return m_pDataSet->GetCols();
}

char    *CDBFaceBase::GetField(int idx)
{
    return m_pDataSet->m_coldatafmt[idx].name;
}



/*bool CDBFaceBase::GetDSN(char *pDSN,char *pHost)
{
HKEY hKey;
int re;
//char str[120]={0};
char key[1024];
DWORD type,lpcbData=50 ;
    sprintf(key,"Software\\ODBC\\ODBC.ini\\%s",pDSN);
    re=RegOpenKeyEx(HKEY_LOCAL_MACHINE,
            key,0,KEY_ALL_ACCESS,
            &hKey);
    if(re==ERROR_SUCCESS)
    {
        re = RegQueryValueEx(hKey,
                    "Server",
                    NULL,
                    &type,
                    pHost,
                    &lpcbData);
        if(re==ERROR_SUCCESS)
        {
            RegCloseKey(hKey);
            return true;
        }
    }
    RegCloseKey(hKey);
//  return 0;
    //if(str[0]=='1')
    //    return 1;
    return false;
}
*/
/*
bool  CDBFaceBase::CreateDSN (char * szAttributes)
{
char *startpos,*pos;
    startpos=szAttributes;
    while(1)
    {
        pos=strstr(startpos,"\\0");
        if(pos)
        {
            pos[0]=0;
            pos++;
            for(int id=0;id<(int)strlen(pos);id++)
                pos[id]=pos[id+1];
        }
        else
            break;
        startpos=pos;
    }
    if(SQLConfigDataSource(NULL,ODBC_ADD_SYS_DSN,m_ClassName,szAttributes)!=1)
        return false;
    return true;
}
*/

⌨️ 快捷键说明

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