用otl写的oracle取数工具,执行传入在查询语句,把结果输出到文件
项目中经常需要用到此类型的工具
1 #pragma warning (disable:4786) 2 #include <iostream> 3 #include <map> 4 #include <string> 5 using namespace std; 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <algorithm> 9 #define OTL_ORA9I // Compile OTL 4.0/OCI9i 10 #define OTL_STREAM_READ_ITERATOR_ON 11 #define OTL_STL 12 13 #ifdef WIN32 14 #include <conio.h> 15 #else 16 //#define PASS_SECURITY 17 #endif 18 19 #ifdef PASS_SECURITY 20 #include <pass_security/TDBCrypt.h> //包含密码解析头文件 21 #else 22 #define DB_ENV_LEN 100 23 #endif 24 25 #include "otlv4.h" // include the OTL 4 header file 26 27 28 #define OTL_BUFFER_SIZE 2000 //otl缓冲行数 29 #define BUFFER_SIZE 2000 //列内容缓冲 30 31 32 33 int select(otl_connect& db, const string &sql, FILE *fp, const string &delemer = "|") //获得sql返回,并以|为分隔符输出到文件(第一行是列名) 34 { 35 36 // create select stream 37 otl_stream stream_i; 38 //将输出类型全部转成string 39 stream_i.set_all_column_types(otl_all_date2str | otl_all_num2str); 40 41 stream_i.open(OTL_BUFFER_SIZE, // buffer size 42 sql.c_str(), 43 // SELECT statement 44 db // connect object 45 ); 46 47 //文件头 48 int desc_len; 49 otl_column_desc *desc = stream_i.describe_select(desc_len); 50 for(int n = 0; n < desc_len; ++n){ 51 fprintf(fp, "%s%s", desc[n].name, delemer.c_str()); 52 } 53 fprintf(fp, "\n"); 54 55 //文件内容 56 string content; 57 int row_count = 0; 58 59 while (!stream_i.eof()) 60 { 61 for(int n = 0; n < desc_len; ++n){ 62 stream_i >> content; 63 fprintf(fp, "%s%s", content.c_str(), delemer.c_str()); 64 } 65 fprintf(fp, "\n"); 66 row_count++; 67 } 68 69 cerr << row_count << " rows loaded " << endl; 70 71 return 0; 72 } 73 74 75 int get_passwd(string& io_dbname) //通过用户名获得 user/pass@database 形式字符串 76 { 77 78 if (string::npos != io_dbname.find("/")) 79 { 80 return 0; 81 } 82 83 84 85 #ifdef PASS_SECURITY 86 std::string strDBConnect = io_dbname; 87 88 char sDB[DB_ENV_LEN + 1],sUSR[DB_ENV_LEN + 1],sPWD[DB_ENV_LEN + 1]; 89 security::TDBCrypt Crypt; 90 int iresult = Crypt.init(); 91 if (iresult) 92 { 93 cerr << "get passfile failed" << std::endl; 94 return 1; 95 } 96 97 if (Crypt.fetchUserPwd(io_dbname.c_str(), sUSR, sPWD, sDB)) 98 { 99 io_dbname = ""; 100 io_dbname = io_dbname + sUSR + "/" + sPWD + "@" + sDB; 101 return 0; 102 } 103 else 104 { 105 cerr << "get password failed" << std::endl; 106 return 1; 107 } 108 #else 109 110 #ifdef PASS_SECURITY 111 size_t nDBpos = io_dbname.find("@"); 112 113 string sUSR = io_dbname.substr(0, nDBpos); 114 string sDB = io_dbname.substr(nDBpos + 1, io_dbname.length() - 1); 115 char sPWD[DB_ENV_LEN + 1] = ""; 116 117 118 cerr << "please input password of "<< io_dbname << " : " << std::endl; 119 120 int ch; 121 size_t i = 0; 122 while (((ch = getch()) != '\r') && i < DB_ENV_LEN) 123 { 124 if (ch != '\b') 125 { 126 sPWD[i++] = (char) ch; 127 cerr << "*"; 128 } 129 else 130 { 131 if (i >= 1) 132 { 133 i--; 134 cerr << "\b \b"; 135 } 136 else 137 { 138 cerr << "\007"; 139 } 140 } 141 } 142 sPWD[i] = '\0'; 143 cerr << endl; 144 145 io_dbname = sUSR + "/" + sPWD + "@" + sDB; 146 #endif 147 148 #endif 149 150 return 0; 151 } 152 153 154 int main(const int argc, const char * const argv[]) 155 { 156 if (argc < 3) 157 { 158 cerr << "参数错误!用法示例:\t" << endl; 159 cerr << "dtload user/pass@DB sql filename" << endl; 160 return 0; 161 } 162 163 map<string, string> mpParam; 164 for (int i = 1; i < argc; i++) 165 { 166 if (*argv[i] == '-') 167 { 168 if (i < argc) 169 { 170 mpParam[argv[i]] = argv[i + 1]; 171 } 172 else 173 { 174 mpParam[argv[i]] = ""; 175 } 176 } 177 } 178 179 otl_connect db; 180 181 string dbname = argv[1]; 182 string sql = argv[2]; 183 string file = ""; 184 FILE *fp = NULL; 185 186 //如果输出文件名为空,则输出到屏幕 187 if (argc >= 4) 188 { 189 190 file = argv[3]; 191 fp = fopen(file.c_str(), "w"); 192 193 if (fp == NULL) 194 { 195 cerr << "openfile " << file << " err! maybe can not writed\n "; 196 return 0; 197 } 198 } 199 else 200 { 201 fp = stdout; 202 } 203 204 string delemer = "|"; 205 if (argc >= 5) 206 { 207 delemer = argv[4]; 208 } 209 210 //如果输入参数不包含密码,则从dbfile里面找密码 211 //get_passwd(dbname); 212 213 214 otl_connect::otl_initialize(); // initialize OCI environment 215 216 try 217 { 218 db.rlogon(dbname.c_str()); // connect to Oracle 219 select(db, sql, fp, delemer); // select records from table 220 } 221 catch(const otl_exception& p) 222 { // intercept OTL exceptions 223 cerr<<p.msg<<endl; // print out error message 224 cerr<<p.stm_text<<endl; // print out SQL that caused the error 225 cerr<<p.var_info<<endl; // print out the variable that caused the error 226 } 227 228 fclose(fp); 229 db.logoff(); // disconnect from Oracle 230 231 }
Makefile
1 ## -*- Makefile -*- 2 ## by hch 3 4 top_srcdir = ../ 5 6 DISPATCH = $(bindir)/Dispatch 7 8 TARGETS = tbasload 9 10 include $(top_srcdir)/MKFILE/Make.rules 11 12 13 OBJS = tbasload.o 14 15 CPPFLAGS := \ 16 $(ORACLE_INCLUDE) \ 17 -I. \ 18 $(CPPFLAGS) 19 20 LDFLAGS := \ 21 $(LDFLAGS) \ 22 $(ORACLE_LDFLAGS) 23 24 tbasload: $(OBJS) 25 @echo "Creating executable -*- $@ -*- ..." 26 @-rm -f $@ 27 @$(CXX) -o $@ $(LDFLAGS) $(ORACLE_LIBS) $^
Make.rules
1 # -*-Make.rules-*- 2 # by szh 3 4 buildbits ?= 32 5 debug ?= 1 6 7 STATICLIBS = yes 8 9 10 VERSION = 1.0.0 11 SOVERSION = 10 12 ifeq ($(buildbits),64) 13 bindir = $(top_srcdir)/bin 14 libdir = $(top_srcdir)/lib 15 else 16 bindir = $(top_srcdir)/bin32 17 libdir = $(top_srcdir)/lib32 18 endif 19 includedir = $(top_srcdir)/include/common 20 21 UNAME := $(shell uname) 22 23 include $(top_srcdir)/MKFILE/Make.rules.$(UNAME) 24 include $(top_srcdir)/MKFILE/Make.rules.Customized 25 26 CPPFLAGS = -I$(includedir) $(ORACLE_INCLUDE) 27 LDFLAGS := $(LDFLAGS) $(CXXFLAGS) -L$(libdir) 28 29 ifeq ($(mklibfilename),) 30 mklibfilename = $(if $(2),lib$(1).so.$(2),lib$(1).so) 31 endif 32 33 ifeq ($(mksoname),) 34 mksoname = $(if $(2),lib$(1).so.$(2),lib$(1).so) 35 endif 36 37 ifeq ($(mklibname),) 38 ifeq ($(STATICLIBS),yes) 39 mklibname = lib$(1).a 40 else 41 mklibname = lib$(1).so 42 endif 43 endif 44 45 ifndef mklibtargets 46 ifeq ($(STATICLIBS),yes) 47 mklibtargets = $(3) 48 else 49 mklibtargets = $(1) $(2) $(3) 50 endif 51 endif 52 53 ifeq ($(mkshlib),) 54 $(error You need to define mkshlib in Make.rules.$(UNAME)) 55 endif 56 57 EVERYTHING = all clean 58 59 .SUFFIXES: 60 61 %.o: %.cpp 62 @echo " $<" 63 @$(CXX) -o $@ -c $(CPPFLAGS) $(CXXFLAGS) $< 64 65 %.o: %.c 66 @echo " $<" 67 @$(CC) -o $@ -c $(CPPFLAGS) $(CFLAGS) $< 68 69 all:: warning $(SRCS) $(TARGETS) 70 71 warning:: 72 @echo 73 @echo "CONFIGURATION: buildbits=$(buildbits) debug=$(debug) STATICLIBS=$(STATICLIBS)" 74 @echo 75 76 77 clean:: 78 @-rm -f $(TARGETS) $(OBJS) 79 @-rm -f core *.o version.h
~~积土成山,风雨兴焉;积水成渊,蛟龙生焉;~~~