NCBI C++ ToolKit
blobreader.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: blobreader.cpp 85334 2019-02-04 14:40:43Z ucko $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Vladimir Soussov
27  *
28  */
29 
30 #include <ncbi_pch.hpp>
34 
36 
37 static char* getParam(char tag, int argc, char* argv[], bool* flag= 0)
38 {
39  for(int i= 1; i < argc; i++) {
40  if(((*argv[i] == '-') || (*argv[i] == '/')) &&
41  (*(argv[i]+1) == tag)) { // tag found
42  if(flag)
43  *flag= true;
44 
45  if(*(argv[i]+2) == '\0') { // tag is a separate arg
46  if(i == argc - 1)
47  return 0;
48  if(*argv[i+1] != *argv[i])
49  return argv[i+1];
50  else
51  return 0;
52  }
53  else {
54  return argv[i]+2;
55  }
56  }
57  }
58 
59  if(flag)
60  *flag= false;
61 
62  return 0;
63 }
64 
65 int main(int argc, char* argv[])
66 {
67  string driver_name;
68  string server_name;
69  string user_name;
70  string passwd;
71  string query;
72  string table_name= kEmptyStr;
73  string blob_key;
74 
75  if(argc < 2) {
76  cerr << argv[0]
77  << " [-d<driver_name>] [-S<server_name>]"
78  << " [-U<user_name>] [-P<password>] [-Q<query> | -T<table_name> -K<blob_id>] [-Z<compress_method>]"
79  << endl;
80  return 0;
81  }
82 
83  const char* p= getParam('S', argc, argv);
84  if(p == 0) {
85  p= getenv("SQL_SERVER");
86  }
87  server_name= p? p : "MSDEV1";
88 
89  p= getParam('d', argc, argv);
90  if (p) {
91  driver_name= p;
92  } else {
93  p= getenv("DBAPI_DRIVER");
94  if(p == 0) {
95  driver_name= (server_name.find("MS") != NPOS)? "ftds" : "ctlib";
96  }
97  else driver_name= p;
98  }
99 
100  p= getParam('U', argc, argv);
101  if(p == 0) {
102  p= getenv("SQL_USER");
103  }
104  user_name= p? p : "anyone";
105 
106  p= getParam('P', argc, argv);
107  if(p == 0) {
108  p= getenv("SQL_PASSWD");
109  }
110  passwd= p? p : "allowed";
111 
113  p= getParam('Z', argc, argv);
114  if(p) {
115  if(*p == 'z') cm= eZLib;
116  else cm= eBZLib;
117  }
118 
119  bool f;
120  p= getParam('Q', argc, argv, &f);
121  if(p) {
122  query= string("set TEXTSIZE 2147483647 ") + p;
123  }
124  else {
125  if(f) { // query is on stdin
126  query= "set TEXTSIZE 2147483647 ";
127  cout << "query is on ctdin" << endl;
128  char c;
129  c= cin.get();
130  while(!cin.eof()) {
131  cout << c;
132  query+= c;
133  c= cin.get();
134  }
135  cout << endl;
136  }
137  if(query.empty()) {
138  p= getParam('T', argc, argv);
139  if(p == 0)
140  p= getenv("DATA_TABLE");
141  table_name= p? p : "";
142  p= getParam('K', argc, argv);
143  blob_key= p? p : "";
144  }
145  }
146 
147  try {
148  C_DriverMgr drv_mgr;
149  string err_msg;
150  map<string, string> packet;
151  packet.insert (map<string, string>::value_type (string("packet"),
152  string("2048")));
153  unique_ptr<I_DriverContext> my_context(drv_mgr.GetDriverContext(driver_name,
154  &err_msg, &packet));
155  if(!my_context) {
156  cerr << "blobreader: Cannot load a driver " << driver_name << " ["
157  << err_msg << "] " << endl;
158  return 1;
159  }
160  if(!table_name.empty()) {
161  CDB_Connection* con= my_context->Connect(server_name, user_name, passwd, 0, true);
162 
163  query= "select * from " + table_name + " where 0=1";
164  CDB_LangCmd* lcmd = con->LangCmd(query);
165 
166  lcmd->Send();
167 
168  unsigned int n;
169  int k= 0;
170  string key_col_name;
171  string num_col_name;
172 
173  query= "set TEXTSIZE 2147483647 select ";
174 
175  while (lcmd->HasMoreResults()) {
176  CDB_Result* r = lcmd->Result();
177  if (!r)
178  continue;
179 
180  if (r->ResultType() == eDB_RowResult) {
181  n= r->NofItems();
182  if(n < 2) {
183  delete r;
184  continue;
185  }
186 
187 
188  for(unsigned int j= 0; j < n; j++) {
189  switch (r->ItemDataType(j)) {
190  case eDB_VarChar:
191  case eDB_Char:
192  case eDB_LongChar:
193  key_col_name= r->ItemName(j);
194  break;
195 
196  case eDB_Int:
197  case eDB_SmallInt:
198  case eDB_TinyInt:
199  case eDB_BigInt:
200  num_col_name= r->ItemName(j);
201  break;
202 
203  case eDB_Text:
204  case eDB_Image:
205  case eDB_VarCharMax:
206  case eDB_VarBinaryMax:
207  if(k++) query+= ",";
208  query+= r->ItemName(j);
209  default:
210  break;
211  }
212  }
213  while(r->Fetch());
214  }
215  delete r;
216  }
217  delete lcmd;
218  delete con;
219 
220  if(k < 1) {
221  query+= "*";
222  }
223  query+= " from " + table_name;
224  if((!blob_key.empty()) && (!key_col_name.empty())) {
225  query+= " where " + key_col_name + "= '" + blob_key + "'";
226  if(!num_col_name.empty()) {
227  query+= " order by " + num_col_name;
228  }
229  }
230  else if(!key_col_name.empty()) {
231  query+= " order by " + key_col_name;
232  if(!num_col_name.empty()) {
233  query+= "," + num_col_name;
234  }
235  }
236  }
237 
238 
239  CBlobRetriever retr(my_context.get(), server_name, user_name, passwd, query);
240  while(retr.IsReady()) {
241  retr.Dump(cout, cm);
242  }
243  } catch (CDB_Exception& e) {
244  CDB_UserHandler_Stream myExHandler(&cerr);
245 
246  myExHandler.HandleIt(&e);
247  return 1;
248  }
249  return 0;
250 }
251 
252 
@ eNone
None specified.
Definition: blast_def.h:326
int main(int argc, char *argv[])
Definition: blobreader.cpp:65
static char * getParam(char tag, int argc, char *argv[], bool *flag=0)
Definition: blobreader.cpp:37
USING_NCBI_SCOPE
Definition: blobreader.cpp:35
ECompressMethod
Definition: blobstore.hpp:40
@ eZLib
Definition: blobstore.hpp:42
@ eBZLib
Definition: blobstore.hpp:43
bool IsReady() const
Definition: blobstore.hpp:145
bool Dump(ostream &s, ECompressMethod cm=eNone)
Definition: blobstore.cpp:218
CDB_Exception –.
Definition: exception.hpp:118
iterator_bool insert(const value_type &val)
Definition: map.hpp:165
string
Definition: cgiapp.hpp:687
I_DriverContext * GetDriverContext(const string &driver_name, string *err_msg=0, const map< string, string > *attr=0)
Definition: driver_mgr.cpp:308
virtual bool HandleIt(CDB_Exception *ex)
Handle the exceptions resulting from a native API call, one-by-one.
Definition: exception.cpp:765
@ eDB_RowResult
Definition: interfaces.hpp:387
virtual CDB_Result * Result()
Get result set.
Definition: public.cpp:771
virtual bool Send()
Send command to the server.
Definition: public.cpp:745
virtual CDB_LangCmd * LangCmd(const string &lang_query)
Make language command.
Definition: public.cpp:355
virtual bool HasMoreResults() const
Definition: public.cpp:777
@ eDB_Char
Definition: types.hpp:58
@ eDB_VarChar
Definition: types.hpp:57
@ eDB_TinyInt
Definition: types.hpp:55
@ eDB_LongChar
Definition: types.hpp:70
@ eDB_Image
Definition: types.hpp:67
@ eDB_Int
Definition: types.hpp:53
@ eDB_VarCharMax
Definition: types.hpp:72
@ eDB_BigInt
Definition: types.hpp:56
@ eDB_Text
Definition: types.hpp:66
@ eDB_SmallInt
Definition: types.hpp:54
@ eDB_VarBinaryMax
Definition: types.hpp:73
#define kEmptyStr
Definition: ncbistr.hpp:123
#define NPOS
Definition: ncbistr.hpp:133
int i
yy_size_t n
const char * tag
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
double f(double x_, const double &y_)
Definition: njn_root.hpp:188
static const char table_name[]
Definition: bcp.c:249
static string query
Modified on Sat Dec 02 09:21:26 2023 by modify_doxy.py rev. 669887