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

Go to the SVN repository for this file.

1 /* $Id: dbapi_bcp.cpp 77792 2017-05-08 13:38:35Z ivanov $
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: Elena Starchenko
27 * $Id: dbapi_bcp.cpp 77792 2017-05-08 13:38:35Z ivanov $
28 *
29 * File Description:
30 * Test the implementation of BCP by DBAPI driver(s):
31 * 1) CREATE a table with 5 rows
32 * 2) make BCP of this table
33 * 3) PRINT the table on screen (use <ROW> and </ROW> to separate the rows)
34 * 4) DELETE the table from the database
35 *
36 * File Description: Implementation of dbapi BCP
37 *
38 */
39 
40 #include <ncbi_pch.hpp>
41 #include <corelib/ncbiapp.hpp>
42 #include <corelib/ncbiargs.hpp>
43 #include <corelib/ncbienv.hpp>
45 
46 #include "dbapi_bcp.hpp"
47 #include "../dbapi_sample_base.hpp"
48 #include <common/test_assert.h> /* This header must go last */
49 
50 
52 
53 
54 /////////////////////////////////////////////////////////////////////////////
55 // CDbapiBcpApp::
56 //
57 
59 {
60 public:
61  CDbapiBcpApp(void);
62  virtual ~CDbapiBcpApp(void);
63 
64 protected:
65  virtual int RunSample(void);
66  string GetTableName(void) const;
67 };
68 
70 {
71 }
72 
74 {
75 }
76 
77 inline
78 string
80 {
81  return "#bcp_test";
82 }
83 
84 int
86 {
87  int count=0;
88 
89  try {
90  unique_ptr<CDB_LangCmd> set_cmd;
91 
92  // Create table in database for the test
93 
95 
96  //Set textsize to work with text and image
97  set_cmd.reset(GetConnection().LangCmd("set textsize 1000000"));
98  set_cmd->Send();
99 
100  while(set_cmd->HasMoreResults()) {
101  unique_ptr<CDB_Result> r(set_cmd->Result());
102  }
103 
104  //"Bulk copy in" command
105  unique_ptr<CDB_BCPInCmd> bcp(GetConnection().BCPIn(GetTableName()));
106 
107  CDB_Int int_val;
108  CDB_Float fl_val;
109  CDB_DateTime date_val(CTime::eCurrent);
110  CDB_VarChar str_val;
111  CDB_Text pTxt;
112  int i;
113  pTxt.Append("This is a test string.");
114 
115  // Bind data from a program variables
116  bcp->Bind(0, &int_val);
117  bcp->Bind(1, &fl_val);
118  bcp->Bind(2, &date_val);
119  bcp->Bind(3, &str_val);
120  bcp->Bind(4, &pTxt);
121 
122  for ( i= 0; *file_name[i] != '\0'; ++i ) {
123  int_val = i;
124  fl_val = float(i + 0.999);
125  date_val = date_val.Value();
126  str_val= file_name[i];
127 
128  pTxt.MoveTo(0);
129  //Send row of data to the server
130  bcp->SendRow();
131 
132  //Save any preceding rows in server
133  if ( count == 2 ) {
134  bcp->CompleteBatch();
135  count = 0;
136  }
137 
138  count++;
139  }
140 
141  //End a bulk copy
142  bcp->CompleteBCP();
143 
144  // Print results on screen
145  ShowResults("select int_val,fl_val,date_val,str_val,txt_val from " + GetTableName());
146 
147  //Delete table from database
149 
150  // Drop lost tables.
152  }
153  catch ( CDB_Exception& e ) {
155  return 1;
156  }
157 
158  return 0;
159 }
160 
161 int main(int argc, const char* argv[])
162 {
163  return CDbapiBcpApp().AppMain(argc, argv);
164 }
165 
166 
void CreateTable(const string &table_name)
function CreateTable is creating table in the database
int main(int argc, const char *argv[])
Definition: dbapi_bcp.cpp:161
void ShowResults(const string &query)
Delete table if it exists ShowResults is printing resuts on screen.
Defines command line argument related classes.
virtual int RunSample(void)
Definition: dbapi_bcp.cpp:85
Defines unified interface to application:
static CDB_UserHandler & GetDefault(void)
Definition: exception.cpp:619
int AppMain(int argc, const char *const *argv, const char *const *envp=0, EAppDiagStream diag=eDS_Default, const char *conf=NcbiEmptyCStr, const string &name=NcbiEmptyString)
Main function (entry point) for the NCBI application.
Definition: ncbiapp.cpp:626
void DeleteLostTables(void)
Delete tables which are lost after previous tests.
int i
virtual bool HandleIt(CDB_Exception *ex)=0
Handle the exceptions resulting from a native API call, one-by-one.
virtual ~CDbapiBcpApp(void)
Definition: dbapi_bcp.cpp:73
virtual bool MoveTo(size_t byte_number)
Definition: types.cpp:2006
const CTime & Value(void) const
Definition: types.cpp:2486
USING_NCBI_SCOPE
Definition: dbapi_bcp.cpp:51
void DeleteTable(const string &table_name)
CDB_Connection & GetConnection(void)
Get connection created using server, username and password specified in the application command line...
CDB_Exception –.
Definition: exception.hpp:116
Defines the CNcbiApplication and CAppException classes for creating NCBI applications.
const char * file_name[]
virtual size_t Append(const void *buff, size_t nof_bytes)
Definition: types.cpp:2197
Use current time.
Definition: ncbitime.hpp:295
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
string GetTableName(void) const
Definition: dbapi_bcp.cpp:79
CDbapiBcpApp(void)
Definition: dbapi_bcp.cpp:69
Modified on Thu Dec 14 18:14:37 2017 by modify_doxy.py rev. 546573