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

Go to the SVN repository for this file.

1 /* $Id: blobrws.cpp 87145 2019-07-30 15:28:08Z 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 Ivanovskiy
27  *
28  */
29 
30 #include <ncbi_pch.hpp>
31 #include <corelib/ncbimisc.hpp>
35 
36 using namespace std;
37 
39 
40 static char* getParam(char tag, int argc, char* argv[], bool* flag= 0)
41 {
42  for(int i= 1; i < argc; i++) {
43  if(((*argv[i] == '-') || (*argv[i] == '/')) &&
44  (*(argv[i]+1) == tag)) { // tag found
45  if(flag)
46  *flag= true;
47 
48  if(*(argv[i]+2) == '\0') { // tag is a separate arg
49  if(i == argc - 1)
50  return 0;
51  if(*argv[i+1] != *argv[i])
52  return argv[i+1];
53  else
54  return 0;
55  }
56  else {
57  return argv[i]+2;
58  }
59  }
60  }
61 
62  if(flag)
63  *flag= false;
64 
65  return 0;
66 }
67 
68 int main(int argc, char* argv[])
69 {
70  string driver_name;
71  string server_name;
72  string user_name;
73  string passwd;
74  string blob_key;
75  string table_name;
76  string table_hint;
77 
78  size_t imagesize= 0;
79 
80  bool readmode = false;
81  bool logmode = false;
82  bool preallocated = false;
83  CSimpleBlobStore::TFlags flags = CSimpleBlobStore::kDefaults;
84 
85  if(argc < 2)
86  {
87  cerr << argv[0]
88  << " -K<blob_id> [-r] [-d<driver_name>] [-S<server_name>] [-T<table_name>]"
89  << " [-U<user_name>] [-P<password>] [-L<image size>] [-Z<compress_method>]"
90  << " [-H<table_hint>] [-l] [-a]"
91  << endl;
92  return 0;
93  }
94 
95  const char* p= getParam('K', argc, argv);
96  if(p == 0)
97  {
98  cerr << argv[0] << "Blob ID is missing" << endl;
99  return 1;
100  }
101  blob_key= p;
102 
103  getParam('r', argc, argv, &readmode);
104  getParam('l', argc, argv, &logmode);
105  if (logmode) {
107  }
108  getParam('a', argc, argv, &preallocated);
109  if (preallocated) {
111  }
112 
113  p= getParam('S', argc, argv);
114  if(p == 0)
115  {
116  p= getenv("SQL_SERVER");
117  }
118  server_name= p? p : "MSDEV1";
119 
120  p= getParam('d', argc, argv);
121  if (p)
122  {
123  driver_name= p;
124  }
125  else
126  {
127  p= getenv("DBAPI_DRIVER");
128  if(p == 0)
129  {
130  driver_name= (server_name.find("MS") != NPOS)? "ftds" : "ctlib";
131  }
132  else driver_name= p;
133  }
134 
135  p= getParam('U', argc, argv);
136  if(p == 0)
137  {
138  p= getenv("SQL_USER");
139  }
140  user_name= p? p : "anyone";
141 
142  p= getParam('P', argc, argv);
143  if(p == 0)
144  {
145  p= getenv("SQL_PASSWD");
146  }
147  passwd= p? p : "allowed";
148 
150  p= getParam('Z', argc, argv);
151  if(p)
152  {
153  if(*p == 'z') cm= eZLib;
154  else cm= eBZLib;
155  }
156 
157  p= getParam('L', argc, argv);
158  if(p) imagesize= atoi(p);
159 
160  p= getParam('T', argc, argv);
161  if(p == 0)
162  {
163  p= getenv("DATA_TABLE");
164  }
165  table_name= p? p : "MyDataTable";
166 
167  p= getParam('H', argc, argv);
168  if(p) table_hint= p;
169 
170  try
171  {
172  C_DriverMgr drv_mgr;
173  string err_msg;
174  map<string, string> packet;
175  packet.insert (map<string, string>::value_type (string("packet"),
176  string("2048")));
177  unique_ptr<I_DriverContext> my_context(drv_mgr.GetDriverContext(driver_name,
178  &err_msg, &packet));
179  if(!my_context)
180  {
181  cerr << "Cannot load a driver " << driver_name << " ["
182  << err_msg << "] " << endl;
183  return 1;
184  }
185 
186  AutoPtr<CDB_Connection> con(my_context->Connect(server_name,
187  user_name, passwd,
188  0, true));
189 
190  CBlobStoreStatic blobrw(con.get(), table_name, cm, imagesize, flags);
191 
192  if(readmode)
193  {
194  if(blobrw.Exists(blob_key))
195  {
196  AutoPtr<istream> pStream
197  (blobrw.OpenForRead(blob_key, table_hint));
198 
199  if(!pStream.get())
200  {
201  cerr << "Can't open blob for read." << endl;
202  return 1;
203  }
204  cout << pStream->rdbuf();
205  }
206  else
207  {
208  cerr << "Blob with key \"" << blob_key
209  << "\" doesn't exist." << endl;
210  return 1;
211  }
212  }
213  else
214  {
215  if(blobrw.Exists(blob_key))
216  {
217  blobrw.Delete(blob_key);
218  }
219  AutoPtr<ostream> pStream(blobrw.OpenForWrite
220  (blob_key, table_hint));
221  if(!pStream.get())
222  {
223  cerr << "Can't open blob for write." << endl;
224  return 1;
225  }
226  *pStream << cin.rdbuf();
227  }
228  } catch (CDB_Exception& e) {
229  CDB_UserHandler_Stream myExHandler(&cerr);
230 
231  myExHandler.HandleIt(&e);
232  return 1;
233  }
234  return 0;
235 }
236 
237 
int main(int argc, char *argv[])
Definition: blobrws.cpp:68
static char * getParam(char tag, int argc, char *argv[], bool *flag=0)
Definition: blobrws.cpp:40
USING_NCBI_SCOPE
Definition: blobrws.cpp:38
ECompressMethod
Definition: blobstore.hpp:40
@ eZLib
Definition: blobstore.hpp:42
@ eBZLib
Definition: blobstore.hpp:43
void Delete(const string &blob_id)
Definition: blobstore.cpp:747
ostream * OpenForWrite(const string &blob_id, const CTempString &table_hint=kEmptyStr)
Obtain an output stream for a specific BLOB.
Definition: blobstore.cpp:831
istream * OpenForRead(const string &blob_id, const CTempString &table_hint=kEmptyStr)
Obtain an input stream for a specific BLOB.
Definition: blobstore.cpp:768
bool Exists(const string &blob_id)
Definition: blobstore.cpp:715
CDB_Exception –.
Definition: exception.hpp:118
static const TFlags kDefaults
Definition: blobstore.hpp:206
@ fLogBlobs
Enable server-side logging.
Definition: blobstore.hpp:202
@ fPreallocated
Don't create rows or clean up excess rows.
Definition: blobstore.hpp:203
iterator_bool insert(const value_type &val)
Definition: map.hpp:165
static uch flags
static const char table_name[]
Definition: bcp.c:249
element_type * get(void) const
Get pointer.
Definition: ncbimisc.hpp:469
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
#define NPOS
Definition: ncbistr.hpp:133
int i
const char * tag
Miscellaneous common-use basic types and functionality.
Modified on Sat Apr 13 11:50:57 2024 by modify_doxy.py rev. 669887