LiteSQL
Implementation Details

Index

Code Generator litesql-gen

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.

Small help:

./litesql-gen --help
Usage: litesql-gen [options] <my-database.xml>
Options:
-t, --target=TARGET generate code for TARGET (default: c++)
-v, --verbose verbosely report code generation
--help print help
--output-dir=/path/to/src output all files to directory
--output-sources=/path/to/src output sources to directory
(only with target c++)
--output-include=/path/to/include output includes to directory
(only with target c++)
Supported targets:
'c++' C++ target (.cpp,.hpp)
'graphviz' Graphviz file (.dot)
'ruby-activerecord' experimental ruby support, generates migration and activeRecord classes

The example database definition file (exampledatabase.xml) page will result in two files (exampledatabase.hpp and testdatabase.cpp) when fed to code generator.

<?xml version="1.0"?>
<!DOCTYPE database SYSTEM "litesql.dtd">
<database name="ExampleDatabase" namespace="example">
<include name="auth.xml" />
<object name="Person">
<field name="name" type="string" length="256"/>
<field name="age" type="integer" default="15"/>
<field name="image" type="blob"/>
<field name="aDoubleValue" type="double"/>
<field name="sex" type="integer">
<value name="Male" value="0"/>
<value name="Female" value="1"/>
</field>
<method name="sayHello"/>
<index unique="true">
<indexfield name="name"/>
<indexfield name="age"/>
</index>
</object>
<relation id="Mother" unidir="true">
<relate object="Person" limit="many" handle="mother"/>
<relate object="Person" limit="one"/>
</relation>
<relation id="Father" unidir="true">
<relate object="Person" limit="many" handle="father"/>
<relate object="Person" limit="one"/>
</relation>
<relation id="Siblings">
<relate object="Person" handle="siblings"/>
<relate object="Person"/>
</relation>
<relation id="Children" unidir="true">
<relate object="Person" handle="children"/>
<relate object="Person"/>
</relation>
<object name="Role"/>
<object name="Student" inherits="Role"/>
<object name="Employee" inherits="Role"/>
<relation id="Roles" name="RoleRelation">
<relate object="Person" handle="roles" limit="one"/>
<relate object="Role" handle="person"/>
</relation>
<object name="School">
<field name="name" type="string" length="512"/>
</object>
<relation>
<relate object="Student" handle="school"/>
<relate object="School" handle="students" limit="one"/>
</relation>
<object name="Office"/>
<relation>
<relate object="Employee" handle="office"/>
<relate object="Office" handle="employees"/>
</relation>
<object name="ThingWithMethods">
<method name="sayHello">
<param type="std::string" name="text" />
<param type="int" name="repeat" />
</method>
</object>
</database>

exampledatabase.hpp contains class declarations for persistent objects and relations

