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 {
using namespace litesql;
: 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_",A_field_type_integer,table__);
const litesql::FieldType PersonPersonRelationMother::Person2(
"Person2_",A_field_type_integer,table__);
void PersonPersonRelationMother::link(
const litesql::Database& db,
const example::Person& o0,
const example::Person& o1) {
Record values;
Record fields;
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));
}
}
SelectQuery sel;
sel.result(Person1.fullName());
sel.result(Person2.fullName());
sel.source(table__);
sel.where(expr);
return DataSource<PersonPersonRelationMother::Row>(db, sel);
}
SelectQuery sel;
sel.source(table__);
sel.result(Person1.fullName());
sel.where(srcExpr);
return DataSource<example::Person>(db, example::Person::Id.in(sel) && expr);
}
SelectQuery sel;
sel.source(table__);
sel.result(Person2.fullName());
sel.where(srcExpr);
return DataSource<example::Person>(db, example::Person::Id.in(sel) && expr);
}
: 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_",A_field_type_integer,table__);
const litesql::FieldType PersonPersonRelationFather::Person2(
"Person2_",A_field_type_integer,table__);
void PersonPersonRelationFather::link(
const litesql::Database& db,
const example::Person& o0,
const example::Person& o1) {
Record values;
Record fields;
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));
}
}
SelectQuery sel;
sel.result(Person1.fullName());
sel.result(Person2.fullName());
sel.source(table__);
sel.where(expr);
return DataSource<PersonPersonRelationFather::Row>(db, sel);
}
SelectQuery sel;
sel.source(table__);
sel.result(Person1.fullName());
sel.where(srcExpr);
return DataSource<example::Person>(db, example::Person::Id.in(sel) && expr);
}
SelectQuery sel;
sel.source(table__);
sel.result(Person2.fullName());
sel.where(srcExpr);
return DataSource<example::Person>(db, example::Person::Id.in(sel) && expr);
}
: 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_",A_field_type_integer,table__);
const litesql::FieldType PersonPersonRelationSiblings::Person2(
"Person2_",A_field_type_integer,table__);
void PersonPersonRelationSiblings::link(
const litesql::Database& db,
const example::Person& o0,
const example::Person& o1) {
Record values;
Record fields;
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));
}
}
SelectQuery sel;
sel.result(Person1.fullName());
sel.result(Person2.fullName());
sel.source(table__);
sel.where(expr);
return DataSource<PersonPersonRelationSiblings::Row>(db, sel);
}
SelectQuery sel;
sel.source(table__);
sel.result(Person1.fullName());
sel.where(srcExpr);
return DataSource<example::Person>(db, example::Person::Id.in(sel) && expr);
}
SelectQuery sel;
sel.source(table__);
sel.result(Person2.fullName());
sel.where(srcExpr);
return DataSource<example::Person>(db, example::Person::Id.in(sel) && expr);
}
: 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_",A_field_type_integer,table__);
const litesql::FieldType PersonPersonRelationChildren::Person2(
"Person2_",A_field_type_integer,table__);
void PersonPersonRelationChildren::link(
const litesql::Database& db,
const example::Person& o0,
const example::Person& o1) {
Record values;
Record fields;
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));
}
}
SelectQuery sel;
sel.result(Person1.fullName());
sel.result(Person2.fullName());
sel.source(table__);
sel.where(expr);
return DataSource<PersonPersonRelationChildren::Row>(db, sel);
}
SelectQuery sel;
sel.source(table__);
sel.result(Person1.fullName());
sel.where(srcExpr);
return DataSource<example::Person>(db, example::Person::Id.in(sel) && expr);
}
SelectQuery sel;
sel.source(table__);
sel.result(Person2.fullName());
sel.where(srcExpr);
return DataSource<example::Person>(db, example::Person::Id.in(sel) && expr);
}
: 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) {
Record values;
Record fields;
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));
}
}
SelectQuery sel;
sel.result(Person.fullName());
sel.result(Role.fullName());
sel.source(table__);
sel.where(expr);
return DataSource<RoleRelation::Row>(db, sel);
}
SelectQuery sel;
sel.source(table__);
sel.result(Person.fullName());
sel.where(srcExpr);
return DataSource<example::Person>(db, example::Person::Id.in(sel) && expr);
}
SelectQuery sel;
sel.source(table__);
sel.result(Role.fullName());
sel.where(srcExpr);
return DataSource<example::Role>(db, example::Role::Id.in(sel) && expr);
}
: 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_");
const litesql::FieldType SchoolStudentRelation::School(
"School1_",A_field_type_integer,table__);
const litesql::FieldType SchoolStudentRelation::Student(
"Student2_",A_field_type_integer,table__);
void SchoolStudentRelation::link(
const litesql::Database& db,
const example::School& o0,
const example::Student& o1) {
Record values;
Record fields;
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));
}
}
SelectQuery sel;
sel.result(School.fullName());
sel.result(Student.fullName());
sel.source(table__);
sel.where(expr);
return DataSource<SchoolStudentRelation::Row>(db, sel);
}
SelectQuery sel;
sel.source(table__);
sel.result(School.fullName());
sel.where(srcExpr);
return DataSource<example::School>(db, example::School::Id.in(sel) && expr);
}
SelectQuery sel;
sel.source(table__);
sel.result(Student.fullName());
sel.where(srcExpr);
return DataSource<example::Student>(db, example::Student::Id.in(sel) && expr);
}
: 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_");
const litesql::FieldType EmployeeOfficeRelation::Employee(
"Employee1_",A_field_type_integer,table__);
const litesql::FieldType EmployeeOfficeRelation::Office(
"Office2_",A_field_type_integer,table__);
void EmployeeOfficeRelation::link(
const litesql::Database& db,
const example::Employee& o0,
const example::Office& o1) {
Record values;
Record fields;
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));
}
}
SelectQuery sel;
sel.result(Employee.fullName());
sel.result(Office.fullName());
sel.source(table__);
sel.where(expr);
return DataSource<EmployeeOfficeRelation::Row>(db, sel);
}
SelectQuery sel;
sel.source(table__);
sel.result(Employee.fullName());
sel.where(srcExpr);
return DataSource<example::Employee>(db, example::Employee::Id.in(sel) && expr);
}
SelectQuery sel;
sel.source(table__);
sel.result(Office.fullName());
sel.where(srcExpr);
return DataSource<example::Office>(db, example::Office::Id.in(sel) && expr);
}
const std::string user::type__("user");
const std::string user::table__("user_");
const std::string user::sequence__("user_seq");
void user::initValues() {
}
void user::defaults() {
id = 0;
}
: litesql::Persistent(db), id(Id), type(Type), name(Name), passwd(Passwd) {
defaults();
}
: litesql::Persistent(db, rec), id(Id), type(Type), name(Name), passwd(Passwd) {
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()) {
std::unique_ptr<user> p(upcastCopy());
p->delRelations();
p->onDelete();
p->delRecord();
} else {
delRelations();
onDelete();
delRecord();
}
inDatabase = false;
}
bool user::typeIsCorrect() const {
return type == type__;
}
std::unique_ptr<user> user::upcast() const {
return unique_ptr<user>(new user(*this));
}
std::unique_ptr<user> user::upcastCopy() const {
user* np = new user(*this);
np->id = id;
np->type = type;
np->name = name;
np->passwd = passwd;
np->inDatabase = inDatabase;
return unique_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, AT_field_type t, const std::string& tbl, const litesql::FieldType::Values& vals)
: litesql::FieldType(n,t,tbl,vals) {
}
const int Person::Sex::Male(0);
const int Person::Sex::Female(1);
Person::MotherHandle::MotherHandle(const Person& owner)
: litesql::RelationHandle<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)
: litesql::RelationHandle<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)
: litesql::RelationHandle<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)
: litesql::RelationHandle<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)
: litesql::RelationHandle<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");
const litesql::FieldType Person::ADoubleValue(
"aDoubleValue_",A_field_type_double,table__);
std::vector < std::pair< std::string, std::string > > Person::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();
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()) {
std::unique_ptr<Person> p(upcastCopy());
p->delRelations();
p->onDelete();
p->delRecord();
} else {
delRelations();
onDelete();
delRecord();
}
inDatabase = false;
}
bool Person::typeIsCorrect() const {
return type == type__;
}
std::unique_ptr<Person> Person::upcast() const {
return unique_ptr<Person>(new Person(*this));
}
std::unique_ptr<Person> Person::upcastCopy() const {
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 unique_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)
: litesql::RelationHandle<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::initValues() {
}
void Role::defaults() {
id = 0;
}
: litesql::Persistent(db), id(Id), type(Type) {
defaults();
}
: litesql::Persistent(db, rec), id(Id), type(Type) {
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)
: litesql::Persistent(obj), id(obj.id), type(obj.type) {
}
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()) {
std::unique_ptr<Role> p(upcastCopy());
p->delRelations();
p->onDelete();
p->delRecord();
} else {
delRelations();
onDelete();
delRecord();
}
inDatabase = false;
}
bool Role::typeIsCorrect() const {
return type == type__;
}
std::unique_ptr<Role> Role::upcast() const {
if (type == Student::type__)
return unique_ptr<Role>(new Student(select<Student>(*db, Id == id).one()));
if (type == Employee::type__)
return unique_ptr<Role>(new Employee(select<Employee>(*db, Id == id).one()));
return unique_ptr<Role>(new Role(*this));
}
std::unique_ptr<Role> Role::upcastCopy() const {
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 unique_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)
: litesql::RelationHandle<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()) {
std::unique_ptr<Student> p(upcastCopy());
p->delRelations();
p->onDelete();
p->delRecord();
} else {
delRelations();
onDelete();
delRecord();
}
inDatabase = false;
}
bool Student::typeIsCorrect() const {
return type == type__;
}
std::unique_ptr<Student> Student::upcast() const {
return unique_ptr<Student>(new Student(*this));
}
std::unique_ptr<Student> Student::upcastCopy() const {
Student* np = new Student(*this);
np->inDatabase = inDatabase;
return unique_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)
: litesql::RelationHandle<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()) {
std::unique_ptr<Employee> p(upcastCopy());
p->delRelations();
p->onDelete();
p->delRecord();
} else {
delRelations();
onDelete();
delRecord();
}
inDatabase = false;
}
bool Employee::typeIsCorrect() const {
return type == type__;
}
std::unique_ptr<Employee> Employee::upcast() const {
return unique_ptr<Employee>(new Employee(*this));
}
std::unique_ptr<Employee> Employee::upcastCopy() const {
Employee* np = new Employee(*this);
np->inDatabase = inDatabase;
return unique_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)
: litesql::RelationHandle<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::initValues() {
}
void School::defaults() {
id = 0;
}
: litesql::Persistent(db), id(Id), type(Type), name(Name) {
defaults();
}
: litesql::Persistent(db, rec), id(Id), type(Type), name(Name) {
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)
: litesql::Persistent(obj), id(obj.id), type(obj.type), name(obj.name) {
}
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()) {
std::unique_ptr<School> p(upcastCopy());
p->delRelations();
p->onDelete();
p->delRecord();
} else {
delRelations();
onDelete();
delRecord();
}
inDatabase = false;
}
bool School::typeIsCorrect() const {
return type == type__;
}
std::unique_ptr<School> School::upcast() const {
return unique_ptr<School>(new School(*this));
}
std::unique_ptr<School> School::upcastCopy() const {
School* np = new School(*this);
np->id = id;
np->type = type;
np->name = name;
np->inDatabase = inDatabase;
return unique_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)
: litesql::RelationHandle<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::initValues() {
}
void Office::defaults() {
id = 0;
}
: litesql::Persistent(db), id(Id), type(Type) {
defaults();
}
: litesql::Persistent(db, rec), id(Id), type(Type) {
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)
: litesql::Persistent(obj), id(obj.id), type(obj.type) {
}
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()) {
std::unique_ptr<Office> p(upcastCopy());
p->delRelations();
p->onDelete();
p->delRecord();
} else {
delRelations();
onDelete();
delRecord();
}
inDatabase = false;
}
bool Office::typeIsCorrect() const {
return type == type__;
}
std::unique_ptr<Office> Office::upcast() const {
return unique_ptr<Office>(new Office(*this));
}
std::unique_ptr<Office> Office::upcastCopy() const {
Office* np = new Office(*this);
np->id = id;
np->type = type;
np->inDatabase = inDatabase;
return unique_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 litesql::FieldType ThingWithMethods::Own::Id(
"id_",A_field_type_integer,
"ThingWithMethods_");
const std::string ThingWithMethods::type__("ThingWithMethods");
const std::string ThingWithMethods::table__("ThingWithMethods_");
const std::string ThingWithMethods::sequence__("ThingWithMethods_seq");
void ThingWithMethods::initValues() {
}
void ThingWithMethods::defaults() {
id = 0;
}
: litesql::Persistent(db), id(Id), type(Type) {
defaults();
}
: litesql::Persistent(db, rec), id(Id), type(Type) {
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)
: litesql::Persistent(obj), id(obj.id), type(obj.type) {
}
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()) {
std::unique_ptr<ThingWithMethods> p(upcastCopy());
p->delRelations();
p->onDelete();
p->delRecord();
} else {
delRelations();
onDelete();
delRecord();
}
inDatabase = false;
}
bool ThingWithMethods::typeIsCorrect() const {
return type == type__;
}
std::unique_ptr<ThingWithMethods> ThingWithMethods::upcast() const {
return unique_ptr<ThingWithMethods>(new ThingWithMethods(*this));
}
std::unique_ptr<ThingWithMethods> ThingWithMethods::upcastCopy() const {
ThingWithMethods* np = new ThingWithMethods(*this);
np->id = id;
np->type = type;
np->inDatabase = inDatabase;
return unique_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( const std::string& backendType, const std::string& connInfo)
: litesql::Database(backendType, connInfo) {
initialize();
}
std::vector<litesql::Database::SchemaItem> ExampleDatabase::getSchema() const {
vector<Database::SchemaItem> res;
string TEXT = backend->getSQLType(A_field_type_string);
string rowIdType = backend->getRowIDType();
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",backend->getCreateSequenceSQL("user_seq")));
res.push_back(Database::SchemaItem("Person_seq","sequence",backend->getCreateSequenceSQL("Person_seq")));
res.push_back(Database::SchemaItem("Role_seq","sequence",backend->getCreateSequenceSQL("Role_seq")));
res.push_back(Database::SchemaItem("School_seq","sequence",backend->getCreateSequenceSQL("School_seq")));
res.push_back(Database::SchemaItem("Office_seq","sequence",backend->getCreateSequenceSQL("Office_seq")));
res.push_back(Database::SchemaItem("ThingWithMethods_seq","sequence",backend->getCreateSequenceSQL("ThingWithMethods_seq")));
}
res.push_back(Database::SchemaItem("user_","table","CREATE TABLE user_ (id_ " + rowIdType + ",type_ " + backend->getSQLType(A_field_type_string,"") + "" +",name_ " + backend->getSQLType(A_field_type_string,"256") + "" +",passwd_ " + backend->getSQLType(A_field_type_string,"") + "" +")"));
res.push_back(Database::SchemaItem("Person_","table","CREATE TABLE Person_ (id_ " + rowIdType + ",type_ " + backend->getSQLType(A_field_type_string,"") + "" +",name_ " + backend->getSQLType(A_field_type_string,"256") + "" +",age_ " + backend->getSQLType(A_field_type_integer,"") + "" +",image_ " + backend->getSQLType(A_field_type_blob,"") + "" +",aDoubleValue_ " + backend->getSQLType(A_field_type_double,"") + "" +",sex_ " + backend->getSQLType(A_field_type_integer,"") + "" +")"));
res.push_back(Database::SchemaItem("Role_","table","CREATE TABLE Role_ (id_ " + rowIdType + ",type_ " + backend->getSQLType(A_field_type_string,"") + "" +")"));
res.push_back(Database::SchemaItem("Student_","table","CREATE TABLE Student_ (id_ " + rowIdType + ")"));
res.push_back(Database::SchemaItem("Employee_","table","CREATE TABLE Employee_ (id_ " + rowIdType + ")"));
res.push_back(Database::SchemaItem("School_","table","CREATE TABLE School_ (id_ " + rowIdType + ",type_ " + backend->getSQLType(A_field_type_string,"") + "" +",name_ " + backend->getSQLType(A_field_type_string,"512") + "" +")"));
res.push_back(Database::SchemaItem("Office_","table","CREATE TABLE Office_ (id_ " + rowIdType + ",type_ " + backend->getSQLType(A_field_type_string,"") + "" +")"));
res.push_back(Database::SchemaItem("ThingWithMethods_","table","CREATE TABLE ThingWithMethods_ (id_ " + rowIdType + ",type_ " + backend->getSQLType(A_field_type_string,"") + "" +")"));
res.push_back(Database::SchemaItem("Person_Person_Mother","table","CREATE TABLE Person_Person_Mother (Person1_ " + backend->getSQLType(A_field_type_integer,"") + " UNIQUE" +",Person2_ " + backend->getSQLType(A_field_type_integer,"") + "" +")"));
res.push_back(Database::SchemaItem("Person_Person_Father","table","CREATE TABLE Person_Person_Father (Person1_ " + backend->getSQLType(A_field_type_integer,"") + " UNIQUE" +",Person2_ " + backend->getSQLType(A_field_type_integer,"") + "" +")"));
res.push_back(Database::SchemaItem("Person_Person_Siblings","table","CREATE TABLE Person_Person_Siblings (Person1_ " + backend->getSQLType(A_field_type_integer,"") + "" +",Person2_ " + backend->getSQLType(A_field_type_integer,"") + "" +")"));
res.push_back(Database::SchemaItem("Person_Person_Children","table","CREATE TABLE Person_Person_Children (Person1_ " + backend->getSQLType(A_field_type_integer,"") + "" +",Person2_ " + backend->getSQLType(A_field_type_integer,"") + "" +")"));
res.push_back(Database::SchemaItem("Person_Role_Roles","table","CREATE TABLE Person_Role_Roles (Person1_ " + backend->getSQLType(A_field_type_integer,"") + "" +",Role2_ " + backend->getSQLType(A_field_type_integer,"") + " UNIQUE" +")"));
res.push_back(Database::SchemaItem("School_Student_","table","CREATE TABLE School_Student_ (School1_ " + backend->getSQLType(A_field_type_integer,"") + "" +",Student2_ " + backend->getSQLType(A_field_type_integer,"") + " UNIQUE" +")"));
res.push_back(Database::SchemaItem("Employee_Office_","table","CREATE TABLE Employee_Office_ (Employee1_ " + backend->getSQLType(A_field_type_integer,"") + "" +",Office2_ " + backend->getSQLType(A_field_type_integer,"") + "" +")"));
res.push_back(Database::SchemaItem("user_id_idx","index","CREATE INDEX user_id_idx ON user_ (id_)"));
res.push_back(Database::SchemaItem("Person_id_idx","index","CREATE INDEX Person_id_idx ON Person_ (id_)"));
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("Role_id_idx","index","CREATE INDEX Role_id_idx ON Role_ (id_)"));
res.push_back(Database::SchemaItem("School_id_idx","index","CREATE INDEX School_id_idx ON School_ (id_)"));
res.push_back(Database::SchemaItem("Office_id_idx","index","CREATE INDEX Office_id_idx ON Office_ (id_)"));
res.push_back(Database::SchemaItem("ThingWithMethods_id_idx","index","CREATE INDEX ThingWithMethods_id_idx ON ThingWithMethods_ (id_)"));
res.push_back(Database::SchemaItem("Oee4eb2b8f280ef74d4d9b000ad762","index","CREATE INDEX Oee4eb2b8f280ef74d4d9b000ad762 ON Person_Person_Mother (Person1_)"));
res.push_back(Database::SchemaItem("O534e82725f31f0ed55507bb93876f","index","CREATE INDEX O534e82725f31f0ed55507bb93876f 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("O015cdc2a9b96a2fe227389cb205e1","index","CREATE INDEX O015cdc2a9b96a2fe227389cb205e1 ON Person_Person_Father (Person1_)"));
res.push_back(Database::SchemaItem("Ob6198f9bf01ff852985e22535b63c","index","CREATE INDEX Ob6198f9bf01ff852985e22535b63c 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("O83113b1dbe037a340a4b4fb558318","index","CREATE INDEX O83113b1dbe037a340a4b4fb558318 ON Person_Person_Siblings (Person1_)"));
res.push_back(Database::SchemaItem("Oa2ca8dcfc753ed5593154e5c78dd9","index","CREATE INDEX Oa2ca8dcfc753ed5593154e5c78dd9 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("O7e9a52474b6d215cf914b0521bd38","index","CREATE INDEX O7e9a52474b6d215cf914b0521bd38 ON Person_Person_Children (Person1_)"));
res.push_back(Database::SchemaItem("Obe20d0fd80388c3efb79c932f5d78","index","CREATE INDEX Obe20d0fd80388c3efb79c932f5d78 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_RolesPerson1_idx","index","CREATE INDEX Person_Role_RolesPerson1_idx ON Person_Role_Roles (Person1_)"));
res.push_back(Database::SchemaItem("Person_Role_RolesRole2_idx","index","CREATE INDEX Person_Role_RolesRole2_idx 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_School1_idx","index","CREATE INDEX School_Student_School1_idx ON School_Student_ (School1_)"));
res.push_back(Database::SchemaItem("School_Student_Student2_idx","index","CREATE INDEX School_Student_Student2_idx 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_Employee1_idx","index","CREATE INDEX Employee_Office_Employee1_idx ON Employee_Office_ (Employee1_)"));
res.push_back(Database::SchemaItem("Employee_Office_Office2_idx","index","CREATE INDEX Employee_Office_Office2_idx 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();
}
}
void insert(const std::string &table, const Record &r, const std::vector< std::string > &fields=std::vector< std::string >()) const
executes SQL INSERT statement.
Definition: database.cpp:247
void delete_(const std::string &table, const Expr &e=Expr()) const
deletes rows from single table, deleted rows are selected by Expr if specified
Definition: database.cpp:277
const Persistent & operator=(const Persistent &p)
copies values and keeps fields unchanged
Definition: persistent.cpp:14
std::string insert(Record &tables, Records &fieldRecs, Records &values, const std::string &sequence)
executed when object is created into database
Definition: persistent.cpp:23
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.