// examples/C++Binding/generic/query/query.cc
#include <eyedb/eyedb.h>
using namespace std;
int
main(int argc, char *argv[])
{
eyedb::init(argc, argv);
if (argc != 3) {
fprintf(stderr, "usage: %s <dbname> <query>\n", argv[0]);
return 1;
}
eyedb::Exception::setMode(eyedb::Exception::ExceptionMode);
try {
eyedb::Connection conn;
// connecting to the eyedb server
conn.open();
eyedb::Database db(argv[1]);
// opening database argv[1]
db.open(&conn, eyedb::Database::DBRW);
// beginning a transaction
db.transactionBegin();
// performing the OQL query argv[2] using the eyedb::OQL interface
eyedb::OQL q(&db, argv[2]);
eyedb::ValueArray arr;
q.execute(arr);
cout << "###### Performing the OQL query " << argv[2] <<
" using the eyedb::OQL interface" << endl;
// for each value returned in the query, display it:
for (int i = 0; i < arr.getCount(); i++) {
// in case of the returned value is an oid, load it first:
if (arr[i].type == eyedb::Value::OID) {
eyedb::Object *o;
db.loadObject(arr[i].oid, &o);
cout << o;
o->release();
}
else
cout << arr[i] << endl;
}
// performing the same query using eyedb::OQLIterator interface
// [1]: getting all returned values
cout << "\n###### Performing the same query using eyedb::OQLIterator "
"interface: getting all returned values" << endl;
eyedb::OQLIterator iter(&db, argv[2]);
eyedb::Value val;
while (iter.next(val)) {
// in case of the returned value is an oid, load it first:
if (val.getType() == eyedb::Value::OID) {
eyedb::Object *o;
db.loadObject(val.oid, &o);
cout << o;
// in case of the returned object is a collection, display its
// contents
if (o->asCollection()) {
eyedb::CollectionIterator citer(o->asCollection());
cout << "contents BEGIN\n";
eyedb::Object *co;
while(citer.next(co)) {
cout << co;
co->release();
}
cout << "contents END\n\n";
}
// in case of the returned object is a class, display its
// extent
else if (o->asClass()) {
eyedb::ClassIterator citer(o->asClass());
cout << "extent BEGIN\n";
eyedb::Object *co;
while(citer.next(co)) {
cout << co;
co->release();
}
cout << "extent END\n\n";
}
o->release();
}
else
cout << val << endl;
}
// [2]: getting only returned objects
cout << "\n###### Performing the same query using eyedb::OQLIterator "
"interface: getting only returned objects" << endl;
eyedb::OQLIterator iter2(&db, argv[2]);
eyedb::Object *o;
while (iter2.next(o)) {
cout << o;
o->release();
}
// committing the transaction
db.transactionCommit();
}
catch(eyedb::Exception &e) {
cerr << argv[0] << ": " << e;
eyedb::release();
return 1;
}
eyedb::release();
return 0;
}
For instance:
./query person "select Person" ./query EYEDBDBM "select class"
// examples/C++Binding/generic/store/store.cc
#include <eyedb/eyedb.h>
using namespace std;
int
main(int argc, char *argv[])
{
eyedb::init(argc, argv);
if (argc != 4) {
fprintf(stderr, "usage: %s <dbname> <person_name> <person_age>\n",
argv[0]);
return 1;
}
eyedb::Exception::setMode(eyedb::Exception::ExceptionMode);
try {
eyedb::Connection conn;
// connecting to the eyedb server
conn.open();
eyedb::Database db(argv[1]);
// opening database argv[1]
db.open(&conn, eyedb::Database::DBRW);
// beginning a transaction
db.transactionBegin();
// looking for class 'Person'
eyedb::Class *personClass = db.getSchema()->getClass("Person");
// looking for the attribute 'name' and 'age' in the class 'Person'
const eyedb::Attribute *name_attr = personClass->getAttribute("name");
if (!name_attr) {
fprintf(stderr, "cannot find name attribute in database\n");
return 1;
}
const eyedb::Attribute *age_attr = personClass->getAttribute("age");
if (!age_attr) {
fprintf(stderr, "cannot find age attribute in database\n");
return 1;
}
// instanciating a 'Person' object
eyedb::Object *p = personClass->newObj(&db);
// setting the name argv[2] to the new Person instance
name_attr->setSize(p, strlen(argv[2])+1);
name_attr->setValue(p, (eyedb::Data)argv[2], strlen(argv[2])+1, 0);
// setting the age argv[3] to the new Person instance
int age = atoi(argv[3]);
age_attr->setValue(p, (eyedb::Data)&age, 1, 0);
p->store();
// committing the transaction
db.transactionCommit();
}
catch(eyedb::Exception &e) {
cerr << e;
eyedb::release();
return 1;
}
eyedb::release();
return 0;
}
For instance:
./store person john 32 ./store person mary 28
// examples/C++Binding/schema-oriented/query/query.cc
#include "person.h"
using namespace std;
int
main(int argc, char *argv[])
{
eyedb::init(argc, argv);
person::init();
if (argc != 3) {
fprintf(stderr, "usage: %s <dbname> <query>\n", argv[0]);
return 1;
}
eyedb::Exception::setMode(eyedb::Exception::ExceptionMode);
try {
eyedb::Connection conn;
// connecting to the eyedb server
conn.open();
// opening database argv[1] using 'personDatabase' class
personDatabase db(argv[1]);
db.open(&conn, eyedb::Database::DBRW);
// beginning a transaction
db.transactionBegin();
// performing the OQL query argv[2]
eyedb::OQL q(&db, argv[2]);
eyedb::ObjectArray arr;
q.execute(arr);
// for each Person returned in the query, display its name and age,
// its address, its spouse name and age and its cars
for (int i = 0; i < arr.getCount(); i++) {
Person *p = Person_c(arr[i]);
if (p) {
cout << "name: " << p->getName() << endl;
cout << "age: " << p->getAge() << endl;
if (p->getAddr()->getStreet().size())
cout << "street: " << p->getAddr()->getStreet() << endl;
if (p->getAddr()->getTown().size())
cout << "town: " << p->getAddr()->getTown() << endl;
if (p->getSpouse()) {
cout << "spouse_name: " << p->getSpouse()->getName() << endl;
cout << "spouse_age: " << p->getSpouse()->getAge() << endl;
}
eyedb::CollectionIterator iter(p->getCarsColl());
Car *car;
while (iter.next((eyedb::Object *&)car)) {
cout << "car: #" << i << ": " <<
car->getBrand() << ";" <<
car->getNum() << endl;
}
}
}
// committing the transaction
db.transactionCommit();
}
catch(eyedb::Exception &e) {
cerr << argv[0] << ": " << e;
eyedb::release();
return 1;
}
eyedb::release();
return 0;
}
For instance:
./query person "select Person"
// examples/C++Binding/schema-oriented/store/store.cc
#include "person.h"
int
main(int argc, char *argv[])
{
// initializing the EyeDB layer
eyedb::init(argc, argv);
// initializing the person package
person::init();
if (argc != 5) {
fprintf(stderr, "usage: %s <dbname> <person name> <person age> "
"<spouse name>\n", argv[0]);
return 1;
}
const char *dbname = argv[1];
const char *name = argv[2];
int age = atoi(argv[3]);
const char *spouse_name = argv[4];
eyedb::Exception::setMode(eyedb::Exception::ExceptionMode);
try {
eyedb::Connection conn;
// connecting to the EyeDB server
conn.open();
// opening database dbname using 'personDatabase' class
personDatabase db(dbname);
db.open(&conn, eyedb::Database::DBRW);
// beginning a transaction
db.transactionBegin();
// first looking for spouse
eyedb::OQL q(&db, "select Person.name = \"%s\"", spouse_name);
eyedb::ObjectArray arr;
q.execute(arr);
// if not found, returns an error
if (!arr.getCount()) {
fprintf(stderr, "cannot find spouse '%s'\n", spouse_name);
return 1;
}
// (safe!) casting returned object
Person *spouse = Person_c(arr[0]);
// creating a Person
Person *p = new Person(&db);
p->setCstate(Sir);
p->setName(name);
p->setAge(age);
p->setSpouse(spouse);
// spouse is no more necessary: releasing it
spouse->release();
p->getAddr()->setStreet("voltaire");
p->getAddr()->setTown("paris");
// creating two cars
Car *car1 = new Car(&db);
car1->setBrand("renault");
car1->setNum(18374);
Car *car2 = new Car(&db);
car2->setBrand("ford");
car2->setNum(233491);
// adding the cars to the created person
p->addToCarsColl(car1);
p->addToCarsColl(car2);
// car pointers are no more necessary: releasing them
car1->release();
car2->release();
// creating ten children
for (int i = 0; i < 10; i++) {
Person *c = new Person(&db);
char tmp[64];
c->setAge(i);
sprintf( tmp, "%d", i);
c->setName( (std::string(name) + std::string("_") + std::string(tmp)).c_str() );
p->setInChildrenCollAt(i, c);
c->release();
}
// storing all in database
p->store(eyedb::RecMode::FullRecurs);
// committing the transaction
db.transactionCommit();
// releasing p
p->release();
}
catch(eyedb::Exception &e) {
std::cerr << argv[0] << ": " << e;
eyedb::release();
return 1;
}
// releasing the EyeDB layer: this is not mandatory
eyedb::release();
return 0;
}
./store person wayne 34 ./store person poppins 51
// examples/C++Binding/schema-oriented/admin/admin.cc
#include <eyedb/eyedb.h>
using namespace std;
static const char *
get_string_mode(eyedb::DBAccessMode mode)
{
if (mode == eyedb::NoDBAccessMode)
return "eyedb::NoDBAccessMode";
if (mode == eyedb::ReadDBAccessMode)
return "eyedb::ReadDBAccessMode";
if (mode == eyedb::WriteDBAccessMode)
return "eyedb::WriteDBAccessMode";
if (mode == eyedb::ExecDBAccessMode)
return "eyedb::ExecDBAccessMode";
if (mode == eyedb::ReadWriteDBAccessMode)
return "eyedb::ReadWriteDBAccessMode";
if (mode == eyedb::ReadExecDBAccessMode)
return "eyedb::ReadExecDBAccessMode";
if (mode == eyedb::ReadWriteExecDBAccessMode)
return "eyedb::ReadWriteExecDBAccessMode";
if (mode == eyedb::AdminDBAccessMode)
return "eyedb::AdminDBAccessMode";
return NULL;
}
int
main(int argc, char *argv[])
{
// initializing the eyedb layer
eyedb::init(argc, argv);
eyedb::Exception::setMode(eyedb::Exception::ExceptionMode);
try {
eyedb::Connection conn;
// connecting to the eyedb server
conn.open();
// opening the database EYEDBDBM using 'dbmDataBase' class
eyedb::DBMDatabase db("EYEDBDBM");
db.open(&conn, eyedb::Database::DBRead);
// beginning a transaction
db.transactionBegin();
// display the scheme on stdout
cout << db.getSchema() << endl;
// looking for all user
eyedb::OQL q_user(&db, "select User");
eyedb::ObjectArray user_arr;
q_user.execute(user_arr);
cout << "User List {" << endl;
for (int i = 0; i < user_arr.getCount(); i++) {
eyedb::UserEntry *user = (eyedb::UserEntry *)user_arr[i];
cout << "\t" << user->name() << endl;
}
cout << "}\n" << endl;
// looking for all database entry
eyedb::OQL q_db(&db, "select eyedb::DBEntry");
eyedb::ObjectArray db_arr;
q_db.execute(db_arr);
cout << "Database List {" << endl;
for (int i = 0; i < db_arr.getCount(); i++) {
eyedb::DBEntry *dbentry = (eyedb::DBEntry *)db_arr[i];
cout << "\t" << dbentry->dbname() << " -> " << dbentry->dbfile() << endl;
// looking for all user which has any permission on this
// database
eyedb::OQL q_useraccess(&db,
"select eyedb::DBUserAccess->dbentry->dbname = \"%s\"",
dbentry->dbname().c_str());
eyedb::ObjectArray useraccess_arr;
q_useraccess.execute(useraccess_arr);
if (useraccess_arr.getCount()) {
cout << "\tUser Access {" << endl;
for (int j = 0; j < useraccess_arr.getCount(); j++) {
eyedb::DBUserAccess *ua = (eyedb::DBUserAccess *)useraccess_arr[j];
cout << "\t\t" << ua->user()->name() << " -> " <<
get_string_mode(ua->mode()) << endl;
}
cout << "\t}" << endl;
}
cout << endl;
useraccess_arr.garbage();
}
cout << "}" << endl;
// releasing runtime pointers
db_arr.garbage();
user_arr.garbage();
}
catch(eyedb::Exception &e) {
cerr << argv[0] << ": " << e;
eyedb::release();
return 1;
}
// releasing the eyedb layer: this is not mandatory
eyedb::release();
return 0;
}
EyeDB manual