static int compare_recs(const QSqlRecord* buf1, const QSqlRecord* buf2, const QSqlIndex& idx) { int cmp = 0; int i = 0; const QString fn(idx.field(i).name()); const QSqlField f1 = buf1->field(fn); if (f1.isValid()) { switch (f1.type()) { // ### more types? case QVariant::String: cmp = f1.value().toString().trimmed().compare( buf2->value(fn).toString().trimmed()); break; default: if (f1.value().toDouble() < buf2->value(fn).toDouble()) cmp = -1; else if (f1.value().toDouble() > buf2->value(fn).toDouble()) cmp = 1; } } if (idx.isDescending(i)) cmp = -cmp; return cmp; }
int TableSchema::primaryKeyIndex() const { QSqlTableModel model; model.setTable(tablename); QSqlIndex index = model.primaryKey(); if (index.isEmpty()) { return -1; } QSqlField fi = index.field(0); return model.record().indexOf(fi.name()); }
QString TableSchema::primaryKeyFieldName() const { QSqlTableModel model; model.setTable(tablename); QSqlIndex index = model.primaryKey(); if (index.isEmpty()) { return QString(); } QSqlField fi = index.field(0); return fi.name(); }
static bool index_matches(const Q3SqlCursor* cur, const QSqlRecord* buf, const QSqlIndex& idx) { bool indexEquals = false; for (int i = 0; i < idx.count(); ++i) { const QString fn(idx.field(i).name()); if (cur->value(fn) == buf->value(fn)) indexEquals = true; else { indexEquals = false; break; } } return indexEquals; }
bool QgsDb2GeometryColumns::populateLayerProperty( QgsDb2LayerProperty &layer ) { if ( !mQuery.isActive() || !mQuery.next() ) { return false; } layer.schemaName = mQuery.value( 0 ).toString().trimmed(); layer.tableName = mQuery.value( 1 ).toString().trimmed(); layer.geometryColName = mQuery.value( 2 ).toString().trimmed(); layer.type = mQuery.value( 3 ).toString(); if ( mQuery.value( 4 ).isNull() ) { layer.srid.clear(); layer.srsName.clear(); } else { layer.srid = mQuery.value( 4 ).toString(); layer.srsName = mQuery.value( 5 ).toString(); } layer.extents = QStringLiteral( "0 0 0 0" ); // no extents if ( ENV_LUW == mEnvironment ) { if ( !mQuery.value( 6 ).isNull() ) // Don't get values if null { layer.extents = QString( mQuery.value( 6 ).toString() + ' ' + mQuery.value( 7 ).toString() + ' ' + mQuery.value( 8 ).toString() + ' ' + mQuery.value( 9 ).toString() ).trimmed(); } } QgsDebugMsg( QStringLiteral( "layer: %1.%2(%3) type='%4' srid='%5' srsName='%6'" ) .arg( layer.schemaName, layer.tableName, layer.geometryColName, layer.type, layer.srid, layer.srsName ) ); QgsDebugMsg( "Extents: '" + layer.extents + "'" ); layer.pkCols = QStringList(); // Use the Qt functionality to get the primary key information // to set the FID column. // We can only use the primary key if it only has one column and // the type is Integer or BigInt. QString table = QStringLiteral( "%1.%2" ).arg( layer.schemaName, layer.tableName ); QSqlIndex pk = mDatabase.primaryIndex( table ); if ( pk.count() == 1 ) { QSqlField pkFld = pk.field( 0 ); QVariant::Type pkType = pkFld.type(); if ( ( pkType == QVariant::Int || pkType == QVariant::LongLong ) ) { QString fidColName = pk.fieldName( 0 ); layer.pkCols.append( fidColName ); QgsDebugMsg( "pk is: " + fidColName ); } } else { QgsDebugMsg( "Warning: table primary key count is " + QString::number( pk.count() ) ); } layer.pkColumnName = layer.pkCols.size() > 0 ? layer.pkCols.at( 0 ) : QString(); return true; }