Value comparator

The value comparator matches a field against a value.

ValueComparator ( FieldExpression, ValueExpression )

Function

Explanation

eq

Include objects where the compared field is equal to value expression.

neq

Include objects where the compared field is not equal to value expression.

gt

Include objects where the compared field is greater than value expression.

lt

Include objects where the compared field is lower than value expression.

gte

Greater than or equals

lte

Lower than or equals

match

For string fields only, the value expression is a regular expression.

Example

value_comparator.cpp

Try it

 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::eq;
15  using dboo::gt;
16  using dboo::gte;
17  using dboo::lte;
18  using dboo::set_union;
19  using dboo::match;
20  using dboo::select;
21  
22  dboo::odb db;
23  
24  db.connect("localhost", 8822,
25             "example_queries",
26             "example_user",
27             "password"s);
28  
29  std::vector<Product*> results_a;
30  std::vector<Purchase*> results_b;
31  std::vector<Customer*> results_c;
32  std::vector<Customer*> results_d;
33  std::vector<Customer*> results_e;
34  
35  // Selects all Product with _price greater than 10.5
36  db.select<Product>(results_a, gt("_price", 10.5));
37  std::cout << "Found " << results_a.size() << " products" << std::endl;
38  
39  // Selects all Purchase with quantity = 10
40  db.select<Purchase>(results_b, eq("_quantity", 10));
41  std::cout << "Found " << results_b.size() << " purchases" << std::endl;
42  
43  // Selects all Customer with a .com email address:
44  db.select<Customer>(results_c, match("_email", ".*@.*\\.com"s));
45  std::cout << "Found " << results_c.size() << " customers" << std::endl;
46  
47  // Selects all Customer with email exactly matching "user@someemail.somewhere"
48  db.select<Customer>(results_d, eq("_email", "op6732@p12.com"s));
49  std::cout << "Found " << results_d.size() << " customers" << std::endl;
50
51  // Selects all Purchases with products priced lower than or equal to 5 or greater than or equal to 10.
52  std::vector<std::unique_ptr<Purchase>> results_up;
53  db.select<Purchase>(results_up, in("_product", set_union(
54    select<Product>(gte("_price", 10)),
55    select<Product>(lte("_price", 5)))));
56  std::cout << "Found " << results_up.size() << " purchases" << std::endl;
57  
58  return 0;
59}

types.h

 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:

init_db.cpp

Try it

init_data.cpp

Try it

Running the example will give the following output:

> ./value_comparator
Found 5 products
Found 4 purchases
Found 1 customers
Found 1 customers
Found 88 purchases

value_comparator.js

Try it

 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 Product with _price greater than 10.5
20db.query(results_a, "select<Product>(gt(_price, 10.5))");
21console.log("Found ", results_a.length, " products");
22  
23// Selects all Purchase with quantity = 10
24db.query(results_b, "select<Purchase>(eq(_quantity, 10))");
25console.log("Found ", results_b.length, " purchases");
26
27// Selects all Customer with a .com email address:
28db.query(results_c, 'select<Customer>(match(_email, ".*@.*\\.com"))');
29console.log("Found ", results_c.length, " customers");
30
31// Selects all Customer with email exactly matching "user@someemail.somewhere"
32db.query(results_d, 'select<Customer>(eq(_email, "op6732@p12.com"))');
33console.log("Found ", results_d.length, " customers");
34
35// Selects all Purchases with products priced lower than or equal to 5 or greater than or equal to 10.
36db.query(results_e, 'select<Purchase>(in(_product, set_union(select<Product>(gte(_price, 10)),select<Product>(lte(_price, 5)))))');
37console.log("Found ", results_e.length, " customers");

types.js

 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_db.js

Try it

init_data.js

Try it

Running the example will give the following output:

> node value_comparator
Found  5  products
Found  4  purchases
Found  1  customers
Found  1  customers
Found  88  customers
# Using dboo manager from command line:
> dboo example_queries -u=example_user -p=password -- "select<Product>(gt(_price, 10.5))"
{"dboo::objectid" : "4a00887c09c8000e", "dboo::class" : "Product", "_name" : "Milk", "_description" : "", "_price" : 14.949999999999999}
{"dboo::objectid" : "4a00887c09c80010", "dboo::class" : "Product", "_name" : "Bread", "_description" : "", "_price" : 28.949999999999999}
{"dboo::objectid" : "4a00887c09c80011", "dboo::class" : "Product", "_name" : "Juice", "_description" : "", "_price" : 34.950000000000003}
{"dboo::objectid" : "4a00887c09c80012", "dboo::class" : "Product", "_name" : "Banana", "_description" : "", "_price" : 20}
{"dboo::objectid" : "4a00887c09c80013", "dboo::class" : "Product", "_name" : "Biscuits", "_description" : "", "_price" : 23.949999999999999}