Set comparator
The set comparator operates on pointer/reference fields and matches them against sets of objects.
Function ( FieldExpression, SetExpression )
Function |
Explanation |
---|---|
in |
Returns objects who’s member field refers to an object in the compared set expression. Returns objects who’s member field value is in the specified constant sequence. |
not_in |
Returns objects who’s member field refers to an object not in the compared set expression. |
any_in |
If field is an array, returns objects where any of the items point to an object in the compared set expression. |
all_in |
If field is an array, returns objects where all of the items point to an object in the compared set expression. |
noitemsin |
If field is an array, returns objects where none of the items point to an object in the compared set expression. |
Example
1#include "types.h"
2
3#include <dboo/rfx.h>
4#include <dboo/odb.h>
5
6#include <vector>
7#include <memory>
8
9int main() {
10 dboo::init();
11
12 using namespace std::string_literals;
13
14 using dboo::in;
15 using dboo::not_in;
16 using dboo::any_items_in;
17 using dboo::all_items_in;
18 using dboo::no_items_in;
19 using dboo::in_set;
20 using dboo::select;
21 using dboo::match;
22 using dboo::eq;
23
24 dboo::odb db;
25
26 db.connect("localhost", 8822,
27 "example_queries",
28 "example_user",
29 "password"s);
30
31 std::vector<Purchase*> results_a;
32 std::vector<Purchase*> results_b;
33 std::vector<Customer*> results_c;
34 std::vector<Customer*> results_d;
35 std::vector<Customer*> results_e;
36
37 // Selects all Purchases of milk
38 db.select<Purchase>(results_a, in("_product", select<Product>(eq("_name", "Milk"s))));
39 std::cout << "Found " << results_a.size() << " purchases" << std::endl;
40
41 // Selects all Purchases that was made by any customer that does not have an email address ending with .com
42 db.select<Purchase>(results_b, not_in("_customer", select<Customer>(match("_email", ".*\\.com"s))));
43 std::cout << "Found " << results_b.size() << " purchases" << std::endl;
44
45 // Selects all Customers that made a purchase with a price higher than 10.
46 db.select<Customer>(results_c, any_items_in("_purchases", select<Purchase>(dboo::gt("_price", 10.))));
47 std::cout << "Found " << results_c.size() << " customers" << std::endl;
48
49 // Selects all Customers that only made purchases with a price lower than 10.
50 db.select<Customer>(results_d, all_items_in("_purchases", select<Purchase>(dboo::lt("_price", 10.))));
51 std::cout << "Found " << results_d.size() << " customers" << std::endl;
52
53 // Selects all Customers that did not make any purchases with a price lower than 10.
54 db.select<Customer>(results_e, no_items_in("_purchases", select<Purchase>(dboo::lt("_price", 10.))));
55 std::cout << "Found " << results_e.size() << " customers" << std::endl;
56
57 // Selects all Purchases with a quantity of 8,10,11 or 13.
58 std::vector<std::unique_ptr<Purchase>> results_up;
59 db.select<Purchase>(results_up, in("_quantity", std::vector<int>{8, 10, 11, 13}));
60 std::cout << "Found " << results_up.size() << " purchases" << std::endl;
61
62 return 0;
63}
1#include <dboo/rfx.h>
2
3class Purchase;
4class Customer {
5public:
6 Customer() = default;
7 Customer(const std::string& name, const std::string& email) : _name{name}, _email{email} {}
8 inline static dboo::cls<Customer> cls{"Customer"};
9 static void dboo_class_init(dboo::cls<Customer>& c) {
10 c.member(&Customer::_email, "_email");
11 c.member(&Customer::_name, "_name");
12 c.member(&Customer::_purchases, "_purchases");
13 }
14
15 void made_purchase(Purchase* purchase) {
16 _purchases.emplace_back(purchase);
17 }
18
19private:
20 std::string _email;
21 std::string _name;
22 std::vector<Purchase*> _purchases;
23};
24
25class Product {
26public:
27 Product() = default;
28 Product(const std::string& name, double price) : _name{name}, _price{price} {}
29 inline static dboo::cls<Product> cls{"Product"};
30 static void dboo_class_init(dboo::cls<Product>& c) {
31 c.member(&Product::_name, "_name");
32 c.member(&Product::_description, "_description");
33 c.member(&Product::_price, "_price");
34 }
35 double price() const { return _price; }
36private:
37 std::string _name;
38 std::string _description;
39 double _price{0.};
40};
41
42class Purchase {
43public:
44 Purchase() = default;
45 Purchase(Product* prod,
46 Customer* cust,
47 double quantity, double price )
48 : _product{prod}
49 , _customer{cust}
50 , _quantity{quantity}
51 , _price{price}
52 {}
53 virtual ~Purchase() = default;
54 inline static dboo::cls<Purchase> cls{"Purchase"};
55 static void dboo_class_init(dboo::cls<Purchase>& c) {
56 c.member(&Purchase::_customer, "_customer");
57 c.member(&Purchase::_product, "_product");
58 c.member(&Purchase::_price, "_price");
59 c.member(&Purchase::_quantity, "_quantity");
60 }
61
62private:
63 Product* _product = nullptr;
64 Customer* _customer = nullptr;
65 double _quantity{0.};
66 double _price{0.};
67};
To create the database and initialize example data:
Running the example will give the following output:
> ./set_comparator
Found 17 purchases
Found 85 purchases
Found 6 customers
Found 0 customers
Found 6 customers
Found 26 purchases
1const dboo = require('dboo')
2const types = require('./types')
3
4dboo.init();
5
6const db = new dboo.odb();
7
8db.connect("localhost", 8822,
9 "example_queries",
10 "example_user",
11 "password");
12
13let results_a = [];
14let results_b = [];
15let results_c = [];
16let results_d = [];
17let results_e = [];
18
19// Selects all Purchases of milk
20db.query(results_a, "select<Purchase>(in(_product, select<Product>(eq(_name, 'Milk'))))");
21console.log("Found ", results_a.length, " purchases");
22
23// Selects all Purchases that was made by any customer that does not have an email address ending with .com
24db.query(results_b, "select<Purchase>(not_in(_customer, select<Customer>(match(_email, '.*\\.com'))))");
25console.log("Found ", results_b.length, " purchases");
26
27// Selects all Customers that made a purchase with a price higher than 10.
28db.query(results_c, "select<Customer>(any_in(_purchases, select<Purchase>(gt(_price, 10.))))");
29console.log("Found ", results_c.length, " customers");
30
31// Selects all Customers that only made purchases with a price lower than 10.
32db.query(results_d, "select<Customer>(all_in(_purchases, select<Purchase>(lt(_price, 10.))))");
33console.log("Found ", results_d.length, " customers");
34
35// Selects all Customers that did not make any purchases with a price lower than 10.
36db.query(results_e, "select<Customer>(noitemsin(_purchases, select<Purchase>(lt(_price, 10.))))");
37console.log("Found ", results_e.length, " customers");
38
39// Selects all Purchases with a quantity of 8,10,11 or 13.
40let results_up = [];
41db.query(results_up, "select<Purchase>(in(_quantity, {8,10,11,13}))");
42console.log("Found ", results_up.length, " purchases");
1const dboo = require('dboo')
2
3class Product {
4 _name;
5 _description;
6 _price;
7 constructor(name = "", price = 0.) {
8 this._name = name;
9 this._price = price;
10 }
11};
12exports.Product=Product;
13
14class Purchase {
15 _customer;
16 _product;
17 _price;
18 _quantity;
19 constructor(prod = null, customer = null, quantity = 0, price = 0) {
20 this._customer = prod;
21 this._product = customer;
22 this._price = quantity;
23 this._quantity = price;
24 }
25};
26exports.Purchase=Purchase;
27
28class Customer {
29 _email;
30 _name;
31 _purchases;
32
33 constructor( name = "", email = "") {
34 this._name = name;
35 this._email = email;
36 this._purchases = [];
37 }
38
39 made_purchase(purchase) {
40 this._purchases.push(purchase);
41 }
42};
43exports.Customer=Customer;
44
45
46dboo.class(Product, [
47 {"_name":dboo.string},
48 {"_description":dboo.string},
49 {"_price":dboo.double},
50]);
51
52dboo.class(Purchase, [
53 {"_customer":Customer},
54 {"_product":Product},
55 {"_price":dboo.double},
56 {"_quantity":dboo.double},
57]);
58
59dboo.class(Customer, [
60 {"_email":dboo.string},
61 {"_name":dboo.string},
62 {"_purchases":dboo.array(Purchase)},
63]);
To create the database and initialize example data:
init_data.js
Running the example will give the following output:
> node set_comparator
Found 17 purchases
Found 85 purchases
Found 6 customers
Found 0 customers
Found 6 customers
Found 26 purchases