12 #define ERRBUFSIZE 11264
14 #include "libfossdb.h"
44 Fconf = fopen(FOSSDB_CONF,
"r");
47 *ErrorBuf = malloc(ERRBUFSIZE);
50 snprintf(*ErrorBuf, ERRBUFSIZE,
"Database conf file: %s, ",
52 BufLen = strlen(*ErrorBuf);
53 strerror_r(errno, *ErrorBuf + BufLen, ERRBUFSIZE - BufLen);
59 memset(
CMD,
'\0',
sizeof(
CMD));
66 memset(Line,
'\0',
sizeof(Line));
70 for (i = 0; (i <
sizeof(Line)) && (C !=
'\n') && (C > 0); i++)
73 if ((C > 0) && (C !=
'\n')) Line[i] = C;
74 if ((C ==
'=') && !PosEqual) PosEqual = i;
75 else if ((C ==
';') && !PosSemi) PosSemi = i;
78 if (PosSemi < PosEqual) PosEqual = 0;
79 if ((Line[0] !=
'#') && PosEqual && PosSemi)
85 if (CMDlen >=
sizeof(
CMD))
92 for (i = 0; i < PosEqual; i++)
94 if (!isspace(Line[i]))
CMD[CMDlen++] = Line[i];
95 if (CMDlen >=
sizeof(
CMD))
102 if (CMDlen >=
sizeof(
CMD))
107 for (i = PosEqual + 1; Line[i] !=
'\0'; i++)
109 if (!isspace(Line[i]))
CMD[CMDlen++] = Line[i];
110 if (CMDlen >=
sizeof(
CMD))
121 if (
CMD[0] ==
'\0')
goto BadConf;
125 if (PQstatus(
pgConn) != CONNECTION_OK)
127 *ErrorBuf = malloc(ERRBUFSIZE);
131 const char pass[10]=
"password=";
132 for(i = strstr(
CMD,pass) -
CMD + strlen(pass); i < strlen(
CMD); i++){
138 snprintf(*ErrorBuf, ERRBUFSIZE,
139 "ERROR: Unable to connect to the database\n Connection string: '%s'\n Connection status: '%d'\n Check: /usr/local/etc/fossology/Db.conf\n",
CMD, PQstatus(
pgConn));
147 *ErrorBuf = malloc(ERRBUFSIZE);
148 snprintf(*ErrorBuf, ERRBUFSIZE,
"Invalid Database conf file: %s, ",
174 printf(
"FATAL: %s:%d, %s\nOn: %s\n",
175 FileID, LineNumb, PQerrorMessage(
pgConn), sql);
180 if (PQresultStatus(result) == PGRES_TUPLES_OK)
return 0;
182 printf(
"ERROR: %s:%d, %s\nOn: %s\n",
183 FileID, LineNumb, PQresultErrorMessage(result), sql);
208 printf(
"FATAL: %s:%d, %sOn: %s\n",
209 FileID, LineNumb, PQerrorMessage(
pgConn), sql);
214 if (PQresultStatus(result) == PGRES_COMMAND_OK)
return 0;
216 printf(
"ERROR: %s:%d, %sOn: %s\n",
217 FileID, LineNumb, PQresultErrorMessage(result), sql);
238 snprintf(sql,
sizeof(sql),
239 "select count(*) from information_schema.tables where table_catalog='%s' and table_name='%s'",
241 result = PQexec(
pgConn, sql);
244 TabCount = atol(PQgetvalue(result, 0, 0));
PGconn * pgConn
Database connection.
cmdlist CMD[]
Global command table.
PGconn * fo_dbconnect(char *DBConfFile, char **ErrorBuf)
Connect to a database. The default is Db.conf.
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
int fo_tableExists(PGconn *pgConn, const char *tableName)
Check if table exists. Note, this assumes the database name is 'fossology'.
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...
char * DBConfFile
DB conf file location.