代码示例如下:
#include "gtest/gtest.h"
#include <oci/occi.h>
#include <cstdlib>
using namespace oracle::occi;
class test_occi: public testing::Test{
public:
void SetUp() override {
// 在所有测试运行之前设置环境变量
::setenv("CL_HOME", "/home/fm2/frw/lib/bin", 1);
::setenv("NLS_LANG", "AMERICAN_AMERICA.ZHS16GBK", 1);
::setenv("ORACLE_HOME", "/home/fm2/frw/lib/bin", 1);
::setenv("TNS_ADMIN", "/home/fm2/server/etc/user_config", 1);
::setenv("LD_LIBRARY_PATH", "$ORACLE_HOME/lib:$LD_LIBRARY_PATH", 1);
const char* env_var = ::getenv("TNS_ADMIN");
std::cout << "TNS_ADMIN:" << env_var << std::endl;
}
void TearDown() override {
// 在所有测试运行之后清除环境变量
::unsetenv("CL_HOME");
::unsetenv("NLS_LANG");
::unsetenv("ORACLE_HOME");
}
};
void populateBlob (Blob &blob, int size)
throw (SQLException)
{
Stream *outstream = blob.getStream (1,0);
char *buffer = new char[size];
memset (buffer, (char)10, size);
outstream->writeBuffer (buffer, size);
char *c = (char *)"";
outstream->writeLastBuffer (c,0);
delete (buffer);
blob.closeStream (outstream);
}
TEST_F(test_occi, test_minus_int_1) {
oracle::occi::Environment* env = oracle::occi::Environment::createEnvironment(oracle::occi::Environment::DEFAULT);
oracle::occi::Connection* conn = env->createConnection("fm", "1", "rxdb");
auto myStmt = conn->createStatement("begin dbms_lob.createtemporary(:p1,FALSE);end;");
myStmt->registerOutParam(1,OCCIBLOB);
myStmt->executeUpdate();
std::string data= "1232";
Blob resClob = myStmt->getBlob(1);
resClob.write((unsigned int)data.size(), (unsigned char*)data.c_str(),(unsigned int)data.size());
conn->terminateStatement (myStmt);
oracle::occi::Statement* stmt = conn->createStatement("insert into tseal (seal_id, seal) values (2, :1)");
stmt->setBlob(1, resClob);
stmt->executeUpdate();
conn->commit();
}
int main(int argc, char * argv[]) {
testing::InitGoogleTest(&argc, argv);
testing::GTEST_FLAG(output) = "xml:test.xml";
return RUN_ALL_TESTS();
}