Tag orientdb orientpp c++ driver intro

OrientDB C++ driver intro

23 Февраль, 15:16, by Rus Метки:

The C++ connector to OrientDB can be used in various applications that need simple remote access to this DB. Connector uses the OrientDB Network Binary Protocol and supports three query languages — SQL, JavaScript and Gremlin.

SQL access example :

#include "db.h"
using namespace OrientPP;
...
orientsrv server("localhost", "root" , "root_pass");
if (!server.dbexists("test_db"))
server.createdb("test_db", AS_GRAPH_DB, DB_LOCAL_STORAGE);
orientdb db(server);
db.open("test_db", AS_GRAPH_DB, "admin", "admin");
clog << "DB size: " << db.size() << endl;
clog << "DB records count: " << db.count() << endl;
orientquery q(db);
q << "select * from ouser where name = 'admin'";
orientresult_ptr res = q.execute(AS_SQL);
dump_result(res);
db.close();

JavaScript query example :

orientsrv server("localhost", "root" , "root_pass");
orientdb db(server);
db.open("test_db", AS_GRAPH_DB, "admin", "admin");
q << "var r = db.query('select from ouser');print(r);r";
orientresult_ptr res = q.execute(AS_JAVASCRIPT);
dump_result(res);

Gremlin query example :

orientsrv server("localhost", "root" , "root_pass");
orientdb db(server);
db.open("test_db", AS_GRAPH_DB, "admin", "admin");
q << "v = g.addVertex()";
orientresult_ptr res = q.execute(AS_GREMLIN);
dump_result(res);

The demo dump_result() code can be taken from orientpp test.cpp file and looks like :

string dump_record(orient_record_t &r);

void dump_result(orientresult_ptr res)
{
clog << "Result has " << res->records.size() << " records" << endl;
for (u32 r = 0; r < res->records.size(); r++)
clog << "#" << r << " " << dump_record(res->records[r] << endl);
}

string dump_record(orient_record_t &r)
{
stringstream ss;
if (r.rid.valid) {
if (r.is_document()) {
ss << string(r.rid) << " class:" << r.classof();
for (property_iterator it = r.properties.begin();
it != r.properties.end(); it++) {
property_t p = it->second;
ss << " t:" << p.type2str() << " n:" << p.name << " v:" << string(p);
if (p.type == ORIENT_RECORD_TYPE_COLLECTION) {
ss << " [";
for (uint i = 0; i < p.embedded.size(); i++) {
if (i)
ss << " | ";
ss << "t:" << p.embedded[i].type2str();
if (p.embedded[i].name.size())
ss << " n:" << p.embedded[i].name;
ss << " v:" << string(p.embedded[i]);
}
ss << " ]";
}
}
} else
ss << string(r.rid) << " " << string(r);
} else
ss << string(r);
return ss.str();
}