QStringList ModelGenerator::genUserModel(const QString &usernameField, const QString &passwordField) const
{
    QStringList ret;
    QPair<QStringList, QStringList> p = createModelParams();
    QString fileName = dstDir.filePath(modelName.toLower() + ".h");
    QString userVar = fieldNameToVariableName(usernameField);
    p.first << userVar << fieldNameToVariableName(passwordField);
    p.first << QLatin1String("    QString ") + USER_VIRTUAL_METHOD + "() const { return " + userVar + "(); }\n";

    gen(fileName, USER_MODEL_HEADER_FILE_TEMPLATE, p.first);
    ret << fileName;

    fileName = dstDir.filePath(modelName.toLower() + ".cpp");
    p.second << fieldNameToVariableName(usernameField) << fieldNameToVariableName(passwordField)
             << fieldNameToEnumName(usernameField);
    gen(fileName, USER_MODEL_IMPL_TEMPLATE, p.second);
    ret << fileName;
    return ret;
}
QString SqlObjGenerator::generate(const QString &dstDir)
{
    QList<QPair<QString, QString>> fieldList = tableSch->getFieldList();
    if (fieldList.isEmpty()) {
        qCritical("table not found, %s", qPrintable(tableSch->tableName()));
        return QString();
    }

    QString output;

    // Header part
    output += QString(SQLOBJECT_HEADER_TEMPLATE).arg(modelName.toUpper(), modelName);
    QListIterator<QPair<QString, QString>> it(fieldList);
    while (it.hasNext()) {
        const QPair<QString, QString> &p = it.next();
        if (isNumericType(p.second)) {
            output += QString("    %1 %2 {0};\n").arg(p.second, p.first);
        } else {
            output += QString("    %1 %2;\n").arg(p.second, p.first);
        }
    }

    // enum part
    output += QLatin1String("\n    enum PropertyIndex {\n");
    it.toFront();
    const QPair<QString, QString> &p = it.next();
    output += QString("        %1 = 0,\n").arg(fieldNameToEnumName(p.first));
    while (it.hasNext()) {
        const QPair<QString, QString> &p = it.next();
        output += QString("        %1,\n").arg(fieldNameToEnumName(p.first));
    }
    output += QLatin1String("    };\n\n");

    // primaryKeyIndex() method
    output += QLatin1String("    int primaryKeyIndex() const override { return ");
    QString pkName = tableSch->primaryKeyFieldName();
    if (pkName.isEmpty()) {
        output += QLatin1String("-1; }\n");
    } else {
        output += fieldNameToEnumName(pkName);
        output += QLatin1String("; }\n");
    }

    // auto-value field, for example auto-increment value
    output += QLatin1String("    int autoValueIndex() const override { return ");
    QString autoValue = tableSch->autoValueFieldName();
    if (autoValue.isEmpty()) {
        output += QLatin1String("-1; }\n");
    } else {
        output += fieldNameToEnumName(autoValue);
        output += QLatin1String("; }\n");
    }

    // tableName() method
    output += QLatin1String("    QString tableName() const override { return QStringLiteral(\"");
    output += tableSch->tableName();
    output += QLatin1String("\"); }\n\n");

    // Property macros part
    output += QLatin1String("private:    /*** Don't modify below this line ***/\n    Q_OBJECT\n");
    it.toFront();
    while (it.hasNext()) {
        const QPair<QString, QString> &p = it.next();
        output += QString(SQLOBJECT_PROPERTY_TEMPLATE).arg(p.second, p.first);
    }

    // Footer part
    output += QString(SQLOBJECT_FOOTER_TEMPLATE).arg(modelName.toUpper());

    // Writes to a file
    QDir dst = QDir(dstDir).filePath("sqlobjects");
    FileWriter fw(dst.filePath(modelName.toLower() + "object.h"));
    fw.write(output, false);
    return QLatin1String("sqlobjects/") + fw.fileName();
}
SqlObjGenerator::SqlObjGenerator(const QString &model, const QString &table)
    : tableSch(new TableSchema(table))
{
    modelName = (!model.isEmpty()) ? model : fieldNameToEnumName(table);
}
ControllerGenerator::ControllerGenerator(const QString &controller, const QStringList &actions)
    : controllerName(fieldNameToEnumName(controller)), actionList(actions)
{ }
MailerGenerator::MailerGenerator(const QString &name, const QStringList &actions, const QString &dst)
    :  actionList(actions), dstDir(dst)
{
    mailerName = fieldNameToEnumName(name);
}
ValidatorGenerator::ValidatorGenerator(const QString &validator, const QString &dst)
    : dstDir(dst)
{
    name = fieldNameToEnumName(validator);
    name.remove(QRegExp("validator$", Qt::CaseInsensitive));
}
QPair<QStringList, QStringList> ModelGenerator::createModelParams() const
{
    QString setgetDecl;
    QString setgetImpl;
    QString crtparams;
    QString getOptDecl;
    QString getOptImpl;
    QString initParams;
    QList<QPair<QString, QString> > writableFields;
    bool optlockMethod = false;
    TableSchema ts(tableName);
    QList<QPair<QString, QString> > fieldList = ts.getFieldList();
    QString autoFieldName = ts.autoValueFieldName();

    for (QListIterator<QPair<QString, QString> > it(fieldList); it.hasNext(); ) {
        const QPair<QString, QString> &p = it.next();
        QString var = fieldNameToVariableName(p.first);

        // Getter method
        setgetDecl += QString("    %1 %2() const;\n").arg(p.second, var);
        setgetImpl += QString("%1 %2::%3() const\n{\n    return d->%4;\n}\n\n").arg(p.second, modelName, var, p.first);

        if (!excludedSetter()->contains(p.first, Qt::CaseInsensitive) && p.first != autoFieldName) {
            // Setter method
            QString str = fieldNameToEnumName(p.first);         
            setgetDecl += QString("    void set%1(%2);\n").arg(str, createParam(p.second, p.first));
            setgetImpl += QString("void %1::set%2(%3)\n{\n    d->%4 = %5;\n}\n\n").arg(modelName, str, createParam(p.second, p.first), p.first, var);

            // Appends to crtparams-string
            crtparams += createParam(p.second, p.first);
            crtparams += ", ";

            writableFields << QPair<QString, QString>(p.first, p.second);

            if (p.second == "int" || p.second == "float" || p.second == "double") {
                initParams += QString("\n    d->") + p.first + " = 0;";
            }
        }
        
        if (p.first == LOCK_REVISION_FIELD) 
            optlockMethod = true;
    }
    crtparams.chop(2);

    initParams += (initParams.isEmpty()) ? ' ' : '\n';

    // Creates parameters of get() method
    int idx = ts.primaryKeyIndex();
    QString getparams;
    if (idx < 0) {
        getparams = crtparams;
    } else {
        const QPair<QString, QString> &pair = fieldList[idx];
        getparams = createParam(pair.second, pair.first);
    }

    // Creates a declaration and a implementation of 'get' method for optimistic lock
    if (idx >= 0 &&optlockMethod) {
        const QPair<QString, QString> &pair = fieldList[idx];
        getOptDecl = QString("    static %1 get(%2, int lockRevision);\n").arg(modelName, createParam(pair.second, pair.first));

        getOptImpl = QString("%1 %1::get(%2, int lockRevision)\n"       \
                             "{\n"                                      \
                             "    TSqlORMapper<%1Object> mapper;\n"     \
                             "    TCriteria cri;\n"                     \
                             "    cri.add(%1Object::%3, %4);\n"         \
                             "    cri.add(%1Object::LockRevision, lockRevision);\n" \
                             "    return %1(mapper.findFirst(cri));\n"  \
                             "}\n\n").arg(modelName, createParam(pair.second, pair.first), fieldNameToEnumName(pair.first), fieldNameToVariableName(pair.first));
    }

    QStringList headerArgs;
    headerArgs << modelName.toUpper() << modelName << setgetDecl << crtparams << getparams << getOptDecl;

    // Creates a model implementation
    QString createImpl;
    createImpl += QString("    %1Object obj;\n").arg(modelName);

    QListIterator<QPair<QString, QString> > fi(writableFields);
    while (fi.hasNext()) {
        const QPair<QString, QString> &p = fi.next();
        createImpl += QString("    obj.%1 = %2;\n").arg(p.first, fieldNameToVariableName(p.first));
    }
    createImpl += "    if (!obj.create()) {\n";
    createImpl += "        obj.clear();\n";
    createImpl += "    }\n";
    createImpl += QString("    return %1(obj);\n").arg(modelName);

    // Creates a implementation of get() method
    QString getImpl;
    if (idx < 0) {
        // If no primary index exists
        getImpl += QString("    TCriteria cri;\n");
        fi.toFront();
        while (fi.hasNext()) {
            const QPair<QString, QString> &p = fi.next();
            getImpl += QString("    cri.add(%1Object::%2, %3);\n").arg(modelName, fieldNameToEnumName(p.first), fieldNameToVariableName(p.first));
        }
    }
    
    getImpl += QString("    TSqlORMapper<%1Object> mapper;\n").arg(modelName);
    getImpl += QString("    return %1(mapper.").arg(modelName);

    if (idx < 0) {
        getImpl += "findFirst(cri));\n";
    } else {
        const QPair<QString, QString> &pair = fieldList[idx];
        getImpl += QString("findByPrimaryKey(%1));\n").arg(fieldNameToVariableName(pair.first));
    }

    QStringList implArgs;
    implArgs << modelName.toLower() << modelName << initParams << setgetImpl << crtparams << createImpl << getparams << getImpl << getOptImpl;

    return QPair<QStringList, QStringList>(headerArgs, implArgs);
}
ModelGenerator::ModelGenerator(const QString &model, const QString &table, const QStringList &fields, const QString &dst)
    : dstDir(dst), fieldList(fields)
{
    tableName = (table.contains('_')) ? table.toLower() : variableNameToFieldName(table);
    modelName = (!model.isEmpty()) ? model : fieldNameToEnumName(tableName);
}
Beispiel #9
0
QStringList ViewGenerator::generateViews() const
{
    QStringList files;
    TableSchema ts(tableName);
    if (ts.primaryKeyIndex() < 0) {
        qWarning("Primary key not found. [table name: %s]", qPrintable(ts.tableName()));
        return files;
    }
    
    FileWriter fw;
    QString output;
    QString caption = fieldNameToCaption(tableName);
    QString className = fieldNameToEnumName(tableName);
    QString varName = fieldNameToVariableName(tableName);
    QPair<QString, QString> pkFld = ts.getPrimaryKeyField();

    // Generates index.html
    QString th ,td, indexOtm, showColumn, showOtm, entryColumn, editColumn, editOtm;
    QList<QPair<QString, QString> > fields = ts.getFieldList();
    for (QListIterator<QPair<QString, QString> > i(fields); i.hasNext(); ) {
        const QPair<QString, QString> &p = i.next();
        QString cap = fieldNameToCaption(p.first);
        QString var = fieldNameToVariableName(p.first);
        QString readonly;

        if (!excludedColumn()->contains(p.first)) {
            th += "    <th>";
            th += cap;
            th += "</th>\n";
            td += "    <td data-tf=\"#";
            td += var;
            td += "\"></td>\n";
            indexOtm += QString("#%1 ~= i.%1()\n\n").arg(var);
            entryColumn += QString("  <p>\n    <label for=\"%1_%2\">%3</label><br />\n    <input id=\"%1_%2\" name=\"%1[%2]\" />\n  </p>\n").arg(varName, var, cap);
            if  (p.first == pkFld.first) {
                readonly = QLatin1String("readonly ");
            }
            editColumn += QString("  <p>\n    <label for=\"%1_%2\">%3</label><br />\n    <input id=\"%1_%2\" %4data-tf=\"#%2\" />\n  </p>\n").arg(varName, var, cap, readonly);
            editOtm += QString("#%1 |== inputTextTag(\"%2[%1]\", %2.%1());\n\n").arg(var, varName);
        }
        showColumn += QString("<p>\n  <b>%1:</b>\n  <span data-tf=\"#%2\">(%2)</span>\n</p>\n").arg(cap, var);
        showOtm += QString("#%1 := %2.%1()\n\n").arg(var, varName);
    }
    
    output = QString(INDEX_HTML_TEMPLATE).arg(caption, th, td);
    fw.setFileName(dstDir.filePath("index.html"));
    if (fw.write(output, false)) {
        files << fw.fileName();
    }

    // Generates index.otm
    QString pkVarName = fieldNameToVariableName(pkFld.first);
    output = QString(INDEX_OTM_TEMPLATE).arg(varName.toLower(), className, varName, indexOtm, pkVarName);
    fw.setFileName(dstDir.filePath("index.otm"));
    if (fw.write(output, false)) {
        files << fw.fileName();
    }

    // Generates show.html
    output = QString(SHOW_HTML_TEMPLATE).arg(showColumn);
    fw.setFileName(dstDir.filePath("show.html"));
    if (fw.write(output, false)) {
        files << fw.fileName();
    }

    // Generates show.otm
    output = QString(SHOW_OTM_TEMPLATE).arg(varName.toLower(), className, varName, showOtm, pkVarName);
    fw.setFileName(dstDir.filePath("show.otm"));
    if (fw.write(output, false)) {
        files << fw.fileName();
    }

    // Generates entry.html
    output = QString(ENTRY_HTML_TEMPLATE).arg(caption, entryColumn);
    fw.setFileName(dstDir.filePath("entry.html"));
    if (fw.write(output, false)) {
        files << fw.fileName();
    }

    // Generates entry.otm
    output = QString(ENTRY_OTM_TEMPLATE).arg(varName.toLower());
    fw.setFileName(dstDir.filePath("entry.otm"));
    if (fw.write(output, false)) {
        files << fw.fileName();
    }

    // Generates edit.html
    output = QString(EDIT_HTML_TEMPLATE).arg(caption, editColumn);
    fw.setFileName(dstDir.filePath("edit.html"));
    if (fw.write(output, false)) {
        files << fw.fileName();
    }

    // Generates edit.otm
    output = QString(EDIT_OTM_TEMPLATE).arg(varName.toLower(), className, varName, pkVarName, editOtm);
    fw.setFileName(dstDir.filePath("edit.otm"));
    if (fw.write(output, false)) {
        files << fw.fileName();
    }

    return files;
}