日期:2014-05-17 浏览次数:20882 次
/*
* 根据ID批量更新BLOB数据
* @param ID和图片路径组成的集合
* @param 表名称
* @param 主键名称
* @param BLOB字段名称
* @param 失败的记录
*/
bool OracleStatementImpl::CopyBlobIn(map<string,string>& fileNames, const string& tableName, const string& primaryKey, const string& blobColumnName, map<string,string>& failure)
{
failure.clear();
reset();
string sql = "update "+ tableName + " set "+ blobColumnName + " = :1"
+" where " + primaryKey +" = :2";
char buffer[255];
CHECK(_sessImpl->errhp, OCIDescriptorAlloc((dvoid *) _sessImpl->envhp, (dvoid **) &lobl,
(ub4)OCI_DTYPE_LOB, (size_t) 0, (dvoid **) 0));
CHECK(_sessImpl->errhp, OCILobCreateTemporary(_sessImpl->svchp,_sessImpl->errhp,lobl,0,SQLCS_IMPLICIT,
OCI_TEMP_BLOB, OCI_ATTR_NOCACHE, OCI_DURATION_SESSION));
CHECK(_sessImpl->errhp, OCIStmtPrepare(stmthp, _sessImpl->errhp, (ub1*)sql.c_str(), strlen(sql.c_str()), OCI_NTV_SYNTAX, OCI_DEFAULT));
CHECK(_sessImpl->errhp, OCIBindByPos(stmthp, &m_pBind, _sessImpl->errhp, 1, (dvoid*)&lobl,
sizeof(OCILobLocator *), SQLT_BLOB, 0,0,0,0,0, OCI_DEFAULT));
CHECK(_sessImpl->errhp, OCIBindByPos(stmthp, &m_pBind, _sessImpl->errhp,
(ub4)2, (dvoid*)buffer, (sb4)sizeof(buffer), (ub2)SQLT_AVC,
0,0,0,0,0, OCI_DEFAULT));
_sessImpl->begin();
for(map<string,string>::iterator file = fileNames.begin(); file != fileNames.end(); file++)
{
strcpy(buffer,file->first.c_str());
sb4 status = OCIStmtExecute(_sessImpl->svchp, stmthp, _sessImpl->errhp,
(ub4)1, (ub4) 0, (CONST OCISnapshot *) NULL, (OCISnapshot *) NULL,
OCI_DEFAULT);
if(status == OCI_NO_DATA)
{
failure.insert(*file);
continue;
}
FILE* fp = fopen(file->second.c_str(), (const char *) "rb");
if(!fp)
{
failure.insert(*file);
continue;
}
stream_write_lob(lobl, fp, get_file_length(fp));
fclose(fp);
}
_sessImpl->commit();
CHECK(_sessImpl->errhp,OCIDescriptorFree((dvoid *) lobl, (ub4) OCI_DTYPE_LOB));
return (failure.size() == 0);
}