23 #define ERRBUFSIZE 11264 25 #include "libfossdb.h" 53 Fconf = fopen(DBConfFile,
"r");
55 Fconf = fopen(FOSSDB_CONF,
"r");
58 *ErrorBuf = malloc(ERRBUFSIZE);
61 snprintf(*ErrorBuf, ERRBUFSIZE,
"Database conf file: %s, ",
62 (DBConfFile ? DBConfFile :
"FOSSDB_CONF"));
63 BufLen = strlen(*ErrorBuf);
64 strerror_r(errno, *ErrorBuf + BufLen, ERRBUFSIZE - BufLen);
70 memset(CMD,
'\0',
sizeof(CMD));
77 memset(Line,
'\0',
sizeof(Line));
81 for (i = 0; (i < sizeof(Line)) && (C != '\n') && (C > 0); i++)
84 if ((C > 0) && (C !=
'\n')) Line[i] = C;
85 if ((C ==
'=') && !PosEqual) PosEqual = i;
86 else if ((C ==
';') && !PosSemi) PosSemi = i;
89 if (PosSemi < PosEqual) PosEqual = 0;
90 if ((Line[0] !=
'#') && PosEqual && PosSemi)
96 if (CMDlen >=
sizeof(CMD))
102 Line[PosSemi] =
'\0';
103 for (i = 0; i < PosEqual; i++)
105 if (!isspace(Line[i])) CMD[CMDlen++] = Line[i];
106 if (CMDlen >=
sizeof(CMD))
113 if (CMDlen >=
sizeof(CMD))
118 for (i = PosEqual + 1; Line[i] !=
'\0'; i++)
120 if (!isspace(Line[i])) CMD[CMDlen++] = Line[i];
121 if (CMDlen >=
sizeof(CMD))
132 if (CMD[0] ==
'\0')
goto BadConf;
135 pgConn = PQconnectdb(CMD);
136 if (PQstatus(pgConn) != CONNECTION_OK)
138 *ErrorBuf = malloc(ERRBUFSIZE);
142 const char pass[10]=
"password=";
143 for(i = strstr(CMD,pass) - CMD + strlen(pass); i < strlen(CMD); i++){
149 snprintf(*ErrorBuf, ERRBUFSIZE,
150 "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));
158 *ErrorBuf = malloc(ERRBUFSIZE);
159 snprintf(*ErrorBuf, ERRBUFSIZE,
"Invalid Database conf file: %s, ",
160 (DBConfFile ? DBConfFile :
"FOSSDB_CONF"));
185 printf(
"FATAL: %s:%d, %s\nOn: %s\n",
186 FileID, LineNumb, PQerrorMessage(pgConn), sql);
191 if (PQresultStatus(result) == PGRES_TUPLES_OK)
return 0;
193 printf(
"ERROR: %s:%d, %s\nOn: %s\n",
194 FileID, LineNumb, PQresultErrorMessage(result), sql);
219 printf(
"FATAL: %s:%d, %sOn: %s\n",
220 FileID, LineNumb, PQerrorMessage(pgConn), sql);
225 if (PQresultStatus(result) == PGRES_COMMAND_OK)
return 0;
227 printf(
"ERROR: %s:%d, %sOn: %s\n",
228 FileID, LineNumb, PQresultErrorMessage(result), sql);
249 snprintf(sql,
sizeof(sql),
250 "select count(*) from information_schema.tables where table_catalog='%s' and table_name='%s'",
251 PQdb(pgConn), tableName);
252 result = PQexec(pgConn, sql);
255 TabCount = atol(PQgetvalue(result, 0, 0));
char * DBConfFile
DB conf file location.
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
PGconn * pgConn
Database connection.
cmdlist CMD[]
Global command table.
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...
int fo_tableExists(PGconn *pgConn, const char *tableName)
Check if table exists. Note, this assumes the database name is 'fossology'.
PGconn * fo_dbconnect(char *DBConfFile, char **ErrorBuf)
Connect to a database. The default is Db.conf.