bool QSqlRecordProto::isGenerated(int index) const
{
  QSqlRecord *item = qscriptvalue_cast<QSqlRecord*>(thisObject());
  if (item)
    return item->isGenerated(index);
  return false;
}
bool QSqlRecordProto::isGenerated(const QString &name) const
{
  QSqlRecord *item = qscriptvalue_cast<QSqlRecord*>(thisObject());
  if (item)
    return item->isGenerated(name);
  return false;
}
Example #3
0
bool QSqlTableModelPrivate::exec(const QString &stmt, bool prepStatement,
                                 const QSqlRecord &rec, const QSqlRecord &whereValues)
{
    if (stmt.isEmpty())
        return false;

    // lazy initialization of editQuery
    if (editQuery.driver() != db.driver())
        editQuery = QSqlQuery(db);

    // workaround for In-Process databases - remove all read locks
    // from the table to make sure the editQuery succeeds
    if (db.driver()->hasFeature(QSqlDriver::SimpleLocking))
        const_cast<QSqlResult *>(query.result())->detachFromResultSet();

    if (prepStatement) {
        if (editQuery.lastQuery() != stmt) {
            if (!editQuery.prepare(stmt)) {
                error = editQuery.lastError();
                return false;
            }
        }
        int i;
        for (i = 0; i < rec.count(); ++i) {
            if (rec.isGenerated(i) && rec.value(i).type() != QVariant::Invalid)
                editQuery.addBindValue(rec.value(i));
        }
        for (i = 0; i < whereValues.count(); ++i) {
            if (whereValues.isGenerated(i))
                editQuery.addBindValue(whereValues.value(i));
        }

        if (!editQuery.exec()) {
            error = editQuery.lastError();
            return false;
        }
    } else {
        if (!editQuery.exec(stmt)) {
            error = editQuery.lastError();
            return false;
        }
    }
    return true;
}
/*! \internal
 */
void QSqlRelationalTableModelPrivate::translateFieldNames(QSqlRecord &values) const
{
    for (int i = 0; i < values.count(); ++i) {
        if (relations.value(i).isValid()) {
            QVariant v = values.value(i);
            bool gen = values.isGenerated(i);
            values.replace(i, baseRec.field(i));
            values.setValue(i, v);
            values.setGenerated(i, gen);
        }
    }
}
/*!
    Applies \a values to the \a row in the model. The source and
    target fields are mapped by field name, not by position in
    the record.

    Note that the generated flags in \a values are preserved
    and determine whether the corresponding fields are used when
    changes are submitted to the database. The caller should
    remember to set the generated flag to FALSE for fields
    where the database is meant to supply the value, such as an
    automatically incremented ID.

    For edit strategies OnFieldChange and OnRowChange, a row may
    receive a change only if no other row has a cached change.
    Changes are submitted immediately. Submitted changes are not
    reverted upon failure.

    Returns \c true if all the values could be set; otherwise returns
    false.

    \sa record(), editStrategy()
*/
bool QSqlTableModel::setRecord(int row, const QSqlRecord &values)
{
    Q_D(QSqlTableModel);
    Q_ASSERT_X(row >= 0, "QSqlTableModel::setRecord()", "Cannot set a record to a row less than 0");
    if (d->busyInsertingRows)
        return false;

    if (row >= rowCount())
        return false;

    if (d->cache.value(row).op() == QSqlTableModelPrivate::Delete)
        return false;

    if (d->strategy != OnManualSubmit && d->cache.value(row).submitted() && isDirty())
        return false;

    // Check field names and remember mapping
    typedef QMap<int, int> Map;
    Map map;
    for (int i = 0; i < values.count(); ++i) {
        int idx = d->nameToIndex(values.fieldName(i));
        if (idx == -1)
            return false;
        map[i] = idx;
    }

    QSqlTableModelPrivate::ModifiedRow &mrow = d->cache[row];
    if (mrow.op() == QSqlTableModelPrivate::None)
        mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update,
                                                  QSqlQueryModel::record(row));

    Map::const_iterator i = map.constBegin();
    const Map::const_iterator e = map.constEnd();
    for ( ; i != e; ++i) {
        // have to use virtual setData() here rather than mrow.setValue()
        EditStrategy strategy = d->strategy;
        d->strategy = OnManualSubmit;
        QModelIndex cIndex = createIndex(row, i.value());
        setData(cIndex, values.value(i.key()));
        d->strategy = strategy;
        // setData() sets generated to TRUE, but source record should prevail.
        if (!values.isGenerated(i.key()))
            mrow.recRef().setGenerated(i.value(), false);
    }

    if (d->strategy != OnManualSubmit)
        return submit();

    return true;
}
Example #6
0
/*! \internal
 */
void QSqlRelationalTableModelPrivate::translateFieldNames(int row, QSqlRecord &values) const
{
    Q_Q(const QSqlRelationalTableModel);

    for (int i = 0; i < values.count(); ++i) {
        int realCol = q->indexInQuery(q->createIndex(row, i)).column();
        if (realCol != -1 && relations.value(realCol).isValid()) {
            QVariant v = values.value(i);
            bool gen = values.isGenerated(i);
            values.replace(i, baseRec.field(realCol));
            values.setValue(i, v);
            values.setGenerated(i, gen);
        }
    }
}
/*!
\since 5.0
    Returns the record at \a row in the model.

    If \a row is the index of a valid row, the record
    will be populated with values from that row.

    If the model is not initialized, an empty record will be
    returned.

    \sa QSqlRecord::isEmpty()
*/
QSqlRecord QSqlTableModel::record(int row) const
{
    Q_D(const QSqlTableModel);

    // the query gets the values from virtual data()
    QSqlRecord rec = QSqlQueryModel::record(row);

    // get generated flags from the cache
    const QSqlTableModelPrivate::ModifiedRow mrow = d->cache.value(row);
    if (mrow.op() != QSqlTableModelPrivate::None) {
        const QSqlRecord crec = mrow.rec();
        for (int i = 0, cnt = rec.count(); i < cnt; ++i)
            rec.setGenerated(i, crec.isGenerated(i));
    }

    return rec;
}
Example #8
0
/*!
    \fn MPeluqueria::agregarServicio( QVariant desc, QVariant precio, QVariant fecha, QVariant id_mascota )
 */
