Set operator
The set operator operates on two sets of objects (or more precisely object ids).
Function ( SetExpression, SetExpression )
Function |
Explanation |
---|---|
set_union |
Returns the union of two sets of object. |
set_isect |
Returns the intersection of two sets of object. |
set_diff |
Returns the difference of two sets of object, i.e. set1 - set2. |
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::set_union;
15 using dboo::set_isect;
16 using dboo::set_diff;
17 using dboo::select;
18 using dboo::match;
19 using dboo::eq;
20
21 dboo::odb db;
22
23 db.connect("localhost", 8822,
24 "example_queries",
25 "example_user",
26 "password"s);
27
28 std::vector<Purchase*> results_a;
29 std::vector<Purchase*> results_b;
30 std::vector<Customer*> results_c;
31
32 // Selects all Purchases of milk and bread
33 db.select<Purchase>(results_a, in("_product", set_union(
34 select<Product>(eq("_name", "Milk"s)),
35 select<Product>(eq("_name", "Bread"s)))));
36 std::cout << "Found " << results_a.size() << " purchases" << std::endl;
37
38 // Selects all Purchases of milk, banana and bread and price greater than 15
39 db.select<Purchase>(results_b, in("_product", set_isect(
40 select<Product>(dboo::in("_name", std::vector<std::string>{"Milk"s, "Bread"s, "Banana"s})),
41 select<Product>(dboo::gt("_price", 15)))));
42 std::cout << "Found " << results_b.size() << " purchases" << std::endl;
43
44 // Selects all Customers with an a in the name but not with a .com email address
45 db.select<Customer>(results_c, set_diff(
46 select<Customer>(dboo::match("_name", ".*a.*"s)),
47 select<Customer>(dboo::match("_email", ".*\\.com"s))));
48 std::cout << "Found " << results_c.size() << " purchases" << std::endl;
49
50 return 0;
51}
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_operator
Found 34 purchases
Found 17 purchases
Found 0 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 = [];
16
17// Selects all Purchases of milk and bread
18db.query(results_a, "select<Purchase>(in(_product, set_union(\n" +
19 " select<Product>(eq(_name, 'Milk')),\n" +
20 "select<Product>(eq(_name, 'Bread')))))");
21console.log("Found ", results_a.length, " purchases");
22
23// Selects all Purchases of milk, banana and bread and price greater than 15
24db.query(results_b, "select<Purchase>(in(_product, set_isect(\n" +
25 " select<Product>(in(_name, {'Milk','Bread','Banana'})),\n" +
26 " select<Product>(gt(_price, 15)))))");
27console.log("Found ", results_b.length, " purchases");
28
29// Selects all Customers with an a in the name but not with a .com email address
30db.query(results_c, 'select<Customer>(set_diff(\n' +
31 ' select<Customer>(match(_name, ".*a.*")),\n' +
32 'select<Customer>(match(_email, ".*\\\\.com")))))');
33console.log("Found ", results_c.length, " customers");
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_operator.js
Found 34 purchases
Found 17 purchases
Found 0 purchases