Commit 7b14294c authored by Giovanne Marcelo's avatar Giovanne Marcelo

Merge branch 'db' into 'master'


See merge request !1
parents 9936658e d98924df
Pipeline #6319 failed with stage
in 1 minute and 9 seconds
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): $(OBJ)
$(CC) $(CFLAGS) -o $@ $^
$(CC) $(CFLAGS) -o $@ $^ $(LFLAGS)
%.o: $(SRC)/%.c $(DEPS)
@$(CC) $(CFLAGS) -c -o $@ $< $(INCL)
$(CC) $(CFLAGS) -c -o $@ $< $(INCL)
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);
* 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);
// 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);
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" );
else if( PQstatus(conn) == CONNECTION_BAD ) {
fprintf(stderr, "Conection to database failed.\n");
fprintf(stderr, "%s", PQerrorMessage(conn));
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
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));
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) {
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