Code Monkey home page Code Monkey logo

pg_async's Introduction

pg_async

Asynchronous client library for PostgreSQL, protocol v3.

Motivation

When developing software we deal a lot with external network resources. Most of them can be accessed through an asynchronous interface, so we don't have to block to wait for a request to finish. The lack of modern C++ asynchronous interface was the reason for writing this library.

Overview

pg_async is an unofficial PostreSQL database asynchronous client library written in modern C++ (std=c++11) on top of boost::asio library used for asynchronous network input-output.

Features

  • Asynchronous operations
  • Connection pooling
  • Database aliases
  • Standard container-compliant resultset interface
  • Execution of prepared statements
  • Multiple result sets for simple query mode
  • Data row extraction to tuples
  • Flexible datatype conversion
  • Compile-time statement parameter types binding and checking
  • Extensible datatypes input-output system
  • TCP or UNIX socket connection

Usage

#include <tip/db/pg.hpp>

using namespace tip::db::pg;

// Register conntions
db_service::add_connection("main=user@localhost:5432[the_database]");
db_service::add_connection("logs=user@localhost:5432[logs_db]");

// Run the service
db_service::run(); // Will block the thread

// Execute queries
db_service::begin(            // Start a transaction
  "main"_db,                  // Database alias
  [](transaction_ptr tran)    // Function to be run inside a transaction
  {
    query(
      tran,                                 // Transaction handle
      "select * from pg_catalog.pg_type"    // SQL statement
    )   
    ([](transaction_ptr tran, resultset res, bool complete) // Query results handler
    {
      for (auto field_desc : 
              res.row_description()) {    // Iterate the field descriptions
        std::cout << field_desc.name << "\t";
      }
      std::cout << "\n";
      for (auto row : res) {              // Iterate the resultset
        int a, b;
        row.to(std::tie(a, b));           // Extract query result row to a tuple
        for (auto field : row) {          // Iterate the fields of the row
          std::cout << field.coalesce( "null" ) 
              << "\t";
        }
      }
    },
    [](db_error const& error)             // Query error handler
    {
    });
  },
  [](db_error cosnt& error)   // Function to be called in case of a transaction error
  {
  }
);

For more information please see accompanying doxygen documentation.

Installation

git clone [email protected]:zmij/pg_async.git
cd pg_async
mkdir build
cd build
cmake ..
make install

pg_async's People

Contributors

jwakely avatar ufna avatar varlog avatar zmij avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pg_async's Issues

Use state machine with priorities

Use zmij/afsm#7 feature to prioritize events. Commit/rollback should have a lower priority than execute or execute_prepared so that queries started on other queries' completion callback execute before commit that was already enqueued.

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.