用otl写的oracle取数工具,执行传入在查询语句,把结果输出到文件

时间:2022-06-28 14:34:17

项目中经常需要用到此类型的工具

 #pragma warning (disable:4786)
#include <iostream>
#include <map>
#include <string>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#define OTL_ORA9I // Compile OTL 4.0/OCI9i
#define OTL_STREAM_READ_ITERATOR_ON
#define OTL_STL #ifdef WIN32
#include <conio.h>
#else
//#define PASS_SECURITY
#endif #ifdef PASS_SECURITY
#include <pass_security/TDBCrypt.h> //包含密码解析头文件
#else
#define DB_ENV_LEN 100
#endif #include "otlv4.h" // include the OTL 4 header file #define OTL_BUFFER_SIZE 2000 //otl缓冲行数
#define BUFFER_SIZE 2000 //列内容缓冲 int select(otl_connect& db, const string &sql, FILE *fp, const string &delemer = "|") //获得sql返回,并以|为分隔符输出到文件(第一行是列名)
{ // create select stream
otl_stream stream_i;
//将输出类型全部转成string
stream_i.set_all_column_types(otl_all_date2str | otl_all_num2str); stream_i.open(OTL_BUFFER_SIZE, // buffer size
sql.c_str(),
// SELECT statement
db // connect object
); //文件头
int desc_len;
otl_column_desc *desc = stream_i.describe_select(desc_len);
for(int n = ; n < desc_len; ++n){
fprintf(fp, "%s%s", desc[n].name, delemer.c_str());
}
fprintf(fp, "\n"); //文件内容
string content;
int row_count = ; while (!stream_i.eof())
{
for(int n = ; n < desc_len; ++n){
stream_i >> content;
fprintf(fp, "%s%s", content.c_str(), delemer.c_str());
}
fprintf(fp, "\n");
row_count++;
} cerr << row_count << " rows loaded " << endl; return ;
} int get_passwd(string& io_dbname) //通过用户名获得 user/pass@database 形式字符串
{ if (string::npos != io_dbname.find("/"))
{
return ;
} #ifdef PASS_SECURITY
std::string strDBConnect = io_dbname; char sDB[DB_ENV_LEN + ],sUSR[DB_ENV_LEN + ],sPWD[DB_ENV_LEN + ];
security::TDBCrypt Crypt;
int iresult = Crypt.init();
if (iresult)
{
cerr << "get passfile failed" << std::endl;
return ;
} if (Crypt.fetchUserPwd(io_dbname.c_str(), sUSR, sPWD, sDB))
{
io_dbname = "";
io_dbname = io_dbname + sUSR + "/" + sPWD + "@" + sDB;
return ;
}
else
{
cerr << "get password failed" << std::endl;
return ;
}
#else #ifdef PASS_SECURITY
size_t nDBpos = io_dbname.find("@"); string sUSR = io_dbname.substr(, nDBpos);
string sDB = io_dbname.substr(nDBpos + , io_dbname.length() - );
char sPWD[DB_ENV_LEN + ] = ""; cerr << "please input password of "<< io_dbname << " : " << std::endl; int ch;
size_t i = ;
while (((ch = getch()) != '\r') && i < DB_ENV_LEN)
{
if (ch != '\b')
{
sPWD[i++] = (char) ch;
cerr << "*";
}
else
{
if (i >= )
{
i--;
cerr << "\b \b";
}
else
{
cerr << "\007";
}
}
}
sPWD[i] = '\0';
cerr << endl; io_dbname = sUSR + "/" + sPWD + "@" + sDB;
#endif #endif return ;
} int main(const int argc, const char * const argv[])
{
if (argc < )
{
cerr << "参数错误!用法示例:\t" << endl;
cerr << "dtload user/pass@DB sql filename" << endl;
return ;
} map<string, string> mpParam;
for (int i = ; i < argc; i++)
{
if (*argv[i] == '-')
{
if (i < argc)
{
mpParam[argv[i]] = argv[i + ];
}
else
{
mpParam[argv[i]] = "";
}
}
} otl_connect db; string dbname = argv[];
string sql = argv[];
string file = "";
FILE *fp = NULL; //如果输出文件名为空,则输出到屏幕
if (argc >= )
{ file = argv[];
fp = fopen(file.c_str(), "w"); if (fp == NULL)
{
cerr << "openfile " << file << " err! maybe can not writed\n ";
return ;
}
}
else
{
fp = stdout;
} string delemer = "|";
if (argc >= )
{
delemer = argv[];
} //如果输入参数不包含密码,则从dbfile里面找密码
//get_passwd(dbname); otl_connect::otl_initialize(); // initialize OCI environment try
{
db.rlogon(dbname.c_str()); // connect to Oracle
select(db, sql, fp, delemer); // select records from table
}
catch(const otl_exception& p)
{ // intercept OTL exceptions
cerr<<p.msg<<endl; // print out error message
cerr<<p.stm_text<<endl; // print out SQL that caused the error
cerr<<p.var_info<<endl; // print out the variable that caused the error
} fclose(fp);
db.logoff(); // disconnect from Oracle }

Makefile

 ## -*- Makefile -*-
## by hch top_srcdir = ../ DISPATCH = $(bindir)/Dispatch TARGETS = tbasload include $(top_srcdir)/MKFILE/Make.rules OBJS = tbasload.o CPPFLAGS := \
$(ORACLE_INCLUDE) \
-I. \
$(CPPFLAGS) LDFLAGS := \
$(LDFLAGS) \
$(ORACLE_LDFLAGS) tbasload: $(OBJS)
@echo "Creating executable -*- $@ -*- ..."
@-rm -f $@
@$(CXX) -o $@ $(LDFLAGS) $(ORACLE_LIBS) $^

Make.rules

 # -*-Make.rules-*-
# by szh buildbits ?=
debug ?= STATICLIBS = yes VERSION = 1.0.
SOVERSION =
ifeq ($(buildbits),)
bindir = $(top_srcdir)/bin
libdir = $(top_srcdir)/lib
else
bindir = $(top_srcdir)/bin32
libdir = $(top_srcdir)/lib32
endif
includedir = $(top_srcdir)/include/common UNAME := $(shell uname) include $(top_srcdir)/MKFILE/Make.rules.$(UNAME)
include $(top_srcdir)/MKFILE/Make.rules.Customized CPPFLAGS = -I$(includedir) $(ORACLE_INCLUDE)
LDFLAGS := $(LDFLAGS) $(CXXFLAGS) -L$(libdir) ifeq ($(mklibfilename),)
mklibfilename = $(if $(),lib$().so.$(),lib$().so)
endif ifeq ($(mksoname),)
mksoname = $(if $(),lib$().so.$(),lib$().so)
endif ifeq ($(mklibname),)
ifeq ($(STATICLIBS),yes)
mklibname = lib$().a
else
mklibname = lib$().so
endif
endif ifndef mklibtargets
ifeq ($(STATICLIBS),yes)
mklibtargets = $()
else
mklibtargets = $() $() $()
endif
endif ifeq ($(mkshlib),)
$(error You need to define mkshlib in Make.rules.$(UNAME))
endif EVERYTHING = all clean .SUFFIXES: %.o: %.cpp
@echo " $<"
@$(CXX) -o $@ -c $(CPPFLAGS) $(CXXFLAGS) $< %.o: %.c
@echo " $<"
@$(CC) -o $@ -c $(CPPFLAGS) $(CFLAGS) $< all:: warning $(SRCS) $(TARGETS) warning::
@echo
@echo "CONFIGURATION: buildbits=$(buildbits) debug=$(debug) STATICLIBS=$(STATICLIBS)"
@echo clean::
@-rm -f $(TARGETS) $(OBJS)
@-rm -f core *.o version.h