dboo::odb::query

size_t query ( const std::string& query_string );   // 1)
size_t query ( std::vector<obj_ref>& objects, const std::string& query_string, fetch_options options = {});   // 2)
size_t query ( std::vector<obj_id>& object_ids, const std::string& query_string, fetch_options options = {});   // 3)
size_t query ( std::vector<internalized_instance>& results, const std::string& query_string, fetch_options options = {});   // 4)
[size_t query ( std::vector<object_meta_data>& meta_data, const std::string& query_string, fetch_options options = {});   // 5)]
size_t query ( format& output, const std::string& query_string, fetch_options options = {});   // 6)
query( query_string ) : Number               // 1)
query( objects, query_string ) : Number      // 2)

Executes the provided query, returns objects in the provided array and returns the number of found items. A maximum number of items are returned, by default this is 10000 but can be modified with fetch_options. The remaining objects can subsequently be retrieved by calling dboo::odb::fetch until all objects have been returned.

1

Executes the query without returning objects (for count or erase queries).

2

Executes the query and returns the selected items in the array/vector objects. Only select queries returns objects, while count or delete obviously don’t. In C++, so called obj_ref references are returned, they can be cast to normal pointers.

3

Returns object ids rather than objects.

4

Returns the objects as internalized_instance. With this call there is no internalization to runtime. Instead, the data is stored in a in-memory structure that can be accessed with index operator to access fields. The classes do not have to exist as types in application.

5

Returns meta data (not implemented yet with new database core)

6

Outputs selected objects to the provided formatter. With the formatter the objects can be output as json or xml. The classes do not have to exist as types in application.

Parameters

objects

An empty array which will be filled with the found objects.

query_string

The query as a string. See the query reference.

object_ids

An empty array which will be filled with ids of the found objects.

results

An empty array which will be filled with internalized_instances of the found objects.

meta_data

An empty array which will be filled with meta data of the found objects.

options

Fetch options givs some control on the returned set of objects:

fetch_option

Explanation

fetch_referenced_objects

Includes all referenced objects (only applies when return type is internalized_instance )

fetch_unlimited

Specifies that all objects should be returned with this call.

default_fetch_limit

Specifies that the number of objects returned are specified by the database configuration.

fetch_limit(number)

Limits the return to specified number of objects

output

If specifying a format object instead of a return container, the objects will be passed to the formatter instead. See dboo-format

Return value

Number of found/affected objects.

Exceptions

Example

#include <dboo/rfx.h>
#include <dboo/odb.h>
#include <dboo/osl/format_json.h>

class A {
public:
  inline static dboo::cls<A> cls{"A"};
  static void dboo_class_init(dboo::cls<A>& c) {
    c.member(&A::var1, "var1");
  }
private:
  int var1;
};

class B {
public:
  B() = default;
  B(A* a) : a{a} {}
  virtual ~B() = default;
  inline static dboo::cls<B> cls{"B"};
  static void dboo_class_init(dboo::cls<B>& c) {
    c.member(&A::a, "a");
  }
private:
  A* a;
};

int main() {

    using dboo::in;
    using dboo::gt;
    using dboo::select;

    dboo::odb db;

    db.connect("localhost", 8823,
              "my_database",
              "usr132",
              "password");

    vector<dboo::obj_id> ids;
    vector<dboo::internalized_instance> ii;
    vector<dboo::obj_ref> refs;

    db.query(ids, "select<B>"); // Selects all B and return their object ids.
    db.query(ii, "select<A>(gt(\"var1\", 10))"); // Selects all A with var1 greater than 1 as internalized_instance

    db.query(refs, "select<B>(in(\"a\", select<A>(gt(\"var1\", 10))))"); // Selects all B pointing to an A
                                                                 // with var1 greater than 10

    dboo::format_json json_out(std::cout);
    db.query(json_out, "select<B>(in(\"a\", select<A>(gt(\"var1\", 10))))"); // Selects all B pointing to an A
                                                                 // with var1 greater than 10 and writes as json to std::cout.

    return 0;
}
const dboo = require('dboo');
const config = require('config');

dboo.argv(process.argv);

dbConfig = config.get('dbConfig');

class UserId {
  userId = "";
  screenName = "";
  constructor(userid = "", screenName = "") {
    this.userId = userid;
    this.screenName = screenName;
  }
};

dboo.class(UserId,
  [{"userId": dboo.string},
    {"screenName": dboo.string},
  ]
);


class User {
  userId = null;
  address = "";
  constructor(userid = null, address = "") {
    this.userId = userid;
    this.screenName = screenName;
  }
};

dboo.class(User,
  [{"userId": UserId},  // referencing a UserId object
    {"address": dboo.string},
  ]
);

dboo.init(); // Class declarations must come before init!

const db = new dboo.odb();
db.connect(dbConfig.host, Number(dbConfig.port),
            dbConfig.dbName, dbConfig.webUserName,
            dbConfig.webUserPwd);

let results_b = [];
let results_a = [];

db.query(results_b, "select<B>()"); // Selects all B
db.query(results_a, "select<A>(gt(var1, 10))"); // Selects all A with var1 greater than 1

db.query(results_b, "select<B>(in(a, select<A>(gt(var1, 10))))"); // Selects all B pointing to an A
                                                             // with var1 greater than 10

In C++, it would be better to use the method dboo::odb::select instead.