Giter Site home page Giter Site logo

libpqnb's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

libpqnb's Issues

Making single query

Hello, I can't seem to figure out how to make just one query. I only pool one query but it always ends up running the query more than once, and it is giving undefined behavior in that the amount of times that the query callback is called is random.

#include "poolInit.h"

#define CONNINFO "postgresql://info@localhost/dbname"
#define QUERY "SELECT * FROM users"
#define TEST_TIME_SEC 1
#define NUM_CONNECTIONS 32

struct query_counter { uint64_t count; PGresult *resRet;};
static void
test_print_res(PGresult *res)
{
  int num_fields = PQnfields(res);
  for (int i = 0; i < num_fields; i++) {
      const int ftype = PQftype(res, i);
      printf("fname %s ftype %d\n", PQfname(res, i), ftype);
  }
  /* next, print out the rows */
  int num_rows = PQntuples(res);
  for (int i = 0; i < num_rows; i++) {
      for (int j = 0; j < num_fields; j++)
        printf("%s\t", PQgetvalue(res, i, j));
      printf("\n");
  }
}

struct PQNB_pool * getPool(){
  struct PQNB_pool *pool;
  pool = PQNB_pool_init(CONNINFO, NUM_CONNECTIONS);
  assert(NULL != pool);
  return pool;
}
int main(){
    struct PQNB_pool *pool = getPool();
    const union PQNB_pool_info *info;
  int epoll_fd, res;
  struct epoll_event ev, evs[1];
  struct query_counter counter;
  time_t end;
    info = PQNB_pool_get_info(pool, PQNB_INFO_EPOLL_FD);
      assert(NULL != info);
      assert(info->epoll_fd != -1);
      epoll_fd = epoll_create1(EPOLL_CLOEXEC);
      assert(epoll_fd != -1);
      ev.events = EPOLLIN;
      ev.data.ptr = NULL;
      res = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, info->epoll_fd, &ev);
      assert(-1 != res);
      counter.count = 0;
    static bool flag = false;
void test_query_cb2(PGresult *res,
              void *user_data,
              char *error_msg,
              bool timeout)
{
  struct query_counter *queries_counter;

  /*
   * ignoring compiler warnings
   */
  (void) res;

  if (timeout)
    {
      printf("timeout\n");
      return;
    }

  if (NULL != error_msg)
    {
      printf("%s", error_msg);
      return;
    }

  if (PGRES_TUPLES_OK == PQresultStatus(res))
    {
flag = true;
      assert(NULL != user_data);
      queries_counter = user_data;
      queries_counter->count++;
      queries_counter->resRet = PQcopyResult(res, PG_COPYRES_TUPLES);
    }
  else
    printf("query failed\n");
}
    // filling query buffer
    while(-1 != PQNB_pool_query(pool, QUERY, test_query_cb2, &counter));
    while(!flag){
         res = epoll_wait(epoll_fd, evs, 1, 1000);
         if (res == -1)
            break;
        if(counter.count ==0 && !flag){
            int run = PQNB_pool_run(pool);
        if (run == -1){
          printf("run returned -1\n");
          //break;
          }
          }
    }
    test_print_res(counter.resRet);
    PQNB_pool_free(pool);
    printf("total queries: %ld\n", counter.count);
    return 0;
}

Any help is very much appreciated, this seems like a really nice library and I'd love to get it fully working.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.