FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
sqlCopyTest.c
Go to the documentation of this file.
1 /**************************************************************
2 Copyright (C) 2011 Hewlett-Packard Development Company, L.P.
3 
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License version 2.1 as published by the Free Software Foundation.
7 
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Lesser General Public License for more details.
12 
13 You should have received a copy of the GNU Lesser General Public License
14 along with this library; if not, write to the Free Software Foundation, Inc.0
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 **************************************************************/
17 
23 #include "libfossology.h"
24 
28 char* GetTextCol(int NumTextBytes)
29 {
30  char* col_text;
31  int i;
32 
33  col_text = calloc(NumTextBytes + 1, sizeof(char));
34  if (!col_text)
35  {
36  ERROR_RETURN("Allocating test text data failed.")
37  exit(-2);
38  }
39  for (i = 0; i < NumTextBytes; i++) col_text[i] = 'a';
40  return (col_text);
41 }
42 
43 /**************** main *******************/
44 int main(int argc, char** argv)
45 {
46  PGconn* pgConn;
47  PGresult* result;
48  psqlCopy_t pCopy;
49  char* TestTable = "TestsqlCopy";
50  char col_vc[40] = "This is \n\r column vc[40] 123456789\r";
51  char* col_text;
52  char* DataBuf;
53  int datasize;
54  char sql[2048];
55  int NumColumns = 3;
56  int CopyBufSize;
57  int RowsToTest;
58  int NumTextBytes;
59  int RowNum;
60  clock_t StartTime, EndTime;
61  char* DBConfFile = NULL; /* use default Db.conf */
62  char* ErrorBuf;
63 
64  if (argc != 4)
65  {
66  printf("Usage: %s RowsToTest NumTextBytes CopyDataBufferSize\n", argv[0]);
67  exit(-1);
68  }
69 
70  /* first argument is the number of rows to test,
71  * the second is the number of bytes to use for col_text
72  * third is the Copy data buffer size
73  */
74  RowsToTest = atoi(argv[1]);
75  NumTextBytes = atoi(argv[2]);
76  CopyBufSize = atoi(argv[3]);
77 
78  /* Populate test data */
79  col_text = GetTextCol(NumTextBytes);
80  datasize = NumTextBytes + 8 + 40 + 1;
81  DataBuf = calloc(datasize, sizeof(char));
82  if (!DataBuf)
83  {
84  free(col_text);
85  ERROR_RETURN("Allocating test data buffer failed.")
86  exit(-2);
87  }
88 
89  pgConn = fo_dbconnect(DBConfFile, &ErrorBuf);
90 
91  /* Create a test table to populate */
92  snprintf(sql, sizeof(sql), "create table %s (col_int integer, col_text text, col_vc varchar(40))", TestTable);
93  result = PQexec(pgConn, sql);
94  fo_checkPQcommand(pgConn, result, sql, __FILE__, __LINE__);
95 
96  /* Start timer */
97  StartTime = clock();
98 
99  /* Create the pCopy */
100  pCopy = fo_sqlCopyCreate(pgConn, TestTable, CopyBufSize, NumColumns,
101  "col_int", "col_text", "col_vc");
102  if (!pCopy) exit(1); /* CopyCreate prints errors to stdout */
103 
104  /* Add data */
105  for (RowNum = 0; RowNum < RowsToTest; RowNum++)
106  {
107  snprintf(DataBuf, datasize, "%d\t%s\t%s\n", RowNum, col_text, col_vc);
108  fo_sqlCopyAdd(pCopy, DataBuf);
109  }
110  free(col_text);
111 
112  /* Destroy - flushes remaining data and frees */
113  fo_sqlCopyDestroy(pCopy, 1);
114 
115  /* Print run time for the load (whole Create/Add/Destroy cycle). */
116  EndTime = clock();
117  printf("%.6f Seconds to load.\n", ((double) (EndTime - StartTime)) / CLOCKS_PER_SEC);
118 
119  /* Verify that the right number of records were loaded */
120  snprintf(sql, sizeof(sql), "select count(*) from %s", TestTable);
121  result = PQexec(pgConn, sql);
122  fo_checkPQresult(pgConn, result, sql, __FILE__, __LINE__);
123  printf("%d records inserted, %d expected\n",
124  atoi(PQgetvalue(result, 0, 0)),
125  RowsToTest);
126  PQclear(result);
127 
128  /* Remove the test table */
129 /*
130  snprintf(sql, sizeof(sql), "drop table %s", TestTable);
131  result = PQexec(pgConn, sql);
132  fo_checkPQcommand(pgConn, result, sql, __FILE__, __LINE__);
133 */
134 
135  PQfinish(pgConn);
136  return (0);
137 }
char * DBConfFile
DB conf file location.
Definition: testRun.c:33
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
Definition: libfossdb.c:181
char * GetTextCol(int NumTextBytes)
Definition: sqlCopyTest.c:28
PGconn * pgConn
Database connection.
Definition: adj2nest.c:98
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:215
The main FOSSology C library.
psqlCopy_t fo_sqlCopyCreate(PGconn *pGconn, char *TableName, int BufSize, int NumColumns,...)
Constructor for sqlCopy_struct.
Definition: sqlCopy.c:62
void fo_sqlCopyDestroy(psqlCopy_t pCopy, int ExecuteFlag)
Destructor for sqlCopy_struct.
Definition: sqlCopy.c:304
PGconn * fo_dbconnect(char *DBConfFile, char **ErrorBuf)
Connect to a database. The default is Db.conf.
Definition: libfossdb.c:40
int fo_sqlCopyAdd(psqlCopy_t pCopy, char *DataRow)
Add a data row to an sqlCopy Use &#39;&#39; to pass in a null.
Definition: sqlCopy.c:157