FOSSology  4.4.0
Open Source License Compliance by Open Source Software
sqlCopyTest.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2011 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: LGPL-2.1-only
5 */
6 
12 #include "libfossology.h"
13 
17 char* GetTextCol(int NumTextBytes)
18 {
19  char* col_text;
20  int i;
21 
22  col_text = calloc(NumTextBytes + 1, sizeof(char));
23  if (!col_text)
24  {
25  ERROR_RETURN("Allocating test text data failed.")
26  exit(-2);
27  }
28  for (i = 0; i < NumTextBytes; i++) col_text[i] = 'a';
29  return (col_text);
30 }
31 
32 /**************** main *******************/
33 int main(int argc, char** argv)
34 {
35  PGconn* pgConn;
36  PGresult* result;
37  psqlCopy_t pCopy;
38  char* TestTable = "TestsqlCopy";
39  char col_vc[40] = "This is \n\r column vc[40] 123456789\r";
40  char* col_text;
41  char* DataBuf;
42  int datasize;
43  char sql[2048];
44  int NumColumns = 3;
45  int CopyBufSize;
46  int RowsToTest;
47  int NumTextBytes;
48  int RowNum;
49  clock_t StartTime, EndTime;
50  char* DBConfFile = NULL; /* use default Db.conf */
51  char* ErrorBuf;
52 
53  if (argc != 4)
54  {
55  printf("Usage: %s RowsToTest NumTextBytes CopyDataBufferSize\n", argv[0]);
56  exit(-1);
57  }
58 
59  /* first argument is the number of rows to test,
60  * the second is the number of bytes to use for col_text
61  * third is the Copy data buffer size
62  */
63  RowsToTest = atoi(argv[1]);
64  NumTextBytes = atoi(argv[2]);
65  CopyBufSize = atoi(argv[3]);
66 
67  /* Populate test data */
68  col_text = GetTextCol(NumTextBytes);
69  datasize = NumTextBytes + 8 + 40 + 1;
70  DataBuf = calloc(datasize, sizeof(char));
71  if (!DataBuf)
72  {
73  free(col_text);
74  ERROR_RETURN("Allocating test data buffer failed.")
75  exit(-2);
76  }
77 
78  pgConn = fo_dbconnect(DBConfFile, &ErrorBuf);
79 
80  /* Create a test table to populate */
81  snprintf(sql, sizeof(sql), "create table %s (col_int integer, col_text text, col_vc varchar(40))", TestTable);
82  result = PQexec(pgConn, sql);
83  fo_checkPQcommand(pgConn, result, sql, __FILE__, __LINE__);
84 
85  /* Start timer */
86  StartTime = clock();
87 
88  /* Create the pCopy */
89  pCopy = fo_sqlCopyCreate(pgConn, TestTable, CopyBufSize, NumColumns,
90  "col_int", "col_text", "col_vc");
91  if (!pCopy) exit(1); /* CopyCreate prints errors to stdout */
92 
93  /* Add data */
94  for (RowNum = 0; RowNum < RowsToTest; RowNum++)
95  {
96  snprintf(DataBuf, datasize, "%d\t%s\t%s\n", RowNum, col_text, col_vc);
97  fo_sqlCopyAdd(pCopy, DataBuf);
98  }
99  free(col_text);
100 
101  /* Destroy - flushes remaining data and frees */
102  fo_sqlCopyDestroy(pCopy, 1);
103 
104  /* Print run time for the load (whole Create/Add/Destroy cycle). */
105  EndTime = clock();
106  printf("%.6f Seconds to load.\n", ((double) (EndTime - StartTime)) / CLOCKS_PER_SEC);
107 
108  /* Verify that the right number of records were loaded */
109  snprintf(sql, sizeof(sql), "select count(*) from %s", TestTable);
110  result = PQexec(pgConn, sql);
111  fo_checkPQresult(pgConn, result, sql, __FILE__, __LINE__);
112  printf("%d records inserted, %d expected\n",
113  atoi(PQgetvalue(result, 0, 0)),
114  RowsToTest);
115  PQclear(result);
116 
117  /* Remove the test table */
118 /*
119  snprintf(sql, sizeof(sql), "drop table %s", TestTable);
120  result = PQexec(pgConn, sql);
121  fo_checkPQcommand(pgConn, result, sql, __FILE__, __LINE__);
122 */
123 
124  PQfinish(pgConn);
125  return (0);
126 }
PGconn * pgConn
Database connection.
Definition: adj2nest.c:86
int s
The socket that the CLI will use to communicate.
Definition: fo_cli.c:37
PGconn * fo_dbconnect(char *DBConfFile, char **ErrorBuf)
Connect to a database. The default is Db.conf.
Definition: libfossdb.c:29
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
Definition: libfossdb.c:170
int fo_checkPQcommand(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres commands (not select) If an error occured, write the error to s...
Definition: libfossdb.c:204
The main FOSSology C library.
if(!preg_match("/\s$projectGroup\s/", $groups) &&(posix_getgid() !=$gInfo[ 'gid']))
get monk license list of one specified uploadtree_id
Definition: migratetest.php:33
char * GetTextCol(int NumTextBytes)
Definition: sqlCopyTest.c:17
int fo_sqlCopyAdd(psqlCopy_t pCopy, char *DataRow)
Add a data row to an sqlCopy Use '\N' to pass in a null.
Definition: sqlCopy.c:146
void fo_sqlCopyDestroy(psqlCopy_t pCopy, int ExecuteFlag)
Destructor for sqlCopy_struct.
Definition: sqlCopy.c:293
psqlCopy_t fo_sqlCopyCreate(PGconn *pGconn, char *TableName, int BufSize, int NumColumns,...)
Constructor for sqlCopy_struct.
Definition: sqlCopy.c:51
char * DBConfFile
DB conf file location.
Definition: testRun.c:21