Commit 72148f89 authored by Alexandre Peres Arias's avatar Alexandre Peres Arias

Merge branch 'master' of gitlab.c3sl.ufpr.br:apa13/recommender_C-stateofart

parents a807a28f 17751df2
Pipeline #6321 failed with stage
in 1 minute and 35 seconds
*.o
*.*~*
*.gch
recommender
CC = gcc
CFLAGS = -Wall -Wextra -Werror -Wconversion -fstack-protector -Wformat-security -g -O3 -pipe -march=native
CFLAGS = -Wall -Wextra -Wconversion -fstack-protector -Wformat-security -g -O3 -pipe -march=native
LFLAGS = -lpq -ljansson
INCL = -I./include
SRC = ./src
DEPS = include/db_api.h
DEPS = ./include/db_api.h
OBJ = db_api.o
APP=recommender
$(APP): $(OBJ)
$(CC) $(CFLAGS) -o $@ $^
$(CC) $(CFLAGS) -o $@ $^ $(LFLAGS)
%.o: $(SRC)/%.c $(DEPS)
@$(CC) $(CFLAGS) -c -o $@ $< $(INCL)
$(CC) $(CFLAGS) -c -o $@ $< $(INCL)
clean:
rm -f $(OBJ) $(APP) core *~ ./include/*~ *.o .*.swp ./include/*.swp ./src/*.swp ./include/.*.swp ./src/.*.swp
{
"dbname": "portalmec_dev",
"user": "portalmec",
"password": "123mudar",
"host": "localhost"
}
#ifndef _DB_API_H
#define _DB_API_H
#define CONNINFO_LEN 350
#define QUERY_LEN 350
#define STRING_LEN 50
#include <postgresql/libpq-fe.h>
#include <string.h>
#include <jansson.h>
#include <stdlib.h>
typedef PGresult DBresult;
PGconn* db_connect();
char* db_get_value(DBresult *result, int row, int column);
int db_count_colums(DBresult *result);
int db_count(DBresult *result);
DBresult* db_select(char *columns, char *table, char *where);
DBresult* db_find_by_id(int id, char *table);
void db_disconnect(PGconn *conn);
void read_conf(const char *path, char * conninfo);
#endif
/*
* Implementation ....
* Implementation of functions that manipulate data from postgres database
*
* This source code is licensed under the GNU General Public License,
* Version 3. See the file LICENSE for more details.
*
*/
#include "db_api.h"
void read_conf(const char *path, char * conninfo) {
const char *key;
json_t *value;
char k_v[CONNINFO_LEN];
json_error_t error;
strcpy(conninfo, "");
strcpy(k_v, "");
json_t *config = json_load_file(path,JSON_ENCODE_ANY,&error);
if(!config) {
fprintf(stderr, "Error in database config file.\n");
fprintf(stderr, "%s\n", error.text);
exit(-1);
}
// Iterate over json and build connection info
json_object_foreach(config, key, value) {
sprintf(k_v, "%s = %s", key, json_string_value(value));
sprintf(conninfo,"%s %s ",conninfo, k_v);
strcpy(k_v,"");
}
}
PGconn* db_connect() {
char conninfo[CONNINFO_LEN];
// Build connection info
read_conf("conf/database.json", conninfo);
// Create Connection
PGconn *conn = PQconnectdb(conninfo);
// Validate
if(!conn) {
fprintf(stderr, "Error in create a connection with db.\n" );
exit(-1);
}
else if( PQstatus(conn) == CONNECTION_BAD ) {
fprintf(stderr, "Conection to database failed.\n");
fprintf(stderr, "%s", PQerrorMessage(conn));
exit(-1);
}
return conn;
}
DBresult* db_select( char *columns, char *table, char *where) {
PGconn *conn = db_connect();
char query[QUERY_LEN];
DBresult *result;
// Build query
sprintf(query,"SELECT %s FROM %s", columns, table);
if (where && strcmp(where, ""))
sprintf(query,"%s WHERE %s ",query, where);
strcat(query, ";");
// Execute query
result = PQexec(conn, query);
// Validate
if(!result){
fprintf(stderr, "Error in executing the command.\n");
}
else if ( PQresultStatus(result) == PGRES_FATAL_ERROR ) {
fprintf(stderr, "Error in query.\n");
fprintf(stderr, "%s\n", PQresultErrorMessage(result));
exit(-1);
}
db_disconnect(conn);
return result;
}
DBresult* db_find_by_id(int id, char *table) {
char query[QUERY_LEN];
sprintf(query,"id=%d", id);
return db_select("*", table, query);
}
int db_count(DBresult *result) {
return PQntuples(result);
}
int db_count_colums(DBresult *result) {
return PQnfields(result);
}
char* db_get_value(DBresult *result, int row, int column) {
return PQgetvalue(result, row, column);
}
void db_disconnect(PGconn *conn) {
PQfinish(conn);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment