⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 litesql-gen-cpp.cpp

📁 LiteSQL is a C++ library that integrates C++ objects tightly to relational database and thus provide
💻 CPP
📖 第 1 页 / 共 4 页
字号:
        params.push_back("owner->getDatabase()");        params.push_back("expr");        del.param(exprParam).body(rel->getName() + "::del(" + params.join(", ") + ");");        hcl.method(cons).method(link).method(unlink).method(del);        string extraExpr = " && srcExpr";        if (handle.destObjects.size() == 1) {            xml::Object* dest = handle.destObjects[0].first;            xml::Relate* relate = handle.destObjects[0].second;            Method get("get", "litesql::DataSource<" + dest->name + ">");            get.param(exprParam).param(srcExprParam);                        params.clear();            params.push_back("owner->getDatabase()");            params.push_back("expr");            params.push_back("(" + rel->getName() + "::" + handle.relate->fieldTypeName + " == owner->id)"                             + extraExpr);                        get                .body("return " + rel->getName() + "::" + relate->getMethodName                      + brackets(params.join(", ")) + ";");                        hcl.method(get);        } else {            if (rel->sameTypes() <= 2) {                Method getTpl("get", "litesql::DataSource<T>");                getTpl.template_("class T").defineOnly()                    .param(exprParam).param(srcExprParam);                hcl.method(getTpl);                                for (size_t i2 = 0; i2 < handle.destObjects.size(); i2++) {                    xml::Object* dest = handle.destObjects[i2].first;                    xml::Relate* relate = handle.destObjects[i2].second;                    Method get("get",                                "litesql::DataSource<" + dest->name + ">");                    get.templateSpec("").param(exprParam).param(srcExprParam);                    params.clear();                    params.push_back("owner->getDatabase()");                    params.push_back("expr");                    params.push_back("(" + rel->getName() + "::" + handle.relate->fieldTypeName + " == owner->id)"                                     + extraExpr);                    get.body("return " + rel->getName() + "::" + relate->getMethodName                              + brackets(params.join(", ")) + ";");                    hcl.method(get);                 }                } else {                for (size_t i2 = 0; i2 < handle.destObjects.size(); i2++) {                    xml::Object* dest = handle.destObjects[i2].first;                    xml::Relate* relate = handle.destObjects[i2].second;                    string num = toString(i2 + 1);                    Method get("get" + dest->name + num,                                "litesql::DataSource<" + dest->name + ">");                    get.param(exprParam).param(srcExprParam);                    params.clear();                    params.push_back("owner->getDatabase()");                    params.push_back("expr");                    params.push_back("(" + rel->getName() + "::" + handle.relate->fieldTypeName + " == owner->id)"                                     + extraExpr);                    get.body("return " + rel->getName() + "::" + relate->getMethodName                              + brackets(params.join(", ")) + ";");                    hcl.method(get);                }            }        }        Method getRows("getRows", "litesql::DataSource<"                        + rel->getName() + "::Row>");        getRows.param(exprParam)            .body("return " + rel->getName()                  + "::getRows(owner->getDatabase(), "                  "expr && (" + rel->getName() + "::"                   + handle.relate->fieldTypeName +                  " == owner->id));");        hcl.method(getRows);        cl.class_(hcl);        Method hdlMethod(handle.name, o.name + "::" + className);        hdlMethod.body("return " + o.name + "::" +  className + "(*this);");        cl.method(hdlMethod);    }}void writeObjBaseMethods(Class& cl, const xml::Object& o) {    Method insert("insert", "std::string");    insert.protected_()        .param(Variable("tables", "litesql::Record&"))        .param(Variable("fieldRecs", "litesql::Records&"))        .param(Variable("valueRecs", "litesql::Records&"))        .body("tables.push_back(table__);")        .body("litesql::Record fields;")        .body("litesql::Record values;");    if (o.parentObject)        insert.body("fields.push_back(\"id_\");")            .body("values.push_back(id);");    for (size_t i = 0; i < o.fields.size(); i++) {        const xml::Field& f = o.fields[i];        insert.body("fields.push_back(" + f.name + ".name()" + ");");        insert.body("values.push_back(" + f.name + ");");        insert.body(f.name + ".setModified(false);");    }        insert.body("fieldRecs.push_back(fields);")        .body("valueRecs.push_back(values);");    if (o.parentObject) {        insert.body("return " + o.inherits                     + "::insert(tables, fieldRecs, valueRecs);");    } else        insert.body("return " + o.inherits                    + "::insert(tables, fieldRecs, valueRecs, "                     + "sequence__);");    Method create("create", "void");    create.protected_();    create.body("litesql::Record tables;")        .body("litesql::Records fieldRecs;")        .body("litesql::Records valueRecs;")        .body("type = type__;")        .body("std::string newID = insert(tables, fieldRecs, valueRecs);")        .body("if (id == 0)")        .body("    id = newID;");    Method addUpdates("addUpdates", "void");    addUpdates.protected_().virtual_()        .param(Variable("updates", "Updates&"))        .body("prepareUpdate(updates, table__);");    for (size_t i = 0; i < o.fields.size(); i++) {        const xml::Field& f = o.fields[i];        addUpdates.body("updateField(updates, table__, " + f.name + ");");    }    if (o.parentObject)         addUpdates.body(o.inherits + "::addUpdates(updates);");    Method addIDUpdates("addIDUpdates", "void");        addIDUpdates.protected_().virtual_()        .param(Variable("updates", "Updates&"));    if (o.parentObject) {        addIDUpdates            .body("prepareUpdate(updates, table__);")            .body("updateField(updates, table__, id);");        if (o.parentObject->parentObject)            addIDUpdates.body(o.inherits + "::addIDUpdates(updates);");    }        Method getFieldTypes("getFieldTypes", "void");    getFieldTypes.static_().        param(Variable("ftypes", "std::vector<litesql::FieldType>&"));    if (o.parentObject)         getFieldTypes.body(o.parentObject->name + "::getFieldTypes(ftypes);");    for (size_t i = 0; i < o.fields.size(); i++)        getFieldTypes.body("ftypes.push_back(" + o.fields[i].fieldTypeName + ");");        Method update("update", "void");    update.virtual_()        .body("if (!inDatabase) {")        .body("    create();")        .body("    return;")        .body("}")        .body("Updates updates;")        .body("addUpdates(updates);")        .body("if (id != oldKey) {")        .body("    if (!typeIsCorrect()) ")        .body("        upcastCopy()->addIDUpdates(updates);");    if (o.parentObject)         update            .body("    else")            .body("        addIDUpdates(updates);");        update        .body("}")        .body("litesql::Persistent::update(updates);")        .body("oldKey = id;");    Method delRecord("delRecord", "void");    delRecord.protected_().virtual_()        .body("deleteFromTable(table__, id);");    if (o.parentObject)        delRecord.body(o.inherits + "::delRecord();");    Method delRelations("delRelations", "void");    delRelations.protected_().virtual_();    for (map<xml::Relation*, vector<xml::Relate*> >::const_iterator i =              o.relations.begin(); i != o.relations.end(); i++) {        const xml::Relation * rel = i->first;        const vector<xml::Relate *> relates = i->second;        Split params;        for (size_t i2 = 0; i2 < relates.size(); i2++)            params.push_back("(" + rel->getName() + "::" + relates[i2]->fieldTypeName                             + " == id)");        delRelations.body(rel->getName() + "::del(*db, " + params.join(" || ") + ");");    }                Method del("del", "void");    del.virtual_()        .body("if (typeIsCorrect() == false) {")        .body("    std::auto_ptr<" + o.name + "> p(upcastCopy());")        .body("    p->delRelations();")        .body("    p->onDelete();")        .body("    p->delRecord();")        .body("} else {")        .body("    onDelete();")        .body("    delRecord();")        .body("}")        .body("inDatabase = false;");    Method typeIsCorrect("typeIsCorrect", "bool");        typeIsCorrect.body("return type == type__;").virtual_();        Method upcast("upcast", "std::auto_ptr<" + o.name + ">");    Method upcastCopy("upcastCopy", "std::auto_ptr<" + o.name + ">");    Split names;    o.getChildrenNames(names);    upcastCopy.body(o.name + "* np = NULL;");    for (size_t i = 0; i < names.size(); i++) {        upcast.body("if (type == " + names[i] + "::type__)")            .body("    return auto_ptr<" + o.name + ">(new " + names[i]                   + "(select<" + names[i]                   + ">(*db, Id == id).one()));");        upcastCopy.body("if (type == " + quote(names[i]) + ")")            .body("    np = new " + names[i] + "(*db);");    }    for (size_t i = 0; i < o.fields.size(); i++) {        upcastCopy.body("np->" + o.fields[i].name + " = "                         + o.fields[i].name + ";");    }    upcastCopy        .body("np->inDatabase = inDatabase;")        .body("if (!np)")        .body("    np = new " + o.name + "(*this);")        .body("return auto_ptr<" + o.name + ">(np);");    upcast.body("return auto_ptr<" + o.name                 + ">(new " + o.name + "(*this));");    for (size_t i = 0; i < o.methods.size(); i++) {        const xml::Method& m = o.methods[i];        Method mtd(m.name, m.returnType);        for (size_t i2 = 0; i2 < m.params.size(); i2++) {            const xml::Param& p = m.params[i2];            mtd.param(Variable(p.name, p.type));                }        mtd.defineOnly().virtual_();        cl.method(mtd);    }        cl.method(insert).method(create).method(addUpdates)        .method(addIDUpdates).method(getFieldTypes).method(delRecord).method(delRelations)         .method(update).method(del)        .method(typeIsCorrect).method(upcast).method(upcastCopy);}void writeStaticRelData(Class& cl, xml::Relation& r) {    Variable table("table__", "const std::string",                    quote(makeTableName(r.getTable())));    table.static_();    cl.variable(table);    Method initValues("initValues", "void");    bool hasValues = false;    initValues.static_();    bool same = r.sameTypes() > 1;    for (size_t i2 = 0; i2 < r.related.size(); i2++) {        string num;        if (same)            num = toString(i2 + 1);                r.related[i2].fieldTypeName = r.related[i2].objectName + num;        Variable ftype(r.related[i2].fieldTypeName,                       "const litesql::FieldType",                       quote(r.related[i2].objectName                              + toString(i2 + 1))                       + "," + quote("INTEGER") + "," + "table__");        ftype.static_();        cl.variable(ftype);    }    for (size_t i2 = 0; i2 < r.fields.size(); i2++) {        xml::Field& fld = r.fields[i2];        string data = quote(fld.name + "_") + "," +                      quote(fld.getSQLType()) + "," +                      "table__";        if (!fld.values.empty()) {            data += "," + fld.name + "_values";             Variable values(fld.name + "_values",                             "std::vector < std::pair< std::string, std::string > >");            values.static_().protected_();            cl.variable(values);            initValues.body(fld.name + "_values.clear();");            hasValues = true;            for (size_t i2 = 0; i2 < fld.values.size(); i2++) {                xml::Value& val = fld.values[i2];                initValues.body(fld.name + "_values.push_back(make_pair("                          + quote(val.name) + "," + quote(val.value) + "));");                }        }        string ftypeClass ="const litesql::FieldType";        if (!fld.values.empty()) {            ftypeClass = fld.fieldTypeName + "Type";            Class ftypeCl(ftypeClass, "litesql::FieldType");            Method cons(ftypeClass);            cons.param(Variable("n", "const string&"))                .param(Variable("t", "const string&"))                .param(Variable("tbl", "const string&"))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -