text: #include <stdio.h>
  #include <libpq-fe.h>
  #include <stdlib.h>
  #include <string.h>
  
  #include "product.h"
  
  //Uses the string *id to place a product in *product.
  //If type is 0 *id is used as barcode, else it is taken as product id
  int _get_product(PGconn *conn, product *product, char *id, char type)
  {
    //Result is static since it should be freed right before the next transaction.
    //This because the strings are used as pointers to it.
    static PGresult *res;
    //The query to get the user
    char *get_product = "SELECT name, price FROM products WHERE id = $1";
    char *get_product_by_barcode = "SELECT name, price FROM barcodes JOIN products ON barcodes.product = products.id WHERE barcodes.barcode = $1";
    //A constant array to hold the param according to library specs
    const char *params[1];
    params[0] = id;
    
    int tmp;
  
    //Clear old and get new data
    PQclear(res);
  
    if(type)
      {
        res = PQexecParams(conn, get_product, 1, NULL, params, NULL, NULL, 0);
      }
    else
      {
        res = PQexecParams(conn, get_product_by_barcode, 1, NULL, params, NULL, NULL, 0);
      }
    //Handle error when getting data
    if ( PQresultStatus(res) != PGRES_TUPLES_OK || PQntuples(res) < 1 )
      {
        //No result was given
        return 1;
      }
    
    //Parse into struct
    if(sscanf(PQgetvalue(res, 0, 1), "%d", &tmp) == EOF)
      {
        return 1;
      }
    product->price = tmp;
    strncpy(product->name, PQgetvalue(res, 0, 0), 64);
    
    return 0;
  }
  
  int get_product(PGconn *conn, product *product, char *id)
  {
    return _get_product(conn, product, id, 1);
  }
  
  int get_product_by_barcode(PGconn *conn, product *product, char *barcode)
  {
    return _get_product(conn, product, barcode, 0);
  }
  
  void print_product(product *product)
  {
  }
  