NCBI C++ ToolKit
bcp.c
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /*
2  * Purpose: Test bcp functions
3  * Functions: bcp_batch bcp_bind bcp_done bcp_init bcp_sendrow
4  */
5 
6 #include "common.h"
7 
8 #if HAVE_SYS_STAT_H
9 #include <sys/stat.h>
10 #endif /* HAVE_SYS_STAT_H */
11 
12 #include "bcp.h"
13 
14 #include <common/test_assert.h>
15 
16 static char cmd[512];
17 static int init(DBPROCESS * dbproc, const char *name);
18 static void test_bind(DBPROCESS * dbproc);
19 
20 /*
21  * Static data for insertion
22  */
23 static int not_null_bit = 1;
24 static char not_null_char[] = "a char";
25 static char not_null_varchar[] = "a varchar";
26 static char not_null_datetime[] = "Dec 17 2003 3:44PM";
27 static char not_null_smalldatetime[] = "Dec 17 2003 3:44PM";
28 static char not_null_money[] = "12.34";
29 static char not_null_smallmoney[] = "12.34";
30 static char not_null_float[] = "12.34";
31 static char not_null_real[] = "12.34";
32 static char not_null_decimal[] = "12.34";
33 static char not_null_numeric[] = "12.34";
34 static int not_null_int = 1234;
35 static int not_null_smallint = 1234;
36 static int not_null_tinyint = 123;
37 
38 
39 static int
40 init(DBPROCESS * dbproc, const char *name)
41 {
42  int res = 0;
43  RETCODE rc;
44 
45  fprintf(stdout, "Dropping %s.%s..%s\n", SERVER, DATABASE, name);
46  sql_cmd(dbproc);
48  while ((rc=dbresults(dbproc)) == SUCCEED) {
49  /* nop */
50  }
51  if (rc != NO_MORE_RESULTS)
52  return 1;
53 
54  fprintf(stdout, "Creating %s.%s..%s\n", SERVER, DATABASE, name);
55  sql_cmd(dbproc);
56  sql_cmd(dbproc);
57 
58  if (dbsqlexec(dbproc) == FAIL) {
59  res = 1;
60  }
61  while ((rc=dbresults(dbproc)) == SUCCEED) {
63  dbprrow(dbproc);
64  while ((rc=dbnextrow(dbproc)) == REG_ROW) {
65  dbprrow(dbproc);
66  }
67  }
68  if (rc != NO_MORE_RESULTS)
69  return 1;
70  fprintf(stdout, "%s\n", res? "error" : "ok");
71  return res;
72 }
73 
74 #define VARCHAR_BIND(x) \
75  bcp_bind( dbproc, (unsigned char *) &x, prefixlen, (DBINT) strlen(x), \
76  NULL, termlen, SYBVARCHAR, col++ )
77 
78 #define INT_BIND(x) \
79  bcp_bind( dbproc, (unsigned char *) &x, prefixlen, -1, NULL, termlen, SYBINT4, col++ )
80 
81 #define NULL_BIND(x, type) \
82  bcp_bind( dbproc, (unsigned char *) &x, prefixlen, 0, NULL, termlen, type, col++ )
83 
84 static void
86 {
87  enum { prefixlen = 0 };
88  enum { termlen = 0 };
89  enum NullValue { IsNull, IsNotNull };
90 
91  RETCODE fOK;
92  int col=1;
93 
94  /* non nulls */
95  fOK = INT_BIND(not_null_bit);
96  assert(fOK == SUCCEED);
97 
99  assert(fOK == SUCCEED);
101  assert(fOK == SUCCEED);
102 
104  assert(fOK == SUCCEED);
106  assert(fOK == SUCCEED);
107 
109  assert(fOK == SUCCEED);
111  assert(fOK == SUCCEED);
112 
114  assert(fOK == SUCCEED);
116  assert(fOK == SUCCEED);
117 
119  assert(fOK == SUCCEED);
121  assert(fOK == SUCCEED);
122 
123  fOK = INT_BIND(not_null_int);
124  assert(fOK == SUCCEED);
126  assert(fOK == SUCCEED);
127  fOK = INT_BIND(not_null_tinyint);
128  assert(fOK == SUCCEED);
129 
130  /* nulls */
132  assert(fOK == SUCCEED);
134  assert(fOK == SUCCEED);
135 
137  assert(fOK == SUCCEED);
139  assert(fOK == SUCCEED);
140 
142  assert(fOK == SUCCEED);
144  assert(fOK == SUCCEED);
145 
147  assert(fOK == SUCCEED);
149  assert(fOK == SUCCEED);
150 
152  assert(fOK == SUCCEED);
154  assert(fOK == SUCCEED);
155 
157  assert(fOK == SUCCEED);
159  assert(fOK == SUCCEED);
161  assert(fOK == SUCCEED);
162 
163 }
164 
165 int
166 main(int argc, char **argv)
167 {
168  LOGINREC *login;
169  DBPROCESS *dbproc;
170  int i, rows_sent=0;
171  int failed = 0;
172  const char *s;
173  const char *table_name = "all_types_bcp_unittest";
174 
176 
177  read_login_info(argc, argv);
178 
179  fprintf(stdout, "Starting %s\n", argv[0]);
180 
181  dbinit();
182 
185 
186  fprintf(stdout, "About to logon\n");
187 
188  login = dblogin();
191  DBSETLAPP(login, "bcp.c unit test");
192  BCP_SETL(login, 1);
193 
194  fprintf(stdout, "About to open %s.%s\n", SERVER, DATABASE);
195 
197  if (strlen(DATABASE))
200 
201  if (init(dbproc, table_name))
202  exit(1);
203 
204  /* set up and send the bcp */
205  sprintf(cmd, "%s..%s", DATABASE, table_name);
206  fprintf(stdout, "preparing to insert into %s ... ", cmd);
207  if (bcp_init(dbproc, cmd, NULL, NULL, DB_IN) == FAIL) {
208  fprintf(stdout, "failed\n");
209  exit(1);
210  }
211  fprintf(stdout, "OK\n");
212 
213  test_bind(dbproc);
214 
215  fprintf(stdout, "Sending same row 10 times... \n");
216  for (i=0; i<10; i++) {
217  if (bcp_sendrow(dbproc) == FAIL) {
218  fprintf(stdout, "send failed\n");
219  exit(1);
220  }
221  }
222 
223  fprintf(stdout, "Sending 5 more rows ... \n");
224  for (i=15; i <= 27; i++) {
225  int type = dbcoltype(dbproc, i);
226  int len = (type == SYBCHAR || type == SYBVARCHAR)? dbcollen(dbproc, i) : -1;
227  if (bcp_collen(dbproc, len, i) == FAIL) {
228  fprintf(stdout, "bcp_collen failed for column %d\n", i);
229  exit(1);
230  }
231  }
232  for (i=0; i<5; i++) {
233  if (bcp_sendrow(dbproc) == FAIL) {
234  fprintf(stdout, "send failed\n");
235  exit(1);
236  }
237  }
238 #if 1
239  rows_sent = bcp_batch(dbproc);
240  if (rows_sent == -1) {
241  fprintf(stdout, "batch failed\n");
242  exit(1);
243  }
244 #endif
245 
246  fprintf(stdout, "OK\n");
247 
248  /* end bcp. */
249  if ((rows_sent += bcp_done(dbproc)) == -1)
250  printf("Bulk copy unsuccessful.\n");
251  else
252  printf("%d rows copied.\n", rows_sent);
253 
254 
255  printf("done\n");
256 
257 
258 #if 1
259  sql_cmd(dbproc);
260 
261  dbsqlexec(dbproc);
262  while ((i=dbresults(dbproc)) == SUCCEED) {
263  dbprhead(dbproc);
264  dbprrow(dbproc);
265  while ((i=dbnextrow(dbproc)) == REG_ROW) {
266  dbprrow(dbproc);
267  }
268  }
269 #endif
270  if ((s = getenv("BCP")) != NULL && 0 == strcmp(s, "nodrop")) {
271  fprintf(stdout, "BCP=nodrop: '%s..%s' kept\n", DATABASE, table_name);
272  } else {
273  fprintf(stdout, "Dropping table %s\n", table_name);
274  sql_cmd(dbproc);
275  dbsqlexec(dbproc);
276  while (dbresults(dbproc) != NO_MORE_RESULTS) {
277  /* nop */
278  }
279  }
280  dbexit();
281 
282  failed = 0;
283 
284  fprintf(stdout, "%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
285  return failed ? 1 : 0;
286 }
char PASSWORD[512]
Definition: common.c:31
char DATABASE[512]
Definition: common.c:32
CS_RETCODE read_login_info(void)
Definition: common.c:71
char SERVER[512]
Definition: common.c:30
static TDSLOGIN * login
Definition: dataread.c:31
static char not_null_float[]
Definition: bcp.c:30
static char not_null_char[]
Definition: bcp.c:24
#define NULL_BIND(x, type)
Definition: bcp.c:81
static char not_null_smalldatetime[]
Definition: bcp.c:27
static char not_null_varchar[]
Definition: bcp.c:25
int main(int argc, char **argv)
Definition: bcp.c:166
static char not_null_numeric[]
Definition: bcp.c:33
#define VARCHAR_BIND(x)
Definition: bcp.c:74
static char not_null_real[]
Definition: bcp.c:31
static void test_bind(DBPROCESS *dbproc)
Definition: bcp.c:85
static char not_null_smallmoney[]
Definition: bcp.c:29
static char cmd[512]
Definition: bcp.c:16
static int init(DBPROCESS *dbproc, const char *name)
Definition: bcp.c:40
static char not_null_decimal[]
Definition: bcp.c:32
#define INT_BIND(x)
Definition: bcp.c:78
static char not_null_datetime[]
Definition: bcp.c:26
static int not_null_smallint
Definition: bcp.c:35
static int not_null_bit
Definition: bcp.c:23
static int not_null_tinyint
Definition: bcp.c:36
static char not_null_money[]
Definition: bcp.c:28
static int not_null_int
Definition: bcp.c:34
int syb_err_handler(DBPROCESS *dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr)
Definition: common.c:404
RETCODE sql_cmd(DBPROCESS *dbproc)
Definition: common.c:280
int syb_msg_handler(DBPROCESS *dbproc, DBINT msgno, int msgstate, int severity, char *msgtext, char *srvname, char *procname, int line)
Definition: common.c:334
void set_malloc_options(void)
Definition: common.c:47
STATUS dbnextrow(DBPROCESS *dbproc)
Definition: dblib.c:2076
int failed
Definition: dbmorecmds.c:10
#define USER
Definition: fastme_common.h:43
#define NULL
Definition: ncbistd.hpp:225
#define BCP_SETL(x, y)
Enable (or prevent) bcp operations for connections made with a login.
Definition: sybdb.h:1269
#define DBSETLPWD(x, y)
Set the password in the login packet.
Definition: sybdb.h:1259
#define DBSETLAPP(x, y)
Set the (client) application name in the login packet.
Definition: sybdb.h:1266
#define DBSETLUSER(x, y)
Set the username in the login packet.
Definition: sybdb.h:1256
DBINT bcp_done(DBPROCESS *dbproc)
Conclude the transfer of data from program variables.
Definition: bcp.c:1990
DBINT bcp_batch(DBPROCESS *dbproc)
Commit a set of rows to the table.
Definition: bcp.c:1963
RETCODE bcp_init(DBPROCESS *dbproc, const char *tblname, const char *hfile, const char *errfile, int direction)
Prepare for bulk copy operation on a table.
Definition: bcp.c:164
RETCODE bcp_collen(DBPROCESS *dbproc, DBINT varlen, int table_column)
Set the length of a host variable to be written to a table.
Definition: bcp.c:261
RETCODE bcp_sendrow(DBPROCESS *dbproc)
Write data in host variables to the table.
Definition: bcp.c:1339
LOGINREC * dblogin(void)
Allocate a LOGINREC structure.
Definition: dblib.c:719
RETCODE dbresults(DBPROCESS *dbproc)
Set up query results.
Definition: dblib.c:1706
RETCODE dbinit(void)
Initialize db-lib.
Definition: dblib.c:674
int dbcoltype(DBPROCESS *dbproc, int column)
Get the datatype of a regular result set column.
Definition: dblib.c:2902
RETCODE dbuse(DBPROCESS *dbproc, const char *name)
Change current database.
Definition: dblib.c:1449
DBINT dbcollen(DBPROCESS *dbproc, int column)
Get size of a regular result column.
Definition: dblib.c:3109
void dbprhead(DBPROCESS *dbproc)
Print result set headings to stdout.
Definition: dblib.c:3854
RETCODE dbprrow(DBPROCESS *dbproc)
Print a result set to stdout.
Definition: dblib.c:3425
RETCODE dbsqlexec(DBPROCESS *dbproc)
send the SQL command to the server and wait for an answer.
Definition: dblib.c:1423
EHANDLEFUNC dberrhandle(EHANDLEFUNC handler)
Set an error handler, for messages from db-lib.
Definition: dblib.c:5030
void dbloginfree(LOGINREC *login)
free the LOGINREC
Definition: dblib.c:751
void dbexit()
Close server connections and free all related structures.
Definition: dblib.c:1559
MHANDLEFUNC dbmsghandle(MHANDLEFUNC handler)
Set a message handler, for messages from the server.
Definition: dblib.c:5049
exit(2)
int i
int len
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
static const char table_name[]
Definition: bcp.c:249
CBulkInsert & NullValue(CBulkInsert &bi)
Manipulator putting null value into the bulk-insert row.
Definition: sdbapi.cpp:2498
#define assert(x)
Definition: srv_diag.hpp:58
Definition: type.c:6
#define dbopen(x, y)
Definition: sybdb.h:855
#define DB_IN
Definition: sybdb.h:588
#define SYBINT4
Definition: sybdb.h:170
#define REG_ROW
Definition: sybdb.h:580
#define SYBVARCHAR
Definition: sybdb.h:162
int RETCODE
Definition: sybdb.h:121
#define SUCCEED
Definition: sybdb.h:585
#define SYBCHAR
Definition: sybdb.h:160
#define FAIL
Definition: sybdb.h:586
#define NO_MORE_RESULTS
Definition: sybdb.h:584
DBPROCESS * dbproc
Definition: t0013.c:18
Modified on Mon Mar 04 05:14:56 2024 by modify_doxy.py rev. 669887