📄 exampledatabase.cpp
字号:
SchoolStudentRelation::del(owner->getDatabase(), expr);}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_","INTEGER",table__);const litesql::FieldType School::Type("type_","TEXT",table__);const litesql::FieldType School::Name("name_","TEXT",table__);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 = rec[2]; case 2: type = rec[1]; case 1: id = rec[0]; }}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; } litesql::Persistent::operator=(obj); 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__); litesql::Record fields; litesql::Record values; 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::Record tables; 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() == false) { std::auto_ptr<School> p(upcastCopy()); p->delRelations(); p->onDelete(); p->delRecord(); } else { onDelete(); delRecord(); } inDatabase = false;}bool School::typeIsCorrect() { return type == type__;}std::auto_ptr<School> School::upcast() { return auto_ptr<School>(new School(*this));}std::auto_ptr<School> School::upcastCopy() { School* np = NULL; np->id = id; np->type = type; np->name = name; np->inDatabase = inDatabase; if (!np) np = new School(*this); return auto_ptr<School>(np);}std::ostream & operator<<(std::ostream& os, School o) { os << "-------------------------------------" << std::endl; os << o.id.name() << " = " << o.id << std::endl; os << o.type.name() << " = " << o.type << std::endl; os << o.name.name() << " = " << o.name << std::endl; os << "-------------------------------------" << std::endl; return os;}Office::EmployeesHandle::EmployeesHandle(const Office& owner) : 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);}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_","INTEGER",table__);const litesql::FieldType Office::Type("type_","TEXT",table__);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 = rec[1]; case 1: id = rec[0]; }}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; } litesql::Persistent::operator=(obj); 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__); litesql::Record fields; litesql::Record values; 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::Record tables; 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() == false) { std::auto_ptr<Office> p(upcastCopy()); p->delRelations(); p->onDelete(); p->delRecord(); } else { onDelete(); delRecord(); } inDatabase = false;}bool Office::typeIsCorrect() { return type == type__;}std::auto_ptr<Office> Office::upcast() { return auto_ptr<Office>(new Office(*this));}std::auto_ptr<Office> Office::upcastCopy() { Office* np = NULL; np->id = id; np->type = type; np->inDatabase = inDatabase; if (!np) np = new Office(*this); return auto_ptr<Office>(np);}std::ostream & operator<<(std::ostream& os, Office o) { os << "-------------------------------------" << std::endl; os << o.id.name() << " = " << o.id << std::endl; os << o.type.name() << " = " << o.type << std::endl; os << "-------------------------------------" << std::endl; return os;}ExampleDatabase::ExampleDatabase(std::string backendType, std::string connInfo) : litesql::Database(backendType, connInfo) { initialize();}std::vector<litesql::Database::SchemaItem> ExampleDatabase::getSchema() const { vector<Database::SchemaItem> res; res.push_back(Database::SchemaItem("schema","table","CREATE TABLE schema (name TEXT, type TEXT, sql TEXT);")); res.push_back(Database::SchemaItem(Person::table__,"table","CREATE TABLE " + Person::table__ + " (" + Person::Id.name() + " " + backend->getRowIDType() + "," + Person::Type.name() + " " + Person::Type.type() + "," + Person::Name.name() + " " + Person::Name.type() + "," + Person::Age.name() + " " + Person::Age.type() + "," + Person::Sex.name() + " " + Person::Sex.type() + ")")); if (backend->supportsSequences()) res.push_back(Database::SchemaItem(Person::sequence__,"sequence","CREATE SEQUENCE " + Person::sequence__ + " START 1 INCREMENT 1")); res.push_back(Database::SchemaItem(Role::table__,"table","CREATE TABLE " + Role::table__ + " (" + Role::Id.name() + " " + backend->getRowIDType() + "," + Role::Type.name() + " " + Role::Type.type() + ")")); if (backend->supportsSequences()) res.push_back(Database::SchemaItem(Role::sequence__,"sequence","CREATE SEQUENCE " + Role::sequence__ + " START 1 INCREMENT 1")); res.push_back(Database::SchemaItem(Student::table__,"table","CREATE TABLE " + Student::table__ + " (" + Student::Id.name() + " " + backend->getRowIDType() + ")")); res.push_back(Database::SchemaItem(Employee::table__,"table","CREATE TABLE " + Employee::table__ + " (" + Employee::Id.name() + " " + backend->getRowIDType() + ")")); res.push_back(Database::SchemaItem(School::table__,"table","CREATE TABLE " + School::table__ + " (" + School::Id.name() + " " + backend->getRowIDType() + "," + School::Type.name() + " " + School::Type.type() + "," + School::Name.name() + " " + School::Name.type() + ")")); if (backend->supportsSequences()) res.push_back(Database::SchemaItem(School::sequence__,"sequence","CREATE SEQUENCE " + School::sequence__ + " START 1 INCREMENT 1")); res.push_back(Database::SchemaItem(Office::table__,"table","CREATE TABLE " + Office::table__ + " (" + Office::Id.name() + " " + backend->getRowIDType() + "," + Office::Type.name() + " " + Office::Type.type() + ")")); if (backend->supportsSequences()) res.push_back(Database::SchemaItem(Office::sequence__,"sequence","CREATE SEQUENCE " + Office::sequence__ + " START 1 INCREMENT 1")); res.push_back(Database::SchemaItem(PersonPersonRelationMother::table__,"table","CREATE TABLE " + PersonPersonRelationMother::table__ + " (" + PersonPersonRelationMother::Person1.name() + " " + PersonPersonRelationMother::Person1.type() + " UNIQUE"+ "," + PersonPersonRelationMother::Person2.name() + " " + PersonPersonRelationMother::Person2.type() + ")")); res.push_back(Database::SchemaItem("Person_Person_Mother_all_idx","index","CREATE INDEX Person_Person_Mother_all_idx ON " + PersonPersonRelationMother::table__ + " (" + PersonPersonRelationMother::Person1.name() + "," + PersonPersonRelationMother::Person2.name() + ")")); res.push_back(Database::SchemaItem("_c0c8d0dc7f86a3cc6937d28222104a7a","index","CREATE INDEX _c0c8d0dc7f86a3cc6937d28222104a7a ON " + PersonPersonRelationMother::table__ + " (" + PersonPersonRelationMother::Person1.name() + ")")); res.push_back(Database::SchemaItem("_0f81440c041fa3489398ae71deb027b1","index","CREATE INDEX _0f81440c041fa3489398ae71deb027b1 ON " + PersonPersonRelationMother::table__ + " (" + PersonPersonRelationMother::Person2.name() + ")")); res.push_back(Database::SchemaItem(PersonPersonRelationFather::table__,"table","CREATE TABLE " + PersonPersonRelationFather::table__ + " (" + PersonPersonRelationFather::Person1.name() + " " + PersonPersonRelationFather::Person1.type() + " UNIQUE"+ "," + PersonPersonRelationFather::Person2.name() + " " + PersonPersonRelationFather::Person2.type() + ")")); res.push_back(Database::SchemaItem("Person_Person_Father_all_idx","index","CREATE INDEX Person_Person_Father_all_idx ON " + PersonPersonRelationFather::table__ + " (" + PersonPersonRelationFather::Person1.name() + "," + PersonPersonRelationFather::Person2.name() + ")")); res.push_back(Database::SchemaItem("_43ca846fa15b609e2ae4ce976b7a4617","index","CREATE INDEX _43ca846fa15b609e2ae4ce976b7a4617 ON " + PersonPersonRelationFather::table__ + " (" + PersonPersonRelationFather::Person1.name() + ")")); res.push_back(Database::SchemaItem("_959568d5a34f5ab35945a55c66952156","index","CREATE INDEX _959568d5a34f5ab35945a55c66952156 ON " + PersonPersonRelationFather::table__ + " (" + PersonPersonRelationFather::Person2.name() + ")")); res.push_back(Database::SchemaItem(PersonPersonRelationSiblings::table__,"table","CREATE TABLE " + PersonPersonRelationSiblings::table__ + " (" + PersonPersonRelationSiblings::Person1.name() + " " + PersonPersonRelationSiblings::Person1.type()+ "," + PersonPersonRelationSiblings::Person2.name() + " " + PersonPersonRelationSiblings::Person2.type() + ")")); res.push_back(Database::SchemaItem("Person_Person_Siblings_all_idx","index","CREATE INDEX Person_Person_Siblings_all_idx ON " + PersonPersonRelationSiblings::table__ + " (" + PersonPersonRelationSiblings::Person1.name() + "," + PersonPersonRelationSiblings::Person2.name() + ")")); res.push_back(Database::SchemaItem("_a48ac42626f643991852d88a8674d00d","index","CREATE INDEX _a48ac42626f643991852d88a8674d00d ON " + PersonPersonRelationSiblings::table__ + " (" + PersonPersonRelationSiblings::Person1.name() + ")")); res.push_back(Database::SchemaItem("_298d9df0aa32109375541a40ec36a617","index","CREATE INDEX _298d9df0aa32109375541a40ec36a617 ON " + PersonPersonRelationSiblings::table__ + " (" + PersonPersonRelationSiblings::Person2.name() + ")")); res.push_back(Database::SchemaItem(PersonPersonRelationChildren::table__,"table","CREATE TABLE " + PersonPersonRelationChildren::table__ + " (" + PersonPersonRelationChildren::Person1.name() + " " + PersonPersonRelationChildren::Person1.type()+ "," + PersonPersonRelationChildren::Person2.name() + " " + PersonPersonRelationChildren::Person2.type() + ")")); res.push_back(Database::SchemaItem("Person_Person_Children_all_idx","index","CREATE INDEX Person_Person_Children_all_idx ON " + PersonPersonRelationChildren::table__ + " (" + PersonPersonRelationChildren::Person1.name() + "," + PersonPersonRelationChildren::Person2.name() + ")")); res.push_back(Database::SchemaItem("_9be68d6bdac0c8cd6bed8f79750553f4","index","CREATE INDEX _9be68d6bdac0c8cd6bed8f79750553f4 ON " + PersonPersonRelationChildren::table__ + " (" + PersonPersonRelationChildren::Person1.name() + ")")); res.push_back(Database::SchemaItem("_aca6c69b4153f661db55163b39d48fb2","index","CREATE INDEX _aca6c69b4153f661db55163b39d48fb2 ON " + PersonPersonRelationChildren::table__ + " (" + PersonPersonRelationChildren::Person2.name() + ")")); res.push_back(Database::SchemaItem(RoleRelation::table__,"table","CREATE TABLE " + RoleRelation::table__ + " (" + RoleRelation::Person.name() + " " + RoleRelation::Person.type()+ "," + RoleRelation::Role.name() + " " + RoleRelation::Role.type() + " UNIQUE" + ")")); res.push_back(Database::SchemaItem("Person_Role_Roles_all_idx","index","CREATE INDEX Person_Role_Roles_all_idx ON " + RoleRelation::table__ + " (" + RoleRelation::Person.name() + "," + RoleRelation::Role.name() + ")")); res.push_back(Database::SchemaItem("Person_Role_Roles_Person_idx","index","CREATE INDEX Person_Role_Roles_Person_idx ON " + RoleRelation::table__ + " (" + RoleRelation::Person.name() + ")")); res.push_back(Database::SchemaItem("Person_Role_Roles_Role_idx","index","CREATE INDEX Person_Role_Roles_Role_idx ON " + RoleRelation::table__ + " (" + RoleRelation::Role.name() + ")")); res.push_back(Database::SchemaItem(SchoolStudentRelation::table__,"table","CREATE TABLE " + SchoolStudentRelation::table__ + " (" + SchoolStudentRelation::School.name() + " " + SchoolStudentRelation::School.type()+ "," + SchoolStudentRelation::Student.name() + " " + SchoolStudentRelation::Student.type() + " UNIQUE" + ")")); res.push_back(Database::SchemaItem("School_Student__all_idx","index","CREATE INDEX School_Student__all_idx ON " + SchoolStudentRelation::table__ + " (" + SchoolStudentRelation::School.name() + "," + SchoolStudentRelation::Student.name() + ")")); res.push_back(Database::SchemaItem("School_Student__School_idx","index","CREATE INDEX School_Student__School_idx ON " + SchoolStudentRelation::table__ + " (" + SchoolStudentRelation::School.name() + ")")); res.push_back(Database::SchemaItem("School_Student__Student_idx","index","CREATE INDEX School_Student__Student_idx ON " + SchoolStudentRelation::table__ + " (" + SchoolStudentRelation::Student.name() + ")")); res.push_back(Database::SchemaItem(EmployeeOfficeRelation::table__,"table","CREATE TABLE " + EmployeeOfficeRelation::table__ + " (" + EmployeeOfficeRelation::Employee.name() + " " + EmployeeOfficeRelation::Employee.type()+ "," + EmployeeOfficeRelation::Office.name() + " " + EmployeeOfficeRelation::Office.type() + ")")); res.push_back(Database::SchemaItem("Employee_Office__all_idx","index","CREATE INDEX Employee_Office__all_idx ON " + EmployeeOfficeRelation::table__ + " (" + EmployeeOfficeRelation::Employee.name() + "," + EmployeeOfficeRelation::Office.name() + ")")); res.push_back(Database::SchemaItem("Employee_Office__Employee_idx","index","CREATE INDEX Employee_Office__Employee_idx ON " + EmployeeOfficeRelation::table__ + " (" + EmployeeOfficeRelation::Employee.name() + ")")); res.push_back(Database::SchemaItem("Employee_Office__Office_idx","index","CREATE INDEX Employee_Office__Office_idx ON " + EmployeeOfficeRelation::table__ + " (" + EmployeeOfficeRelation::Office.name() + ")")); return res;}void ExampleDatabase::initialize() { static bool initialized = false; if (initialized) return; initialized = true; Person::initValues();}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -