Skip to content

zussel/matador

Repository files navigation

matador

Take your database by the horns.

Version 0.9.2 GPLv3 C++14

Build Status

Branches Linux-CI Windows-CI CodeCov
master Build Status Build status codecov
develop Build Status Build status codecov

matador is an ORM and Web Application framework written in C++. It encapsulates all database backend communication and offers a simple (web) server. You don't have to deal with database backends or sql statements neither with mapping of data types nor serialization of objects. It comes with relation handling out of the box, brings a unique container for all kind of objects and a fluent sql query interface. All features are combined into an ORM layer. With the integrated web server it is possible to write your own web application with database backend.

Features:

  • A unique fluent query interface
  • One to one/many relations
  • One storage container
  • Filter with simple expressions
  • Transactions
  • STL like interface and iterators
  • Json support including parser and object mapper
  • Simple Logging mechanism
  • Networking
  • Dependency Injection
  • Web Server
  • Template Engine

Supported databases:

  • PostgreSQL
  • SQLite3
  • MySQL/MariaDB
  • MS SQL Server (ODBC)

Documentation can be found here.

Example

The following example define an entity person, sets up orm backend and logging and creates a http server listening at port 8000. The server serves five REST apis at http://localhost:8000/person/:

  • GET / (returns all person)
  • GET /{id} (return person with id where id must be numeric)
  • POST / (creates a person from the given json body)
  • PUT /{id} (updates a person with id from the given json body)
  • DELETE /{id} (deletes a person with id)
// use matador namespace
using namespace matador

// a simple person class
struct person
{
  long id;   // primary key
  std::string name;
  date birthday;
  container<std::string> colors;
  
  template < class Operator >
  void process(Operator &op) {
    namespace field = matador::access;
    field::primary_key(op, "id", id);
    field::attribute(op, "name", name, 255);
    field::attribute(op, "birthday", birthday);
    field::has_many(op,                 // operator
                    "person_color",     // relation table name
                    colors,             // class member
                    "person_id",        // left column in relation table
                    "color",            // right column in relation table
                    cascade_type::ALL); // cascade type
  }
};

// setup application logging
add_log_sink(matador::create_file_sink("log/server.log"));
add_log_sink(matador::create_stdout_sink());

// prepare the persistence layer
persistence p("sqlite://db.sqlite");
p.enable_log();
p.attach<person>("person");

// create tables
p.create();

// create a database session and
// load data from db
session s(p);
s.load();

// initialize network stack
net::init();

// create server and add routing
http::server server(8000);
server.add_routing_middleware();

// return all persons
server.on_get("/person", [&s](const request &req) {
  auto result = s.select<person>();
  json_object_mapper mapper;

  std::string body = mapper.to_string(result, json_format::pretty);

  return response::ok(body, mime_types::TYPE_APPLICATION_JSON);
});

// return one person
server.on_get("/person/{id: \\d+}", [&s](const http::request &req) {
  auto id = std::stoul(req.path_params().at("id"));
  auto result = s.get<person>(id);

  if (result.empty()) {
    return http::response::not_found();
  }

  json_object_mapper mapper;

  std::string body = mapper.to_string(result, json_format::pretty);

  return http::response::ok(body, http::mime_types::TYPE_APPLICATION_JSON);
});

// insert person
server.on_post("/person", [&s](const http::request &req) {
  json_object_mapper mapper;

  auto p = mapper.to_ptr<person>(req.body());
  auto optr = s.insert(p.release());
  s.flush();

  std::string body = mapper.to_string(optr, json_format::pretty);
  return http::response::ok(body, http::mime_types::TYPE_APPLICATION_JSON);
});

// update person
server.on_put("/person/{id: \\d+}", [&s](const http::request &req) {
  auto id = std::stoul(req.path_params().at("id"));
  auto result = s.get<person>(id);

  if (result.empty()) {
    return http::response::not_found();
  }

  json_object_mapper mapper;
  auto p = mapper.to_ptr<person>(req.body());

  // update entity
  result.modify()->name = p->name;
  result.modify()->birthday = p->birthday;
  result.modify()->colors.clear();
  for (const auto &color : p->colors) {
    result.modify()->colors.push_back(color);
  }
  s.flush();

  return http::response::no_content();
});

// delete person
server.on_remove("/person/{id: \\d+}", [&s](const http::request &req) {
  auto id = std::stoul(req.path_params().at("id"));
  auto result = s.get<person>(id);

  if (result.empty()) {
   return http::response::not_found();
  }

  s.remove(result);

  return http::response::no_content();
});

server.run();
net::cleanup();

Requirements

The library has almost no dependencies. At least the database library you want to use. If you would like to build from the sources you need at least the cmake build system installed. If you plan to generate an installation the package on a Windows system you need the nullsoft scriptable install system.

Sources

Get the sources from GitHub and enter the created directory:

$ git clone https://github.com/zussel/matador.git
$ cd matador

Building under Linux

Create a build directory change to it and call cmake:

$ mkdir build
$ cd build
$ cmake ..

Then you can build matador from sources:

$ make

Building under Windows (for Visual Studio)

Create a build directory change to it and call cmake:

$ mkdir build
$ cd build
$ cmake -G "Visual Studio *" ..

Where * is one of the "Visual Studio" strings up from "14". See cmake documentation here. After generation, you find a matador.sln solution file in the current directory.

Contact

If you have questions or issues concerning matador you can place an issue in my matador github repository