updated README.md
This commit is contained in:
parent
81f22d73d9
commit
56c65203fc
60
README.md
60
README.md
|
|
@ -4,11 +4,36 @@ A fluent sql query_context builder
|
||||||
|
|
||||||
Object definition
|
Object definition
|
||||||
```cpp
|
```cpp
|
||||||
|
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
|
struct airplane
|
||||||
{
|
{
|
||||||
unsigned long id;
|
unsigned long id;
|
||||||
std::string brand;
|
std::string brand;
|
||||||
std::string model;
|
std::string model;
|
||||||
|
Color color;
|
||||||
|
Coordinate position;
|
||||||
|
|
||||||
template<class Operator>
|
template<class Operator>
|
||||||
void process(Operator &op) {
|
void process(Operator &op) {
|
||||||
|
|
@ -16,6 +41,8 @@ struct airplane
|
||||||
field::primary_key(op, "id", id);
|
field::primary_key(op, "id", id);
|
||||||
field::attribute(op, "brand", brand, 255);
|
field::attribute(op, "brand", brand, 255);
|
||||||
field::attribute(op, "model", model, 255);
|
field::attribute(op, "model", model, 255);
|
||||||
|
field::attribute(op, "color", color);
|
||||||
|
field::attribute(op, "position", position);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
@ -23,20 +50,35 @@ struct airplane
|
||||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
||||||
session s(pool);
|
session s(pool);
|
||||||
|
|
||||||
// register entity
|
|
||||||
s.attach<airplane>("airplane")
|
|
||||||
|
|
||||||
// create all tables
|
// create all tables
|
||||||
s.create();
|
s.create().table<airplane>().execute();
|
||||||
|
|
||||||
|
std::vector<entity<airplane>> planes {
|
||||||
|
make_entity<airplane>(1, "Airbus", "A380"),
|
||||||
|
make_entity<airplane>(2, "Boeing", "707"),
|
||||||
|
make_entity<airplane>(3, "Boeing", "747")
|
||||||
|
};
|
||||||
|
|
||||||
|
auto stmt = s.insert()
|
||||||
|
.into<airplane>("airplane")
|
||||||
|
.values<airplane>().prepare();
|
||||||
|
|
||||||
|
|
||||||
// insert
|
// insert
|
||||||
s.insert<airplane>(1, "Airbus", "A380");
|
for (const auto &plane: planes) {
|
||||||
s.insert<airplane>(2, "Boeing", "748");
|
auto res = stmt.bind(*plane).execute();
|
||||||
s.insert<airplane>(3, "Boeing", "707");
|
stmt.reset();
|
||||||
|
}
|
||||||
|
|
||||||
s.update<airplane>().set({"model", "747"}).where("id"_col == 2);
|
s.update("airplane")
|
||||||
|
.set({"model", "737"})
|
||||||
|
.where("id"_col == 2)
|
||||||
|
.execute();
|
||||||
|
|
||||||
auto result = s.select<airplane>().where("brand"_col == "Boeing");
|
auto result = s.select<airplane>()
|
||||||
|
.from("airplane")
|
||||||
|
.where("brand"_col == "Boeing")
|
||||||
|
.execute();
|
||||||
|
|
||||||
for (const auto &plane : result) {
|
for (const auto &plane : result) {
|
||||||
std::cout << "airplane: " << plane->brand << " - " << plane->model << "\n";
|
std::cout << "airplane: " << plane->brand << " - " << plane->model << "\n";
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue