Example #1
0
QString QDjangoCompiler::databaseColumn(const QString &name)
{
    QDjangoMetaModel model = baseModel;
    QString modelPath;
    QString modelRef = referenceModel(QString(), &model);
    QStringList bits = name.split(QLatin1String("__"));

    while (bits.size() > 1) {
        const QByteArray fk = bits.first().toLatin1();
        QDjangoMetaModel foreignModel;
        if (!model.foreignFields().contains(fk)) {
            // this might be a reverse relation, so look for the model
            // and if it exists continue
            foreignModel = QDjango::metaModel(fk);
            if (!foreignModel.isValid())
                break;

            reverseModelRefs[bits.first()] = foreignModel.primaryKey();
        } else {
            foreignModel = QDjango::metaModel(model.foreignFields()[fk]);
        }

        // store reference
        if (!modelPath.isEmpty())
            modelPath += QLatin1String("__");
        modelPath += bits.first();
        modelRef = referenceModel(modelPath, &foreignModel);

        model = foreignModel;
        bits.takeFirst();
    }

    const QDjangoMetaField field = model.localField(bits.join(QLatin1String("__")).toLatin1());
    return modelRef + QLatin1Char('.') + driver->escapeIdentifier(field.column(), QSqlDriver::FieldName);
}
Example #2
0
QString QDjangoCompiler::databaseColumn(const QString &name)
{
    QDjangoMetaModel model = baseModel;
    QString modelName;
    QString modelPath;
    QString modelRef = referenceModel(QString(), &model, false);
    QStringList bits = name.split(QLatin1String("__"));

    while (bits.size() > 1) {
        const QByteArray fk = bits.first().toLatin1();
        QDjangoMetaModel foreignModel;
        bool foreignNullable = false;

        if (!modelPath.isEmpty())
            modelPath += QLatin1String("__");
        modelPath += bits.first();

        if (!model.foreignFields().contains(fk)) {
            // this might be a reverse relation, so look for the model
            // and if it exists continue
            foreignModel = QDjango::metaModel(fk);
            QDjangoReverseReference rev;
            const QMap<QByteArray, QByteArray> foreignFields = foreignModel.foreignFields();
            foreach (const QByteArray &foreignKey, foreignFields.keys()) {
                if (foreignFields[foreignKey] == baseModel.className()) {
                    rev.leftHandKey = foreignModel.localField(foreignKey + "_id").column();
                    break;
                }
            }

            if (rev.leftHandKey.isEmpty()) {
                qWarning() << "Invalid field lookup" << name;
                return QString();
            }
            rev.rightHandKey = foreignModel.primaryKey();
            reverseModelRefs[modelPath] = rev;
        } else {
void tst_QDjangoMetaModel::testOptions()
{
    QStringList sql;
    if (QDjango::database().driverName() == QLatin1String("QPSQL")) {
        sql << QLatin1String(
            "CREATE TABLE \"some_table\" ("
                "\"id\" serial PRIMARY KEY, "
                "\"aField\" integer NOT NULL, "
                "\"b_field\" integer NOT NULL, "
                "\"blankField\" integer NOT NULL, "
                "\"indexField\" integer NOT NULL, "
                "\"nullField\" integer, "
                "\"uniqueField\" integer NOT NULL UNIQUE, "
                "UNIQUE (\"aField\", \"b_field\")"
            ")");
        sql << QLatin1String("CREATE INDEX \"some_table_ac243651\" ON \"some_table\" (\"indexField\")");
    } else if (QDjango::database().driverName() == QLatin1String("QMYSQL") ||
               QDjango::database().driverName() == QLatin1String("QMYSQL3")) {
        sql << QLatin1String(
            "CREATE TABLE `some_table` ("
                "`id` integer NOT NULL PRIMARY KEY AUTO_INCREMENT, "
                "`aField` integer NOT NULL, "
                "`b_field` integer NOT NULL, "
                "`blankField` integer NOT NULL, "
                "`indexField` integer NOT NULL, "
                "`nullField` integer, "
                "`uniqueField` integer NOT NULL UNIQUE, "
                "UNIQUE (`aField`, `b_field`)"
                ")");
        sql << QLatin1String("CREATE INDEX `some_table_ac243651` ON `some_table` (`indexField`)");
    } else {
        sql << QLatin1String(
            "CREATE TABLE \"some_table\" ("
                "\"id\" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "
                "\"aField\" integer NOT NULL, "
                "\"b_field\" integer NOT NULL, "
                "\"blankField\" integer NOT NULL, "
                "\"indexField\" integer NOT NULL, "
                "\"nullField\" integer, "
                "\"uniqueField\" integer NOT NULL UNIQUE, "
                "UNIQUE (\"aField\", \"b_field\")"
            ")");
        sql << QLatin1String("CREATE INDEX \"some_table_ac243651\" ON \"some_table\" (\"indexField\")");
    }

    init<tst_Options>(sql);

    QDjangoMetaField metaField;
    const QDjangoMetaModel metaModel = QDjango::registerModel<tst_Options>();

    metaField = metaModel.localField("aField");
    QCOMPARE(metaField.isAutoIncrement(), false);
    QCOMPARE(metaField.isBlank(), false);
    QCOMPARE(metaField.isNullable(), false);
    QCOMPARE(metaField.isUnique(), false);
    QCOMPARE(metaField.isValid(), true);

    metaField = metaModel.localField("bField");
    QCOMPARE(metaField.isAutoIncrement(), false);
    QCOMPARE(metaField.isBlank(), false);
    QCOMPARE(metaField.isNullable(), false);
    QCOMPARE(metaField.isUnique(), false);
    QCOMPARE(metaField.isValid(), true);

    metaField = metaModel.localField("blankField");
    QCOMPARE(metaField.isAutoIncrement(), false);
    QCOMPARE(metaField.isBlank(), true);
    QCOMPARE(metaField.isNullable(), false);
    QCOMPARE(metaField.isUnique(), false);
    QCOMPARE(metaField.isValid(), true);

    metaField = metaModel.localField("nullField");
    QCOMPARE(metaField.isAutoIncrement(), false);
    QCOMPARE(metaField.isBlank(), false);
    QCOMPARE(metaField.isNullable(), true);
    QCOMPARE(metaField.isUnique(), false);
    QCOMPARE(metaField.isValid(), true);

    metaField = metaModel.localField("uniqueField");
    QCOMPARE(metaField.isAutoIncrement(), false);
    QCOMPARE(metaField.isBlank(), false);
    QCOMPARE(metaField.isNullable(), false);
    QCOMPARE(metaField.isUnique(), true);
    QCOMPARE(metaField.isValid(), true);

    cleanup<tst_Options>();
}