LiteSQL uses XML database definition file which is used to generate Persistent and Relation classes.
litesql-gen is the code generator written in C++ that generates code from database definition file.
The example database definition file (exampledatabase.xml) page will result in two files (exampledatabase.hpp and testdatabase.cpp) when fed to code generator.
exampledatabase.hpp contains class declarations for persistent objects and relations
exampledatabase.cpp contains implementation of methods and lots of static data.
#include "exampledatabase.hpp"
namespace example {
: person2(PersonPersonRelationMother::Person2), person1(PersonPersonRelationMother::Person1) {
switch(rec.size()) {
case 2:
person2 = rec[1];
case 1:
person1 = rec[0];
}
}
const std::string PersonPersonRelationMother::table__("Person_Person_Mother");
const litesql::FieldType PersonPersonRelationMother::Person1(
"Person1",
"INTEGER",table__);
const litesql::FieldType PersonPersonRelationMother::Person2(
"Person2",
"INTEGER",table__);
void PersonPersonRelationMother::link(
const litesql::Database& db,
const example::Person& o0,
const example::Person& o1) {
fields.push_back(Person1.name());
values.push_back(o0.id);
fields.push_back(Person2.name());
values.push_back(o1.id);
db.
insert(table__, values, fields);
}
void PersonPersonRelationMother::unlink(
const litesql::Database& db,
const example::Person& o0,
const example::Person& o1) {
db.
delete_(table__, (Person1 == o0.id && Person2 == o1.id));
}
}
sel.result(Person1.fullName());
sel.result(Person2.fullName());
sel.source(table__);
sel.where(expr);
}
sel.source(table__);
sel.result(Person1.fullName());
sel.where(srcExpr);
}
sel.source(table__);
sel.result(Person2.fullName());
sel.where(srcExpr);
}
: person2(PersonPersonRelationFather::Person2), person1(PersonPersonRelationFather::Person1) {
switch(rec.size()) {
case 2:
person2 = rec[1];
case 1:
person1 = rec[0];
}
}
const std::string PersonPersonRelationFather::table__("Person_Person_Father");
const litesql::FieldType PersonPersonRelationFather::Person1(
"Person1",
"INTEGER",table__);
const litesql::FieldType PersonPersonRelationFather::Person2(
"Person2",
"INTEGER",table__);
void PersonPersonRelationFather::link(
const litesql::Database& db,
const example::Person& o0,
const example::Person& o1) {
fields.push_back(Person1.name());
values.push_back(o0.id);
fields.push_back(Person2.name());
values.push_back(o1.id);
db.
insert(table__, values, fields);
}
void PersonPersonRelationFather::unlink(
const litesql::Database& db,
const example::Person& o0,
const example::Person& o1) {
db.
delete_(table__, (Person1 == o0.id && Person2 == o1.id));
}
}
sel.result(Person1.fullName());
sel.result(Person2.fullName());
sel.source(table__);
sel.where(expr);
}
sel.source(table__);
sel.result(Person1.fullName());
sel.where(srcExpr);
}
sel.source(table__);
sel.result(Person2.fullName());
sel.where(srcExpr);
}
: person2(PersonPersonRelationSiblings::Person2), person1(PersonPersonRelationSiblings::Person1) {
switch(rec.size()) {
case 2:
person2 = rec[1];
case 1:
person1 = rec[0];
}
}
const std::string PersonPersonRelationSiblings::table__("Person_Person_Siblings");
const litesql::FieldType PersonPersonRelationSiblings::Person1(
"Person1",
"INTEGER",table__);
const litesql::FieldType PersonPersonRelationSiblings::Person2(
"Person2",
"INTEGER",table__);
void PersonPersonRelationSiblings::link(
const litesql::Database& db,
const example::Person& o0,
const example::Person& o1) {
fields.push_back(Person1.name());
values.push_back(o0.id);
fields.push_back(Person2.name());
values.push_back(o1.id);
db.
insert(table__, values, fields);
fields.clear();
values.clear();
fields.push_back(Person1.name());
values.push_back(o1.id);
fields.push_back(Person2.name());
values.push_back(o0.id);
db.
insert(table__, values, fields);
}
void PersonPersonRelationSiblings::unlink(
const litesql::Database& db,
const example::Person& o0,
const example::Person& o1) {
db.
delete_(table__, (Person1 == o0.id && Person2 == o1.id));
db.
delete_(table__, (Person1 == o1.id && Person2 == o0.id));
}
}
sel.result(Person1.fullName());
sel.result(Person2.fullName());
sel.source(table__);
sel.where(expr);
}
sel.source(table__);
sel.result(Person1.fullName());
sel.where(srcExpr);
}
sel.source(table__);
sel.result(Person2.fullName());
sel.where(srcExpr);
}
: person2(PersonPersonRelationChildren::Person2), person1(PersonPersonRelationChildren::Person1) {
switch(rec.size()) {
case 2:
person2 = rec[1];
case 1:
person1 = rec[0];
}
}
const std::string PersonPersonRelationChildren::table__("Person_Person_Children");
const litesql::FieldType PersonPersonRelationChildren::Person1(
"Person1",
"INTEGER",table__);
const litesql::FieldType PersonPersonRelationChildren::Person2(
"Person2",
"INTEGER",table__);
void PersonPersonRelationChildren::link(
const litesql::Database& db,
const example::Person& o0,
const example::Person& o1) {
fields.push_back(Person1.name());
values.push_back(o0.id);
fields.push_back(Person2.name());
values.push_back(o1.id);
db.
insert(table__, values, fields);
}
void PersonPersonRelationChildren::unlink(
const litesql::Database& db,
const example::Person& o0,
const example::Person& o1) {
db.
delete_(table__, (Person1 == o0.id && Person2 == o1.id));
}
}
sel.result(Person1.fullName());
sel.result(Person2.fullName());
sel.source(table__);
sel.where(expr);
}
sel.source(table__);
sel.result(Person1.fullName());
sel.where(srcExpr);
}
sel.source(table__);
sel.result(Person2.fullName());
sel.where(srcExpr);
}
: role(RoleRelation::Role), person(RoleRelation::Person) {
switch(rec.size()) {
case 2:
role = rec[1];
case 1:
person = rec[0];
}
}
const std::string RoleRelation::table__("Person_Role_Roles");
void RoleRelation::link(
const litesql::Database& db,
const example::Person& o0,
const example::Role& o1) {
fields.push_back(Person.name());
values.push_back(o0.id);
fields.push_back(Role.name());
values.push_back(o1.id);
db.
insert(table__, values, fields);
}
void RoleRelation::unlink(
const litesql::Database& db,
const example::Person& o0,
const example::Role& o1) {
db.
delete_(table__, (Person == o0.id && Role == o1.id));
}
}
sel.result(Person.fullName());
sel.result(Role.fullName());
sel.source(table__);
sel.where(expr);
}
sel.source(table__);
sel.result(Person.fullName());
sel.where(srcExpr);
}
sel.source(table__);
sel.result(Role.fullName());
sel.where(srcExpr);
}
: student(SchoolStudentRelation::Student), school(SchoolStudentRelation::School) {
switch(rec.size()) {
case 2:
student = rec[1];
case 1:
school = rec[0];
}
}
const std::string SchoolStudentRelation::table__("School_Student_");
void SchoolStudentRelation::link(
const litesql::Database& db,
const example::School& o0,
const example::Student& o1) {
fields.push_back(School.name());
values.push_back(o0.id);
fields.push_back(Student.name());
values.push_back(o1.id);
db.
insert(table__, values, fields);
}
void SchoolStudentRelation::unlink(
const litesql::Database& db,
const example::School& o0,
const example::Student& o1) {
db.
delete_(table__, (School == o0.id && Student == o1.id));
}
}
sel.result(School.fullName());
sel.result(Student.fullName());
sel.source(table__);
sel.where(expr);
}
sel.source(table__);
sel.result(School.fullName());
sel.where(srcExpr);
}
sel.source(table__);
sel.result(Student.fullName());
sel.where(srcExpr);
}
: office(EmployeeOfficeRelation::Office), employee(EmployeeOfficeRelation::Employee) {
switch(rec.size()) {
case 2:
office = rec[1];
case 1:
employee = rec[0];
}
}
const std::string EmployeeOfficeRelation::table__("Employee_Office_");
void EmployeeOfficeRelation::link(
const litesql::Database& db,
const example::Employee& o0,
const example::Office& o1) {
fields.push_back(Employee.name());
values.push_back(o0.id);
fields.push_back(Office.name());
values.push_back(o1.id);
db.
insert(table__, values, fields);
}
void EmployeeOfficeRelation::unlink(
const litesql::Database& db,
const example::Employee& o0,
const example::Office& o1) {
db.
delete_(table__, (Employee == o0.id && Office == o1.id));
}
}
sel.result(Employee.fullName());
sel.result(Office.fullName());
sel.source(table__);
sel.where(expr);
}
sel.source(table__);
sel.result(Employee.fullName());
sel.where(srcExpr);
}
sel.source(table__);
sel.result(Office.fullName());
sel.where(srcExpr);
}
const std::string user::type__("user");
const std::string user::table__("user_");
const std::string user::sequence__("user_seq");
void user::defaults() {
id = 0;
}
defaults();
}
defaults();
size_t size = (rec.size() > 4) ? 4 : rec.size();
switch(size) {
case 4: passwd = convert<const std::string&, std::string>(rec[3]);
passwd.setModified(false);
case 3: name = convert<const std::string&, std::string>(rec[2]);
name.setModified(false);
case 2: type = convert<const std::string&, std::string>(rec[1]);
type.setModified(false);
case 1: id = convert<const std::string&, int>(rec[0]);
id.setModified(false);
}
}
user::user(const user& obj)
:
litesql::
Persistent(obj), id(obj.id), type(obj.type), name(obj.name), passwd(obj.passwd) {
}
const user& user::operator=(const user& obj) {
if (this != &obj) {
id = obj.id;
type = obj.type;
name = obj.name;
passwd = obj.passwd;
}
return *this;
}
tables.push_back(table__);
fields.push_back(id.name());
values.push_back(id);
id.setModified(false);
fields.push_back(type.name());
values.push_back(type);
type.setModified(false);
fields.push_back(name.name());
values.push_back(name);
name.setModified(false);
fields.push_back(passwd.name());
values.push_back(passwd);
passwd.setModified(false);
fieldRecs.push_back(fields);
valueRecs.push_back(values);
}
void user::create() {
type = type__;
std::string newID = insert(tables, fieldRecs, valueRecs);
if (id == 0)
id = newID;
}
void user::addUpdates(Updates& updates) {
prepareUpdate(updates, table__);
updateField(updates, table__, id);
updateField(updates, table__, type);
updateField(updates, table__, name);
updateField(updates, table__, passwd);
}
void user::addIDUpdates(Updates& updates) {
}
void user::getFieldTypes(std::vector<litesql::FieldType>& ftypes) {
ftypes.push_back(Id);
ftypes.push_back(Type);
ftypes.push_back(Name);
ftypes.push_back(Passwd);
}
void user::delRecord() {
deleteFromTable(table__, id);
}
void user::delRelations() {
}
void user::update() {
if (!inDatabase) {
create();
return;
}
Updates updates;
addUpdates(updates);
if (id != oldKey) {
if (!typeIsCorrect())
upcastCopy()->addIDUpdates(updates);
}
litesql::Persistent::update(updates);
oldKey = id;
}
void user::del() {
if (typeIsCorrect() == false) {
std::auto_ptr<user> p(upcastCopy());
p->delRelations();
p->onDelete();
p->delRecord();
} else {
onDelete();
delRecord();
}
inDatabase = false;
}
bool user::typeIsCorrect() {
return type == type__;
}
std::auto_ptr<user> user::upcast() {
return auto_ptr<user>(new user(*this));
}
std::auto_ptr<user> user::upcastCopy() {
user* np = new user(*this);
np->id = id;
np->type = type;
np->name = name;
np->passwd = passwd;
np->inDatabase = inDatabase;
return auto_ptr<user>(np);
}
std::ostream & operator<<(std::ostream& os, user o) {
os << "-------------------------------------" << std::endl;
os << o.id.name() << " = " << o.id << std::endl;
os << o.type.name() << " = " << o.type << std::endl;
os << o.name.name() << " = " << o.name << std::endl;
os << o.passwd.name() << " = " << o.passwd << std::endl;
os << "-------------------------------------" << std::endl;
return os;
}
const int Person::SexType::Male(0);
const int Person::SexType::Female(1);
Person::SexType::SexType(const std::string& n, const std::string& t, const std::string& tbl, const litesql::FieldType::Values& vals)
}
const int Person::Sex::Male(0);
const int Person::Sex::Female(1);
Person::MotherHandle::MotherHandle(const Person& owner)
}
void Person::MotherHandle::link(const Person& o0) {
PersonPersonRelationMother::link(owner->getDatabase(), *owner, o0);
}
void Person::MotherHandle::unlink(const Person& o0) {
PersonPersonRelationMother::unlink(owner->getDatabase(), *owner, o0);
}
PersonPersonRelationMother::del(owner->getDatabase(), expr && PersonPersonRelationMother::Person1 == owner->id);
}
return PersonPersonRelationMother::getPerson2(owner->getDatabase(), expr, (PersonPersonRelationMother::Person1 == owner->id) && srcExpr);
}
return PersonPersonRelationMother::getRows(owner->getDatabase(), expr && (PersonPersonRelationMother::Person1 == owner->id));
}
Person::FatherHandle::FatherHandle(const Person& owner)
}
void Person::FatherHandle::link(const Person& o0) {
PersonPersonRelationFather::link(owner->getDatabase(), *owner, o0);
}
void Person::FatherHandle::unlink(const Person& o0) {
PersonPersonRelationFather::unlink(owner->getDatabase(), *owner, o0);
}
PersonPersonRelationFather::del(owner->getDatabase(), expr && PersonPersonRelationFather::Person1 == owner->id);
}
return PersonPersonRelationFather::getPerson2(owner->getDatabase(), expr, (PersonPersonRelationFather::Person1 == owner->id) && srcExpr);
}
return PersonPersonRelationFather::getRows(owner->getDatabase(), expr && (PersonPersonRelationFather::Person1 == owner->id));
}
Person::SiblingsHandle::SiblingsHandle(const Person& owner)
}
void Person::SiblingsHandle::link(const Person& o0) {
PersonPersonRelationSiblings::link(owner->getDatabase(), *owner, o0);
}
void Person::SiblingsHandle::unlink(const Person& o0) {
PersonPersonRelationSiblings::unlink(owner->getDatabase(), *owner, o0);
}
PersonPersonRelationSiblings::del(owner->getDatabase(), expr && PersonPersonRelationSiblings::Person1 == owner->id);
}
return PersonPersonRelationSiblings::getPerson2(owner->getDatabase(), expr, (PersonPersonRelationSiblings::Person1 == owner->id) && srcExpr);
}
return PersonPersonRelationSiblings::getRows(owner->getDatabase(), expr && (PersonPersonRelationSiblings::Person1 == owner->id));
}
Person::ChildrenHandle::ChildrenHandle(const Person& owner)
}
void Person::ChildrenHandle::link(const Person& o0) {
PersonPersonRelationChildren::link(owner->getDatabase(), *owner, o0);
}
void Person::ChildrenHandle::unlink(const Person& o0) {
PersonPersonRelationChildren::unlink(owner->getDatabase(), *owner, o0);
}
PersonPersonRelationChildren::del(owner->getDatabase(), expr && PersonPersonRelationChildren::Person1 == owner->id);
}
return PersonPersonRelationChildren::getPerson2(owner->getDatabase(), expr, (PersonPersonRelationChildren::Person1 == owner->id) && srcExpr);
}
return PersonPersonRelationChildren::getRows(owner->getDatabase(), expr && (PersonPersonRelationChildren::Person1 == owner->id));
}
Person::RolesHandle::RolesHandle(const Person& owner)
}
void Person::RolesHandle::link(const Role& o0) {
RoleRelation::link(owner->getDatabase(), *owner, o0);
}
void Person::RolesHandle::unlink(const Role& o0) {
RoleRelation::unlink(owner->getDatabase(), *owner, o0);
}
RoleRelation::del(owner->getDatabase(), expr && RoleRelation::Person == owner->id);
}
return RoleRelation::get<Role>(owner->getDatabase(), expr, (RoleRelation::Person == owner->id) && srcExpr);
}
return RoleRelation::getRows(owner->getDatabase(), expr && (RoleRelation::Person == owner->id));
}
const std::string Person::type__("Person");
const std::string Person::table__("Person_");
const std::string Person::sequence__("Person_seq");
std::vector < std::pair< std::string, std::string > > Person::sex_values;
const Person::SexType Person::Sex("sex_","INTEGER",table__,sex_values);
void Person::initValues() {
sex_values.clear();
sex_values.push_back(make_pair<std::string, std::string>("Male","0"));
sex_values.push_back(make_pair<std::string, std::string>("Female","1"));
}
void Person::defaults() {
id = 0;
age = 15;
image = Blob::nil;
aDoubleValue = 0.0;
sex = 0;
}
:
litesql::
Persistent(db), id(Id), type(Type), name(Name), age(Age), image(Image), aDoubleValue(ADoubleValue), sex(Sex) {
defaults();
}
:
litesql::
Persistent(db, rec), id(Id), type(Type), name(Name), age(Age), image(Image), aDoubleValue(ADoubleValue), sex(Sex) {
defaults();
size_t size = (rec.size() > 7) ? 7 : rec.size();
switch(size) {
case 7: sex = convert<const std::string&, int>(rec[6]);
sex.setModified(false);
case 6: aDoubleValue = convert<const std::string&, double>(rec[5]);
aDoubleValue.setModified(false);
case 5: image = convert<const std::string&, litesql::Blob>(rec[4]);
image.setModified(false);
case 4: age = convert<const std::string&, int>(rec[3]);
age.setModified(false);
case 3: name = convert<const std::string&, std::string>(rec[2]);
name.setModified(false);
case 2: type = convert<const std::string&, std::string>(rec[1]);
type.setModified(false);
case 1: id = convert<const std::string&, int>(rec[0]);
id.setModified(false);
}
}
Person::Person(const Person& obj)
:
litesql::
Persistent(obj), id(obj.id), type(obj.type), name(obj.name), age(obj.age), image(obj.image), aDoubleValue(obj.aDoubleValue), sex(obj.sex) {
}
const Person& Person::operator=(const Person& obj) {
if (this != &obj) {
id = obj.id;
type = obj.type;
name = obj.name;
age = obj.age;
image = obj.image;
aDoubleValue = obj.aDoubleValue;
sex = obj.sex;
}
return *this;
}
Person::MotherHandle Person::mother() {
return Person::MotherHandle(*this);
}
Person::FatherHandle Person::father() {
return Person::FatherHandle(*this);
}
Person::SiblingsHandle Person::siblings() {
return Person::SiblingsHandle(*this);
}
Person::ChildrenHandle Person::children() {
return Person::ChildrenHandle(*this);
}
Person::RolesHandle Person::roles() {
return Person::RolesHandle(*this);
}
tables.push_back(table__);
fields.push_back(id.name());
values.push_back(id);
id.setModified(false);
fields.push_back(type.name());
values.push_back(type);
type.setModified(false);
fields.push_back(name.name());
values.push_back(name);
name.setModified(false);
fields.push_back(age.name());
values.push_back(age);
age.setModified(false);
fields.push_back(image.name());
values.push_back(image);
image.setModified(false);
fields.push_back(aDoubleValue.name());
values.push_back(aDoubleValue);
aDoubleValue.setModified(false);
fields.push_back(sex.name());
values.push_back(sex);
sex.setModified(false);
fieldRecs.push_back(fields);
valueRecs.push_back(values);
}
void Person::create() {
type = type__;
std::string newID = insert(tables, fieldRecs, valueRecs);
if (id == 0)
id = newID;
}
void Person::addUpdates(Updates& updates) {
prepareUpdate(updates, table__);
updateField(updates, table__, id);
updateField(updates, table__, type);
updateField(updates, table__, name);
updateField(updates, table__, age);
updateField(updates, table__, image);
updateField(updates, table__, aDoubleValue);
updateField(updates, table__, sex);
}
void Person::addIDUpdates(Updates& updates) {
}
void Person::getFieldTypes(std::vector<litesql::FieldType>& ftypes) {
ftypes.push_back(Id);
ftypes.push_back(Type);
ftypes.push_back(Name);
ftypes.push_back(Age);
ftypes.push_back(Image);
ftypes.push_back(ADoubleValue);
ftypes.push_back(Sex);
}
void Person::delRecord() {
deleteFromTable(table__, id);
}
void Person::delRelations() {
PersonPersonRelationMother::del(*db, (PersonPersonRelationMother::Person1 == id) || (PersonPersonRelationMother::Person2 == id));
PersonPersonRelationFather::del(*db, (PersonPersonRelationFather::Person1 == id) || (PersonPersonRelationFather::Person2 == id));
PersonPersonRelationSiblings::del(*db, (PersonPersonRelationSiblings::Person1 == id) || (PersonPersonRelationSiblings::Person2 == id));
PersonPersonRelationChildren::del(*db, (PersonPersonRelationChildren::Person1 == id) || (PersonPersonRelationChildren::Person2 == id));
RoleRelation::del(*db, (RoleRelation::Person == id));
}
void Person::update() {
if (!inDatabase) {
create();
return;
}
Updates updates;
addUpdates(updates);
if (id != oldKey) {
if (!typeIsCorrect())
upcastCopy()->addIDUpdates(updates);
}
litesql::Persistent::update(updates);
oldKey = id;
}
void Person::del() {
if (typeIsCorrect() == false) {
std::auto_ptr<Person> p(upcastCopy());
p->delRelations();
p->onDelete();
p->delRecord();
} else {
onDelete();
delRecord();
}
inDatabase = false;
}
bool Person::typeIsCorrect() {
return type == type__;
}
std::auto_ptr<Person> Person::upcast() {
return auto_ptr<Person>(new Person(*this));
}
std::auto_ptr<Person> Person::upcastCopy() {
Person* np = new Person(*this);
np->id = id;
np->type = type;
np->name = name;
np->age = age;
np->image = image;
np->aDoubleValue = aDoubleValue;
np->sex = sex;
np->inDatabase = inDatabase;
return auto_ptr<Person>(np);
}
std::ostream & operator<<(std::ostream& os, Person o) {
os << "-------------------------------------" << std::endl;
os << o.id.name() << " = " << o.id << std::endl;
os << o.type.name() << " = " << o.type << std::endl;
os << o.name.name() << " = " << o.name << std::endl;
os << o.age.name() << " = " << o.age << std::endl;
os << o.image.name() << " = " << o.image << std::endl;
os << o.aDoubleValue.name() << " = " << o.aDoubleValue << std::endl;
os << o.sex.name() << " = " << o.sex << std::endl;
os << "-------------------------------------" << std::endl;
return os;
}
Role::PersonHandle::PersonHandle(const Role& owner)
}
void Role::PersonHandle::link(const Person& o0) {
RoleRelation::link(owner->getDatabase(), o0, *owner);
}
void Role::PersonHandle::unlink(const Person& o0) {
RoleRelation::unlink(owner->getDatabase(), o0, *owner);
}
RoleRelation::del(owner->getDatabase(), expr && RoleRelation::Role == owner->id);
}
return RoleRelation::get<Person>(owner->getDatabase(), expr, (RoleRelation::Role == owner->id) && srcExpr);
}
return RoleRelation::getRows(owner->getDatabase(), expr && (RoleRelation::Role == owner->id));
}
const std::string Role::type__("Role");
const std::string Role::table__("Role_");
const std::string Role::sequence__("Role_seq");
void Role::defaults() {
id = 0;
}
defaults();
}
defaults();
size_t size = (rec.size() > 2) ? 2 : rec.size();
switch(size) {
case 2: type = convert<const std::string&, std::string>(rec[1]);
type.setModified(false);
case 1: id = convert<const std::string&, int>(rec[0]);
id.setModified(false);
}
}
Role::Role(const Role& obj)
}
const Role& Role::operator=(const Role& obj) {
if (this != &obj) {
id = obj.id;
type = obj.type;
}
return *this;
}
Role::PersonHandle Role::person() {
return Role::PersonHandle(*this);
}
tables.push_back(table__);
fields.push_back(id.name());
values.push_back(id);
id.setModified(false);
fields.push_back(type.name());
values.push_back(type);
type.setModified(false);
fieldRecs.push_back(fields);
valueRecs.push_back(values);
}
void Role::create() {
type = type__;
std::string newID = insert(tables, fieldRecs, valueRecs);
if (id == 0)
id = newID;
}
void Role::addUpdates(Updates& updates) {
prepareUpdate(updates, table__);
updateField(updates, table__, id);
updateField(updates, table__, type);
}
void Role::addIDUpdates(Updates& updates) {
}
void Role::getFieldTypes(std::vector<litesql::FieldType>& ftypes) {
ftypes.push_back(Id);
ftypes.push_back(Type);
}
void Role::delRecord() {
deleteFromTable(table__, id);
}
void Role::delRelations() {
RoleRelation::del(*db, (RoleRelation::Role == id));
}
void Role::update() {
if (!inDatabase) {
create();
return;
}
Updates updates;
addUpdates(updates);
if (id != oldKey) {
if (!typeIsCorrect())
upcastCopy()->addIDUpdates(updates);
}
litesql::Persistent::update(updates);
oldKey = id;
}
void Role::del() {
if (typeIsCorrect() == false) {
std::auto_ptr<Role> p(upcastCopy());
p->delRelations();
p->onDelete();
p->delRecord();
} else {
onDelete();
delRecord();
}
inDatabase = false;
}
bool Role::typeIsCorrect() {
return type == type__;
}
std::auto_ptr<Role> Role::upcast() {
if (type == Student::type__)
return auto_ptr<Role>(new Student(select<Student>(*db, Id == id).one()));
if (type == Employee::type__)
return auto_ptr<Role>(new Employee(select<Employee>(*db, Id == id).one()));
return auto_ptr<Role>(new Role(*this));
}
std::auto_ptr<Role> Role::upcastCopy() {
Role* np = NULL;
if (type == "Student")
np = new Student(*db);
if (type == "Employee")
np = new Employee(*db);
np->id = id;
np->type = type;
np->inDatabase = inDatabase;
return auto_ptr<Role>(np);
}
std::ostream & operator<<(std::ostream& os, Role o) {
os << "-------------------------------------" << std::endl;
os << o.id.name() << " = " << o.id << std::endl;
os << o.type.name() << " = " << o.type << std::endl;
os << "-------------------------------------" << std::endl;
return os;
}
Student::SchoolHandle::SchoolHandle(const Student& owner)
}
void Student::SchoolHandle::link(const School& o0) {
SchoolStudentRelation::link(owner->getDatabase(), o0, *owner);
}
void Student::SchoolHandle::unlink(const School& o0) {
SchoolStudentRelation::unlink(owner->getDatabase(), o0, *owner);
}
SchoolStudentRelation::del(owner->getDatabase(), expr && SchoolStudentRelation::Student == owner->id);
}
return SchoolStudentRelation::get<School>(owner->getDatabase(), expr, (SchoolStudentRelation::Student == owner->id) && srcExpr);
}
return SchoolStudentRelation::getRows(owner->getDatabase(), expr && (SchoolStudentRelation::Student == owner->id));
}
const std::string Student::type__("Student");
const std::string Student::table__("Student_");
: Role(db) {
}
: Role(db, rec) {
}
Student::Student(const Student& obj)
: Role(obj) {
}
const Student& Student::operator=(const Student& obj) {
Role::operator=(obj);
return *this;
}
Student::SchoolHandle Student::school() {
return Student::SchoolHandle(*this);
}
tables.push_back(table__);
fields.push_back("id_");
values.push_back(id);
fieldRecs.push_back(fields);
valueRecs.push_back(values);
return Role::insert(tables, fieldRecs, valueRecs);
}
void Student::create() {
type = type__;
std::string newID = insert(tables, fieldRecs, valueRecs);
if (id == 0)
id = newID;
}
void Student::addUpdates(Updates& updates) {
prepareUpdate(updates, table__);
Role::addUpdates(updates);
}
void Student::addIDUpdates(Updates& updates) {
prepareUpdate(updates, table__);
updateField(updates, table__, id);
}
void Student::getFieldTypes(std::vector<litesql::FieldType>& ftypes) {
Role::getFieldTypes(ftypes);
}
void Student::delRecord() {
deleteFromTable(table__, id);
Role::delRecord();
}
void Student::delRelations() {
SchoolStudentRelation::del(*db, (SchoolStudentRelation::Student == id));
}
void Student::update() {
if (!inDatabase) {
create();
return;
}
Updates updates;
addUpdates(updates);
if (id != oldKey) {
if (!typeIsCorrect())
upcastCopy()->addIDUpdates(updates);
else
addIDUpdates(updates);
}
litesql::Persistent::update(updates);
oldKey = id;
}
void Student::del() {
if (typeIsCorrect() == false) {
std::auto_ptr<Student> p(upcastCopy());
p->delRelations();
p->onDelete();
p->delRecord();
} else {
onDelete();
delRecord();
}
inDatabase = false;
}
bool Student::typeIsCorrect() {
return type == type__;
}
std::auto_ptr<Student> Student::upcast() {
return auto_ptr<Student>(new Student(*this));
}
std::auto_ptr<Student> Student::upcastCopy() {
Student* np = new Student(*this);
np->inDatabase = inDatabase;
return auto_ptr<Student>(np);
}
std::ostream & operator<<(std::ostream& os, Student o) {
os << "-------------------------------------" << std::endl;
os << o.id.name() << " = " << o.id << std::endl;
os << o.type.name() << " = " << o.type << std::endl;
os << "-------------------------------------" << std::endl;
return os;
}
Employee::OfficeHandle::OfficeHandle(const Employee& owner)
}
void Employee::OfficeHandle::link(const Office& o0) {
EmployeeOfficeRelation::link(owner->getDatabase(), *owner, o0);
}
void Employee::OfficeHandle::unlink(const Office& o0) {
EmployeeOfficeRelation::unlink(owner->getDatabase(), *owner, o0);
}
EmployeeOfficeRelation::del(owner->getDatabase(), expr && EmployeeOfficeRelation::Employee == owner->id);
}
return EmployeeOfficeRelation::get<Office>(owner->getDatabase(), expr, (EmployeeOfficeRelation::Employee == owner->id) && srcExpr);
}
return EmployeeOfficeRelation::getRows(owner->getDatabase(), expr && (EmployeeOfficeRelation::Employee == owner->id));
}
const std::string Employee::type__("Employee");
const std::string Employee::table__("Employee_");
: Role(db) {
}
: Role(db, rec) {
}
Employee::Employee(const Employee& obj)
: Role(obj) {
}
const Employee& Employee::operator=(const Employee& obj) {
Role::operator=(obj);
return *this;
}
Employee::OfficeHandle Employee::office() {
return Employee::OfficeHandle(*this);
}
tables.push_back(table__);
fields.push_back("id_");
values.push_back(id);
fieldRecs.push_back(fields);
valueRecs.push_back(values);
return Role::insert(tables, fieldRecs, valueRecs);
}
void Employee::create() {
type = type__;
std::string newID = insert(tables, fieldRecs, valueRecs);
if (id == 0)
id = newID;
}
void Employee::addUpdates(Updates& updates) {
prepareUpdate(updates, table__);
Role::addUpdates(updates);
}
void Employee::addIDUpdates(Updates& updates) {
prepareUpdate(updates, table__);
updateField(updates, table__, id);
}
void Employee::getFieldTypes(std::vector<litesql::FieldType>& ftypes) {
Role::getFieldTypes(ftypes);
}
void Employee::delRecord() {
deleteFromTable(table__, id);
Role::delRecord();
}
void Employee::delRelations() {
EmployeeOfficeRelation::del(*db, (EmployeeOfficeRelation::Employee == id));
}
void Employee::update() {
if (!inDatabase) {
create();
return;
}
Updates updates;
addUpdates(updates);
if (id != oldKey) {
if (!typeIsCorrect())
upcastCopy()->addIDUpdates(updates);
else
addIDUpdates(updates);
}
litesql::Persistent::update(updates);
oldKey = id;
}
void Employee::del() {
if (typeIsCorrect() == false) {
std::auto_ptr<Employee> p(upcastCopy());
p->delRelations();
p->onDelete();
p->delRecord();
} else {
onDelete();
delRecord();
}
inDatabase = false;
}
bool Employee::typeIsCorrect() {
return type == type__;
}
std::auto_ptr<Employee> Employee::upcast() {
return auto_ptr<Employee>(new Employee(*this));
}
std::auto_ptr<Employee> Employee::upcastCopy() {
Employee* np = new Employee(*this);
np->inDatabase = inDatabase;
return auto_ptr<Employee>(np);
}
std::ostream & operator<<(std::ostream& os, Employee o) {
os << "-------------------------------------" << std::endl;
os << o.id.name() << " = " << o.id << std::endl;
os << o.type.name() << " = " << o.type << std::endl;
os << "-------------------------------------" << std::endl;
return os;
}
School::StudentsHandle::StudentsHandle(const School& owner)
}
void School::StudentsHandle::link(const Student& o0) {
SchoolStudentRelation::link(owner->getDatabase(), *owner, o0);
}
void School::StudentsHandle::unlink(const Student& o0) {
SchoolStudentRelation::unlink(owner->getDatabase(), *owner, o0);
}
SchoolStudentRelation::del(owner->getDatabase(), expr && SchoolStudentRelation::School == owner->id);
}
return SchoolStudentRelation::get<Student>(owner->getDatabase(), expr, (SchoolStudentRelation::School == owner->id) && srcExpr);
}
return SchoolStudentRelation::getRows(owner->getDatabase(), expr && (SchoolStudentRelation::School == owner->id));
}
const std::string School::type__("School");
const std::string School::table__("School_");
const std::string School::sequence__("School_seq");
void School::defaults() {
id = 0;
}
defaults();
}
defaults();
size_t size = (rec.size() > 3) ? 3 : rec.size();
switch(size) {
case 3: name = convert<const std::string&, std::string>(rec[2]);
name.setModified(false);
case 2: type = convert<const std::string&, std::string>(rec[1]);
type.setModified(false);
case 1: id = convert<const std::string&, int>(rec[0]);
id.setModified(false);
}
}
School::School(const School& obj)
}
const School& School::operator=(const School& obj) {
if (this != &obj) {
id = obj.id;
type = obj.type;
name = obj.name;
}
return *this;
}
School::StudentsHandle School::students() {
return School::StudentsHandle(*this);
}
tables.push_back(table__);
fields.push_back(id.name());
values.push_back(id);
id.setModified(false);
fields.push_back(type.name());
values.push_back(type);
type.setModified(false);
fields.push_back(name.name());
values.push_back(name);
name.setModified(false);
fieldRecs.push_back(fields);
valueRecs.push_back(values);
}
void School::create() {
type = type__;
std::string newID = insert(tables, fieldRecs, valueRecs);
if (id == 0)
id = newID;
}
void School::addUpdates(Updates& updates) {
prepareUpdate(updates, table__);
updateField(updates, table__, id);
updateField(updates, table__, type);
updateField(updates, table__, name);
}
void School::addIDUpdates(Updates& updates) {
}
void School::getFieldTypes(std::vector<litesql::FieldType>& ftypes) {
ftypes.push_back(Id);
ftypes.push_back(Type);
ftypes.push_back(Name);
}
void School::delRecord() {
deleteFromTable(table__, id);
}
void School::delRelations() {
SchoolStudentRelation::del(*db, (SchoolStudentRelation::School == id));
}
void School::update() {
if (!inDatabase) {
create();
return;
}
Updates updates;
addUpdates(updates);
if (id != oldKey) {
if (!typeIsCorrect())
upcastCopy()->addIDUpdates(updates);
}
litesql::Persistent::update(updates);
oldKey = id;
}
void School::del() {
if (typeIsCorrect() == false) {
std::auto_ptr<School> p(upcastCopy());
p->delRelations();
p->onDelete();
p->delRecord();
} else {
onDelete();
delRecord();
}
inDatabase = false;
}
bool School::typeIsCorrect() {
return type == type__;
}
std::auto_ptr<School> School::upcast() {
return auto_ptr<School>(new School(*this));
}
std::auto_ptr<School> School::upcastCopy() {
School* np = new School(*this);
np->id = id;
np->type = type;
np->name = name;
np->inDatabase = inDatabase;
return auto_ptr<School>(np);
}
std::ostream & operator<<(std::ostream& os, School o) {
os << "-------------------------------------" << std::endl;
os << o.id.name() << " = " << o.id << std::endl;
os << o.type.name() << " = " << o.type << std::endl;
os << o.name.name() << " = " << o.name << std::endl;
os << "-------------------------------------" << std::endl;
return os;
}
Office::EmployeesHandle::EmployeesHandle(const Office& owner)
}
void Office::EmployeesHandle::link(const Employee& o0) {
EmployeeOfficeRelation::link(owner->getDatabase(), o0, *owner);
}
void Office::EmployeesHandle::unlink(const Employee& o0) {
EmployeeOfficeRelation::unlink(owner->getDatabase(), o0, *owner);
}
EmployeeOfficeRelation::del(owner->getDatabase(), expr && EmployeeOfficeRelation::Office == owner->id);
}
return EmployeeOfficeRelation::get<Employee>(owner->getDatabase(), expr, (EmployeeOfficeRelation::Office == owner->id) && srcExpr);
}
return EmployeeOfficeRelation::getRows(owner->getDatabase(), expr && (EmployeeOfficeRelation::Office == owner->id));
}
const std::string Office::type__("Office");
const std::string Office::table__("Office_");
const std::string Office::sequence__("Office_seq");
void Office::defaults() {
id = 0;
}
defaults();
}
defaults();
size_t size = (rec.size() > 2) ? 2 : rec.size();
switch(size) {
case 2: type = convert<const std::string&, std::string>(rec[1]);
type.setModified(false);
case 1: id = convert<const std::string&, int>(rec[0]);
id.setModified(false);
}
}
Office::Office(const Office& obj)
}
const Office& Office::operator=(const Office& obj) {
if (this != &obj) {
id = obj.id;
type = obj.type;
}
return *this;
}
Office::EmployeesHandle Office::employees() {
return Office::EmployeesHandle(*this);
}
tables.push_back(table__);
fields.push_back(id.name());
values.push_back(id);
id.setModified(false);
fields.push_back(type.name());
values.push_back(type);
type.setModified(false);
fieldRecs.push_back(fields);
valueRecs.push_back(values);
}
void Office::create() {
type = type__;
std::string newID = insert(tables, fieldRecs, valueRecs);
if (id == 0)
id = newID;
}
void Office::addUpdates(Updates& updates) {
prepareUpdate(updates, table__);
updateField(updates, table__, id);
updateField(updates, table__, type);
}
void Office::addIDUpdates(Updates& updates) {
}
void Office::getFieldTypes(std::vector<litesql::FieldType>& ftypes) {
ftypes.push_back(Id);
ftypes.push_back(Type);
}
void Office::delRecord() {
deleteFromTable(table__, id);
}
void Office::delRelations() {
EmployeeOfficeRelation::del(*db, (EmployeeOfficeRelation::Office == id));
}
void Office::update() {
if (!inDatabase) {
create();
return;
}
Updates updates;
addUpdates(updates);
if (id != oldKey) {
if (!typeIsCorrect())
upcastCopy()->addIDUpdates(updates);
}
litesql::Persistent::update(updates);
oldKey = id;
}
void Office::del() {
if (typeIsCorrect() == false) {
std::auto_ptr<Office> p(upcastCopy());
p->delRelations();
p->onDelete();
p->delRecord();
} else {
onDelete();
delRecord();
}
inDatabase = false;
}
bool Office::typeIsCorrect() {
return type == type__;
}
std::auto_ptr<Office> Office::upcast() {
return auto_ptr<Office>(new Office(*this));
}
std::auto_ptr<Office> Office::upcastCopy() {
Office* np = new Office(*this);
np->id = id;
np->type = type;
np->inDatabase = inDatabase;
return auto_ptr<Office>(np);
}
std::ostream & operator<<(std::ostream& os, Office o) {
os << "-------------------------------------" << std::endl;
os << o.id.name() << " = " << o.id << std::endl;
os << o.type.name() << " = " << o.type << std::endl;
os << "-------------------------------------" << std::endl;
return os;
}
const std::string ThingWithMethods::type__("ThingWithMethods");
const std::string ThingWithMethods::table__("ThingWithMethods_");
const std::string ThingWithMethods::sequence__("ThingWithMethods_seq");
void ThingWithMethods::defaults() {
id = 0;
}
defaults();
}
defaults();
size_t size = (rec.size() > 2) ? 2 : rec.size();
switch(size) {
case 2: type = convert<const std::string&, std::string>(rec[1]);
type.setModified(false);
case 1: id = convert<const std::string&, int>(rec[0]);
id.setModified(false);
}
}
ThingWithMethods::ThingWithMethods(const ThingWithMethods& obj)
}
const ThingWithMethods& ThingWithMethods::operator=(const ThingWithMethods& obj) {
if (this != &obj) {
id = obj.id;
type = obj.type;
}
return *this;
}
tables.push_back(table__);
fields.push_back(id.name());
values.push_back(id);
id.setModified(false);
fields.push_back(type.name());
values.push_back(type);
type.setModified(false);
fieldRecs.push_back(fields);
valueRecs.push_back(values);
}
void ThingWithMethods::create() {
type = type__;
std::string newID = insert(tables, fieldRecs, valueRecs);
if (id == 0)
id = newID;
}
void ThingWithMethods::addUpdates(Updates& updates) {
prepareUpdate(updates, table__);
updateField(updates, table__, id);
updateField(updates, table__, type);
}
void ThingWithMethods::addIDUpdates(Updates& updates) {
}
void ThingWithMethods::getFieldTypes(std::vector<litesql::FieldType>& ftypes) {
ftypes.push_back(Id);
ftypes.push_back(Type);
}
void ThingWithMethods::delRecord() {
deleteFromTable(table__, id);
}
void ThingWithMethods::delRelations() {
}
void ThingWithMethods::update() {
if (!inDatabase) {
create();
return;
}
Updates updates;
addUpdates(updates);
if (id != oldKey) {
if (!typeIsCorrect())
upcastCopy()->addIDUpdates(updates);
}
litesql::Persistent::update(updates);
oldKey = id;
}
void ThingWithMethods::del() {
if (typeIsCorrect() == false) {
std::auto_ptr<ThingWithMethods> p(upcastCopy());
p->delRelations();
p->onDelete();
p->delRecord();
} else {
onDelete();
delRecord();
}
inDatabase = false;
}
bool ThingWithMethods::typeIsCorrect() {
return type == type__;
}
std::auto_ptr<ThingWithMethods> ThingWithMethods::upcast() {
return auto_ptr<ThingWithMethods>(new ThingWithMethods(*this));
}
std::auto_ptr<ThingWithMethods> ThingWithMethods::upcastCopy() {
ThingWithMethods* np = new ThingWithMethods(*this);
np->id = id;
np->type = type;
np->inDatabase = inDatabase;
return auto_ptr<ThingWithMethods>(np);
}
std::ostream & operator<<(std::ostream& os, ThingWithMethods o) {
os << "-------------------------------------" << std::endl;
os << o.id.name() << " = " << o.id << std::endl;
os << o.type.name() << " = " << o.type << std::endl;
os << "-------------------------------------" << std::endl;
return os;
}
ExampleDatabase::ExampleDatabase(std::string backendType, std::string connInfo)
initialize();
}
std::vector<litesql::Database::SchemaItem> ExampleDatabase::getSchema() const {
vector<Database::SchemaItem> res;
res.push_back(
Database::SchemaItem(
"schema_",
"table",
"CREATE TABLE schema_ (name_ TEXT, type_ TEXT, sql_ TEXT);"));
if (backend->supportsSequences()) {
res.push_back(
Database::SchemaItem(
"user_seq",
"sequence",
"CREATE SEQUENCE user_seq START 1 INCREMENT 1"));
res.push_back(
Database::SchemaItem(
"Person_seq",
"sequence",
"CREATE SEQUENCE Person_seq START 1 INCREMENT 1"));
res.push_back(
Database::SchemaItem(
"Role_seq",
"sequence",
"CREATE SEQUENCE Role_seq START 1 INCREMENT 1"));
res.push_back(
Database::SchemaItem(
"School_seq",
"sequence",
"CREATE SEQUENCE School_seq START 1 INCREMENT 1"));
res.push_back(
Database::SchemaItem(
"Office_seq",
"sequence",
"CREATE SEQUENCE Office_seq START 1 INCREMENT 1"));
res.push_back(
Database::SchemaItem(
"ThingWithMethods_seq",
"sequence",
"CREATE SEQUENCE ThingWithMethods_seq START 1 INCREMENT 1"));
}
res.push_back(
Database::SchemaItem(
"user_",
"table",
"CREATE TABLE user_ (id_ " + backend->getRowIDType() +
",type_ TEXT,name_ TEXT,passwd_ TEXT)"));
res.push_back(
Database::SchemaItem(
"Person_",
"table",
"CREATE TABLE Person_ (id_ " + backend->getRowIDType() +
",type_ TEXT,name_ TEXT,age_ INTEGER,image_ BLOB,aDoubleValue_ DOUBLE,sex_ INTEGER)"));
res.push_back(
Database::SchemaItem(
"Role_",
"table",
"CREATE TABLE Role_ (id_ " + backend->getRowIDType() +
",type_ TEXT)"));
res.push_back(
Database::SchemaItem(
"Student_",
"table",
"CREATE TABLE Student_ (id_ " + backend->getRowIDType() +
")"));
res.push_back(
Database::SchemaItem(
"Employee_",
"table",
"CREATE TABLE Employee_ (id_ " + backend->getRowIDType() +
")"));
res.push_back(
Database::SchemaItem(
"School_",
"table",
"CREATE TABLE School_ (id_ " + backend->getRowIDType() +
",type_ TEXT,name_ TEXT)"));
res.push_back(
Database::SchemaItem(
"Office_",
"table",
"CREATE TABLE Office_ (id_ " + backend->getRowIDType() +
",type_ TEXT)"));
res.push_back(
Database::SchemaItem(
"ThingWithMethods_",
"table",
"CREATE TABLE ThingWithMethods_ (id_ " + backend->getRowIDType() +
",type_ TEXT)"));
res.push_back(
Database::SchemaItem(
"Person_Person_Mother",
"table",
"CREATE TABLE Person_Person_Mother (Person1 INTEGER UNIQUE,Person2 INTEGER)"));
res.push_back(
Database::SchemaItem(
"Person_Person_Father",
"table",
"CREATE TABLE Person_Person_Father (Person1 INTEGER UNIQUE,Person2 INTEGER)"));
res.push_back(
Database::SchemaItem(
"Person_Person_Siblings",
"table",
"CREATE TABLE Person_Person_Siblings (Person1 INTEGER,Person2 INTEGER)"));
res.push_back(
Database::SchemaItem(
"Person_Person_Children",
"table",
"CREATE TABLE Person_Person_Children (Person1 INTEGER,Person2 INTEGER)"));
res.push_back(
Database::SchemaItem(
"Person_Role_Roles",
"table",
"CREATE TABLE Person_Role_Roles (Person1 INTEGER,Role2 INTEGER UNIQUE)"));
res.push_back(
Database::SchemaItem(
"School_Student_",
"table",
"CREATE TABLE School_Student_ (School1 INTEGER,Student2 INTEGER UNIQUE)"));
res.push_back(
Database::SchemaItem(
"Employee_Office_",
"table",
"CREATE TABLE Employee_Office_ (Employee1 INTEGER,Office2 INTEGER)"));
res.push_back(
Database::SchemaItem(
"Person__name_age_idx",
"index",
"CREATE UNIQUE INDEX Person__name_age_idx ON Person_ (name_,age_)"));
res.push_back(
Database::SchemaItem(
"Person_Person_MotherPerson1idx",
"index",
"CREATE INDEX Person_Person_MotherPerson1idx ON Person_Person_Mother (Person1)"));
res.push_back(
Database::SchemaItem(
"Person_Person_MotherPerson2idx",
"index",
"CREATE INDEX Person_Person_MotherPerson2idx ON Person_Person_Mother (Person2)"));
res.push_back(
Database::SchemaItem(
"Person_Person_Mother_all_idx",
"index",
"CREATE INDEX Person_Person_Mother_all_idx ON Person_Person_Mother (Person1,Person2)"));
res.push_back(
Database::SchemaItem(
"Person_Person_FatherPerson1idx",
"index",
"CREATE INDEX Person_Person_FatherPerson1idx ON Person_Person_Father (Person1)"));
res.push_back(
Database::SchemaItem(
"Person_Person_FatherPerson2idx",
"index",
"CREATE INDEX Person_Person_FatherPerson2idx ON Person_Person_Father (Person2)"));
res.push_back(
Database::SchemaItem(
"Person_Person_Father_all_idx",
"index",
"CREATE INDEX Person_Person_Father_all_idx ON Person_Person_Father (Person1,Person2)"));
res.push_back(
Database::SchemaItem(
"_fc4501d1c1e9cc173fbe356a08a9d12f",
"index",
"CREATE INDEX _fc4501d1c1e9cc173fbe356a08a9d12f ON Person_Person_Siblings (Person1)"));
res.push_back(
Database::SchemaItem(
"_29908e51ecc673e39c38238d4abe5b3b",
"index",
"CREATE INDEX _29908e51ecc673e39c38238d4abe5b3b ON Person_Person_Siblings (Person2)"));
res.push_back(
Database::SchemaItem(
"Person_Person_Siblings_all_idx",
"index",
"CREATE INDEX Person_Person_Siblings_all_idx ON Person_Person_Siblings (Person1,Person2)"));
res.push_back(
Database::SchemaItem(
"_c77a0c252bbee950ec06bda52dd09648",
"index",
"CREATE INDEX _c77a0c252bbee950ec06bda52dd09648 ON Person_Person_Children (Person1)"));
res.push_back(
Database::SchemaItem(
"_64f9014350ce47b5d0f7606b127df7c3",
"index",
"CREATE INDEX _64f9014350ce47b5d0f7606b127df7c3 ON Person_Person_Children (Person2)"));
res.push_back(
Database::SchemaItem(
"Person_Person_Children_all_idx",
"index",
"CREATE INDEX Person_Person_Children_all_idx ON Person_Person_Children (Person1,Person2)"));
res.push_back(
Database::SchemaItem(
"Person_Role_RolesPerson1idx",
"index",
"CREATE INDEX Person_Role_RolesPerson1idx ON Person_Role_Roles (Person1)"));
res.push_back(
Database::SchemaItem(
"Person_Role_RolesRole2idx",
"index",
"CREATE INDEX Person_Role_RolesRole2idx ON Person_Role_Roles (Role2)"));
res.push_back(
Database::SchemaItem(
"Person_Role_Roles_all_idx",
"index",
"CREATE INDEX Person_Role_Roles_all_idx ON Person_Role_Roles (Person1,Role2)"));
res.push_back(
Database::SchemaItem(
"School_Student_School1idx",
"index",
"CREATE INDEX School_Student_School1idx ON School_Student_ (School1)"));
res.push_back(
Database::SchemaItem(
"School_Student_Student2idx",
"index",
"CREATE INDEX School_Student_Student2idx ON School_Student_ (Student2)"));
res.push_back(
Database::SchemaItem(
"School_Student__all_idx",
"index",
"CREATE INDEX School_Student__all_idx ON School_Student_ (School1,Student2)"));
res.push_back(
Database::SchemaItem(
"Employee_Office_Employee1idx",
"index",
"CREATE INDEX Employee_Office_Employee1idx ON Employee_Office_ (Employee1)"));
res.push_back(
Database::SchemaItem(
"Employee_Office_Office2idx",
"index",
"CREATE INDEX Employee_Office_Office2idx ON Employee_Office_ (Office2)"));
res.push_back(
Database::SchemaItem(
"Employee_Office__all_idx",
"index",
"CREATE INDEX Employee_Office__all_idx ON Employee_Office_ (Employee1,Office2)"));
return res;
}
void ExampleDatabase::initialize() {
static bool initialized = false;
if (initialized)
return;
initialized = true;
Person::initValues();
}
}
Fields of multiple Persistent-classes are stored in separate tables. Each Persistent-class has its own table. Also, each relation is stored in separate table. An example database demonstrates data mapping.
The relation table AudioFile_Collection_ is indexed by both of its fields. If table or index identifier is longer than 31 characters, it will be truncated by calculating MD5-sum of the identifier because all backends do not support longer identifiers (certain PostgreSQL - versions at least).
Fields in tables of Persistent classes and relation attributes (extra fields in relation tables) are postfixed with an underscore to allow usage of SQL's reserved words in identifiers.
Using database defined in previous section, suppose one needs to select a Playable from database and play() it. The routine selecting and playing the Playable cannot know the actual type of the Playable.
After Playable-object is selected from database it really is a Playable despite it was stored as an AudioFile for example.
In order to retrieve all fields that AudioFile has, the Playable object needs to be upcasted.
The code generator (litesql-gen) writes upcast-method that returns object of correct type.
Sometimes extra fields that an inherited Persistent may have are not needed when accessing virtual methods. It would be futile to execute SQL statements. Another upcaster function is provided to help in this situation.
For example, onDelete() - may not need any fields. del() - method that calls onDelete() uses upcastCopy() to access onDelete() of inherited Persistent.
Note that upcastCopy will return a Persistent whose all fields are not retrieved from database.