A fluent sql query builder
Go to file
Sascha Kühl 9e60b5e485 introduced a processed table set 2025-02-07 11:49:32 +01:00
backends added missing include 2025-02-07 11:49:00 +01:00
cmake initial matador ng commit 2025-02-02 20:37:12 +01:00
demo changed pk type to unsigned int 2025-02-07 11:45:00 +01:00
include/matador introduced a processed table set 2025-02-07 11:49:32 +01:00
source removed obsolete namespace 2025-02-07 11:48:40 +01:00
test disabled coverage option 2025-02-07 11:47:42 +01:00
.gitignore fixed record class 2024-07-15 08:14:11 +02:00
CMakeLists.txt added connection pool and tests 2025-02-05 15:47:51 +01:00
LICENSE Initial commit 2023-11-01 19:40:26 +01:00
README.md query progress 2024-02-28 18:02:12 +01:00

README.md

query_context

A fluent sql query_context builder

MATADOR_BACKENDS_PATH=/home/sascha/Develop/query/cmake-build-debug/backends

Object definition

enum class Color {
  Red, Green, Blue, Yellow, Pink, Black, White
};

struct Coordinate
{
  int x;
  int y;
  int z;
};

// special handling for custom type Coordinate
namespace matador::utils::access {

template<class Operator>
void attribute(Operator &op, const char *id, Coordinate &value, const field_attributes &attr = null_attributes) {
  attribute(op, (std::string(id) + "_x").c_str(), value.x, attr);
  attribute(op, (std::string(id) + "_y").c_str(), value.y, attr);
  attribute(op, (std::string(id) + "_z").c_str(), value.z, attr);
}

}

struct airplane
{
  unsigned long id;
  std::string brand;
  std::string model;
  Color color;
  Coordinate position;

  template<class Operator>
  void process(Operator &op) {
    namespace field = matador::utils::access;
    field::primary_key(op, "id", id);
    field::attribute(op, "brand", brand, 255);
    field::attribute(op, "model", model, 255);
    field::attribute(op, "color", color);
    field::attribute(op, "position", position);
  }
};
connection_pool<connection> pool("sqlite://sqlite.db", 4);
session s(pool);

// create all tables
s.create().table<airplane>().execute();

std::vector<airplane> planes {
  {1, "Airbus", "A380", Color::Green, {1, 2, 3}},
  {2, "Boeing", "707", Color::White, {4, 5, 6}},
  {3, "Boeing", "747", Color::Blue, {0, 0, 0}}
};

auto stmt = s.insert()
             .into<airplane>("airplane")
             .values<airplane>().prepare();


// insert
for (const auto &plane: planes) {
  auto res = stmt.bind(plane).execute();
  stmt.reset();
}

s.update("airplane")
 .set({"model", "737"})
 .where("id"_col == 2 && "color"_col == Color::White)
 .execute();

auto result = s.select<airplane>()
               .from("airplane")
               .where("brand"_col == "Boeing")
               .execute();

for (const auto &plane : result) {
  std::cout << "airplane: " << plane->brand << " - " << plane->model << "\n";
}