#ifndef exampledatabase_hpp
#define exampledatabase_hpp
#include "litesql.hpp"
namespace example {
class user;
class Person;
class Role;
class Student;
class Employee;
class School;
class Office;
class ThingWithMethods;
class PersonPersonRelationMother {
public:
class Row {
public:
Row(const litesql::Database& db, const litesql::Record& rec=litesql::Record());
};
static const std::string table__;
static const litesql::FieldType Person1;
static const litesql::FieldType Person2;
static void link(const litesql::Database& db, const example::Person& o0, const example::Person& o1);
static void unlink(const litesql::Database& db, const example::Person& o0, const example::Person& o1);
static void del(const litesql::Database& db, const litesql::Expr& expr=litesql::Expr());
};
class PersonPersonRelationFather {
public:
class Row {
public:
Row(const litesql::Database& db, const litesql::Record& rec=litesql::Record());
};
static const std::string table__;
static const litesql::FieldType Person1;
static const litesql::FieldType Person2;
static void link(const litesql::Database& db, const example::Person& o0, const example::Person& o1);
static void unlink(const litesql::Database& db, const example::Person& o0, const example::Person& o1);
static void del(const litesql::Database& db, const litesql::Expr& expr=litesql::Expr());
};
class PersonPersonRelationSiblings {
public:
class Row {
public:
Row(const litesql::Database& db, const litesql::Record& rec=litesql::Record());
};
static const std::string table__;
static const litesql::FieldType Person1;
static const litesql::FieldType Person2;
static void link(const litesql::Database& db, const example::Person& o0, const example::Person& o1);
static void unlink(const litesql::Database& db, const example::Person& o0, const example::Person& o1);
static void del(const litesql::Database& db, const litesql::Expr& expr=litesql::Expr());
};
class PersonPersonRelationChildren {
public:
class Row {
public:
Row(const litesql::Database& db, const litesql::Record& rec=litesql::Record());
};
static const std::string table__;
static const litesql::FieldType Person1;
static const litesql::FieldType Person2;
static void link(const litesql::Database& db, const example::Person& o0, const example::Person& o1);
static void unlink(const litesql::Database& db, const example::Person& o0, const example::Person& o1);
static void del(const litesql::Database& db, const litesql::Expr& expr=litesql::Expr());
};
class RoleRelation {
public:
class Row {
public:
Row(const litesql::Database& db, const litesql::Record& rec=litesql::Record());
};
static const std::string table__;
static const litesql::FieldType Person;
static const litesql::FieldType Role;
static void link(const litesql::Database& db, const example::Person& o0, const example::Role& o1);
static void unlink(const litesql::Database& db, const example::Person& o0, const example::Role& o1);
static void del(const litesql::Database& db, const litesql::Expr& expr=litesql::Expr());
template <class T> static litesql::DataSource<T> get(const litesql::Database& db, const litesql::Expr& expr=litesql::Expr(), const litesql::Expr& srcExpr=litesql::Expr());
;
;
};
class SchoolStudentRelation {
public:
class Row {
public:
Row(const litesql::Database& db, const litesql::Record& rec=litesql::Record());
};
static const std::string table__;
static const litesql::FieldType School;
static const litesql::FieldType Student;
static void link(const litesql::Database& db, const example::School& o0, const example::Student& o1);
static void unlink(const litesql::Database& db, const example::School& o0, const example::Student& o1);
static void del(const litesql::Database& db, const litesql::Expr& expr=litesql::Expr());
template <class T> static litesql::DataSource<T> get(const litesql::Database& db, const litesql::Expr& expr=litesql::Expr(), const litesql::Expr& srcExpr=litesql::Expr());
;
;
};
class EmployeeOfficeRelation {
public:
class Row {
public:
Row(const litesql::Database& db, const litesql::Record& rec=litesql::Record());
};
static const std::string table__;
static const litesql::FieldType Employee;
static const litesql::FieldType Office;
static void link(const litesql::Database& db, const example::Employee& o0, const example::Office& o1);
static void unlink(const litesql::Database& db, const example::Employee& o0, const example::Office& o1);
static void del(const litesql::Database& db, const litesql::Expr& expr=litesql::Expr());
template <class T> static litesql::DataSource<T> get(const litesql::Database& db, const litesql::Expr& expr=litesql::Expr(), const litesql::Expr& srcExpr=litesql::Expr());
;
;
};
class user : public litesql::Persistent {
public:
class Own {
public:
static const litesql::FieldType Id;
};
static const std::string type__;
static const std::string table__;
static const std::string sequence__;
static const litesql::FieldType Id;
static const litesql::FieldType Type;
static const litesql::FieldType Name;
static const litesql::FieldType Passwd;
static void initValues();
protected:
void defaults();
public:
user(const litesql::Database& db);
user(const litesql::Database& db, const litesql::Record& rec);
user(const user& obj);
const user& operator=(const user& obj);
virtual void sayHello();
protected:
std::string insert(litesql::Record& tables, litesql::Records& fieldRecs, litesql::Records& valueRecs);
void create();
virtual void addUpdates(Updates& updates);
virtual void addIDUpdates(Updates& updates);
public:
static void getFieldTypes(std::vector<litesql::FieldType>& ftypes);
protected:
virtual void delRecord();
virtual void delRelations();
public:
virtual void update();
virtual void del();
virtual bool typeIsCorrect() const;
std::unique_ptr<user> upcast() const;
std::unique_ptr<user> upcastCopy() const;
};
std::ostream & operator<<(std::ostream& os, user o);
class Person : public litesql::Persistent {
public:
class Own {
public:
static const litesql::FieldType Id;
};
class SexType : public litesql::FieldType {
public:
static const int Male;
static const int Female;
SexType(const std::string& n, AT_field_type t, const std::string& tbl, const litesql::FieldType::Values& vals=Values());
};
class Sex {
public:
static const int Male;
static const int Female;
};
class MotherHandle : public litesql::RelationHandle<Person> {
public:
MotherHandle(const Person& owner);
void link(const Person& o0);
void unlink(const Person& o0);
void del(const litesql::Expr& expr=litesql::Expr());
};
class FatherHandle : public litesql::RelationHandle<Person> {
public:
FatherHandle(const Person& owner);
void link(const Person& o0);
void unlink(const Person& o0);
void del(const litesql::Expr& expr=litesql::Expr());
};
class SiblingsHandle : public litesql::RelationHandle<Person> {
public:
SiblingsHandle(const Person& owner);
void link(const Person& o0);
void unlink(const Person& o0);
void del(const litesql::Expr& expr=litesql::Expr());
};
class ChildrenHandle : public litesql::RelationHandle<Person> {
public:
ChildrenHandle(const Person& owner);
void link(const Person& o0);
void unlink(const Person& o0);
void del(const litesql::Expr& expr=litesql::Expr());
};
class RolesHandle : public litesql::RelationHandle<Person> {
public:
RolesHandle(const Person& owner);
void link(const Role& o0);
void unlink(const Role& o0);
void del(const litesql::Expr& expr=litesql::Expr());
};
static const std::string type__;
static const std::string table__;
static const std::string sequence__;
static const litesql::FieldType Id;
static const litesql::FieldType Type;
static const litesql::FieldType Name;
static const litesql::FieldType Age;
static const litesql::FieldType Image;
static const litesql::FieldType ADoubleValue;
litesql::Field<double> aDoubleValue;
protected:
static std::vector < std::pair< std::string, std::string > > sex_values;
public:
static const litesql::FieldType Sex;
static void initValues();
protected:
void defaults();
public:
Person(const litesql::Database& db);
Person(const litesql::Database& db, const litesql::Record& rec);
Person(const Person& obj);
const Person& operator=(const Person& obj);
Person::MotherHandle mother();
Person::FatherHandle father();
Person::SiblingsHandle siblings();
Person::ChildrenHandle children();
Person::RolesHandle roles();
virtual void sayHello();
protected:
std::string insert(litesql::Record& tables, litesql::Records& fieldRecs, litesql::Records& valueRecs);
void create();
virtual void addUpdates(Updates& updates);
virtual void addIDUpdates(Updates& updates);
public:
static void getFieldTypes(std::vector<litesql::FieldType>& ftypes);
protected:
virtual void delRecord();
virtual void delRelations();
public:
virtual void update();
virtual void del();
virtual bool typeIsCorrect() const;
std::unique_ptr<Person> upcast() const;
std::unique_ptr<Person> upcastCopy() const;
};
std::ostream & operator<<(std::ostream& os, Person o);
class Role : public litesql::Persistent {
public:
class Own {
public:
static const litesql::FieldType Id;
};
class PersonHandle : public litesql::RelationHandle<Role> {
public:
PersonHandle(const Role& owner);
void link(const Person& o0);
void unlink(const Person& o0);
void del(const litesql::Expr& expr=litesql::Expr());
};
static const std::string type__;
static const std::string table__;
static const std::string sequence__;
static const litesql::FieldType Id;
static const litesql::FieldType Type;
static void initValues();
protected:
void defaults();
public:
Role(const litesql::Database& db);
Role(const litesql::Database& db, const litesql::Record& rec);
Role(const Role& obj);
const Role& operator=(const Role& obj);
Role::PersonHandle person();
protected:
std::string insert(litesql::Record& tables, litesql::Records& fieldRecs, litesql::Records& valueRecs);
void create();
virtual void addUpdates(Updates& updates);
virtual void addIDUpdates(Updates& updates);
public:
static void getFieldTypes(std::vector<litesql::FieldType>& ftypes);
protected:
virtual void delRecord();
virtual void delRelations();
public:
virtual void update();
virtual void del();
virtual bool typeIsCorrect() const;
std::unique_ptr<Role> upcast() const;
std::unique_ptr<Role> upcastCopy() const;
};
std::ostream & operator<<(std::ostream& os, Role o);
class Student : public Role {
public:
class Own {
public:
static const litesql::FieldType Id;
};
class SchoolHandle : public litesql::RelationHandle<Student> {
public:
SchoolHandle(const Student& owner);
void link(const School& o0);
void unlink(const School& o0);
void del(const litesql::Expr& expr=litesql::Expr());
};
static const std::string type__;
static const std::string table__;
Student(const litesql::Database& db);
Student(const litesql::Database& db, const litesql::Record& rec);
Student(const Student& obj);
const Student& operator=(const Student& obj);
Student::SchoolHandle school();
protected:
std::string insert(litesql::Record& tables, litesql::Records& fieldRecs, litesql::Records& valueRecs);
void create();
virtual void addUpdates(Updates& updates);
virtual void addIDUpdates(Updates& updates);
public:
static void getFieldTypes(std::vector<litesql::FieldType>& ftypes);
protected:
virtual void delRecord();
virtual void delRelations();
public:
virtual void update();
virtual void del();
virtual bool typeIsCorrect() const;
std::unique_ptr<Student> upcast() const;
std::unique_ptr<Student> upcastCopy() const;
};
std::ostream & operator<<(std::ostream& os, Student o);
class Employee : public Role {
public:
class Own {
public:
static const litesql::FieldType Id;
};
class OfficeHandle : public litesql::RelationHandle<Employee> {
public:
OfficeHandle(const Employee& owner);
void link(const Office& o0);
void unlink(const Office& o0);
void del(const litesql::Expr& expr=litesql::Expr());
};
static const std::string type__;
static const std::string table__;
Employee(const litesql::Database& db);
Employee(const litesql::Database& db, const litesql::Record& rec);
Employee(const Employee& obj);
const Employee& operator=(const Employee& obj);
Employee::OfficeHandle office();
protected:
std::string insert(litesql::Record& tables, litesql::Records& fieldRecs, litesql::Records& valueRecs);
void create();
virtual void addUpdates(Updates& updates);
virtual void addIDUpdates(Updates& updates);
public:
static void getFieldTypes(std::vector<litesql::FieldType>& ftypes);
protected:
virtual void delRecord();
virtual void delRelations();
public:
virtual void update();
virtual void del();
virtual bool typeIsCorrect() const;
std::unique_ptr<Employee> upcast() const;
std::unique_ptr<Employee> upcastCopy() const;
};
std::ostream & operator<<(std::ostream& os, Employee o);
class School : public litesql::Persistent {
public:
class Own {
public:
static const litesql::FieldType Id;
};
class StudentsHandle : public litesql::RelationHandle<School> {
public:
StudentsHandle(const School& owner);
void link(const Student& o0);
void unlink(const Student& o0);
void del(const litesql::Expr& expr=litesql::Expr());
};
static const std::string type__;
static const std::string table__;
static const std::string sequence__;
static const litesql::FieldType Id;
static const litesql::FieldType Type;
static const litesql::FieldType Name;
static void initValues();
protected:
void defaults();
public:
School(const litesql::Database& db);
School(const litesql::Database& db, const litesql::Record& rec);
School(const School& obj);
const School& operator=(const School& obj);
School::StudentsHandle students();
protected:
std::string insert(litesql::Record& tables, litesql::Records& fieldRecs, litesql::Records& valueRecs);
void create();
virtual void addUpdates(Updates& updates);
virtual void addIDUpdates(Updates& updates);
public:
static void getFieldTypes(std::vector<litesql::FieldType>& ftypes);
protected:
virtual void delRecord();
virtual void delRelations();
public:
virtual void update();
virtual void del();
virtual bool typeIsCorrect() const;
std::unique_ptr<School> upcast() const;
std::unique_ptr<School> upcastCopy() const;
};
std::ostream & operator<<(std::ostream& os, School o);
class Office : public litesql::Persistent {
public:
class Own {
public:
static const litesql::FieldType Id;
};
class EmployeesHandle : public litesql::RelationHandle<Office> {
public:
EmployeesHandle(const Office& owner);
void link(const Employee& o0);
void unlink(const Employee& o0);
void del(const litesql::Expr& expr=litesql::Expr());
};
static const std::string type__;
static const std::string table__;
static const std::string sequence__;
static const litesql::FieldType Id;
static const litesql::FieldType Type;
static void initValues();
protected:
void defaults();
public:
Office(const litesql::Database& db);
Office(const litesql::Database& db, const litesql::Record& rec);
Office(const Office& obj);
const Office& operator=(const Office& obj);
Office::EmployeesHandle employees();
protected:
std::string insert(litesql::Record& tables, litesql::Records& fieldRecs, litesql::Records& valueRecs);
void create();
virtual void addUpdates(Updates& updates);
virtual void addIDUpdates(Updates& updates);
public:
static void getFieldTypes(std::vector<litesql::FieldType>& ftypes);
protected:
virtual void delRecord();
virtual void delRelations();
public:
virtual void update();
virtual void del();
virtual bool typeIsCorrect() const;
std::unique_ptr<Office> upcast() const;
std::unique_ptr<Office> upcastCopy() const;
};
std::ostream & operator<<(std::ostream& os, Office o);
class ThingWithMethods : public litesql::Persistent {
public:
class Own {
public:
static const litesql::FieldType Id;
};
static const std::string type__;
static const std::string table__;
static const std::string sequence__;
static const litesql::FieldType Id;
static const litesql::FieldType Type;
static void initValues();
protected:
void defaults();
public:
ThingWithMethods(const litesql::Database& db);
ThingWithMethods(const litesql::Database& db, const litesql::Record& rec);
ThingWithMethods(const ThingWithMethods& obj);
const ThingWithMethods& operator=(const ThingWithMethods& obj);
virtual void sayHello(std::string text, int repeat);
protected:
std::string insert(litesql::Record& tables, litesql::Records& fieldRecs, litesql::Records& valueRecs);
void create();
virtual void addUpdates(Updates& updates);
virtual void addIDUpdates(Updates& updates);
public:
static void getFieldTypes(std::vector<litesql::FieldType>& ftypes);
protected:
virtual void delRecord();
virtual void delRelations();
public:
virtual void update();
virtual void del();
virtual bool typeIsCorrect() const;
std::unique_ptr<ThingWithMethods> upcast() const;
std::unique_ptr<ThingWithMethods> upcastCopy() const;
};
std::ostream & operator<<(std::ostream& os, ThingWithMethods o);
class ExampleDatabase : public litesql::Database {
public:
ExampleDatabase( const std::string& backendType, const std::string& connInfo);
protected:
virtual std::vector<litesql::Database::SchemaItem> getSchema() const;
static void initialize();
};
}
#endif
template class which holds SelectQuery for selecting objects of type T
Definition: datasource.hpp:35
A base class of databases.
Definition: database.hpp:33
A base class for expression in WHERE - clause.
Definition: expr.hpp:18
Definition: field.hpp:25
Definition: field.hpp:118
holds field value
Definition: field.hpp:75
An abstract base class of persistents objects.
Definition: persistent.hpp:25
SQL data row wrapper.
Definition: types.hpp:20
base class of relation handles attached to Persistent objects
Definition: relation.hpp:15
include this from your project
std::vector< Record > Records
shortcut
Definition: types.hpp:26