bool MPeluqueria::agregarServicio( QVariant desc, QVariant precio, QVariant fecha, QVariant id_mascota )
{
/* if( !mascota.isValid() )
 {
   qDebug( "Inidce Mascota es invalido" );
   return false;
 }
 QVariant id_mascota = relationModel( 1 )->data( mascota, 0 );
 if( id_mascota.isValid() && id_mascota.toInt() <= 0 )
 {return false;}*/
 QSqlRecord registro = record();
 registro.setValue( 1, id_mascota );
 registro.setValue( 2, desc );
 registro.setValue( 3, precio );
 registro.setValue( 4, fecha );
 for( int i = 0; i<registro.count(); i++ )
 {
   qDebug( QString( "Campo: %1, generado? %2, null? %3, valor? |%4|" ).arg( registro.fieldName( i ) ).arg( registro.isGenerated( i ) ).arg( registro.isNull( i ) ).arg( registro.value( i ).toString() ).toLocal8Bit() );
 }
 if( insertRecord( -1, registro ) )
 {
   qDebug( "Registro de servicio agregado correctamente" );
   return true;
 }
 else
 {
   qDebug( "Error al insertar registro de servicio de mascota" );
   qDebug( QString( "Detalles: tipo: %1, errno: %2, descripcion: %3" ).arg( lastError().type() ).arg( lastError().number() ).arg( lastError().text() ).toLocal8Bit() );
   return false;
 }
}
Example #9
0
/*!
    Returns a SQL statement of type \a type for the table \a tableName
    with the values from \a rec. If \a preparedStatement is true, the
    string will contain placeholders instead of values.

    This method can be used to manipulate tables without having to worry
    about database-dependent SQL dialects. For non-prepared statements,
    the values will be properly escaped.
*/
QString QSqlDriver::sqlStatement(StatementType type, const QString &tableName,
                                 const QSqlRecord &rec, bool preparedStatement) const
{
    int i;
    QString s;
    s.reserve(128);
    switch (type) {
    case SelectStatement:
        for (i = 0; i < rec.count(); ++i) {
            if (rec.isGenerated(i))
                s.append(escapeIdentifier(rec.fieldName(i), FieldName)).append(QLatin1String(", "));
        }
        if (s.isEmpty())
            return s;
        s.chop(2);
        s.prepend(QLatin1String("SELECT ")).append(QLatin1String(" FROM ")).append(escapeIdentifier(tableName, TableName));
        break;
    case WhereStatement:
        if (preparedStatement) {
            for (int i = 0; i < rec.count(); ++i) {
                s.append(escapeIdentifier(rec.fieldName(i), FieldName));
                if (rec.isNull(i))
                    s.append(QLatin1String(" IS NULL"));
                else
                    s.append(QLatin1String(" = ?"));
                s.append(QLatin1String(" AND "));
            }
        } else {
            for (i = 0; i < rec.count(); ++i) {
                s.append(escapeIdentifier(rec.fieldName(i), FieldName));
                QString val = formatValue(rec.field(i));
                if (val == QLatin1String("NULL"))
                    s.append(QLatin1String(" IS NULL"));
                else
                    s.append(QLatin1String(" = ")).append(val);
                s.append(QLatin1String(" AND "));
            }
        }
        if (!s.isEmpty()) {
            s.prepend(QLatin1String("WHERE "));
            s.chop(5); // remove tailing AND
        }
        break;
    case UpdateStatement:
        s.append(QLatin1String("UPDATE ")).append(escapeIdentifier(tableName, TableName)).append(
                 QLatin1String(" SET "));
        for (i = 0; i < rec.count(); ++i) {
            if (!rec.isGenerated(i) || !rec.value(i).isValid())
                continue;
            s.append(escapeIdentifier(rec.fieldName(i), FieldName)).append(QLatin1Char('='));
            if (preparedStatement)
                s.append(QLatin1Char('?'));
            else
                s.append(formatValue(rec.field(i)));
            s.append(QLatin1String(", "));
        }
        if (s.endsWith(QLatin1String(", ")))
            s.chop(2);
        else
            s.clear();
        break;
    case DeleteStatement:
        s.append(QLatin1String("DELETE FROM ")).append(escapeIdentifier(tableName, TableName));
        break;
    case InsertStatement: {
        s.append(QLatin1String("INSERT INTO ")).append(escapeIdentifier(tableName, TableName)).append(QLatin1String(" ("));
        QString vals;
        for (i = 0; i < rec.count(); ++i) {
            if (!rec.isGenerated(i) || !rec.value(i).isValid())
                continue;
            s.append(escapeIdentifier(rec.fieldName(i), FieldName)).append(QLatin1String(", "));
            if (preparedStatement)
                vals.append(QLatin1String("?"));
            else
                vals.append(formatValue(rec.field(i)));
            vals.append(QLatin1String(", "));
        }
        if (vals.isEmpty()) {
            s.clear();
        } else {
            vals.chop(2); // remove trailing comma
            s[s.length() - 2] = QLatin1Char(')');
            s.append(QLatin1String("VALUES (")).append(vals).append(QLatin1String(")"));
        }
        break; }
    }
    return s;
}