Beispiel #1
0
bool ModelObject::save()
{
    QSqlQuery query;
    QStringList fields;
    QStringList questionMarks;
    QList<QVariant> values;

    foreach (auto field, mModel->fields())
        if (field != "id")
        {
            fields.append(field);
            values.append(mData[field]);
            questionMarks.append("?");
        }

    if (id() == -1)
    {
        auto fieldString = fields.join(", ");
        auto questionMarkString = questionMarks.join(", ");

        query.prepare(QString("INSERT INTO %1(%2) VALUES(%3)")
                      .arg(mModel->databaseTable()).arg(fieldString).arg(questionMarkString));
        foreach (auto value, values)
            query.addBindValue(value);

        mModel->mBotInterface->executeDatabaseQuery(query);

        mData["id"] = query.lastInsertId().toLongLong();

        return BotUtils::getNumRowsAffected(query);
    }
Beispiel #2
0
        // primary key
        if (fieldName == m_pkName)
        {
            fieldSql += " PRIMARY KEY";
            // auto-increment is backend specific
            if (autoIncrement)
                fieldSql += QDjango::autoIncrementSql();
        }

        propSql << fieldSql;
    }

    // create table
    QSqlQuery createQuery(db);
    createQuery.prepare(QString("CREATE TABLE %1 (%2)").arg(QDjango::quote(databaseTable()), propSql.join(", ")));
    if (!sqlExec(createQuery))
        return false;

    // create index
    if (m_pkName != "id")
    {
        QString indexName = m_pkName;
        QSqlQuery indexQuery(db);
        indexQuery.prepare(QString("CREATE UNIQUE INDEX %1 ON %2 (%3)").arg(indexName, QDjango::quote(databaseTable()), m_pkName));
        if (!sqlExec(indexQuery))
            return false;
    }
    return true;
}