exampledatabase.cpp contains implementation of methods and lots of static data.

#include "exampledatabase.hpp"
namespace example {
using namespace litesql;
PersonPersonRelationMother::Row::Row(const litesql::Database& db, const litesql::Record& rec)
: 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));
}
void PersonPersonRelationMother::del(const litesql::Database& db, const litesql::Expr& expr) {
db.delete_(table__, expr);
}
litesql::DataSource<PersonPersonRelationMother::Row> PersonPersonRelationMother::getRows(const litesql::Database& db, const litesql::Expr& expr) {
SelectQuery sel;
sel.result(Person1.fullName());
sel.result(Person2.fullName());
sel.source(table__);
sel.where(expr);
return DataSource<PersonPersonRelationMother::Row>(db, sel);
}
litesql::DataSource<example::Person> PersonPersonRelationMother::getPerson1(const litesql::Database& db, const litesql::Expr& expr, const litesql::Expr& srcExpr) {
SelectQuery sel;
sel.source(table__);
sel.result(Person1.fullName());
sel.where(srcExpr);
return DataSource<example::Person>(db, example::Person::Id.in(sel) && expr);
}
litesql::DataSource<example::Person> PersonPersonRelationMother::getPerson2(const litesql::Database& db, const litesql::Expr& expr, const litesql::Expr& srcExpr) {
SelectQuery sel;
sel.source(table__);
sel.result(Person2.fullName());
sel.where(srcExpr);
return DataSource<example::Person>(db, example::Person::Id.in(sel) && expr);
}
PersonPersonRelationFather::Row::Row(const litesql::Database& db, const litesql::Record& rec)
: 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));
}
void PersonPersonRelationFather::del(const litesql::Database& db, const litesql::Expr& expr) {
db.delete_(table__, expr);
}
litesql::DataSource<PersonPersonRelationFather::Row> PersonPersonRelationFather::getRows(const litesql::Database& db, const litesql::Expr& expr) {
SelectQuery sel;
sel.result(Person1.fullName());
sel.result(Person2.fullName());
sel.source(table__);
sel.where(expr);
return DataSource<PersonPersonRelationFather::Row>(db, sel);
}
litesql::DataSource<example::Person> PersonPersonRelationFather::getPerson1(const litesql::Database& db, const litesql::Expr& expr, const litesql::Expr& srcExpr) {
SelectQuery sel;
sel.source(table__);
sel.result(Person1.fullName());
sel.where(srcExpr);
return DataSource<example::Person>(db, example::Person::Id.in(sel) && expr);
}
litesql::DataSource<example::Person> PersonPersonRelationFather::getPerson2(const litesql::Database& db, const litesql::Expr& expr, const litesql::Expr& srcExpr) {
SelectQuery sel;
sel.source(table__);
sel.result(Person2.fullName());
sel.where(srcExpr);
return DataSource<example::Person>(db, example::Person::Id.in(sel) && expr);
}
PersonPersonRelationSiblings::Row::Row(const litesql::Database& db, const litesql::Record& rec)
: 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));
}
void PersonPersonRelationSiblings::del(const litesql::Database& db, const litesql::Expr& expr) {
db.delete_(table__, expr);
}
litesql::DataSource<PersonPersonRelationSiblings::Row> PersonPersonRelationSiblings::getRows(const litesql::Database& db, const litesql::Expr& expr) {
SelectQuery sel;
sel.result(Person1.fullName());
sel.result(Person2.fullName());
sel.source(table__);
sel.where(expr);
return DataSource<PersonPersonRelationSiblings::Row>(db, sel);
}
litesql::DataSource<example::Person> PersonPersonRelationSiblings::getPerson1(const litesql::Database& db, const litesql::Expr& expr, const litesql::Expr& srcExpr) {
SelectQuery sel;
sel.source(table__);
sel.result(Person1.fullName());
sel.where(srcExpr);
return DataSource<example::Person>(db, example::Person::Id.in(sel) && expr);
}
litesql::DataSource<example::Person> PersonPersonRelationSiblings::getPerson2(const litesql::Database& db, const litesql::Expr& expr, const litesql::Expr& srcExpr) {
SelectQuery sel;
sel.source(table__);
sel.result(Person2.fullName());
sel.where(srcExpr);
return DataSource<example::Person>(db, example::Person::Id.in(sel) && expr);
}
PersonPersonRelationChildren::Row::Row(const litesql::Database& db, const litesql::Record& rec)
: 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));
}
void PersonPersonRelationChildren::del(const litesql::Database& db, const litesql::Expr& expr) {
db.delete_(table__, expr);
}
litesql::DataSource<PersonPersonRelationChildren::Row> PersonPersonRelationChildren::getRows(const litesql::Database& db, const litesql::Expr& expr) {
SelectQuery sel;
sel.result(Person1.fullName());
sel.result(Person2.fullName());
sel.source(table__);
sel.where(expr);
return DataSource<PersonPersonRelationChildren::Row>(db, sel);
}
litesql::DataSource<example::Person> PersonPersonRelationChildren::getPerson1(const litesql::Database& db, const litesql::Expr& expr, const litesql::Expr& srcExpr) {
SelectQuery sel;
sel.source(table__);
sel.result(Person1.fullName());
sel.where(srcExpr);
return DataSource<example::Person>(db, example::Person::Id.in(sel) && expr);
}
litesql::DataSource<example::Person> PersonPersonRelationChildren::getPerson2(const litesql::Database& db, const litesql::Expr& expr, const litesql::Expr& srcExpr) {
SelectQuery sel;
sel.source(table__);
sel.result(Person2.fullName());
sel.where(srcExpr);
return DataSource<example::Person>(db, example::Person::Id.in(sel) && expr);
}
RoleRelation::Row::Row(const litesql::Database& db, const litesql::Record& rec)
: 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");
const litesql::FieldType RoleRelation::Person("Person1_",A_field_type_integer,table__);
const litesql::FieldType RoleRelation::Role("Role2_",A_field_type_integer,table__);
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));
}
void RoleRelation::del(const litesql::Database& db, const litesql::Expr& expr) {
db.delete_(table__, expr);
}
litesql::DataSource<RoleRelation::Row> RoleRelation::getRows(const litesql::Database& db, const litesql::Expr& expr) {
SelectQuery sel;
sel.result(Person.fullName());
sel.result(Role.fullName());
sel.source(table__);
sel.where(expr);
return DataSource<RoleRelation::Row>(db, sel);
}
template <> litesql::DataSource<example::Person> RoleRelation::get(const litesql::Database& db, const litesql::Expr& expr, const litesql::Expr& srcExpr) {
SelectQuery sel;
sel.source(table__);
sel.result(Person.fullName());
sel.where(srcExpr);
return DataSource<example::Person>(db, example::Person::Id.in(sel) && expr);
}
template <> litesql::DataSource<example::Role> RoleRelation::get(const litesql::Database& db, const litesql::Expr& expr, const litesql::Expr& srcExpr) {
SelectQuery sel;
sel.source(table__);
sel.result(Role.fullName());
sel.where(srcExpr);
return DataSource<example::Role>(db, example::Role::Id.in(sel) && expr);
}
SchoolStudentRelation::Row::Row(const litesql::Database& db, const litesql::Record& rec)
: 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));
}
void SchoolStudentRelation::del(const litesql::Database& db, const litesql::Expr& expr) {
db.delete_(table__, expr);
}
litesql::DataSource<SchoolStudentRelation::Row> SchoolStudentRelation::getRows(const litesql::Database& db, const litesql::Expr& expr) {
SelectQuery sel;
sel.result(School.fullName());
sel.result(Student.fullName());
sel.source(table__);
sel.where(expr);
return DataSource<SchoolStudentRelation::Row>(db, sel);
}
template <> litesql::DataSource<example::School> SchoolStudentRelation::get(const litesql::Database& db, const litesql::Expr& expr, const litesql::Expr& srcExpr) {
SelectQuery sel;
sel.source(table__);
sel.result(School.fullName());
sel.where(srcExpr);
return DataSource<example::School>(db, example::School::Id.in(sel) && expr);
}
template <> litesql::DataSource<example::Student> SchoolStudentRelation::get(const litesql::Database& db, const litesql::Expr& expr, const litesql::Expr& srcExpr) {
SelectQuery sel;
sel.source(table__);
sel.result(Student.fullName());
sel.where(srcExpr);
return DataSource<example::Student>(db, example::Student::Id.in(sel) && expr);
}
EmployeeOfficeRelation::Row::Row(const litesql::Database& db, const litesql::Record& rec)
: 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));
}
void EmployeeOfficeRelation::del(const litesql::Database& db, const litesql::Expr& expr) {
db.delete_(table__, expr);
}
litesql::DataSource<EmployeeOfficeRelation::Row> EmployeeOfficeRelation::getRows(const litesql::Database& db, const litesql::Expr& expr) {
SelectQuery sel;
sel.result(Employee.fullName());
sel.result(Office.fullName());
sel.source(table__);
sel.where(expr);
return DataSource<EmployeeOfficeRelation::Row>(db, sel);
}
template <> litesql::DataSource<example::Employee> EmployeeOfficeRelation::get(const litesql::Database& db, const litesql::Expr& expr, const litesql::Expr& srcExpr) {
SelectQuery sel;
sel.source(table__);
sel.result(Employee.fullName());
sel.where(srcExpr);
return DataSource<example::Employee>(db, example::Employee::Id.in(sel) && expr);
}
template <> litesql::DataSource<example::Office> EmployeeOfficeRelation::get(const litesql::Database& db, const litesql::Expr& expr, const litesql::Expr& srcExpr) {
SelectQuery sel;
sel.source(table__);
sel.result(Office.fullName());
sel.where(srcExpr);
return DataSource<example::Office>(db, example::Office::Id.in(sel) && expr);
}
const litesql::FieldType user::Own::Id("id_",A_field_type_integer,"user_");
const std::string user::type__("user");
const std::string user::table__("user_");
const std::string user::sequence__("user_seq");
const litesql::FieldType user::Id("id_",A_field_type_integer,table__);
const litesql::FieldType user::Type("type_",A_field_type_string,table__);
const litesql::FieldType user::Name("name_",A_field_type_string,table__);
const litesql::FieldType user::Passwd("passwd_",A_field_type_string,table__);
void user::initValues() {
}
void user::defaults() {
id = 0;
}
user::user(const litesql::Database& db)
: litesql::Persistent(db), id(Id), type(Type), name(Name), passwd(Passwd) {
defaults();
}
user::user(const litesql::Database& db, const litesql::Record& rec)
: 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;
}
std::string user::insert(litesql::Record& tables, litesql::Records& fieldRecs, litesql::Records& valueRecs) {
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);
return litesql::Persistent::insert(tables, fieldRecs, valueRecs, sequence__);
}
void user::create() {
litesql::Records fieldRecs;
litesql::Records valueRecs;
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 litesql::FieldType Person::Own::Id("id_",A_field_type_integer,"Person_");
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);
}
void Person::MotherHandle::del(const litesql::Expr& expr) {
PersonPersonRelationMother::del(owner->getDatabase(), expr && PersonPersonRelationMother::Person1 == owner->id);
}
litesql::DataSource<Person> Person::MotherHandle::get(const litesql::Expr& expr, const litesql::Expr& srcExpr) {
return PersonPersonRelationMother::getPerson2(owner->getDatabase(), expr, (PersonPersonRelationMother::Person1 == owner->id) && srcExpr);
}
litesql::DataSource<PersonPersonRelationMother::Row> Person::MotherHandle::getRows(const litesql::Expr& expr) {
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);
}
void Person::FatherHandle::del(const litesql::Expr& expr) {
PersonPersonRelationFather::del(owner->getDatabase(), expr && PersonPersonRelationFather::Person1 == owner->id);
}
litesql::DataSource<Person> Person::FatherHandle::get(const litesql::Expr& expr, const litesql::Expr& srcExpr) {
return PersonPersonRelationFather::getPerson2(owner->getDatabase(), expr, (PersonPersonRelationFather::Person1 == owner->id) && srcExpr);
}
litesql::DataSource<PersonPersonRelationFather::Row> Person::FatherHandle::getRows(const litesql::Expr& expr) {
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);
}
void Person::SiblingsHandle::del(const litesql::Expr& expr) {
PersonPersonRelationSiblings::del(owner->getDatabase(), expr && PersonPersonRelationSiblings::Person1 == owner->id);
}
litesql::DataSource<Person> Person::SiblingsHandle::get(const litesql::Expr& expr, const litesql::Expr& srcExpr) {
return PersonPersonRelationSiblings::getPerson2(owner->getDatabase(), expr, (PersonPersonRelationSiblings::Person1 == owner->id) && srcExpr);
}
litesql::DataSource<PersonPersonRelationSiblings::Row> Person::SiblingsHandle::getRows(const litesql::Expr& expr) {
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);
}
void Person::ChildrenHandle::del(const litesql::Expr& expr) {
PersonPersonRelationChildren::del(owner->getDatabase(), expr && PersonPersonRelationChildren::Person1 == owner->id);
}
litesql::DataSource<Person> Person::ChildrenHandle::get(const litesql::Expr& expr, const litesql::Expr& srcExpr) {
return PersonPersonRelationChildren::getPerson2(owner->getDatabase(), expr, (PersonPersonRelationChildren::Person1 == owner->id) && srcExpr);
}
litesql::DataSource<PersonPersonRelationChildren::Row> Person::ChildrenHandle::getRows(const litesql::Expr& expr) {
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);
}
void Person::RolesHandle::del(const litesql::Expr& expr) {
RoleRelation::del(owner->getDatabase(), expr && RoleRelation::Person == owner->id);
}
litesql::DataSource<Role> Person::RolesHandle::get(const litesql::Expr& expr, const litesql::Expr& srcExpr) {
return RoleRelation::get<Role>(owner->getDatabase(), expr, (RoleRelation::Person == owner->id) && srcExpr);
}
litesql::DataSource<RoleRelation::Row> Person::RolesHandle::getRows(const litesql::Expr& expr) {
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::Id("id_",A_field_type_integer,table__);
const litesql::FieldType Person::Type("type_",A_field_type_string,table__);
const litesql::FieldType Person::Name("name_",A_field_type_string,table__);
const litesql::FieldType Person::Age("age_",A_field_type_integer,table__);
const litesql::FieldType Person::Image("image_",A_field_type_blob,table__);
const litesql::FieldType Person::ADoubleValue("aDoubleValue_",A_field_type_double,table__);
std::vector < std::pair< std::string, std::string > > Person::sex_values;
const litesql::FieldType Person::Sex("sex_",A_field_type_integer,table__);
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;
}
Person::Person(const litesql::Database& db)
: litesql::Persistent(db), id(Id), type(Type), name(Name), age(Age), image(Image), aDoubleValue(ADoubleValue), sex(Sex) {
defaults();
}
Person::Person(const litesql::Database& db, const litesql::Record& rec)
: 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);
}
std::string Person::insert(litesql::Record& tables, litesql::Records& fieldRecs, litesql::Records& valueRecs) {
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);
return litesql::Persistent::insert(tables, fieldRecs, valueRecs, sequence__);
}
void Person::create() {
litesql::Records fieldRecs;
litesql::Records valueRecs;
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;
}
const litesql::FieldType Role::Own::Id("id_",A_field_type_integer,"Role_");
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);
}
void Role::PersonHandle::del(const litesql::Expr& expr) {
RoleRelation::del(owner->getDatabase(), expr && RoleRelation::Role == owner->id);
}
litesql::DataSource<Person> Role::PersonHandle::get(const litesql::Expr& expr, const litesql::Expr& srcExpr) {
return RoleRelation::get<Person>(owner->getDatabase(), expr, (RoleRelation::Role == owner->id) && srcExpr);
}
litesql::DataSource<RoleRelation::Row> Role::PersonHandle::getRows(const litesql::Expr& expr) {
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");
const litesql::FieldType Role::Id("id_",A_field_type_integer,table__);
const litesql::FieldType Role::Type("type_",A_field_type_string,table__);
void Role::initValues() {
}
void Role::defaults() {
id = 0;
}
Role::Role(const litesql::Database& db)
: litesql::Persistent(db), id(Id), type(Type) {
defaults();
}
Role::Role(const litesql::Database& db, const litesql::Record& rec)
: 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);
}
std::string Role::insert(litesql::Record& tables, litesql::Records& fieldRecs, litesql::Records& valueRecs) {
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);
return litesql::Persistent::insert(tables, fieldRecs, valueRecs, sequence__);
}
void Role::create() {
litesql::Records fieldRecs;
litesql::Records valueRecs;
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;
}
const litesql::FieldType Student::Own::Id("id_",A_field_type_integer,"Student_");
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);
}
void Student::SchoolHandle::del(const litesql::Expr& expr) {
SchoolStudentRelation::del(owner->getDatabase(), expr && SchoolStudentRelation::Student == owner->id);
}
litesql::DataSource<School> Student::SchoolHandle::get(const litesql::Expr& expr, const litesql::Expr& srcExpr) {
return SchoolStudentRelation::get<School>(owner->getDatabase(), expr, (SchoolStudentRelation::Student == owner->id) && srcExpr);
}
litesql::DataSource<SchoolStudentRelation::Row> Student::SchoolHandle::getRows(const litesql::Expr& expr) {
return SchoolStudentRelation::getRows(owner->getDatabase(), expr && (SchoolStudentRelation::Student == owner->id));
}
const std::string Student::type__("Student");
const std::string Student::table__("Student_");
Student::Student(const litesql::Database& db)
: Role(db) {
}
Student::Student(const litesql::Database& db, const litesql::Record& rec)
: 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);
}
std::string Student::insert(litesql::Record& tables, litesql::Records& fieldRecs, litesql::Records& valueRecs) {
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() {
litesql::Records fieldRecs;
litesql::Records valueRecs;
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;
}
const litesql::FieldType Employee::Own::Id("id_",A_field_type_integer,"Employee_");
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);
}
void Employee::OfficeHandle::del(const litesql::Expr& expr) {
EmployeeOfficeRelation::del(owner->getDatabase(), expr && EmployeeOfficeRelation::Employee == owner->id);
}
litesql::DataSource<Office> Employee::OfficeHandle::get(const litesql::Expr& expr, const litesql::Expr& srcExpr) {
return EmployeeOfficeRelation::get<Office>(owner->getDatabase(), expr, (EmployeeOfficeRelation::Employee == owner->id) && srcExpr);
}
litesql::DataSource<EmployeeOfficeRelation::Row> Employee::OfficeHandle::getRows(const litesql::Expr& expr) {
return EmployeeOfficeRelation::getRows(owner->getDatabase(), expr && (EmployeeOfficeRelation::Employee == owner->id));
}
const std::string Employee::type__("Employee");
const std::string Employee::table__("Employee_");
Employee::Employee(const litesql::Database& db)
: Role(db) {
}
Employee::Employee(const litesql::Database& db, const litesql::Record& rec)
: 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);
}
std::string Employee::insert(litesql::Record& tables, litesql::Records& fieldRecs, litesql::Records& valueRecs) {
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() {
litesql::Records fieldRecs;
litesql::Records valueRecs;
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;
}
const litesql::FieldType School::Own::Id("id_",A_field_type_integer,"School_");
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);
}
void School::StudentsHandle::del(const litesql::Expr& expr) {
SchoolStudentRelation::del(owner->getDatabase(), expr && SchoolStudentRelation::School == owner->id);
}
litesql::DataSource<Student> School::StudentsHandle::get(const litesql::Expr& expr, const litesql::Expr& srcExpr) {
return SchoolStudentRelation::get<Student>(owner->getDatabase(), expr, (SchoolStudentRelation::School == owner->id) && srcExpr);
}
litesql::DataSource<SchoolStudentRelation::Row> School::StudentsHandle::getRows(const litesql::Expr& expr) {
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");
const litesql::FieldType School::Id("id_",A_field_type_integer,table__);
const litesql::FieldType School::Type("type_",A_field_type_string,table__);
const litesql::FieldType School::Name("name_",A_field_type_string,table__);
void School::initValues() {
}
void School::defaults() {
id = 0;
}
School::School(const litesql::Database& db)
: litesql::Persistent(db), id(Id), type(Type), name(Name) {
defaults();
}
School::School(const litesql::Database& db, const litesql::Record& rec)
: 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);
}
std::string School::insert(litesql::Record& tables, litesql::Records& fieldRecs, litesql::Records& valueRecs) {
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);
return litesql::Persistent::insert(tables, fieldRecs, valueRecs, sequence__);
}
void School::create() {
litesql::Records fieldRecs;
litesql::Records valueRecs;
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;
}
const litesql::FieldType Office::Own::Id("id_",A_field_type_integer,"Office_");
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);
}
void Office::EmployeesHandle::del(const litesql::Expr& expr) {
EmployeeOfficeRelation::del(owner->getDatabase(), expr && EmployeeOfficeRelation::Office == owner->id);
}
litesql::DataSource<Employee> Office::EmployeesHandle::get(const litesql::Expr& expr, const litesql::Expr& srcExpr) {
return EmployeeOfficeRelation::get<Employee>(owner->getDatabase(), expr, (EmployeeOfficeRelation::Office == owner->id) && srcExpr);
}
litesql::DataSource<EmployeeOfficeRelation::Row> Office::EmployeesHandle::getRows(const litesql::Expr& expr) {
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");
const litesql::FieldType Office::Id("id_",A_field_type_integer,table__);
const litesql::FieldType Office::Type("type_",A_field_type_string,table__);
void Office::initValues() {
}
void Office::defaults() {
id = 0;
}
Office::Office(const litesql::Database& db)
: litesql::Persistent(db), id(Id), type(Type) {
defaults();
}
Office::Office(const litesql::Database& db, const litesql::Record& rec)
: 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);
}
std::string Office::insert(litesql::Record& tables, litesql::Records& fieldRecs, litesql::Records& valueRecs) {
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);
return litesql::Persistent::insert(tables, fieldRecs, valueRecs, sequence__);
}
void Office::create() {
litesql::Records fieldRecs;
litesql::Records valueRecs;
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");
const litesql::FieldType ThingWithMethods::Id("id_",A_field_type_integer,table__);
const litesql::FieldType ThingWithMethods::Type("type_",A_field_type_string,table__);
void ThingWithMethods::initValues() {
}
void ThingWithMethods::defaults() {
id = 0;
}
ThingWithMethods::ThingWithMethods(const litesql::Database& db)
: litesql::Persistent(db), id(Id), type(Type) {
defaults();
}
ThingWithMethods::ThingWithMethods(const litesql::Database& db, const litesql::Record& rec)
: 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;
}
std::string ThingWithMethods::insert(litesql::Record& tables, litesql::Records& fieldRecs, litesql::Records& valueRecs) {
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);
return litesql::Persistent::insert(tables, fieldRecs, valueRecs, sequence__);
}
void ThingWithMethods::create() {
litesql::Records fieldRecs;
litesql::Records valueRecs;
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

Mapping Objects to Relational Database

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.

<database name="example">
<object name="Playable">
<field name="name" type="string"/>
<method name="play"/>
</object>
<object name="AudioFile" inherits="Playable">
<field name="file" type="string"/>
<method name="play"/>
</object>
<object name="Collection">
<field name="description" type="string"/>
</object>
<relation>
<relate object="AudioFile" handle="collections"/>
<relate object="Collection" handle="files"/>
<field name="percent" type="integer" default="100"/>
</relation>
</database>

The database schema extracted from definitions above is similar to following:

CREATE TABLE Playable_ (id_ INTEGER PRIMARY KEY, type_ TEXT, name_ TEXT);
CREATE TABLE AudioFile_ (id_ INTEGER PRIMARY KEY, file_ TEXT);
CREATE TABLE Collection_ (id_ INTEGER PRIMARY KEY, type_ TEXT, description_ TEXT);
CREATE TABLE AudioFile_Collection_ (AudioFile1 INTEGER, Collection2 INTEGER, percent_ INTEGER);
-- original name: idx_AudioFile_Collection_AudioFile1
CREATE INDEX _71b11d59ca507479edd464e053cd01 ON AudioFile_Collection_ (AudioFile1);
-- original name: idx_AudioFile_Collection_Collection2
CREATE INDEX _e1305d9ca2d1c6947b38fc301ad676 ON AudioFile_Collection_ (Collection2);
-- original name: idx_AudioFile_Collection_AudioFile1_Collection2
CREATE INDEX _7e632b2ede1b6f3348d36e44e443d0 ON AudioFile_Collection_ (AudioFile1,Collection2);

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.

Virtual Methods

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.

AudioFile af(db);
af.name = "A good song";
af.file = "goodsong.mp3";
af.update();
Playable p = select<Playable>(db, Playable::Name == "A good song").one()
// p has only one field: name
p.upcast().play();
// this executes actually select<AudioFile>(db, Playable::Id == x).one().play();

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.

// same Playable is selected as in above example
p.upcastCopy()->makeLogEntry(log)

Note that upcastCopy will return a Persistent whose all fields are not retrieved from database.


SourceForge.net Logo