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

Go to the SVN repository for this file.

1 /* $Id: blobwriter.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 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 blob_key;
72  string table_name;
73  string key_col_name;
74  string num_col_name;
75  string table_hint;
76 
77  size_t imagesize= 0;
78 
79  bool preallocated = false;
80  CSimpleBlobStore::TFlags flags = CSimpleBlobStore::kDefaults;
81 
82  if(argc < 2) {
83  cerr << argv[0]
84  << " -K<blob_id> [-d<driver_name>] [-S<server_name>]"
85  << " [-U<user_name>] [-P<password>] [-L<image size>] [-Z<compress_method>]"
86  << " [-T<table_name>] [-H<table_hint>] [-a]"
87  << endl;
88  return 0;
89  }
90 
91  const char* p= getParam('K', argc, argv);
92  if(p == 0) {
93  cerr << argv[0] << "Blob ID is missing" << endl;
94  return 1;
95  }
96  blob_key= p;
97 
98  p= getParam('S', argc, argv);
99  if(p == 0) {
100  p= getenv("SQL_SERVER");
101  }
102  server_name= p? p : "MSDEV1";
103 
104  p= getParam('d', argc, argv);
105  if (p) {
106  driver_name= p;
107  } else {
108  p= getenv("DBAPI_DRIVER");
109  if(p == 0) {
110  driver_name= (server_name.find("MS") != NPOS)? "ftds" : "ctlib";
111  }
112  else driver_name= p;
113  }
114 
115  p= getParam('U', argc, argv);
116  if(p == 0) {
117  p= getenv("SQL_USER");
118  }
119  user_name= p? p : "anyone";
120 
121  p= getParam('P', argc, argv);
122  if(p == 0) {
123  p= getenv("SQL_PASSWD");
124  }
125  passwd= p? p : "allowed";
126 
128  p= getParam('Z', argc, argv);
129  if(p) {
130  if(*p == 'z') cm= eZLib;
131  else cm= eBZLib;
132  }
133 
134  p= getParam('L', argc, argv);
135  if(p) imagesize= atoi(p);
136 
137  p= getParam('T', argc, argv);
138  if(p == 0) {
139  p= getenv("DATA_TABLE");
140  }
141  table_name= p? p : "MyDataTable";
142 
143  p= getParam('H', argc, argv);
144  if(p) table_hint= p;
145 
146  getParam('a', argc, argv, &preallocated);
147  if (preallocated) {
149  }
150 
151  string *blob_column = NULL;
152 
153  try {
154  C_DriverMgr drv_mgr;
155  string err_msg;
156  map<string, string> packet;
157  packet.insert (map<string, string>::value_type (string("packet"),
158  string("2048")));
159  unique_ptr<I_DriverContext> my_context(drv_mgr.GetDriverContext(driver_name,
160  &err_msg, &packet));
161  if(!my_context) {
162  cerr << "Cannot load a driver " << driver_name << " ["
163  << err_msg << "] " << endl;
164  return 1;
165  }
166 
167  CDB_Connection* con= my_context->Connect(server_name, user_name, passwd, 0, true);
168 
169  string query= "select * from " + table_name + " where 0=1";
170  CDB_LangCmd* lcmd = con->LangCmd(query);
171 
172  lcmd->Send();
173 
174  unsigned int n;
175  int k= 0;
176 
177  while (lcmd->HasMoreResults()) {
178  CDB_Result* r = lcmd->Result();
179  if (!r)
180  continue;
181 
182  if (r->ResultType() == eDB_RowResult) {
183  n= r->NofItems();
184  if(n < 2) {
185  delete r;
186  continue;
187  }
188 
189  blob_column= new string[n];
190 
191  for(unsigned int j= 0; j < n; j++) {
192  switch (r->ItemDataType(j)) {
193  case eDB_VarChar:
194  case eDB_Char:
195  case eDB_LongChar:
196  key_col_name= r->ItemName(j);
197  break;
198 
199  case eDB_Int:
200  case eDB_SmallInt:
201  case eDB_TinyInt:
202  case eDB_BigInt:
203  num_col_name= r->ItemName(j);
204  break;
205 
206  case eDB_Text:
207  case eDB_VarCharMax:
209  // fall through
210  case eDB_Image:
211  case eDB_VarBinaryMax:
212  blob_column[k++]= r->ItemName(j);
213  break;
214  default:
215  break;
216  }
217  }
218  blob_column[k]= kEmptyStr;
219  while(r->Fetch());
220  }
221  delete r;
222  }
223  delete lcmd;
224  delete con;
225 
226  CSimpleBlobStore sbs(table_name, key_col_name, num_col_name,
227  blob_column, flags, table_hint);
228 
229  CBlobLoader bload(my_context.get(), server_name, user_name, passwd, &sbs);
230 
231  sbs.SetKey(blob_key);
232  bload.Load(cin, cm, imagesize);
233  } catch (CDB_Exception& e) {
234  CDB_UserHandler_Stream myExHandler(&cerr);
235 
236  myExHandler.HandleIt(&e);
237  return 1;
238  }
239  return 0;
240 }
241 
242 
@ eNone
None specified.
Definition: blast_def.h:326
ECompressMethod
Definition: blobstore.hpp:40
@ eZLib
Definition: blobstore.hpp:42
@ eBZLib
Definition: blobstore.hpp:43
int main(int argc, char *argv[])
Definition: blobwriter.cpp:65
static char * getParam(char tag, int argc, char *argv[], bool *flag=0)
Definition: blobwriter.cpp:37
USING_NCBI_SCOPE
Definition: blobwriter.cpp:35
bool Load(istream &s, ECompressMethod cm=eNone, size_t image_limit=0, bool log_it=false)
Definition: blobstore.cpp:281
CDB_Exception –.
Definition: exception.hpp:118
static const TFlags kDefaults
Definition: blobstore.hpp:206
void SetKey(const string &key)
Definition: blobstore.hpp:221
@ fIsText
(N)TEXT or (N)VARCHAR(MAX)
Definition: blobstore.hpp:201
@ 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
#define NULL
Definition: ncbistd.hpp:225
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_)
static string query
Modified on Tue May 21 10:53:35 2024 by modify_doxy.py rev. 669887